Skip to content

Commit

Permalink
additional mimetable fixes, additional string moves to progmem (#4371)
Browse files Browse the repository at this point in the history
  • Loading branch information
devyte authored Feb 16, 2018
1 parent bb90e12 commit 5328a8b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 29 deletions.
17 changes: 7 additions & 10 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,9 @@
#define DEBUG_OUTPUT Serial
#endif

//const char * AUTHORIZATION_HEADER = "Authorization";
static const char AUTHORIZATION_HEADER[] PROGMEM = "Authorization";
static const char qop_auth[] PROGMEM = "qop=auth";
static const char WWW_Authenticate[] PROGMEM = "WWW-Authenticate";
static const char colon[] PROGMEM = ":";
static const char Content_Length[] PROGMEM = "Content-Length";


Expand Down Expand Up @@ -81,10 +79,9 @@ ESP8266WebServer::ESP8266WebServer(int port)
}

ESP8266WebServer::~ESP8266WebServer() {
close();
_server.close();
if (_currentHeaders)
delete[]_currentHeaders;
_headerKeysCount = 0;
RequestHandler* handler = _firstHandler;
while (handler) {
RequestHandler* next = handler->next();
Expand All @@ -105,7 +102,7 @@ void ESP8266WebServer::begin(uint16_t port) {

String ESP8266WebServer::_extractParam(String& authReq,const String& param,const char delimit){
int _begin = authReq.indexOf(param);
if (_begin==-1)
if (_begin == -1)
return "";
return authReq.substring(_begin+param.length(),authReq.indexOf(delimit,_begin+param.length()));
}
Expand Down Expand Up @@ -195,9 +192,9 @@ bool ESP8266WebServer::authenticate(const char * username, const char * password
#endif
md5.begin();
if(authReq.indexOf(FPSTR(qop_auth)) != -1) {
md5.add(_H1 + FPSTR(colon) + _nonce + FPSTR(colon) + _nc + FPSTR(colon) + _cnonce + ":auth:" + _H2);
}else{
md5.add(_H1 + FPSTR(colon) + _nonce + FPSTR(colon) + _H2);
md5.add(_H1 + ':' + _nonce + ':' + _nc + ':' + _cnonce + F(":auth:") + _H2);
} else {
md5.add(_H1 + ':' + _nonce + ':' + _H2);
}
md5.calculate();
String _responsecheck = md5.toString();
Expand Down Expand Up @@ -237,7 +234,7 @@ void ESP8266WebServer::requestAuthentication(HTTPAuthMethod mode, const char* re
sendHeader(String(FPSTR(WWW_Authenticate)), String(F("Digest realm=\"")) +_srealm + String(F("\", qop=\"auth\", nonce=\"")) + _snonce + String(F("\", opaque=\"")) + _sopaque + String(F("\"")));
}
using namespace mime;
send(401, mimeTable[html].mimeType, authFailMsg);
send(401, String(FPSTR(mimeTable[html].mimeType)), authFailMsg);
}

void ESP8266WebServer::on(const String &uri, ESP8266WebServer::THandlerFunction handler) {
Expand Down Expand Up @@ -603,7 +600,7 @@ void ESP8266WebServer::_handleRequest() {
}
if (!handled) {
using namespace mime;
send(404, mimeTable[html].mimeType, String(F("Not found: ")) + _currentUri);
send(404, String(FPSTR(mimeTable[html].mimeType)), String(F("Not found: ")) + _currentUri);
handled = true;
}
if (handled) {
Expand Down
44 changes: 26 additions & 18 deletions libraries/ESP8266WebServer/src/Parsing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "WiFiServer.h"
#include "WiFiClient.h"
#include "ESP8266WebServer.h"
#include "detail/mimetable.h"

//#define DEBUG_ESP_HTTP_SERVER
#ifdef DEBUG_ESP_PORT
Expand All @@ -31,6 +32,9 @@
#define DEBUG_OUTPUT Serial
#endif

static const char Content_Type[] PROGMEM = "Content-Type";
static const char filename[] PROGMEM = "filename";

static char* readBytesWithTimeout(WiFiClient& client, size_t maxLength, size_t& dataLength, int timeout_ms)
{
char *buf = nullptr;
Expand Down Expand Up @@ -98,15 +102,15 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
_chunked = false;

HTTPMethod method = HTTP_GET;
if (methodStr == "POST") {
if (methodStr == F("POST")) {
method = HTTP_POST;
} else if (methodStr == "DELETE") {
} else if (methodStr == F("DELETE")) {
method = HTTP_DELETE;
} else if (methodStr == "OPTIONS") {
} else if (methodStr == F("OPTIONS")) {
method = HTTP_OPTIONS;
} else if (methodStr == "PUT") {
} else if (methodStr == F("PUT")) {
method = HTTP_PUT;
} else if (methodStr == "PATCH") {
} else if (methodStr == F("PATCH")) {
method = HTTP_PATCH;
}
_currentMethod = method;
Expand Down Expand Up @@ -158,20 +162,21 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
DEBUG_OUTPUT.println(headerValue);
#endif

if (headerName.equalsIgnoreCase("Content-Type")){
if (headerValue.startsWith("text/plain")){
if (headerName.equalsIgnoreCase(FPSTR(Content_Type))){
using namespace mime;
if (headerValue.startsWith(FPSTR(mimeTable[txt].mimeType))){
isForm = false;
} else if (headerValue.startsWith("application/x-www-form-urlencoded")){
} else if (headerValue.startsWith(F("application/x-www-form-urlencoded"))){
isForm = false;
isEncoded = true;
} else if (headerValue.startsWith("multipart/")){
boundaryStr = headerValue.substring(headerValue.indexOf('=')+1);
} else if (headerValue.startsWith(F("multipart/"))){
boundaryStr = headerValue.substring(headerValue.indexOf('=') + 1);
boundaryStr.replace("\"","");
isForm = true;
}
} else if (headerName.equalsIgnoreCase("Content-Length")){
} else if (headerName.equalsIgnoreCase(F("Content-Length"))){
contentLength = headerValue.toInt();
} else if (headerName.equalsIgnoreCase("Host")){
} else if (headerName.equalsIgnoreCase(F("Host"))){
_hostHeader = headerValue;
}
}
Expand All @@ -193,7 +198,7 @@ bool ESP8266WebServer::_parseRequest(WiFiClient& client) {
if(!isEncoded){
//plain post json or other data
RequestArgument& arg = _currentArgs[_currentArgCount++];
arg.key = "plain";
arg.key = F("plain");
arg.value = String(plainBuf);
}

Expand Down Expand Up @@ -389,7 +394,7 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t

line = client.readStringUntil('\r');
client.readStringUntil('\n');
if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase("Content-Disposition")){
if (line.length() > 19 && line.substring(0, 19).equalsIgnoreCase(F("Content-Disposition"))){
int nameStart = line.indexOf('=');
if (nameStart != -1){
argName = line.substring(nameStart+2);
Expand All @@ -405,16 +410,18 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
DEBUG_OUTPUT.println(argFilename);
#endif
//use GET to set the filename if uploading using blob
if (argFilename == "blob" && hasArg("filename")) argFilename = arg("filename");
if (argFilename == F("blob") && hasArg(FPSTR(filename)))
argFilename = arg(FPSTR(filename));
}
#ifdef DEBUG_ESP_HTTP_SERVER
DEBUG_OUTPUT.print("PostArg Name: ");
DEBUG_OUTPUT.println(argName);
#endif
argType = "text/plain";
using namespace mime;
argType = FPSTR(mimeTable[txt].mimeType);
line = client.readStringUntil('\r');
client.readStringUntil('\n');
if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase("Content-Type")){
if (line.length() > 12 && line.substring(0, 12).equalsIgnoreCase(FPSTR(Content_Type))){
argType = line.substring(line.indexOf(':')+2);
//skip next line
client.readStringUntil('\r');
Expand Down Expand Up @@ -559,7 +566,8 @@ bool ESP8266WebServer::_parseForm(WiFiClient& client, String boundary, uint32_t
arg.value = postArgs[iarg].value;
}
_currentArgCount = iarg;
if (postArgs) delete[] postArgs;
if (postArgs)
delete[] postArgs;
return true;
}
#ifdef DEBUG_ESP_HTTP_SERVER
Expand Down
3 changes: 2 additions & 1 deletion libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@ class StaticRequestHandler : public RequestHandler {
if (!_isFile) {
// Base URI doesn't point to a file.
// If a directory is requested, look for index file.
if (requestUri.endsWith("/")) requestUri += "index.htm";
if (requestUri.endsWith("/"))
requestUri += "index.htm";

// Append whatever follows this URI in request to get the file path.
path += requestUri.substring(_baseUriLength);
Expand Down

0 comments on commit 5328a8b

Please sign in to comment.