peibaishi 发表于 2015-8-1 13:22:24

Apache模块开发/用C语言扩展apache

Apache模块开发/用C语言扩展apache(1:简述)
by linux_prog
   Apache是一个非常稳定而且非常open的web server,它的很多功能都可以通过plugin的方式去扩展。
比如:mod_proxy使得apache可以作代理, mod_rewrite使得apache可以实现非常强大的url mapping和rewritting
功能,你是否也想自己来开发一个apache module呢?网上这方面的文章非常的少,而且全是E文,
希望我的这篇文章能够给你一些实质性的帮助。
   开发apache module之前,我们有必要先分析一下其源代码。
   $ cd httpd-2.2.4/
   $ ls
   其中:server/目录是apache核心程序的代码
         include/目录存放主要的头文件
         srclib/目录存放apr和apr-util代码(这两个是什么,后面介绍)
         modules/目录下存放目前已经有的各种module(可以看看这些代码先)

   $ cd include/
   先分析一下apache的头文件
   $ vi httpd.h
   第766行,这个结构非常的重要,后面编写模块时都要用到这个结构,所以分析一下。
   每个http request都会对应这个结构的一个实例。
   由于apache的源代码都有很详细的英文注释,所以我也不翻译了。  struct request_rec {
   
    //内存管理池,后面讲apr时会讲到
    apr_pool_t *pool;
   
    conn_rec *connection;
   
    server_rec *server;
   
    request_rec *next;
   
    request_rec *prev;
   
    request_rec *main;
   
   
    char *the_request;
   
    int assbackwards;
   
    int proxyreq;
   
    int header_only;
   
    char *protocol;
   
    int proto_num;
   
    const char *hostname;
   
    apr_time_t request_time;
   
    const char *status_line;
   
    int status;
   
   
    const char *method;
   
    int method_number;
   
    apr_int64_t allowed;
   
    apr_array_header_t *allowed_xmethods;
   
    ap_method_list_t *allowed_methods;
   
    apr_off_t sent_bodyct;
   
    apr_off_t bytes_sent;
   
    apr_time_t mtime;
   
   
    int chunked;
   
    const char *range;
   
    apr_off_t clength;
   
    apr_off_t remaining;
   
    apr_off_t read_length;
   
    int read_body;
   
    int read_chunked;
   
    unsigned expecting_100;
   
   
    apr_table_t *headers_in;
   
    apr_table_t *headers_out;
   
    apr_table_t *err_headers_out;
   
    apr_table_t *subprocess_env;
   
    apr_table_t *notes;
   
   
    const char *content_type;
   
    const char *handler;
   
    const char *content_encoding;
   
    apr_array_header_t *content_languages;
   
    char *vlist_validator;
   
   
    char *user;
   
    char *ap_auth_type;
   
    int no_cache;
   
    int no_local_copy;
   
   
    char *unparsed_uri;
   
    char *uri;
   
    char *filename;
   
   
    char *canonical_filename;
   
    char *path_info;
   
    char *args;
   
    apr_finfo_t finfo;
   
    apr_uri_t parsed_uri;
   
    int used_path_info;
   
   
    struct ap_conf_vector_t *per_dir_config;
   
    struct ap_conf_vector_t *request_config;
   
    const struct htaccess_result *htaccess;
   
    struct ap_filter_t *output_filters;
   
    struct ap_filter_t *input_filters;
   
    struct ap_filter_t *proto_output_filters;
   
    struct ap_filter_t *proto_input_filters;
   
    int eos_sent;

};
  可以看到源码中有很多apr_开头的结构,这个是什么呢?下节介绍一下。
页: [1]
查看完整版本: Apache模块开发/用C语言扩展apache