Skip to content

Commit 394c6f3

Browse files
committed
Merge commit 'a8b68f153b72b8c022eceec254abe129cf5f5802' into prevent-parent-directory-access
2 parents bef9f76 + a8b68f1 commit 394c6f3

File tree

3 files changed

+13
-7
lines changed

3 files changed

+13
-7
lines changed

adafruit_httpserver/response.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,12 @@ def _get_file_length(file_path: str) -> int:
215215
raise FileNotExistsError(file_path) # pylint: disable=raise-missing-from
216216

217217
@_prevent_multiple_send_calls
218-
def send_file(
218+
def send_file( # pylint: disable=too-many-arguments
219219
self,
220220
filename: str = "index.html",
221221
root_path: str = "./",
222222
buffer_size: int = 1024,
223+
head_only: bool = False,
223224
safe: bool = True,
224225
) -> None:
225226
"""
@@ -247,9 +248,10 @@ def send_file(
247248
content_length=file_length,
248249
)
249250

250-
with open(full_file_path, "rb") as file:
251-
while bytes_read := file.read(buffer_size):
252-
self._send_bytes(self.request.connection, bytes_read)
251+
if not head_only:
252+
with open(full_file_path, "rb") as file:
253+
while bytes_read := file.read(buffer_size):
254+
self._send_bytes(self.request.connection, bytes_read)
253255
self._response_already_sent = True
254256

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

adafruit_httpserver/route.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def route_func(request, my_parameter):
106106
my_parameter == "123" # True
107107
"""
108108
if not self._routes:
109-
raise ValueError("No routes added")
109+
return None
110110

111111
found_route, _route = False, None
112112

adafruit_httpserver/server.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,13 +166,17 @@ def poll(self):
166166
if handler is not None and callable(handler):
167167
handler(request)
168168

169-
# If no handler exists and request method is GET, try to serve a file.
170-
elif handler is None and request.method == HTTPMethod.GET:
169+
# If no handler exists and request method is GET or HEAD, try to serve a file.
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)