ESP_IOT v2.5
IOT ESP Coding
WebServer.h
Go to the documentation of this file.
1/*
2 WebServer.h - Dead simple web-server.
3 Supports only one simultaneous client, knows how to handle GET and POST.
4
5 Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or (at your option) any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
21*/
22
23
24#ifndef WEBSERVER_H
25#define WEBSERVER_H
26#include "../../Defines.h"
27
28#include <functional>
29#ifdef USE_REST_MESSAGING
30#include <WiFiClientSecure.h>
31#else
32#include <WiFi.h>
33#endif
34#include <Update.h>
35#include <WiFiUdp.h>
36
41
42#define HTTP_DOWNLOAD_UNIT_SIZE 1460
43#define HTTP_UPLOAD_BUFLEN 2048
44#define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request
45#define HTTP_MAX_POST_WAIT 1000 //ms to wait for POST data to arrive
46#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed
47#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
48
49#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
50#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
51
52class WebServer;
53
54typedef struct {
56 String filename;
57 String name;
58 String type;
59 size_t totalSize; // file size
60 size_t currentSize; // size of data currently in buf
61 uint8_t buf[HTTP_UPLOAD_BUFLEN];
63
64#include "RequestHandler.h"
65
66namespace fs {
67class FS;
68}
69
71{
72public:
73 WebServer(IPAddress addr, int port = 80);
74 WebServer(int port = 80);
75 ~WebServer();
76
77 void begin();
78 void handleClient();
79
80 void close();
81 void stop();
82
83 bool authenticate(const char * username, const char * password);
85
86 typedef std::function<void(void)> THandlerFunction;
87 void on(const String &uri, THandlerFunction handler);
88 void on(const String &uri, HTTPMethod method, THandlerFunction fn);
89 void on(const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
90 void addHandler(RequestHandler* handler);
91 void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
92 void onNotFound(THandlerFunction fn); //called when handler is not assigned
93 void onFileUpload(THandlerFunction fn); //handle file uploads
94
95 String uri() { return _currentUri; }
97 WiFiClient client() { return _currentClient; }
99
100 String arg(String name); // get request argument value by name
101 String arg(int i); // get request argument value by number
102 String argName(int i); // get request argument name by number
103 int args(); // get arguments count
104 bool hasArg(String name); // check if argument exists
105 void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
106 String header(String name); // get request header value by name
107 String header(int i); // get request header value by number
108 String headerName(int i); // get request header name by number
109 int headers(); // get header count
110 bool hasHeader(String name); // check if header exists
111
112 String hostHeader(); // get request host header if available or empty String if not
113
114 // send response to the client
115 // code - HTTP response code, can be 200 or 404
116 // content_type - HTTP content type, like "text/plain" or "image/png"
117 // content - actual content body
118 void send(int code, const char* content_type = NULL, const String& content = String(""));
119 void send(int code, char* content_type, const String& content);
120 void send(int code, const String& content_type, const String& content);
121 void send_P(int code, PGM_P content_type, PGM_P content);
122 void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
123
124 void setContentLength(size_t contentLength);
125 void sendHeader(const String& name, const String& value, bool first = false);
126 void sendContent(const String& content);
127 void sendContent_P(PGM_P content);
128 void sendContent_P(PGM_P content, size_t size);
129
130 static String urlDecode(const String& text);
131
132template<typename T> size_t streamFile(T &file, const String& contentType){
133 setContentLength(file.size());
134 if (String(file.name()).endsWith(".gz") &&
135 contentType != "application/x-gzip" &&
136 contentType != "application/octet-stream"){
137 sendHeader("Content-Encoding", "gzip");
138 }
139 send(200, contentType, "");
140 return _currentClient.write(file);
141}
142
143protected:
144 void _addRequestHandler(RequestHandler* handler);
145 void _handleRequest();
146 bool _parseRequest(WiFiClient& client);
147 void _parseArguments(String data);
148 static String _responseCodeToString(int code);
149 bool _parseForm(WiFiClient& client, String boundary, uint32_t len);
151 void _uploadWriteByte(uint8_t b);
152 uint8_t _uploadReadByte(WiFiClient& client);
153 void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
154 bool _collectHeader(const char* headerName, const char* headerValue);
155
157 String key;
158 String value;
159 };
160
161 WiFiServer _server;
162
163 WiFiClient _currentClient;
168 unsigned long _statusChange;
169
175
179
184
187
188};
189
190
191#endif //WebServer_H
HTTPMethod
Definition: WebServer.h:37
@ HTTP_PUT
Definition: WebServer.h:37
@ HTTP_ANY
Definition: WebServer.h:37
@ HTTP_DELETE
Definition: WebServer.h:37
@ HTTP_GET
Definition: WebServer.h:37
@ HTTP_OPTIONS
Definition: WebServer.h:37
@ HTTP_POST
Definition: WebServer.h:37
@ HTTP_PATCH
Definition: WebServer.h:37
HTTPClientStatus
Definition: WebServer.h:40
@ HC_WAIT_CLOSE
Definition: WebServer.h:40
@ HC_NONE
Definition: WebServer.h:40
@ HC_WAIT_READ
Definition: WebServer.h:40
HTTPUploadStatus
Definition: WebServer.h:38
@ UPLOAD_FILE_START
Definition: WebServer.h:38
@ UPLOAD_FILE_END
Definition: WebServer.h:38
@ UPLOAD_FILE_ABORTED
Definition: WebServer.h:39
@ UPLOAD_FILE_WRITE
Definition: WebServer.h:38
#define HTTP_UPLOAD_BUFLEN
Definition: WebServer.h:43
String headerName(int i)
Definition: WebServer.cpp:426
int args()
Definition: WebServer.cpp:388
WiFiClient client()
Definition: WebServer.h:97
bool authenticate(const char *username, const char *password)
Definition: WebServer.cpp:100
static String urlDecode(const String &text)
Definition: Parsing.cpp:572
bool hasHeader(String name)
Definition: WebServer.cpp:436
int headers()
Definition: WebServer.cpp:432
void _prepareHeader(String &response, int code, const char *content_type, size_t contentLength)
Definition: WebServer.cpp:259
RequestHandler * _currentHandler
Definition: WebServer.h:170
size_t _contentLength
Definition: WebServer.h:182
void on(const String &uri, THandlerFunction handler)
Definition: WebServer.cpp:138
void begin()
Definition: WebServer.cpp:93
void _handleRequest()
Definition: WebServer.cpp:456
RequestArgument * _currentArgs
Definition: WebServer.h:177
static String _responseCodeToString(int code)
Definition: WebServer.cpp:484
void sendContent(const String &content)
Definition: WebServer.cpp:330
void _parseArguments(String data)
Definition: Parsing.cpp:269
void requestAuthentication()
Definition: WebServer.cpp:133
String hostHeader()
Definition: WebServer.cpp:444
HTTPMethod _currentMethod
Definition: WebServer.h:164
HTTPClientStatus _currentStatus
Definition: WebServer.h:167
String _currentUri
Definition: WebServer.h:165
String _responseHeaders
Definition: WebServer.h:183
WiFiClient _currentClient
Definition: WebServer.h:163
String argName(int i)
Definition: WebServer.cpp:382
void _addRequestHandler(RequestHandler *handler)
Definition: WebServer.cpp:154
size_t streamFile(T &file, const String &contentType)
Definition: WebServer.h:132
void addHandler(RequestHandler *handler)
Definition: WebServer.cpp:150
void handleClient()
Definition: WebServer.cpp:169
WiFiServer _server
Definition: WebServer.h:161
RequestHandler * _firstHandler
Definition: WebServer.h:171
void collectHeaders(const char *headerKeys[], const size_t headerKeysCount)
Definition: WebServer.cpp:409
void send(int code, const char *content_type=NULL, const String &content=String(""))
Definition: WebServer.cpp:287
unsigned long _statusChange
Definition: WebServer.h:168
bool _chunked
Definition: WebServer.h:186
String header(String name)
Definition: WebServer.cpp:401
void sendHeader(const String &name, const String &value, bool first=false)
Definition: WebServer.cpp:241
void onNotFound(THandlerFunction fn)
Definition: WebServer.cpp:452
WebServer(IPAddress addr, int port=80)
Definition: WebServer.cpp:44
String arg(String name)
Definition: WebServer.cpp:368
bool hasArg(String name)
Definition: WebServer.cpp:392
uint8_t _currentVersion
Definition: WebServer.h:166
int _headerKeysCount
Definition: WebServer.h:180
bool _parseRequest(WiFiClient &client)
Definition: Parsing.cpp:67
void stop()
Definition: WebServer.cpp:237
void setContentLength(size_t contentLength)
Definition: WebServer.cpp:255
THandlerFunction _fileUploadHandler
Definition: WebServer.h:174
THandlerFunction _notFoundHandler
Definition: WebServer.h:173
RequestHandler * _lastHandler
Definition: WebServer.h:172
RequestArgument * _currentHeaders
Definition: WebServer.h:181
bool _collectHeader(const char *headerName, const char *headerValue)
Definition: Parsing.cpp:259
HTTPUpload _currentUpload
Definition: WebServer.h:178
void close()
Definition: WebServer.cpp:233
int _currentArgCount
Definition: WebServer.h:176
void onFileUpload(THandlerFunction fn)
Definition: WebServer.cpp:448
void send_P(int code, PGM_P content_type, PGM_P content)
Definition: WebServer.cpp:298
void sendContent_P(PGM_P content)
Definition: WebServer.cpp:347
bool _parseFormUploadAborted()
Definition: Parsing.cpp:603
void serveStatic(const char *uri, fs::FS &fs, const char *path, const char *cache_header=NULL)
Definition: WebServer.cpp:165
String _hostHeader
Definition: WebServer.h:185
bool _parseForm(WiFiClient &client, String boundary, uint32_t len)
Definition: Parsing.cpp:364
uint8_t _uploadReadByte(WiFiClient &client)
Definition: Parsing.cpp:354
HTTPMethod method()
Definition: WebServer.h:96
void _uploadWriteByte(uint8_t b)
Definition: Parsing.cpp:344
HTTPUpload & upload()
Definition: WebServer.h:98
std::function< void(void)> THandlerFunction
Definition: WebServer.h:86
String uri()
Definition: WebServer.h:95
Definition: WebServer.h:66
size_t currentSize
Definition: WebServer.h:60
String type
Definition: WebServer.h:58
String name
Definition: WebServer.h:57
HTTPUploadStatus status
Definition: WebServer.h:55
size_t totalSize
Definition: WebServer.h:59
String filename
Definition: WebServer.h:56