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
14 changes: 13 additions & 1 deletion libraries/ESP8266WebServer/src/ESP8266WebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,10 +151,22 @@ class ESP8266WebServerTemplate

static String urlDecode(const String& text);

// Handle a GET request by sending a response header and stream file content to response body
template<typename T>
size_t streamFile(T &file, const String& contentType) {
return streamFile(file, contentType, HTTP_GET);
}

// Implement GET and HEAD requests for files.
// Stream body on HTTP_GET but not on HTTP_HEAD requests.
template<typename T>
size_t streamFile(T &file, const String& contentType, HTTPMethod requestMethod) {
size_t contentLength = 0;
_streamFileCore(file.size(), file.name(), contentType);
return _currentClient.write(file);
if (requestMethod == HTTP_GET) {
contentLength = _currentClient.write(file);
}
return contentLength;
}

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