Skip to content

Commit a8b68f1

Browse files
authored
Merge pull request #48 from Neradoc/respond-to-file-head
Allow HEAD requests to files paths
2 parents f323111 + 9ea2665 commit a8b68f1

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

adafruit_httpserver/response.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,7 @@ def send_file(
172172
filename: str = "index.html",
173173
root_path: str = "./",
174174
buffer_size: int = 1024,
175+
head_only: bool = False,
175176
) -> None:
176177
"""
177178
Send response with content of ``filename`` located in ``root_path``.
@@ -197,9 +198,10 @@ def send_file(
197198
content_length=file_length,
198199
)
199200

200-
with open(root_path + filename, "rb") as file:
201-
while bytes_read := file.read(buffer_size):
202-
self._send_bytes(self.request.connection, bytes_read)
201+
if not head_only:
202+
with open(root_path + filename, "rb") as file:
203+
while bytes_read := file.read(buffer_size):
204+
self._send_bytes(self.request.connection, bytes_read)
203205
self._response_already_sent = True
204206

205207
def send_chunk(self, chunk: str = "") -> None:

adafruit_httpserver/server.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,12 +167,16 @@ def poll(self):
167167
handler(request)
168168

169169
# If no handler exists and request method is GET, try to serve a file.
170-
elif handler is None and request.method == HTTPMethod.GET:
170+
elif handler is None and request.method in (
171+
HTTPMethod.GET,
172+
HTTPMethod.HEAD,
173+
):
171174
filename = "index.html" if request.path == "/" else request.path
172175
HTTPResponse(request).send_file(
173176
filename=filename,
174177
root_path=self.root_path,
175178
buffer_size=self.request_buffer_size,
179+
head_only=(request.method == HTTPMethod.HEAD),
176180
)
177181
else:
178182
HTTPResponse(

0 commit comments

Comments
 (0)