Apache Module简单一例

首先声明我对apache mod编写一点也不熟悉。只是弄mod_perl的时候查阅文章的时候看到相关文档。自己做了个试验,纯粹为了娱乐而已:)

--文件 mod_foo.c

 

#include  " httpd.h "
#include 
" http_config.h "
#include 
" http_core.h "
#include 
" http_request.h "
#include 
" apr.h "
#include 
" apr_lib.h "
#include 
" apr_strings.h "
#include <stdio.h>

module AP_MODULE_DECLARE_DATA foo_module
;
static int foo_handler(request_rec *r)
{
    if ( strcmp(r->uri
, " /foomod.html " ) == 0  )
    {
        r->content_type
= " text/plain " ;
        ap_rprintf(r , " handler:%s " , r->handler) ;
        ap_rprintf(r , " query string:%s " , r->args) ;
        ap_rprintf(r , " filename:%s " , r->filename) ;
        return OK ;
    }
    else {
        return DECLINED
;
    }
}

static void register_hooks(apr_pool_t *p)
{
    ap_hook_handler(foo_handler
,  NULL ,  NULL ,  APR_HOOK_MIDDLE) ;
}

/* module structure */
module AP_MODULE_DECLARE_DATA foo_module 
=  {
    STANDARD20_MODULE_STUFF
,
    NULL
,       /* dir config creater */
    NULL
,                        /* dir merger — default is to override */
    NULL
,       /* server config */
    NULL
,                        /* merge server configs */
    NULL
,       /* command apr_table_t */
    register_hooks    /* register hooks */
}
;

 

接着用 /usr/local/apache2/bin/apxs -i -a -c mod_foo.c  编译这个文件。结果成功之后就会在apache的module目录下生成 mod_foo.so 这个动态库。

修改apache的主配文件httpd.conf 添加LoadModule foo_module         modules/mod_foo.so

重新启动apache。

访问http://xxx/foomod.html?test=12345 就可以看到效果。

注:这个只对foomod.html文件处理。其它client的请求照样工作。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值