From 4370f96c24265658a8e3df1cbd9d23426b21c321 Mon Sep 17 00:00:00 2001 From: Matthias Hertel <mathertel@hotmail.com> Date: Mon, 25 Nov 2019 17:13:39 +0100 Subject: [PATCH 1/2] Handle HEAD requests for static files correctly --- libraries/ESP8266WebServer/src/ESP8266WebServer.h | 9 +++++++-- .../ESP8266WebServer/src/detail/RequestHandlersImpl.h | 4 ++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 60b10249d1..9a27465f0f 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -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); diff --git a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h index 33895d8a7c..fb7bbb84d8 100644 --- a/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h +++ b/libraries/ESP8266WebServer/src/detail/RequestHandlersImpl.h @@ -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)) @@ -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; } From 5a2401d831c8d6c5e86a9cc836796580d20bed18 Mon Sep 17 00:00:00 2001 From: Matthias Hertel <mathertel@hotmail.com> Date: Wed, 27 Nov 2019 21:25:00 +0100 Subject: [PATCH 2/2] Handle HEAD requests for static files correctly --- .../ESP8266WebServer/src/ESP8266WebServer.h | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/libraries/ESP8266WebServer/src/ESP8266WebServer.h b/libraries/ESP8266WebServer/src/ESP8266WebServer.h index 9a27465f0f..aa9b6ee8e6 100644 --- a/libraries/ESP8266WebServer/src/ESP8266WebServer.h +++ b/libraries/ESP8266WebServer/src/ESP8266WebServer.h @@ -151,15 +151,22 @@ class ESP8266WebServerTemplate static String urlDecode(const String& text); - // send file based header and stream body when not "HTTP_HEAD" + // 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, HTTPMethod requestMethod = HTTP_GET) { + 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); - if (requestMethod != HTTP_HEAD) { - return _currentClient.write(file); - } else { - return 0; + if (requestMethod == HTTP_GET) { + contentLength = _currentClient.write(file); } + return contentLength; } static const String responseCodeToString(const int code);