Skip to content

Commit 5d15850

Browse files
committed
Add Initial WebServer
1 parent 5ca87f0 commit 5d15850

30 files changed

+3733
-3
lines changed

CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ set(LIBRARY_SRCS
5353
libraries/SPI/src/SPI.cpp
5454
libraries/Ticker/src/Ticker.cpp
5555
libraries/Update/src/Updater.cpp
56+
libraries/WebServer/src/WebServer.cpp
57+
libraries/WebServer/src/Parsing.cpp
58+
libraries/WebServer/src/detail/mimetable.cpp
5659
libraries/WiFiClientSecure/src/ssl_client.cpp
5760
libraries/WiFiClientSecure/src/WiFiClientSecure.cpp
5861
libraries/WiFi/src/ETH.cpp
@@ -182,6 +185,7 @@ set(COMPONENT_ADD_INCLUDEDIRS
182185
libraries/SPI/src
183186
libraries/Ticker/src
184187
libraries/Update/src
188+
libraries/WebServer/src
185189
libraries/WiFiClientSecure/src
186190
libraries/WiFi/src
187191
libraries/Wire/src

cores/esp32/Server.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
class Server: public Print
2626
{
2727
public:
28-
virtual void begin() =0;
28+
virtual void begin(uint16_t port=0) =0;
2929
};
3030

3131
#endif

cores/esp32/WString.cpp

+28
Original file line numberDiff line numberDiff line change
@@ -881,3 +881,31 @@ float String::toFloat(void) const
881881
}
882882
return 0;
883883
}
884+
885+
886+
unsigned char String::equalsConstantTime(const String &s2) const {
887+
// To avoid possible time-based attacks present function
888+
// compares given strings in a constant time.
889+
if(len != s2.len)
890+
return 0;
891+
//at this point lengths are the same
892+
if(len == 0)
893+
return 1;
894+
//at this point lenghts are the same and non-zero
895+
const char *p1 = buffer;
896+
const char *p2 = s2.buffer;
897+
unsigned int equalchars = 0;
898+
unsigned int diffchars = 0;
899+
while(*p1) {
900+
if(*p1 == *p2)
901+
++equalchars;
902+
else
903+
++diffchars;
904+
++p1;
905+
++p2;
906+
}
907+
//the following should force a constant time eval of the condition without a compiler "logical shortcut"
908+
unsigned char equalcond = (equalchars == len);
909+
unsigned char diffcond = (diffchars == 0);
910+
return (equalcond & diffcond); //bitwise AND
911+
}

cores/esp32/WString.h

+1
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ class String
212212
unsigned char operator <=(const String &rhs) const;
213213
unsigned char operator >=(const String &rhs) const;
214214
unsigned char equalsIgnoreCase(const String &s) const;
215+
unsigned char equalsConstantTime(const String &s) const;
215216
unsigned char startsWith(const String &prefix) const;
216217
unsigned char startsWith(const String &prefix, unsigned int offset) const;
217218
unsigned char endsWith(const String &suffix) const;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
/*
2+
Copyright (c) 2015, Majenko Technologies
3+
All rights reserved.
4+
5+
Redistribution and use in source and binary forms, with or without modification,
6+
are permitted provided that the following conditions are met:
7+
8+
* * Redistributions of source code must retain the above copyright notice, this
9+
list of conditions and the following disclaimer.
10+
11+
* * Redistributions in binary form must reproduce the above copyright notice, this
12+
list of conditions and the following disclaimer in the documentation and/or
13+
other materials provided with the distribution.
14+
15+
* * Neither the name of Majenko Technologies nor the names of its
16+
contributors may be used to endorse or promote products derived from
17+
this software without specific prior written permission.
18+
19+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
20+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
21+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
23+
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
26+
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29+
*/
30+
31+
#include <WiFi.h>
32+
#include <WiFiClient.h>
33+
#include <WebServer.h>
34+
#include <ESPmDNS.h>
35+
36+
const char *ssid = "YourSSIDHere";
37+
const char *password = "YourPSKHere";
38+
39+
WebServer server(80);
40+
41+
const int led = 13;
42+
43+
void handleRoot() {
44+
digitalWrite(led, 1);
45+
char temp[400];
46+
int sec = millis() / 1000;
47+
int min = sec / 60;
48+
int hr = min / 60;
49+
50+
snprintf(temp, 400,
51+
52+
"<html>\
53+
<head>\
54+
<meta http-equiv='refresh' content='5'/>\
55+
<title>ESP32 Demo</title>\
56+
<style>\
57+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
58+
</style>\
59+
</head>\
60+
<body>\
61+
<h1>Hello from ESP32!</h1>\
62+
<p>Uptime: %02d:%02d:%02d</p>\
63+
<img src=\"/test.svg\" />\
64+
</body>\
65+
</html>",
66+
67+
hr, min % 60, sec % 60
68+
);
69+
server.send(200, "text/html", temp);
70+
digitalWrite(led, 0);
71+
}
72+
73+
void handleNotFound() {
74+
digitalWrite(led, 1);
75+
String message = "File Not Found\n\n";
76+
message += "URI: ";
77+
message += server.uri();
78+
message += "\nMethod: ";
79+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
80+
message += "\nArguments: ";
81+
message += server.args();
82+
message += "\n";
83+
84+
for (uint8_t i = 0; i < server.args(); i++) {
85+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
86+
}
87+
88+
server.send(404, "text/plain", message);
89+
digitalWrite(led, 0);
90+
}
91+
92+
void setup(void) {
93+
pinMode(led, OUTPUT);
94+
digitalWrite(led, 0);
95+
Serial.begin(115200);
96+
WiFi.mode(WIFI_STA);
97+
WiFi.begin(ssid, password);
98+
Serial.println("");
99+
100+
// Wait for connection
101+
while (WiFi.status() != WL_CONNECTED) {
102+
delay(500);
103+
Serial.print(".");
104+
}
105+
106+
Serial.println("");
107+
Serial.print("Connected to ");
108+
Serial.println(ssid);
109+
Serial.print("IP address: ");
110+
Serial.println(WiFi.localIP());
111+
112+
if (MDNS.begin("esp32")) {
113+
Serial.println("MDNS responder started");
114+
}
115+
116+
server.on("/", handleRoot);
117+
server.on("/test.svg", drawGraph);
118+
server.on("/inline", []() {
119+
server.send(200, "text/plain", "this works as well");
120+
});
121+
server.onNotFound(handleNotFound);
122+
server.begin();
123+
Serial.println("HTTP server started");
124+
}
125+
126+
void loop(void) {
127+
server.handleClient();
128+
}
129+
130+
void drawGraph() {
131+
String out = "";
132+
char temp[100];
133+
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
134+
out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
135+
out += "<g stroke=\"black\">\n";
136+
int y = rand() % 130;
137+
for (int x = 10; x < 390; x += 10) {
138+
int y2 = rand() % 130;
139+
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
140+
out += temp;
141+
y = y2;
142+
}
143+
out += "</g>\n</svg>\n";
144+
145+
server.send(200, "image/svg+xml", out);
146+
}

0 commit comments

Comments
 (0)