系列列表:
1 首先在Linux系统安装Apache
2 建立初始代码工作区
mkdir -p ~/codes/apache/mods/mod_sample/src
cd ~/codes/apache/mods/mod_sample/src
vi mod_sample.c
3 编写mod_sample.c
#include "httpd.h"
#include "http_core.h"
#include "http_protocol.h"
#include "http_request.h"
class Handler
{
public:
virtual int handle(request_rec* r) = 0;
};
class HelloHandler : public Handler
{
public:
virtual int handle(request_rec* r)
{
// The first thing we will do is write a simple "Hello, world!" back to the client.
ap_rputs("Hello, world!<br/>", r);
return OK;
}
};
/* The handler function for our module.
* This is where all the fun happens!
*/
static int example_handler(request_rec *r)
{
/* First off, we need to check if this is a call for the "example" handler.
* If it is, we accept it and do our things, it not, we simply return DECLINED,
* and Apache will try somewhere else.
*/
if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);
//concrete one handler object and handle the request.
HelloHandler hh;
return hh.handle(r);
}
/* register_hooks: Adds a hook to the httpd process */
static void register_hooks(apr_pool_t *pool)
{
/* Hook the request handler */
ap_hook_handler(example_handler, NULL, NULL, APR_HOOK_LAST);
}
/* Define our module as an entity and assign a function for registering hooks */
module AP_MODULE_DECLARE_DATA example_module =
{
STANDARD20_MODULE_STUFF,
NULL, // Per-directory configuration handler
NULL, // Merge handler for per-directory configurations
NULL, // Per-server configuration handler
NULL, // Merge handler for per-server configurations
NULL, // Any directives we may have for httpd
register_hooks // Our hook registering function
};
4 直接使用apxs编译并安装进apache中
~/publishes/httpd/bin/apxs -i -a -c -Wc,-lstdc++ -Wc,-xc++ mod_sample.c
5 修改apache conf文件以设置hello rewrite 映射关系
vi ~/publishes/httpd/conf/httpd.conf
</Directory>
<Location "/example">
SetHandler example-handler
</Location>
6 重新运行apache以测试接口
~/publishes/httpd/bin/apachectl restart
curl http://localhost/example