Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1839282
  • 博文数量: 333
  • 博客积分: 10791
  • 博客等级: 上将
  • 技术积分: 4314
  • 用 户 组: 普通用户
  • 注册时间: 2007-08-08 07:39
文章分类

全部博文(333)

文章存档

2015年(1)

2011年(116)

2010年(187)

2009年(25)

2008年(3)

2007年(1)

分类: C/C++

2010-04-13 17:38:11

本文是对Apache模块结构的基本说明,为了更好的诠释Apache模块的架构,这里有一个helloworld模块,其超链接为:helloworld模块

对于每个Apache模块,都会分为三大部分:

一、模块的声明,每个Apache模块都会输出一个数据结构。如下所示:

module AP_MODULE_DECLARE_DATA _module = {
    STANDARD20_MODULE_STUFF,
    dir_cfg,           //创建目录配置结构
    dir_merge,         //合并目录配置结构
    svr_cfg,         //创建主机配置结构
    svr_merge,         //合并主机配置结构
    cmds,         //为模块配置相关指令
    _hooks         //注册模块的钩子程序
};

在helloworld模块中,这个数据结构是这样定义的:

module AP_MODULE_DECLARE_DATA helloworld_module = {
    STANDARD20_MODULE_STUFF,
    NULL,
    NULL,
    NULL,
    NULL,
    NULL,
    helloworld_hooks
};


二、注册模块的钩子程序。

static void <regist_name>_hooks(apr_pool_t *pool)
{
    ap_hook_handler(<module_name>_handler, NULL, NULL, APR_HOOK_MIDDLE);
}

在helloworld模块中,钩子是这样定义的:

static void helloworld_hooks(apr_pool_t *pool)
{
    ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);
}


三、处理器代码,即在注册模块的钩子程序中调用的“_handler”程序。在helloworld模块中,处理程序是这样的:

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("\n", r);
    ap_rputs("Hello Apache Module", r);
    ap_rputs("

Hello Apache Module

", r);
    ap_rputs(" ", r);
    return OK;
}


阅读(2472) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~