Apache2.0模块开发基础

本博文是总结《Apache模块开发》中的第5章

基本说明

mod_helloworld是一个非常简单的模块,仅仅完成内容展示工作。但是它却可以展示编写Apache模块的基本框架。

基本开发环境及说明

我的开发环境是ubuntu 10.04 amd64 server,apache版本是2.2.15

1、源码安装apache 
解压
tar zvxf httpd-2.2.15.tar.gz

配置
./configure --prefix=/usr/local/apache2 --enable-module=so --enable-module=rewrite  --enable-module=env --enable-shared=max

编译安装
make; make install

2、功能
当用户在浏览器中输入http://www.xxx.com/hellworld/时,helloworld模块会在浏览器上显示一个大大的Hello Apache module的字样。

编写模块

1、模块框架
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)表示错误处理。

2、完整代码
/* 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的。一般来说,只有模块结构被导出,其他的都是模块自己私有的。这是一个好的实践

3、编译安装模块
/usr/local/apache2/bin/apxs -iac mod_helloworld.c
这样我们的modules目录中就多了一个mod_helloworld.so的文件

4、运行
要运行我们还要修改配置文件httpd.conf
添加如下:
LoadModule helloworld_module modules/mod_helloworld.so
<Location /helloworld>
    SetHandler helloworld
</Location>
第一行是让Apache动态的载入这个模块。
第二行设置浏览器的请求url的路径。
SetHandler helloworld其实是设置r->handler的值。

重启apache
最后在浏览器中敲入http://localhost/hellworld,就可以看到模块的效果了。








猜你喜欢

转载自blog.csdn.net/reesun/article/details/8266357