@@ -97,6 +97,7 @@ def __init__(
97
97
headers : Optional [LooseHeaders ] = None ,
98
98
) -> None :
99
99
super ().__init__ (status = status , reason = reason , headers = headers )
100
+ # either set self._io or self._path
100
101
if isinstance (path , io .BufferedReader ):
101
102
self ._io = path
102
103
self ._path = None
@@ -193,6 +194,10 @@ def _make_response(
193
194
file_path , st , file_encoding = self ._get_file_path_stat_encoding (
194
195
accept_encoding
195
196
)
197
+ # file_path is None if the path is not a regular file
198
+ # it is also None if self._io is used instead of self._path
199
+ if file_path is None and self ._io is None :
200
+ return _FileResponseResult .NOT_ACCEPTABLE , None , st , None
196
201
197
202
etag_value = f"{ st .st_mtime_ns :x} -{ st .st_size :x} "
198
203
@@ -222,10 +227,9 @@ def _make_response(
222
227
):
223
228
return _FileResponseResult .NOT_MODIFIED , None , st , file_encoding
224
229
230
+ # if file_path is None at this stage, self._io is set or NOT_ACCEPTABLE
231
+ # would have been returned earlier
225
232
if file_path is None :
226
- if self ._io is None :
227
- return _FileResponseResult .NOT_ACCEPTABLE , None , st , None
228
-
229
233
return _FileResponseResult .SEND_FILE , self ._io , st , file_encoding
230
234
231
235
fobj = file_path .open ("rb" )
@@ -240,6 +244,7 @@ def _make_response(
240
244
def _get_file_path_stat_encoding (
241
245
self , accept_encoding : str
242
246
) -> Tuple [Optional [pathlib .Path ], os .stat_result , Optional [str ]]:
247
+ # self._io used instead of self._path
243
248
if self ._path is None :
244
249
assert self ._io is not None
245
250
return None , os .stat (self ._io .fileno ()), None
@@ -396,6 +401,8 @@ async def _prepare_open_file(
396
401
guesser = CONTENT_TYPES .guess_type
397
402
self .content_type = guesser (self ._path )[0 ] or FALLBACK_CONTENT_TYPE
398
403
else :
404
+ # content-type cannot be determined if self._io is used
405
+ # instead of self._path
399
406
self .content_type = FALLBACK_CONTENT_TYPE
400
407
401
408
if file_encoding :
0 commit comments