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:20:11 UTC

svn commit: r1352123 - /httpd/httpd/trunk/docs/manual/developer/modguide.html.en

Author: humbedooh
Date: Wed Jun 20 14:20:11 2012
New Revision: 1352123

URL: http://svn.apache.org/viewvc?rev=1352123&view=rev
Log:
xforms

Modified:
    httpd/httpd/trunk/docs/manual/developer/modguide.html.en

Modified: httpd/httpd/trunk/docs/manual/developer/modguide.html.en
URL: http://svn.apache.org/viewvc/httpd/httpd/trunk/docs/manual/developer/modguide.html.en?rev=1352123&r1=1352122&r2=1352123&view=diff
==============================================================================
--- httpd/httpd/trunk/docs/manual/developer/modguide.html.en (original)
+++ httpd/httpd/trunk/docs/manual/developer/modguide.html.en Wed Jun 20 14:20:11 2012
@@ -1611,34 +1611,56 @@ or check out the rest of our documentati
 <div class="section">
 <h2><a name="snippets" id="snippets">Some useful snippets of code</a></h2>
 
-<h3><a name="get_post" id="get_post">Retrieve a variable from POST form data</a></h3>
+<h3><a name="get_post" id="get_post">Retrieve variables from POST form data</a></h3>
 
 
 
 <pre class="prettyprint lang-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-&gt;elts;
-    for(i = 0; i &lt; fields-&gt;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, &amp;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-&gt;pool, sizeof(keyValuePair) * (pairs-&gt;nelts + 1));
+    while (pairs &amp;&amp; !apr_is_empty_array(pairs)) {
+        i++;
+        ap_form_pair_t *pair = (ap_form_pair_t *) apr_array_pop(pairs);
+        apr_brigade_length(pair-&gt;value, 1, &amp;len);
+        size = (apr_size_t) len;
+        buffer = apr_palloc(r-&gt;pool, size + 1);
+        apr_brigade_flatten(pair-&gt;value, buffer, &amp;size);
+        buffer[len] = 0;
+        kvp[i]-&gt;key = apr_pstrdup(r-&gt;pool, pair-&gt;name);
+        kvp[i]-&gt;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, &amp;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]-&gt;key, formData[i]-&gt;value);
+        }
+    }
     return OK;
 }
 </pre>