JSON hook, keep connection alive

pull/193/head
Karai Csaba 9 years ago committed by Thorsten von Eicken
parent 979510681b
commit 89a9ff0d24
  1. 151
      web-server/web-server.c

@ -88,88 +88,95 @@ int ICACHE_FLASH_ATTR 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.
if( !flashConfig.slip_enable ) void * cgiData = connData->cgiData;
{
errorResponse(connData, 400, "Slip processing is disabled!");
return HTTPD_CGI_DONE;
}
CmdCallback* cb = cmdGetCbByName( WEB_CB );
if( cb == NULL )
{
errorResponse(connData, 500, "No MCU callback is registered!");
return HTTPD_CGI_DONE;
}
if( serbridgeInProgramming() )
{
errorResponse(connData, 500, "Slip disabled at programming mode!");
return HTTPD_CGI_DONE;
}
char reasonBuf[16]; if( cgiData == NULL )
int i;
int len = httpdFindArg(connData->getArgs, "reason", reasonBuf, sizeof(reasonBuf));
if( len < 0 )
{ {
errorResponse(connData, 400, "No reason specified!"); if( !flashConfig.slip_enable )
return HTTPD_CGI_DONE; {
} errorResponse(connData, 400, "Slip processing is disabled!");
return HTTPD_CGI_DONE;
}
CmdCallback* cb = cmdGetCbByName( WEB_CB );
if( cb == NULL )
{
errorResponse(connData, 500, "No MCU callback is registered!");
return HTTPD_CGI_DONE;
}
if( serbridgeInProgramming() )
{
errorResponse(connData, 500, "Slip disabled at programming mode!");
return HTTPD_CGI_DONE;
}
RequestReason reason = INVALID; char reasonBuf[16];
for(i=0; i < sizeof(web_server_reasons)/sizeof(char *); i++) int i;
{ int len = httpdFindArg(connData->getArgs, "reason", reasonBuf, sizeof(reasonBuf));
if( os_strcmp( web_server_reasons[i], reasonBuf ) == 0 ) if( len < 0 )
reason = (RequestReason)i; {
} errorResponse(connData, 400, "No reason specified!");
return HTTPD_CGI_DONE;
}
if( reason == INVALID ) RequestReason reason = INVALID;
{ for(i=0; i < sizeof(web_server_reasons)/sizeof(char *); i++)
errorResponse(connData, 400, "Invalid reason!"); {
return HTTPD_CGI_DONE; if( os_strcmp( web_server_reasons[i], reasonBuf ) == 0 )
} reason = (RequestReason)i;
}
char body[1024]; if( reason == INVALID )
int bodyLen = -1; {
errorResponse(connData, 400, "Invalid reason!");
return HTTPD_CGI_DONE;
}
switch(reason) char body[1024];
{ int bodyLen = -1;
case BUTTON:
bodyLen = httpdFindArg(connData->getArgs, "id", body, sizeof(body));
if( bodyLen <= 0 )
{
errorResponse(connData, 400, "No button ID specified!");
return HTTPD_CGI_DONE;
}
break;
case SUBMIT:
{
// TODO
}
break;
case LOAD:
case REFRESH:
default:
break;
}
os_printf("Web callback to MCU: %s\n", reasonBuf); switch(reason)
{
case BUTTON:
bodyLen = httpdFindArg(connData->getArgs, "id", body, sizeof(body));
if( bodyLen <= 0 )
{
errorResponse(connData, 400, "No button ID specified!");
return HTTPD_CGI_DONE;
}
break;
case SUBMIT:
{
// TODO
}
break;
case LOAD:
case REFRESH:
default:
break;
}
cmdResponseStart(CMD_WEB_REQ_CB, (uint32_t)cb->callback, bodyLen >= 0 ? 5 : 4); os_printf("Web callback to MCU: %s\n", reasonBuf);
uint16_t r = (uint16_t)reason;
cmdResponseBody(&r, sizeof(uint16_t));
cmdResponseBody(&connData->conn->proto.tcp->remote_ip, 4);
cmdResponseBody(&connData->conn->proto.tcp->remote_port, sizeof(uint16_t));
cmdResponseBody(connData->url, os_strlen(connData->url));
if( bodyLen >= 0 )
cmdResponseBody(body, bodyLen);
cmdResponseEnd();
if( reason == SUBMIT ) cmdResponseStart(CMD_WEB_REQ_CB, (uint32_t)cb->callback, bodyLen >= 0 ? 5 : 4);
{ uint16_t r = (uint16_t)reason;
httpdStartResponse(connData, 204); cmdResponseBody(&r, sizeof(uint16_t));
httpdEndHeaders(connData); cmdResponseBody(&connData->conn->proto.tcp->remote_ip, 4);
return HTTPD_CGI_DONE; cmdResponseBody(&connData->conn->proto.tcp->remote_port, sizeof(uint16_t));
cmdResponseBody(connData->url, os_strlen(connData->url));
if( bodyLen >= 0 )
cmdResponseBody(body, bodyLen);
cmdResponseEnd();
if( reason == SUBMIT )
{
httpdStartResponse(connData, 204);
httpdEndHeaders(connData);
return HTTPD_CGI_DONE;
}
connData->cgiData = (void *)1;
} }
// TODO // TODO
return HTTPD_CGI_DONE; return HTTPD_CGI_MORE;
} }

Loading…
Cancel
Save