From 83dc2332c8e98a499a07c2c40d04b78c06acc813 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Sun, 9 Sep 2007 19:13:57 +0000 Subject: fix --- src/daemon/daemontest_post.c | 7 +--- src/daemon/daemontest_postform.c | 5 ++- src/daemon/postprocessor.c | 89 +++++++++++++++++++--------------------- 3 files changed, 46 insertions(+), 55 deletions(-) diff --git a/src/daemon/daemontest_post.c b/src/daemon/daemontest_post.c index 86fd67f9..e7ac257a 100644 --- a/src/daemon/daemontest_post.c +++ b/src/daemon/daemontest_post.c @@ -20,12 +20,7 @@ /** * @file daemontest_post.c - * @brief Testcase for libmicrohttpd POST operations - * TODO: use curl_formadd to produce POST data and - * add that to the CURL operation; then check - * on the server side if the headers arrive - * nicely (need to implement parsing POST data - * first!) + * @brief Testcase for libmicrohttpd POST operations using URL-encoding * @author Christian Grothoff */ diff --git a/src/daemon/daemontest_postform.c b/src/daemon/daemontest_postform.c index a82d4705..c94e2b06 100644 --- a/src/daemon/daemontest_postform.c +++ b/src/daemon/daemontest_postform.c @@ -20,7 +20,7 @@ /** * @file daemontest_post.c - * @brief Testcase for libmicrohttpd POST operations + * @brief Testcase for libmicrohttpd POST operations using multipart/postform data * @author Christian Grothoff */ @@ -107,6 +107,8 @@ ahc_echo (void *cls, { eok = 0; pp = MHD_create_post_processor (connection, 1024, &post_iterator, &eok); + if (pp == NULL) + abort(); *unused = pp; } MHD_post_process (pp, upload_data, *upload_data_size); @@ -161,7 +163,6 @@ testInternalPost () curl_easy_setopt (c, CURLOPT_WRITEDATA, &cbc); pd = make_form(); curl_easy_setopt(c, CURLOPT_HTTPPOST, pd); - curl_easy_setopt (c, CURLOPT_VERBOSE, 1L); curl_easy_setopt (c, CURLOPT_FAILONERROR, 1); curl_easy_setopt (c, CURLOPT_TIMEOUT, 2L); if (oneone) diff --git a/src/daemon/postprocessor.c b/src/daemon/postprocessor.c index 5fbf4068..3ce715ee 100644 --- a/src/daemon/postprocessor.c +++ b/src/daemon/postprocessor.c @@ -30,34 +30,20 @@ */ enum PP_State { - PP_Init = 0, - PP_HaveKey = 1, - PP_ExpectNewLine = 2, - PP_ExpectNewLineR = 3, - PP_ExpectNewLineN = 4, - - PP_Headers = 5, - - PP_SkipRNRN = 6, - - PP_SkipNRN = 7, - - PP_SkipRN = 8, - - PP_SkipN = 9, - - PP_ValueToBoundary = 10, - - PP_FinalDash = 11, - + PP_ExpectNewLineNOPT = 5, + PP_Headers = 6, + PP_SkipRN = 7, + PP_SkipN = 8, + PP_ValueToBoundary = 9, + PP_FinalDash = 10, + PP_FinalRN = 11, + PP_FinalN = 12, PP_Error = 9999, - - }; /** @@ -177,7 +163,8 @@ MHD_create_post_processor (struct MHD_Connection *connection, return NULL; if ((0 != strcasecmp (MHD_HTTP_POST_ENCODING_FORM_URLENCODED, encoding)) && - (0 != strcasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, encoding))) + (0 != strncasecmp (MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA, encoding, + strlen(MHD_HTTP_POST_ENCODING_MULTIPART_FORMDATA)))) return NULL; ret = malloc (sizeof (struct MHD_PostProcessor) + buffer_size + 1); if (ret == NULL) @@ -352,8 +339,6 @@ try_match_header (const char *prefix, char *line, char **suffix) * apart and give them to the callback individually (will require some * additional states & state). * - * TODO: this code has never been tested... - * * See http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4 */ static int @@ -386,16 +371,16 @@ post_process_multipart (struct MHD_PostProcessor *pp, { /* first, move data to our internal buffer */ max = pp->buffer_size - pp->buffer_pos; - if ((max < ioff) && (max < post_data_len)) + if ((max < ioff) && (max < post_data_len - poff)) { memmove (buf, &buf[ioff], pp->buffer_pos - ioff); pp->buffer_pos -= ioff; ioff = 0; max = pp->buffer_size - pp->buffer_pos; } - if (max > post_data_len) - max = post_data_len; - memcpy (&buf[pp->buffer_pos], post_data, max); + if (max > post_data_len - poff) + max = post_data_len - poff; + memcpy (&buf[pp->buffer_pos], &post_data[poff], max); poff += max; pp->buffer_pos += max; @@ -426,7 +411,7 @@ post_process_multipart (struct MHD_PostProcessor *pp, if (buf[ioff] == '\r') { ioff++; - pp->state = PP_ExpectNewLineN; + pp->state = PP_ExpectNewLineNOPT; break; } /* fall through! */ @@ -438,6 +423,14 @@ post_process_multipart (struct MHD_PostProcessor *pp, break; } return MHD_NO; + case PP_ExpectNewLineNOPT: + if (buf[ioff] == '\n') + { + ioff++; + pp->state = PP_Headers; + break; + } + /* fall through! */ case PP_Headers: newline = 0; while ((newline + ioff < pp->buffer_pos) && @@ -456,7 +449,7 @@ post_process_multipart (struct MHD_PostProcessor *pp, } if (newline == 0) { - pp->state = PP_SkipRNRN; + pp->state = PP_SkipRN; break; } buf[ioff + newline] = '\0'; @@ -483,23 +476,9 @@ post_process_multipart (struct MHD_PostProcessor *pp, try_match_header ("Content-Type: ", &buf[ioff], &pp->content_type); try_match_header ("Content-Transfer-Encoding: ", &buf[ioff], &pp->transfer_encoding); + ioff += newline + 1; + pp->state = PP_ExpectNewLineNOPT; break; - case PP_SkipRNRN: - if (buf[ioff] == '\r') - { - ioff++; - pp->state = PP_SkipNRN; - break; - } - /* fall through! */ - case PP_SkipNRN: - if (buf[ioff] == '\n') - { - ioff++; - pp->state = PP_SkipRN; - break; - } - return MHD_NO; /* parse error */ case PP_SkipRN: if (buf[ioff] == '\r') { @@ -605,6 +584,22 @@ post_process_multipart (struct MHD_PostProcessor *pp, if (buf[ioff] == '-') { /* last boundary ends with "--" */ + ioff++; + pp->state = PP_FinalRN; + break; + } + return MHD_NO; /* parse error */ + case PP_FinalRN: + if (buf[ioff] == '\r') + { + ioff++; + pp->state = PP_FinalN; + break; + } + /* fall through! */ + case PP_FinalN: + if (buf[ioff] == '\n') + { ioff++; pp->state = PP_Error; break; -- cgit v1.2.3