Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chunked encoding #2199

Merged
merged 16 commits into from
Jun 28, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand All @@ -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>";
Expand Down
8 changes: 3 additions & 5 deletions libraries/ESP8266WebServer/examples/WebUpdate/WebUpdate.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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();
},[](){
Expand Down Expand Up @@ -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);
}
}
51 changes: 44 additions & 7 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const char * AUTHORIZATION_HEADER = "Authorization";
ESP8266WebServer::ESP8266WebServer(IPAddress addr, int port)
: _server(addr, port)
, _currentMethod(HTTP_ANY)
, _currentVersion(0)
, _currentHandler(0)
, _firstHandler(0)
, _lastHandler(0)
Expand All @@ -49,12 +50,14 @@ ESP8266WebServer::ESP8266WebServer(IPAddress addr, int port)
, _headerKeysCount(0)
, _currentHeaders(0)
, _contentLength(0)
, _chunked(false)
{
}

ESP8266WebServer::ESP8266WebServer(int port)
: _server(port)
, _currentMethod(HTTP_ANY)
, _currentVersion(0)
, _currentHandler(0)
, _firstHandler(0)
, _lastHandler(0)
Expand All @@ -63,6 +66,7 @@ ESP8266WebServer::ESP8266WebServer(int port)
, _headerKeysCount(0)
, _currentHeaders(0)
, _contentLength(0)
, _chunked(false)
{
}

Expand Down Expand Up @@ -246,7 +250,7 @@ void ESP8266WebServer::setContentLength(size_t contentLength) {
}

void ESP8266WebServer::_prepareHeader(String& response, int code, const char* content_type, size_t contentLength) {
response = "HTTP/1.1 ";
response = "HTTP/1."+String(_currentVersion)+" ";
response += String(code);
response += " ";
response += _responseCodeToString(code);
Expand All @@ -260,9 +264,13 @@ void ESP8266WebServer::_prepareHeader(String& response, int code, const char* co
sendHeader("Content-Length", String(contentLength));
} else if (_contentLength != CONTENT_LENGTH_UNKNOWN) {
sendHeader("Content-Length", String(_contentLength));
} else if(_contentLength == CONTENT_LENGTH_UNKNOWN && _currentVersion){ //HTTP/1.1 or above client
//let's do chunked
_chunked = true;
sendHeader("Accept-Ranges","none");
sendHeader("Transfer-Encoding","chunked");
}
sendHeader("Connection", "close");
sendHeader("Access-Control-Allow-Origin", "*");

response += _responseHeaders;
response += "\r\n";
Expand All @@ -271,9 +279,13 @@ void ESP8266WebServer::_prepareHeader(String& response, int code, const char* co

void ESP8266WebServer::send(int code, const char* content_type, const String& content) {
String header;
// Can we asume the following?
//if(code == 200 && content.length() == 0 && _contentLength == CONTENT_LENGTH_NOT_SET)
// _contentLength = CONTENT_LENGTH_UNKNOWN;
_prepareHeader(header, code, content_type, content.length());
sendContent(header);
sendContent(content);
_currentClient.write(header.c_str(), header.length());
if(content.length())
sendContent(content);
}

void ESP8266WebServer::send_P(int code, PGM_P content_type, PGM_P content) {
Expand All @@ -287,7 +299,7 @@ void ESP8266WebServer::send_P(int code, PGM_P content_type, PGM_P content) {
char type[64];
memccpy_P((void*)type, (PGM_VOID_P)content_type, 0, sizeof(type));
_prepareHeader(header, code, (const char* )type, contentLength);
sendContent(header);
_currentClient.write(header.c_str(), header.length());
sendContent_P(content);
}

Expand All @@ -309,15 +321,40 @@ void ESP8266WebServer::send(int code, const String& content_type, const String&
}

void ESP8266WebServer::sendContent(const String& content) {
_currentClient.write(content.c_str(), content.length());
const char * footer = "\r\n";
size_t len = content.length();
if(_chunked) {
char * chunkSize = (char *)malloc(11);
if(chunkSize){
sprintf(chunkSize, "%x%s", len, footer);
_currentClient.write(chunkSize, strlen(chunkSize));
free(chunkSize);
}
}
_currentClient.write(content.c_str(), len);
if(_chunked){
_currentClient.write(footer, 2);
}
}

void ESP8266WebServer::sendContent_P(PGM_P content) {
_currentClient.write_P(content, strlen_P(content));
sendContent_P(content, strlen_P(content));
}

void ESP8266WebServer::sendContent_P(PGM_P content, size_t size) {
const char * footer = "\r\n";
if(_chunked) {
char * chunkSize = (char *)malloc(11);
if(chunkSize){
sprintf(chunkSize, "%x%s", size, footer);
_currentClient.write(chunkSize, strlen(chunkSize));
free(chunkSize);
}
}
_currentClient.write_P(content, size);
if(_chunked){
_currentClient.write(footer, 2);
}
}


Expand Down
2 changes: 2 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
WiFiClient _currentClient;
HTTPMethod _currentMethod;
String _currentUri;
uint8_t _currentVersion;
HTTPClientStatus _currentStatus;
unsigned long _statusChange;

Expand All @@ -175,6 +176,7 @@ template<typename T> size_t streamFile(T &file, const String& contentType){
String _responseHeaders;

String _hostHeader;
bool _chunked;

};

Expand Down
3 changes: 3 additions & 0 deletions libraries/ESP8266WebServer/src/Parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,16 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {

String methodStr = req.substring(0, addr_start);
String url = req.substring(addr_start + 1, addr_end);
String versionEnd = req.substring(addr_end + 8);
_currentVersion = atoi(versionEnd.c_str());
String searchStr = "";
int hasSearch = url.indexOf('?');
if (hasSearch != -1){
searchStr = url.substring(hasSearch + 1);
url = url.substring(0, hasSearch);
}
_currentUri = url;
_chunked = false;

HTTPMethod method = HTTP_GET;
if (methodStr == "POST") {
Expand Down