You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@httpd.apache.org by hu...@apache.org on 2012/06/20 16:19:45 UTC
svn commit: r1352122 - /httpd/httpd/trunk/docs/manual/developer/modguide.xml
Author: humbedooh
Date: Wed Jun 20 14:19:45 2012
New Revision: 1352122
URL: http://svn.apache.org/viewvc?rev=1352122&view=rev
Log:
Update (fix) the POST data retrieval example.
Modified:
httpd/httpd/trunk/docs/manual/developer/modguide.xml
Modified: httpd/httpd/trunk/docs/manual/developer/modguide.xml
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/developer/modguide.xml?rev=1352122&r1=1352121&r2=1352122&view=diff
==============================================================================
--- httpd/httpd/trunk/docs/manual/developer/modguide.xml (original)
+++ httpd/httpd/trunk/docs/manual/developer/modguide.xml Wed Jun 20 14:19:45 2012
@@ -1572,34 +1572,56 @@ or check out the rest of our documentati
<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)) {
+ i++;
+ 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 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>