-
Notifications
You must be signed in to change notification settings - Fork 164
/
Copy pathWebSrv.cpp
343 lines (259 loc) · 8.51 KB
/
WebSrv.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "ESPWebDAV.h"
// Sections are copied from ESP8266Webserver
// ------------------------
String ESPWebDAV::getMimeType(String path) {
// ------------------------
if(path.endsWith(".html")) return "text/html";
else if(path.endsWith(".htm")) return "text/html";
else if(path.endsWith(".css")) return "text/css";
else if(path.endsWith(".txt")) return "text/plain";
else if(path.endsWith(".js")) return "application/javascript";
else if(path.endsWith(".json")) return "application/json";
else if(path.endsWith(".png")) return "image/png";
else if(path.endsWith(".gif")) return "image/gif";
else if(path.endsWith(".jpg")) return "image/jpeg";
else if(path.endsWith(".ico")) return "image/x-icon";
else if(path.endsWith(".svg")) return "image/svg+xml";
else if(path.endsWith(".ttf")) return "application/x-font-ttf";
else if(path.endsWith(".otf")) return "application/x-font-opentype";
else if(path.endsWith(".woff")) return "application/font-woff";
else if(path.endsWith(".woff2")) return "application/font-woff2";
else if(path.endsWith(".eot")) return "application/vnd.ms-fontobject";
else if(path.endsWith(".sfnt")) return "application/font-sfnt";
else if(path.endsWith(".xml")) return "text/xml";
else if(path.endsWith(".pdf")) return "application/pdf";
else if(path.endsWith(".zip")) return "application/zip";
else if(path.endsWith(".gz")) return "application/x-gzip";
else if(path.endsWith(".appcache")) return "text/cache-manifest";
return "application/octet-stream";
}
// ------------------------
String ESPWebDAV::urlDecode(const String& text) {
// ------------------------
String decoded = "";
char temp[] = "0x00";
unsigned int len = text.length();
unsigned int i = 0;
while (i < len) {
char decodedChar;
char encodedChar = text.charAt(i++);
if ((encodedChar == '%') && (i + 1 < len)) {
temp[2] = text.charAt(i++);
temp[3] = text.charAt(i++);
decodedChar = strtol(temp, NULL, 16);
}
else {
if (encodedChar == '+')
decodedChar = ' ';
else
decodedChar = encodedChar; // normal ascii char
}
decoded += decodedChar;
}
return decoded;
}
// ------------------------
String ESPWebDAV::urlToUri(String url) {
// ------------------------
if(url.startsWith("http://")) {
int uriStart = url.indexOf('/', 7);
return url.substring(uriStart);
}
else
return url;
}
// ------------------------
bool ESPWebDAV::isClientWaiting() {
// ------------------------
return server->hasClient();
}
// ------------------------
void ESPWebDAV::handleClient(String blank) {
// ------------------------
processClient(&ESPWebDAV::handleRequest, blank);
}
// ------------------------
void ESPWebDAV::rejectClient(String rejectMessage) {
// ------------------------
processClient(&ESPWebDAV::handleReject, rejectMessage);
}
// ------------------------
void ESPWebDAV::processClient(THandlerFunction handler, String message) {
// ------------------------
// Check if a client has connected
client = server->available();
if(!client)
return;
// Wait until the client sends some data
while(!client.available())
delay(1);
// reset all variables
_chunked = false;
_responseHeaders = String();
_contentLength = CONTENT_LENGTH_NOT_SET;
method = String();
uri = String();
contentLengthHeader = String();
depthHeader = String();
hostHeader = String();
destinationHeader = String();
// extract uri, headers etc
if(parseRequest())
// invoke the handler
(this->*handler)(message);
// finalize the response
if(_chunked)
sendContent("");
// send all data before closing connection
client.flush();
// close the connection
client.stop();
}
// ------------------------
bool ESPWebDAV::parseRequest() {
// ------------------------
// Read the first line of HTTP request
String req = client.readStringUntil('\r');
client.readStringUntil('\n');
// First line of HTTP request looks like "GET /path HTTP/1.1"
// Retrieve the "/path" part by finding the spaces
int addr_start = req.indexOf(' ');
int addr_end = req.indexOf(' ', addr_start + 1);
if (addr_start == -1 || addr_end == -1) {
return false;
}
method = req.substring(0, addr_start);
uri = urlDecode(req.substring(addr_start + 1, addr_end));
// DBG_PRINT("method: "); DBG_PRINT(method); DBG_PRINT(" url: "); DBG_PRINTLN(uri);
// parse and finish all headers
String headerName;
String headerValue;
while(1) {
req = client.readStringUntil('\r');
client.readStringUntil('\n');
if(req == "")
// no more headers
break;
int headerDiv = req.indexOf(':');
if (headerDiv == -1)
break;
headerName = req.substring(0, headerDiv);
headerValue = req.substring(headerDiv + 2);
// DBG_PRINT("\t"); DBG_PRINT(headerName); DBG_PRINT(": "); DBG_PRINTLN(headerValue);
if(headerName.equalsIgnoreCase("Host"))
hostHeader = headerValue;
else if(headerName.equalsIgnoreCase("Depth"))
depthHeader = headerValue;
else if(headerName.equalsIgnoreCase("Content-Length"))
contentLengthHeader = headerValue;
else if(headerName.equalsIgnoreCase("Destination"))
destinationHeader = headerValue;
}
return true;
}
// ------------------------
void ESPWebDAV::sendHeader(const String& name, const String& value, bool first) {
// ------------------------
String headerLine = name + ": " + value + "\r\n";
if (first)
_responseHeaders = headerLine + _responseHeaders;
else
_responseHeaders += headerLine;
}
// ------------------------
void ESPWebDAV::send(String code, const char* content_type, const String& content) {
// ------------------------
String header;
_prepareHeader(header, code, content_type, content.length());
client.write(header.c_str(), header.length());
if(content.length())
sendContent(content);
}
// ------------------------
void ESPWebDAV::_prepareHeader(String& response, String code, const char* content_type, size_t contentLength) {
// ------------------------
response = "HTTP/1.1 " + code + "\r\n";
if(content_type)
sendHeader("Content-Type", content_type, true);
if(_contentLength == CONTENT_LENGTH_NOT_SET)
sendHeader("Content-Length", String(contentLength));
else if(_contentLength != CONTENT_LENGTH_UNKNOWN)
sendHeader("Content-Length", String(_contentLength));
else if(_contentLength == CONTENT_LENGTH_UNKNOWN) {
_chunked = true;
sendHeader("Accept-Ranges","none");
sendHeader("Transfer-Encoding","chunked");
}
sendHeader("Connection", "close");
response += _responseHeaders;
response += "\r\n";
}
// ------------------------
void ESPWebDAV::sendContent(const String& content) {
// ------------------------
const char * footer = "\r\n";
size_t size = content.length();
if(_chunked) {
char * chunkSize = (char *) malloc(11);
if(chunkSize) {
sprintf(chunkSize, "%x%s", size, footer);
client.write(chunkSize, strlen(chunkSize));
free(chunkSize);
}
}
client.write(content.c_str(), size);
if(_chunked) {
client.write(footer, 2);
if (size == 0) {
_chunked = false;
}
}
}
// ------------------------
void ESPWebDAV::sendContent_P(PGM_P content) {
// ------------------------
const char * footer = "\r\n";
size_t size = strlen_P(content);
if(_chunked) {
char * chunkSize = (char *) malloc(11);
if(chunkSize) {
sprintf(chunkSize, "%x%s", size, footer);
client.write(chunkSize, strlen(chunkSize));
free(chunkSize);
}
}
client.write_P(content, size);
if(_chunked) {
client.write(footer, 2);
if (size == 0) {
_chunked = false;
}
}
}
// ------------------------
void ESPWebDAV::setContentLength(size_t len) {
// ------------------------
_contentLength = len;
}
// ------------------------
size_t ESPWebDAV::readBytesWithTimeout(uint8_t *buf, size_t bufSize) {
// ------------------------
int timeout_ms = HTTP_MAX_POST_WAIT;
size_t numAvailable = 0;
while(!(numAvailable = client.available()) && client.connected() && timeout_ms--)
delay(1);
if(!numAvailable)
return 0;
return client.read(buf, bufSize);
}
// ------------------------
size_t ESPWebDAV::readBytesWithTimeout(uint8_t *buf, size_t bufSize, size_t numToRead) {
// ------------------------
int timeout_ms = HTTP_MAX_POST_WAIT;
size_t numAvailable = 0;
while(((numAvailable = client.available()) < numToRead) && client.connected() && timeout_ms--)
delay(1);
if(!numAvailable)
return 0;
return client.read(buf, bufSize);
}