Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for PUT, PATCH, and DELETE httpd methods. #30

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ espfs/espfstest/*.o
espfs/espfstest/espfstest
*.DS_Store
html_compressed/
libwebpages-espfs.a
libwebpages-espfs.a
*.swp
11 changes: 9 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -180,6 +185,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")
Expand All @@ -191,9 +198,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
Expand Down
11 changes: 6 additions & 5 deletions core/httpd-nonos.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ static void ICACHE_FLASH_ATTR platConnCb(void *arg) {

int ICACHE_FLASH_ATTR httpdPlatSendData(ConnTypePtr conn, char *buff, int len) {
int r;
r=espconn_sent(conn, (uint8_t*)buff, len);
return (r>=0);
r=espconn_sent(conn, (uint8_t*)buff, len);
return (r>=0);
}

void ICACHE_FLASH_ATTR httpdPlatDisconnect(ConnTypePtr conn) {
espconn_disconnect(conn);
espconn_disconnect(conn);
}

void ICACHE_FLASH_ATTR httpdPlatDisableTimeout(ConnTypePtr conn) {
Expand All @@ -80,8 +80,9 @@ 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);
espconn_accept(&httpdConn);
espconn_tcp_set_max_con_allow(&httpdConn, maxConnCt);

}


Expand Down
20 changes: 15 additions & 5 deletions core/httpd.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -683,15 +692,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;
}
Expand Down
1 change: 1 addition & 0 deletions espfs/espfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions include/httpd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
7 changes: 5 additions & 2 deletions util/cgiflash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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...
Expand Down Expand Up @@ -175,7 +175,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;
Expand Down