Skip to content

Commit

Permalink
Merge pull request #13 from me-no-dev/improved-parsing
Browse files Browse the repository at this point in the history
Rearrange and rename files and add index to chunked responses
  • Loading branch information
me-no-dev committed Feb 8, 2016
2 parents 97c9388 + c80722c commit c95ac73
Show file tree
Hide file tree
Showing 11 changed files with 680 additions and 465 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/*
Expand Down
26 changes: 24 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ 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.
//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!
Expand All @@ -249,7 +249,7 @@ 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.
//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!
Expand Down Expand Up @@ -309,6 +309,28 @@ response->print("</body></html>");
request->send(response);
```

### Send a large webpage from PROGMEM using chunked 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+sentCounter)>maxLen) {
// We have more to read than fits in maxLen Buffer
memcpy_P((char*)buffer, indexhtml+sentCounter, 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
Expand Down
24 changes: 22 additions & 2 deletions src/ESPAsyncWebServer.h
Original file line number Diff line number Diff line change
@@ -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_

Expand Down Expand Up @@ -76,7 +96,7 @@ class AsyncWebHeader {
* REQUEST :: Each incoming Client is wrapped inside a Request and both live together until disconnect
* */

typedef std::function<size_t(uint8_t*, size_t)> AwsResponseFiller;
typedef std::function<size_t(uint8_t*, size_t, size_t)> AwsResponseFiller;

class AsyncWebServerRequest {
private:
Expand Down Expand Up @@ -286,6 +306,6 @@ class AsyncWebServer {
void _handleRequest(AsyncWebServerRequest *request);
};

#include "AsyncWebServerResponseImpl.h"
#include <WebResponseImpl.h>

#endif /* _AsyncWebServer_H_ */
24 changes: 19 additions & 5 deletions src/StringArray.h
Original file line number Diff line number Diff line change
@@ -1,9 +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_
Expand Down
29 changes: 22 additions & 7 deletions src/AsyncWebServerHandlerImpl.h → src/WebHandlerImpl.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,42 @@
/*
* 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_


#include "stddef.h"
#include "ESPAsyncWebServer.h"

class AsyncStaticWebHandler: public AsyncWebHandler {
private:
String _getPath(AsyncWebServerRequest *request);
protected:
FS _fs;
fs::FS _fs;
String _uri;
String _path;
String _cache_header;
bool _isFile;
public:
AsyncStaticWebHandler(FS& fs, const char* path, const char* uri, const char* cache_header)
AsyncStaticWebHandler(fs::FS& fs, const char* path, const char* uri, const char* cache_header)
: _fs(fs), _uri(uri), _path(path), _cache_header(cache_header){

_isFile = _fs.exists(path) || _fs.exists((String(path)+".gz").c_str());
Expand Down
26 changes: 20 additions & 6 deletions src/WebHandlers.cpp
Original file line number Diff line number Diff line change
@@ -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 <WebHandlerImpl.h>
#include "ESPAsyncWebServer.h"
#include "AsyncWebServerHandlerImpl.h"

bool AsyncStaticWebHandler::canHandle(AsyncWebServerRequest *request)
{
Expand Down

0 comments on commit c95ac73

Please sign in to comment.