首先聲明我對apache mod編寫一點也不熟悉。只是弄mod_perl的時候查閱文章的時候看到相關文檔。自己做了個試驗,純粹爲了娛樂而已:)
--文件 mod_foo.c
#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的請求照樣工作。