26#include "../../Defines.h"
27#ifdef USE_LIB_WEBSERVER
31#ifdef USE_REST_MESSAGING
32#include <WiFiClientSecure.h>
39enum HTTPMethod { HTTP_ANY, HTTP_GET, HTTP_POST, HTTP_PUT, HTTP_PATCH, HTTP_DELETE, HTTP_OPTIONS };
40enum HTTPUploadStatus { UPLOAD_FILE_START, UPLOAD_FILE_WRITE, UPLOAD_FILE_END,
41 UPLOAD_FILE_ABORTED };
42enum HTTPClientStatus { HC_NONE, HC_WAIT_READ, HC_WAIT_CLOSE };
44#define HTTP_DOWNLOAD_UNIT_SIZE 1460
45#define HTTP_UPLOAD_BUFLEN 2048
46#define HTTP_MAX_DATA_WAIT 1000
47#define HTTP_MAX_POST_WAIT 1000
48#define HTTP_MAX_SEND_WAIT 5000
49#define HTTP_MAX_CLOSE_WAIT 2000
51#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
52#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
57 HTTPUploadStatus status;
63 uint8_t buf[HTTP_UPLOAD_BUFLEN];
75 WebServer(IPAddress addr,
int port = 80);
76 WebServer(
int port = 80);
85 bool authenticate(
const char * username,
const char * password);
86 void requestAuthentication();
88 typedef std::function<void(
void)> THandlerFunction;
89 void on(
const String &uri, THandlerFunction handler);
90 void on(
const String &uri, HTTPMethod method, THandlerFunction fn);
91 void on(
const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
92 void addHandler(RequestHandler* handler);
93 void serveStatic(
const char* uri, fs::FS& fs,
const char* path,
const char* cache_header = NULL );
94 void onNotFound(THandlerFunction fn);
95 void onFileUpload(THandlerFunction fn);
97 String uri() {
return _currentUri; }
98 HTTPMethod method() {
return _currentMethod; }
99 WiFiClient client() {
return _currentClient; }
100 HTTPUpload& upload() {
return _currentUpload; }
102 String arg(String name);
104 String argName(
int i);
106 bool hasArg(String name);
107 void collectHeaders(
const char* headerKeys[],
const size_t headerKeysCount);
108 String header(String name);
109 String header(
int i);
110 String headerName(
int i);
112 bool hasHeader(String name);
120 void send(
int code,
const char* content_type = NULL,
const String& content = String(
""));
121 void send(
int code,
char* content_type,
const String& content);
122 void send(
int code,
const String& content_type,
const String& content);
123 void send_P(
int code, PGM_P content_type, PGM_P content);
124 void send_P(
int code, PGM_P content_type, PGM_P content,
size_t contentLength);
126 void setContentLength(
size_t contentLength);
127 void sendHeader(
const String& name,
const String& value,
bool first =
false);
128 void sendContent(
const String& content);
129 void sendContent_P(PGM_P content);
130 void sendContent_P(PGM_P content,
size_t size);
132 static String urlDecode(
const String& text);
134template<
typename T>
size_t streamFile(T &file,
const String& contentType){
135 setContentLength(file.size());
136 if (String(file.name()).endsWith(
".gz") &&
137 contentType !=
"application/x-gzip" &&
138 contentType !=
"application/octet-stream"){
139 sendHeader(
"Content-Encoding",
"gzip");
141 send(200, contentType,
"");
142 return _currentClient.write(file);
146 void _addRequestHandler(RequestHandler* handler);
147 void _handleRequest();
148 bool _parseRequest(WiFiClient& client);
149 void _parseArguments(String data);
150 static String _responseCodeToString(
int code);
151 bool _parseForm(WiFiClient& client, String boundary, uint32_t len);
152 bool _parseFormUploadAborted();
153 void _uploadWriteByte(uint8_t b);
154 uint8_t _uploadReadByte(WiFiClient& client);
155 void _prepareHeader(String& response,
int code,
const char* content_type,
size_t contentLength);
156 bool _collectHeader(
const char* headerName,
const char* headerValue);
158 struct RequestArgument {
165 WiFiClient _currentClient;
166 HTTPMethod _currentMethod;
168 uint8_t _currentVersion;
169 HTTPClientStatus _currentStatus;
170 unsigned long _statusChange;
172 RequestHandler* _currentHandler;
173 RequestHandler* _firstHandler;
174 RequestHandler* _lastHandler;
175 THandlerFunction _notFoundHandler;
176 THandlerFunction _fileUploadHandler;
178 int _currentArgCount;
179 RequestArgument* _currentArgs;
180 HTTPUpload _currentUpload;
182 int _headerKeysCount;
183 RequestArgument* _currentHeaders;
185 String _responseHeaders;