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的請求照樣工作。

發佈了63 篇原創文章 · 獲贊 2 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章