File tree 2 files changed +10
-4
lines changed
2 files changed +10
-4
lines changed Original file line number Diff line number Diff line change @@ -172,6 +172,7 @@ def send_file(
172
172
filename : str = "index.html" ,
173
173
root_path : str = "./" ,
174
174
buffer_size : int = 1024 ,
175
+ head_only : bool = False ,
175
176
) -> None :
176
177
"""
177
178
Send response with content of ``filename`` located in ``root_path``.
@@ -197,9 +198,10 @@ def send_file(
197
198
content_length = file_length ,
198
199
)
199
200
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 )
203
205
self ._response_already_sent = True
204
206
205
207
def send_chunk (self , chunk : str = "" ) -> None :
Original file line number Diff line number Diff line change @@ -167,12 +167,16 @@ def poll(self):
167
167
handler (request )
168
168
169
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 :
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