From 9dc282fca8636d712a8338809be7f2ad15f9a12c Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Wed, 27 Mar 2019 23:25:18 +0900 Subject: [PATCH 1/2] Changed the chunk transfer method. Issue #51 --- src/AutoConnect.cpp | 1 - src/AutoConnectPage.cpp | 9 +++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/AutoConnect.cpp b/src/AutoConnect.cpp index d30e631..bf844c8 100644 --- a/src/AutoConnect.cpp +++ b/src/AutoConnect.cpp @@ -389,7 +389,6 @@ void AutoConnect::_startWebServer(void) { // here, Prepare PageBuilders for captive portal if (!_responsePage) { _responsePage = new PageBuilder(); - _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); _responsePage->reserve(AUTOCONNECT_CONTENTBUFFER_SIZE); _responsePage->exitCanHandle(std::bind(&AutoConnect::_classifyHandle, this, std::placeholders::_1, std::placeholders::_2)); _responsePage->onUpload(std::bind(&AutoConnect::_handleUpload, this, std::placeholders::_1, std::placeholders::_2)); diff --git a/src/AutoConnectPage.cpp b/src/AutoConnectPage.cpp index cf37d5a..d93bfc4 100644 --- a/src/AutoConnectPage.cpp +++ b/src/AutoConnectPage.cpp @@ -1259,6 +1259,7 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("FLASH_SIZE")), std::bind(&AutoConnect::_token_FLASH_SIZE, this, std::placeholders::_1)); elm->addToken(String(FPSTR("CHIP_ID")), std::bind(&AutoConnect::_token_CHIP_ID, this, std::placeholders::_1)); elm->addToken(String(FPSTR("FREE_HEAP")), std::bind(&AutoConnect::_token_FREE_HEAP, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else if (uri == String(AUTOCONNECT_URI_CONFIG)) { @@ -1277,6 +1278,7 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("LIST_SSID")), std::bind(&AutoConnect::_token_LIST_SSID, this, std::placeholders::_1)); elm->addToken(String(FPSTR("SSID_COUNT")), std::bind(&AutoConnect::_token_SSID_COUNT, this, std::placeholders::_1)); elm->addToken(String(FPSTR("HIDDEN_COUNT")), std::bind(&AutoConnect::_token_HIDDEN_COUNT, this, std::placeholders::_1)); + _responsePage->chunked(PB_Chunk); } else if (uri == String(AUTOCONNECT_URI_CONNECT)) { @@ -1291,6 +1293,7 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("MENU_PRE")), std::bind(&AutoConnect::_token_MENU_PRE, this, std::placeholders::_1)); elm->addToken(String(FPSTR("MENU_POST")), std::bind(&AutoConnect::_token_MENU_POST, this, std::placeholders::_1)); elm->addToken(String(FPSTR("CUR_SSID")), std::bind(&AutoConnect::_token_CURRENT_SSID, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else if (uri == String(AUTOCONNECT_URI_OPEN)) { @@ -1305,6 +1308,7 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("MENU_AUX")), std::bind(&AutoConnect::_token_MENU_AUX, this, std::placeholders::_1)); elm->addToken(String(FPSTR("MENU_POST")), std::bind(&AutoConnect::_token_MENU_POST, this, std::placeholders::_1)); elm->addToken(String(FPSTR("OPEN_SSID")), std::bind(&AutoConnect::_token_OPEN_SSID, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else if (uri == String(AUTOCONNECT_URI_DISCON)) { @@ -1317,6 +1321,7 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("CSS_LUXBAR")), std::bind(&AutoConnect::_token_CSS_LUXBAR, this, std::placeholders::_1)); elm->addToken(String(FPSTR("MENU_PRE")), std::bind(&AutoConnect::_token_MENU_PRE, this, std::placeholders::_1)); elm->addToken(String(FPSTR("MENU_POST")), std::bind(&AutoConnect::_token_MENU_POST, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else if (uri == String(AUTOCONNECT_URI_RESET)) { @@ -1326,12 +1331,14 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("BOOTURI")), std::bind(&AutoConnect::_token_BOOTURI, this, std::placeholders::_1)); elm->addToken(String(FPSTR("UPTIME")), std::bind(&AutoConnect::_token_UPTIME, this, std::placeholders::_1)); elm->addToken(String(FPSTR("RESET")), std::bind(&AutoConnect::_induceReset, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else if (uri == String(AUTOCONNECT_URI_RESULT)) { // Setup /auto/result elm->setMold("{{RESULT}}"); elm->addToken(String(FPSTR("RESULT")), std::bind(&AutoConnect::_invokeResult, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else if (uri == String(AUTOCONNECT_URI_SUCCESS)) { @@ -1352,6 +1359,7 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("NETMASK")), std::bind(&AutoConnect::_token_NETMASK, this, std::placeholders::_1)); elm->addToken(String(FPSTR("CHANNEL")), std::bind(&AutoConnect::_token_CHANNEL, this, std::placeholders::_1)); elm->addToken(String(FPSTR("DBM")), std::bind(&AutoConnect::_token_DBM, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else if (uri == String(AUTOCONNECT_URI_FAIL)) { @@ -1366,6 +1374,7 @@ PageElement* AutoConnect::_setupPage(String uri) { elm->addToken(String(FPSTR("MENU_AUX")), std::bind(&AutoConnect::_token_MENU_AUX, this, std::placeholders::_1)); elm->addToken(String(FPSTR("MENU_POST")), std::bind(&AutoConnect::_token_MENU_POST, this, std::placeholders::_1)); elm->addToken(String(FPSTR("STATION_STATUS")), std::bind(&AutoConnect::_token_STATION_STATUS, this, std::placeholders::_1)); + _responsePage->chunked(AUTOCONNECT_HTTP_TRANSFER); } else { delete elm; From 6dbe23ab005b74103150caf5a5c13adfbcf5248d Mon Sep 17 00:00:00 2001 From: Hieromon Ikasamo Date: Thu, 28 Mar 2019 02:04:45 +0900 Subject: [PATCH 2/2] Add the operator [] --- examples/FileUpload/FileUpload.ino | 27 ++++++++++++++++++++------- src/AutoConnectAux.h | 1 + 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/examples/FileUpload/FileUpload.ino b/examples/FileUpload/FileUpload.ino index e44d0dd..6bc4c48 100644 --- a/examples/FileUpload/FileUpload.ino +++ b/examples/FileUpload/FileUpload.ino @@ -29,6 +29,7 @@ #include #include +// Upload request custom Web page static const char PAGE_UPLOAD[] PROGMEM = R"( { "uri": "/", @@ -56,6 +57,7 @@ static const char PAGE_UPLOAD[] PROGMEM = R"( } )"; +// Upload result display static const char PAGE_BROWSE[] PROGMEM = R"( { "uri": "/upload", @@ -86,13 +88,17 @@ static const char PAGE_BROWSE[] PROGMEM = R"( )"; #if defined(ARDUINO_ARCH_ESP8266) +#define FILE_MODE_R "r" typedef ESP8266WebServer WiFiWebServer; #elif defined(ARDUINO_ARCH_ESP32) +#define FILE_MODE_R FILE_READ typedef WebServer WiFiWebServer; #endif WiFiWebServer server; AutoConnect portal(server); +// Declare AutoConnectAux separately as a custom web page to access +// easily for each page in the post-upload handler. AutoConnectAux auxUpload; AutoConnectAux auxBrowse; @@ -107,14 +113,21 @@ AutoConnectAux auxBrowse; */ String postUpload(AutoConnectAux& aux, PageArgument& args) { String content; - String filename = auxUpload.getElement("filename").value; - aux.getElement("filename").value = filename; - aux.getElement("size").value = String(auxUpload.getElement("filename").size); - String contentType = auxUpload.getElement("filename").mimeType; - aux.getElement("content_type").value = contentType; - if (contentType.indexOf("text/") >= 0) { + // Explicitly cast to the desired element to correctly extract + // the element using the operator []. + AutoConnectFile& filename = (AutoConnectFile&)auxUpload["filename"]; + AutoConnectText& aux_filename = (AutoConnectText&)aux["filename"]; + AutoConnectText& aux_size = (AutoConnectText&)aux["size"]; + AutoConnectText& aux_contentType = (AutoConnectText&)aux["content_type"]; + // Assignment operator can be used for the element attribute. + aux_filename.value = filename.value; + aux_size.value = String(filename.size); + aux_contentType.value = filename.mimeType; + // The file saved by the AutoConnect upload handler is read from + // the EEPROM and echoed to a custom web page. + if (filename.mimeType.indexOf("text/") >= 0) { SPIFFS.begin(); - File uploadFile = SPIFFS.open(String("/" + filename).c_str(), "r"); + File uploadFile = SPIFFS.open(String("/" + filename.value).c_str(), FILE_MODE_R); if (uploadFile) { while (uploadFile.available()) { char c = uploadFile.read(); diff --git a/src/AutoConnectAux.h b/src/AutoConnectAux.h index 2a1d0b4..352e30d 100644 --- a/src/AutoConnectAux.h +++ b/src/AutoConnectAux.h @@ -49,6 +49,7 @@ class AutoConnectAux : public PageBuilder { explicit AutoConnectAux(const String& uri = String(""), const String& title = String(""), const bool menu = true, const AutoConnectElementVT addons = AutoConnectElementVT()) : _title(title), _menu(menu), _uriStr(String(uri)), _addonElm(addons), _handler(nullptr), _order(AC_EXIT_AHEAD), _uploadHandler(nullptr) { _uri = _uriStr.c_str(); _next.release(); _ac.release(); } ~AutoConnectAux(); + AutoConnectElement& operator[](const String& name) { return *getElement(name); } void add(AutoConnectElement& addon); /**< Add an element to the auxiliary page */ void add(AutoConnectElementVT addons); /**< Add the element set to the auxiliary page */ template