gpt4 book ai didi

c - 使用 ap_parse_form_data 从 C 中的 apache 模块检索 POST 数据时出错

转载 作者:太空宇宙 更新时间:2023-11-03 23:31:31 29 4
gpt4 key购买 nike

我想使用“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: Syntax error on line 204 of /etc/apache2/apache2.conf:
Syntax error on line 1 of /etc/apache2/mods-enabled/apache_post.load:
Cannot load /usr/lib/apache2/modules/mod_apache_post.so into server:
/usr/lib/apache2/modules/mod_apache_post.so: undefined symbol:
ap_parse_form_data

undefined symbol :ap_parse_form_data

有什么解决这个问题的技巧吗?

最佳答案

我更新了这个话题,因为有人想要这个问题的答案,它会有所帮助。

    #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获取更多信息。

关于c - 使用 ap_parse_form_data 从 C 中的 apache 模块检索 POST 数据时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14214849/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com