Skip to content

Commit ae9fa9f

Browse files
Make WebServer::getServer/client return ref
1 parent 4d7f2b9 commit ae9fa9f

File tree

6 files changed

+244
-15
lines changed

6 files changed

+244
-15
lines changed

libraries/DNSServer/examples/CaptivePortalAdvanced/handleHttp.ino

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ void handleRoot() {
1212
"<meta name='viewport' content='width=device-width'>"
1313
"<title>CaptivePortal</title></head><body>"
1414
"<h1>HELLO WORLD!!</h1>");
15-
if (server.client()->localIP() == apIP) {
15+
if (server.client().localIP() == apIP) {
1616
Page += String(F("<p>You are connected through the soft AP: ")) + softAP_ssid + F("</p>");
1717
} else {
1818
Page += String(F("<p>You are connected through the wifi network: ")) + ssid + F("</p>");
@@ -27,9 +27,9 @@ void handleRoot() {
2727
boolean captivePortal() {
2828
if (!isIp(server.hostHeader()) && server.hostHeader() != (String(myHostname) + ".local")) {
2929
Serial.println("Request redirected to captive portal");
30-
server.sendHeader("Location", String("http://") + toStringIp(server.client()->localIP()), true);
30+
server.sendHeader("Location", String("http://") + toStringIp(server.client().localIP()), true);
3131
server.send(302, "text/plain", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
32-
server.client()->stop(); // Stop is needed because we sent no content length
32+
server.client().stop(); // Stop is needed because we sent no content length
3333
return true;
3434
}
3535
return false;
@@ -46,7 +46,7 @@ void handleWifi() {
4646
"<meta name='viewport' content='width=device-width'>"
4747
"<title>CaptivePortal</title></head><body>"
4848
"<h1>Wifi config</h1>");
49-
if (server.client()->localIP() == apIP) {
49+
if (server.client().localIP() == apIP) {
5050
Page += String(F("<p>You are connected through the soft AP: ")) + softAP_ssid + F("</p>");
5151
} else {
5252
Page += String(F("<p>You are connected through the wifi network: ")) + ssid + F("</p>");
@@ -83,7 +83,7 @@ void handleWifi() {
8383
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
8484
"</body></html>");
8585
server.send(200, "text/html", Page);
86-
server.client()->stop(); // Stop is needed because we sent no content length
86+
server.client().stop(); // Stop is needed because we sent no content length
8787
}
8888

8989
/** Handle the WLAN save form and redirect to WLAN config page again */
@@ -96,7 +96,7 @@ void handleWifiSave() {
9696
server.sendHeader("Pragma", "no-cache");
9797
server.sendHeader("Expires", "-1");
9898
server.send(302, "text/plain", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
99-
server.client()->stop(); // Stop is needed because we sent no content length
99+
server.client().stop(); // Stop is needed because we sent no content length
100100
saveCredentials();
101101
connect = strlen(ssid) > 0; // Request WLAN connect with new credentials if there is a SSID
102102
}
Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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 <LEAmDNS.h>
35+
36+
#ifndef STASSID
37+
#define STASSID "your-ssid"
38+
#define STAPSK "your-password"
39+
#endif
40+
41+
const char *ssid = STASSID;
42+
const char *password = STAPSK;
43+
44+
WebServer server(80);
45+
46+
const int led = 13;
47+
48+
void handleRoot() {
49+
digitalWrite(led, 1);
50+
char temp[400];
51+
int sec = millis() / 1000;
52+
int min = sec / 60;
53+
int hr = min / 60;
54+
55+
snprintf(temp, 400,
56+
57+
"<html>\
58+
<head>\
59+
<meta http-equiv='refresh' content='5'/>\
60+
<title>Pico-W Demo</title>\
61+
<style>\
62+
body { background-color: #cccccc; font-family: Arial, Helvetica, Sans-Serif; Color: #000088; }\
63+
</style>\
64+
</head>\
65+
<body>\
66+
<h1>Hello from the Pico W!</h1>\
67+
<p>Uptime: %02d:%02d:%02d</p>\
68+
<img src=\"/test.svg\" />\
69+
</body>\
70+
</html>",
71+
72+
hr, min % 60, sec % 60);
73+
server.send(200, "text/html", temp);
74+
digitalWrite(led, 0);
75+
}
76+
77+
void handleNotFound() {
78+
digitalWrite(led, 1);
79+
String message = "File Not Found\n\n";
80+
message += "URI: ";
81+
message += server.uri();
82+
message += "\nMethod: ";
83+
message += (server.method() == HTTP_GET) ? "GET" : "POST";
84+
message += "\nArguments: ";
85+
message += server.args();
86+
message += "\n";
87+
88+
for (uint8_t i = 0; i < server.args(); i++) {
89+
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
90+
}
91+
92+
server.send(404, "text/plain", message);
93+
digitalWrite(led, 0);
94+
}
95+
96+
void drawGraph() {
97+
String out;
98+
out.reserve(2600);
99+
char temp[70];
100+
out += "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"400\" height=\"150\">\n";
101+
out += "<rect width=\"400\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"1\" stroke=\"rgb(0, 0, 0)\" />\n";
102+
out += "<g stroke=\"black\">\n";
103+
int y = rand() % 130;
104+
for (int x = 10; x < 390; x += 10) {
105+
int y2 = rand() % 130;
106+
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"1\" />\n", x, 140 - y, x + 10, 140 - y2);
107+
out += temp;
108+
y = y2;
109+
}
110+
out += "</g>\n</svg>\n";
111+
112+
server.send(200, "image/svg+xml", out);
113+
}
114+
115+
void setup(void) {
116+
pinMode(led, OUTPUT);
117+
digitalWrite(led, 0);
118+
Serial.begin(115200);
119+
WiFi.mode(WIFI_STA);
120+
WiFi.begin(ssid, password);
121+
Serial.println("");
122+
123+
// Wait for connection
124+
while (WiFi.status() != WL_CONNECTED) {
125+
delay(500);
126+
Serial.print(".");
127+
}
128+
129+
Serial.println("");
130+
Serial.print("Connected to ");
131+
Serial.println(ssid);
132+
Serial.print("IP address: ");
133+
Serial.println(WiFi.localIP());
134+
135+
if (MDNS.begin("picow")) {
136+
Serial.println("MDNS responder started");
137+
}
138+
139+
server.on("/", handleRoot);
140+
server.on("/test.svg", drawGraph);
141+
server.on("/inline", []() {
142+
server.send(200, "text/plain", "this works as well");
143+
});
144+
server.onNotFound(handleNotFound);
145+
server.begin();
146+
Serial.println("HTTP server started");
147+
}
148+
149+
void loop(void) {
150+
server.handleClient();
151+
MDNS.update();
152+
}

libraries/WebServer/examples/HelloServerBearSSL/HelloServerBearSSL.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,10 @@ void setup(void) {
125125

126126
if (MDNS.begin("picow")) { Serial.println("MDNS responder started"); }
127127

128-
server.getServer()->setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
128+
server.getServer().setRSACert(new BearSSL::X509List(serverCert), new BearSSL::PrivateKey(serverKey));
129129

130130
// Cache SSL sessions to accelerate the TLS handshake.
131-
server.getServer()->setCache(&serverCache);
131+
server.getServer().setCache(&serverCache);
132132

133133
server.on("/", handleRoot);
134134

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
To upload through terminal you can use: curl -F "image=@firmware.bin" esp8266-webupdate.local/update
3+
*/
4+
5+
#include <ESP8266WiFi.h>
6+
#include <WiFiClient.h>
7+
#include <ESP8266WebServer.h>
8+
#include <ESP8266mDNS.h>
9+
10+
#ifndef STASSID
11+
#define STASSID "your-ssid"
12+
#define STAPSK "your-password"
13+
#endif
14+
15+
const char* host = "esp8266-webupdate";
16+
const char* ssid = STASSID;
17+
const char* password = STAPSK;
18+
19+
ESP8266WebServer server(80);
20+
const char* serverIndex = "<form method='POST' action='/update' enctype='multipart/form-data'><input type='file' name='update'><input type='submit' value='Update'></form>";
21+
22+
void setup(void) {
23+
Serial.begin(115200);
24+
Serial.println();
25+
Serial.println("Booting Sketch...");
26+
WiFi.mode(WIFI_AP_STA);
27+
WiFi.begin(ssid, password);
28+
if (WiFi.waitForConnectResult() == WL_CONNECTED) {
29+
MDNS.begin(host);
30+
server.on("/", HTTP_GET, []() {
31+
server.sendHeader("Connection", "close");
32+
server.send(200, "text/html", serverIndex);
33+
});
34+
server.on(
35+
"/update", HTTP_POST, []() {
36+
server.sendHeader("Connection", "close");
37+
server.send(200, "text/plain", (Update.hasError()) ? "FAIL" : "OK");
38+
ESP.restart();
39+
},
40+
[]() {
41+
HTTPUpload& upload = server.upload();
42+
if (upload.status == UPLOAD_FILE_START) {
43+
Serial.setDebugOutput(true);
44+
WiFiUDP::stopAll();
45+
Serial.printf("Update: %s\n", upload.filename.c_str());
46+
uint32_t maxSketchSpace = (ESP.getFreeSketchSpace() - 0x1000) & 0xFFFFF000;
47+
if (!Update.begin(maxSketchSpace)) { // start with max available size
48+
Update.printError(Serial);
49+
}
50+
} else if (upload.status == UPLOAD_FILE_WRITE) {
51+
if (Update.write(upload.buf, upload.currentSize) != upload.currentSize) {
52+
Update.printError(Serial);
53+
}
54+
} else if (upload.status == UPLOAD_FILE_END) {
55+
if (Update.end(true)) { // true to set the size to the current progress
56+
Serial.printf("Update Success: %u\nRebooting...\n", upload.totalSize);
57+
} else {
58+
Update.printError(Serial);
59+
}
60+
Serial.setDebugOutput(false);
61+
}
62+
yield();
63+
});
64+
server.begin();
65+
MDNS.addService("http", "tcp", 80);
66+
67+
Serial.printf("Ready! Open http://%s.local in your browser\n", host);
68+
} else {
69+
Serial.println("WiFi Failed");
70+
}
71+
}
72+
73+
void loop(void) {
74+
server.handleClient();
75+
MDNS.update();
76+
}

libraries/WebServer/src/HTTPServer.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,6 @@ class HTTPServer {
102102
HTTPMethod method() {
103103
return _currentMethod;
104104
}
105-
// virtual WiFiClient client() { return _currentClient; }
106-
virtual WiFiClient *client() {
107-
return _currentClient;
108-
}
109105
HTTPUpload& upload() {
110106
return *_currentUpload;
111107
}

libraries/WebServer/src/WebServerTemplate.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,18 @@ class WebServerTemplate : public HTTPServer {
4141

4242
virtual void close();
4343
virtual void stop();
44-
ServerType *getServer() {
45-
return &_server;
44+
45+
ServerType &getServer() {
46+
return _server;
47+
}
48+
49+
ClientType& client() {
50+
// _currentClient is always a WiFiClient*, so we need to coerce to the proper type for SSL
51+
return *(ClientType*)_currentClient;
4652
}
4753

4854
private:
4955
ServerType _server;
50-
ClientType _curClient;
5156
};
5257

5358
template <typename ServerType, int DefaultPort>

0 commit comments

Comments
 (0)