mirror of
https://github.com/esp8266/Arduino.git
synced 2025-06-12 01:53:07 +03:00
Chunked encoding (#2199)
* Add chunked encoding example: ```cpp server.on("/chunked", HTTP_GET, [](){ server.send(200, "text/html", String()); server.sendContent("<!DOCTYPE html><html><head><title>Chunked Test</title></head><body>"); server.sendContent("<p>Chunk 1</p>"); server.sendContent("<p>Chunk 2</p>"); server.sendContent("<p>Chunk 3</p>"); server.sendContent("<p>Chunk 4</p>"); server.sendContent("<p>Chunk 5</p>"); server.sendContent("<p>Chunk 6</p>"); server.sendContent("</html>"); server.sendContent("");//end of chunked }); ``` * update examples, keep setContentLength and add bool _chunked * fix wrong session id * set _chunked to false earlier for cases where users use only sendContent
This commit is contained in:
committed by
Ivan Grokhotkov
parent
6d3109e8c7
commit
6390cf6bd6
@ -10,7 +10,7 @@ ESP8266WebServer server(80);
|
||||
//Check if header is present and correct
|
||||
bool is_authentified(){
|
||||
Serial.println("Enter is_authentified");
|
||||
if (server.hasHeader("Cookie")){
|
||||
if (server.hasHeader("Cookie")){
|
||||
Serial.print("Found cookie: ");
|
||||
String cookie = server.header("Cookie");
|
||||
Serial.println(cookie);
|
||||
@ -20,27 +20,31 @@ bool is_authentified(){
|
||||
}
|
||||
}
|
||||
Serial.println("Authentification Failed");
|
||||
return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
//login page, also called for disconnect
|
||||
void handleLogin(){
|
||||
String msg;
|
||||
if (server.hasHeader("Cookie")){
|
||||
if (server.hasHeader("Cookie")){
|
||||
Serial.print("Found cookie: ");
|
||||
String cookie = server.header("Cookie");
|
||||
Serial.println(cookie);
|
||||
}
|
||||
if (server.hasArg("DISCONNECT")){
|
||||
Serial.println("Disconnection");
|
||||
String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=0\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
|
||||
server.sendContent(header);
|
||||
server.sendHeader("Location","/login");
|
||||
server.sendHeader("Cache-Control","no-cache");
|
||||
server.sendHeader("Set-Cookie","ESPSESSIONID=0");
|
||||
server.send(301);
|
||||
return;
|
||||
}
|
||||
if (server.hasArg("USERNAME") && server.hasArg("PASSWORD")){
|
||||
if (server.arg("USERNAME") == "admin" && server.arg("PASSWORD") == "admin" ){
|
||||
String header = "HTTP/1.1 301 OK\r\nSet-Cookie: ESPSESSIONID=1\r\nLocation: /\r\nCache-Control: no-cache\r\n\r\n";
|
||||
server.sendContent(header);
|
||||
server.sendHeader("Location","/");
|
||||
server.sendHeader("Cache-Control","no-cache");
|
||||
server.sendHeader("Set-Cookie","ESPSESSIONID=1");
|
||||
server.send(301);
|
||||
Serial.println("Log in Successful");
|
||||
return;
|
||||
}
|
||||
@ -60,8 +64,9 @@ void handleRoot(){
|
||||
Serial.println("Enter handleRoot");
|
||||
String header;
|
||||
if (!is_authentified()){
|
||||
String header = "HTTP/1.1 301 OK\r\nLocation: /login\r\nCache-Control: no-cache\r\n\r\n";
|
||||
server.sendContent(header);
|
||||
server.sendHeader("Location","/login");
|
||||
server.sendHeader("Cache-Control","no-cache");
|
||||
server.send(301);
|
||||
return;
|
||||
}
|
||||
String content = "<html><body><H2>hello, you successfully connected to esp8266!</H2><br>";
|
||||
|
@ -24,12 +24,10 @@ void setup(void){
|
||||
MDNS.begin(host);
|
||||
server.on("/", HTTP_GET, [](){
|
||||
server.sendHeader("Connection", "close");
|
||||
server.sendHeader("Access-Control-Allow-Origin", "*");
|
||||
server.send(200, "text/html", serverIndex);
|
||||
});
|
||||
server.on("/update", HTTP_POST, [](){
|
||||
server.sendHeader("Connection", "close");
|
||||
server.sendHeader("Access-Control-Allow-Origin", "*");
|
||||
server.send(200, "text/plain", (Update.hasError())?"FAIL":"OK");
|
||||
ESP.restart();
|
||||
},[](){
|
||||
@ -58,14 +56,14 @@ void setup(void){
|
||||
});
|
||||
server.begin();
|
||||
MDNS.addService("http", "tcp", 80);
|
||||
|
||||
|
||||
Serial.printf("Ready! Open http://%s.local in your browser\n", host);
|
||||
} else {
|
||||
Serial.println("WiFi Failed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void loop(void){
|
||||
server.handleClient();
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user