From c14c214f49e7f3cf2c15b69596de653c981d8011 Mon Sep 17 00:00:00 2001 From: Karai Csaba Date: Wed, 11 May 2016 08:00:27 +0200 Subject: [PATCH] JSON hook, keep connection alive --- web-server/web-server.c | 163 +++++++++++++++++++++------------------- 1 file changed, 85 insertions(+), 78 deletions(-) diff --git a/web-server/web-server.c b/web-server/web-server.c index 4a4de3a..654937f 100644 --- a/web-server/web-server.c +++ b/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( !flashConfig.slip_enable ) - { - 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]; - int i; - int len = httpdFindArg(connData->getArgs, "reason", reasonBuf, sizeof(reasonBuf)); - if( len < 0 ) - { - errorResponse(connData, 400, "No reason specified!"); - return HTTPD_CGI_DONE; - } - - RequestReason reason = INVALID; - for(i=0; i < sizeof(web_server_reasons)/sizeof(char *); i++) - { - if( os_strcmp( web_server_reasons[i], reasonBuf ) == 0 ) - reason = (RequestReason)i; - } + void * cgiData = connData->cgiData; - if( reason == INVALID ) + if( cgiData == NULL ) { - errorResponse(connData, 400, "Invalid reason!"); - return HTTPD_CGI_DONE; - } - - char body[1024]; - int bodyLen = -1; - - 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; - } - - os_printf("Web callback to MCU: %s\n", reasonBuf); - - cmdResponseStart(CMD_WEB_REQ_CB, (uint32_t)cb->callback, bodyLen >= 0 ? 5 : 4); - 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( !flashConfig.slip_enable ) + { + 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]; + int i; + int len = httpdFindArg(connData->getArgs, "reason", reasonBuf, sizeof(reasonBuf)); + if( len < 0 ) + { + errorResponse(connData, 400, "No reason specified!"); + return HTTPD_CGI_DONE; + } + + RequestReason reason = INVALID; + for(i=0; i < sizeof(web_server_reasons)/sizeof(char *); i++) + { + if( os_strcmp( web_server_reasons[i], reasonBuf ) == 0 ) + reason = (RequestReason)i; + } + + if( reason == INVALID ) + { + errorResponse(connData, 400, "Invalid reason!"); + return HTTPD_CGI_DONE; + } + + char body[1024]; + int bodyLen = -1; + + 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; + } + + os_printf("Web callback to MCU: %s\n", reasonBuf); + + cmdResponseStart(CMD_WEB_REQ_CB, (uint32_t)cb->callback, bodyLen >= 0 ? 5 : 4); + 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 ) - { - httpdStartResponse(connData, 204); - httpdEndHeaders(connData); - return HTTPD_CGI_DONE; + if( reason == SUBMIT ) + { + httpdStartResponse(connData, 204); + httpdEndHeaders(connData); + return HTTPD_CGI_DONE; + } + + connData->cgiData = (void *)1; } // TODO - return HTTPD_CGI_DONE; + return HTTPD_CGI_MORE; }