mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
committed by
david gauchard
parent
98125f8860
commit
eea9999dc5
File diff suppressed because it is too large
Load Diff
@ -1,23 +1,23 @@
|
||||
/*
|
||||
ESP8266WebServer.h - Dead simple web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
ESP8266WebServer.h - Dead simple web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
|
||||
|
||||
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 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.
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
*/
|
||||
|
||||
|
||||
@ -30,8 +30,7 @@
|
||||
|
||||
enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };
|
||||
enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
|
||||
UPLOAD_FILE_ABORTED
|
||||
};
|
||||
UPLOAD_FILE_ABORTED };
|
||||
enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
|
||||
enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
|
||||
|
||||
@ -51,175 +50,153 @@ enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
|
||||
|
||||
class ESP8266WebServer;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
HTTPUploadStatus status;
|
||||
String filename;
|
||||
String name;
|
||||
String type;
|
||||
size_t totalSize; // total size of uploaded file so far
|
||||
size_t currentSize; // size of data currently in buf
|
||||
size_t contentLength; // size of entire post request, file size + headers and other request data.
|
||||
uint8_t buf[HTTP_UPLOAD_BUFLEN];
|
||||
typedef struct {
|
||||
HTTPUploadStatus status;
|
||||
String filename;
|
||||
String name;
|
||||
String type;
|
||||
size_t totalSize; // total size of uploaded file so far
|
||||
size_t currentSize; // size of data currently in buf
|
||||
size_t contentLength; // size of entire post request, file size + headers and other request data.
|
||||
uint8_t buf[HTTP_UPLOAD_BUFLEN];
|
||||
} HTTPUpload;
|
||||
|
||||
#include "detail/RequestHandler.h"
|
||||
|
||||
namespace fs
|
||||
{
|
||||
namespace fs {
|
||||
class FS;
|
||||
}
|
||||
|
||||
class ESP8266WebServer
|
||||
{
|
||||
public:
|
||||
ESP8266WebServer(IPAddress addr, int port = 80);
|
||||
ESP8266WebServer(int port = 80);
|
||||
virtual ~ESP8266WebServer();
|
||||
ESP8266WebServer(IPAddress addr, int port = 80);
|
||||
ESP8266WebServer(int port = 80);
|
||||
virtual ~ESP8266WebServer();
|
||||
|
||||
virtual void begin();
|
||||
virtual void begin(uint16_t port);
|
||||
virtual void handleClient();
|
||||
virtual void begin();
|
||||
virtual void begin(uint16_t port);
|
||||
virtual void handleClient();
|
||||
|
||||
virtual void close();
|
||||
void stop();
|
||||
virtual void close();
|
||||
void stop();
|
||||
|
||||
bool authenticate(const char * username, const char * password);
|
||||
void requestAuthentication(HTTPAuthMethod mode = BASIC_AUTH, const char* realm = NULL, const String& authFailMsg = String(""));
|
||||
bool authenticate(const char * username, const char * password);
|
||||
void requestAuthentication(HTTPAuthMethod mode = BASIC_AUTH, const char* realm = NULL, const String& authFailMsg = String("") );
|
||||
|
||||
typedef std::function<void(void)> THandlerFunction;
|
||||
void on(const String &uri, THandlerFunction handler);
|
||||
void on(const String &uri, HTTPMethod method, THandlerFunction fn);
|
||||
void on(const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
|
||||
void addHandler(RequestHandler* handler);
|
||||
void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL);
|
||||
void onNotFound(THandlerFunction fn); //called when handler is not assigned
|
||||
void onFileUpload(THandlerFunction fn); //handle file uploads
|
||||
typedef std::function<void(void)> THandlerFunction;
|
||||
void on(const String &uri, THandlerFunction handler);
|
||||
void on(const String &uri, HTTPMethod method, THandlerFunction fn);
|
||||
void on(const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
|
||||
void addHandler(RequestHandler* handler);
|
||||
void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
|
||||
void onNotFound(THandlerFunction fn); //called when handler is not assigned
|
||||
void onFileUpload(THandlerFunction fn); //handle file uploads
|
||||
|
||||
const String& uri() const
|
||||
{
|
||||
return _currentUri;
|
||||
}
|
||||
HTTPMethod method() const
|
||||
{
|
||||
return _currentMethod;
|
||||
}
|
||||
virtual WiFiClient client()
|
||||
{
|
||||
return _currentClient;
|
||||
}
|
||||
HTTPUpload& upload()
|
||||
{
|
||||
return *_currentUpload;
|
||||
}
|
||||
const String& uri() const { return _currentUri; }
|
||||
HTTPMethod method() const { return _currentMethod; }
|
||||
virtual WiFiClient client() { return _currentClient; }
|
||||
HTTPUpload& upload() { return *_currentUpload; }
|
||||
|
||||
const String& arg(String name) const; // get request argument value by name
|
||||
const String& arg(int i) const; // get request argument value by number
|
||||
const String& argName(int i) const; // get request argument name by number
|
||||
int args() const; // get arguments count
|
||||
bool hasArg(const String& name) const; // check if argument exists
|
||||
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
|
||||
const String& header(String name) const; // get request header value by name
|
||||
const String& header(int i) const; // get request header value by number
|
||||
const String& headerName(int i) const; // get request header name by number
|
||||
int headers() const; // get header count
|
||||
bool hasHeader(String name) const; // check if header exists
|
||||
const String& hostHeader() const; // get request host header if available or empty String if not
|
||||
const String& arg(String name) const; // get request argument value by name
|
||||
const String& arg(int i) const; // get request argument value by number
|
||||
const String& argName(int i) const; // get request argument name by number
|
||||
int args() const; // get arguments count
|
||||
bool hasArg(const String& name) const; // check if argument exists
|
||||
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
|
||||
const String& header(String name) const; // get request header value by name
|
||||
const String& header(int i) const; // get request header value by number
|
||||
const String& headerName(int i) const; // get request header name by number
|
||||
int headers() const; // get header count
|
||||
bool hasHeader(String name) const; // check if header exists
|
||||
const String& hostHeader() const; // get request host header if available or empty String if not
|
||||
|
||||
// send response to the client
|
||||
// code - HTTP response code, can be 200 or 404
|
||||
// content_type - HTTP content type, like "text/plain" or "image/png"
|
||||
// content - actual content body
|
||||
void send(int code, const char* content_type = NULL, const String& content = String(""));
|
||||
void send(int code, char* content_type, const String& content);
|
||||
void send(int code, const String& content_type, const String& content);
|
||||
void send_P(int code, PGM_P content_type, PGM_P content);
|
||||
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
|
||||
// send response to the client
|
||||
// code - HTTP response code, can be 200 or 404
|
||||
// content_type - HTTP content type, like "text/plain" or "image/png"
|
||||
// content - actual content body
|
||||
void send(int code, const char* content_type = NULL, const String& content = String(""));
|
||||
void send(int code, char* content_type, const String& content);
|
||||
void send(int code, const String& content_type, const String& content);
|
||||
void send_P(int code, PGM_P content_type, PGM_P content);
|
||||
void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
|
||||
|
||||
void setContentLength(const size_t contentLength);
|
||||
void sendHeader(const String& name, const String& value, bool first = false);
|
||||
void sendContent(const String& content);
|
||||
void sendContent_P(PGM_P content);
|
||||
void sendContent_P(PGM_P content, size_t size);
|
||||
void setContentLength(const size_t contentLength);
|
||||
void sendHeader(const String& name, const String& value, bool first = false);
|
||||
void sendContent(const String& content);
|
||||
void sendContent_P(PGM_P content);
|
||||
void sendContent_P(PGM_P content, size_t size);
|
||||
|
||||
static String urlDecode(const String& text);
|
||||
static String urlDecode(const String& text);
|
||||
|
||||
template<typename T>
|
||||
size_t streamFile(T &file, const String& contentType)
|
||||
{
|
||||
_streamFileCore(file.size(), file.name(), contentType);
|
||||
return _currentClient.write(file);
|
||||
}
|
||||
template<typename T>
|
||||
size_t streamFile(T &file, const String& contentType) {
|
||||
_streamFileCore(file.size(), file.name(), contentType);
|
||||
return _currentClient.write(file);
|
||||
}
|
||||
|
||||
static const String responseCodeToString(const int code);
|
||||
static const String responseCodeToString(const int code);
|
||||
|
||||
protected:
|
||||
virtual size_t _currentClientWrite(const char* b, size_t l)
|
||||
{
|
||||
return _currentClient.write(b, l);
|
||||
}
|
||||
virtual size_t _currentClientWrite_P(PGM_P b, size_t l)
|
||||
{
|
||||
return _currentClient.write_P(b, l);
|
||||
}
|
||||
void _addRequestHandler(RequestHandler* handler);
|
||||
void _handleRequest();
|
||||
void _finalizeResponse();
|
||||
bool _parseRequest(WiFiClient& client);
|
||||
void _parseArguments(const String& data);
|
||||
int _parseArgumentsPrivate(const String& data, std::function<void(String&, String&, const String&, int, int, int, int)> handler);
|
||||
bool _parseForm(WiFiClient& client, const String& boundary, uint32_t len);
|
||||
bool _parseFormUploadAborted();
|
||||
void _uploadWriteByte(uint8_t b);
|
||||
uint8_t _uploadReadByte(WiFiClient& client);
|
||||
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
|
||||
bool _collectHeader(const char* headerName, const char* headerValue);
|
||||
virtual size_t _currentClientWrite(const char* b, size_t l) { return _currentClient.write( b, l ); }
|
||||
virtual size_t _currentClientWrite_P(PGM_P b, size_t l) { return _currentClient.write_P( b, l ); }
|
||||
void _addRequestHandler(RequestHandler* handler);
|
||||
void _handleRequest();
|
||||
void _finalizeResponse();
|
||||
bool _parseRequest(WiFiClient& client);
|
||||
void _parseArguments(const String& data);
|
||||
int _parseArgumentsPrivate(const String& data, std::function<void(String&,String&,const String&,int,int,int,int)> handler);
|
||||
bool _parseForm(WiFiClient& client, const String& boundary, uint32_t len);
|
||||
bool _parseFormUploadAborted();
|
||||
void _uploadWriteByte(uint8_t b);
|
||||
uint8_t _uploadReadByte(WiFiClient& client);
|
||||
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
|
||||
bool _collectHeader(const char* headerName, const char* headerValue);
|
||||
|
||||
void _streamFileCore(const size_t fileSize, const String & fileName, const String & contentType);
|
||||
void _streamFileCore(const size_t fileSize, const String & fileName, const String & contentType);
|
||||
|
||||
static String _getRandomHexString();
|
||||
// for extracting Auth parameters
|
||||
String _extractParam(String& authReq, const String& param, const char delimit = '"') const;
|
||||
static String _getRandomHexString();
|
||||
// for extracting Auth parameters
|
||||
String _extractParam(String& authReq,const String& param,const char delimit = '"') const;
|
||||
|
||||
struct RequestArgument
|
||||
{
|
||||
String key;
|
||||
String value;
|
||||
};
|
||||
struct RequestArgument {
|
||||
String key;
|
||||
String value;
|
||||
};
|
||||
|
||||
WiFiServer _server;
|
||||
WiFiServer _server;
|
||||
|
||||
WiFiClient _currentClient;
|
||||
HTTPMethod _currentMethod;
|
||||
String _currentUri;
|
||||
uint8_t _currentVersion;
|
||||
HTTPClientStatus _currentStatus;
|
||||
unsigned long _statusChange;
|
||||
WiFiClient _currentClient;
|
||||
HTTPMethod _currentMethod;
|
||||
String _currentUri;
|
||||
uint8_t _currentVersion;
|
||||
HTTPClientStatus _currentStatus;
|
||||
unsigned long _statusChange;
|
||||
|
||||
RequestHandler* _currentHandler;
|
||||
RequestHandler* _firstHandler;
|
||||
RequestHandler* _lastHandler;
|
||||
THandlerFunction _notFoundHandler;
|
||||
THandlerFunction _fileUploadHandler;
|
||||
RequestHandler* _currentHandler;
|
||||
RequestHandler* _firstHandler;
|
||||
RequestHandler* _lastHandler;
|
||||
THandlerFunction _notFoundHandler;
|
||||
THandlerFunction _fileUploadHandler;
|
||||
|
||||
int _currentArgCount;
|
||||
RequestArgument* _currentArgs;
|
||||
std::unique_ptr<HTTPUpload> _currentUpload;
|
||||
int _postArgsLen;
|
||||
RequestArgument* _postArgs;
|
||||
int _currentArgCount;
|
||||
RequestArgument* _currentArgs;
|
||||
std::unique_ptr<HTTPUpload> _currentUpload;
|
||||
int _postArgsLen;
|
||||
RequestArgument* _postArgs;
|
||||
|
||||
int _headerKeysCount;
|
||||
RequestArgument* _currentHeaders;
|
||||
|
||||
size_t _contentLength;
|
||||
String _responseHeaders;
|
||||
|
||||
int _headerKeysCount;
|
||||
RequestArgument* _currentHeaders;
|
||||
String _hostHeader;
|
||||
bool _chunked;
|
||||
|
||||
size_t _contentLength;
|
||||
String _responseHeaders;
|
||||
|
||||
String _hostHeader;
|
||||
bool _chunked;
|
||||
|
||||
String _snonce; // Store noance and opaque for future comparison
|
||||
String _sopaque;
|
||||
String _srealm; // Store the Auth realm between Calls
|
||||
String _snonce; // Store noance and opaque for future comparison
|
||||
String _sopaque;
|
||||
String _srealm; // Store the Auth realm between Calls
|
||||
|
||||
};
|
||||
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
ESP8266WebServerSecure.h - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
ESP8266WebServerSecure.h - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
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 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.
|
||||
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
|
||||
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 <WiFiClientSecure.h>
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
ESP8266WebServerSecure.cpp - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
ESP8266WebServerSecure.cpp - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
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 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.
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
*/
|
||||
|
||||
|
||||
@ -34,19 +34,18 @@
|
||||
#define DEBUG_OUTPUT Serial
|
||||
#endif
|
||||
|
||||
namespace axTLS
|
||||
{
|
||||
namespace axTLS {
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
|
||||
ESP8266WebServerSecure::ESP8266WebServerSecure(IPAddress addr, int port)
|
||||
: _serverSecure(addr, port)
|
||||
ESP8266WebServerSecure::ESP8266WebServerSecure(IPAddress addr, int port)
|
||||
: _serverSecure(addr, port)
|
||||
{
|
||||
}
|
||||
|
||||
ESP8266WebServerSecure::ESP8266WebServerSecure(int port)
|
||||
: _serverSecure(port)
|
||||
: _serverSecure(port)
|
||||
{
|
||||
}
|
||||
|
||||
@ -62,116 +61,97 @@ void ESP8266WebServerSecure::setServerKeyAndCert(const uint8_t *key, int keyLen,
|
||||
_serverSecure.setServerKeyAndCert(key, keyLen, cert, certLen);
|
||||
}
|
||||
|
||||
ESP8266WebServerSecure::~ESP8266WebServerSecure()
|
||||
{
|
||||
// Nothing to do here.
|
||||
// Base class's destructor will be called to clean up itself
|
||||
ESP8266WebServerSecure::~ESP8266WebServerSecure() {
|
||||
// Nothing to do here.
|
||||
// Base class's destructor will be called to clean up itself
|
||||
}
|
||||
|
||||
// We need to basically cut-n-paste these from WebServer because of the problem
|
||||
// of object slicing. The class uses assignment operators like "WiFiClient x=y;"
|
||||
// When this happens, even if "y" is a WiFiClientSecure, the main class is
|
||||
// When this happens, even if "y" is a WiFiClientSecure, the main class is
|
||||
// already compiled down into code which will only copy the WiFiClient superclass
|
||||
// and not the extra bits for our own class (since when it was compiled it needed
|
||||
// to know the size of memory to allocate on the stack for this local variable
|
||||
// there's not realy anything else it could do).
|
||||
|
||||
void ESP8266WebServerSecure::begin()
|
||||
{
|
||||
_currentStatus = HC_NONE;
|
||||
_serverSecure.begin();
|
||||
if (!_headerKeysCount)
|
||||
{
|
||||
collectHeaders(0, 0);
|
||||
}
|
||||
void ESP8266WebServerSecure::begin() {
|
||||
_currentStatus = HC_NONE;
|
||||
_serverSecure.begin();
|
||||
if(!_headerKeysCount)
|
||||
collectHeaders(0, 0);
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::handleClient()
|
||||
{
|
||||
if (_currentStatus == HC_NONE)
|
||||
{
|
||||
WiFiClientSecure client = _serverSecure.available();
|
||||
if (!client)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void ESP8266WebServerSecure::handleClient() {
|
||||
if (_currentStatus == HC_NONE) {
|
||||
WiFiClientSecure client = _serverSecure.available();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ESP_HTTP_SERVER
|
||||
DEBUG_OUTPUT.println("New secure client");
|
||||
DEBUG_OUTPUT.println("New secure client");
|
||||
#endif
|
||||
|
||||
_currentClientSecure = client;
|
||||
_currentStatus = HC_WAIT_READ;
|
||||
_statusChange = millis();
|
||||
}
|
||||
_currentClientSecure = client;
|
||||
_currentStatus = HC_WAIT_READ;
|
||||
_statusChange = millis();
|
||||
}
|
||||
|
||||
bool keepCurrentClient = false;
|
||||
bool callYield = false;
|
||||
bool keepCurrentClient = false;
|
||||
bool callYield = false;
|
||||
|
||||
if (_currentClientSecure.connected())
|
||||
{
|
||||
switch (_currentStatus)
|
||||
{
|
||||
case HC_NONE:
|
||||
// No-op to avoid C++ compiler warning
|
||||
break;
|
||||
case HC_WAIT_READ:
|
||||
// Wait for data from client to become available
|
||||
if (_currentClientSecure.available())
|
||||
{
|
||||
if (_parseRequest(_currentClientSecure))
|
||||
{
|
||||
_currentClientSecure.setTimeout(HTTP_MAX_SEND_WAIT);
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
if (_currentClientSecure.connected()) {
|
||||
switch (_currentStatus) {
|
||||
case HC_NONE:
|
||||
// No-op to avoid C++ compiler warning
|
||||
break;
|
||||
case HC_WAIT_READ:
|
||||
// Wait for data from client to become available
|
||||
if (_currentClientSecure.available()) {
|
||||
if (_parseRequest(_currentClientSecure)) {
|
||||
_currentClientSecure.setTimeout(HTTP_MAX_SEND_WAIT);
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
|
||||
if (_currentClientSecure.connected())
|
||||
{
|
||||
_currentStatus = HC_WAIT_CLOSE;
|
||||
_statusChange = millis();
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else // !_currentClient.available()
|
||||
{
|
||||
if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT)
|
||||
{
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
callYield = true;
|
||||
}
|
||||
break;
|
||||
case HC_WAIT_CLOSE:
|
||||
// Wait for client to close the connection
|
||||
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT)
|
||||
{
|
||||
keepCurrentClient = true;
|
||||
callYield = true;
|
||||
}
|
||||
if (_currentClientSecure.connected()) {
|
||||
_currentStatus = HC_WAIT_CLOSE;
|
||||
_statusChange = millis();
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
}
|
||||
} else { // !_currentClient.available()
|
||||
if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT) {
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
callYield = true;
|
||||
}
|
||||
break;
|
||||
case HC_WAIT_CLOSE:
|
||||
// Wait for client to close the connection
|
||||
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
|
||||
keepCurrentClient = true;
|
||||
callYield = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!keepCurrentClient)
|
||||
{
|
||||
if (!keepCurrentClient) {
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
_currentClientSecure = WiFiClientSecure();
|
||||
_currentClientSecure = WiFiClientSecure();
|
||||
#pragma GCC diagnostic pop
|
||||
_currentStatus = HC_NONE;
|
||||
_currentUpload.reset();
|
||||
}
|
||||
_currentStatus = HC_NONE;
|
||||
_currentUpload.reset();
|
||||
}
|
||||
|
||||
if (callYield)
|
||||
{
|
||||
yield();
|
||||
}
|
||||
if (callYield) {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::close()
|
||||
{
|
||||
_currentClientSecure.stop();
|
||||
_serverSecure.close();
|
||||
void ESP8266WebServerSecure::close() {
|
||||
_currentClientSecure.stop();
|
||||
_serverSecure.close();
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
ESP8266WebServerSecure.h - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
ESP8266WebServerSecure.h - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
@ -27,48 +27,37 @@
|
||||
#include <WiFiServerSecureAxTLS.h>
|
||||
#include <WiFiClientSecureAxTLS.h>
|
||||
|
||||
namespace axTLS
|
||||
{
|
||||
namespace axTLS {
|
||||
|
||||
class ESP8266WebServerSecure : public ESP8266WebServer
|
||||
{
|
||||
public:
|
||||
ESP8266WebServerSecure(IPAddress addr, int port = 443);
|
||||
ESP8266WebServerSecure(int port = 443);
|
||||
virtual ~ESP8266WebServerSecure();
|
||||
ESP8266WebServerSecure(IPAddress addr, int port = 443);
|
||||
ESP8266WebServerSecure(int port = 443);
|
||||
virtual ~ESP8266WebServerSecure();
|
||||
|
||||
void setServerKeyAndCert_P(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
void setServerKeyAndCert(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
void setServerKeyAndCert_P(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
void setServerKeyAndCert(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
|
||||
WiFiClient client() override
|
||||
{
|
||||
return _currentClientSecure;
|
||||
}
|
||||
WiFiClient client() override { return _currentClientSecure; }
|
||||
|
||||
void begin() override;
|
||||
void handleClient() override;
|
||||
void close() override;
|
||||
void begin() override;
|
||||
void handleClient() override;
|
||||
void close() override;
|
||||
|
||||
template<typename T>
|
||||
size_t streamFile(T &file, const String& contentType)
|
||||
{
|
||||
_streamFileCore(file.size(), file.name(), contentType);
|
||||
return _currentClientSecure.write(file);
|
||||
}
|
||||
template<typename T>
|
||||
size_t streamFile(T &file, const String& contentType) {
|
||||
_streamFileCore(file.size(), file.name(), contentType);
|
||||
return _currentClientSecure.write(file);
|
||||
}
|
||||
|
||||
private:
|
||||
size_t _currentClientWrite(const char *bytes, size_t len) override
|
||||
{
|
||||
return _currentClientSecure.write((const uint8_t *)bytes, len);
|
||||
}
|
||||
size_t _currentClientWrite_P(PGM_P bytes, size_t len) override
|
||||
{
|
||||
return _currentClientSecure.write_P(bytes, len);
|
||||
}
|
||||
size_t _currentClientWrite (const char *bytes, size_t len) override { return _currentClientSecure.write((const uint8_t *)bytes, len); }
|
||||
size_t _currentClientWrite_P (PGM_P bytes, size_t len) override { return _currentClientSecure.write_P(bytes, len); }
|
||||
|
||||
protected:
|
||||
WiFiServerSecure _serverSecure;
|
||||
WiFiClientSecure _currentClientSecure;
|
||||
WiFiServerSecure _serverSecure;
|
||||
WiFiClientSecure _currentClientSecure;
|
||||
};
|
||||
|
||||
};
|
||||
|
@ -1,23 +1,23 @@
|
||||
/*
|
||||
ESP8266WebServerSecure.cpp - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
ESP8266WebServerSecure.cpp - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
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 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.
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
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
|
||||
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
|
||||
*/
|
||||
|
||||
|
||||
@ -34,153 +34,132 @@
|
||||
#define DEBUG_OUTPUT Serial
|
||||
#endif
|
||||
|
||||
namespace BearSSL
|
||||
{
|
||||
namespace BearSSL {
|
||||
|
||||
ESP8266WebServerSecure::ESP8266WebServerSecure(IPAddress addr, int port)
|
||||
: _serverSecure(addr, port)
|
||||
ESP8266WebServerSecure::ESP8266WebServerSecure(IPAddress addr, int port)
|
||||
: _serverSecure(addr, port)
|
||||
{
|
||||
}
|
||||
|
||||
ESP8266WebServerSecure::ESP8266WebServerSecure(int port)
|
||||
: _serverSecure(port)
|
||||
: _serverSecure(port)
|
||||
{
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::setRSACert(const X509List *chain, const PrivateKey *sk)
|
||||
{
|
||||
_serverSecure.setRSACert(chain, sk);
|
||||
_serverSecure.setRSACert(chain, sk);
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::setECCert(const X509List *chain, unsigned cert_issuer_key_type, const PrivateKey *sk)
|
||||
{
|
||||
_serverSecure.setECCert(chain, cert_issuer_key_type, sk);
|
||||
_serverSecure.setECCert(chain, cert_issuer_key_type, sk);
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::setBufferSizes(int recv, int xmit)
|
||||
{
|
||||
_serverSecure.setBufferSizes(recv, xmit);
|
||||
_serverSecure.setBufferSizes(recv, xmit);
|
||||
}
|
||||
|
||||
ESP8266WebServerSecure::~ESP8266WebServerSecure()
|
||||
{
|
||||
// Nothing to do here.
|
||||
// Base class's destructor will be called to clean up itself
|
||||
ESP8266WebServerSecure::~ESP8266WebServerSecure() {
|
||||
// Nothing to do here.
|
||||
// Base class's destructor will be called to clean up itself
|
||||
}
|
||||
|
||||
// We need to basically cut-n-paste these from WebServer because of the problem
|
||||
// of object slicing. The class uses assignment operators like "WiFiClient x=y;"
|
||||
// When this happens, even if "y" is a WiFiClientSecure, the main class is
|
||||
// When this happens, even if "y" is a WiFiClientSecure, the main class is
|
||||
// already compiled down into code which will only copy the WiFiClient superclass
|
||||
// and not the extra bits for our own class (since when it was compiled it needed
|
||||
// to know the size of memory to allocate on the stack for this local variable
|
||||
// there's not realy anything else it could do).
|
||||
|
||||
void ESP8266WebServerSecure::begin()
|
||||
{
|
||||
_currentStatus = HC_NONE;
|
||||
_serverSecure.begin();
|
||||
if (!_headerKeysCount)
|
||||
{
|
||||
collectHeaders(0, 0);
|
||||
}
|
||||
void ESP8266WebServerSecure::begin() {
|
||||
_currentStatus = HC_NONE;
|
||||
_serverSecure.begin();
|
||||
if(!_headerKeysCount)
|
||||
collectHeaders(0, 0);
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::handleClient()
|
||||
{
|
||||
if (_currentStatus == HC_NONE)
|
||||
{
|
||||
WiFiClientSecure client = _serverSecure.available();
|
||||
if (!client)
|
||||
{
|
||||
return;
|
||||
}
|
||||
void ESP8266WebServerSecure::handleClient() {
|
||||
if (_currentStatus == HC_NONE) {
|
||||
WiFiClientSecure client = _serverSecure.available();
|
||||
if (!client) {
|
||||
return;
|
||||
}
|
||||
|
||||
#ifdef DEBUG_ESP_HTTP_SERVER
|
||||
DEBUG_OUTPUT.println("New secure client");
|
||||
DEBUG_OUTPUT.println("New secure client");
|
||||
#endif
|
||||
|
||||
_currentClientSecure = client;
|
||||
_currentStatus = HC_WAIT_READ;
|
||||
_statusChange = millis();
|
||||
}
|
||||
_currentClientSecure = client;
|
||||
_currentStatus = HC_WAIT_READ;
|
||||
_statusChange = millis();
|
||||
}
|
||||
|
||||
bool keepCurrentClient = false;
|
||||
bool callYield = false;
|
||||
bool keepCurrentClient = false;
|
||||
bool callYield = false;
|
||||
|
||||
if (_currentClientSecure.connected())
|
||||
{
|
||||
switch (_currentStatus)
|
||||
{
|
||||
case HC_NONE:
|
||||
// No-op to avoid C++ compiler warning
|
||||
break;
|
||||
case HC_WAIT_READ:
|
||||
// Wait for data from client to become available
|
||||
if (_currentClientSecure.available())
|
||||
{
|
||||
if (_parseRequest(_currentClientSecure))
|
||||
{
|
||||
_currentClientSecure.setTimeout(HTTP_MAX_SEND_WAIT);
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
if (_currentClientSecure.connected()) {
|
||||
switch (_currentStatus) {
|
||||
case HC_NONE:
|
||||
// No-op to avoid C++ compiler warning
|
||||
break;
|
||||
case HC_WAIT_READ:
|
||||
// Wait for data from client to become available
|
||||
if (_currentClientSecure.available()) {
|
||||
if (_parseRequest(_currentClientSecure)) {
|
||||
_currentClientSecure.setTimeout(HTTP_MAX_SEND_WAIT);
|
||||
_contentLength = CONTENT_LENGTH_NOT_SET;
|
||||
_handleRequest();
|
||||
|
||||
if (_currentClientSecure.connected())
|
||||
{
|
||||
_currentStatus = HC_WAIT_CLOSE;
|
||||
_statusChange = millis();
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
else // !_currentClient.available()
|
||||
{
|
||||
if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT)
|
||||
{
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
callYield = true;
|
||||
}
|
||||
break;
|
||||
case HC_WAIT_CLOSE:
|
||||
// Wait for client to close the connection
|
||||
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT)
|
||||
{
|
||||
keepCurrentClient = true;
|
||||
callYield = true;
|
||||
}
|
||||
if (_currentClientSecure.connected()) {
|
||||
_currentStatus = HC_WAIT_CLOSE;
|
||||
_statusChange = millis();
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
}
|
||||
} else { // !_currentClient.available()
|
||||
if (millis() - _statusChange <= HTTP_MAX_DATA_WAIT) {
|
||||
keepCurrentClient = true;
|
||||
}
|
||||
callYield = true;
|
||||
}
|
||||
break;
|
||||
case HC_WAIT_CLOSE:
|
||||
// Wait for client to close the connection
|
||||
if (millis() - _statusChange <= HTTP_MAX_CLOSE_WAIT) {
|
||||
keepCurrentClient = true;
|
||||
callYield = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!keepCurrentClient)
|
||||
{
|
||||
_currentClientSecure = WiFiClientSecure();
|
||||
_currentStatus = HC_NONE;
|
||||
_currentUpload.reset();
|
||||
}
|
||||
if (!keepCurrentClient) {
|
||||
_currentClientSecure = WiFiClientSecure();
|
||||
_currentStatus = HC_NONE;
|
||||
_currentUpload.reset();
|
||||
}
|
||||
|
||||
if (callYield)
|
||||
{
|
||||
yield();
|
||||
}
|
||||
if (callYield) {
|
||||
yield();
|
||||
}
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::close()
|
||||
{
|
||||
_currentClientSecure.flush();
|
||||
_currentClientSecure.stop();
|
||||
_serverSecure.close();
|
||||
void ESP8266WebServerSecure::close() {
|
||||
_currentClientSecure.flush();
|
||||
_currentClientSecure.stop();
|
||||
_serverSecure.close();
|
||||
}
|
||||
|
||||
|
||||
void ESP8266WebServerSecure::setServerKeyAndCert_P(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen)
|
||||
{
|
||||
_serverSecure.setServerKeyAndCert_P(key, keyLen, cert, certLen);
|
||||
void ESP8266WebServerSecure::setServerKeyAndCert_P(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen) {
|
||||
_serverSecure.setServerKeyAndCert_P(key, keyLen, cert, certLen);
|
||||
}
|
||||
|
||||
void ESP8266WebServerSecure::setServerKeyAndCert(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen)
|
||||
{
|
||||
_serverSecure.setServerKeyAndCert(key, keyLen, cert, certLen);
|
||||
_serverSecure.setServerKeyAndCert(key, keyLen, cert, certLen);
|
||||
}
|
||||
|
||||
};
|
||||
|
@ -1,22 +1,22 @@
|
||||
/*
|
||||
ESP8266WebServerSecure.h - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
ESP8266WebServerSecure.h - Dead simple HTTPS web-server.
|
||||
Supports only one simultaneous client, knows how to handle GET and POST.
|
||||
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
Copyright (c) 2017 Earle F. Philhower, III. All rights reserved.
|
||||
|
||||
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 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.
|
||||
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
|
||||
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
|
||||
*/
|
||||
|
||||
|
||||
@ -27,53 +27,42 @@
|
||||
#include <BearSSLHelpers.h>
|
||||
#include <WiFiServerSecureBearSSL.h>
|
||||
|
||||
namespace BearSSL
|
||||
{
|
||||
namespace BearSSL {
|
||||
|
||||
class ESP8266WebServerSecure : public ESP8266WebServer
|
||||
{
|
||||
public:
|
||||
ESP8266WebServerSecure(IPAddress addr, int port = 443);
|
||||
ESP8266WebServerSecure(int port = 443);
|
||||
virtual ~ESP8266WebServerSecure();
|
||||
ESP8266WebServerSecure(IPAddress addr, int port = 443);
|
||||
ESP8266WebServerSecure(int port = 443);
|
||||
virtual ~ESP8266WebServerSecure();
|
||||
|
||||
void setBufferSizes(int recv, int xmit);
|
||||
void setRSACert(const X509List *chain, const PrivateKey *sk);
|
||||
void setECCert(const X509List *chain, unsigned cert_issuer_key_type, const PrivateKey *sk);
|
||||
void setBufferSizes(int recv, int xmit);
|
||||
void setRSACert(const X509List *chain, const PrivateKey *sk);
|
||||
void setECCert(const X509List *chain, unsigned cert_issuer_key_type, const PrivateKey *sk);
|
||||
|
||||
WiFiClient client() override
|
||||
{
|
||||
return _currentClientSecure;
|
||||
}
|
||||
WiFiClient client() override { return _currentClientSecure; }
|
||||
|
||||
void begin() override;
|
||||
void handleClient() override;
|
||||
void close() override;
|
||||
void begin() override;
|
||||
void handleClient() override;
|
||||
void close() override;
|
||||
|
||||
template<typename T>
|
||||
size_t streamFile(T &file, const String& contentType)
|
||||
{
|
||||
_streamFileCore(file.size(), file.name(), contentType);
|
||||
return _currentClientSecure.write(file);
|
||||
}
|
||||
template<typename T>
|
||||
size_t streamFile(T &file, const String& contentType) {
|
||||
_streamFileCore(file.size(), file.name(), contentType);
|
||||
return _currentClientSecure.write(file);
|
||||
}
|
||||
|
||||
// AXTLS Compatibility
|
||||
void setServerKeyAndCert_P(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
void setServerKeyAndCert(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
// AXTLS Compatibility
|
||||
void setServerKeyAndCert_P(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
void setServerKeyAndCert(const uint8_t *key, int keyLen, const uint8_t *cert, int certLen);
|
||||
|
||||
private:
|
||||
size_t _currentClientWrite(const char *bytes, size_t len) override
|
||||
{
|
||||
return _currentClientSecure.write((const uint8_t *)bytes, len);
|
||||
}
|
||||
size_t _currentClientWrite_P(PGM_P bytes, size_t len) override
|
||||
{
|
||||
return _currentClientSecure.write_P(bytes, len);
|
||||
}
|
||||
size_t _currentClientWrite (const char *bytes, size_t len) override { return _currentClientSecure.write((const uint8_t *)bytes, len); }
|
||||
size_t _currentClientWrite_P (PGM_P bytes, size_t len) override { return _currentClientSecure.write_P(bytes, len); }
|
||||
|
||||
protected:
|
||||
WiFiServerSecure _serverSecure;
|
||||
WiFiClientSecure _currentClientSecure;
|
||||
WiFiServerSecure _serverSecure;
|
||||
WiFiClientSecure _currentClientSecure;
|
||||
};
|
||||
|
||||
};
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -1,43 +1,16 @@
|
||||
#ifndef REQUESTHANDLER_H
|
||||
#define REQUESTHANDLER_H
|
||||
|
||||
class RequestHandler
|
||||
{
|
||||
class RequestHandler {
|
||||
public:
|
||||
virtual ~RequestHandler() { }
|
||||
virtual bool canHandle(HTTPMethod method, String uri)
|
||||
{
|
||||
(void) method;
|
||||
(void) uri;
|
||||
return false;
|
||||
}
|
||||
virtual bool canUpload(String uri)
|
||||
{
|
||||
(void) uri;
|
||||
return false;
|
||||
}
|
||||
virtual bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri)
|
||||
{
|
||||
(void) server;
|
||||
(void) requestMethod;
|
||||
(void) requestUri;
|
||||
return false;
|
||||
}
|
||||
virtual void upload(ESP8266WebServer& server, String requestUri, HTTPUpload& upload)
|
||||
{
|
||||
(void) server;
|
||||
(void) requestUri;
|
||||
(void) upload;
|
||||
}
|
||||
virtual bool canHandle(HTTPMethod method, String uri) { (void) method; (void) uri; return false; }
|
||||
virtual bool canUpload(String uri) { (void) uri; return false; }
|
||||
virtual bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) { (void) server; (void) requestMethod; (void) requestUri; return false; }
|
||||
virtual void upload(ESP8266WebServer& server, String requestUri, HTTPUpload& upload) { (void) server; (void) requestUri; (void) upload; }
|
||||
|
||||
RequestHandler* next()
|
||||
{
|
||||
return _next;
|
||||
}
|
||||
void next(RequestHandler* r)
|
||||
{
|
||||
_next = r;
|
||||
}
|
||||
RequestHandler* next() { return _next; }
|
||||
void next(RequestHandler* r) { _next = r; }
|
||||
|
||||
private:
|
||||
RequestHandler* _next = nullptr;
|
||||
|
@ -7,62 +7,47 @@
|
||||
|
||||
using namespace mime;
|
||||
|
||||
class FunctionRequestHandler : public RequestHandler
|
||||
{
|
||||
class FunctionRequestHandler : public RequestHandler {
|
||||
public:
|
||||
FunctionRequestHandler(ESP8266WebServer::THandlerFunction fn, ESP8266WebServer::THandlerFunction ufn, const String &uri, HTTPMethod method)
|
||||
: _fn(fn)
|
||||
, _ufn(ufn)
|
||||
, _uri(uri)
|
||||
, _method(method)
|
||||
: _fn(fn)
|
||||
, _ufn(ufn)
|
||||
, _uri(uri)
|
||||
, _method(method)
|
||||
{
|
||||
}
|
||||
|
||||
bool canHandle(HTTPMethod requestMethod, String requestUri) override
|
||||
{
|
||||
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
|
||||
if (_method != HTTP_ANY && _method != requestMethod)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (requestUri != _uri)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool canUpload(String requestUri) override
|
||||
{
|
||||
bool canUpload(String requestUri) override {
|
||||
if (!_ufn || !canHandle(HTTP_POST, requestUri))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override
|
||||
{
|
||||
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
|
||||
(void) server;
|
||||
if (!canHandle(requestMethod, requestUri))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
_fn();
|
||||
return true;
|
||||
}
|
||||
|
||||
void upload(ESP8266WebServer& server, String requestUri, HTTPUpload& upload) override
|
||||
{
|
||||
void upload(ESP8266WebServer& server, String requestUri, HTTPUpload& upload) override {
|
||||
(void) server;
|
||||
(void) upload;
|
||||
if (canUpload(requestUri))
|
||||
{
|
||||
_ufn();
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
@ -72,54 +57,42 @@ protected:
|
||||
HTTPMethod _method;
|
||||
};
|
||||
|
||||
class StaticRequestHandler : public RequestHandler
|
||||
{
|
||||
class StaticRequestHandler : public RequestHandler {
|
||||
public:
|
||||
StaticRequestHandler(FS& fs, const char* path, const char* uri, const char* cache_header)
|
||||
: _fs(fs)
|
||||
, _uri(uri)
|
||||
, _path(path)
|
||||
, _cache_header(cache_header)
|
||||
: _fs(fs)
|
||||
, _uri(uri)
|
||||
, _path(path)
|
||||
, _cache_header(cache_header)
|
||||
{
|
||||
_isFile = fs.exists(path);
|
||||
DEBUGV("StaticRequestHandler: path=%s uri=%s isFile=%d, cache_header=%s\r\n", path, uri, _isFile, cache_header);
|
||||
_baseUriLength = _uri.length();
|
||||
}
|
||||
|
||||
bool canHandle(HTTPMethod requestMethod, String requestUri) override
|
||||
{
|
||||
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
|
||||
if (requestMethod != HTTP_GET)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override
|
||||
{
|
||||
bool handle(ESP8266WebServer& server, HTTPMethod requestMethod, String requestUri) override {
|
||||
if (!canHandle(requestMethod, requestUri))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
DEBUGV("StaticRequestHandler::handle: request=%s _uri=%s\r\n", requestUri.c_str(), _uri.c_str());
|
||||
|
||||
String path(_path);
|
||||
|
||||
if (!_isFile)
|
||||
{
|
||||
if (!_isFile) {
|
||||
// Base URI doesn't point to a file.
|
||||
// If a directory is requested, look for index file.
|
||||
if (requestUri.endsWith("/"))
|
||||
{
|
||||
requestUri += "index.htm";
|
||||
}
|
||||
if (requestUri.endsWith("/"))
|
||||
requestUri += "index.htm";
|
||||
|
||||
// Append whatever follows this URI in request to get the file path.
|
||||
path += requestUri.substring(_baseUriLength);
|
||||
@ -130,45 +103,35 @@ public:
|
||||
|
||||
// look for gz file, only if the original specified path is not a gz. So part only works to send gzip via content encoding when a non compressed is asked for
|
||||
// if you point the the path to gzip you will serve the gzip as content type "application/x-gzip", not text or javascript etc...
|
||||
if (!path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !_fs.exists(path))
|
||||
{
|
||||
if (!path.endsWith(FPSTR(mimeTable[gz].endsWith)) && !_fs.exists(path)) {
|
||||
String pathWithGz = path + FPSTR(mimeTable[gz].endsWith);
|
||||
if (_fs.exists(pathWithGz))
|
||||
{
|
||||
if(_fs.exists(pathWithGz))
|
||||
path += FPSTR(mimeTable[gz].endsWith);
|
||||
}
|
||||
}
|
||||
|
||||
File f = _fs.open(path, "r");
|
||||
if (!f)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (_cache_header.length() != 0)
|
||||
{
|
||||
server.sendHeader("Cache-Control", _cache_header);
|
||||
}
|
||||
|
||||
server.streamFile(f, contentType);
|
||||
return true;
|
||||
}
|
||||
|
||||
static String getContentType(const String& path)
|
||||
{
|
||||
static String getContentType(const String& path) {
|
||||
char buff[sizeof(mimeTable[0].mimeType)];
|
||||
// Check all entries but last one for match, return if found
|
||||
for (size_t i = 0; i < sizeof(mimeTable) / sizeof(mimeTable[0]) - 1; i++)
|
||||
{
|
||||
for (size_t i=0; i < sizeof(mimeTable)/sizeof(mimeTable[0])-1; i++) {
|
||||
strcpy_P(buff, mimeTable[i].endsWith);
|
||||
if (path.endsWith(buff))
|
||||
{
|
||||
if (path.endsWith(buff)) {
|
||||
strcpy_P(buff, mimeTable[i].mimeType);
|
||||
return String(buff);
|
||||
}
|
||||
}
|
||||
// Fall-through and just return default type
|
||||
strcpy_P(buff, mimeTable[sizeof(mimeTable) / sizeof(mimeTable[0]) - 1].mimeType);
|
||||
strcpy_P(buff, mimeTable[sizeof(mimeTable)/sizeof(mimeTable[0])-1].mimeType);
|
||||
return String(buff);
|
||||
}
|
||||
|
||||
|
@ -5,7 +5,7 @@ namespace mime
|
||||
{
|
||||
|
||||
// Table of extension->MIME strings stored in PROGMEM, needs to be global due to GCC section typing rules
|
||||
const Entry mimeTable[maxType] PROGMEM =
|
||||
const Entry mimeTable[maxType] PROGMEM =
|
||||
{
|
||||
{ ".html", "text/html" },
|
||||
{ ".htm", "text/html" },
|
||||
@ -29,7 +29,7 @@ const Entry mimeTable[maxType] PROGMEM =
|
||||
{ ".zip", "application/zip" },
|
||||
{ ".gz", "application/x-gzip" },
|
||||
{ ".appcache", "text/cache-manifest" },
|
||||
{ "", "application/octet-stream" }
|
||||
{ "", "application/octet-stream" }
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -7,36 +7,36 @@ namespace mime
|
||||
|
||||
enum type
|
||||
{
|
||||
html,
|
||||
htm,
|
||||
css,
|
||||
txt,
|
||||
js,
|
||||
json,
|
||||
png,
|
||||
gif,
|
||||
jpg,
|
||||
ico,
|
||||
svg,
|
||||
ttf,
|
||||
otf,
|
||||
woff,
|
||||
woff2,
|
||||
eot,
|
||||
sfnt,
|
||||
xml,
|
||||
pdf,
|
||||
zip,
|
||||
gz,
|
||||
appcache,
|
||||
none,
|
||||
maxType
|
||||
html,
|
||||
htm,
|
||||
css,
|
||||
txt,
|
||||
js,
|
||||
json,
|
||||
png,
|
||||
gif,
|
||||
jpg,
|
||||
ico,
|
||||
svg,
|
||||
ttf,
|
||||
otf,
|
||||
woff,
|
||||
woff2,
|
||||
eot,
|
||||
sfnt,
|
||||
xml,
|
||||
pdf,
|
||||
zip,
|
||||
gz,
|
||||
appcache,
|
||||
none,
|
||||
maxType
|
||||
};
|
||||
|
||||
struct Entry
|
||||
{
|
||||
const char endsWith[16];
|
||||
const char mimeType[32];
|
||||
const char endsWith[16];
|
||||
const char mimeType[32];
|
||||
};
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user