mirror of
https://github.com/apache/httpd.git
synced 2025-07-30 20:03:10 +03:00
xforms
git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1352123 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
@ -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->elts;
|
apr_array_header_t *pairs = NULL;
|
||||||
for(i = 0; i < fields->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, &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)
|
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");
|
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]->key, formData[i]->value);
|
||||||
|
}
|
||||||
|
}
|
||||||
return OK;
|
return OK;
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
|
Reference in New Issue
Block a user