def __call__( self, environ: Environ, start_response: StartResponse ) -> Iterable[bytes]: if_none_match: str = environ.get("HTTP_IF_NONE_MATCH", "") if_modified_since: str = environ.get("HTTP_IF_MODIFIED_SINCE", "") filepath = self.ensure_absolute_path(environ.get("PATH_INFO", "")) stat_result, is_file = self.check_path_is_file(filepath) if ( stat_result is None # filepath is not exist and filepath is not None # Just for type check and not filepath.endswith(".html") # filepath is not a html file ): filepath += ".html" stat_result, is_file = self.check_path_is_file(filepath) if stat_result is not None: assert filepath is not None # Just for type check if is_file: if self.if_none_match( FileResponse.generate_etag(stat_result), if_none_match ) or self.if_modified_since(stat_result.st_ctime, if_modified_since): response = Response(304) else: response = FileResponse(filepath, stat_result=stat_result) self.set_response_headers(response) return response(environ, start_response) if stat.S_ISDIR(stat_result.st_mode): url = URL(environ=environ) url = url.replace(scheme="", path=url.path + "/") return RedirectResponse(url)(environ, start_response) raise HTTPException(404)
async def data(self) -> typing.Any: content_type = self.content_type if content_type == "application/json": return await self.json elif str(content_type) in ( "multipart/form-data", "application/x-www-form-urlencoded", ): return await self.form raise HTTPException(HTTPStatus.UNSUPPORTED_MEDIA_TYPE)
def __call__( self, environ: Environ, start_response: StartResponse ) -> Iterable[bytes]: if_none_match: str = environ.get("HTTP_IF_NONE_MATCH", "") if_modified_since: str = environ.get("HTTP_IF_MODIFIED_SINCE", "") filepath = self.ensure_absolute_path(environ.get("PATH_INFO", "")) stat_result, is_file = self.check_path_is_file(filepath) if is_file and stat_result: assert filepath is not None # Just for type check if self.if_none_match( FileResponse.generate_etag(stat_result), if_none_match ) or self.if_modified_since(stat_result.st_ctime, if_modified_since): response = Response(304) else: response = FileResponse(filepath, stat_result=stat_result) self.set_response_headers(response) return response(environ, start_response) raise HTTPException(404)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if_none_match: str = "" if_modified_since: str = "" for k, v in scope["headers"]: if k == b"if-none-match": if_none_match = v.decode("latin-1") elif k == b"if-modified-since": if_modified_since = v.decode("latin-1") filepath = self.ensure_absolute_path(scope["path"]) stat_result, is_file = self.check_path_is_file(filepath) if is_file and stat_result: assert filepath is not None # Just for type check if self.if_none_match(FileResponse.generate_etag(stat_result), if_none_match) or self.if_modified_since( stat_result.st_ctime, if_modified_since): response = Response(304) else: response = FileResponse(filepath, stat_result=stat_result) self.set_response_headers(response) return await response(scope, receive, send) raise HTTPException(404)
async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: if_none_match: str = "" if_modified_since: str = "" for k, v in scope["headers"]: if k == b"if-none-match": if_none_match = v.decode("latin-1") elif k == b"if-modified-since": if_modified_since = v.decode("latin-1") filepath = self.ensure_absolute_path(scope["path"]) stat_result, is_file = self.check_path_is_file(filepath) if (stat_result is None # filepath is not exist and filepath is not None # Just for type check and not filepath.endswith(".html") # filepath is not a html file ): filepath += ".html" stat_result, is_file = self.check_path_is_file(filepath) if stat_result is not None: assert filepath is not None # Just for type check if is_file: if self.if_none_match(FileResponse.generate_etag(stat_result), if_none_match) or self.if_modified_since( stat_result.st_ctime, if_modified_since): response = Response(304) else: response = FileResponse(filepath, stat_result=stat_result) self.set_response_headers(response) return await response(scope, receive, send) if stat.S_ISDIR(stat_result.st_mode): url = URL(scope=scope) url = url.replace(scheme="", path=url.path + "/") return await RedirectResponse(url)(scope, receive, send) raise HTTPException(404)
def test_custom_status_code(): assert str(HTTPException(0)) == "(0, 'Maybe a custom HTTP status code')"