查看文章 |
编写第一个Apache模块——mod_helloworld
2008-11-08 14:20
自Apache2.0以来Apache就是一个开放的平台,开发人员可以轻松的为Apache编写模块。我下面要讲的例子是摘自Apache Modules Book一书。 mod_helloworld是一个内容生成模块(content generator),它及其简单,但是他却可以反映编写Apache模块的基本概念。在以后的博文中,我会扩展这个helloworld模块,让它可以像简单的CGI一样访问request header、环境变量、处理表单数据等等。不过现在让我们尽快的开始我们的helloworld吧。 安装Apache 要编写Apache模块首先肯定要按照Aapche,这里选择从源码安装,笔者在写这篇文章时Apache的版本是2.2.10。 解压 tar zvxf httpd-2.2.10.tar.gz 配置 ./configure --prefix=/usr/local/apache2 --enable-so --enable-mods-shared=most 编译安装 make make install (超级用户) 功能 当用户在浏览器中输入http://www.xxx.com/hellworld/时,helloworld模块会在浏览器上显示一个大大的Hello Apache module的字样。 编写模块 好了,我们现在已经有环境了,bin目录下有个对开发人员最重要的工具apxs,它其实是个脚本,用来方便我们编译安装apache模块。 模块框架 所有Apache模块都要将它的module数据结构导出(exporting),对Apache2.x模块来说,导出module的形式如下: module AP_MODULE_DECLARE_DATA some_module = { STANDARD20_MODULE_STUFF, some_dir_cfg, /* create per-directory config struct */ some_dir_merge, /* merge per-directory config struct */ some_svr_cfg, /* create per-host config struct */ some_svr_merge, /* merge per-host config struct */ some_cmds, /* configuration directives for this module */ some_hooks /* register module's hooks/etc. with the core */ }; STANDARD20_MODULE_STUFF宏展开,它提供了模块的版本信息。在这里我们不对他做深入的研究,有兴趣了解的可以阅读Apache的源代码。 对于helloworld模块,我们这里只考虑模块的最后一个字段,他注册模块个钩子(hooks)。helloworld模块的module结构如下: module AP_MODULE_DECLARE_DATA helloworld_module = { STANDARD20_MODULE_STUFF, NULL, NULL, NULL, NULL, NULL, helloworld_hooks }; 现在我们来写钩子注册函数,它会在Apache服务启动时调用。它的用途是把我们的处理函数注册给服务器核心(server core),这样模块的函数就会在适当的时机得到调用。对于helloworld来说,我们这样仅仅注册一个”内容生成“类型的钩子(Apache有很多类型的钩子) static void helloworld_hooks(apr_pool_t *pool) { ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE); } 最后当然是实现处理函数helloworld_handler啦。它是一个回调函数,他会在Apache处理HTTP请求的适当时机调用。处理函数可以选择处理或是忽略这个请求。 static int helloworld_handler(request_rec *r) { if (!r->handler || strcmp(r->handler, "helloworld")) { return DECLINED; } if (r->method_number != M_GET) { return HTTP_METHOD_NOT_ALLOWED; } ap_set_content_type(r, "text/html;charset=ascii"); ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n", r); ap_rputs("<html><head><title>Hello Apache Module</title></head>", r); ap_rputs("<body><h1>Hello Apache Module</h1>", r); ap_rputs("</body></html>", r); return OK; } 我们开始做一些检查,来决定模块是处理这个请求还是忽略它。返回DECLINED表示忽略,OK表示成功处理,也可返回HTTP状态代码(HTTP status code)表示错误处理。 全部放在一起 /* The simplest HelloWorld module */ #include <httpd.h> #include <http_protocol.h> #include <http_config.h> static int helloworld_handler(request_rec *r) { if (!r->handler || strcmp(r->handler, "helloworld")) { return DECLINED; } if (r->method_number != M_GET) { return HTTP_METHOD_NOT_ALLOWED; } ap_set_content_type(r, "text/html;charset=ascii"); ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n", r); ap_rputs("<html><head><title>Hello Apache Module</title></head>", r); ap_rputs("<body><h1>Hello Apache Module</h1>", r); ap_rputs("</body></html>", r); return OK; } static void helloworld_hooks(apr_pool_t *pool) { ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE); } module AP_MODULE_DECLARE_DATA helloworld_module = { STANDARD20_MODULE_STUFF, NULL, NULL, NULL, NULL, NULL, helloworld_hooks }; 注意:helloworld_hooks和helloworld_handler都是static的。一般来说,只有模块结构被导出,其他的都是模块自己私有的。这是一个好的实践。 编译安装 apxs -c mod_helloworld.c (编译) apxs -i mod_helloworld.la (安装) 这样我们的modules目录中就多了一个mod_helloworld.so的文件 运行 要运行我们还要修改配置文件httpd.conf 在最后添加 LoadModule helloworld_module modules/mod_helloworld.so <Location /helloworld> SetHandler helloworld </Location> 第一行是让Apache动态的载入这个模块。 第二行设置浏览器的请求url的路径。 SetHandler helloworld其实是设置r->handler的值。 别忘了重启Apache哦! 最后在浏览器中敲入http://localhost/hellworld,就可以看到令人兴奋的效果了。 |
最近读者: