开发Apache模块例子

来源:互联网 发布:mac os 版本 编辑:程序博客网 时间:2024/04/27 17:31

目的:原打算是为Apache挂一个钩子看请求消息内容的,学习了如何为Apache增加模块,就把测试代码贴与此。

步骤:1. 编写代码,代码见下面

         2.编译动态库

         3.在httpd.conf增加模块

         4.运行Aapche

1.测试代码如下(放到httpd编译源代码下,我是放到了httpd-2.0.63/test下):

 

#include"httpd.h"

#include"http_config.h"

#include"http_protocol.h"

#include"http_log.h"

#include"ap_compat.h"

#include"apr_strings.h"

 

static int test_handle(request_rec* r)

{

    printf("%s : %d, invoke %s /n", __FILE__, __LINE__, __FUNCTION__);

    return DECLINED;

}

 

static void register_hooks(apr_pool_t* p)

{

    printf("%s : %d, invoke %s /n", __FILE__, __LINE__, __FUNCTION__);

    

    ap_hook_handler(test_handle, NULL, NULL, APR_HOOK_FIRST);

}

 

AP_DECLARE_DATA module test_module = {

    STANDARD20_MODULE_STUFF,

    NULL,

    NULL,

    NULL,

    NULL,

    NULL,

    register_hooks

};

2.编译工程如下:
top_srcdir   = /home/httpd-2.0.63
top_builddir = /home//httpd-2.0.63
srcdir       = /home/httpd-2.0.63/test
builddir     = /home/ttpd-2.0.63/test
VPATH        = /home/httpd-2.0.63/test
OS_DIR=unix
ALL_INCLUDE = -I$(top_srcdir)/srclib/apr/include /
  -I$(top_srcdir)/srclib/apr-util/include /
  -I. /
  -I$(top_srcdir)/os/$(OS_DIR) /
  -I$(top_srcdir)/modules/http /
  -I$(top_srcdir)/modules/filters /
  -I$(top_srcdir)/modules/proxy /
  -I$(top_srcdir)/include 
  
test: test.o
gcc -shared -o test_module.so test.o
test.o:test.c
gcc -g -O0 -c $(ALL_INCLUDE) -fPIC -o test.o test.c
install:
cp ./test_module.so /usr/local/apache2/modules/test_module.so
clean:
rm -rf test.o test_module.so
3.在httpd.conf中增加
 LoadModule test_module modules/test_module.so
4. 运行Apache看到有打印
 .libs/httpd -X -d /usr/local/apache

 

原创粉丝点击