libmicrohttpd

HTTP/1.x server C library (MHD 1.x, stable)
Log | Files | Refs | Submodules | README | LICENSE

commit 00a9277f498260289d89f0de6783829b6391394b
parent 602e9c682982c4a3b2c232d5d384e4f16698fdaf
Author: Christian Grothoff <christian@grothoff.org>
Date:   Sat, 21 Dec 2013 16:27:06 +0000

-fix postexample

Diffstat:
MChangeLog | 7++++++-
Msrc/examples/post_example.c | 111+++++++++++++++++++++++++++++++++++++++----------------------------------------
2 files changed, 61 insertions(+), 57 deletions(-)

diff --git a/ChangeLog b/ChangeLog @@ -1,5 +1,10 @@ +Sat Dec 21 17:26:08 CET 2013 + Fixed an issue with a missing argument in the postexample. + Fixed issue with bogus offset increment involving sendfile + on GNU/Linux. + Mon Dec 9 21:41:57 CET 2013 - Fix for per-worker daemon pipes enabled with + Fix for per-worker daemon pipes enabled with MHD_USE_SUSPEND_RESUME that were not closed in MHD_stop_daemon. -MH diff --git a/src/examples/post_example.c b/src/examples/post_example.c @@ -76,7 +76,7 @@ struct Session struct Session *next; /** - * Unique ID for this session. + * Unique ID for this session. */ char sid[33]; @@ -122,7 +122,7 @@ struct Request struct MHD_PostProcessor *pp; /** - * URL to serve in response to this POST (if this request + * URL to serve in response to this POST (if this request * was a 'POST') */ const char *post_url; @@ -140,7 +140,7 @@ static struct Session *sessions; /** - * Return the session handle for this connection, or + * Return the session handle for this connection, or * create one if this is a new user. */ static struct Session * @@ -171,9 +171,9 @@ get_session (struct MHD_Connection *connection) /* create fresh session */ ret = calloc (1, sizeof (struct Session)); if (NULL == ret) - { + { fprintf (stderr, "calloc error: %s\n", strerror (errno)); - return NULL; + return NULL; } /* not a super-secure way to generate a random session ID, but should do for a simple example... */ @@ -184,7 +184,7 @@ get_session (struct MHD_Connection *connection) (unsigned int) rand (), (unsigned int) rand (), (unsigned int) rand ()); - ret->rc++; + ret->rc++; ret->start = time (NULL); ret->next = sessions; sessions = ret; @@ -209,7 +209,7 @@ typedef int (*PageHandler)(const void *cls, /** * Entry we generate for each page served. - */ + */ struct Page { /** @@ -229,7 +229,7 @@ struct Page /** * Extra argument to handler. - */ + */ const void *handler_cls; }; @@ -239,7 +239,7 @@ struct Page * * @param session session to use * @param response response to modify - */ + */ static void add_session_cookie (struct Session *session, struct MHD_Response *response) @@ -250,12 +250,12 @@ add_session_cookie (struct Session *session, "%s=%s", COOKIE_NAME, session->sid); - if (MHD_NO == + if (MHD_NO == MHD_add_response_header (response, MHD_HTTP_HEADER_SET_COOKIE, cstr)) { - fprintf (stderr, + fprintf (stderr, "Failed to set session cookie header!\n"); } } @@ -267,7 +267,7 @@ add_session_cookie (struct Session *session, * * @param cls a 'const char *' with the HTML webpage to return * @param mime mime type to use - * @param session session handle + * @param session session handle * @param connection connection to use */ static int @@ -290,8 +290,8 @@ serve_simple_form (const void *cls, MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, mime); - ret = MHD_queue_response (connection, - MHD_HTTP_OK, + ret = MHD_queue_response (connection, + MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; @@ -301,9 +301,9 @@ serve_simple_form (const void *cls, /** * Handler that adds the 'v1' value to the given HTML code. * - * @param cls a 'const char *' with the HTML webpage to return + * @param cls unused * @param mime mime type to use - * @param session session handle + * @param session session handle * @param connection connection to use */ static int @@ -313,16 +313,15 @@ fill_v1_form (const void *cls, struct MHD_Connection *connection) { int ret; - const char *form = cls; char *reply; struct MHD_Response *response; - reply = malloc (strlen (form) + strlen (session->value_1) + 1); + reply = malloc (strlen (MAIN_PAGE) + strlen (session->value_1) + 1); if (NULL == reply) return MHD_NO; snprintf (reply, - strlen (form) + strlen (session->value_1) + 1, - form, + strlen (MAIN_PAGE) + strlen (session->value_1) + 1, + MAIN_PAGE, session->value_1); /* return static form */ response = MHD_create_response_from_buffer (strlen (reply), @@ -334,8 +333,8 @@ fill_v1_form (const void *cls, MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, mime); - ret = MHD_queue_response (connection, - MHD_HTTP_OK, + ret = MHD_queue_response (connection, + MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; @@ -345,9 +344,9 @@ fill_v1_form (const void *cls, /** * Handler that adds the 'v1' and 'v2' values to the given HTML code. * - * @param cls a 'const char *' with the HTML webpage to return + * @param cls unused * @param mime mime type to use - * @param session session handle + * @param session session handle * @param connection connection to use */ static int @@ -357,17 +356,17 @@ fill_v1_v2_form (const void *cls, struct MHD_Connection *connection) { int ret; - const char *form = cls; char *reply; struct MHD_Response *response; - reply = malloc (strlen (form) + strlen (session->value_1) + strlen (session->value_2) + 1); + reply = malloc (strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1); if (NULL == reply) return MHD_NO; snprintf (reply, - strlen (form) + strlen (session->value_1) + strlen (session->value_2) + 1, - form, - session->value_1); + strlen (SECOND_PAGE) + strlen (session->value_1) + strlen (session->value_2) + 1, + SECOND_PAGE, + session->value_1, + session->value_2); /* return static form */ response = MHD_create_response_from_buffer (strlen (reply), (void *) reply, @@ -378,8 +377,8 @@ fill_v1_v2_form (const void *cls, MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, mime); - ret = MHD_queue_response (connection, - MHD_HTTP_OK, + ret = MHD_queue_response (connection, + MHD_HTTP_OK, response); MHD_destroy_response (response); return ret; @@ -391,7 +390,7 @@ fill_v1_v2_form (const void *cls, * * @param cls a 'const char *' with the HTML webpage to return * @param mime mime type to use - * @param session session handle + * @param session session handle * @param connection connection to use */ static int @@ -409,8 +408,8 @@ not_found_page (const void *cls, MHD_RESPMEM_PERSISTENT); if (NULL == response) return MHD_NO; - ret = MHD_queue_response (connection, - MHD_HTTP_NOT_FOUND, + ret = MHD_queue_response (connection, + MHD_HTTP_NOT_FOUND, response); MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_ENCODING, @@ -423,10 +422,10 @@ not_found_page (const void *cls, /** * List of all pages served by this HTTP server. */ -static struct Page pages[] = +static struct Page pages[] = { - { "/", "text/html", &fill_v1_form, MAIN_PAGE }, - { "/2", "text/html", &fill_v1_v2_form, SECOND_PAGE }, + { "/", "text/html", &fill_v1_form, NULL }, + { "/2", "text/html", &fill_v1_v2_form, NULL }, { "/S", "text/html", &serve_simple_form, SUBMIT_PAGE }, { "/F", "text/html", &serve_simple_form, LAST_PAGE }, { NULL, NULL, &not_found_page, NULL } /* 404 */ @@ -476,27 +475,27 @@ post_iterator (void *cls, } if (0 == strcmp ("v1", key)) { - if (size + off > sizeof(session->value_1)) - size = sizeof (session->value_1) - off; + if (size + off >= sizeof(session->value_1)) + size = sizeof (session->value_1) - off - 1; memcpy (&session->value_1[off], data, size); - if (size + off < sizeof (session->value_1)) - session->value_1[size+off] = '\0'; + session->value_1[size+off] = '\0'; return MHD_YES; } if (0 == strcmp ("v2", key)) { - if (size + off > sizeof(session->value_2)) - size = sizeof (session->value_2) - off; + if (size + off >= sizeof(session->value_2)) + size = sizeof (session->value_2) - off - 1; memcpy (&session->value_2[off], data, size); - if (size + off < sizeof (session->value_2)) - session->value_2[size+off] = '\0'; + session->value_2[size+off] = '\0'; return MHD_YES; } - fprintf (stderr, "Unsupported form value `%s'\n", key); + fprintf (stderr, + "Unsupported form value `%s'\n", + key); return MHD_YES; } @@ -540,7 +539,7 @@ create_response (void *cls, const char *url, const char *method, const char *version, - const char *upload_data, + const char *upload_data, size_t *upload_data_size, void **ptr) { @@ -586,7 +585,7 @@ create_response (void *cls, session = request->session; session->start = time (NULL); if (0 == strcmp (method, MHD_HTTP_METHOD_POST)) - { + { /* evaluate POST data */ MHD_post_process (request->pp, upload_data, @@ -612,7 +611,7 @@ create_response (void *cls, while ( (pages[i].url != NULL) && (0 != strcmp (pages[i].url, url)) ) i++; - ret = pages[i].handler (pages[i].handler_cls, + ret = pages[i].handler (pages[i].handler_cls, pages[i].mime, session, connection); if (ret != MHD_YES) @@ -624,8 +623,8 @@ create_response (void *cls, response = MHD_create_response_from_buffer (strlen (METHOD_ERROR), (void *) METHOD_ERROR, MHD_RESPMEM_PERSISTENT); - ret = MHD_queue_response (connection, - MHD_HTTP_METHOD_NOT_ACCEPTABLE, + ret = MHD_queue_response (connection, + MHD_HTTP_METHOD_NOT_ACCEPTABLE, response); MHD_destroy_response (response); return ret; @@ -689,7 +688,7 @@ expire_sessions () else prev = pos; pos = next; - } + } } @@ -718,8 +717,8 @@ main (int argc, char *const *argv) srand ((unsigned int) time (NULL)); d = MHD_start_daemon (MHD_USE_DEBUG, atoi (argv[1]), - NULL, NULL, - &create_response, NULL, + NULL, NULL, + &create_response, NULL, MHD_OPTION_CONNECTION_TIMEOUT, (unsigned int) 15, MHD_OPTION_NOTIFY_COMPLETED, &request_completed_callback, NULL, MHD_OPTION_END); @@ -734,11 +733,11 @@ main (int argc, char *const *argv) FD_ZERO (&es); if (MHD_YES != MHD_get_fdset (d, &rs, &ws, &es, &max)) break; /* fatal internal error */ - if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES) + if (MHD_get_timeout (d, &mhd_timeout) == MHD_YES) { tv.tv_sec = mhd_timeout / 1000; tv.tv_usec = (mhd_timeout - (tv.tv_sec * 1000)) * 1000; - tvp = &tv; + tvp = &tv; } else tvp = NULL;