1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-06-13 13:01:55 +03:00

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 60d2c7762e.

* Revert "auto numbering of HTTPUpdateError enum"

This reverts commit 61785b27da.

* Revert "added getAvailableVersion(), moved _httpClientTimeout and _followRedirects to protected, added enum HTTPUpdateError"

This reverts commit cec84ed17a.

* 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
This commit is contained in:
Holger Müller
2024-03-24 23:52:28 +01:00
committed by GitHub
parent 2bb1b5a4d5
commit 877d44059e
4 changed files with 28 additions and 14 deletions

View File

@ -43,8 +43,7 @@ void handleRedirect() {
if (!LittleFS.exists(url)) { url = "/$update.htm"; }
server.sendHeader("Location", url, true);
server.send(302);
server.redirect(url);
} // handleRedirect()

View File

@ -207,6 +207,30 @@ public:
sendContent(emptyString);
}
/**
* @brief Redirect to another URL, e.g.
* webserver.on("/index.html", HTTP_GET, []() { webserver.redirect("/"); });
* There are 3 points of redirection here:
* 1) "Location" element in the header
* 2) Disable client caching
* 3) HTML "content" element to redirect
* If the "Location" header element works the HTML content is never seen.
* https://tools.ietf.org/html/rfc7231#section-6.4.3
* @param url URL to redirect to
* @param content Optional redirect content
*/
void redirect(const String& url, const String& content = emptyString) {
sendHeader(F("Location"), url, true);
sendHeader(F("Cache-Control"), F("no-cache, no-store, must-revalidate"));
sendHeader(F("Pragma"), F("no-cache"));
sendHeader(F("Expires"), F("-1"));
send(302, F("text/html"), content); // send 302: "Found"
if (content.isEmpty()) {
// Empty content inhibits Content-length header so we have to close the socket ourselves.
client().stop(); // Stop is needed because we sent no content length
}
}
// Whether other requests should be accepted from the client on the
// same socket after a response is sent.
// This will automatically configure the "Connection" header of the response.