Skip to content
This repository was archived by the owner on Mar 5, 2026. It is now read-only.

Commit bde2fce

Browse files
baggiorme-no-dev
authored andcommitted
arduino json 6 async web server migration (me-no-dev#491)
* arduino json 6 async web server migration * Update .travis.yml fix travis error * Revert "Update .travis.yml" This reverts commit 37d1bf0. * gitignore * test fix travis * fix travis * fix proposed in me-no-dev#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
1 parent 95dedf7 commit bde2fce

8 files changed

Lines changed: 166 additions & 79 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode

.travis.yml

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,25 @@ language: bash
33
os:
44
- linux
55

6+
dist:
7+
- xenial
8+
9+
addons:
10+
apt:
11+
packages:
12+
- xvfb
13+
14+
# services:
15+
# - xvfb
16+
17+
618
script:
719
- /sbin/start-stop-daemon --start --quiet --pidfile /tmp/custom_xvfb_1.pid --make-pidfile --background --exec /usr/bin/Xvfb -- :1 -ac -screen 0 1280x1024x16
8-
- sleep 3
20+
# - /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;
921
- export DISPLAY=:1.0
22+
- sleep 3
23+
- ls -l /tmp/*pid
24+
1025
- wget http://downloads.arduino.cc/arduino-1.6.5-linux64.tar.xz
1126
- tar xf arduino-1.6.5-linux64.tar.xz
1227
- mv arduino-1.6.5 $HOME/arduino_ide

README.md

Lines changed: 74 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -11,76 +11,80 @@ For ESP32 it requires [AsyncTCP](https://github.com/me-no-dev/AsyncTCP) to work
1111
To use this library you might need to have the latest git versions of [ESP32](https://github.com/espressif/arduino-esp32) Arduino Core
1212

1313
## Table of contents
14-
- [ESPAsyncWebServer ](#espasyncwebserver-)
15-
- [Installation](#installation)
16-
- [Using PlatformIO](#using-platformio)
17-
- [Why should you care](#why-should-you-care)
18-
- [Important things to remember](#important-things-to-remember)
19-
- [Principles of operation](#principles-of-operation)
20-
- [The Async Web server](#the-async-web-server)
21-
- [Request Life Cycle](#request-life-cycle)
22-
- [Rewrites and how do they work](#rewrites-and-how-do-they-work)
23-
- [Handlers and how do they work](#handlers-and-how-do-they-work)
24-
- [Responses and how do they work](#responses-and-how-do-they-work)
25-
- [Template processing](#template-processing)
26-
- [Libraries and projects that use AsyncWebServer](#libraries-and-projects-that-use-asyncwebserver)
27-
- [Request Variables](#request-variables)
28-
- [Common Variables](#common-variables)
29-
- [Headers](#headers)
30-
- [GET, POST and FILE parameters](#get-post-and-file-parameters)
31-
- [FILE Upload handling](#file-upload-handling)
32-
- [Body data handling](#body-data-handling)
33-
- [JSON body handling with ArduinoJson](#json-body-handling-with-arduinojson)
34-
- [Responses](#responses)
35-
- [Redirect to another URL](#redirect-to-another-url)
36-
- [Basic response with HTTP Code](#basic-response-with-http-code)
37-
- [Basic response with HTTP Code and extra headers](#basic-response-with-http-code-and-extra-headers)
38-
- [Basic response with string content](#basic-response-with-string-content)
39-
- [Basic response with string content and extra headers](#basic-response-with-string-content-and-extra-headers)
40-
- [Send large webpage from PROGMEM](#send-large-webpage-from-progmem)
41-
- [Send large webpage from PROGMEM and extra headers](#send-large-webpage-from-progmem-and-extra-headers)
42-
- [Send large webpage from PROGMEM containing templates](#send-large-webpage-from-progmem-containing-templates)
43-
- [Send large webpage from PROGMEM containing templates and extra headers](#send-large-webpage-from-progmem-containing-templates-and-extra-headers)
44-
- [Send binary content from PROGMEM](#send-binary-content-from-progmem)
45-
- [Respond with content coming from a Stream](#respond-with-content-coming-from-a-stream)
46-
- [Respond with content coming from a Stream and extra headers](#respond-with-content-coming-from-a-stream-and-extra-headers)
47-
- [Respond with content coming from a Stream containing templates](#respond-with-content-coming-from-a-stream-containing-templates)
48-
- [Respond with content coming from a Stream containing templates and extra headers](#respond-with-content-coming-from-a-stream-containing-templates-and-extra-headers)
49-
- [Respond with content coming from a File](#respond-with-content-coming-from-a-file)
50-
- [Respond with content coming from a File and extra headers](#respond-with-content-coming-from-a-file-and-extra-headers)
51-
- [Respond with content coming from a File containing templates](#respond-with-content-coming-from-a-file-containing-templates)
52-
- [Respond with content using a callback](#respond-with-content-using-a-callback)
53-
- [Respond with content using a callback and extra headers](#respond-with-content-using-a-callback-and-extra-headers)
54-
- [Respond with content using a callback containing templates](#respond-with-content-using-a-callback-containing-templates)
55-
- [Respond with content using a callback containing templates and extra headers](#respond-with-content-using-a-callback-containing-templates-and-extra-headers)
56-
- [Chunked Response](#chunked-response)
57-
- [Print to response](#print-to-response)
58-
- [ArduinoJson Basic Response](#arduinojson-basic-response)
59-
- [ArduinoJson Advanced Response](#arduinojson-advanced-response)
60-
- [Serving static files](#serving-static-files)
61-
- [Serving specific file by name](#serving-specific-file-by-name)
62-
- [Serving files in directory](#serving-files-in-directory)
63-
- [Specifying Cache-Control header](#specifying-cache-control-header)
64-
- [Specifying Date-Modified header](#specifying-date-modified-header)
65-
- [Specifying Template Processor callback](#specifying-template-processor-callback)
66-
- [Using filters](#using-filters)
67-
- [Serve different site files in AP mode](#serve-different-site-files-in-ap-mode)
68-
- [Rewrite to different index on AP](#rewrite-to-different-index-on-ap)
69-
- [Serving different hosts](#serving-different-hosts)
70-
- [Bad Responses](#bad-responses)
71-
- [Respond with content using a callback without content length to HTTP/1.0 clients](#respond-with-content-using-a-callback-without-content-length-to-http10-clients)
72-
- [Async WebSocket Plugin](#async-websocket-plugin)
73-
- [Async WebSocket Event](#async-websocket-event)
74-
- [Methods for sending data to a socket client](#methods-for-sending-data-to-a-socket-client)
75-
- [Async Event Source Plugin](#async-event-source-plugin)
76-
- [Setup Event Source on the server](#setup-event-source-on-the-server)
77-
- [Setup Event Source in the browser](#setup-event-source-in-the-browser)
78-
- [Scanning for available WiFi Networks](#scanning-for-available-wifi-networks)
79-
- [Remove handlers and rewrites](#remove-handlers-and-rewrites)
80-
- [Setting up the server](#setting-up-the-server)
81-
- [Setup global and class functions as request handlers](#setup-global-and-class-functions-as-request-handlers)
82-
- [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections)
83-
- [Adding default headers to all responses](#adding-default-headers)
14+
- [ESPAsyncWebServer ![Build Status](https://travis-ci.org/me-no-dev/ESPAsyncWebServer)](#espasyncwebserver-build-statushttpstravis-ciorgme-no-devespasyncwebserver)
15+
- [Table of contents](#table-of-contents)
16+
- [Installation](#installation)
17+
- [Using PlatformIO](#using-platformio)
18+
- [Why should you care](#why-should-you-care)
19+
- [Important things to remember](#important-things-to-remember)
20+
- [Principles of operation](#principles-of-operation)
21+
- [The Async Web server](#the-async-web-server)
22+
- [Request Life Cycle](#request-life-cycle)
23+
- [Rewrites and how do they work](#rewrites-and-how-do-they-work)
24+
- [Handlers and how do they work](#handlers-and-how-do-they-work)
25+
- [Responses and how do they work](#responses-and-how-do-they-work)
26+
- [Template processing](#template-processing)
27+
- [Libraries and projects that use AsyncWebServer](#libraries-and-projects-that-use-asyncwebserver)
28+
- [Request Variables](#request-variables)
29+
- [Common Variables](#common-variables)
30+
- [Headers](#headers)
31+
- [GET, POST and FILE parameters](#get-post-and-file-parameters)
32+
- [FILE Upload handling](#file-upload-handling)
33+
- [Body data handling](#body-data-handling)
34+
- [JSON body handling with ArduinoJson](#json-body-handling-with-arduinojson)
35+
- [Responses](#responses)
36+
- [Redirect to another URL](#redirect-to-another-url)
37+
- [Basic response with HTTP Code](#basic-response-with-http-code)
38+
- [Basic response with HTTP Code and extra headers](#basic-response-with-http-code-and-extra-headers)
39+
- [Basic response with string content](#basic-response-with-string-content)
40+
- [Basic response with string content and extra headers](#basic-response-with-string-content-and-extra-headers)
41+
- [Send large webpage from PROGMEM](#send-large-webpage-from-progmem)
42+
- [Send large webpage from PROGMEM and extra headers](#send-large-webpage-from-progmem-and-extra-headers)
43+
- [Send large webpage from PROGMEM containing templates](#send-large-webpage-from-progmem-containing-templates)
44+
- [Send large webpage from PROGMEM containing templates and extra headers](#send-large-webpage-from-progmem-containing-templates-and-extra-headers)
45+
- [Send binary content from PROGMEM](#send-binary-content-from-progmem)
46+
- [Respond with content coming from a Stream](#respond-with-content-coming-from-a-stream)
47+
- [Respond with content coming from a Stream and extra headers](#respond-with-content-coming-from-a-stream-and-extra-headers)
48+
- [Respond with content coming from a Stream containing templates](#respond-with-content-coming-from-a-stream-containing-templates)
49+
- [Respond with content coming from a Stream containing templates and extra headers](#respond-with-content-coming-from-a-stream-containing-templates-and-extra-headers)
50+
- [Respond with content coming from a File](#respond-with-content-coming-from-a-file)
51+
- [Respond with content coming from a File and extra headers](#respond-with-content-coming-from-a-file-and-extra-headers)
52+
- [Respond with content coming from a File containing templates](#respond-with-content-coming-from-a-file-containing-templates)
53+
- [Respond with content using a callback](#respond-with-content-using-a-callback)
54+
- [Respond with content using a callback and extra headers](#respond-with-content-using-a-callback-and-extra-headers)
55+
- [Respond with content using a callback containing templates](#respond-with-content-using-a-callback-containing-templates)
56+
- [Respond with content using a callback containing templates and extra headers](#respond-with-content-using-a-callback-containing-templates-and-extra-headers)
57+
- [Chunked Response](#chunked-response)
58+
- [Chunked Response containing templates](#chunked-response-containing-templates)
59+
- [Print to response](#print-to-response)
60+
- [ArduinoJson Basic Response](#arduinojson-basic-response)
61+
- [ArduinoJson Advanced Response](#arduinojson-advanced-response)
62+
- [Serving static files](#serving-static-files)
63+
- [Serving specific file by name](#serving-specific-file-by-name)
64+
- [Serving files in directory](#serving-files-in-directory)
65+
- [Serving static files with authentication](#serving-static-files-with-authentication)
66+
- [Specifying Cache-Control header](#specifying-cache-control-header)
67+
- [Specifying Date-Modified header](#specifying-date-modified-header)
68+
- [Specifying Template Processor callback](#specifying-template-processor-callback)
69+
- [Using filters](#using-filters)
70+
- [Serve different site files in AP mode](#serve-different-site-files-in-ap-mode)
71+
- [Rewrite to different index on AP](#rewrite-to-different-index-on-ap)
72+
- [Serving different hosts](#serving-different-hosts)
73+
- [Bad Responses](#bad-responses)
74+
- [Respond with content using a callback without content length to HTTP/1.0 clients](#respond-with-content-using-a-callback-without-content-length-to-http10-clients)
75+
- [Async WebSocket Plugin](#async-websocket-plugin)
76+
- [Async WebSocket Event](#async-websocket-event)
77+
- [Methods for sending data to a socket client](#methods-for-sending-data-to-a-socket-client)
78+
- [Direct access to web socket message buffer](#direct-access-to-web-socket-message-buffer)
79+
- [Async Event Source Plugin](#async-event-source-plugin)
80+
- [Setup Event Source on the server](#setup-event-source-on-the-server)
81+
- [Setup Event Source in the browser](#setup-event-source-in-the-browser)
82+
- [Scanning for available WiFi Networks](#scanning-for-available-wifi-networks)
83+
- [Remove handlers and rewrites](#remove-handlers-and-rewrites)
84+
- [Setting up the server](#setting-up-the-server)
85+
- [Setup global and class functions as request handlers](#setup-global-and-class-functions-as-request-handlers)
86+
- [Methods for controlling websocket connections](#methods-for-controlling-websocket-connections)
87+
- [Adding Default Headers](#adding-default-headers)
8488

8589
## Installation
8690

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
"type": "git",
1313
"url": "https://github.com/me-no-dev/ESPAsyncWebServer.git"
1414
},
15-
"version": "1.2.0",
15+
"version": "1.2.1",
1616
"license": "LGPL-3.0",
1717
"frameworks": "arduino",
1818
"platforms": ["espressif8266", "espressif32"],

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP Async WebServer
2-
version=1.2.0
2+
version=1.2.1
33
author=Me-No-Dev
44
maintainer=Me-No-Dev
55
sentence=Async Web Server for ESP8266 and ESP31B

src/AsyncJson.h

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,15 @@
3535
#ifndef ASYNC_JSON_H_
3636
#define ASYNC_JSON_H_
3737
#include <ArduinoJson.h>
38+
#include <Print.h>
3839

39-
constexpr char* JSON_MIMETYPE = "application/json";
40+
#if ARDUINOJSON_VERSION_MAJOR == 5
41+
#define ARDUINOJSON_5_COMPATIBILITY
42+
#else
43+
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
44+
#endif
45+
46+
constexpr const char* JSON_MIMETYPE = "application/json";
4047

4148
/*
4249
* Json Response
@@ -63,14 +70,27 @@ class ChunkPrint : public Print {
6370
}
6471
return 0;
6572
}
73+
size_t write(const uint8_t *buffer, size_t size)
74+
{
75+
return this->Print::write(buffer, size);
76+
}
6677
};
6778

6879
class AsyncJsonResponse: public AsyncAbstractResponse {
6980
private:
81+
82+
#ifdef ARDUINOJSON_5_COMPATIBILITY
7083
DynamicJsonBuffer _jsonBuffer;
84+
#else
85+
DynamicJsonDocument _jsonBuffer;
86+
#endif
87+
7188
JsonVariant _root;
7289
bool _isValid;
73-
public:
90+
91+
public:
92+
93+
#ifdef ARDUINOJSON_5_COMPATIBILITY
7494
AsyncJsonResponse(bool isArray=false): _isValid{false} {
7595
_code = 200;
7696
_contentType = JSON_MIMETYPE;
@@ -79,11 +99,28 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
7999
else
80100
_root = _jsonBuffer.createObject();
81101
}
102+
#else
103+
AsyncJsonResponse(size_t maxJsonBufferSize = DYNAMIC_JSON_DOCUMENT_SIZE, bool isArray=false) : _jsonBuffer(maxJsonBufferSize), _isValid{false} {
104+
_code = 200;
105+
_contentType = JSON_MIMETYPE;
106+
if(isArray)
107+
_root = _jsonBuffer.createNestedArray();
108+
else
109+
_root = _jsonBuffer.createNestedObject();
110+
}
111+
#endif
112+
82113
~AsyncJsonResponse() {}
83114
JsonVariant & getRoot() { return _root; }
84115
bool _sourceValid() const { return _isValid; }
85116
size_t setLength() {
117+
118+
#ifdef ARDUINOJSON_5_COMPATIBILITY
86119
_contentLength = _root.measureLength();
120+
#else
121+
_contentLength = measureJson(_root);
122+
#endif
123+
87124
if (_contentLength) { _isValid = true; }
88125
return _contentLength;
89126
}
@@ -92,7 +129,12 @@ class AsyncJsonResponse: public AsyncAbstractResponse {
92129

93130
size_t _fillBuffer(uint8_t *data, size_t len){
94131
ChunkPrint dest(data, _sentLength, len);
132+
133+
#ifdef ARDUINOJSON_5_COMPATIBILITY
95134
_root.printTo( dest ) ;
135+
#else
136+
serializeJson(_root, dest);
137+
#endif
96138
return len;
97139
}
98140
};
@@ -106,9 +148,19 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler {
106148
WebRequestMethodComposite _method;
107149
ArJsonRequestHandlerFunction _onRequest;
108150
int _contentLength;
151+
#ifndef ARDUINOJSON_5_COMPATIBILITY
152+
const size_t maxJsonBufferSize;
153+
#endif
109154
int _maxContentLength;
110155
public:
111-
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest) : _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
156+
#ifdef ARDUINOJSON_5_COMPATIBILITY
157+
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest)
158+
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), _maxContentLength(16384) {}
159+
#else
160+
AsyncCallbackJsonWebHandler(const String& uri, ArJsonRequestHandlerFunction onRequest, size_t maxJsonBufferSize=DYNAMIC_JSON_DOCUMENT_SIZE)
161+
: _uri(uri), _method(HTTP_POST|HTTP_PUT|HTTP_PATCH), _onRequest(onRequest), maxJsonBufferSize(maxJsonBufferSize), _maxContentLength(16384) {}
162+
#endif
163+
112164
void setMethod(WebRequestMethodComposite method){ _method = method; }
113165
void setMaxContentLength(int maxContentLength){ _maxContentLength = maxContentLength; }
114166
void onRequest(ArJsonRequestHandlerFunction fn){ _onRequest = fn; }
@@ -123,7 +175,7 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler {
123175
if(_uri.length() && (_uri != request->url() && !request->url().startsWith(_uri+"/")))
124176
return false;
125177

126-
if (!request->contentType().equalsIgnoreCase(JSON_MIMETYPE))
178+
if ( !request->contentType().equalsIgnoreCase(JSON_MIMETYPE) )
127179
return false;
128180

129181
request->addInterestingHeader("ANY");
@@ -133,9 +185,18 @@ class AsyncCallbackJsonWebHandler: public AsyncWebHandler {
133185
virtual void handleRequest(AsyncWebServerRequest *request) override final {
134186
if(_onRequest) {
135187
if (request->_tempObject != NULL) {
188+
189+
#ifdef ARDUINOJSON_5_COMPATIBILITY
136190
DynamicJsonBuffer jsonBuffer;
137191
JsonVariant json = jsonBuffer.parse((uint8_t*)(request->_tempObject));
138192
if (json.success()) {
193+
#else
194+
DynamicJsonDocument jsonBuffer(this->maxJsonBufferSize);
195+
DeserializationError error = deserializeJson(jsonBuffer, (uint8_t*)(request->_tempObject));
196+
if(!error) {
197+
JsonVariant json = jsonBuffer.as<JsonVariant>();
198+
#endif
199+
139200
_onRequest(request, json);
140201
return;
141202
}

src/ESPAsyncWebServer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ class AsyncWebServer {
393393
~AsyncWebServer();
394394

395395
void begin();
396+
void end();
396397

397398
#if ASYNC_TCP_SSL_ENABLED
398399
void onSslFileRequest(AcSSlFileHandler cb, void* arg);

src/WebServer.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ AsyncWebServer::AsyncWebServer(uint16_t port)
5252
}
5353

5454
AsyncWebServer::~AsyncWebServer(){
55-
reset();
56-
delete _catchAllHandler;
55+
reset();
56+
end();
57+
if(_catchAllHandler) delete _catchAllHandler;
5758
}
5859

5960
AsyncWebRewrite& AsyncWebServer::addRewrite(AsyncWebRewrite* rewrite){
@@ -83,6 +84,10 @@ void AsyncWebServer::begin(){
8384
_server.begin();
8485
}
8586

87+
void AsyncWebServer::end(){
88+
_server.end();
89+
}
90+
8691
#if ASYNC_TCP_SSL_ENABLED
8792
void AsyncWebServer::onSslFileRequest(AcSSlFileHandler cb, void* arg){
8893
_server.onSslFileRequest(cb, arg);

0 commit comments

Comments
 (0)