Skip to content

Commit a738884

Browse files
matherteld-a-v
authored andcommitted
Handle HEAD requests for static files correctly (esp8266#6837)
* Handle HEAD requests for static files correctly * Handle HEAD requests for static files correctly
1 parent 5aefed2 commit a738884

File tree

2 files changed

+15
-3
lines changed

2 files changed

+15
-3
lines changed

libraries/ESP8266WebServer/src/ESP8266WebServer.h

+13-1
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,22 @@ class ESP8266WebServerTemplate
151151

152152
static String urlDecode(const String& text);
153153

154+
// Handle a GET request by sending a response header and stream file content to response body
154155
template<typename T>
155156
size_t streamFile(T &file, const String& contentType) {
157+
return streamFile(file, contentType, HTTP_GET);
158+
}
159+
160+
// Implement GET and HEAD requests for files.
161+
// Stream body on HTTP_GET but not on HTTP_HEAD requests.
162+
template<typename T>
163+
size_t streamFile(T &file, const String& contentType, HTTPMethod requestMethod) {
164+
size_t contentLength = 0;
156165
_streamFileCore(file.size(), file.name(), contentType);
157-
return _currentClient.write(file);
166+
if (requestMethod == HTTP_GET) {
167+
contentLength = _currentClient.write(file);
168+
}
169+
return contentLength;
158170
}
159171

160172
static const String responseCodeToString(const int code);

libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ class StaticRequestHandler : public RequestHandler<ServerType> {
7676
}
7777

7878
bool canHandle(HTTPMethod requestMethod, String requestUri) override {
79-
if (requestMethod != HTTP_GET)
79+
if ((requestMethod != HTTP_GET) && (requestMethod != HTTP_HEAD))
8080
return false;
8181

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

128-
server.streamFile(f, contentType);
128+
server.streamFile(f, contentType, requestMethod);
129129
return true;
130130
}
131131

0 commit comments

Comments
 (0)