diff --git a/examples/ESP32_AsyncFSBrowser/ESP32_AsyncFSBrowser.ino b/examples/ESP32_AsyncFSBrowser/ESP32_AsyncFSBrowser.ino new file mode 100644 index 0000000..faf0b7e --- /dev/null +++ b/examples/ESP32_AsyncFSBrowser/ESP32_AsyncFSBrowser.ino @@ -0,0 +1,199 @@ +#include +#include +#include +#include + +// WEB HANDLER IMPLEMENTATION +class SPIFFSEditor: public AsyncWebHandler { + private: + String _username; + String _password; + bool _uploadAuthenticated; + public: + SPIFFSEditor(String username=String(), String password=String()):_username(username),_password(password),_uploadAuthenticated(false){} + bool canHandle(AsyncWebServerRequest *request){ + if(request->method() == HTTP_GET && request->url() == "/edit" && (SPIFFS.exists("/edit.htm") || SPIFFS.exists("/edit.htm.gz"))) + return true; + else if(request->method() == HTTP_GET && request->url() == "/list") + return true; + else if(request->method() == HTTP_GET && (request->url().endsWith("/") || SPIFFS.exists(request->url()) || (!request->hasParam("download") && SPIFFS.exists(request->url()+".gz")))) + return true; + else if(request->method() == HTTP_POST && request->url() == "/edit") + return true; + else if(request->method() == HTTP_DELETE && request->url() == "/edit") + return true; + else if(request->method() == HTTP_PUT && request->url() == "/edit") + return true; + return false; + } + + void handleRequest(AsyncWebServerRequest *request){ + if(_username.length() && (request->method() != HTTP_GET || request->url() == "/edit" || request->url() == "/list") && !request->authenticate(_username.c_str(),_password.c_str())) + return request->requestAuthentication(); + + if(request->method() == HTTP_GET && request->url() == "/edit"){ + request->send(SPIFFS, "/edit.htm"); + } else if(request->method() == HTTP_GET && request->url() == "/list"){ + if(request->hasParam("dir")){ + String path = request->getParam("dir")->value(); + Dir dir = SPIFFS.openDir(path); + path = String(); + String output = "["; + while(dir.next()){ + File entry = dir.openFile("r"); + if (output != "[") output += ','; + bool isDir = false; + output += "{\"type\":\""; + output += (isDir)?"dir":"file"; + output += "\",\"name\":\""; + output += String(entry.name()).substring(1); + output += "\"}"; + entry.close(); + } + output += "]"; + request->send(200, "text/json", output); + output = String(); + } + else + request->send(400); + } else if(request->method() == HTTP_GET){ + String path = request->url(); + if(path.endsWith("/")) + path += "index.htm"; + request->send(SPIFFS, path, String(), request->hasParam("download")); + } else if(request->method() == HTTP_DELETE){ + if(request->hasParam("path", true)){ + SPIFFS.remove(request->getParam("path", true)->value()); + request->send(200, "", "DELETE: "+request->getParam("path", true)->value()); + } else + request->send(404); + } else if(request->method() == HTTP_POST){ + if(request->hasParam("data", true, true) && SPIFFS.exists(request->getParam("data", true, true)->value())) + request->send(200, "", "UPLOADED: "+request->getParam("data", true, true)->value()); + else + request->send(500); + } else if(request->method() == HTTP_PUT){ + if(request->hasParam("path", true)){ + String filename = request->getParam("path", true)->value(); + if(SPIFFS.exists(filename)){ + request->send(200); + } else { + File f = SPIFFS.open(filename, "w"); + if(f){ + f.write(0x00); + f.close(); + request->send(200, "", "CREATE: "+filename); + } else { + request->send(500); + } + } + } else + request->send(400); + } + } + + void handleUpload(AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ + if(!index){ + if(!_username.length() || request->authenticate(_username.c_str(),_password.c_str())) + _uploadAuthenticated = true; + request->_tempFile = SPIFFS.open(filename, "w"); + } + if(_uploadAuthenticated && request->_tempFile && len){ + request->_tempFile.write(data,len); + } + if(_uploadAuthenticated && final) + if(request->_tempFile) request->_tempFile.close(); + } +}; + + +// SKETCH BEGIN +AsyncWebServer server(80); + +const char* ssid = "**********"; +const char* password = "************"; +const char* http_username = "admin"; +const char* http_password = "admin"; + +void setup(){ + Serial.begin(115200); + Serial.setDebugOutput(true); + SPIFFS.begin(); + WiFi.mode(WIFI_STA); + WiFi.begin(ssid, password); + if(WiFi.waitForConnectResult() != WL_CONNECTED){ + Serial.printf("WiFi Failed!\n"); + } + + server.serveStatic("/fs", SPIFFS, "/"); + + server.on("/heap", HTTP_GET, [](AsyncWebServerRequest *request){ + request->send(200, "text/plain", String(ESP.getFreeHeap())); + }); + server.addHandler(new SPIFFSEditor(http_username,http_password)); + + server.onNotFound([](AsyncWebServerRequest *request){ + os_printf("NOT_FOUND: "); + if(request->method() == HTTP_GET) + os_printf("GET"); + else if(request->method() == HTTP_POST) + os_printf("POST"); + else if(request->method() == HTTP_DELETE) + os_printf("DELETE"); + else if(request->method() == HTTP_PUT) + os_printf("PUT"); + else if(request->method() == HTTP_PATCH) + os_printf("PATCH"); + else if(request->method() == HTTP_HEAD) + os_printf("HEAD"); + else if(request->method() == HTTP_OPTIONS) + os_printf("OPTIONS"); + else + os_printf("UNKNOWN"); + os_printf(" http://%s%s\n", request->host().c_str(), request->url().c_str()); + + if(request->contentLength()){ + os_printf("_CONTENT_TYPE: %s\n", request->contentType().c_str()); + os_printf("_CONTENT_LENGTH: %u\n", request->contentLength()); + } + + int headers = request->headers(); + int i; + for(i=0;igetHeader(i); + os_printf("_HEADER[%s]: %s\n", h->name().c_str(), h->value().c_str()); + } + + int params = request->params(); + for(i=0;igetParam(i); + if(p->isFile()){ + os_printf("_FILE[%s]: %s, size: %u\n", p->name().c_str(), p->value().c_str(), p->size()); + } else if(p->isPost()){ + os_printf("_POST[%s]: %s\n", p->name().c_str(), p->value().c_str()); + } else { + os_printf("_GET[%s]: %s\n", p->name().c_str(), p->value().c_str()); + } + } + + request->send(404); + }); + server.onFileUpload([](AsyncWebServerRequest *request, String filename, size_t index, uint8_t *data, size_t len, bool final){ + if(!index) + os_printf("UploadStart: %s\n", filename.c_str()); + os_printf("%s", (const char*)data); + if(final) + os_printf("UploadEnd: %s (%u)\n", filename.c_str(), index+len); + }); + server.onRequestBody([](AsyncWebServerRequest *request, uint8_t *data, size_t len, size_t index, size_t total){ + if(!index) + os_printf("BodyStart: %u\n", total); + os_printf("%s", (const char*)data); + if(index + len == total) + os_printf("BodyEnd: %u\n", total); + }); + server.begin(); +} + +void loop(){} + diff --git a/examples/ESP32_AsyncFSBrowser/data/edit.htm.gz b/examples/ESP32_AsyncFSBrowser/data/edit.htm.gz new file mode 100644 index 0000000..9ee1b81 Binary files /dev/null and b/examples/ESP32_AsyncFSBrowser/data/edit.htm.gz differ diff --git a/examples/ESP32_AsyncFSBrowser/data/favicon.ico b/examples/ESP32_AsyncFSBrowser/data/favicon.ico new file mode 100644 index 0000000..71b25fe Binary files /dev/null and b/examples/ESP32_AsyncFSBrowser/data/favicon.ico differ diff --git a/examples/ESP32_AsyncFSBrowser/data/graphs.js.gz b/examples/ESP32_AsyncFSBrowser/data/graphs.js.gz new file mode 100644 index 0000000..e0e13f6 Binary files /dev/null and b/examples/ESP32_AsyncFSBrowser/data/graphs.js.gz differ diff --git a/examples/ESP32_AsyncFSBrowser/data/index.htm b/examples/ESP32_AsyncFSBrowser/data/index.htm new file mode 100644 index 0000000..7523b22 --- /dev/null +++ b/examples/ESP32_AsyncFSBrowser/data/index.htm @@ -0,0 +1,366 @@ + + + + + + ESP Monitor + + + + + + +
+
+
+ + + + +
+
+
+
+
+
+
+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + + + +
   SSIDBSSIDChannelSecureHiddenRSSI
