1
0
mirror of https://github.com/apache/httpd.git synced 2025-07-30 20:03:10 +03:00
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1352123 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Daniel Gruno
2012-06-20 14:20:11 +00:00
parent e86fe7d457
commit e55222e733

View File

@ -1611,34 +1611,56 @@ or check out the rest of our documentation for further tips.
<div class="section"> <div class="section">
<h2><a name="snippets" id="snippets">Some useful snippets of code</a></h2> <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"> <pre class="prettyprint lang-c">
const char *read_post_value(const apr_array_header_t *fields, const char *key) typedef struct {
{ const char* key;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ const char* value;
int i; } keyValuePair;
apr_table_entry_t *e = 0;
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ keyValuePair* readPost(request_req* r) {
e = (apr_table_entry_t *) fields-&gt;elts; apr_array_header_t *pairs = NULL;
for(i = 0; i &lt; fields-&gt;nelts; i++) { apr_off_t len;
if(!strcmp(e[i].key, key)) return e[i].val; 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) 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"); keyValuePair* formData;
if (!value) value = "(undefined)"; /*~~~~~~~~~~~~~~~~~~~~~~*/
ap_rprintf(r, "The value of valueA is: %s", value);
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; return OK;
} }
</pre> </pre>