From d84a8d404ffb31f00845bc19bbfa8f0dd3dd873d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Engelthaler?= Date: Sat, 4 Aug 2018 21:07:00 +0200 Subject: [PATCH] CaptivePortalAdvanced: Fix compatibility with Android Android refuses to show page with missing Content-Length header. Prepare page data to String and send it with server.send Moved respose strings to PROGMEM --- .../CaptivePortalAdvanced/handleHttp.ino | 114 +++++++++--------- 1 file changed, 58 insertions(+), 56 deletions(-) diff --git a/libraries/DNSServer/examples/CaptivePortalAdvanced/handleHttp.ino b/libraries/DNSServer/examples/CaptivePortalAdvanced/handleHttp.ino index b2f34fd572..1baac437d1 100644 --- a/libraries/DNSServer/examples/CaptivePortalAdvanced/handleHttp.ino +++ b/libraries/DNSServer/examples/CaptivePortalAdvanced/handleHttp.ino @@ -6,22 +6,21 @@ void handleRoot() { server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); - server.setContentLength(CONTENT_LENGTH_UNKNOWN); - server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. - server.sendContent( - "" - "

HELLO WORLD!!

" - ); + + String Page; + Page += F( + "" + "

HELLO WORLD!!

"); if (server.client().localIP() == apIP) { - server.sendContent(String("

You are connected through the soft AP: ") + softAP_ssid + "

"); + Page += String(F("

You are connected through the soft AP: ")) + softAP_ssid + F("

"); } else { - server.sendContent(String("

You are connected through the wifi network: ") + ssid + "

"); + Page += String(F("

You are connected through the wifi network: ")) + ssid + F("

"); } - server.sendContent( - "

You may want to config the wifi connection.

" - "" - ); - server.client().stop(); // Stop is needed because we sent no content length + Page += F( + "

You may want to config the wifi connection.

" + ""); + + server.send(200, "text/html", Page); } /** Redirect to captive portal if we got a request for another domain. Return true in that case so the page handler do not try to handle the request again. */ @@ -41,54 +40,57 @@ void handleWifi() { server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache"); server.sendHeader("Expires", "-1"); - server.setContentLength(CONTENT_LENGTH_UNKNOWN); - server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves. - server.sendContent( - "" - "

Wifi config

" - ); + + String Page; + Page += F( + "" + "

Wifi config

"); if (server.client().localIP() == apIP) { - server.sendContent(String("

You are connected through the soft AP: ") + softAP_ssid + "

"); + Page += String(F("

You are connected through the soft AP: ")) + softAP_ssid + F("

"); } else { - server.sendContent(String("

You are connected through the wifi network: ") + ssid + "

"); + Page += String(F("

You are connected through the wifi network: ")) + ssid + F("

"); } - server.sendContent( - "\r\n
" - "" - ); - server.sendContent(String() + ""); - server.sendContent(String() + ""); - server.sendContent( - "
SoftAP config
SSID " + String(softAP_ssid) + "
IP " + toStringIp(WiFi.softAPIP()) + "
" - "\r\n
" - "" - ); - server.sendContent(String() + ""); - server.sendContent(String() + ""); - server.sendContent( - "
WLAN config
SSID " + String(ssid) + "
IP " + toStringIp(WiFi.localIP()) + "
" - "\r\n
" - "" - ); + Page += + String(F( + "\r\n
" + "
WLAN list (refresh if any missing)
" + "" + "" + "
SoftAP config
SSID ")) + + String(softAP_ssid) + + F("
IP ") + + toStringIp(WiFi.softAPIP()) + + F("
" + "\r\n
" + "" + "" + "" + "
WLAN config
SSID ") + + String(ssid) + + F("
IP ") + + toStringIp(WiFi.localIP()) + + F("
" + "\r\n
" + ""); Serial.println("scan start"); int n = WiFi.scanNetworks(); Serial.println("scan done"); if (n > 0) { for (int i = 0; i < n; i++) { - server.sendContent(String() + "\r\n"); + Page += String(F("\r\n"); } } else { - server.sendContent(String() + ""); + Page += F(""); } - server.sendContent( - "
WLAN list (refresh if any missing)
SSID " + WiFi.SSID(i) + String((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : " *") + " (" + WiFi.RSSI(i) + ")
SSID ")) + WiFi.SSID(i) + ((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? F(" ") : F(" *")) + F(" (") + WiFi.RSSI(i) + F(")
No WLAN found
No WLAN found
" - "\r\n

Connect to network:

" - "" - "
" - "
" - "

You may want to return to the home page.

" - "" - ); + Page += F( + "" + "\r\n

Connect to network:

" + "" + "
" + "
" + "

You may want to return to the home page.

" + ""); + server.send(200, "text/html", Page); server.client().stop(); // Stop is needed because we sent no content length } @@ -111,17 +113,17 @@ void handleNotFound() { if (captivePortal()) { // If caprive portal redirect instead of displaying the error page. return; } - String message = "File Not Found\n\n"; - message += "URI: "; + String message = F("File Not Found\n\n"); + message += F("URI: "); message += server.uri(); - message += "\nMethod: "; + message += F("\nMethod: "); message += (server.method() == HTTP_GET) ? "GET" : "POST"; - message += "\nArguments: "; + message += F("\nArguments: "); message += server.args(); - message += "\n"; + message += F("\n"); for (uint8_t i = 0; i < server.args(); i++) { - message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; + message += String(F(" ")) + server.argName(i) + F(": ") + server.arg(i) + F("\n"); } server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Pragma", "no-cache");