From 5becf241a38162ab48b8f3b01f40d4d7c67d2e3e Mon Sep 17 00:00:00 2001 From: Scott Price Date: Tue, 27 Sep 2016 08:02:28 -0500 Subject: [PATCH 1/9] Added support for PUT, PATCH, and DELETE httpd methods. --- core/httpd.c | 9 +++++++++ include/httpd.h | 3 +++ 2 files changed, 12 insertions(+) diff --git a/core/httpd.c b/core/httpd.c index bb5b5f5b..0b963182 100644 --- a/core/httpd.c +++ b/core/httpd.c @@ -588,6 +588,15 @@ static void ICACHE_FLASH_ATTR httpdParseHeader(char *h, HttpdConnData *conn) { } else if (strncmp(h, "POST ", 5)==0) { conn->requestType = HTTPD_METHOD_POST; firstLine=1; + } else if (strncmp(h, "PUT ", 4)==0) { + conn->requestType = HTTPD_METHOD_PUT; + firstLine=1; + } else if (strncmp(h, "PATCH ", 6)==0) { + conn->requestType = HTTPD_METHOD_PATCH; + firstLine=1; + } else if (strncmp(h, "DELETE ", 7)==0) { + conn->requestType = HTTPD_METHOD_DELETE; + firstLine=1; } if (firstLine) { diff --git a/include/httpd.h b/include/httpd.h index 73745ac7..a669624d 100644 --- a/include/httpd.h +++ b/include/httpd.h @@ -21,6 +21,9 @@ #define HTTPD_METHOD_GET 1 #define HTTPD_METHOD_POST 2 +#define HTTPD_METHOD_PUT 3 +#define HTTPD_METHOD_PATCH 4 +#define HTTPD_METHOD_DELETE 5 #define HTTPD_TRANSFER_CLOSE 0 #define HTTPD_TRANSFER_CHUNKED 1 From 062272517cda118cd6ce8a74e6795021876a33d0 Mon Sep 17 00:00:00 2001 From: "Scott L. Price" Date: Sun, 9 Oct 2016 08:17:15 -0500 Subject: [PATCH 2/9] Added the option to build this for ssl. --- Makefile | 5 +++++ core/httpd-nonos.c | 23 +++++++++++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 59a6db62..a27a0fd8 100644 --- a/Makefile +++ b/Makefile @@ -12,6 +12,7 @@ YUI-COMPRESSOR ?= /usr/bin/yui-compressor USE_HEATSHRINK ?= yes HTTPD_WEBSOCKETS ?= yes USE_OPENSDK ?= no +USE_SSL ?= no HTTPD_MAX_CONNECTIONS ?= 4 #For FreeRTOS HTTPD_STACKSIZE ?= 2048 @@ -152,6 +153,10 @@ ifeq ("$(HTTPD_WEBSOCKETS)","yes") CFLAGS += -DHTTPD_WEBSOCKETS endif +ifeq ("$(USE_SSL)","yes") +CFLAGS += -DUSE_SSL +endif + vpath %.c $(SRC_DIR) define compile-objects diff --git a/core/httpd-nonos.c b/core/httpd-nonos.c index 410aa309..b070f9f1 100644 --- a/core/httpd-nonos.c +++ b/core/httpd-nonos.c @@ -53,15 +53,23 @@ static void ICACHE_FLASH_ATTR platConnCb(void *arg) { espconn_regist_disconcb(conn, platDisconCb); espconn_regist_sentcb(conn, platSentCb); } else { +#ifdef USE_SSL + espconn_secure_disconnect(conn); +#else espconn_disconnect(conn); +#endif } } int ICACHE_FLASH_ATTR httpdPlatSendData(ConnTypePtr conn, char *buff, int len) { int r; - r=espconn_sent(conn, (uint8_t*)buff, len); - return (r>=0); +#ifdef USE_SSL + r=espconn_secure_send(conn, (uint8_t*)buff, len); +#else + r=espconn_sent(conn, (uint8_t*)buff, len); +#endif + return (r>=0); } void ICACHE_FLASH_ATTR httpdPlatDisconnect(ConnTypePtr conn) { @@ -80,8 +88,15 @@ void ICACHE_FLASH_ATTR httpdPlatInit(int port, int maxConnCt) { httpdTcp.local_port=port; httpdConn.proto.tcp=&httpdTcp; espconn_regist_connectcb(&httpdConn, platConnCb); - espconn_accept(&httpdConn); - espconn_tcp_set_max_con_allow(&httpdConn, maxConnCt); +#ifdef USE_SSL + //espconn_secure_set_default_certificate(default_certificate, default_certificate_len); + //espconn_secure_set_default_private_key(default_private_key, default_private_key_len); + espconn_secure_accept(&httpdConn); +#else + espconn_accept(&httpdConn); +#endif + espconn_tcp_set_max_con_allow(&httpdConn, maxConnCt); + } From c5c0bd083d4703b25e936b57bfa4022851b57bfb Mon Sep 17 00:00:00 2001 From: "Scott L. Price" Date: Tue, 25 Oct 2016 07:16:50 -0500 Subject: [PATCH 3/9] This allows for a single firmware image to be put in either user1 or user2. This is for the case when we are using a 1MB image with a 4MB flash where the user1 and user2 images are identical. --- include/cgiflash.h | 1 + util/cgiflash.c | 15 ++++++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/include/cgiflash.h b/include/cgiflash.h index ef29ca39..4e11048f 100644 --- a/include/cgiflash.h +++ b/include/cgiflash.h @@ -5,6 +5,7 @@ #define CGIFLASH_TYPE_FW 0 #define CGIFLASH_TYPE_ESPFS 1 +#define CGIFLASH_TYPE_FW_SINGLE 2 // This is where user1 & user2 are identical typedef struct { int type; diff --git a/util/cgiflash.c b/util/cgiflash.c index 1a74c2ed..3921e619 100644 --- a/util/cgiflash.c +++ b/util/cgiflash.c @@ -171,7 +171,20 @@ int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { state->address=def->fw2Pos; } } - } else if (def->type==CGIFLASH_TYPE_FW && checkBinHeader(connData->post->buff)) { + } else if (def->type==CGIFLASH_TYPE_FW_SINGLE && checkBinHeader(connData->post->buff)) { + if (connData->post->len > def->fwSize) { + state->err="Firmware image too large"; + state->state=FLST_ERROR; + } else { + state->len=connData->post->len; + if (system_upgrade_userbin_check() == 1) { + state->address=def->fw1Pos; + } else { + state->address=def->fw2Pos; + } + state->state=FLST_WRITE; + } + } else if (def->type==CGIFLASH_TYPE_FW && checkBinHeader(connData->post->buff)) { if (connData->post->len > def->fwSize) { state->err="Firmware image too large"; state->state=FLST_ERROR; From 2c29daee86fbec52a6f7ddaf13cd7189fd84a3fd Mon Sep 17 00:00:00 2001 From: "Scott L. Price" Date: Tue, 25 Oct 2016 07:30:43 -0500 Subject: [PATCH 4/9] It won't overwrite the running image now. --- util/cgiflash.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/util/cgiflash.c b/util/cgiflash.c index 3921e619..9bb533bb 100644 --- a/util/cgiflash.c +++ b/util/cgiflash.c @@ -25,7 +25,7 @@ Some flash handling cgi routines. Used for updating the ESPFS/OTA image. #include "espfs.h" #ifndef UPGRADE_FLAG_FINISH -#define UPGRADE_FLAG_FINISH 0x02 +#define UPGRADE_FLAG_FINISH 0x02 #endif // Check that the header of the firmware blob looks like actual firmware... @@ -178,8 +178,10 @@ int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { } else { state->len=connData->post->len; if (system_upgrade_userbin_check() == 1) { + httpd_printf("Flashing image into user1\n"); state->address=def->fw1Pos; } else { + httpd_printf("Flashing image into user2\n"); state->address=def->fw2Pos; } state->state=FLST_WRITE; @@ -188,7 +190,10 @@ int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { if (connData->post->len > def->fwSize) { state->err="Firmware image too large"; state->state=FLST_ERROR; - } else { + } else if (system_upgrade_userbin_check() == 0) { + state->err="Can't overwrite running image"; + state->state=FLST_ERROR; + } else { state->len=connData->post->len; state->address=def->fw1Pos; state->state=FLST_WRITE; From e6f008f728c4961a88a5243047bca2897954a364 Mon Sep 17 00:00:00 2001 From: Scott Price Date: Tue, 25 Oct 2016 08:02:31 -0500 Subject: [PATCH 5/9] Revert "This allows for a single firmware image to be put in either user1 or user2. This" This reverts commit c5c0bd083d4703b25e936b57bfa4022851b57bfb. --- include/cgiflash.h | 1 - util/cgiflash.c | 17 +---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/include/cgiflash.h b/include/cgiflash.h index 4e11048f..ef29ca39 100644 --- a/include/cgiflash.h +++ b/include/cgiflash.h @@ -5,7 +5,6 @@ #define CGIFLASH_TYPE_FW 0 #define CGIFLASH_TYPE_ESPFS 1 -#define CGIFLASH_TYPE_FW_SINGLE 2 // This is where user1 & user2 are identical typedef struct { int type; diff --git a/util/cgiflash.c b/util/cgiflash.c index 9bb533bb..b38c55ee 100644 --- a/util/cgiflash.c +++ b/util/cgiflash.c @@ -171,22 +171,7 @@ int ICACHE_FLASH_ATTR cgiUploadFirmware(HttpdConnData *connData) { state->address=def->fw2Pos; } } - } else if (def->type==CGIFLASH_TYPE_FW_SINGLE && checkBinHeader(connData->post->buff)) { - if (connData->post->len > def->fwSize) { - state->err="Firmware image too large"; - state->state=FLST_ERROR; - } else { - state->len=connData->post->len; - if (system_upgrade_userbin_check() == 1) { - httpd_printf("Flashing image into user1\n"); - state->address=def->fw1Pos; - } else { - httpd_printf("Flashing image into user2\n"); - state->address=def->fw2Pos; - } - state->state=FLST_WRITE; - } - } else if (def->type==CGIFLASH_TYPE_FW && checkBinHeader(connData->post->buff)) { + } else if (def->type==CGIFLASH_TYPE_FW && checkBinHeader(connData->post->buff)) { if (connData->post->len > def->fwSize) { state->err="Firmware image too large"; state->state=FLST_ERROR; From cd876003b3d057ff06a3e8cc2f70252ccd2f3ffe Mon Sep 17 00:00:00 2001 From: Chen YewMing Date: Sun, 1 Jan 2017 18:32:35 +0800 Subject: [PATCH 6/9] Ignore vim swap files --- .gitignore | 3 ++- Makefile | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 5e14a6fa..0d1e171c 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ espfs/espfstest/*.o espfs/espfstest/espfstest *.DS_Store html_compressed/ -libwebpages-espfs.a \ No newline at end of file +libwebpages-espfs.a +*.swp diff --git a/Makefile b/Makefile index 59a6db62..8a2e4993 100644 --- a/Makefile +++ b/Makefile @@ -180,6 +180,8 @@ checkdirs: $(BUILD_DIR) $(BUILD_DIR): $(Q) mkdir -p $@ +#ignore vim swap files +FIND_OPTIONS = -not -iname '*.swp' webpages.espfs: $(HTMLDIR) espfs/mkespfsimage/mkespfsimage ifeq ("$(COMPRESS_W_YUI)","yes") @@ -191,9 +193,9 @@ ifeq ("$(COMPRESS_W_YUI)","yes") $(Q) awk "BEGIN {printf \"YUI compression ratio was: %.2f%%\\n\", (`du -b -s html_compressed/ | sed 's/\([0-9]*\).*/\1/'`/`du -b -s ../html/ | sed 's/\([0-9]*\).*/\1/'`)*100}" # mkespfsimage will compress html, css, svg and js files with gzip by default if enabled # override with -g cmdline parameter - $(Q) cd html_compressed; find . | $(THISDIR)/espfs/mkespfsimage/mkespfsimage > $(THISDIR)/webpages.espfs; cd ..; + $(Q) cd html_compressed; find . $(FIND_OPTIONS) | $(THISDIR)/espfs/mkespfsimage/mkespfsimage > $(THISDIR)/webpages.espfs; cd ..; else - $(Q) cd ../html; find . | $(THISDIR)/espfs/mkespfsimage/mkespfsimage > $(THISDIR)/webpages.espfs; cd .. + $(Q) cd ../html; find . $(FIND_OPTIONS) | $(THISDIR)/espfs/mkespfsimage/mkespfsimage > $(THISDIR)/webpages.espfs; cd .. endif libwebpages-espfs.a: webpages.espfs From 941f4b1dcef7dea70a3354edf61261cb19c11607 Mon Sep 17 00:00:00 2001 From: Chen YewMing Date: Mon, 2 Jan 2017 00:18:16 +0800 Subject: [PATCH 7/9] Fix possible memory leak --- core/httpd.c | 11 ++++++----- espfs/espfs.c | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/core/httpd.c b/core/httpd.c index bb5b5f5b..9a4ee65b 100644 --- a/core/httpd.c +++ b/core/httpd.c @@ -683,15 +683,16 @@ void ICACHE_FLASH_ATTR httpdRecvCb(ConnTypePtr rconn, char *remIp, int remPort, int x, r; char *p, *e; httpdPlatLock(); - char *sendBuff=malloc(HTTPD_MAX_SENDBUFF_LEN); - if (sendBuff==NULL) { - printf("Malloc sendBuff failed!\n"); + + HttpdConnData *conn=httpdFindConnData(rconn, remIp, remPort); + if (conn==NULL) { httpdPlatUnlock(); return; } - HttpdConnData *conn=httpdFindConnData(rconn, remIp, remPort); - if (conn==NULL) { + char *sendBuff=malloc(HTTPD_MAX_SENDBUFF_LEN); + if (sendBuff==NULL) { + printf("Malloc sendBuff failed!\n"); httpdPlatUnlock(); return; } diff --git a/espfs/espfs.c b/espfs/espfs.c index d5270d2a..e179cb95 100644 --- a/espfs/espfs.c +++ b/espfs/espfs.c @@ -183,6 +183,7 @@ EspFsFile ICACHE_FLASH_ATTR *espFsOpen(char *fileName) { #endif } else { httpd_printf("Invalid compression: %d\n", h.compression); + free(r); return NULL; } return r; From c6a57b55f5845997ff401bb63e16e505c7a89214 Mon Sep 17 00:00:00 2001 From: "Scott L. Price" Date: Wed, 25 Jan 2017 18:35:56 -0600 Subject: [PATCH 8/9] More work adding to the SSL code to get this working on https. --- core/httpd-nonos.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/httpd-nonos.c b/core/httpd-nonos.c index b070f9f1..09705c00 100644 --- a/core/httpd-nonos.c +++ b/core/httpd-nonos.c @@ -73,7 +73,11 @@ int ICACHE_FLASH_ATTR httpdPlatSendData(ConnTypePtr conn, char *buff, int len) { } void ICACHE_FLASH_ATTR httpdPlatDisconnect(ConnTypePtr conn) { - espconn_disconnect(conn); +#ifdef USE_SSL + espconn_secure_disconnect(conn); +#else + espconn_disconnect(conn); +#endif } void ICACHE_FLASH_ATTR httpdPlatDisableTimeout(ConnTypePtr conn) { From 3403e52f407b474f9e6481f89c6638cb0561a267 Mon Sep 17 00:00:00 2001 From: Scott Price Date: Fri, 27 Jan 2017 16:58:56 -0600 Subject: [PATCH 9/9] Removed the failed ssl code. --- core/httpd-nonos.c | 18 ------------------ 1 file changed, 18 deletions(-) diff --git a/core/httpd-nonos.c b/core/httpd-nonos.c index 09705c00..faefaa6d 100644 --- a/core/httpd-nonos.c +++ b/core/httpd-nonos.c @@ -53,31 +53,19 @@ static void ICACHE_FLASH_ATTR platConnCb(void *arg) { espconn_regist_disconcb(conn, platDisconCb); espconn_regist_sentcb(conn, platSentCb); } else { -#ifdef USE_SSL - espconn_secure_disconnect(conn); -#else espconn_disconnect(conn); -#endif } } int ICACHE_FLASH_ATTR httpdPlatSendData(ConnTypePtr conn, char *buff, int len) { int r; -#ifdef USE_SSL - r=espconn_secure_send(conn, (uint8_t*)buff, len); -#else r=espconn_sent(conn, (uint8_t*)buff, len); -#endif return (r>=0); } void ICACHE_FLASH_ATTR httpdPlatDisconnect(ConnTypePtr conn) { -#ifdef USE_SSL - espconn_secure_disconnect(conn); -#else espconn_disconnect(conn); -#endif } void ICACHE_FLASH_ATTR httpdPlatDisableTimeout(ConnTypePtr conn) { @@ -92,13 +80,7 @@ void ICACHE_FLASH_ATTR httpdPlatInit(int port, int maxConnCt) { httpdTcp.local_port=port; httpdConn.proto.tcp=&httpdTcp; espconn_regist_connectcb(&httpdConn, platConnCb); -#ifdef USE_SSL - //espconn_secure_set_default_certificate(default_certificate, default_certificate_len); - //espconn_secure_set_default_private_key(default_private_key, default_private_key_len); - espconn_secure_accept(&httpdConn); -#else espconn_accept(&httpdConn); -#endif espconn_tcp_set_max_con_allow(&httpdConn, maxConnCt); }