File tree Expand file tree Collapse file tree 3 files changed +13
-7
lines changed Expand file tree Collapse file tree 3 files changed +13
-7
lines changed Original file line number Diff line number Diff line change @@ -215,11 +215,12 @@ def _get_file_length(file_path: str) -> int:
215
215
raise FileNotExistsError (file_path ) # pylint: disable=raise-missing-from
216
216
217
217
@_prevent_multiple_send_calls
218
- def send_file (
218
+ def send_file ( # pylint: disable=too-many-arguments
219
219
self ,
220
220
filename : str = "index.html" ,
221
221
root_path : str = "./" ,
222
222
buffer_size : int = 1024 ,
223
+ head_only : bool = False ,
223
224
safe : bool = True ,
224
225
) -> None :
225
226
"""
@@ -247,9 +248,10 @@ def send_file(
247
248
content_length = file_length ,
248
249
)
249
250
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 )
253
255
self ._response_already_sent = True
254
256
255
257
def send_chunk (self , chunk : str = "" ) -> None :
Original file line number Diff line number Diff line change @@ -106,7 +106,7 @@ def route_func(request, my_parameter):
106
106
my_parameter == "123" # True
107
107
"""
108
108
if not self ._routes :
109
- raise ValueError ( "No routes added" )
109
+ return None
110
110
111
111
found_route , _route = False , None
112
112
Original file line number Diff line number Diff line change @@ -166,13 +166,17 @@ def poll(self):
166
166
if handler is not None and callable (handler ):
167
167
handler (request )
168
168
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
+ ):
171
174
filename = "index.html" if request .path == "/" else request .path
172
175
HTTPResponse (request ).send_file (
173
176
filename = filename ,
174
177
root_path = self .root_path ,
175
178
buffer_size = self .request_buffer_size ,
179
+ head_only = (request .method == HTTPMethod .HEAD ),
176
180
)
177
181
else :
178
182
HTTPResponse (
You can’t perform that action at this time.
0 commit comments