This commit is contained in:
Christian Grothoff
2025-09-07 01:09:18 +02:00
parent 0db39f453a
commit 1fd9b3779f
+60 -52
View File
@@ -23,12 +23,9 @@
#define MAXNAMESIZE 20 #define MAXNAMESIZE 20
#define MAXANSWERSIZE 512 #define MAXANSWERSIZE 512
#define GET 0
#define POST 1
struct connection_info_struct struct connection_info_struct
{ {
int connectiontype; enum { GET, POST } connectiontype;
char *answerstring; char *answerstring;
struct MHD_PostProcessor *postprocessor; struct MHD_PostProcessor *postprocessor;
}; };
@@ -49,87 +46,100 @@ static const char *errorpage =
static enum MHD_Result static enum MHD_Result
send_page (struct MHD_Connection *connection, const char *page) send_page (struct MHD_Connection *connection,
const char *page)
{ {
enum MHD_Result ret; enum MHD_Result ret;
struct MHD_Response *response; struct MHD_Response *response;
response = MHD_create_response_from_buffer_static (strlen (page),
response = MHD_create_response_from_buffer_static (strlen (page), page); page);
if (! response) if (! response)
return MHD_NO; return MHD_NO;
ret = MHD_queue_response (connection,
ret = MHD_queue_response (connection, MHD_HTTP_OK, response); MHD_HTTP_OK,
response);
MHD_destroy_response (response); MHD_destroy_response (response);
return ret; return ret;
} }
static enum MHD_Result static enum MHD_Result
iterate_post (void *coninfo_cls, enum MHD_ValueKind kind, const char *key, iterate_post (void *coninfo_cls,
const char *filename, const char *content_type, enum MHD_ValueKind kind,
const char *transfer_encoding, const char *data, uint64_t off, const char *key,
const char *filename,
const char *content_type,
const char *transfer_encoding,
const char *data,
uint64_t off,
size_t size) size_t size)
{ {
struct connection_info_struct *con_info = coninfo_cls; struct connection_info_struct *con_info = coninfo_cls;
(void) kind; /* Unused. Silent compiler warning. */ (void) kind; /* Unused. Silent compiler warning. */
(void) filename; /* Unused. Silent compiler warning. */ (void) filename; /* Unused. Silent compiler warning. */
(void) content_type; /* Unused. Silent compiler warning. */ (void) content_type; /* Unused. Silent compiler warning. */
(void) transfer_encoding; /* Unused. Silent compiler warning. */ (void) transfer_encoding; /* Unused. Silent compiler warning. */
(void) off; /* Unused. Silent compiler warning. */ (void) off; /* Unused. Silent compiler warning. */
if (0 != strcmp (key,
if (0 == strcmp (key, "name")) "name"))
{ return MHD_YES;
if ((size > 0) && (size <= MAXNAMESIZE)) if ( (size > 0) &&
(size <= MAXNAMESIZE) )
{ {
char *answerstring; char *answerstring;
answerstring = malloc (MAXANSWERSIZE); answerstring = malloc (MAXANSWERSIZE);
if (! answerstring) if (! answerstring)
return MHD_NO; return MHD_NO;
snprintf (answerstring,
snprintf (answerstring, MAXANSWERSIZE, GREETINGPAGE, data); MAXANSWERSIZE,
GREETINGPAGE,
data);
con_info->answerstring = answerstring; con_info->answerstring = answerstring;
} }
else else
{
con_info->answerstring = NULL; con_info->answerstring = NULL;
return MHD_NO;
} }
return MHD_YES; return MHD_YES;
} }
static void static void
request_completed (void *cls, struct MHD_Connection *connection, request_completed (void *cls,
void **req_cls, enum MHD_RequestTerminationCode toe) struct MHD_Connection *connection,
void **req_cls,
enum MHD_RequestTerminationCode toe)
{ {
struct connection_info_struct *con_info = *req_cls; struct connection_info_struct *con_info = *req_cls;
(void) cls; /* Unused. Silent compiler warning. */ (void) cls; /* Unused. Silent compiler warning. */
(void) connection; /* Unused. Silent compiler warning. */ (void) connection; /* Unused. Silent compiler warning. */
(void) toe; /* Unused. Silent compiler warning. */ (void) toe; /* Unused. Silent compiler warning. */
if (NULL == con_info) if (NULL == con_info)
return; return;
if (POST == con_info->connectiontype)
if (con_info->connectiontype == POST)
{ {
MHD_destroy_post_processor (con_info->postprocessor); MHD_destroy_post_processor (con_info->postprocessor);
if (con_info->answerstring) if (con_info->answerstring)
free (con_info->answerstring); free (con_info->answerstring);
} }
free (con_info); free (con_info);
*req_cls = NULL; *req_cls = NULL;
} }
static enum MHD_Result static enum MHD_Result
answer_to_connection (void *cls, struct MHD_Connection *connection, answer_to_connection (void *cls,
const char *url, const char *method, struct MHD_Connection *connection,
const char *version, const char *upload_data, const char *url,
size_t *upload_data_size, void **req_cls) const char *method,
const char *version,
const char *upload_data,
size_t *upload_data_size,
void **req_cls)
{ {
(void) cls; /* Unused. Silent compiler warning. */ (void) cls; /* Unused. Silent compiler warning. */
(void) url; /* Unused. Silent compiler warning. */ (void) url; /* Unused. Silent compiler warning. */
@@ -147,35 +157,35 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
if (0 == strcmp (method, "POST")) if (0 == strcmp (method, "POST"))
{ {
con_info->postprocessor = con_info->postprocessor =
MHD_create_post_processor (connection, POSTBUFFERSIZE, MHD_create_post_processor (connection,
iterate_post, (void *) con_info); POSTBUFFERSIZE,
iterate_post,
(void *) con_info);
if (NULL == con_info->postprocessor) if (NULL == con_info->postprocessor)
{ {
free (con_info); free (con_info);
return MHD_NO; return MHD_NO;
} }
con_info->connectiontype = POST; con_info->connectiontype = POST;
} }
else else
{
con_info->connectiontype = GET; con_info->connectiontype = GET;
}
*req_cls = (void *) con_info; *req_cls = (void *) con_info;
return MHD_YES; return MHD_YES;
} }
if (GET == con_info->connectiontype)
if (0 == strcmp (method, "GET"))
{ {
return send_page (connection, askpage); return send_page (connection,
askpage);
} }
if (POST == con_info->connectiontype)
if (0 == strcmp (method, "POST"))
{ {
struct connection_info_struct *con_info = *req_cls; struct connection_info_struct *con_info = *req_cls;
if (*upload_data_size != 0) if (0 != *upload_data_size)
{ {
if (MHD_YES != if (MHD_YES !=
MHD_post_process (con_info->postprocessor, MHD_post_process (con_info->postprocessor,
@@ -183,14 +193,14 @@ answer_to_connection (void *cls, struct MHD_Connection *connection,
*upload_data_size)) *upload_data_size))
return MHD_NO; return MHD_NO;
*upload_data_size = 0; *upload_data_size = 0;
return MHD_YES; return MHD_YES;
} }
else if (NULL != con_info->answerstring) else if (NULL != con_info->answerstring)
return send_page (connection, con_info->answerstring); return send_page (connection,
con_info->answerstring);
} }
return send_page (connection,
return send_page (connection, errorpage); errorpage);
} }
@@ -202,14 +212,12 @@ main (void)
daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD, daemon = MHD_start_daemon (MHD_USE_AUTO | MHD_USE_INTERNAL_POLLING_THREAD,
PORT, NULL, NULL, PORT, NULL, NULL,
&answer_to_connection, NULL, &answer_to_connection, NULL,
MHD_OPTION_NOTIFY_COMPLETED, request_completed, MHD_OPTION_NOTIFY_COMPLETED,
NULL, MHD_OPTION_END); &request_completed, NULL,
MHD_OPTION_END);
if (NULL == daemon) if (NULL == daemon)
return 1; return 1;
(void) getchar (); (void) getchar ();
MHD_stop_daemon (daemon); MHD_stop_daemon (daemon);
return 0; return 0;
} }