1
0
mirror of https://github.com/esp8266/Arduino.git synced 2025-04-19 23:22:16 +03:00

CaptivePortalAdvanced: Fix compatibility with Android (#5069)

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
This commit is contained in:
Jiří Engelthaler 2019-02-05 13:28:37 +01:00 committed by david gauchard
parent 82789d201c
commit c218d945a4

View File

@ -6,22 +6,21 @@ void handleRoot() {
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate"); server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache"); server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1"); 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. String Page;
server.sendContent( Page += F(
"<html><head></head><body>" "<html><head></head><body>"
"<h1>HELLO WORLD!!</h1>" "<h1>HELLO WORLD!!</h1>");
);
if (server.client().localIP() == apIP) { if (server.client().localIP() == apIP) {
server.sendContent(String("<p>You are connected through the soft AP: ") + softAP_ssid + "</p>"); Page += String(F("<p>You are connected through the soft AP: ")) + softAP_ssid + F("</p>");
} else { } else {
server.sendContent(String("<p>You are connected through the wifi network: ") + ssid + "</p>"); Page += String(F("<p>You are connected through the wifi network: ")) + ssid + F("</p>");
} }
server.sendContent( Page += F(
"<p>You may want to <a href='/wifi'>config the wifi connection</a>.</p>" "<p>You may want to <a href='/wifi'>config the wifi connection</a>.</p>"
"</body></html>" "</body></html>");
);
server.client().stop(); // Stop is needed because we sent no content length 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. */ /** 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("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache"); server.sendHeader("Pragma", "no-cache");
server.sendHeader("Expires", "-1"); 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. String Page;
server.sendContent( Page += F(
"<html><head></head><body>" "<html><head></head><body>"
"<h1>Wifi config</h1>" "<h1>Wifi config</h1>");
);
if (server.client().localIP() == apIP) { if (server.client().localIP() == apIP) {
server.sendContent(String("<p>You are connected through the soft AP: ") + softAP_ssid + "</p>"); Page += String(F("<p>You are connected through the soft AP: ")) + softAP_ssid + F("</p>");
} else { } else {
server.sendContent(String("<p>You are connected through the wifi network: ") + ssid + "</p>"); Page += String(F("<p>You are connected through the wifi network: ")) + ssid + F("</p>");
} }
server.sendContent( Page +=
"\r\n<br />" String(F(
"<table><tr><th align='left'>SoftAP config</th></tr>" "\r\n<br />"
); "<table><tr><th align='left'>SoftAP config</th></tr>"
server.sendContent(String() + "<tr><td>SSID " + String(softAP_ssid) + "</td></tr>"); "<tr><td>SSID ")) +
server.sendContent(String() + "<tr><td>IP " + toStringIp(WiFi.softAPIP()) + "</td></tr>"); String(softAP_ssid) +
server.sendContent( F("</td></tr>"
"</table>" "<tr><td>IP ") +
"\r\n<br />" toStringIp(WiFi.softAPIP()) +
"<table><tr><th align='left'>WLAN config</th></tr>" F("</td></tr>"
); "</table>"
server.sendContent(String() + "<tr><td>SSID " + String(ssid) + "</td></tr>"); "\r\n<br />"
server.sendContent(String() + "<tr><td>IP " + toStringIp(WiFi.localIP()) + "</td></tr>"); "<table><tr><th align='left'>WLAN config</th></tr>"
server.sendContent( "<tr><td>SSID ") +
"</table>" String(ssid) +
"\r\n<br />" F("</td></tr>"
"<table><tr><th align='left'>WLAN list (refresh if any missing)</th></tr>" "<tr><td>IP ") +
); toStringIp(WiFi.localIP()) +
F("</td></tr>"
"</table>"
"\r\n<br />"
"<table><tr><th align='left'>WLAN list (refresh if any missing)</th></tr>");
Serial.println("scan start"); Serial.println("scan start");
int n = WiFi.scanNetworks(); int n = WiFi.scanNetworks();
Serial.println("scan done"); Serial.println("scan done");
if (n > 0) { if (n > 0) {
for (int i = 0; i < n; i++) { for (int i = 0; i < n; i++) {
server.sendContent(String() + "\r\n<tr><td>SSID " + WiFi.SSID(i) + String((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : " *") + " (" + WiFi.RSSI(i) + ")</td></tr>"); Page += String(F("\r\n<tr><td>SSID ")) + WiFi.SSID(i) + ((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? F(" ") : F(" *")) + F(" (") + WiFi.RSSI(i) + F(")</td></tr>");
} }
} else { } else {
server.sendContent(String() + "<tr><td>No WLAN found</td></tr>"); Page += F("<tr><td>No WLAN found</td></tr>");
} }
server.sendContent( Page += F(
"</table>" "</table>"
"\r\n<br /><form method='POST' action='wifisave'><h4>Connect to network:</h4>" "\r\n<br /><form method='POST' action='wifisave'><h4>Connect to network:</h4>"
"<input type='text' placeholder='network' name='n'/>" "<input type='text' placeholder='network' name='n'/>"
"<br /><input type='password' placeholder='password' name='p'/>" "<br /><input type='password' placeholder='password' name='p'/>"
"<br /><input type='submit' value='Connect/Disconnect'/></form>" "<br /><input type='submit' value='Connect/Disconnect'/></form>"
"<p>You may want to <a href='/'>return to the home page</a>.</p>" "<p>You may want to <a href='/'>return to the home page</a>.</p>"
"</body></html>" "</body></html>");
); server.send(200, "text/html", Page);
server.client().stop(); // Stop is needed because we sent no content length 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. if (captivePortal()) { // If caprive portal redirect instead of displaying the error page.
return; return;
} }
String message = "File Not Found\n\n"; String message = F("File Not Found\n\n");
message += "URI: "; message += F("URI: ");
message += server.uri(); message += server.uri();
message += "\nMethod: "; message += F("\nMethod: ");
message += (server.method() == HTTP_GET) ? "GET" : "POST"; message += (server.method() == HTTP_GET) ? "GET" : "POST";
message += "\nArguments: "; message += F("\nArguments: ");
message += server.args(); message += server.args();
message += "\n"; message += F("\n");
for (uint8_t i = 0; i < server.args(); i++) { 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("Cache-Control", "no-cache, no-store, must-revalidate");
server.sendHeader("Pragma", "no-cache"); server.sendHeader("Pragma", "no-cache");