diff options
Diffstat (limited to 'src/webserver.c')
-rw-r--r-- | src/webserver.c | 98 |
1 files changed, 67 insertions, 31 deletions
diff --git a/src/webserver.c b/src/webserver.c index 02353a5..331dfd9 100644 --- a/src/webserver.c +++ b/src/webserver.c @@ -50,7 +50,12 @@ static struct MHD_Response *wrap_response( size_t size, void *buffer, enum MHD_R } #endif - + +static const char *CONTENT_TYPE_TEXT_HTML = "text/html"; +static const char *CONTENT_TYPE_TEXT_JS = "text/javascript"; +static const char *CONTENT_TYPE_TEXT_CSS = "text/css"; +static const char *CONTENT_TYPE_IMAGE_PNG = "image/png"; + static int handle_request( 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 **ptr ) @@ -62,7 +67,11 @@ static int handle_request( void *cls, struct MHD_Connection *connection, if( strcmp( method, "GET" ) == 0 ) { char *biruda_msg = malloc( 512 * 1024 ); + size_t biruda_msg_len = 0; + const char *content_type = CONTENT_TYPE_TEXT_HTML; + biruda_msg[0] = '\0'; + if( strcmp( url, "/status" ) == 0 ) { for( int pos = 0; pos < MAX_COORDINATORS; pos++ ) { coordinator_t *c = &coordinator[pos]; @@ -87,7 +96,10 @@ static int handle_request( void *cls, struct MHD_Connection *connection, strncat( biruda_msg, part, strlen( part ) ); } } - } + } + + biruda_msg_len = strlen( biruda_msg ); + } else if( strcmp( url, "/worker" ) == 0 ) { const char *op = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "op" ); @@ -116,68 +128,92 @@ static int handle_request( void *cls, struct MHD_Connection *connection, } else { return MHD_NO; } - } + } + + biruda_msg_len = strlen( biruda_msg ); } else if( strcmp( url, "/web" ) == 0 ) { strncpy( biruda_msg, (char *)index_html, index_html_len ); biruda_msg[index_html_len] = '\0'; + biruda_msg_len = strlen( biruda_msg ); } else if( strcmp( url, "/web/jquery-ui.css" ) == 0 ) { strncpy( biruda_msg, (char *)jquery_ui_css, jquery_ui_css_len ); biruda_msg[jquery_ui_css_len] = '\0'; + content_type = CONTENT_TYPE_TEXT_CSS; + biruda_msg_len = strlen( biruda_msg ); } else if( strcmp( url, "/web/jquery.js" ) == 0 ) { strncpy( biruda_msg, (char *)jquery_js, jquery_js_len ); biruda_msg[jquery_js_len] = '\0'; + content_type = CONTENT_TYPE_TEXT_JS; + biruda_msg_len = strlen( biruda_msg ); } else if( strcmp( url, "/web/jquery-ui.js" ) == 0 ) { strncpy( biruda_msg, (char *)jquery_ui_js, jquery_ui_js_len ); biruda_msg[jquery_ui_js_len] = '\0'; + content_type = CONTENT_TYPE_TEXT_JS; + biruda_msg_len = strlen( biruda_msg ); } else if( strcmp( url, "/web/images/ui-bg_flat_0_aaaaaa_40x100.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_flat_0_aaaaaa_40x100_png, images_ui_bg_flat_0_aaaaaa_40x100_png_len ); - images_ui_bg_flat_0_aaaaaa_40x100_png[images_ui_bg_flat_0_aaaaaa_40x100_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_flat_0_aaaaaa_40x100_png, images_ui_bg_flat_0_aaaaaa_40x100_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_flat_0_aaaaaa_40x100_png_len; } else if( strcmp( url, "/web/images/ui-bg_flat_75_ffffff_40x100.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_flat_75_ffffff_40x100_png, images_ui_bg_flat_75_ffffff_40x100_png_len ); - images_ui_bg_flat_75_ffffff_40x100_png[images_ui_bg_flat_75_ffffff_40x100_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_flat_75_ffffff_40x100_png, images_ui_bg_flat_75_ffffff_40x100_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_flat_75_ffffff_40x100_png_len; } else if( strcmp( url, "/web/images/ui-bg_glass_55_fbf9ee_1x400.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_glass_55_fbf9ee_1x400_png, images_ui_bg_glass_55_fbf9ee_1x400_png_len ); - images_ui_bg_glass_55_fbf9ee_1x400_png[images_ui_bg_glass_55_fbf9ee_1x400_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_glass_55_fbf9ee_1x400_png, images_ui_bg_glass_55_fbf9ee_1x400_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_glass_55_fbf9ee_1x400_png_len; } else if( strcmp( url, "/web/images/ui-bg_glass_65_ffffff_1x400.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_glass_65_ffffff_1x400_png, images_ui_bg_glass_65_ffffff_1x400_png_len ); - images_ui_bg_glass_65_ffffff_1x400_png[images_ui_bg_glass_65_ffffff_1x400_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_glass_65_ffffff_1x400_png, images_ui_bg_glass_65_ffffff_1x400_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_glass_65_ffffff_1x400_png_len; } else if( strcmp( url, "/web/images/ui-bg_glass_75_dadada_1x400.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_glass_75_dadada_1x400_png, images_ui_bg_glass_75_dadada_1x400_png_len ); - images_ui_bg_glass_75_dadada_1x400_png[images_ui_bg_glass_75_dadada_1x400_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_glass_75_dadada_1x400_png, images_ui_bg_glass_75_dadada_1x400_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_glass_75_dadada_1x400_png_len; } else if( strcmp( url, "/web/images/ui-bg_glass_75_e6e6e6_1x400.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_glass_75_e6e6e6_1x400_png, images_ui_bg_glass_75_e6e6e6_1x400_png_len ); - images_ui_bg_glass_75_e6e6e6_1x400_png[images_ui_bg_glass_75_e6e6e6_1x400_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_glass_75_e6e6e6_1x400_png, images_ui_bg_glass_75_e6e6e6_1x400_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_glass_75_e6e6e6_1x400_png_len; } else if( strcmp( url, "/web/images/ui-bg_glass_95_fef1ec_1x400.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_glass_95_fef1ec_1x400_png, images_ui_bg_glass_95_fef1ec_1x400_png_len ); - images_ui_bg_glass_95_fef1ec_1x400_png[images_ui_bg_glass_95_fef1ec_1x400_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_glass_95_fef1ec_1x400_png, images_ui_bg_glass_95_fef1ec_1x400_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_glass_95_fef1ec_1x400_png_len; } else if( strcmp( url, "/web/images/ui-bg_highlight-soft_75_cccccc_1x100.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_bg_highlight_soft_75_cccccc_1x100_png, images_ui_bg_highlight_soft_75_cccccc_1x100_png_len ); - images_ui_bg_highlight_soft_75_cccccc_1x100_png[images_ui_bg_highlight_soft_75_cccccc_1x100_png_len] = '\0'; + memcpy( biruda_msg, images_ui_bg_highlight_soft_75_cccccc_1x100_png, images_ui_bg_highlight_soft_75_cccccc_1x100_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_bg_highlight_soft_75_cccccc_1x100_png_len; } else if( strcmp( url, "/web/images/ui-icons_222222_256x240.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_icons_222222_256x240_png, images_ui_icons_222222_256x240_png_len ); - images_ui_icons_222222_256x240_png[images_ui_icons_222222_256x240_png_len] = '\0'; + memcpy( biruda_msg, images_ui_icons_222222_256x240_png, images_ui_icons_222222_256x240_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_icons_222222_256x240_png_len; } else if( strcmp( url, "/web/images/ui-icons_2e83ff_256x240.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_icons_2e83ff_256x240_png, images_ui_icons_2e83ff_256x240_png_len ); - images_ui_icons_2e83ff_256x240_png[images_ui_icons_2e83ff_256x240_png_len] = '\0'; + memcpy( biruda_msg, images_ui_icons_2e83ff_256x240_png, images_ui_icons_2e83ff_256x240_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_icons_2e83ff_256x240_png_len; } else if( strcmp( url, "/web/images/ui-icons_454545_256x240.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_icons_454545_256x240_png, images_ui_icons_454545_256x240_png_len ); - images_ui_icons_454545_256x240_png[images_ui_icons_454545_256x240_png_len] = '\0'; + memcpy( biruda_msg, images_ui_icons_454545_256x240_png, images_ui_icons_454545_256x240_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_icons_454545_256x240_png_len; } else if( strcmp( url, "/web/images/ui-icons_888888_256x240.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_icons_888888_256x240_png, images_ui_icons_888888_256x240_png_len ); - images_ui_icons_888888_256x240_png[images_ui_icons_888888_256x240_png_len] = '\0'; + memcpy( biruda_msg, images_ui_icons_888888_256x240_png, images_ui_icons_888888_256x240_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_icons_888888_256x240_png_len; } else if( strcmp( url, "/web/images/ui-icons_cd0a0a_256x240.png" ) == 0 ) { - strncpy( biruda_msg, (char *)images_ui_icons_cd0a0a_256x240_png, images_ui_icons_cd0a0a_256x240_png_len ); - images_ui_icons_cd0a0a_256x240_png[images_ui_icons_cd0a0a_256x240_png_len] = '\0'; + memcpy( biruda_msg, images_ui_icons_cd0a0a_256x240_png, images_ui_icons_cd0a0a_256x240_png_len ); + content_type = CONTENT_TYPE_IMAGE_PNG; + biruda_msg_len = images_ui_icons_cd0a0a_256x240_png_len; } else { strcat( biruda_msg, "<html><body><p>This is biruda, the REST api. You can access the web interface <a href=\"/web\">here</a>.</p></body></html>" ); + biruda_msg_len = strlen( biruda_msg ); } - response = MHD_create_response_from_buffer( strlen( biruda_msg ), + response = MHD_create_response_from_buffer( biruda_msg_len, (void *)biruda_msg, MHD_RESPMEM_MUST_COPY ); - free( biruda_msg ); + ret = MHD_add_response_header( response, "Content-Type", content_type ); ret = MHD_queue_response( connection, MHD_HTTP_OK, response ); MHD_destroy_response( response ); + free( biruda_msg ); } else if( strcmp( method, "POST" ) == 0 ) { |