(self, scope: Scope, receive: Receive, send: Send)
| 341 | self.headers.setdefault("etag", etag) |
| 342 | |
| 343 | async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None: |
| 344 | scope_type = scope["type"] |
| 345 | send_header_only = scope_type == "http" and scope["method"].upper() == "HEAD" |
| 346 | send_pathsend = scope_type == "http" and "http.response.pathsend" in scope.get("extensions", {}) |
| 347 | if scope_type == "websocket": |
| 348 | send = self._wrap_websocket_denial_send(send) |
| 349 | |
| 350 | if self.stat_result is None: |
| 351 | try: |
| 352 | stat_result = await anyio.to_thread.run_sync(os.stat, self.path) |
| 353 | self.set_stat_headers(stat_result) |
| 354 | except FileNotFoundError: |
| 355 | raise RuntimeError(f"File at path {self.path} does not exist.") |
| 356 | else: |
| 357 | mode = stat_result.st_mode |
| 358 | if not stat.S_ISREG(mode): |
| 359 | raise RuntimeError(f"File at path {self.path} is not a file.") |
| 360 | else: |
| 361 | stat_result = self.stat_result |
| 362 | |
| 363 | headers = Headers(scope=scope) |
| 364 | http_range = headers.get("range") |
| 365 | http_if_range = headers.get("if-range") |
| 366 | |
| 367 | if ( |
| 368 | self.status_code != 200 |
| 369 | or http_range is None |
| 370 | or (http_if_range is not None and not self._should_use_range(http_if_range)) |
| 371 | ): |
| 372 | await self._handle_simple(send, send_header_only, send_pathsend) |
| 373 | else: |
| 374 | try: |
| 375 | ranges = self._parse_range_header(http_range, stat_result.st_size) |
| 376 | except MalformedRangeHeader as exc: |
| 377 | return await PlainTextResponse(exc.content, status_code=400)(scope, receive, send) |
| 378 | except RangeNotSatisfiable as exc: |
| 379 | response = PlainTextResponse(status_code=416, headers={"Content-Range": f"bytes */{exc.max_size}"}) |
| 380 | return await response(scope, receive, send) |
| 381 | |
| 382 | if len(ranges) == 0: |
| 383 | await self._handle_simple(send, send_header_only, send_pathsend) |
| 384 | elif len(ranges) == 1: |
| 385 | start, end = ranges[0] |
| 386 | await self._handle_single_range(send, start, end, stat_result.st_size, send_header_only) |
| 387 | else: |
| 388 | await self._handle_multiple_ranges(send, ranges, stat_result.st_size, send_header_only) |
| 389 | |
| 390 | if self.background is not None: |
| 391 | await self.background() |
| 392 | |
| 393 | async def _handle_simple(self, send: Send, send_header_only: bool, send_pathsend: bool) -> None: |
| 394 | await send({"type": "http.response.start", "status": self.status_code, "headers": self.raw_headers}) |
nothing calls this directly
no test coverage detected