Skip to content

Commit c218d94

Browse files
engyczd-a-v
authored andcommitted
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
1 parent 82789d2 commit c218d94

File tree

1 file changed

+58
-56
lines changed

1 file changed

+58
-56
lines changed

libraries/DNSServer/examples/CaptivePortalAdvanced/handleHttp.ino

+58-56
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,21 @@ void handleRoot() {
66
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
77
server.sendHeader("Pragma", "no-cache");
88
server.sendHeader("Expires", "-1");
9-
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
10-
server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
11-
server.sendContent(
12-
"<html><head></head><body>"
13-
"<h1>HELLO WORLD!!</h1>"
14-
);
9+
10+
String Page;
11+
Page += F(
12+
"<html><head></head><body>"
13+
"<h1>HELLO WORLD!!</h1>");
1514
if (server.client().localIP() == apIP) {
16-
server.sendContent(String("<p>You are connected through the soft AP: ") + softAP_ssid + "</p>");
15+
Page += String(F("<p>You are connected through the soft AP: ")) + softAP_ssid + F("</p>");
1716
} else {
18-
server.sendContent(String("<p>You are connected through the wifi network: ") + ssid + "</p>");
17+
Page += String(F("<p>You are connected through the wifi network: ")) + ssid + F("</p>");
1918
}
20-
server.sendContent(
21-
"<p>You may want to <a href='/wifi'>config the wifi connection</a>.</p>"
22-
"</body></html>"
23-
);
24-
server.client().stop(); // Stop is needed because we sent no content length
19+
Page += F(
20+
"<p>You may want to <a href='/wifi'>config the wifi connection</a>.</p>"
21+
"</body></html>");
22+
23+
server.send(200, "text/html", Page);
2524
}
2625

2726
/** 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() {
4140
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
4241
server.sendHeader("Pragma", "no-cache");
4342
server.sendHeader("Expires", "-1");
44-
server.setContentLength(CONTENT_LENGTH_UNKNOWN);
45-
server.send(200, "text/html", ""); // Empty content inhibits Content-length header so we have to close the socket ourselves.
46-
server.sendContent(
47-
"<html><head></head><body>"
48-
"<h1>Wifi config</h1>"
49-
);
43+
44+
String Page;
45+
Page += F(
46+
"<html><head></head><body>"
47+
"<h1>Wifi config</h1>");
5048
if (server.client().localIP() == apIP) {
51-
server.sendContent(String("<p>You are connected through the soft AP: ") + softAP_ssid + "</p>");
49+
Page += String(F("<p>You are connected through the soft AP: ")) + softAP_ssid + F("</p>");
5250
} else {
53-
server.sendContent(String("<p>You are connected through the wifi network: ") + ssid + "</p>");
51+
Page += String(F("<p>You are connected through the wifi network: ")) + ssid + F("</p>");
5452
}
55-
server.sendContent(
56-
"\r\n<br />"
57-
"<table><tr><th align='left'>SoftAP config</th></tr>"
58-
);
59-
server.sendContent(String() + "<tr><td>SSID " + String(softAP_ssid) + "</td></tr>");
60-
server.sendContent(String() + "<tr><td>IP " + toStringIp(WiFi.softAPIP()) + "</td></tr>");
61-
server.sendContent(
62-
"</table>"
63-
"\r\n<br />"
64-
"<table><tr><th align='left'>WLAN config</th></tr>"
65-
);
66-
server.sendContent(String() + "<tr><td>SSID " + String(ssid) + "</td></tr>");
67-
server.sendContent(String() + "<tr><td>IP " + toStringIp(WiFi.localIP()) + "</td></tr>");
68-
server.sendContent(
69-
"</table>"
70-
"\r\n<br />"
71-
"<table><tr><th align='left'>WLAN list (refresh if any missing)</th></tr>"
72-
);
53+
Page +=
54+
String(F(
55+
"\r\n<br />"
56+
"<table><tr><th align='left'>SoftAP config</th></tr>"
57+
"<tr><td>SSID ")) +
58+
String(softAP_ssid) +
59+
F("</td></tr>"
60+
"<tr><td>IP ") +
61+
toStringIp(WiFi.softAPIP()) +
62+
F("</td></tr>"
63+
"</table>"
64+
"\r\n<br />"
65+
"<table><tr><th align='left'>WLAN config</th></tr>"
66+
"<tr><td>SSID ") +
67+
String(ssid) +
68+
F("</td></tr>"
69+
"<tr><td>IP ") +
70+
toStringIp(WiFi.localIP()) +
71+
F("</td></tr>"
72+
"</table>"
73+
"\r\n<br />"
74+
"<table><tr><th align='left'>WLAN list (refresh if any missing)</th></tr>");
7375
Serial.println("scan start");
7476
int n = WiFi.scanNetworks();
7577
Serial.println("scan done");
7678
if (n > 0) {
7779
for (int i = 0; i < n; i++) {
78-
server.sendContent(String() + "\r\n<tr><td>SSID " + WiFi.SSID(i) + String((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : " *") + " (" + WiFi.RSSI(i) + ")</td></tr>");
80+
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>");
7981
}
8082
} else {
81-
server.sendContent(String() + "<tr><td>No WLAN found</td></tr>");
83+
Page += F("<tr><td>No WLAN found</td></tr>");
8284
}
83-
server.sendContent(
84-
"</table>"
85-
"\r\n<br /><form method='POST' action='wifisave'><h4>Connect to network:</h4>"
86-
"<input type='text' placeholder='network' name='n'/>"
87-
"<br /><input type='password' placeholder='password' name='p'/>"
88-
"<br /><input type='submit' value='Connect/Disconnect'/></form>"
89-
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
90-
"</body></html>"
91-
);
85+
Page += F(
86+
"</table>"
87+
"\r\n<br /><form method='POST' action='wifisave'><h4>Connect to network:</h4>"
88+
"<input type='text' placeholder='network' name='n'/>"
89+
"<br /><input type='password' placeholder='password' name='p'/>"
90+
"<br /><input type='submit' value='Connect/Disconnect'/></form>"
91+
"<p>You may want to <a href='/'>return to the home page</a>.</p>"
92+
"</body></html>");
93+
server.send(200, "text/html", Page);
9294
server.client().stop(); // Stop is needed because we sent no content length
9395
}
9496

@@ -111,17 +113,17 @@ void handleNotFound() {
111113
if (captivePortal()) { // If caprive portal redirect instead of displaying the error page.
112114
return;
113115
}
114-
String message = "File Not Found\n\n";
115-
message += "URI: ";
116+
String message = F("File Not Found\n\n");
117+
message += F("URI: ");
116118
message += server.uri();
117-
message += "\nMethod: ";
119+
message += F("\nMethod: ");
118120
message += (server.method() == HTTP_GET) ? "GET" : "POST";
119-
message += "\nArguments: ";
121+
message += F("\nArguments: ");
120122
message += server.args();
121-
message += "\n";
123+
message += F("\n");
122124

123125
for (uint8_t i = 0; i < server.args(); i++) {
124-
message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
126+
message += String(F(" ")) + server.argName(i) + F(": ") + server.arg(i) + F("\n");
125127
}
126128
server.sendHeader("Cache-Control", "no-cache, no-store, must-revalidate");
127129
server.sendHeader("Pragma", "no-cache");

0 commit comments

Comments
 (0)