首先声明我对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的请求照样工作。