+
+ + + + diff --git a/library.properties b/library.properties index b2354cf..18bbbbd 100644 --- a/library.properties +++ b/library.properties @@ -1,9 +1,9 @@ -name=ESP8266 Async Web Server +name=ESP Async WebServer version=1.0.0 -author=ESP8266 +author=Me-No-Dev maintainer=Me-No-Dev -sentence=Async Web Server for ESP8266 -paragraph=Async Web Server for ESP8266 +sentence=Async Web Server for ESP8266 and ESP31B +paragraph=Async Web Server for ESP8266 and ESP31B category=Other url=https://github.com/me-no-dev/ESPAsyncWebServer architectures=* diff --git a/src/AsyncWebServerResponseImpl.h b/src/AsyncWebServerResponseImpl.h index 145243b..3b20997 100644 --- a/src/AsyncWebServerResponseImpl.h +++ b/src/AsyncWebServerResponseImpl.h @@ -57,6 +57,8 @@ class AsyncCallbackResponse: public AsyncAbstractResponse { size_t _fillBuffer(uint8_t *buf, size_t maxLen); }; +class cbuf; + class AsyncResponseStream: public AsyncAbstractResponse, public Print { private: cbuf *_content; diff --git a/src/StringArray.h b/src/StringArray.h index 1d89bbb..bce1067 100644 --- a/src/StringArray.h +++ b/src/StringArray.h @@ -9,7 +9,7 @@ #define STRINGARRAY_H_ #include "stddef.h" -#include "String.h" +#include "WString.h" class StringArrayItem; class StringArrayItem { diff --git a/src/WebServerClient.cpp b/src/WebServerClient.cpp index 5f6a419..95e4991 100644 --- a/src/WebServerClient.cpp +++ b/src/WebServerClient.cpp @@ -147,11 +147,12 @@ void AsyncWebServerRequest::_onAck(size_t len, uint32_t time){ } void AsyncWebServerRequest::_onError(int8_t error){ - //os_printf("e:%d:%u\n", error, _client->state()); + if(error != -11) + os_printf("ERROR[%d] %s, state: %s\n", error, _client->errorToString(error), _client->stateToString()); } void AsyncWebServerRequest::_onTimeout(uint32_t time){ - //os_printf("t:%u\n", time); + os_printf("TIMEOUT: %u, state: %s\n", time, _client->stateToString()); _client->close(); } @@ -640,7 +641,7 @@ void AsyncWebServerRequest::send(String contentType, size_t len, AwsResponseFill bool AsyncWebServerRequest::authenticate(const char * username, const char * password){ if(_authorization.length()){ - char toencodeLen = strlen(username)+strlen(password)+1; + char toencodeLen = os_strlen(username)+os_strlen(password)+1; char *toencode = new char[toencodeLen]; if(toencode == NULL){ return false;