分享

APACHE2.0 MOD 模块开发|技术文章收集

 openlog 2007-05-11
  • APACHE2.0 MOD 模块开发 [原创]
  • 发表时间:(2007-02-05 21:48:34)
  • APACHE2.0 MOD 模块开发 STEP 1

     

    一.目的

    写一个APACHE2.0MOD模块,读取配置,并对所有后缀为.hello的请求进行处理。

    二.步骤

    创建一个mod_hello.c文件

    1.  定义一个模块。

    #include "httpd.h"

    #include "http_config.h"

    module AP_MODULE_DECLARE_DATA hello_module;

     

    2.  定义接口。

    module AP_MODULE_DECLARE_DATA hello_module =

    {

            STANDARD20_MODULE_STUFF, // standard stuff; no need to mess with this.

            NULL, // create per-directory configuration structures - we do not.

            NULL, // merge per-directory - no need to merge if we are not creating anything.

            create_modhello_config, // create per-server configuration structures.

            NULL, // merge per-server - hrm - examples I have been reading don‘t bother with this for trivial cases.

            mod_hello_cmds, // configuration directive handlers

            mod_hello_register_hooks, // request handlers

    };

    说明:

    其中create_modhello_config函数为用来为自定义的结构分配空间,mod_hello_cmds定义了参数序列和参数的读取函数。mod_hello_register_hooks定义了请求处理函数

    3.  初始化配置,读取配置。

    配置结构的定义:

    typedef struct {

            char   *welcome;

            int    max_process;

    } modhello_config;

    参数的定义:

    static const command_rec mod_hello_cmds[] =

    {

            AP_INIT_TAKE1(

                    "welcome",

                    set_modhello_string,

                    NULL,

                    RSRC_CONF,

                    "hello,apache"

            ),

            AP_INIT_TAKE1(

                    "ModuleMaxProcess",

                    set_modhello_string,

                    NULL,

                    RSRC_CONF,

                    NULL

            ),

            {NULL}

    };

    参数结构的创建,由apache在装载模块时候调用。

    static void *create_modhello_config(apr_pool_t *p, server_rec *s)

    {

            modhello_config *newcfg;

     

            // allocate space for the configuration structure from the provided pool p.

            newcfg = (modhello_config *) apr_pcalloc(p, sizeof(modhello_config));

     

            // return the new server configuration structure.

            return (void *) newcfg;

    }

    参数读取函数

     static const char *set_modhello_string(cmd_parms *parms, void *mconfig, const char *arg)

    {

            modhello_config *s_cfg = ap_get_module_config(parms->server->module_config, &hello_module);

             if(!strcmp(parms->cmd->name,"welcome")){

                    s_cfg->welcome= (char *) arg;

            }else if(!strcmp(parms->cmd->name,"ModuleMaxProcess")){

                    s_cfg->max_process=atoi(arg);

            }

     

            // success

            return NULL;

    }

     

    4.  处理请求。

    注册请求。

    static void mod_hello_register_hooks (apr_pool_t *p)

    {

      ap_hook_handler(mod_hello_method_handler, NULL, NULL, APR_HOOK_LAST);

    }

     

    请求处理函数

     static int mod_hello_method_handler (request_rec *r)

    {

            modhello_config *s_cfg ;

            if(strcmp("hello-s cript",r->handler)) return DECLINED;

            s_cfg= ap_get_module_config(r->server->module_config, &hello_module);

            fprintf(stderr,"%s,%s,%d\n",r->content_type,r->handler,s_cfg->max_process);

            ap_rputs("hello,world!",r);

            return 0;

    }

    三.安装。

    1.       编译。

    Makefile.

     all:    mod_hello.c

            gcc -g -I/home/wee/apache2/include/ -fPIC -o mod_hello.o -c mod_hello.c

            gcc -shared -I/home/wee/apache2/include/ -o libmodhello.so -lc mod_hello.o

            cp *.so /home/wee/apache2/modules/

    clean:

            rm *.o *.so

    2.       配置。

    修改Httpd.conf

    增加处理:

     LoadModule hello_module        modules/libmodhello.so

    AddHandler hello-s cript .hello

              增加参数:

                  welcome  "hello,world"

    ModuleMaxProcess   5

    3.       安装

    gcc -v

    Reading specs from /usr/lib/gcc-lib/i386-redhat-linux/2.96/specs

    gcc version 2.96 20000731 (Red Hat Linux 7.3 2.96-110)

    make.

    四.测试。

    访问http://./a.hello, 屏幕上打印出 “hello,world”,同时LOG中也有打印信息。

    五.参考资料

    1.  http:///tutorials/apache2_modules/tut1/tutorial1.html

    2.  Writing.Apache Modules with Perl and CLincoln Stein and Doug MacEachern

    3.  http:///

    4.  http://www./ 

     

    APACHE MOD 开发 Step 2 获取用户输入

    APACHE MOD 开发 Step 2 获取用户输入

    1. 了解apr_table_t结构
    可以把他理解为一个HASH表,可以对他进行取值赋值操作,常用的有
    apr_table_add
    apr_table_set
    apr_table_get
    例如:
    char *slen=apr_table_get(r->headers_in, "Content-Length");
    2. 了解request_rec
    这是一个最重要的结构,定义在httpd.h第682行.
    注意到handle函数的唯一的参数
    static int mod_hello_method_handler (request_rec *r);你可以认为从这个结构里面你可以得到所有一切.

     

     

    重要的几个结构成员
        apr_pool_t *pool;
        /** The connection to the client */
        conn_rec *connection;
        /** The virtual host for this request */
    server_rec *server;

     

     

    int method_number; //提交信息的类型,GET或者POST
    char *args; //存放GET的参数
    apr_table_t *headers_in; //提交信息的头信息的保存位置
    const char *handler;    //处理的类型

     

     

      
    3. 读取HTTP头
    信息在r->headers_in里面,那么就是
    char *slen=apr_table_get(r->headers_in, "Content-Length");

     

     

    4. 获得GET方法传递的数据
    信息在r->args里面.注意,这里的数据是没有经过解析的,也就是说URL编码过的,如果你不使用类似libapreq2而自行解析的话,需要自行编码.
    ap_log_rerror(APLOG_MARK, APLOG_ERR,0,r,"get query string:%s",r->args);
    5. 获得POST方法传递的数据
         数据在request_rec关联的bucket里面.bucket的解释将在下一步解释,那么我们简单的使用ap_get_client_block来读取吧.其实这个函数里面也是调用了bucket操作.
      
    6. 简单的例子.
            if(strcmp("hello-s cript",r->handler)) return DECLINED;
            //get the comand.
            if(r->method_number==M_GET){
                    ap_log_rerror(APLOG_MARK, APLOG_ERR,0,r,"get query string:%s",r->args);
            }else if(r->method_number==M_POST){
                   handle_post (r);
            }else{
                    return DECLINED;
            }

     

     

    handle_post 函数
     void handle_post(request_rec *r)
    {         
              
            size_t total_bytes;
            int rstat=0;
            char cbuf[HUGE_STRING_LEN];

     

     

            rstat = ap_setup_client_block(r, REQUEST_CHUNKED_DECHUNK);
            if (ap_should_client_block(r)) {
                    int nbytes;
                    while ((nbytes = ap_get_client_block(r, cbuf, HUGE_STRING_LEN)) > 0){
                            cbuf[nbytes]=‘\0‘;
                            ap_rputs(cbuf,r);
                            total_bytes += nbytes;
                    }
           }
     } 
    7. 小结
    现在对模块有了简单的理解,知道如何写一个模块,知道数据在哪.知道如何处理输入输出,下面要知道APAHCE内部是如何的运作,APR运行库的常用函数,然后是利用APACHE的服务框架完成更多的工作.要把APACHE看做一个SOCKET服务器,而不仅仅是一个WEB服务器
    • 本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
      转藏 分享 献花(0

      0条评论

      发表

      请遵守用户 评论公约

      类似文章 更多