Pokus o nejnovejsi upravy kvuli ESP32 - nutno overit na ESP8266!!
This commit is contained in:
parent
4ceb5c02d0
commit
71ab0b8e8c
@ -137,16 +137,17 @@ size_t AsyncEventSourceMessage::ack(size_t len, uint32_t time) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
// This could also return void as the return value is not used.
|
||||
// Leaving as-is for compatibility...
|
||||
size_t AsyncEventSourceMessage::send(AsyncClient *client) {
|
||||
const size_t len = _len - _sent;
|
||||
if(client->space() < len){
|
||||
return 0;
|
||||
}
|
||||
size_t sent = client->add((const char *)_data, len);
|
||||
if(client->canSend())
|
||||
client->send();
|
||||
_sent += sent;
|
||||
return sent;
|
||||
if (_sent >= _len) {
|
||||
return 0;
|
||||
}
|
||||
const size_t len_to_send = _len - _sent;
|
||||
auto position = reinterpret_cast<const char*>(_data + _sent);
|
||||
const size_t sent_now = client->write(position, len_to_send);
|
||||
_sent += sent_now;
|
||||
return sent_now;
|
||||
}
|
||||
|
||||
// Client
|
||||
@ -173,7 +174,9 @@ AsyncEventSourceClient::AsyncEventSourceClient(AsyncWebServerRequest *request, A
|
||||
}
|
||||
|
||||
AsyncEventSourceClient::~AsyncEventSourceClient(){
|
||||
_messageQueue.free();
|
||||
_lockmq.lock();
|
||||
_messageQueue.free();
|
||||
_lockmq.unlock();
|
||||
close();
|
||||
}
|
||||
|
||||
@ -184,33 +187,41 @@ void AsyncEventSourceClient::_queueMessage(AsyncEventSourceMessage *dataMessage)
|
||||
delete dataMessage;
|
||||
return;
|
||||
}
|
||||
//length() is not thread-safe, thus acquiring the lock before this call..
|
||||
_lockmq.lock();
|
||||
if(_messageQueue.length() >= SSE_MAX_QUEUED_MESSAGES){
|
||||
ets_printf(String(F("ERROR: Too many messages queued\n")).c_str());
|
||||
delete dataMessage;
|
||||
} else {
|
||||
_messageQueue.add(dataMessage);
|
||||
_messageQueue.add(dataMessage);
|
||||
// runqueue trigger when new messages added
|
||||
if(_client->canSend()) {
|
||||
_runQueue();
|
||||
}
|
||||
}
|
||||
if(_client->canSend())
|
||||
_runQueue();
|
||||
_lockmq.unlock();
|
||||
}
|
||||
|
||||
void AsyncEventSourceClient::_onAck(size_t len, uint32_t time){
|
||||
// Same here, acquiring the lock early
|
||||
_lockmq.lock();
|
||||
while(len && !_messageQueue.isEmpty()){
|
||||
len = _messageQueue.front()->ack(len, time);
|
||||
if(_messageQueue.front()->finished())
|
||||
_messageQueue.remove(_messageQueue.front());
|
||||
}
|
||||
|
||||
_runQueue();
|
||||
_lockmq.unlock();
|
||||
}
|
||||
|
||||
void AsyncEventSourceClient::_onPoll(){
|
||||
_lockmq.lock();
|
||||
if(!_messageQueue.isEmpty()){
|
||||
_runQueue();
|
||||
}
|
||||
_lockmq.unlock();
|
||||
}
|
||||
|
||||
|
||||
void AsyncEventSourceClient::_onTimeout(uint32_t time __attribute__((unused))){
|
||||
_client->close(true);
|
||||
}
|
||||
@ -225,7 +236,7 @@ void AsyncEventSourceClient::close(){
|
||||
_client->close();
|
||||
}
|
||||
|
||||
void AsyncEventSourceClient::write(const char * message, size_t len){
|
||||
void AsyncEventSourceClient::_write(const char * message, size_t len){
|
||||
_queueMessage(new AsyncEventSourceMessage(message, len));
|
||||
}
|
||||
|
||||
@ -234,15 +245,23 @@ void AsyncEventSourceClient::send(const char *message, const char *event, uint32
|
||||
_queueMessage(new AsyncEventSourceMessage(ev.c_str(), ev.length()));
|
||||
}
|
||||
|
||||
void AsyncEventSourceClient::_runQueue(){
|
||||
while(!_messageQueue.isEmpty() && _messageQueue.front()->finished()){
|
||||
_messageQueue.remove(_messageQueue.front());
|
||||
}
|
||||
size_t AsyncEventSourceClient::packetsWaiting() const {
|
||||
size_t len;
|
||||
_lockmq.lock();
|
||||
len = _messageQueue.length();
|
||||
_lockmq.unlock();
|
||||
return len;
|
||||
}
|
||||
|
||||
for(auto i = _messageQueue.begin(); i != _messageQueue.end(); ++i)
|
||||
{
|
||||
if(!(*i)->sent())
|
||||
void AsyncEventSourceClient::_runQueue() {
|
||||
// Calls to this private method now already protected by _lockmq acquisition
|
||||
// so no extra call of _lockmq.lock() here..
|
||||
for (auto i = _messageQueue.begin(); i != _messageQueue.end(); ++i) {
|
||||
// If it crashes here, iterator (i) has been invalidated as _messageQueue
|
||||
// has been changed... (UL 2020-11-15: Not supposed to happen any more ;-) )
|
||||
if (!(*i)->sent()) {
|
||||
(*i)->send(_client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -280,17 +299,22 @@ void AsyncEventSource::_addClient(AsyncEventSourceClient * client){
|
||||
client->write((const char *)temp, 2053);
|
||||
free(temp);
|
||||
}*/
|
||||
|
||||
AsyncWebLockGuard l(_client_queue_lock);
|
||||
_clients.add(client);
|
||||
if(_connectcb)
|
||||
_connectcb(client);
|
||||
}
|
||||
|
||||
void AsyncEventSource::_handleDisconnect(AsyncEventSourceClient * client){
|
||||
AsyncWebLockGuard l(_client_queue_lock);
|
||||
_clients.remove(client);
|
||||
}
|
||||
|
||||
void AsyncEventSource::close(){
|
||||
// While the whole loop is not done, the linked list is locked and so the
|
||||
// iterator should remain valid even when AsyncEventSource::_handleDisconnect()
|
||||
// is called very early
|
||||
AsyncWebLockGuard l(_client_queue_lock);
|
||||
for(const auto &c: _clients){
|
||||
if(c->connected())
|
||||
c->close();
|
||||
@ -299,37 +323,39 @@ void AsyncEventSource::close(){
|
||||
|
||||
// pmb fix
|
||||
size_t AsyncEventSource::avgPacketsWaiting() const {
|
||||
if(_clients.isEmpty())
|
||||
size_t aql = 0;
|
||||
uint32_t nConnectedClients = 0;
|
||||
AsyncWebLockGuard l(_client_queue_lock);
|
||||
if (_clients.isEmpty()) {
|
||||
return 0;
|
||||
|
||||
size_t aql=0;
|
||||
uint32_t nConnectedClients=0;
|
||||
|
||||
}
|
||||
for(const auto &c: _clients){
|
||||
if(c->connected()) {
|
||||
aql+=c->packetsWaiting();
|
||||
aql += c->packetsWaiting();
|
||||
++nConnectedClients;
|
||||
}
|
||||
}
|
||||
// return aql / nConnectedClients;
|
||||
return ((aql) + (nConnectedClients/2))/(nConnectedClients); // round up
|
||||
return ((aql) + (nConnectedClients/2)) / (nConnectedClients); // round up
|
||||
}
|
||||
|
||||
void AsyncEventSource::send(const char *message, const char *event, uint32_t id, uint32_t reconnect){
|
||||
|
||||
|
||||
void AsyncEventSource::send(
|
||||
const char *message, const char *event, uint32_t id, uint32_t reconnect){
|
||||
String ev = generateEventMessage(message, event, id, reconnect);
|
||||
AsyncWebLockGuard l(_client_queue_lock);
|
||||
for(const auto &c: _clients){
|
||||
if(c->connected()) {
|
||||
c->write(ev.c_str(), ev.length());
|
||||
c->_write(ev.c_str(), ev.length());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
size_t AsyncEventSource::count() const {
|
||||
return _clients.count_if([](AsyncEventSourceClient *c){
|
||||
return c->connected();
|
||||
});
|
||||
size_t n_clients;
|
||||
AsyncWebLockGuard l(_client_queue_lock);
|
||||
n_clients = _clients.count_if([](AsyncEventSourceClient *c){
|
||||
return c->connected();
|
||||
});
|
||||
return n_clients;
|
||||
}
|
||||
|
||||
bool AsyncEventSource::canHandle(AsyncWebServerRequest *request){
|
||||
|
@ -73,6 +73,8 @@ class AsyncEventSourceClient {
|
||||
AsyncEventSource *_server;
|
||||
uint32_t _lastId;
|
||||
LinkedList<AsyncEventSourceMessage *> _messageQueue;
|
||||
// ArFi 2020-08-27 for protecting/serializing _messageQueue
|
||||
AsyncPlainLock _lockmq;
|
||||
void _queueMessage(AsyncEventSourceMessage *dataMessage);
|
||||
void _runQueue();
|
||||
|
||||
@ -83,12 +85,12 @@ class AsyncEventSourceClient {
|
||||
|
||||
AsyncClient* client(){ return _client; }
|
||||
void close();
|
||||
void write(const char * message, size_t len);
|
||||
void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0);
|
||||
bool connected() const { return (_client != NULL) && _client->connected(); }
|
||||
uint32_t lastId() const { return _lastId; }
|
||||
size_t packetsWaiting() const { return _messageQueue.length(); }
|
||||
size_t packetsWaiting() const;
|
||||
|
||||
void _write(const char * message, size_t len);
|
||||
//system callbacks (do not call)
|
||||
void _onAck(size_t len, uint32_t time);
|
||||
void _onPoll();
|
||||
@ -100,6 +102,9 @@ class AsyncEventSource: public AsyncWebHandler {
|
||||
private:
|
||||
String _url;
|
||||
LinkedList<AsyncEventSourceClient *> _clients;
|
||||
// Same as for individual messages, protect mutations of _clients list
|
||||
// since simultaneous access from different tasks is possible
|
||||
AsyncWebLock _client_queue_lock;
|
||||
ArEventHandlerFunction _connectcb;
|
||||
ArAuthorizeConnectHandler _authorizeConnectHandler;
|
||||
public:
|
||||
@ -111,7 +116,7 @@ class AsyncEventSource: public AsyncWebHandler {
|
||||
void onConnect(ArEventHandlerFunction cb);
|
||||
void authorizeConnect(ArAuthorizeConnectHandler cb);
|
||||
void send(const char *message, const char *event=NULL, uint32_t id=0, uint32_t reconnect=0);
|
||||
size_t count() const; //number clinets connected
|
||||
size_t count() const; //number clients connected
|
||||
size_t avgPacketsWaiting() const;
|
||||
|
||||
//system callbacks (do not call)
|
||||
|
@ -41,7 +41,9 @@
|
||||
#if ARDUINOJSON_VERSION_MAJOR == 5
|
||||
#define ARDUINOJSON_5_COMPATIBILITY
|
||||
#else
|
||||
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
||||
#ifndef DYNAMIC_JSON_DOCUMENT_SIZE
|
||||
#define DYNAMIC_JSON_DOCUMENT_SIZE 1024
|
||||
#endif
|
||||
#endif
|
||||
|
||||
constexpr const char* JSON_MIMETYPE = "application/json";
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -24,7 +24,7 @@
|
||||
#include <Arduino.h>
|
||||
#ifdef ESP32
|
||||
#include <AsyncTCP.h>
|
||||
#define WS_MAX_QUEUED_MESSAGES 32
|
||||
#define WS_MAX_QUEUED_MESSAGES 16
|
||||
#else
|
||||
#include <ESPAsyncTCP.h>
|
||||
#define WS_MAX_QUEUED_MESSAGES 8
|
||||
@ -33,6 +33,10 @@
|
||||
|
||||
#include "AsyncWebSynchronization.h"
|
||||
|
||||
#include <list>
|
||||
#include <deque>
|
||||
#include <memory>
|
||||
|
||||
#ifdef ESP8266
|
||||
#include <Hash.h>
|
||||
#ifdef CRYPTO_HASH_h // include Hash.h from espressif framework if the first include was from the crypto library
|
||||
@ -80,78 +84,25 @@ typedef enum { WS_CONTINUATION, WS_TEXT, WS_BINARY, WS_DISCONNECT = 0x08, WS_PIN
|
||||
typedef enum { WS_MSG_SENDING, WS_MSG_SENT, WS_MSG_ERROR } AwsMessageStatus;
|
||||
typedef enum { WS_EVT_CONNECT, WS_EVT_DISCONNECT, WS_EVT_PONG, WS_EVT_ERROR, WS_EVT_DATA } AwsEventType;
|
||||
|
||||
class AsyncWebSocketMessageBuffer {
|
||||
private:
|
||||
uint8_t * _data;
|
||||
size_t _len;
|
||||
bool _lock;
|
||||
uint32_t _count;
|
||||
class AsyncWebSocketMessage
|
||||
{
|
||||
private:
|
||||
std::shared_ptr<std::vector<uint8_t>> _WSbuffer;
|
||||
uint8_t _opcode{WS_TEXT};
|
||||
bool _mask{false};
|
||||
AwsMessageStatus _status{WS_MSG_ERROR};
|
||||
size_t _sent{};
|
||||
size_t _ack{};
|
||||
size_t _acked{};
|
||||
|
||||
public:
|
||||
AsyncWebSocketMessageBuffer();
|
||||
AsyncWebSocketMessageBuffer(size_t size);
|
||||
AsyncWebSocketMessageBuffer(uint8_t * data, size_t size);
|
||||
AsyncWebSocketMessageBuffer(const AsyncWebSocketMessageBuffer &);
|
||||
AsyncWebSocketMessageBuffer(AsyncWebSocketMessageBuffer &&);
|
||||
~AsyncWebSocketMessageBuffer();
|
||||
void operator ++(int i) { (void)i; _count++; }
|
||||
void operator --(int i) { (void)i; if (_count > 0) { _count--; } ; }
|
||||
bool reserve(size_t size);
|
||||
void lock() { _lock = true; }
|
||||
void unlock() { _lock = false; }
|
||||
uint8_t * get() { return _data; }
|
||||
size_t length() { return _len; }
|
||||
uint32_t count() { return _count; }
|
||||
bool canDelete() { return (!_count && !_lock); }
|
||||
|
||||
friend AsyncWebSocket;
|
||||
|
||||
};
|
||||
|
||||
class AsyncWebSocketMessage {
|
||||
protected:
|
||||
uint8_t _opcode;
|
||||
bool _mask;
|
||||
AwsMessageStatus _status;
|
||||
public:
|
||||
AsyncWebSocketMessage():_opcode(WS_TEXT),_mask(false),_status(WS_MSG_ERROR){}
|
||||
virtual ~AsyncWebSocketMessage(){}
|
||||
virtual void ack(size_t len __attribute__((unused)), uint32_t time __attribute__((unused))){}
|
||||
virtual size_t send(AsyncClient *client __attribute__((unused))){ return 0; }
|
||||
virtual bool finished(){ return _status != WS_MSG_SENDING; }
|
||||
virtual bool betweenFrames() const { return false; }
|
||||
};
|
||||
|
||||
class AsyncWebSocketBasicMessage: public AsyncWebSocketMessage {
|
||||
private:
|
||||
size_t _len;
|
||||
size_t _sent;
|
||||
size_t _ack;
|
||||
size_t _acked;
|
||||
uint8_t * _data;
|
||||
public:
|
||||
AsyncWebSocketBasicMessage(const char * data, size_t len, uint8_t opcode=WS_TEXT, bool mask=false);
|
||||
AsyncWebSocketBasicMessage(uint8_t opcode=WS_TEXT, bool mask=false);
|
||||
virtual ~AsyncWebSocketBasicMessage() override;
|
||||
virtual bool betweenFrames() const override { return _acked == _ack; }
|
||||
virtual void ack(size_t len, uint32_t time) override ;
|
||||
virtual size_t send(AsyncClient *client) override ;
|
||||
};
|
||||
AsyncWebSocketMessage(std::shared_ptr<std::vector<uint8_t>> buffer, uint8_t opcode=WS_TEXT, bool mask=false);
|
||||
|
||||
class AsyncWebSocketMultiMessage: public AsyncWebSocketMessage {
|
||||
private:
|
||||
uint8_t * _data;
|
||||
size_t _len;
|
||||
size_t _sent;
|
||||
size_t _ack;
|
||||
size_t _acked;
|
||||
AsyncWebSocketMessageBuffer * _WSbuffer;
|
||||
public:
|
||||
AsyncWebSocketMultiMessage(AsyncWebSocketMessageBuffer * buffer, uint8_t opcode=WS_TEXT, bool mask=false);
|
||||
virtual ~AsyncWebSocketMultiMessage() override;
|
||||
virtual bool betweenFrames() const override { return _acked == _ack; }
|
||||
virtual void ack(size_t len, uint32_t time) override ;
|
||||
virtual size_t send(AsyncClient *client) override ;
|
||||
bool finished() const { return _status != WS_MSG_SENDING; }
|
||||
bool betweenFrames() const { return _acked == _ack; }
|
||||
|
||||
void ack(size_t len, uint32_t time);
|
||||
size_t send(AsyncClient *client);
|
||||
};
|
||||
|
||||
class AsyncWebSocketClient {
|
||||
@ -161,8 +112,10 @@ class AsyncWebSocketClient {
|
||||
uint32_t _clientId;
|
||||
AwsClientStatus _status;
|
||||
|
||||
LinkedList<AsyncWebSocketControl *> _controlQueue;
|
||||
LinkedList<AsyncWebSocketMessage *> _messageQueue;
|
||||
AsyncWebLock _lock;
|
||||
|
||||
std::deque<AsyncWebSocketControl> _controlQueue;
|
||||
std::deque<AsyncWebSocketMessage> _messageQueue;
|
||||
|
||||
uint8_t _pstate;
|
||||
AwsFrameInfo _pinfo;
|
||||
@ -170,8 +123,8 @@ class AsyncWebSocketClient {
|
||||
uint32_t _lastMessageTime;
|
||||
uint32_t _keepAlivePeriod;
|
||||
|
||||
void _queueMessage(AsyncWebSocketMessage *dataMessage);
|
||||
void _queueControl(AsyncWebSocketControl *controlMessage);
|
||||
void _queueControl(uint8_t opcode, const uint8_t *data=NULL, size_t len=0, bool mask=false);
|
||||
void _queueMessage(std::shared_ptr<std::vector<uint8_t>> buffer, uint8_t opcode=WS_TEXT, bool mask=false);
|
||||
void _runQueue();
|
||||
void _clearQueue();
|
||||
|
||||
@ -182,18 +135,22 @@ class AsyncWebSocketClient {
|
||||
~AsyncWebSocketClient();
|
||||
|
||||
//client id increments for the given server
|
||||
uint32_t id(){ return _clientId; }
|
||||
AwsClientStatus status(){ return _status; }
|
||||
AsyncClient* client(){ return _client; }
|
||||
uint32_t id() const { return _clientId; }
|
||||
AwsClientStatus status() const { return _status; }
|
||||
AsyncClient* client() { return _client; }
|
||||
const AsyncClient* client() const { return _client; }
|
||||
AsyncWebSocket *server(){ return _server; }
|
||||
const AsyncWebSocket *server() const { return _server; }
|
||||
AwsFrameInfo const &pinfo() const { return _pinfo; }
|
||||
|
||||
IPAddress remoteIP();
|
||||
uint16_t remotePort();
|
||||
IPAddress remoteIP() const;
|
||||
uint16_t remotePort() const;
|
||||
|
||||
bool shouldBeDeleted() const { return !_client; }
|
||||
|
||||
//control frames
|
||||
void close(uint16_t code=0, const char * message=NULL);
|
||||
void ping(uint8_t *data=NULL, size_t len=0);
|
||||
void ping(const uint8_t *data=NULL, size_t len=0);
|
||||
|
||||
//set auto-ping period in seconds. disabled if zero (default)
|
||||
void keepAlivePeriod(uint16_t seconds){
|
||||
@ -204,31 +161,30 @@ class AsyncWebSocketClient {
|
||||
}
|
||||
|
||||
//data packets
|
||||
void message(AsyncWebSocketMessage *message){ _queueMessage(message); }
|
||||
bool queueIsFull();
|
||||
size_t queueLen() { return _messageQueue.length() + _controlQueue.length(); }
|
||||
void message(std::shared_ptr<std::vector<uint8_t>> buffer, uint8_t opcode=WS_TEXT, bool mask=false) { _queueMessage(buffer, opcode, mask); }
|
||||
bool queueIsFull() const;
|
||||
size_t queueLen() const;
|
||||
|
||||
size_t printf(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
#ifndef ESP32
|
||||
size_t printf_P(PGM_P formatP, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
#endif
|
||||
void text(const char * message, size_t len);
|
||||
void text(const char * message);
|
||||
void text(uint8_t * message, size_t len);
|
||||
void text(char * message);
|
||||
void text(const String &message);
|
||||
void text(const __FlashStringHelper *data);
|
||||
void text(AsyncWebSocketMessageBuffer *buffer);
|
||||
|
||||
void text(std::shared_ptr<std::vector<uint8_t>> buffer);
|
||||
void text(const uint8_t *message, size_t len);
|
||||
void text(const char *message, size_t len);
|
||||
void text(const char *message);
|
||||
void text(const String &message);
|
||||
void text(const __FlashStringHelper *message);
|
||||
|
||||
void binary(std::shared_ptr<std::vector<uint8_t>> buffer);
|
||||
void binary(const uint8_t *message, size_t len);
|
||||
void binary(const char * message, size_t len);
|
||||
void binary(const char * message);
|
||||
void binary(uint8_t * message, size_t len);
|
||||
void binary(char * message);
|
||||
void binary(const String &message);
|
||||
void binary(const __FlashStringHelper *data, size_t len);
|
||||
void binary(AsyncWebSocketMessageBuffer *buffer);
|
||||
void binary(const __FlashStringHelper *message, size_t len);
|
||||
|
||||
bool canSend() { return _messageQueue.length() < WS_MAX_QUEUED_MESSAGES; }
|
||||
bool canSend() const;
|
||||
|
||||
//system callbacks (do not call)
|
||||
void _onAck(size_t len, uint32_t time);
|
||||
@ -244,11 +200,9 @@ typedef std::function<void(AsyncWebSocket * server, AsyncWebSocketClient * clien
|
||||
|
||||
//WebServer Handler implementation that plays the role of a socket server
|
||||
class AsyncWebSocket: public AsyncWebHandler {
|
||||
public:
|
||||
typedef LinkedList<AsyncWebSocketClient *> AsyncWebSocketClientLinkedList;
|
||||
private:
|
||||
String _url;
|
||||
AsyncWebSocketClientLinkedList _clients;
|
||||
std::list<AsyncWebSocketClient> _clients;
|
||||
uint32_t _cNextId;
|
||||
AwsEventHandler _eventHandler;
|
||||
AwsHandshakeHandler _handshakeHandler;
|
||||
@ -272,41 +226,34 @@ class AsyncWebSocket: public AsyncWebHandler {
|
||||
void closeAll(uint16_t code=0, const char * message=NULL);
|
||||
void cleanupClients(uint16_t maxClients = DEFAULT_MAX_WS_CLIENTS);
|
||||
|
||||
void ping(uint32_t id, uint8_t *data=NULL, size_t len=0);
|
||||
void pingAll(uint8_t *data=NULL, size_t len=0); // done
|
||||
void ping(uint32_t id, const uint8_t *data=NULL, size_t len=0);
|
||||
void pingAll(const uint8_t *data=NULL, size_t len=0); // done
|
||||
|
||||
void text(uint32_t id, const char * message, size_t len);
|
||||
void text(uint32_t id, const char * message);
|
||||
void text(uint32_t id, uint8_t * message, size_t len);
|
||||
void text(uint32_t id, char * message);
|
||||
void text(uint32_t id, const uint8_t * message, size_t len);
|
||||
void text(uint32_t id, const char *message, size_t len);
|
||||
void text(uint32_t id, const char *message);
|
||||
void text(uint32_t id, const String &message);
|
||||
void text(uint32_t id, const __FlashStringHelper *message);
|
||||
|
||||
void textAll(std::shared_ptr<std::vector<uint8_t>> buffer);
|
||||
void textAll(const uint8_t *message, size_t len);
|
||||
void textAll(const char * message, size_t len);
|
||||
void textAll(const char * message);
|
||||
void textAll(uint8_t * message, size_t len);
|
||||
void textAll(char * message);
|
||||
void textAll(const String &message);
|
||||
void textAll(const __FlashStringHelper *message); // need to convert
|
||||
void textAll(AsyncWebSocketMessageBuffer * buffer);
|
||||
|
||||
void binary(uint32_t id, const char * message, size_t len);
|
||||
void binary(uint32_t id, const char * message);
|
||||
void binary(uint32_t id, uint8_t * message, size_t len);
|
||||
void binary(uint32_t id, char * message);
|
||||
void binary(uint32_t id, const uint8_t *message, size_t len);
|
||||
void binary(uint32_t id, const char *message, size_t len);
|
||||
void binary(uint32_t id, const char *message);
|
||||
void binary(uint32_t id, const String &message);
|
||||
void binary(uint32_t id, const __FlashStringHelper *message, size_t len);
|
||||
|
||||
void binaryAll(const char * message, size_t len);
|
||||
void binaryAll(const char * message);
|
||||
void binaryAll(uint8_t * message, size_t len);
|
||||
void binaryAll(char * message);
|
||||
void binaryAll(std::shared_ptr<std::vector<uint8_t>> buffer);
|
||||
void binaryAll(const uint8_t *message, size_t len);
|
||||
void binaryAll(const char *message, size_t len);
|
||||
void binaryAll(const char *message);
|
||||
void binaryAll(const String &message);
|
||||
void binaryAll(const __FlashStringHelper *message, size_t len);
|
||||
void binaryAll(AsyncWebSocketMessageBuffer * buffer);
|
||||
|
||||
void message(uint32_t id, AsyncWebSocketMessage *message);
|
||||
void messageAll(AsyncWebSocketMultiMessage *message);
|
||||
|
||||
size_t printf(uint32_t id, const char *format, ...) __attribute__ ((format (printf, 3, 4)));
|
||||
size_t printfAll(const char *format, ...) __attribute__ ((format (printf, 2, 3)));
|
||||
@ -327,20 +274,12 @@ class AsyncWebSocket: public AsyncWebHandler {
|
||||
|
||||
//system callbacks (do not call)
|
||||
uint32_t _getNextId(){ return _cNextId++; }
|
||||
void _addClient(AsyncWebSocketClient * client);
|
||||
void _handleDisconnect(AsyncWebSocketClient * client);
|
||||
AsyncWebSocketClient *_newClient(AsyncWebServerRequest *request);
|
||||
void _handleEvent(AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
|
||||
virtual bool canHandle(AsyncWebServerRequest *request) override final;
|
||||
virtual void handleRequest(AsyncWebServerRequest *request) override final;
|
||||
|
||||
|
||||
// messagebuffer functions/objects.
|
||||
AsyncWebSocketMessageBuffer * makeBuffer(size_t size = 0);
|
||||
AsyncWebSocketMessageBuffer * makeBuffer(uint8_t * data, size_t size);
|
||||
LinkedList<AsyncWebSocketMessageBuffer *> _buffers;
|
||||
void _cleanBuffers();
|
||||
|
||||
AsyncWebSocketClientLinkedList getClients() const;
|
||||
const std::list<AsyncWebSocketClient> &getClients() const { return _clients; }
|
||||
};
|
||||
|
||||
//WebServer response to authenticate the socket and detach the tcp client from the web server request
|
||||
|
@ -8,39 +8,75 @@
|
||||
#ifdef ESP32
|
||||
|
||||
// This is the ESP32 version of the Sync Lock, using the FreeRTOS Semaphore
|
||||
class AsyncWebLock
|
||||
// Modified 'AsyncWebLock' to just only use mutex since pxCurrentTCB is not
|
||||
// always available. According to example by Arjan Filius, changed name,
|
||||
// added unimplemented version for ESP8266
|
||||
class AsyncPlainLock
|
||||
{
|
||||
private:
|
||||
SemaphoreHandle_t _lock;
|
||||
mutable void *_lockedBy;
|
||||
|
||||
public:
|
||||
AsyncWebLock() {
|
||||
AsyncPlainLock() {
|
||||
_lock = xSemaphoreCreateBinary();
|
||||
_lockedBy = NULL;
|
||||
// In this fails, the system is likely that much out of memory that
|
||||
// we should abort anyways. If assertions are disabled, nothing is lost..
|
||||
assert(_lock);
|
||||
xSemaphoreGive(_lock);
|
||||
}
|
||||
|
||||
~AsyncWebLock() {
|
||||
~AsyncPlainLock() {
|
||||
vSemaphoreDelete(_lock);
|
||||
}
|
||||
|
||||
bool lock() const {
|
||||
extern void *pxCurrentTCB;
|
||||
if (_lockedBy != pxCurrentTCB) {
|
||||
xSemaphoreTake(_lock, portMAX_DELAY);
|
||||
_lockedBy = pxCurrentTCB;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void unlock() const {
|
||||
_lockedBy = NULL;
|
||||
xSemaphoreGive(_lock);
|
||||
}
|
||||
};
|
||||
|
||||
// This is the ESP32 version of the Sync Lock, using the FreeRTOS Semaphore
|
||||
class AsyncWebLock
|
||||
{
|
||||
private:
|
||||
SemaphoreHandle_t _lock;
|
||||
mutable TaskHandle_t _lockedBy{};
|
||||
|
||||
public:
|
||||
AsyncWebLock()
|
||||
{
|
||||
_lock = xSemaphoreCreateBinary();
|
||||
// In this fails, the system is likely that much out of memory that
|
||||
// we should abort anyways. If assertions are disabled, nothing is lost..
|
||||
assert(_lock);
|
||||
_lockedBy = NULL;
|
||||
xSemaphoreGive(_lock);
|
||||
}
|
||||
|
||||
~AsyncWebLock() {
|
||||
vSemaphoreDelete(_lock);
|
||||
}
|
||||
|
||||
bool lock() const {
|
||||
const auto currentTask = xTaskGetCurrentTaskHandle();
|
||||
if (_lockedBy != currentTask) {
|
||||
xSemaphoreTake(_lock, portMAX_DELAY);
|
||||
_lockedBy = currentTask;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void unlock() const {
|
||||
_lockedBy = NULL;
|
||||
xSemaphoreGive(_lock);
|
||||
}
|
||||
};
|
||||
|
||||
#else
|
||||
|
||||
// This is the 8266 version of the Sync Lock which is currently unimplemented
|
||||
@ -61,6 +97,10 @@ public:
|
||||
void unlock() const {
|
||||
}
|
||||
};
|
||||
|
||||
// Same for AsyncPlainLock, for ESP8266 this is just the unimplemented version above.
|
||||
using AsyncPlainLock = AsyncWebLock;
|
||||
|
||||
#endif
|
||||
|
||||
class AsyncWebLockGuard
|
||||
@ -82,6 +122,13 @@ public:
|
||||
_lock->unlock();
|
||||
}
|
||||
}
|
||||
|
||||
void unlock() {
|
||||
if (_lock) {
|
||||
_lock->unlock();
|
||||
_lock = NULL;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
#endif // ASYNCWEBSYNCHRONIZATION_H_
|
||||
#endif // ASYNCWEBSYNCHRONIZATION_H_
|
||||
|
@ -24,6 +24,8 @@
|
||||
#include "Arduino.h"
|
||||
|
||||
#include <functional>
|
||||
#include <list>
|
||||
#include <vector>
|
||||
#include "FS.h"
|
||||
|
||||
#include "StringArray.h"
|
||||
@ -59,22 +61,14 @@ class AsyncResponseStream;
|
||||
|
||||
#ifndef WEBSERVER_H
|
||||
typedef enum {
|
||||
HTTP_GET = 0b0000000000000001,
|
||||
HTTP_POST = 0b0000000000000010,
|
||||
HTTP_DELETE = 0b0000000000000100,
|
||||
HTTP_PUT = 0b0000000000001000,
|
||||
HTTP_PATCH = 0b0000000000010000,
|
||||
HTTP_HEAD = 0b0000000000100000,
|
||||
HTTP_OPTIONS = 0b0000000001000000,
|
||||
HTTP_PROPFIND = 0b0000000010000000,
|
||||
HTTP_LOCK = 0b0000000100000000,
|
||||
HTTP_UNLOCK = 0b0000001000000000,
|
||||
HTTP_PROPPATCH = 0b0000010000000000,
|
||||
HTTP_MKCOL = 0b0000100000000000,
|
||||
HTTP_MOVE = 0b0001000000000000,
|
||||
HTTP_COPY = 0b0010000000000000,
|
||||
HTTP_RESERVED = 0b0100000000000000,
|
||||
HTTP_ANY = 0b0111111111111111,
|
||||
HTTP_GET = 0b00000001,
|
||||
HTTP_POST = 0b00000010,
|
||||
HTTP_DELETE = 0b00000100,
|
||||
HTTP_PUT = 0b00001000,
|
||||
HTTP_PATCH = 0b00010000,
|
||||
HTTP_HEAD = 0b00100000,
|
||||
HTTP_OPTIONS = 0b01000000,
|
||||
HTTP_ANY = 0b01111111,
|
||||
} WebRequestMethod;
|
||||
#endif
|
||||
|
||||
@ -94,7 +88,7 @@ namespace fs {
|
||||
//if this value is returned when asked for data, packet will not be sent and you will be asked for data again
|
||||
#define RESPONSE_TRY_AGAIN 0xFFFFFFFF
|
||||
|
||||
typedef uint16_t WebRequestMethodComposite;
|
||||
typedef uint8_t WebRequestMethodComposite;
|
||||
typedef std::function<void(void)> ArDisconnectHandler;
|
||||
|
||||
/*
|
||||
@ -129,6 +123,9 @@ class AsyncWebHeader {
|
||||
String _value;
|
||||
|
||||
public:
|
||||
AsyncWebHeader() = default;
|
||||
AsyncWebHeader(const AsyncWebHeader &) = default;
|
||||
|
||||
AsyncWebHeader(const String& name, const String& value): _name(name), _value(value){}
|
||||
AsyncWebHeader(const String& data): _name(), _value(){
|
||||
if(!data) return;
|
||||
@ -137,10 +134,12 @@ class AsyncWebHeader {
|
||||
_name = data.substring(0, index);
|
||||
_value = data.substring(index + 2);
|
||||
}
|
||||
~AsyncWebHeader(){}
|
||||
|
||||
AsyncWebHeader &operator=(const AsyncWebHeader &) = default;
|
||||
|
||||
const String& name() const { return _name; }
|
||||
const String& value() const { return _value; }
|
||||
String toString() const { return String(_name + F(": ") + _value + F("\r\n")); }
|
||||
String toString() const { return _name + F(": ") + _value + F("\r\n"); }
|
||||
};
|
||||
|
||||
/*
|
||||
@ -162,7 +161,7 @@ class AsyncWebServerRequest {
|
||||
AsyncWebServer* _server;
|
||||
AsyncWebHandler* _handler;
|
||||
AsyncWebServerResponse* _response;
|
||||
StringArray _interestingHeaders;
|
||||
std::vector<String> _interestingHeaders;
|
||||
ArDisconnectHandler _onDisconnectfn;
|
||||
|
||||
String _temp;
|
||||
@ -184,9 +183,9 @@ class AsyncWebServerRequest {
|
||||
size_t _contentLength;
|
||||
size_t _parsedLength;
|
||||
|
||||
LinkedList<AsyncWebHeader *> _headers;
|
||||
std::list<AsyncWebHeader> _headers;
|
||||
LinkedList<AsyncWebParameter *> _params;
|
||||
LinkedList<String *> _pathParams;
|
||||
std::vector<String> _pathParams;
|
||||
|
||||
uint8_t _multiParseState;
|
||||
uint8_t _boundaryPosition;
|
||||
@ -278,9 +277,12 @@ class AsyncWebServerRequest {
|
||||
bool hasHeader(const String& name) const; // check if header exists
|
||||
bool hasHeader(const __FlashStringHelper * data) const; // check if header exists
|
||||
|
||||
AsyncWebHeader* getHeader(const String& name) const;
|
||||
AsyncWebHeader* getHeader(const __FlashStringHelper * data) const;
|
||||
AsyncWebHeader* getHeader(size_t num) const;
|
||||
AsyncWebHeader* getHeader(const String& name);
|
||||
const AsyncWebHeader* getHeader(const String& name) const;
|
||||
AsyncWebHeader* getHeader(const __FlashStringHelper * data);
|
||||
const AsyncWebHeader* getHeader(const __FlashStringHelper * data) const;
|
||||
AsyncWebHeader* getHeader(size_t num);
|
||||
const AsyncWebHeader* getHeader(size_t num) const;
|
||||
|
||||
size_t params() const; // get arguments count
|
||||
bool hasParam(const String& name, bool post=false, bool file=false) const;
|
||||
@ -379,7 +381,7 @@ typedef enum {
|
||||
class AsyncWebServerResponse {
|
||||
protected:
|
||||
int _code;
|
||||
LinkedList<AsyncWebHeader *> _headers;
|
||||
std::list<AsyncWebHeader> _headers;
|
||||
String _contentType;
|
||||
size_t _contentLength;
|
||||
bool _sendContentLength;
|
||||
@ -462,17 +464,16 @@ class AsyncWebServer {
|
||||
};
|
||||
|
||||
class DefaultHeaders {
|
||||
using headers_t = LinkedList<AsyncWebHeader *>;
|
||||
using headers_t = std::list<AsyncWebHeader>;
|
||||
headers_t _headers;
|
||||
|
||||
DefaultHeaders()
|
||||
:_headers(headers_t([](AsyncWebHeader *h){ delete h; }))
|
||||
{}
|
||||
public:
|
||||
using ConstIterator = headers_t::ConstIterator;
|
||||
DefaultHeaders() = default;
|
||||
|
||||
using ConstIterator = headers_t::const_iterator;
|
||||
|
||||
void addHeader(const String& name, const String& value){
|
||||
_headers.add(new AsyncWebHeader(name, value));
|
||||
_headers.emplace_back(name, value);
|
||||
}
|
||||
|
||||
ConstIterator begin() const { return _headers.begin(); }
|
||||
@ -480,6 +481,7 @@ public:
|
||||
|
||||
DefaultHeaders(DefaultHeaders const &) = delete;
|
||||
DefaultHeaders &operator=(DefaultHeaders const &) = delete;
|
||||
|
||||
static DefaultHeaders &Instance() {
|
||||
static DefaultHeaders instance;
|
||||
return instance;
|
||||
|
@ -1,8 +1,6 @@
|
||||
#include "SPIFFSEditor.h"
|
||||
#include <FS.h>
|
||||
|
||||
#define EDFS
|
||||
|
||||
#ifndef EDFS
|
||||
#include "edit.htm.gz.h"
|
||||
#endif
|
||||
|
@ -171,23 +171,4 @@ class LinkedList {
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
class StringArray : public LinkedList<String> {
|
||||
public:
|
||||
|
||||
StringArray() : LinkedList(nullptr) {}
|
||||
|
||||
bool containsIgnoreCase(const String& str){
|
||||
for (const auto& s : *this) {
|
||||
if (str.equalsIgnoreCase(s)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
#endif /* STRINGARRAY_H_ */
|
||||
|
@ -77,9 +77,9 @@ static bool getMD5(uint8_t * data, uint16_t len, char * output){//33 bytes or mo
|
||||
memset(_buf, 0x00, 16);
|
||||
#ifdef ESP32
|
||||
mbedtls_md5_init(&_ctx);
|
||||
mbedtls_md5_starts(&_ctx);
|
||||
mbedtls_md5_update(&_ctx, data, len);
|
||||
mbedtls_md5_finish(&_ctx, _buf);
|
||||
mbedtls_md5_starts_ret(&_ctx);
|
||||
mbedtls_md5_update_ret(&_ctx, data, len);
|
||||
mbedtls_md5_finish_ret(&_ctx, _buf);
|
||||
#else
|
||||
MD5Init(&_ctx);
|
||||
MD5Update(&_ctx, data, len);
|
||||
|
@ -51,9 +51,7 @@ AsyncWebServerRequest::AsyncWebServerRequest(AsyncWebServer* s, AsyncClient* c)
|
||||
, _expectingContinue(false)
|
||||
, _contentLength(0)
|
||||
, _parsedLength(0)
|
||||
, _headers(LinkedList<AsyncWebHeader *>([](AsyncWebHeader *h){ delete h; }))
|
||||
, _params(LinkedList<AsyncWebParameter *>([](AsyncWebParameter *p){ delete p; }))
|
||||
, _pathParams(LinkedList<String *>([](String *p){ delete p; }))
|
||||
, _multiParseState(0)
|
||||
, _boundaryPosition(0)
|
||||
, _itemStartIndex(0)
|
||||
@ -76,12 +74,12 @@ AsyncWebServerRequest::AsyncWebServerRequest(AsyncWebServer* s, AsyncClient* c)
|
||||
}
|
||||
|
||||
AsyncWebServerRequest::~AsyncWebServerRequest(){
|
||||
_headers.free();
|
||||
_headers.clear();
|
||||
|
||||
_params.free();
|
||||
_pathParams.free();
|
||||
_pathParams.clear();
|
||||
|
||||
_interestingHeaders.free();
|
||||
_interestingHeaders.clear();
|
||||
|
||||
if(_response != NULL){
|
||||
delete _response;
|
||||
@ -182,11 +180,19 @@ void AsyncWebServerRequest::_onData(void *buf, size_t len){
|
||||
}
|
||||
|
||||
void AsyncWebServerRequest::_removeNotInterestingHeaders(){
|
||||
if (_interestingHeaders.containsIgnoreCase(F("ANY"))) return; // nothing to do
|
||||
for(const auto& header: _headers){
|
||||
if(!_interestingHeaders.containsIgnoreCase(header->name().c_str())){
|
||||
_headers.remove(header);
|
||||
}
|
||||
if (std::any_of(std::begin(_interestingHeaders), std::end(_interestingHeaders),
|
||||
[](const String &str){ return str.equalsIgnoreCase(F("ANY")); }))
|
||||
return; // nothing to do
|
||||
|
||||
for(auto iter = std::begin(_headers); iter != std::end(_headers); )
|
||||
{
|
||||
const auto name = iter->name();
|
||||
|
||||
if (std::none_of(std::begin(_interestingHeaders), std::end(_interestingHeaders),
|
||||
[&name](const String &str){ return str.equalsIgnoreCase(name); }))
|
||||
iter = _headers.erase(iter);
|
||||
else
|
||||
iter++;
|
||||
}
|
||||
}
|
||||
|
||||
@ -247,7 +253,7 @@ void AsyncWebServerRequest::_addParam(AsyncWebParameter *p){
|
||||
}
|
||||
|
||||
void AsyncWebServerRequest::_addPathParam(const char *p){
|
||||
_pathParams.add(new String(p));
|
||||
_pathParams.emplace_back(p);
|
||||
}
|
||||
|
||||
void AsyncWebServerRequest::_addGetParams(const String& params){
|
||||
@ -286,24 +292,6 @@ bool AsyncWebServerRequest::_parseReqHead(){
|
||||
_method = HTTP_HEAD;
|
||||
} else if(m == F("OPTIONS")){
|
||||
_method = HTTP_OPTIONS;
|
||||
} else if(m == F("PROPFIND")){
|
||||
_method = HTTP_PROPFIND;
|
||||
} else if(m == F("LOCK")){
|
||||
_method = HTTP_LOCK;
|
||||
} else if(m == F("UNLOCK")){
|
||||
_method = HTTP_UNLOCK;
|
||||
} else if(m == F("PROPPATCH")){
|
||||
_method = HTTP_PROPPATCH;
|
||||
} else if(m == F("MKCOL")){
|
||||
_method = HTTP_MKCOL;
|
||||
} else if(m == F("MOVE")){
|
||||
_method = HTTP_MOVE;
|
||||
} else if(m == F("COPY")){
|
||||
_method = HTTP_COPY;
|
||||
} else if(m == F("RESERVED")){
|
||||
_method = HTTP_RESERVED;
|
||||
} else if(m == F("ANY")){
|
||||
_method = HTTP_ANY;
|
||||
}
|
||||
|
||||
String g;
|
||||
@ -379,7 +367,7 @@ bool AsyncWebServerRequest::_parseReqHeader(){
|
||||
}
|
||||
}
|
||||
}
|
||||
_headers.add(new AsyncWebHeader(name, value));
|
||||
_headers.emplace_back(name, value);
|
||||
}
|
||||
_temp = String();
|
||||
return true;
|
||||
@ -620,12 +608,12 @@ void AsyncWebServerRequest::_parseLine(){
|
||||
}
|
||||
|
||||
size_t AsyncWebServerRequest::headers() const{
|
||||
return _headers.length();
|
||||
return _headers.size();
|
||||
}
|
||||
|
||||
bool AsyncWebServerRequest::hasHeader(const String& name) const {
|
||||
for(const auto& h: _headers){
|
||||
if(h->name().equalsIgnoreCase(name)){
|
||||
if(h.name().equalsIgnoreCase(name)){
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@ -636,22 +624,64 @@ bool AsyncWebServerRequest::hasHeader(const __FlashStringHelper * data) const {
|
||||
return hasHeader(String(data));
|
||||
}
|
||||
|
||||
AsyncWebHeader* AsyncWebServerRequest::getHeader(const String& name) const {
|
||||
for(const auto& h: _headers){
|
||||
if(h->name().equalsIgnoreCase(name)){
|
||||
return h;
|
||||
}
|
||||
AsyncWebHeader* AsyncWebServerRequest::getHeader(const String& name) {
|
||||
auto iter = std::find_if(std::begin(_headers), std::end(_headers),
|
||||
[&name](const AsyncWebHeader &header){ return header.name().equalsIgnoreCase(name); });
|
||||
|
||||
if (iter == std::end(_headers))
|
||||
return nullptr;
|
||||
|
||||
return &(*iter);
|
||||
}
|
||||
|
||||
const AsyncWebHeader* AsyncWebServerRequest::getHeader(const String& name) const {
|
||||
auto iter = std::find_if(std::begin(_headers), std::end(_headers),
|
||||
[&name](const AsyncWebHeader &header){ return header.name().equalsIgnoreCase(name); });
|
||||
|
||||
if (iter == std::end(_headers))
|
||||
return nullptr;
|
||||
|
||||
return &(*iter);
|
||||
}
|
||||
|
||||
AsyncWebHeader* AsyncWebServerRequest::getHeader(const __FlashStringHelper * data) {
|
||||
PGM_P p = reinterpret_cast<PGM_P>(data);
|
||||
size_t n = strlen_P(p);
|
||||
char * name = (char*) malloc(n+1);
|
||||
if (name) {
|
||||
strcpy_P(name, p);
|
||||
AsyncWebHeader* result = getHeader( String(name));
|
||||
free(name);
|
||||
return result;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
AsyncWebHeader* AsyncWebServerRequest::getHeader(const __FlashStringHelper * data) const {
|
||||
return getHeader(String(data));
|
||||
const AsyncWebHeader* AsyncWebServerRequest::getHeader(const __FlashStringHelper * data) const {
|
||||
PGM_P p = reinterpret_cast<PGM_P>(data);
|
||||
size_t n = strlen_P(p);
|
||||
char * name = (char*) malloc(n+1);
|
||||
if (name) {
|
||||
strcpy_P(name, p);
|
||||
const AsyncWebHeader* result = getHeader( String(name));
|
||||
free(name);
|
||||
return result;
|
||||
} else {
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
AsyncWebHeader* AsyncWebServerRequest::getHeader(size_t num) const {
|
||||
auto header = _headers.nth(num);
|
||||
return header ? *header : nullptr;
|
||||
AsyncWebHeader* AsyncWebServerRequest::getHeader(size_t num) {
|
||||
if (num >= _headers.size())
|
||||
return nullptr;
|
||||
return &(*std::next(std::begin(_headers), num));
|
||||
}
|
||||
|
||||
const AsyncWebHeader* AsyncWebServerRequest::getHeader(size_t num) const {
|
||||
if (num >= _headers.size())
|
||||
return nullptr;
|
||||
return &(*std::next(std::begin(_headers), num));
|
||||
}
|
||||
|
||||
size_t AsyncWebServerRequest::params() const {
|
||||
@ -690,8 +720,9 @@ AsyncWebParameter* AsyncWebServerRequest::getParam(size_t num) const {
|
||||
}
|
||||
|
||||
void AsyncWebServerRequest::addInterestingHeader(const String& name){
|
||||
if(!_interestingHeaders.containsIgnoreCase(name))
|
||||
_interestingHeaders.add(name);
|
||||
if(std::none_of(std::begin(_interestingHeaders), std::end(_interestingHeaders),
|
||||
[&name](const String &str){ return str.equalsIgnoreCase(name); }))
|
||||
_interestingHeaders.push_back(name);
|
||||
}
|
||||
|
||||
void AsyncWebServerRequest::send(AsyncWebServerResponse *response){
|
||||
@ -883,12 +914,11 @@ const String& AsyncWebServerRequest::argName(size_t i) const {
|
||||
}
|
||||
|
||||
const String& AsyncWebServerRequest::pathArg(size_t i) const {
|
||||
auto param = _pathParams.nth(i);
|
||||
return param ? **param : emptyString;
|
||||
return i < _pathParams.size() ? _pathParams[i] : emptyString;
|
||||
}
|
||||
|
||||
const String& AsyncWebServerRequest::header(const char* name) const {
|
||||
AsyncWebHeader* h = getHeader(String(name));
|
||||
const AsyncWebHeader* h = getHeader(String(name));
|
||||
return h ? h->value() : emptyString;
|
||||
}
|
||||
|
||||
@ -898,12 +928,12 @@ const String& AsyncWebServerRequest::header(const __FlashStringHelper * data) co
|
||||
|
||||
|
||||
const String& AsyncWebServerRequest::header(size_t i) const {
|
||||
AsyncWebHeader* h = getHeader(i);
|
||||
const AsyncWebHeader* h = getHeader(i);
|
||||
return h ? h->value() : emptyString;
|
||||
}
|
||||
|
||||
const String& AsyncWebServerRequest::headerName(size_t i) const {
|
||||
AsyncWebHeader* h = getHeader(i);
|
||||
const AsyncWebHeader* h = getHeader(i);
|
||||
return h ? h->name() : emptyString;
|
||||
}
|
||||
|
||||
@ -940,14 +970,6 @@ const __FlashStringHelper *AsyncWebServerRequest::methodToString() const {
|
||||
else if(_method & HTTP_PATCH) return F("PATCH");
|
||||
else if(_method & HTTP_HEAD) return F("HEAD");
|
||||
else if(_method & HTTP_OPTIONS) return F("OPTIONS");
|
||||
else if(_method & HTTP_PROPFIND) return F("PROPFIND");
|
||||
else if(_method & HTTP_LOCK) return F("LOCK");
|
||||
else if(_method & HTTP_UNLOCK) return F("UNLOCK");
|
||||
else if(_method & HTTP_PROPPATCH) return F("PROPPATCH");
|
||||
else if(_method & HTTP_MKCOL) return F("MKCOL");
|
||||
else if(_method & HTTP_MOVE) return F("MOVE");
|
||||
else if(_method & HTTP_COPY) return F("COPY");
|
||||
else if(_method & HTTP_RESERVED) return F("RESERVED");
|
||||
return F("UNKNOWN");
|
||||
}
|
||||
|
||||
|
@ -27,6 +27,8 @@
|
||||
#undef max
|
||||
#endif
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
// It is possible to restore these defines, but one can use _min and _max instead. Or std::min, std::max.
|
||||
|
||||
class AsyncBasicResponse: public AsyncWebServerResponse {
|
||||
@ -122,7 +124,7 @@ class cbuf;
|
||||
|
||||
class AsyncResponseStream: public AsyncAbstractResponse, public Print {
|
||||
private:
|
||||
cbuf *_content;
|
||||
std::unique_ptr<cbuf> _content;
|
||||
public:
|
||||
AsyncResponseStream(const String& contentType, size_t bufferSize);
|
||||
~AsyncResponseStream();
|
||||
|
@ -88,7 +88,6 @@ const __FlashStringHelper *AsyncWebServerResponse::responseCodeToString(int code
|
||||
|
||||
AsyncWebServerResponse::AsyncWebServerResponse()
|
||||
: _code(0)
|
||||
, _headers(LinkedList<AsyncWebHeader *>([](AsyncWebHeader *h){ delete h; }))
|
||||
, _contentType()
|
||||
, _contentLength(0)
|
||||
, _sendContentLength(true)
|
||||
@ -99,14 +98,12 @@ AsyncWebServerResponse::AsyncWebServerResponse()
|
||||
, _writtenLength(0)
|
||||
, _state(RESPONSE_SETUP)
|
||||
{
|
||||
for(auto header: DefaultHeaders::Instance()) {
|
||||
_headers.add(new AsyncWebHeader(header->name(), header->value()));
|
||||
for(const auto &header: DefaultHeaders::Instance()) {
|
||||
_headers.emplace_back(header);
|
||||
}
|
||||
}
|
||||
|
||||
AsyncWebServerResponse::~AsyncWebServerResponse(){
|
||||
_headers.free();
|
||||
}
|
||||
AsyncWebServerResponse::~AsyncWebServerResponse() = default;
|
||||
|
||||
void AsyncWebServerResponse::setCode(int code){
|
||||
if(_state == RESPONSE_SETUP)
|
||||
@ -124,7 +121,7 @@ void AsyncWebServerResponse::setContentType(const String& type){
|
||||
}
|
||||
|
||||
void AsyncWebServerResponse::addHeader(const String& name, const String& value){
|
||||
_headers.add(new AsyncWebHeader(name, value));
|
||||
_headers.emplace_back(name, value);
|
||||
}
|
||||
|
||||
String AsyncWebServerResponse::_assembleHead(uint8_t version){
|
||||
@ -150,10 +147,10 @@ String AsyncWebServerResponse::_assembleHead(uint8_t version){
|
||||
}
|
||||
|
||||
for(const auto& header: _headers){
|
||||
snprintf_P(buf, bufSize, PSTR("%s: %s\r\n"), header->name().c_str(), header->value().c_str());
|
||||
snprintf_P(buf, bufSize, PSTR("%s: %s\r\n"), header.name().c_str(), header.value().c_str());
|
||||
out.concat(buf);
|
||||
}
|
||||
_headers.free();
|
||||
_headers.clear();
|
||||
|
||||
out.concat(F("\r\n"));
|
||||
_headLength = out.length();
|
||||
@ -675,16 +672,15 @@ size_t AsyncProgmemResponse::_fillBuffer(uint8_t *data, size_t len){
|
||||
* Response Stream (You can print/write/printf to it, up to the contentLen bytes)
|
||||
* */
|
||||
|
||||
AsyncResponseStream::AsyncResponseStream(const String& contentType, size_t bufferSize){
|
||||
AsyncResponseStream::AsyncResponseStream(const String& contentType, size_t bufferSize)
|
||||
{
|
||||
_code = 200;
|
||||
_contentLength = 0;
|
||||
_contentType = contentType;
|
||||
_content = new cbuf(bufferSize);
|
||||
_content = std::unique_ptr<cbuf>(new cbuf(bufferSize)); //std::make_unique<cbuf>(bufferSize);
|
||||
}
|
||||
|
||||
AsyncResponseStream::~AsyncResponseStream(){
|
||||
delete _content;
|
||||
}
|
||||
AsyncResponseStream::~AsyncResponseStream() = default;
|
||||
|
||||
size_t AsyncResponseStream::_fillBuffer(uint8_t *buf, size_t maxLen){
|
||||
return _content->read((char*)buf, maxLen);
|
||||
|
Loading…
Reference in New Issue
Block a user