2

我想使用“C”从 Apache 模块中的浏览器解析 POST 数据。根据 Apache API 文档,函数 ap_parse_form_data 可用于此目的。该函数在 httpd.h 中声明,我已将其包含在我的模块中:

...
#include <httpd.h>
#include <apr_tables.h>
#include "http_config.h"
#include "http_protocol.h"
#include "ap_config.h"
...
keyValuePair* readPost(request_rec* r) {
    ...
    apr_array_header_t *pairs=NULL;
    int res;
    ...
    res = ap_parse_form_data(r, NULL, &pairs, -1, 8192);

该程序使用 apxs2 命令成功编译,并且模块安装在正确的路径中。但是,当我启动 Apache 服务器时,它会抛出如下错误:

apache2:/etc/apache2/apache2.conf 第 204 行的
语法错误:/etc/apache2/mods-enabled/apache_post.load 第 1 行的语法错误:
无法加载 /usr/lib/apache2/modules/mod_apache_post.so进入服务器:
/usr/lib/apache2/modules/mod_apache_post.so:未定义符号:
ap_parse_form_data

undefined symbol:ap_parse_form_data

有什么提示可以解决这个问题吗?

4

1 回答 1

2

我更新了这个帖子,因为有人想要回答这个问题,这会有所帮助。

    #include "httpd.h"
    #include "http_core.h"
    #include "http_protocol.h"
    #include "http_request.h"

    #include "apr_strings.h"
    #include "apr_network_io.h"
    #include "apr_dbd.h"
    #include <apr_file_info.h>
    #include <apr_file_io.h>
    #include <apr_tables.h>
    #include "util_script.h"

    typedef struct {
    const char* key;
    const char* value;
    } keyValuePair;

    /* Define prototypes of our functions in this module */
    static void register_hooks(apr_pool_t *pool);
    static int example_handler(request_rec *r);
    keyValuePair* readPost(request_rec* r);
    /* 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
    };


    /* 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);
    }

    /* The handler function for our module.
            * This is where all the fun happens!
    */



    keyValuePair* readPost(request_rec* r) {
            apr_array_header_t *pairs = NULL;
            apr_off_t len;
            apr_size_t size;
            int res;
            int i = 0;
            char *buffer;
            keyValuePair* kvp;

    res = ap_parse_form_data(r, NULL, &pairs, -1, HUGE_STRING_LEN);
    if (res != OK || !pairs) return NULL; /* Return NULL if we failed or if there are is no POST data */
    kvp = apr_pcalloc(r->pool, sizeof(keyValuePair) * (pairs->nelts + 1));
    while (pairs && !apr_is_empty_array(pairs)) {
    ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs);
    apr_brigade_length(pair->value, 1, &len);
    size = (apr_size_t) len;
    buffer = apr_palloc(r->pool, size + 1);
    apr_brigade_flatten(pair->value, buffer, &size);
    buffer[len] = 0;
    kvp[i].key = apr_pstrdup(r->pool, pair->name);
    kvp[i].value = buffer;
    i++;
    }
    return kvp;
    }

    static int example_handler(request_rec *r)
    {
    /*~~~~~~~~~~~~~~~~~~~~~~*/
    keyValuePair* formData;
    /*~~~~~~~~~~~~~~~~~~~~~~*/

            formData = readPost(r);
            if (formData) {
             int i;
            for (i = 0; &formData[i]; i++) {
            if (formData[i].key && formData[i].value) {
                    ap_rprintf(r, "%s = %s\n", formData[i].key, formData[i].value);
            } else if (formData[i].key) {
                    ap_rprintf(r, "%s\n", formData[i].key);
            } else if (formData[i].value) {
                    ap_rprintf(r, "= %s\n", formData[i].value);
            } else {
                    break;
            }
            }
    }
    return OK;
    }

所以在用一些名字(mod_example.c)保存上面的代码之后,你可以像这样使用apxs编译它

             apxs -i -a -c mod_example.c

然后在 apache2.conf(etc/apache2/) 文件中添加这个。

             <Location "/exampleDir">
                   SetHandler example-handler
             </Location>   

然后尝试将发布请求发送到目录 /exampleDir。请按照本教程http://httpd.apache.org/docs/2.4/developer/modguide.html获取更多信息。

于 2015-01-14T19:27:10.213 回答