From 733378679f1b8a067804fdb46c03d17a39593f27 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Sun, 22 Mar 2015 22:38:01 +0000 Subject: [PATCH 1/5] Prettify httpd top matter in the web demo --- examples/cc26xx/cc26xx-web-demo/httpd-simple.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c index 281567acf..f02ae7897 100644 --- a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c +++ b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c @@ -395,12 +395,12 @@ PT_THREAD(generate_top_matter(struct httpd_state *s, const char *title, s->page = list_head(pages_list); PT_WAIT_THREAD(&s->top_matter_pt, - enqueue_chunk(s, 0, "[ %s ]", + enqueue_chunk(s, 0, "[ %s ]", s->page->filename, s->page->title)); for(s->page = s->page->next; s->page != NULL; s->page = s->page->next) { PT_WAIT_THREAD(&s->top_matter_pt, - enqueue_chunk(s, 0, " | [ %s ]", + enqueue_chunk(s, 0, " | [ %s ]", s->page->filename, s->page->title)); } @@ -1279,8 +1279,8 @@ PROCESS_THREAD(httpd_simple_process, ev, data) init(); snprintf(http_mqtt_a, IBM_QUICKSTART_LINK_LEN, - "[ IBM Quickstart ]", + "[ IBM Quickstart ]", linkaddr_node_addr.u8[0], linkaddr_node_addr.u8[1], linkaddr_node_addr.u8[2], linkaddr_node_addr.u8[5], linkaddr_node_addr.u8[6], linkaddr_node_addr.u8[7]); From 90a3cd8e14abb414709c394bd26d576bd5206499 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Mon, 23 Mar 2015 04:10:33 +0000 Subject: [PATCH 2/5] Change the way we handle HTTP response headers --- .../cc26xx/cc26xx-web-demo/httpd-simple.c | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c index f02ae7897..04c3e7208 100644 --- a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c +++ b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c @@ -163,11 +163,6 @@ static const char *http_header_srv_str[] = { NULL }; -static const char *http_header_redir_location[] = { - "Location: /config.html\r\n", - NULL -}; - static const char *http_header_con_close[] = { CONN_CLOSE, NULL @@ -1003,7 +998,8 @@ PT_THREAD(send_string(struct httpd_state *s, const char *str)) /*---------------------------------------------------------------------------*/ static PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr, - const char *content_type, const char **additional)) + const char *content_type, const char *redir, + const char **additional)) { PT_BEGIN(&s->generate_pt); @@ -1013,6 +1009,11 @@ PT_THREAD(send_headers(struct httpd_state *s, const char *statushdr, PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, *(s->ptr))); } + if(redir) { + PT_WAIT_THREAD(&s->generate_pt, + enqueue_chunk(s, 0, "Location: %s\r\n", redir)); + } + if(additional) { for(s->ptr = additional; *(s->ptr) != NULL; s->ptr++) { PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, *(s->ptr))); @@ -1041,25 +1042,30 @@ PT_THREAD(handle_output(struct httpd_state *s)) if(s->return_code == RETURN_CODE_OK) { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_302, http_content_type_plain, - http_header_redir_location)); + s->filename, + NULL)); } else if(s->return_code == RETURN_CODE_LR) { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_411, http_content_type_plain, + NULL, http_header_con_close)); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Content-Length Required\n")); } else if(s->return_code == RETURN_CODE_TL) { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_413, http_content_type_plain, + NULL, http_header_con_close)); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Content-Length too Large\n")); } else if(s->return_code == RETURN_CODE_SU) { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_503, http_content_type_plain, + NULL, http_header_con_close)); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Service Unavailable\n")); } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_400, http_content_type_plain, + NULL, http_header_con_close)); PT_WAIT_THREAD(&s->outputpt, send_string(s, "Bad Request\n")); } @@ -1069,6 +1075,7 @@ PT_THREAD(handle_output(struct httpd_state *s)) strncpy(s->filename, "/notfound.html", sizeof(s->filename)); PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_404, http_content_type_html, + NULL, http_header_con_close)); PT_WAIT_THREAD(&s->outputpt, send_string(s, NOT_FOUND)); @@ -1077,6 +1084,7 @@ PT_THREAD(handle_output(struct httpd_state *s)) } else { PT_WAIT_THREAD(&s->outputpt, send_headers(s, http_header_200, http_content_type_html, + NULL, http_header_con_close)); PT_WAIT_THREAD(&s->outputpt, s->script(s)); } From 54c1cd05e88aa7a7d143902f267878ff8367696c Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Mon, 23 Mar 2015 04:11:57 +0000 Subject: [PATCH 3/5] Simplify the page name to script mapping --- .../cc26xx/cc26xx-web-demo/httpd-simple.c | 33 +++++++++++-------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c index 04c3e7208..4f63340b1 100644 --- a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c +++ b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c @@ -192,37 +192,49 @@ static const char config_div_left[] = "
"; static const char config_div_right[] = "
"; static const char config_div_close[] = "
"; /*---------------------------------------------------------------------------*/ +static char generate_index(struct httpd_state *s); +static char generate_config(struct httpd_state *s); +/*---------------------------------------------------------------------------*/ typedef struct page { struct page *next; char *filename; char *title; + char (*script)(struct httpd_state *s); } page_t; static page_t http_index_page = { NULL, "index.html", "Index", + generate_index, }; static page_t http_dev_cfg_page = { NULL, "config.html", "Device Config", + generate_config, }; #if CC26XX_WEB_DEMO_NET_UART +static char generate_net_uart_config(struct httpd_state *s); + static page_t http_net_cfg_page = { NULL, - "net.html", + "netu.html", "Net-UART Config", + generate_net_uart_config, }; #endif #if CC26XX_WEB_DEMO_MQTT_CLIENT +static char generate_mqtt_config(struct httpd_state *s); + static page_t http_mqtt_cfg_page = { NULL, "mqtt.html", "MQTT/IBM Cloud Config", + generate_mqtt_config, }; #endif /*---------------------------------------------------------------------------*/ @@ -969,18 +981,13 @@ parse_post_request_chunk(char *buf, int buf_len, int last_chunk) static httpd_simple_script_t get_script(const char *name) { - if(strlen(name) == 10 && strncmp(name, "index.html", 10) == 0) { - return generate_index; - } else if(strlen(name) == 11 && strncmp(name, "config.html", 11) == 0) { - return generate_config; -#if CC26XX_WEB_DEMO_MQTT_CLIENT - } else if(strlen(name) == 9 && strncmp(name, "mqtt.html", 9) == 0) { - return generate_mqtt_config; -#endif -#if CC26XX_WEB_DEMO_NET_UART - } else if(strlen(name) == 8 && strncmp(name, "net.html", 8) == 0) { - return generate_net_uart_config; -#endif + page_t *page; + + for(page = list_head(pages_list); page != NULL; + page = list_item_next(page)) { + if(strncmp(name, page->filename, strlen(page->filename)) == 0) { + return page->script; + } } return NULL; From 7637164ae7d7169c095af73a3e3b8a83e09da2a0 Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Mon, 23 Mar 2015 04:13:50 +0000 Subject: [PATCH 4/5] Parse filename for POST requests so we can return it in the "Location:" header subsequently --- examples/cc26xx/cc26xx-web-demo/httpd-simple.c | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c index 4f63340b1..dd6bf618d 100644 --- a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c +++ b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c @@ -1124,6 +1124,15 @@ PT_THREAD(handle_input(struct httpd_state *s)) } } else if(strncasecmp(s->inputbuf, http_post, 5) == 0) { s->request_type = REQUEST_TYPE_POST; + PSOCK_READTO(&s->sin, ISO_space); + + if(s->inputbuf[0] != ISO_slash) { + PSOCK_CLOSE_EXIT(&s->sin); + } + + s->inputbuf[PSOCK_DATALEN(&s->sin) - 1] = 0; + strncpy(s->filename, s->inputbuf, sizeof(s->filename)); + /* POST: Read out the rest of the line and ignore it */ PSOCK_READTO(&s->sin, ISO_nl); From 7730215f991068c6d5d818d7e6cb800b15bfc1ff Mon Sep 17 00:00:00 2001 From: George Oikonomou Date: Mon, 23 Mar 2015 04:14:35 +0000 Subject: [PATCH 5/5] Point all form action attributes to the current html page --- examples/cc26xx/cc26xx-web-demo/httpd-simple.c | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c index dd6bf618d..29c22d22b 100644 --- a/examples/cc26xx/cc26xx-web-demo/httpd-simple.c +++ b/examples/cc26xx/cc26xx-web-demo/httpd-simple.c @@ -538,7 +538,8 @@ PT_THREAD(generate_config(struct httpd_state *s)) PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, - "
generate_pt, enqueue_chunk(s, 0, "method=\"post\" enctype=\"")); PT_WAIT_THREAD(&s->generate_pt, @@ -577,7 +578,8 @@ PT_THREAD(generate_config(struct httpd_state *s)) PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, "

Actions

")); PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, - "generate_pt, enqueue_chunk(s, 0, "method=\"post\" enctype=\"")); PT_WAIT_THREAD(&s->generate_pt, @@ -614,7 +616,8 @@ PT_THREAD(generate_mqtt_config(struct httpd_state *s)) PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, - "generate_pt, enqueue_chunk(s, 0, "method=\"post\" enctype=\"")); PT_WAIT_THREAD(&s->generate_pt, @@ -754,7 +757,8 @@ PT_THREAD(generate_mqtt_config(struct httpd_state *s)) enqueue_chunk(s, 0, "
")); PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, - "
generate_pt, enqueue_chunk(s, 0, "method=\"post\" enctype=\"")); PT_WAIT_THREAD(&s->generate_pt, @@ -793,7 +797,8 @@ PT_THREAD(generate_net_uart_config(struct httpd_state *s)) PT_WAIT_THREAD(&s->generate_pt, enqueue_chunk(s, 0, - "generate_pt, enqueue_chunk(s, 0, "method=\"post\" enctype=\"")); PT_WAIT_THREAD(&s->generate_pt,