(3:一个非常简单的apache module)Apache模块开发/用C语言扩展apache
时间:2007-09-02
来源:互联网
from: http://www.loveopensource.com/?p=18
Apache模块开发/用C语言扩展apache(3:一个非常简单的apache module)
by linux_prog
有了上面几篇文章的基础,大家自己再下点功夫,应该可以去写一些简单的模块了,
下面贴出一个很简单的apache module,大家一起分析一下。
$ cd /usr/local/apache2.2.4
$ vi mod_c.c
#include
#include
#include “apr.h”
#include “apr_lib.h”
#include “apr_strings.h”
#define APR_WANT_STRFUNC
#include “apr_want.h”
#include “httpd.h”
#include “http_config.h”
#include “http_core.h”
#include “http_request.h”
module AP_MODULE_DECLARE_DATA c_module;
static int c_handler(request_rec *r)
{
r->content_type=”text/plain”;
ap_rprintf(r,”handler:%s\n”,r->handler);
ap_rprintf(r,”query string:%s\n”,r->args);
ap_rprintf(r,”filename:%s\n”,r->filename);
return OK;
}
static void register_hooks(apr_pool_t *p)
{
ap_hook_handler(c_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* module structure */
module AP_MODULE_DECLARE_DATA c_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 */
};
编译并安装这个模块(apache提供的apxs非常好):
$ ./bin/apxs -c ./mod_c.c
$ ./bin/apxs -a -i -n c mod_c.la
这时apxs会自动帮我们把编译好的mod_c.so安装到modules/目录中,而且httpd.conf中已经把这个module load进去了:
[root@cn-weblog apache2.2.4]# grep mod_c conf/httpd.conf
LoadModule c_module modules/mod_c.so
测试这个模块:
$ ./bin/apachectl stop
$ ./bin/apachectl start
在IE中访问http://myhostname/index.html?query=yy
IE中会出现:
handler:text/html
query string:query=yy
filename:/usr/local/apache2.2.4/htdocs/index.html
说明该module运行成功。
把上面的module简单解释一下。
所有的apache module都必须是这个结构体,里面要定义各个内容。
/* module structure */
module AP_MODULE_DECLARE_DATA c_module = {
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger ― default is to override */
NULL, /* server config */
NULL, /* merge server configs */
//上面4项都是定义httpd.conf中命令的作用的
NULL, /* command apr_table_t */ //定义在httpd.conf中添加的命令,和各命令的处理函数
register_hooks /* register hooks */ //hooks,定义什么时候执行我们这个module的相关函数
};
ap_hook_handler(c_handler, NULL, NULL, APR_HOOK_MIDDLE);
表示在处理内容请求时调用我们函数�Cc_handler
http://httpd.apache.org/docs/2.2/developer/
提供了非常不错的文档,可以参考一下。
Apache模块开发/用C语言扩展apache(3:一个非常简单的apache module)
by linux_prog
有了上面几篇文章的基础,大家自己再下点功夫,应该可以去写一些简单的模块了,
下面贴出一个很简单的apache module,大家一起分析一下。
$ cd /usr/local/apache2.2.4
$ vi mod_c.c
#include
#include
#include “apr.h”
#include “apr_lib.h”
#include “apr_strings.h”
#define APR_WANT_STRFUNC
#include “apr_want.h”
#include “httpd.h”
#include “http_config.h”
#include “http_core.h”
#include “http_request.h”
module AP_MODULE_DECLARE_DATA c_module;
static int c_handler(request_rec *r)
{
r->content_type=”text/plain”;
ap_rprintf(r,”handler:%s\n”,r->handler);
ap_rprintf(r,”query string:%s\n”,r->args);
ap_rprintf(r,”filename:%s\n”,r->filename);
return OK;
}
static void register_hooks(apr_pool_t *p)
{
ap_hook_handler(c_handler, NULL, NULL, APR_HOOK_MIDDLE);
}
/* module structure */
module AP_MODULE_DECLARE_DATA c_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 */
};
编译并安装这个模块(apache提供的apxs非常好):
$ ./bin/apxs -c ./mod_c.c
$ ./bin/apxs -a -i -n c mod_c.la
这时apxs会自动帮我们把编译好的mod_c.so安装到modules/目录中,而且httpd.conf中已经把这个module load进去了:
[root@cn-weblog apache2.2.4]# grep mod_c conf/httpd.conf
LoadModule c_module modules/mod_c.so
测试这个模块:
$ ./bin/apachectl stop
$ ./bin/apachectl start
在IE中访问http://myhostname/index.html?query=yy
IE中会出现:
handler:text/html
query string:query=yy
filename:/usr/local/apache2.2.4/htdocs/index.html
说明该module运行成功。
把上面的module简单解释一下。
所有的apache module都必须是这个结构体,里面要定义各个内容。
/* module structure */
module AP_MODULE_DECLARE_DATA c_module = {
STANDARD20_MODULE_STUFF,
NULL, /* dir config creater */
NULL, /* dir merger ― default is to override */
NULL, /* server config */
NULL, /* merge server configs */
//上面4项都是定义httpd.conf中命令的作用的
NULL, /* command apr_table_t */ //定义在httpd.conf中添加的命令,和各命令的处理函数
register_hooks /* register hooks */ //hooks,定义什么时候执行我们这个module的相关函数
};
ap_hook_handler(c_handler, NULL, NULL, APR_HOOK_MIDDLE);
表示在处理内容请求时调用我们函数�Cc_handler
http://httpd.apache.org/docs/2.2/developer/
提供了非常不错的文档,可以参考一下。
作者: gleon 发布时间: 2007-09-01
:L
作者: thankwsx 发布时间: 2007-09-01
引用:
原帖由 thankwsx 于 2007-9-1 23:00 发表:L
作者: gleon 发布时间: 2007-09-01
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28