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