<!-- BEGIN EXAMPLE CODE -->
<highlight language="c">
-<a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__apr__tables.html#gad7ea82d6608a4a633fc3775694ab71e4">apr_table_t</a> *GET;
-<a href="http://ci.apache.org/projects/httpd/trunk/doxygen/structapr__array__header__t.html">apr_array_header_t</a> *POST;
-
-<a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__SCRIPT.html#gaed25877b529623a4d8f99f819ba1b7bd">ap_args_to_table</a>(r, &GET);
-<a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__DAEMON.html#ga9d426b6382b49754d4f87c55f65af202">ap_parse_form_data</a>(r, NULL, &POST, -1, 8192);
+<a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__apr__tables.html#gad7ea82d6608a4a633fc3775694ab71e4">apr_table_t</a> *GET; <em>
+</em><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/structapr__array__header__t.html">apr_array_header_t</a>*POST;
+<em>
+</em>
+<a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__SCRIPT.html#gaed25877b529623a4d8f99f819ba1b7bd">
+ap_args_to_table</a>(r, &GET); <em>
+</em><a href="http://ci.apache.org/projects/httpd/trunk/doxygen/group__APACHE__CORE__DAEMON.html#ga9d426b6382b49754d4f87c55f65af202">
+ap_parse_form_data</a>(r, NULL, &POST, -1, 8192);
</highlight>
<!-- END EXAMPLE CODE -->
<section id="snippets"><title>Some useful snippets of code</title>
-<section id="get_post"><title>Retrieve a variable from POST form data</title>
+<section id="get_post"><title>Retrieve variables from POST form data</title>
<!-- BEGIN EXAMPLE CODE -->
<highlight language="c">
-const char *read_post_value(const apr_array_header_t *fields, const char *key)
-{
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- int i;
- apr_table_entry_t *e = 0;
- /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
- e = (apr_table_entry_t *) fields->elts;
- for(i = 0; i < fields->nelts; i++) {
- if(!strcmp(e[i].key, key)) return e[i].val;
+typedef struct {
+ const char* key;
+ const char* value;
+} keyValuePair;
+
+keyValuePair* readPost(request_req* 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);
+ i++;
+ 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 0;
+ return kvp;
}
+
static int example_handler(request_req *r)
{
/*~~~~~~~~~~~~~~~~~~~~~~*/
- apr_array_header_t *POST;
- const char *value;
- /*~~~~~~~~~~~~~~~~~~~~~~*/
- ap_parse_form_data(r, NULL, &POST, -1, 8192);
- value = read_post_value(POST, "valueA");
- if (!value) value = "(undefined)";
- ap_rprintf(r, "The value of valueA is: %s", value);
+ keyValuePair* formData;
+ /*~~~~~~~~~~~~~~~~~~~~~~*/
+
+ formData = readPost();
+ if (formData) {
+ int i;
+ for (i = 0; formData[i]; i++) {
+ ap_rprintf(r, "%s == %s\n", formData[i]->key, formData[i]->value);
+ }
+ }
return OK;
}
</highlight>