diff --git a/.travis.yml b/.travis.yml index 340a1c5..837dd8e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,7 +7,6 @@ script: - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16 - sleep 3 - export DISPLAY=:1.0 - - wget http://downloads.arduino.cc/arduino-1.6.5-linux64.tar.xz - tar xf arduino-1.6.5-linux64.tar.xz - mv arduino-1.6.5 $HOME/arduino_ide @@ -28,7 +27,7 @@ script: - cd espressif - git clone https://github.com/me-no-dev/ESP31B.git ESP31B - cd ESP31B/tools - - wget http://static.ficeto.com/xtensa-esp108-elf-linux64.tar.gz + - wget https://github.com/me-no-dev/ESP31B/releases/download/0.0.1/xtensa-esp108-elf-linux64.tar.gz - tar zxvf xtensa-esp108-elf-linux64.tar.gz - chmod +x xtensa-esp108-elf/bin/* - chmod +x xtensa-esp108-elf/xtensa-esp108-elf/bin/* diff --git a/README.md b/README.md index 27f3283..3b79528 100644 --- a/README.md +++ b/README.md @@ -235,8 +235,9 @@ request->send("text/plain", 128, [](uint8_t *buffer, size_t maxLen) -> size_t { ### Respond with content using a callback and extra headers ```cpp //send 128 bytes as plain text -AsyncWebServerResponse *response = request->beginResponse("text/plain", 128, [](uint8_t *buffer, size_t maxLen) -> size_t { +AsyncWebServerResponse *response = request->beginResponse("text/plain", 128, [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent //You will not be asked for more bytes once the content length has been reached. //Keep in mind that you can not delay or yield waiting for more data! //Send what you currently have and you will be asked for more again @@ -249,8 +250,9 @@ request->send(response); ### Chunked Response Used when content length is unknown. Works best if the client supports HTTP/1.1 ```cpp -AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [](uint8_t *buffer, size_t maxLen) -> size_t { +AsyncWebServerResponse *response = request->beginChunkedResponse("text/plain", [](uint8_t *buffer, size_t maxLen, size_t index) -> size_t { //Write up to "maxLen" bytes into "buffer" and return the amount written. + //index equals the amount of bytes that have been already sent //You will be asked for more data until 0 is returned //Keep in mind that you can not delay or yield waiting for more data! return mySource.read(buffer, maxLen); @@ -309,6 +311,28 @@ response->print(""); request->send(response); ``` +### Send a large webpage from PROGMEM using callback response +Example provided by [@nouser2013](https://github.com/nouser2013) +```cpp +const char indexhtml[] PROGMEM = "..."; // large char array, tested with 5k +AsyncWebServerResponse *response = request->beginResponse( + String("text/html"), + strlen_P(indexhtml), + [](uint8_t *buffer, size_t maxLen, size_t alreadySent) -> size_t { + if (strlen_P(indexhtml+alreadySent)>maxLen) { + // We have more to read than fits in maxLen Buffer + memcpy_P((char*)buffer, indexhtml+alreadySent, maxLen); + return maxLen; + } + // Ok, last chunk + memcpy_P((char*)buffer, indexhtml+sentCounter, strlen_P(indexhtml+sentCounter)); + return strlen_P(indexhtml+sentCounter); // Return from here to end of indexhtml + } +); +response->addHeader("Server", "MyServerString"); +request->send(response); +``` + ### ArduinoJson Basic Response This way of sending Json is great for when the result is below 4KB ```cpp diff --git a/src/ESPAsyncWebServer.h b/src/ESPAsyncWebServer.h index 8b228eb..f5513a8 100644 --- a/src/ESPAsyncWebServer.h +++ b/src/ESPAsyncWebServer.h @@ -1,3 +1,23 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #ifndef _ESPAsyncWebServer_H_ #define _ESPAsyncWebServer_H_ @@ -76,7 +96,7 @@ class AsyncWebHeader { * REQUEST :: Each incoming Client is wrapped inside a Request and both live together until disconnect * */ -typedef std::function AwsResponseFiller; +typedef std::function AwsResponseFiller; class AsyncWebServerRequest { private: @@ -286,6 +306,6 @@ class AsyncWebServer { void _handleRequest(AsyncWebServerRequest *request); }; -#include "AsyncWebServerResponseImpl.h" +#include "WebResponseImpl.h" #endif /* _AsyncWebServer_H_ */ diff --git a/src/StringArray.h b/src/StringArray.h index bce1067..204f7cf 100644 --- a/src/StringArray.h +++ b/src/StringArray.h @@ -1,10 +1,23 @@ /* - * StringArray.h - * - * Created on: 18.12.2015 г. - * Author: ficeto - */ + Asynchronous WebServer library for Espressif MCUs + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #ifndef STRINGARRAY_H_ #define STRINGARRAY_H_ diff --git a/src/AsyncWebServerHandlerImpl.h b/src/WebHandlerImpl.h similarity index 74% rename from src/AsyncWebServerHandlerImpl.h rename to src/WebHandlerImpl.h index ee027d2..dc0992e 100644 --- a/src/AsyncWebServerHandlerImpl.h +++ b/src/WebHandlerImpl.h @@ -1,10 +1,23 @@ /* - * AsyncWebServerHandlerImpl.h - * - * Created on: 19.12.2015 г. - * Author: ficeto - */ + Asynchronous WebServer library for Espressif MCUs + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #ifndef ASYNCWEBSERVERHANDLERIMPL_H_ #define ASYNCWEBSERVERHANDLERIMPL_H_ diff --git a/src/WebHandlers.cpp b/src/WebHandlers.cpp index 0c1dcff..e55269f 100644 --- a/src/WebHandlers.cpp +++ b/src/WebHandlers.cpp @@ -1,11 +1,25 @@ /* - * WebHandlers.cpp - * - * Created on: 18.12.2015 г. - * Author: ficeto - */ + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "ESPAsyncWebServer.h" -#include "AsyncWebServerHandlerImpl.h" +#include "WebHandlerImpl.h" bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request) { diff --git a/src/WebServerClient.cpp b/src/WebRequest.cpp similarity index 96% rename from src/WebServerClient.cpp rename to src/WebRequest.cpp index 143e396..b585449 100644 --- a/src/WebServerClient.cpp +++ b/src/WebRequest.cpp @@ -1,13 +1,26 @@ /* - * WebServerClient.cpp - * - * Created on: 18.12.2015 г. - * Author: ficeto - */ + Asynchronous WebServer library for Espressif MCUs + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "ESPAsyncWebServer.h" -#include "AsyncWebServerResponseImpl.h" #include +#include "WebResponseImpl.h" #ifndef ESP8266 #define os_strlen strlen diff --git a/src/AsyncWebServerResponseImpl.h b/src/WebResponseImpl.h similarity index 73% rename from src/AsyncWebServerResponseImpl.h rename to src/WebResponseImpl.h index ca13ee3..1f73920 100644 --- a/src/AsyncWebServerResponseImpl.h +++ b/src/WebResponseImpl.h @@ -1,10 +1,23 @@ /* - * AsyncWebImpl.h - * - * Created on: 19.12.2015 г. - * Author: ficeto - */ + Asynchronous WebServer library for Espressif MCUs + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #ifndef ASYNCWEBSERVERRESPONSEIMPL_H_ #define ASYNCWEBSERVERRESPONSEIMPL_H_ diff --git a/src/WebResponses.cpp b/src/WebResponses.cpp index de673f0..f418744 100644 --- a/src/WebResponses.cpp +++ b/src/WebResponses.cpp @@ -1,5 +1,25 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "ESPAsyncWebServer.h" -#include "AsyncWebServerResponseImpl.h" +#include "WebResponseImpl.h" #include "cbuf.h" /* @@ -265,7 +285,10 @@ size_t AsyncAbstractResponse::_ack(AsyncWebServerRequest *request, size_t len, u if(outLen) outLen = request->client()->write((const char*)buf, outLen); - _sentLength += outLen; + if(_chunked) + _sentLength += readLen; + else + _sentLength += outLen; free(buf); if((_chunked && readLen == 0) || (!_sendContentLength && outLen == 0) || _sentLength == _contentLength){ @@ -381,7 +404,7 @@ AsyncCallbackResponse::AsyncCallbackResponse(String contentType, size_t len, Aws } size_t AsyncCallbackResponse::_fillBuffer(uint8_t *data, size_t len){ - return _content(data, len); + return _content(data, len, _sentLength); } /* @@ -398,7 +421,7 @@ AsyncChunkedResponse::AsyncChunkedResponse(String contentType, AwsResponseFiller } size_t AsyncChunkedResponse::_fillBuffer(uint8_t *data, size_t len){ - return _content(data, len); + return _content(data, len, _sentLength); } diff --git a/src/WebServer.cpp b/src/WebServer.cpp index d4d40a1..42b678d 100644 --- a/src/WebServer.cpp +++ b/src/WebServer.cpp @@ -1,5 +1,25 @@ +/* + Asynchronous WebServer library for Espressif MCUs + + Copyright (c) 2016 Hristo Gochkov. All rights reserved. + This file is part of the esp8266 core for Arduino environment. + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA +*/ #include "ESPAsyncWebServer.h" -#include "AsyncWebServerHandlerImpl.h" +#include "WebHandlerImpl.h" AsyncWebServer::AsyncWebServer(uint16_t port):_server(port), _handlers(0), _catchAllHandler(new AsyncCallbackWebHandler()){