httpd-modules-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Prakash Premkumar <>
Subject read POST parameters in apache module
Date Mon, 06 Jul 2015 11:51:53 GMT
I'm trying to read POST parameters from an apache c module.

Here's the code I'm using :

/* Include the required headers from httpd */#include
"httpd.h"#include "http_core.h"#include "http_protocol.h"#include
"http_request.h"#include "http_config.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"

/* Define prototypes of our functions in this module */typedef struct {
    const char *key;
    const char *value;} keyValuePair;
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 ={
    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!
static int example_handler(request_rec *r){
    /* First off, we need to check if this is a call for the "example" handler.
     * If it is, we accept it and do our things, it not, we simply
return DECLINED,
     * and Apache will try somewhere else.
    if (!r->handler || strcmp(r->handler, "example-handler")) return (DECLINED);

    // The first thing we will do is write a simple "Hello, world!"
back to the client.
    ap_rputs("Hello, world!<br/>", r);
    return OK;}

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;
    return kvp;}

I have copied the read post function from the apache website:

I get the following error while trying to compile the module:

mod_example.c:82:9: error: use of undeclared identifier 'ap_form_pair_t'
ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs);

apxs does not recognize ap_form_pair_t. Am I missing any header file ?

Can you please help me resolve this ?

  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message