Skip to content

Commit 877d440

Browse files
authored
Add url redirect (#8970)
* added getAvailableVersion(), moved _httpClientTimeout and _followRedirects to protected, added enum HTTPUpdateError * auto numbering of HTTPUpdateError enum * added getAvailableVersion(), debug output current current Sketch MD5 * Revert "added getAvailableVersion(), debug output current current Sketch MD5" This reverts commit 60d2c77. * Revert "auto numbering of HTTPUpdateError enum" This reverts commit 61785b2. * Revert "added getAvailableVersion(), moved _httpClientTimeout and _followRedirects to protected, added enum HTTPUpdateError" This reverts commit cec84ed. * add redirect function * enhanced redirect() by cache control and client stop * updated redirect() comment * replaced redirect() API calls in examples * server.client().stop() not needed, redirect() does this
1 parent 2bb1b5a commit 877d440

File tree

4 files changed

+28
-14
lines changed

4 files changed

+28
-14
lines changed

libraries/DNSServer/examples/CaptivePortalAdvanced/handleHttp.ino

+2-9
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@ 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);
31-
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
30+
server.redirect(String("http://") + toStringIp(server.client().localIP()));
3331
return true;
3432
}
3533
return false;
@@ -91,12 +89,7 @@ void handleWifiSave() {
9189
Serial.println("wifi save");
9290
server.arg("n").toCharArray(ssid, sizeof(ssid) - 1);
9391
server.arg("p").toCharArray(password, sizeof(password) - 1);
94-
server.sendHeader("Location", "wifi", true);
95-
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
96-
server.sendHeader("Pragma", "no-cache");
97-
server.sendHeader("Expires", "-1");
98-
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
92+
server.redirect("wifi");
10093
saveCredentials();
10194
connect = strlen(ssid) > 0; // Request WLAN connect with new credentials if there is a SSID
10295
}

libraries/DNSServer/examples/NAPTCaptivePortal/PortalRedirectHttp.ino

+1-3
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,11 @@ void sendPortalRedirect(String path, String targetName) {
3636
If the "Location" header element works the HTML stuff is never seen.
3737
*/
3838
// https://tools.ietf.org/html/rfc7231#section-6.4.3
39-
server.sendHeader("Location", path, true);
40-
addNoCacheHeader();
4139
String reply = FPSTR(portalRedirectHTML);
4240
reply.reserve(reply.length() + 2 * path.length() + 80);
4341
reply.replace("{t}", targetName);
4442
reply.replace("{1}", path);
45-
server.send(302, "text/html", reply);
43+
server.redirect(path, reply);
4644
}
4745

4846
#endif // LWIP_FEATURES && !LWIP_IPV6

libraries/ESP8266WebServer/examples/WebServer/WebServer.ino

+1-2
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ void handleRedirect() {
4343

4444
if (!LittleFS.exists(url)) { url = "/$update.htm"; }
4545

46-
server.sendHeader("Location", url, true);
47-
server.send(302);
46+
server.redirect(url);
4847
} // handleRedirect()
4948

5049

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+24
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,30 @@ class ESP8266WebServerTemplate
207207
sendContent(emptyString);
208208
}
209209

210+
/**
211+
* @brief Redirect to another URL, e.g.
212+
* webserver.on("/index.html", HTTP_GET, []() { webserver.redirect("/"); });
213+
* There are 3 points of redirection here:
214+
* 1) "Location" element in the header
215+
* 2) Disable client caching
216+
* 3) HTML "content" element to redirect
217+
* If the "Location" header element works the HTML content is never seen.
218+
* https://tools.ietf.org/html/rfc7231#section-6.4.3
219+
* @param url URL to redirect to
220+
* @param content Optional redirect content
221+
*/
222+
void redirect(const String& url, const String& content = emptyString) {
223+
sendHeader(F("Location"), url, true);
224+
sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
225+
sendHeader(F("Pragma"), F("no-cache"));
226+
sendHeader(F("Expires"), F("-1"));
227+
send(302, F("text/html"), content); // send 302: "Found"
228+
if (content.isEmpty()) {
229+
// Empty content inhibits Content-length header so we have to close the socket ourselves.
230+
client().stop(); // Stop is needed because we sent no content length
231+
}
232+
}
233+
210234
// Whether other requests should be accepted from the client on the
211235
// same socket after a response is sent.
212236
// This will automatically configure the "Connection" header of the response.

0 commit comments

Comments
 (0)