forked from ardyesp/ESPWebDAV
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathWebSrv.cpp
136 lines (115 loc) · 4.63 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
/*
ESP8266WebServer.h - Dead simple web-server.
Supports only one simultaneous client, knows how to handle GET and POST.
Copyright (c) 2014 Ivan Grokhotkov. All rights reserved.
Simplified/Adapted for ESPWebDav:
Copyright (c) 2018 Gurpreet Bal https://github.com/ardyesp/ESPWebDAV
Copyright (c) 2020 David Gauchard https://github.com/d-a-v/ESPWebDAV
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Modified 8 May 2015 by Hristo Gochkov (proper post and file upload handling)
*/
#include "ESPWebDAV.h"
// Sections are copied from ESP8266Webserver
String ESPWebDAV::getMimeType(const 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;
}
void ESPWebDAV::handleClient()
{
if (!server)
return;
if (server->hasClient())
{
if (!locClient || !locClient.available())
{
// no or sleeping current client
// take it over
locClient = server->accept();
m_persistent_timer_ms = millis();
DBG_PRINT("NEW CLIENT-------------------------------------------------------");
}
}
if (!locClient || !locClient.available())
return;
// extract uri, headers etc
parseRequest();
if (!m_persistent)
// close the connection
locClient.stop();
}
bool ESPWebDAV::parseRequest()
{
// Read the first line of HTTP request
String req = locClient.readStringUntil('\r');
locClient.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));
return ESPWebDAVCore::parseRequest(method, uri, &locClient, getMimeType);
}