diff --git a/examples/arduino/EspLinkSample/EspLinkSample.ino b/examples/arduino/EspLinkSample/EspLinkSample.ino index 81d721c..94a50f6 100644 --- a/examples/arduino/EspLinkSample/EspLinkSample.ino +++ b/examples/arduino/EspLinkSample/EspLinkSample.ino @@ -42,18 +42,22 @@ void packetReceived(CmdRequest *req) switch( reason ) { case BUTTON: + case SUBMIT: { uint16_t len = espLink.cmdArgLen(req); char bf[len+1]; bf[len] = 0; espLink.cmdPopArg(req, bf, len); - Serial.print("Button: "); + Serial.print("Arg: "); Serial.println(bf); + + if( reason == SUBMIT ) + return; } break; } - char * json = "{'last_name': 'helloka'}"; + char * json = "{\"last_name\": \"helloka\"}"; espLink.sendPacketStart(CMD_WEB_JSON_DATA, 100, 3); espLink.sendPacketArg(4, ip); diff --git a/web-server/web-server.c b/web-server/web-server.c index 8597dcb..4c5437c 100644 --- a/web-server/web-server.c +++ b/web-server/web-server.c @@ -84,6 +84,36 @@ void ICACHE_FLASH_ATTR WEB_Init() WEB_BrowseFiles(); } +int ICACHE_FLASH_ATTR WEB_addJsonString(char * str, char * buf, int maxLen) +{ + char * start = buf; + if( maxLen < 10 ) + return -1; + char * endp = start + maxLen - 10; + + *buf++ = '"'; + + int len = os_strlen(str); + char avalbuf[len*2+1]; + char * valbuf = avalbuf; + valbuf[len*2] = 0; + + httpdUrlDecode(str, len, valbuf, len * 2); + + while(*valbuf) + { + if( *valbuf == '"' || *valbuf == '\\' ) + *buf++ = '\\'; + *buf++ = *(valbuf++); + + if( buf > endp ) + return -1; + } + + *buf++ = '"'; + return buf - start; +} + int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData) { if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. @@ -146,7 +176,66 @@ int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData) break; case SUBMIT: { - // TODO + if( connData->post->received < connData->post->len ) + { + errorResponse(connData, 400, "Post too large!"); + return HTTPD_CGI_DONE; + } + + bodyLen = 0; + + int bptr = 0; + + body[bodyLen++] = '{'; + + while( bptr < connData->post->len ) + { + char * line = connData->post->buff + bptr; + + char * eo = os_strchr(line, '&' ); + if( eo != NULL ) + { + *eo = 0; + bptr = eo - connData->post->buff + 1; + } + else + { + eo = line + os_strlen( line ); + bptr = connData->post->len; + } + + int len = os_strlen(line); + while( len >= 1 && ( line[len-1] == '\r' || line[len-1] == '\n' )) + len--; + line[len] = 0; + + char * val = os_strchr(line, '='); + if( val != NULL ) + { + *val = 0; + char * name = line; + char * value = val+1; + + int alen = WEB_addJsonString(name, body + bodyLen, sizeof(body) - bodyLen); + if( alen == -1 ) + { + errorResponse(connData, 400, "Post too large!"); + return HTTPD_CGI_DONE; + } + bodyLen += alen; + body[bodyLen++] = ':'; + alen = WEB_addJsonString(value, body + bodyLen, sizeof(body) - bodyLen); + if( alen == -1 ) + { + errorResponse(connData, 400, "Post too large!"); + return HTTPD_CGI_DONE; + } + bodyLen += alen; + body[bodyLen++] = ','; + } + } + + body[bodyLen++] = '}'; } break; case LOAD: