mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-28 05:01:28 +03:00
Convert ESP8266WebServer* into templatized model (#5982)
* Convert ESP8266WebServer* into templatized model Supercedes #4912 Refactor the three versions of ESP8266WebServer and *WebServerSecure to a single templated class. Use "using" to enable old, non-templated names to b used (so no user changes required to compile or run). Fixes #4908 and clean up the code base a lot. Basic tests run (the ones in the example code). No code changes are required in userland except for setting the SSL certificates which now use a cleaner "getServer()" accessor and lets the app use the native BearSSL calls on the WiFiClientSecure object. @devyte should be proud, it removes virtuals and even has template specialization... * Fix HTTPUpdate templates and examples * Fix HTTPUpdateServer library build Need to remove dot-a linkage since there are no .cpp files in the directory anymore due to templates. * Provide backward-compat names for updt template Allow existing code to use the same well known names for HTTPUpdateSecure. * Remove ClientType from all templates, auto-infer Remove the ClientType template parameter from all objects. Simplifies the code and makes it more foolproof. Add a "using" in each server to define the type of connection returned by all servers, which is then used in the above templates automatically. * Can safely include FS.h now that SD/SPIFFS unified * Move the templates/objects to their own namespaces * Fix merge issues with untemplated methods * Address review comments * Fix mock test, remove warnings inside test dir Make the simple mock test CI job pass and clean up any spurious warnings in the test directory. There still are warnings in the libraries and core, but they should be addressed in a separate PR.
This commit is contained in:
committed by
david gauchard
parent
93a52f923b
commit
7036297920
@ -27,6 +27,8 @@
|
||||
#include <functional>
|
||||
#include <memory>
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <FS.h>
|
||||
#include "detail/mimetable.h"
|
||||
|
||||
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,
|
||||
@ -48,8 +50,6 @@ enum HTTPAuthMethod { BASIC_AUTH, DIGEST_AUTH };
|
||||
#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
|
||||
#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
|
||||
|
||||
class ESP8266WebServer;
|
||||
|
||||
typedef struct {
|
||||
HTTPUploadStatus status;
|
||||
String filename;
|
||||
@ -61,24 +61,29 @@ typedef struct {
|
||||
uint8_t buf[HTTP_UPLOAD_BUFLEN];
|
||||
} HTTPUpload;
|
||||
|
||||
namespace esp8266webserver {
|
||||
|
||||
template<typename ServerType>
|
||||
class ESP8266WebServerTemplate;
|
||||
|
||||
#include "detail/RequestHandler.h"
|
||||
|
||||
namespace fs {
|
||||
class FS;
|
||||
}
|
||||
|
||||
class ESP8266WebServer
|
||||
template<typename ServerType>
|
||||
class ESP8266WebServerTemplate
|
||||
{
|
||||
public:
|
||||
ESP8266WebServer(IPAddress addr, int port = 80);
|
||||
ESP8266WebServer(int port = 80);
|
||||
virtual ~ESP8266WebServer();
|
||||
ESP8266WebServerTemplate(IPAddress addr, int port = 80);
|
||||
ESP8266WebServerTemplate(int port = 80);
|
||||
~ESP8266WebServerTemplate();
|
||||
|
||||
virtual void begin();
|
||||
virtual void begin(uint16_t port);
|
||||
virtual void handleClient();
|
||||
using ClientType = typename ServerType::ClientType;
|
||||
using RequestHandlerType = RequestHandler<ServerType>;
|
||||
using WebServerType = ESP8266WebServerTemplate<ServerType>;
|
||||
|
||||
virtual void close();
|
||||
void begin();
|
||||
void begin(uint16_t port);
|
||||
void handleClient();
|
||||
void close();
|
||||
void stop();
|
||||
|
||||
bool authenticate(const char * username, const char * password);
|
||||
@ -89,16 +94,19 @@ public:
|
||||
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 addHandler(RequestHandlerType* 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; }
|
||||
ClientType client() { return _currentClient; }
|
||||
HTTPUpload& upload() { return *_currentUpload; }
|
||||
|
||||
// Allows setting server options (i.e. SSL keys) by the instantiator
|
||||
ServerType &getServer() { return _server; }
|
||||
|
||||
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
|
||||
@ -141,18 +149,16 @@ public:
|
||||
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 _addRequestHandler(RequestHandlerType* handler);
|
||||
void _handleRequest();
|
||||
void _finalizeResponse();
|
||||
bool _parseRequest(WiFiClient& client);
|
||||
bool _parseRequest(ClientType& 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 _parseForm(ClientType& client, const String& boundary, uint32_t len);
|
||||
bool _parseFormUploadAborted();
|
||||
void _uploadWriteByte(uint8_t b);
|
||||
uint8_t _uploadReadByte(WiFiClient& client);
|
||||
uint8_t _uploadReadByte(ClientType& client);
|
||||
void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
|
||||
bool _collectHeader(const char* headerName, const char* headerValue);
|
||||
|
||||
@ -167,18 +173,17 @@ protected:
|
||||
String value;
|
||||
};
|
||||
|
||||
WiFiServer _server;
|
||||
|
||||
WiFiClient _currentClient;
|
||||
ServerType _server;
|
||||
ClientType _currentClient;
|
||||
HTTPMethod _currentMethod;
|
||||
String _currentUri;
|
||||
uint8_t _currentVersion;
|
||||
HTTPClientStatus _currentStatus;
|
||||
unsigned long _statusChange;
|
||||
|
||||
RequestHandler* _currentHandler;
|
||||
RequestHandler* _firstHandler;
|
||||
RequestHandler* _lastHandler;
|
||||
RequestHandlerType* _currentHandler;
|
||||
RequestHandlerType* _firstHandler;
|
||||
RequestHandlerType* _lastHandler;
|
||||
THandlerFunction _notFoundHandler;
|
||||
THandlerFunction _fileUploadHandler;
|
||||
|
||||
@ -204,4 +209,13 @@ protected:
|
||||
};
|
||||
|
||||
|
||||
#include "ESP8266WebServer-impl.h"
|
||||
#include "Parsing-impl.h"
|
||||
|
||||
};
|
||||
|
||||
|
||||
using ESP8266WebServer = esp8266webserver::ESP8266WebServerTemplate<WiFiServer>;
|
||||
|
||||
|
||||
#endif //ESP8266WEBSERVER_H
|
||||
|
Reference in New Issue
Block a user