arduino json 6 async web server migration (#491)
* arduino json 6 async web server migration * Update .travis.yml fix travis error * Revert "Update .travis.yml" This reverts commit 37d1bf0e71ad21dc502aa632c1df503820686046. * gitignore * test fix travis * fix travis * fix proposed in #487 * fix travis * fix travis * fix travis * fix travis * ARDUINOJSON_5_COMPATIBILITY * replaced to #if ARDUINOJSON_VERSION_MAJOR == 5 * fix * added AsyncWebServer::end() * added AsyncWebServer::end() * Update WebServer.cpp fix * fix typo * fix typo
This commit is contained in:
parent
95dedf7a2d
commit
bde2fce07b
|
@ -0,0 +1 @@
|
|||
.vscode
|
17
.travis.yml
17
.travis.yml
|
@ -3,10 +3,25 @@ language: bash
|
|||
os:
|
||||
- linux
|
||||
|
||||
dist:
|
||||
- xenial
|
||||
|
||||
addons:
|
||||
apt:
|
||||
packages:
|
||||
- xvfb
|
||||
|
||||
# services:
|
||||
# - xvfb
|
||||
|
||||
|
||||
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
|
||||
# - /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -screen 0 1400x900x24 -ac +extension GLX +render;
|
||||
- export DISPLAY=:1.0
|
||||
- sleep 3
|
||||
- ls -l /tmp/*pid
|
||||
|
||||
- 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
|
||||
|
|
|
@ -11,7 +11,8 @@ For ESP32 it requires [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) to work
|
|||
To use this library you might need to have the latest git versions of [ESP32](https://github.com/espressif/arduino-esp32) Arduino Core
|
||||
|
||||
## Table of contents
|
||||
- [ESPAsyncWebServer ](#espasyncwebserver-)
|
||||
- [ESPAsyncWebServer ![Build Status](https://travis-ci.org/me-no-dev/ESPAsyncWebServer)](#espasyncwebserver-build-statushttpstravis-ciorgme-no-devespasyncwebserver)
|
||||
- [Table of contents](#table-of-contents)
|
||||
- [Installation](#installation)
|
||||
- [Using PlatformIO](#using-platformio)
|
||||
- [Why should you care](#why-should-you-care)
|
||||
|
@ -54,12 +55,14 @@ To use this library you might need to have the latest git versions of [ESP32](ht
|
|||
- [Respond with content using a callback containing templates](#respond-with-content-using-a-callback-containing-templates)
|
||||
- [Respond with content using a callback containing templates and extra headers](#respond-with-content-using-a-callback-containing-templates-and-extra-headers)
|
||||
- [Chunked Response](#chunked-response)
|
||||
- [Chunked Response containing templates](#chunked-response-containing-templates)
|
||||
- [Print to response](#print-to-response)
|
||||
- [ArduinoJson Basic Response](#arduinojson-basic-response)
|
||||
- [ArduinoJson Advanced Response](#arduinojson-advanced-response)
|
||||
- [Serving static files](#serving-static-files)
|
||||
- [Serving specific file by name](#serving-specific-file-by-name)
|
||||
- [Serving files in directory](#serving-files-in-directory)
|
||||
- [Serving static files with authentication](#serving-static-files-with-authentication)
|
||||
- [Specifying Cache-Control header](#specifying-cache-control-header)
|
||||
- [Specifying Date-Modified header](#specifying-date-modified-header)
|
||||
- [Specifying Template Processor callback](#specifying-template-processor-callback)
|
||||
|
@ -72,6 +75,7 @@ To use this library you might need to have the latest git versions of [ESP32](ht
|
|||
- [Async WebSocket Plugin](#async-websocket-plugin)
|
||||
- [Async WebSocket Event](#async-websocket-event)
|
||||
- [Methods for sending data to a socket client](#methods-for-sending-data-to-a-socket-client)
|
||||
- [Direct access to web socket message buffer](#direct-access-to-web-socket-message-buffer)
|
||||
- [Async Event Source Plugin](#async-event-source-plugin)
|
||||
- [Setup Event Source on the server](#setup-event-source-on-the-server)
|
||||
- [Setup Event Source in the browser](#setup-event-source-in-the-browser)
|
||||
|
@ -80,7 +84,7 @@ To use this library you might need to have the latest git versions of [ESP32](ht
|
|||
- [Setting up the server](#setting-up-the-server)
|
||||
- [Setup global and class functions as request handlers](#setup-global-and-class-functions-as-request-handlers)
|
||||
- [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections)
|
||||
- [Adding default headers to all responses](#adding-default-headers)
|
||||
- [Adding Default Headers](#adding-default-headers)
|
||||
|
||||
## Installation
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@
|
|||
"type": "git",
|
||||
"url": "https://github.com/me-no-dev/ESPAsyncWebServer.git"
|
||||
},
|
||||
"version": "1.2.0",
|
||||
"version": "1.2.1",
|
||||
"license": "LGPL-3.0",
|
||||
"frameworks": "arduino",
|
||||
"platforms": ["espressif8266", "espressif32"],
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
name=ESP Async WebServer
|
||||
version=1.2.0
|
||||
version=1.2.1
|
||||
author=Me-No-Dev
|
||||
maintainer=Me-No-Dev
|
||||
sentence=Async Web Server for ESP8266 and ESP31B
|
||||
|
|
|
@ -35,8 +35,15 @@
|
|||
#ifndef ASYNC_JSON_H_
|
||||
#define ASYNC_JSON_H_
|
||||
#include <ArduinoJson.h>
|
||||
#include <Print.h>
|
||||
|
||||
constexpr char* JSON_MIMETYPE = "application/json";
|
||||
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||
#define ARDUINOJSON_5_COMPATIBILITY
|
||||
#else
|
||||
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
||||
#endif
|
||||
|
||||
constexpr const char* JSON_MIMETYPE = "application/json";
|
||||
|
||||
/*
|
||||
* Json Response
|
||||
|
@ -63,14 +70,27 @@ class ChunkPrint : public Print {
|
|||
}
|
||||
return 0;
|
||||
}
|
||||
size_t write(const uint8_t *buffer, size_t size)
|
||||
{
|
||||
return this->Print::write(buffer, size);
|
||||
}
|
||||
};
|
||||
|
||||
class AsyncJsonResponse: public AsyncAbstractResponse {
|
||||
private:
|
||||
|
||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||
DynamicJsonBuffer _jsonBuffer;
|
||||
#else
|
||||
DynamicJsonDocument _jsonBuffer;
|
||||
#endif
|
||||
|
||||
JsonVariant _root;
|
||||
bool _isValid;
|
||||
|
||||
public:
|
||||
|
||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||
AsyncJsonResponse(bool isArray=false): _isValid{false} {
|
||||
_code = 200;
|
||||
_contentType = JSON_MIMETYPE;
|
||||
|
@ -79,11 +99,28 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||
else
|
||||
_root = _jsonBuffer.createObject();
|
||||
}
|
||||
#else
|
||||
AsyncJsonResponse(size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE, bool isArray=false) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
|
||||
_code = 200;
|
||||
_contentType = JSON_MIMETYPE;
|
||||
if(isArray)
|
||||
_root = _jsonBuffer.createNestedArray();
|
||||
else
|
||||
_root = _jsonBuffer.createNestedObject();
|
||||
}
|
||||
#endif
|
||||
|
||||
~AsyncJsonResponse() {}
|
||||
JsonVariant & getRoot() { return _root; }
|
||||
bool _sourceValid() const { return _isValid; }
|
||||
size_t setLength() {
|
||||
|
||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||
_contentLength = _root.measureLength();
|
||||
#else
|
||||
_contentLength = measureJson(_root);
|
||||
#endif
|
||||
|
||||
if (_contentLength) { _isValid = true; }
|
||||
return _contentLength;
|
||||
}
|
||||
|
@ -92,7 +129,12 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
|
|||
|
||||
size_t _fillBuffer(uint8_t *data, size_t len){
|
||||
ChunkPrint dest(data, _sentLength, len);
|
||||
|
||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||
_root.printTo( dest ) ;
|
||||
#else
|
||||
serializeJson(_root, dest);
|
||||
#endif
|
||||
return len;
|
||||
}
|
||||
};
|
||||
|
@ -106,9 +148,19 @@ protected:
|
|||
WebRequestMethodComposite _method;
|
||||
ArJsonRequestHandlerFunction _onRequest;
|
||||
int _contentLength;
|
||||
#ifndef ARDUINOJSON_5_COMPATIBILITY
|
||||
const size_t maxJsonBufferSize;
|
||||
#endif
|
||||
int _maxContentLength;
|
||||
public:
|
||||
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest) : _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
|
||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
|
||||
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
|
||||
#else
|
||||
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMIC_JSON_DOCUMENT_SIZE)
|
||||
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
|
||||
#endif
|
||||
|
||||
void setMethod(WebRequestMethodComposite method){ _method = method; }
|
||||
void setMaxContentLength(int maxContentLength){ _maxContentLength = maxContentLength; }
|
||||
void onRequest(ArJsonRequestHandlerFunction fn){ _onRequest = fn; }
|
||||
|
@ -133,9 +185,18 @@ public:
|
|||
virtual void handleRequest(AsyncWebServerRequest *request) override final {
|
||||
if(_onRequest) {
|
||||
if (request->_tempObject != NULL) {
|
||||
|
||||
#ifdef ARDUINOJSON_5_COMPATIBILITY
|
||||
DynamicJsonBuffer jsonBuffer;
|
||||
JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject));
|
||||
if (json.success()) {
|
||||
#else
|
||||
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
|
||||
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
|
||||
if(!error) {
|
||||
JsonVariant json = jsonBuffer.as<JsonVariant>();
|
||||
#endif
|
||||
|
||||
_onRequest(request, json);
|
||||
return;
|
||||
}
|
||||
|
|
|
@ -393,6 +393,7 @@ class AsyncWebServer {
|
|||
~AsyncWebServer();
|
||||
|
||||
void begin();
|
||||
void end();
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
void onSslFileRequest(AcSSlFileHandler cb, void* arg);
|
||||
|
|
|
@ -53,7 +53,8 @@ AsyncWebServer::AsyncWebServer(uint16_t port)
|
|||
|
||||
AsyncWebServer::~AsyncWebServer(){
|
||||
reset();
|
||||
delete _catchAllHandler;
|
||||
end();
|
||||
if(_catchAllHandler) delete _catchAllHandler;
|
||||
}
|
||||
|
||||
AsyncWebRewrite& AsyncWebServer::addRewrite(AsyncWebRewrite* rewrite){
|
||||
|
@ -83,6 +84,10 @@ void AsyncWebServer::begin(){
|
|||
_server.begin();
|
||||
}
|
||||
|
||||
void AsyncWebServer::end(){
|
||||
_server.end();
|
||||
}
|
||||
|
||||
#if ASYNC_TCP_SSL_ENABLED
|
||||
void AsyncWebServer::onSslFileRequest(AcSSlFileHandler cb, void* arg){
|
||||
_server.onSslFileRequest(cb, arg);
|
||||
|
|
Loading…
Reference in New Issue