Skip to content

Handle HEAD requests for static files correctly #6837

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

Merged
merged 8 commits into from
Dec 4, 2019
9 changes: 7 additions & 2 deletions libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,15 @@ class ESP8266WebServerTemplate

static String urlDecode(const String& text);

// send file based header and stream body when not "HTTP_HEAD"
template<typename T>
size_t streamFile(T &file, const String& contentType) {
size_t streamFile(T &file, const String& contentType, HTTPMethod requestMethod = HTTP_GET) {
_streamFileCore(file.size(), file.name(), contentType);
return _currentClient.write(file);
if (requestMethod != HTTP_HEAD) {
return _currentClient.write(file);
} else {
return 0;
}
}

static const String responseCodeToString(const int code);
Expand Down
4 changes: 2 additions & 2 deletions libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
}

bool canHandle(HTTPMethod requestMethod, String requestUri) override {
if (requestMethod != HTTP_GET)
if ((requestMethod != HTTP_GET) && (requestMethod != HTTP_HEAD))
return false;

if ((_isFile && requestUri != _uri) || !requestUri.startsWith(_uri))
Expand Down Expand Up @@ -125,7 +125,7 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
if (_cache_header.length() != 0)
server.sendHeader("Cache-Control", _cache_header);

server.streamFile(f, contentType);
server.streamFile(f, contentType, requestMethod);
return true;
}

Expand Down