我想使用“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
有任何提示可以解决这个问题吗?
答案 0 :(得分: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>
然后尝试将帖子请求发送到derectory /exampleDir.Follow本教程http://httpd.apache.org/docs/2.4/developer/modguide.html以获取更多信息。