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#include <WiFi.h>
30#include <Update.h>
31#include <WiFiUdp.h>
32
37
38#define HTTP_DOWNLOAD_UNIT_SIZE 1460
39#define HTTP_UPLOAD_BUFLEN 2048
40#define HTTP_MAX_DATA_WAIT 1000 //ms to wait for the client to send the request
41#define HTTP_MAX_POST_WAIT 1000 //ms to wait for POST data to arrive
42#define HTTP_MAX_SEND_WAIT 5000 //ms to wait for data chunk to be ACKed
43#define HTTP_MAX_CLOSE_WAIT 2000 //ms to wait for the client to close the connection
44
45#define CONTENT_LENGTH_UNKNOWN ((size_t) -1)
46#define CONTENT_LENGTH_NOT_SET ((size_t) -2)
47
48class WebServer;
49
50typedef struct {
52 String filename;
53 String name;
54 String type;
55 size_t totalSize; // file size
56 size_t currentSize; // size of data currently in buf
57 uint8_t buf[HTTP_UPLOAD_BUFLEN];
59
60#include "RequestHandler.h"
61
62namespace fs {
63class FS;
64}
65
67{
68public:
69 WebServer(IPAddress addr, int port = 80);
70 WebServer(int port = 80);
71 ~WebServer();
72
73 void begin();
74 void handleClient();
75
76 void close();
77 void stop();
78
79 bool authenticate(const char * username, const char * password);
81
82 typedef std::function<void(void)> THandlerFunction;
83 void on(const String &uri, THandlerFunction handler);
84 void on(const String &uri, HTTPMethod method, THandlerFunction fn);
85 void on(const String &uri, HTTPMethod method, THandlerFunction fn, THandlerFunction ufn);
86 void addHandler(RequestHandler* handler);
87 void serveStatic(const char* uri, fs::FS& fs, const char* path, const char* cache_header = NULL );
88 void onNotFound(THandlerFunction fn); //called when handler is not assigned
89 void onFileUpload(THandlerFunction fn); //handle file uploads
90
91 String uri() { return _currentUri; }
93 WiFiClient client() { return _currentClient; }
95
96 String arg(String name); // get request argument value by name
97 String arg(int i); // get request argument value by number
98 String argName(int i); // get request argument name by number
99 int args(); // get arguments count
100 bool hasArg(String name); // check if argument exists
101 void collectHeaders(const char* headerKeys[], const size_t headerKeysCount); // set the request headers to collect
102 String header(String name); // get request header value by name
103 String header(int i); // get request header value by number
104 String headerName(int i); // get request header name by number
105 int headers(); // get header count
106 bool hasHeader(String name); // check if header exists
107
108 String hostHeader(); // get request host header if available or empty String if not
109
110 // send response to the client
111 // code - HTTP response code, can be 200 or 404
112 // content_type - HTTP content type, like "text/plain" or "image/png"
113 // content - actual content body
114 void send(int code, const char* content_type = NULL, const String& content = String(""));
115 void send(int code, char* content_type, const String& content);
116 void send(int code, const String& content_type, const String& content);
117 void send_P(int code, PGM_P content_type, PGM_P content);
118 void send_P(int code, PGM_P content_type, PGM_P content, size_t contentLength);
119
120 void setContentLength(size_t contentLength);
121 void sendHeader(const String& name, const String& value, bool first = false);
122 void sendContent(const String& content);
123 void sendContent_P(PGM_P content);
124 void sendContent_P(PGM_P content, size_t size);
125
126 static String urlDecode(const String& text);
127
128template<typename T> size_t streamFile(T &file, const String& contentType){
129 setContentLength(file.size());
130 if (String(file.name()).endsWith(".gz") &&
131 contentType != "application/x-gzip" &&
132 contentType != "application/octet-stream"){
133 sendHeader("Content-Encoding", "gzip");
134 }
135 send(200, contentType, "");
136 return _currentClient.write(file);
137}
138
139protected:
140 void _addRequestHandler(RequestHandler* handler);
141 void _handleRequest();
142 bool _parseRequest(WiFiClient& client);
143 void _parseArguments(String data);
144 static String _responseCodeToString(int code);
145 bool _parseForm(WiFiClient& client, String boundary, uint32_t len);
147 void _uploadWriteByte(uint8_t b);
148 uint8_t _uploadReadByte(WiFiClient& client);
149 void _prepareHeader(String& response, int code, const char* content_type, size_t contentLength);
150 bool _collectHeader(const char* headerName, const char* headerValue);
151
153 String key;
154 String value;
155 };
156
157 WiFiServer _server;
158
159 WiFiClient _currentClient;
164 unsigned long _statusChange;
165
171
175
180
183
184};
185
186
187#endif //WebServer_H
HTTPMethod
Definition: WebServer.h:33
@ HTTP_PUT
Definition: WebServer.h:33
@ HTTP_ANY
Definition: WebServer.h:33
@ HTTP_DELETE
Definition: WebServer.h:33
@ HTTP_GET
Definition: WebServer.h:33
@ HTTP_OPTIONS
Definition: WebServer.h:33
@ HTTP_POST
Definition: WebServer.h:33
@ HTTP_PATCH
Definition: WebServer.h:33
HTTPClientStatus
Definition: WebServer.h:36
@ HC_WAIT_CLOSE
Definition: WebServer.h:36
@ HC_NONE
Definition: WebServer.h:36
@ HC_WAIT_READ
Definition: WebServer.h:36
HTTPUploadStatus
Definition: WebServer.h:34
@ UPLOAD_FILE_START
Definition: WebServer.h:34
@ UPLOAD_FILE_END
Definition: WebServer.h:34
@ UPLOAD_FILE_ABORTED
Definition: WebServer.h:35
@ UPLOAD_FILE_WRITE
Definition: WebServer.h:34
#define HTTP_UPLOAD_BUFLEN
Definition: WebServer.h:39
String headerName(int i)
Definition: WebServer.cpp:426
int args()
Definition: WebServer.cpp:388
WiFiClient client()
Definition: WebServer.h:93
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:166
size_t _contentLength
Definition: WebServer.h:178
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:173
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:160
HTTPClientStatus _currentStatus
Definition: WebServer.h:163
String _currentUri
Definition: WebServer.h:161
String _responseHeaders
Definition: WebServer.h:179
WiFiClient _currentClient
Definition: WebServer.h:159
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:128
void addHandler(RequestHandler *handler)
Definition: WebServer.cpp:150
void handleClient()
Definition: WebServer.cpp:169
WiFiServer _server
Definition: WebServer.h:157
RequestHandler * _firstHandler
Definition: WebServer.h:167
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:164
bool _chunked
Definition: WebServer.h:182
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:162
int _headerKeysCount
Definition: WebServer.h:176
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:170
THandlerFunction _notFoundHandler
Definition: WebServer.h:169
RequestHandler * _lastHandler
Definition: WebServer.h:168
RequestArgument * _currentHeaders
Definition: WebServer.h:177
bool _collectHeader(const char *headerName, const char *headerValue)
Definition: Parsing.cpp:259
HTTPUpload _currentUpload
Definition: WebServer.h:174
void close()
Definition: WebServer.cpp:233
int _currentArgCount
Definition: WebServer.h:172
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:181
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:92
void _uploadWriteByte(uint8_t b)
Definition: Parsing.cpp:344
HTTPUpload & upload()
Definition: WebServer.h:94
std::function< void(void)> THandlerFunction
Definition: WebServer.h:82
String uri()
Definition: WebServer.h:91
Definition: WebServer.h:62
size_t currentSize
Definition: WebServer.h:56
String type
Definition: WebServer.h:54
String name
Definition: WebServer.h:53
HTTPUploadStatus status
Definition: WebServer.h:51
size_t totalSize
Definition: WebServer.h:55
String filename
Definition: WebServer.h:52