Apache2.0模块开发基础
来源:互联网 发布:java 颜色选择控件 编辑:程序博客网 时间:2024/05/17 05:05
本博文是总结《Apache模块开发》中的第5章
基本说明
mod_helloworld是一个非常简单的模块,仅仅完成内容展示工作。但是它却可以展示编写Apache模块的基本框架。
基本开发环境及说明
我的开发环境是ubuntu 10.04 amd64 server,apache版本是2.2.15
1、源码安装apache
解压
tar zvxf httpd-2.2.15.tar.gz
tar zvxf httpd-2.2.15.tar.gz
配置
./configure --prefix=/usr/local/apache2 --enable-module=so --enable-module=rewrite --enable-module=env --enable-shared=max
./configure --prefix=/usr/local/apache2 --enable-module=so --enable-module=rewrite --enable-module=env --enable-shared=max
编译安装
make; make install
2、功能
当用户在浏览器中输入http://www.xxx.com/hellworld/时,helloworld模块会在浏览器上显示一个大大的Hello Apache module的字样。编写模块
1、模块框架
module AP_MODULE_DECLARE_DATA some_module = { STANDARD20_MODULE_STUFF, some_dir_cfg, /* create per-directory config struct */ some_dir_merge, /* merge per-directory config struct */ some_svr_cfg, /* create per-host config struct */ some_svr_merge, /* merge per-host config struct */ some_cmds, /* configuration directives for this module */ some_hooks /* register module's hooks/etc. with the core */};STANDARD20_MODULE_STUFF宏展开,它提供了模块的版本信息。在这里我们不对他做深入的研究,有兴趣了解的可以阅读Apache的源代码。
对于helloworld模块,我们这里只考虑模块的最后一个字段,他注册模块个钩子(hooks)。helloworld模块的module结构如下:
module AP_MODULE_DECLARE_DATA helloworld_module = { STANDARD20_MODULE_STUFF, NULL, NULL, NULL, NULL, NULL, helloworld_hooks};现在我们来写钩子注册函数,它会在Apache服务启动时调用。它的用途是把我们的处理函数注册给服务器核心(server core),这样模块的函数就会在适当的时机得到调用。对于helloworld来说,我们这样仅仅注册一个”内容生成“类型的钩子(Apache有很多类型的钩子)
static void helloworld_hooks(apr_pool_t *pool){ ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);}最后当然是实现处理函数helloworld_handler啦。它是一个回调函数,他会在Apache处理HTTP请求的适当时机调用。处理函数可以选择处理或是忽略这个请求。
static int helloworld_handler(request_rec *r){ if (!r->handler || strcmp(r->handler, "helloworld")) { return DECLINED; } if (r->method_number != M_GET) { return HTTP_METHOD_NOT_ALLOWED; } ap_set_content_type(r, "text/html;charset=ascii"); ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n", r); ap_rputs("<html><head><title>Hello Apache Module</title></head>", r); ap_rputs("<body><h1>Hello Apache Module</h1>", r); ap_rputs("</body></html>", r); return OK;}我们开始做一些检查,来决定模块是处理这个请求还是忽略它。返回DECLINED表示忽略,OK表示成功处理,也可返回HTTP状态代码(HTTP status code)表示错误处理。
2、完整代码
/* The simplest HelloWorld module */#include <httpd.h>#include <http_protocol.h>#include <http_config.h>static int helloworld_handler(request_rec *r){ if (!r->handler || strcmp(r->handler, "helloworld")) { return DECLINED; } if (r->method_number != M_GET) { return HTTP_METHOD_NOT_ALLOWED; } ap_set_content_type(r, "text/html;charset=ascii"); ap_rputs("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n", r); ap_rputs("<html><head><title>Hello Apache Module</title></head>", r); ap_rputs("<body><h1>Hello Apache Module</h1>", r); ap_rputs("</body></html>", r); return OK;}static void helloworld_hooks(apr_pool_t *pool){ ap_hook_handler(helloworld_handler, NULL, NULL, APR_HOOK_MIDDLE);}module AP_MODULE_DECLARE_DATA helloworld_module = { STANDARD20_MODULE_STUFF, NULL, NULL, NULL, NULL, NULL, helloworld_hooks};
注意:helloworld_hooks和helloworld_handler都是static的。一般来说,只有模块结构被导出,其他的都是模块自己私有的。这是一个好的实践
3、编译安装模块
/usr/local/apache2/bin/apxs -iac mod_helloworld.c这样我们的modules目录中就多了一个mod_helloworld.so的文件
4、运行
要运行我们还要修改配置文件httpd.conf添加如下:
LoadModule helloworld_module modules/mod_helloworld.so<Location /helloworld> SetHandler helloworld</Location>第一行是让Apache动态的载入这个模块。
第二行设置浏览器的请求url的路径。
SetHandler helloworld其实是设置r->handler的值。
重启apache
最后在浏览器中敲入http://localhost/hellworld,就可以看到模块的效果了。
- Apache2.0模块开发基础
- APACHE2.0 MOD 模块开发 STEP 1
- DSO开发指南晋级(APACHE2.0 MOD 模块开发)
- apache2.4模块开发学习
- Apache2.0过滤器开发
- Apache2.0过滤器开发
- Apache2.0过滤器开发
- Apache2.0过滤器开发
- 内核模块开发基础
- 内核模块开发基础
- apache2开启重写模块
- linux内核模块开发基础
- apache2.0 prefork和worker模块性能调优
- apache2.0 prefork和worker模块性能调优
- Titanium-Modules 模块开发 (一) :模块开发基础
- c apache2模块开发--根据自定义业务逻辑实现文件下载
- apache2 开发C++模块 —— 基于cximage实现图片、缩略图下载功能
- Apache2.4开启Rewrite模块
- Android 中LayoutInflater的使用
- 云计算的基本原理
- QT与OpenGL之旋转
- 对候选人的票的统计程序。
- 帮帮忙了,我是初学者在学习Struts2时关于文件上传时的错误,不知道怎么去改了,让我花费一下午的时间还是不对,一直提示我配置不对
- Apache2.0模块开发基础
- assert()函数用法总结
- Ubuntu(Linux)使用alt+c关闭显示器命令
- 全能的权限管理框架(一)——基础表的建立与说明
- 二叉树基本功能的汇集(C++类实现)
- Visual Basic编程实现Excel表操作
- 每天一算法(三色棋)
- 使用JavaScript防止页面内容被赋值,可兼容多个浏览器
- 动态分配的数组的初始化,sizeof长度