From 98eda2caba4d8bea667bfb015f0bc4426b7f0ee0 Mon Sep 17 00:00:00 2001 From: Andreas Baumann Date: Fri, 19 Jun 2015 08:34:28 +0200 Subject: proper PNG handling and parsing AJAX answer for status page in web client --- src/index.html | 43 +++++++++++++++++++------ src/webserver.c | 98 +++++++++++++++++++++++++++++++++++++++------------------ 2 files changed, 101 insertions(+), 40 deletions(-) (limited to 'src') diff --git a/src/index.html b/src/index.html index d828762..0f4f0a5 100644 --- a/src/index.html +++ b/src/index.html @@ -11,10 +11,12 @@ $( "#tabs" ).tabs({ cache: false, beforeLoad: function( event, ui ) { - ui.jqXHR.fail( function( ) { - ui.panel.html( - "Couldn't load status of biruda. Check if biruda is running and the webserver is enabled." ); - }); + //~ ui.jqXHR.fail( function( ) { + //~ ui.panel.html( + //~ "Couldn't load status of biruda. Check if biruda is running and the webserver is enabled." ); + //~ }); + event.preventDefault( ); + return false; } }); }); @@ -24,21 +26,44 @@ }); function refresh_status( ) { - $('#tabs').tabs( 'load', 0 ); + $.get( "/status", { } ) + .done( function( data ) { + var lines = data.split( '\n' ); + var html = "\n"; + $.each( lines, function( ) { + var fields = this.split( ' ' ); + var type = fields[0]; + var name = fields[1]; + var status = fields[2]; + if( type == "worker" ) { + html += "\n"; + } + }); + html += "
workerstatus
" + name + "" + status + "
\n"; + $('#tabs-1').html( html ); + }); + } - + var refreshId =setInterval( refresh_status, 5000 ); + +
+
+

.. loading status ..

+

Morbi tincidunt, dui sit amet facilisis feugiat, odio metus gravida ante, ut pharetra massa metus id nunc. Duis scelerisque molestie turpis. Sed fringilla, massa eget luctus malesuada, metus eros molestie lectus, ut tempus eros massa ut dolor. Aenean aliquet fringilla sem. Suspendisse sed ligula in ligula suscipit aliquam. Praesent in eros vestibulum mi adipiscing adipiscing. Morbi facilisis. Curabitur ornare consequat nunc. Aenean vel metus. Ut posuere viverra nulla. Aliquam erat volutpat. Pellentesque convallis. Maecenas feugiat, tellus pellentesque pretium posuere, felis lorem euismod felis, eu ornare leo nisi vel felis. Mauris consectetur tortor et purus.

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, "

This is biruda, the REST api. You can access the web interface here.

" ); + 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 ) { -- cgit v1.2.3-54-g00ecf