File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff 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 :
Original file line number Diff line number Diff 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
Original file line number Diff line number Diff 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 (
You can’t perform that action at this time.
0 commit comments