Implemented submit

pull/193/head
Karai Csaba 9 years ago committed by Thorsten von Eicken
parent d2d89505e9
commit 3b8d321859
  1. 8
      examples/arduino/EspLinkSample/EspLinkSample.ino
  2. 91
      web-server/web-server.c

@ -42,18 +42,22 @@ void packetReceived(CmdRequest *req)
switch( reason ) switch( reason )
{ {
case BUTTON: case BUTTON:
case SUBMIT:
{ {
uint16_t len = espLink.cmdArgLen(req); uint16_t len = espLink.cmdArgLen(req);
char bf[len+1]; char bf[len+1];
bf[len] = 0; bf[len] = 0;
espLink.cmdPopArg(req, bf, len); espLink.cmdPopArg(req, bf, len);
Serial.print("Button: "); Serial.print("Arg: ");
Serial.println(bf); Serial.println(bf);
if( reason == SUBMIT )
return;
} }
break; break;
} }
char * json = "{'last_name': 'helloka'}"; char * json = "{\"last_name\": \"helloka\"}";
espLink.sendPacketStart(CMD_WEB_JSON_DATA, 100, 3); espLink.sendPacketStart(CMD_WEB_JSON_DATA, 100, 3);
espLink.sendPacketArg(4, ip); espLink.sendPacketArg(4, ip);

@ -84,6 +84,36 @@ void ICACHE_FLASH_ATTR WEB_Init()
WEB_BrowseFiles(); 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) int ICACHE_FLASH_ATTR WEB_CgiJsonHook(HttpdConnData *connData)
{ {
if (connData->conn==NULL) return HTTPD_CGI_DONE; // Connection aborted. Clean up. 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; break;
case SUBMIT: 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; break;
case LOAD: case LOAD:

Loading…
Cancel
Save