From 979510681b84cf29d1ba5677913740a1d6ef0c23 Mon Sep 17 00:00:00 2001 From: Karai Csaba Date: Wed, 11 May 2016 07:49:22 +0200 Subject: [PATCH] Refactored: use different cgi for JSON --- esp-link/main.c | 1 + httpd/httpd.c | 7 +++++-- httpd/httpdespfs.c | 9 --------- web-server/web-server.c | 4 +++- web-server/web-server.h | 2 +- 5 files changed, 10 insertions(+), 13 deletions(-) diff --git a/esp-link/main.c b/esp-link/main.c index 1edef55..4eeac66 100644 --- a/esp-link/main.c +++ b/esp-link/main.c @@ -100,6 +100,7 @@ HttpdBuiltInUrl builtInUrls[] = { { "/mqtt", cgiMqtt, NULL }, #endif { "/web-server/upload", cgiWebServerUpload, NULL }, + { "*.json", cgiJsonHook, NULL }, //Catch-all cgi JSON queries { "*", cgiEspFsHook, NULL }, //Catch-all cgi function for the filesystem { NULL, NULL, NULL } }; diff --git a/httpd/httpd.c b/httpd/httpd.c index 8602ce3..2dfacfa 100644 --- a/httpd/httpd.c +++ b/httpd/httpd.c @@ -355,11 +355,14 @@ static void ICACHE_FLASH_ATTR httpdProcessRequest(HttpdConnData *conn) { if (conn->cgi == NULL) { while (builtInUrls[i].url != NULL) { int match = 0; + int urlLen = os_strlen(builtInUrls[i].url); //See if there's a literal match if (os_strcmp(builtInUrls[i].url, conn->url) == 0) match = 1; //See if there's a wildcard match - if (builtInUrls[i].url[os_strlen(builtInUrls[i].url) - 1] == '*' && - os_strncmp(builtInUrls[i].url, conn->url, os_strlen(builtInUrls[i].url) - 1) == 0) match = 1; + if (builtInUrls[i].url[urlLen - 1] == '*' && + os_strncmp(builtInUrls[i].url, conn->url, urlLen - 1) == 0) match = 1; + else if (builtInUrls[i].url[0] == '*' && ( strlen(conn->url) >= urlLen -1 ) && + os_strncmp(builtInUrls[i].url + 1, conn->url + strlen(conn->url) - urlLen + 1, urlLen - 1) == 0) match = 1; if (match) { //os_printf("Is url index %d\n", i); conn->cgiData = NULL; diff --git a/httpd/httpdespfs.c b/httpd/httpdespfs.c index ea443d1..9321aad 100644 --- a/httpd/httpdespfs.c +++ b/httpd/httpdespfs.c @@ -13,7 +13,6 @@ Connector to let httpd use the espfs filesystem to serve the files in it. * ---------------------------------------------------------------------------- */ #include "httpdespfs.h" -#include "web-server.h" // The static files marked with FLAG_GZIP are compressed and will be served with GZIP compression. // If the client does not advertise that he accepts GZIP send following warning message (telnet users for e.g.) @@ -30,14 +29,6 @@ cgiEspFsHook(HttpdConnData *connData) { char acceptEncodingBuffer[64]; int isGzip; - int urlLen = os_strlen(connData->url); - if( urlLen > 5 ) - { - if( os_strcmp( connData->url+urlLen-5, ".json" ) == 0 ) - { - return webServerProcessJsonQuery(connData); - } - } //os_printf("cgiEspFsHook conn=%p conn->conn=%p file=%p\n", connData, connData->conn, file); if (connData->conn==NULL) { diff --git a/web-server/web-server.c b/web-server/web-server.c index d8c55b4..4a4de3a 100644 --- a/web-server/web-server.c +++ b/web-server/web-server.c @@ -84,8 +84,10 @@ void ICACHE_FLASH_ATTR webServerInit() webServerBrowseFiles(); } -int ICACHE_FLASH_ATTR webServerProcessJsonQuery(HttpdConnData *connData) +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!"); diff --git a/web-server/web-server.h b/web-server/web-server.h index 5c4c594..b17373f 100644 --- a/web-server/web-server.h +++ b/web-server/web-server.h @@ -19,7 +19,7 @@ void webServerInit(); char * webServerUserPages(); -int webServerProcessJsonQuery(HttpdConnData *connData); +int ICACHE_FLASH_ATTR cgiJsonHook(HttpdConnData *connData); #endif /* WEB_SERVER_H */