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

ETag support for WebServer #7709

Merged
merged 13 commits into from
Dec 1, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
48 changes: 48 additions & 0 deletions libraries/ESP8266WebServer/src/ESP8266WebServer-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ static const char qop_auth[] PROGMEM = "qop=auth";
static const char qop_auth_quoted[] PROGMEM = "qop=\"auth\"";
static const char WWW_Authenticate[] PROGMEM = "WWW-Authenticate";
static const char Content_Length[] PROGMEM = "Content-Length";
static const char ETAG_HEADER[] PROGMEM = "If-None-Match";

namespace esp8266webserver {

Expand Down Expand Up @@ -618,6 +619,20 @@ void ESP8266WebServerTemplate<ServerType>::collectHeaders(const char* headerKeys
}
}

template<typename ServerType>
void ESP8266WebServerETagTemplate<ServerType>::collectHeaders(const char* headerKeys[], const size_t headerKeysCount) {
WST::_headerKeysCount = headerKeysCount + 2;
if (WST::_currentHeaders){
delete[] WST::_currentHeaders;
}
WST::_currentHeaders = new typename WST::RequestArgument[WST::_headerKeysCount];
WST::_currentHeaders[0].key = FPSTR(AUTHORIZATION_HEADER);
WST::_currentHeaders[1].key = FPSTR(ETAG_HEADER);
for (int i = 2; i < WST::_headerKeysCount; i++){
WST::_currentHeaders[i].key = headerKeys[i-2];
}
}

template <typename ServerType>
const String& ESP8266WebServerTemplate<ServerType>::header(int i) const {
if (i < _headerKeysCount)
Expand Down Expand Up @@ -831,4 +846,37 @@ String ESP8266WebServerTemplate<ServerType>::responseCodeToString(const int code
return String(r);
}

template<typename ServerType>
void ESP8266WebServerETagTemplate<ServerType>::serveStaticETag(const char* uri, FS& fs, const char* path, const char* cache_header){
WST::_addRequestHandler(new StaticRequestETagHandler<ServerType>(fs, path, uri, cache_header));
}

template<typename ServerType>
void ESP8266WebServerETagTemplate<ServerType>::serveStaticETag(const char* uri, FS& fs, const char * path) {

File toHash = fs.open(path, "r");

if(toHash){

MD5Builder calcMD5;
calcMD5.begin();

calcMD5.addStream(toHash, toHash.size());

toHash.close();

calcMD5.calculate();

uint8_t buff[16];

calcMD5.getBytes(buff);

const String etag = "\"" + base64::encode(buff, 16, false) + "\"";

serveStaticETag(uri, fs, path, etag.c_str());

}

}

} // namespace
17 changes: 16 additions & 1 deletion libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,20 @@ class ESP8266WebServerTemplate
HookFunction _hook;
};


template<typename ServerType>
class ESP8266WebServerETagTemplate : public ESP8266WebServerTemplate<ServerType>
{
using WST = ESP8266WebServerTemplate<ServerType>;

using WST::WST;

public:
void collectHeaders(const char* headerKeys[], const size_t headerKeysCount);
void serveStaticETag(const char* uri, FS& fs, const char* path, const char* cache_header);
void serveStaticETag(const char* uri, FS& fs, const char* path);
};

} // namespace

#include "ESP8266WebServer-impl.h"
Expand All @@ -308,5 +322,6 @@ class ESP8266WebServerTemplate
using ESP8266WebServer = esp8266webserver::ESP8266WebServerTemplate<WiFiServer>;
using RequestHandler = esp8266webserver::RequestHandler<WiFiServer>;

using ESP8266WebServerETag = esp8266webserver::ESP8266WebServerETagTemplate<WiFiServer>;

#endif //ESP8266WEBSERVER_H
#endif //ESP8266WEBSERVER_H
47 changes: 46 additions & 1 deletion libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,51 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
size_t _baseUriLength;
};

template<typename ServerType>
class StaticRequestETagHandler
:
public StaticRequestHandler<ServerType> {

using SRH = StaticRequestHandler<ServerType>;

using SRH::SRH;

using WebServerType = ESP8266WebServerTemplate<ServerType>;

public:
bool handle(WebServerType& server, HTTPMethod requestMethod, const String & requestUri) override {
if (!SRH::canHandle(requestMethod, requestUri)){
return false;
}

if(server.header("If-None-Match") == SRH::_cache_header){
// Serial.println("Sending 304!!!");
server.send(304);
return true;
}


File f = SRH::_fs.open(SRH::_path, "r");

if (!f){
return false;
}

if (!f.isFile()) {
f.close();
return false;
}

if (SRH::_cache_header.length() != 0){
server.sendHeader("ETag", SRH::_cache_header);
}

server.streamFile(f, SRH::getContentType(SRH::_path), requestMethod);
return true;
}

};

} // namespace

#endif //REQUESTHANDLERSIMPL_H
#endif //REQUESTHANDLERSIMPL_H