commit 26a19e6787689b1ee88c9896ac8e8cb2342355d4
parent 5ac9c9835594471154e54252fbc364fd8f50e6a5
Author: Christian Grothoff <christian@grothoff.org>
Date: Fri, 11 Dec 2015 22:29:46 +0000
cleaning up largepost
Diffstat:
1 file changed, 68 insertions(+), 33 deletions(-)
diff --git a/doc/examples/largepost.c b/doc/examples/largepost.c
@@ -17,14 +17,17 @@
#define POSTBUFFERSIZE 512
#define MAXCLIENTS 2
-#define GET 0
-#define POST 1
+enum ConnectionType
+ {
+ GET = 0,
+ POST = 1
+ };
static unsigned int nr_of_uploading_clients = 0;
struct connection_info_struct
{
- int connectiontype;
+ enum ConnectionType connectiontype;
struct MHD_PostProcessor *postprocessor;
FILE *fp;
const char *answerstring;
@@ -54,19 +57,25 @@ const char *fileexistspage =
static int
-send_page (struct MHD_Connection *connection, const char *page,
+send_page (struct MHD_Connection *connection,
+ const char *page,
int status_code)
{
int ret;
struct MHD_Response *response;
response =
- MHD_create_response_from_buffer (strlen (page), (void *) page,
+ MHD_create_response_from_buffer (strlen (page),
+ (void *) page,
MHD_RESPMEM_MUST_COPY);
if (!response)
return MHD_NO;
- MHD_add_response_header (response, MHD_HTTP_HEADER_CONTENT_TYPE, "text/html");
- ret = MHD_queue_response (connection, status_code, response);
+ MHD_add_response_header (response,
+ MHD_HTTP_HEADER_CONTENT_TYPE,
+ "text/html");
+ ret = MHD_queue_response (connection,
+ status_code,
+ response);
MHD_destroy_response (response);
return ret;
@@ -74,9 +83,14 @@ send_page (struct MHD_Connection *connection, const char *page,
static int
-iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
- const char *filename, const char *content_type,
- const char *transfer_encoding, const char *data, uint64_t off,
+iterate_post (void *coninfo_cls,
+ enum MHD_ValueKind kind,
+ const char *key,
+ const char *filename,
+ const char *content_type,
+ const char *transfer_encoding,
+ const char *data,
+ uint64_t off,
size_t size)
{
struct connection_info_struct *con_info = coninfo_cls;
@@ -88,7 +102,7 @@ iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
if (0 != strcmp (key, "file"))
return MHD_NO;
- if (!con_info->fp)
+ if (! con_info->fp)
{
if (NULL != (fp = fopen (filename, "rb")))
{
@@ -105,7 +119,7 @@ iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
if (size > 0)
{
- if (!fwrite (data, size, sizeof (char), con_info->fp))
+ if (! fwrite (data, sizeof (char), size, con_info->fp))
return MHD_NO;
}
@@ -117,8 +131,10 @@ iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key,
static void
-request_completed (void *cls, struct MHD_Connection *connection,
- void **con_cls, enum MHD_RequestTerminationCode toe)
+request_completed (void *cls,
+ struct MHD_Connection *connection,
+ void **con_cls,
+ enum MHD_RequestTerminationCode toe)
{
struct connection_info_struct *con_info = *con_cls;
@@ -143,17 +159,23 @@ request_completed (void *cls, struct MHD_Connection *connection,
static int
-answer_to_connection (void *cls, struct MHD_Connection *connection,
- const char *url, const char *method,
- const char *version, const char *upload_data,
- size_t *upload_data_size, void **con_cls)
+answer_to_connection (void *cls,
+ struct MHD_Connection *connection,
+ const char *url,
+ const char *method,
+ const char *version,
+ const char *upload_data,
+ size_t *upload_data_size,
+ void **con_cls)
{
if (NULL == *con_cls)
{
struct connection_info_struct *con_info;
if (nr_of_uploading_clients >= MAXCLIENTS)
- return send_page (connection, busypage, MHD_HTTP_SERVICE_UNAVAILABLE);
+ return send_page (connection,
+ busypage,
+ MHD_HTTP_SERVICE_UNAVAILABLE);
con_info = malloc (sizeof (struct connection_info_struct));
if (NULL == con_info)
@@ -161,11 +183,13 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
con_info->fp = NULL;
- if (0 == strcmp (method, "POST"))
+ if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
{
con_info->postprocessor =
- MHD_create_post_processor (connection, POSTBUFFERSIZE,
- iterate_post, (void *) con_info);
+ MHD_create_post_processor (connection,
+ POSTBUFFERSIZE,
+ &iterate_post,
+ (void *) con_info);
if (NULL == con_info->postprocessor)
{
@@ -187,21 +211,27 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
return MHD_YES;
}
- if (0 == strcmp (method, "GET"))
+ if (0 == strcasecmp (method, MHD_HTTP_METHOD_GET))
{
char buffer[1024];
- snprintf (buffer, sizeof (buffer), askpage, nr_of_uploading_clients);
- return send_page (connection, buffer, MHD_HTTP_OK);
+ snprintf (buffer,
+ sizeof (buffer),
+ askpage,
+ nr_of_uploading_clients);
+ return send_page (connection,
+ buffer,
+ MHD_HTTP_OK);
}
- if (0 == strcmp (method, "POST"))
+ if (0 == strcasecmp (method, MHD_HTTP_METHOD_POST))
{
struct connection_info_struct *con_info = *con_cls;
if (0 != *upload_data_size)
{
- MHD_post_process (con_info->postprocessor, upload_data,
+ MHD_post_process (con_info->postprocessor,
+ upload_data,
*upload_data_size);
*upload_data_size = 0;
@@ -214,14 +244,18 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
fclose (con_info->fp);
con_info->fp = NULL;
}
- /* Now it is safe to open and inspect the file before calling send_page with a response */
- return send_page (connection, con_info->answerstring,
+ /* Now it is safe to open and inspect the file before
+ calling send_page with a response */
+ return send_page (connection,
+ con_info->answerstring,
con_info->answercode);
}
}
- return send_page (connection, errorpage, MHD_HTTP_BAD_REQUEST);
+ return send_page (connection,
+ errorpage,
+ MHD_HTTP_BAD_REQUEST);
}
@@ -230,10 +264,11 @@ main ()
{
struct MHD_Daemon *daemon;
- daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY, PORT, NULL, NULL,
+ daemon = MHD_start_daemon (MHD_USE_SELECT_INTERNALLY,
+ PORT, NULL, NULL,
&answer_to_connection, NULL,
- MHD_OPTION_NOTIFY_COMPLETED, request_completed,
- NULL, MHD_OPTION_END);
+ MHD_OPTION_NOTIFY_COMPLETED, &request_completed, NULL,
+ MHD_OPTION_END);
if (NULL == daemon)
return 1;
(void) getchar ();