You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

549 lines
20 KiB

  1. from __future__ import annotations
  2. import hashlib
  3. import http.cookies
  4. import json
  5. import os
  6. import re
  7. import stat
  8. import sys
  9. import warnings
  10. from collections.abc import AsyncIterable, Awaitable, Iterable, Mapping, Sequence
  11. from datetime import datetime
  12. from email.utils import format_datetime, formatdate
  13. from functools import partial
  14. from mimetypes import guess_type
  15. from secrets import token_hex
  16. from typing import Any, Callable, Literal, Union
  17. from urllib.parse import quote
  18. import anyio
  19. import anyio.to_thread
  20. from starlette._utils import collapse_excgroups
  21. from starlette.background import BackgroundTask
  22. from starlette.concurrency import iterate_in_threadpool
  23. from starlette.datastructures import URL, Headers, MutableHeaders
  24. from starlette.requests import ClientDisconnect
  25. from starlette.types import Receive, Scope, Send
  26. class Response:
  27. media_type = None
  28. charset = "utf-8"
  29. def __init__(
  30. self,
  31. content: Any = None,
  32. status_code: int = 200,
  33. headers: Mapping[str, str] | None = None,
  34. media_type: str | None = None,
  35. background: BackgroundTask | None = None,
  36. ) -> None:
  37. self.status_code = status_code
  38. if media_type is not None:
  39. self.media_type = media_type
  40. self.background = background
  41. self.body = self.render(content)
  42. self.init_headers(headers)
  43. def render(self, content: Any) -> bytes | memoryview:
  44. if content is None:
  45. return b""
  46. if isinstance(content, (bytes, memoryview)):
  47. return content
  48. return content.encode(self.charset) # type: ignore
  49. def init_headers(self, headers: Mapping[str, str] | None = None) -> None:
  50. if headers is None:
  51. raw_headers: list[tuple[bytes, bytes]] = []
  52. populate_content_length = True
  53. populate_content_type = True
  54. else:
  55. raw_headers = [(k.lower().encode("latin-1"), v.encode("latin-1")) for k, v in headers.items()]
  56. keys = [h[0] for h in raw_headers]
  57. populate_content_length = b"content-length" not in keys
  58. populate_content_type = b"content-type" not in keys
  59. body = getattr(self, "body", None)
  60. if (
  61. body is not None
  62. and populate_content_length
  63. and not (self.status_code < 200 or self.status_code in (204, 304))
  64. ):
  65. content_length = str(len(body))
  66. raw_headers.append((b"content-length", content_length.encode("latin-1")))
  67. content_type = self.media_type
  68. if content_type is not None and populate_content_type:
  69. if content_type.startswith("text/") and "charset=" not in content_type.lower():
  70. content_type += "; charset=" + self.charset
  71. raw_headers.append((b"content-type", content_type.encode("latin-1")))
  72. self.raw_headers = raw_headers
  73. @property
  74. def headers(self) -> MutableHeaders:
  75. if not hasattr(self, "_headers"):
  76. self._headers = MutableHeaders(raw=self.raw_headers)
  77. return self._headers
  78. def set_cookie(
  79. self,
  80. key: str,
  81. value: str = "",
  82. max_age: int | None = None,
  83. expires: datetime | str | int | None = None,
  84. path: str | None = "/",
  85. domain: str | None = None,
  86. secure: bool = False,
  87. httponly: bool = False,
  88. samesite: Literal["lax", "strict", "none"] | None = "lax",
  89. partitioned: bool = False,
  90. ) -> None:
  91. cookie: http.cookies.BaseCookie[str] = http.cookies.SimpleCookie()
  92. cookie[key] = value
  93. if max_age is not None:
  94. cookie[key]["max-age"] = max_age
  95. if expires is not None:
  96. if isinstance(expires, datetime):
  97. cookie[key]["expires"] = format_datetime(expires, usegmt=True)
  98. else:
  99. cookie[key]["expires"] = expires
  100. if path is not None:
  101. cookie[key]["path"] = path
  102. if domain is not None:
  103. cookie[key]["domain"] = domain
  104. if secure:
  105. cookie[key]["secure"] = True
  106. if httponly:
  107. cookie[key]["httponly"] = True
  108. if samesite is not None:
  109. assert samesite.lower() in [
  110. "strict",
  111. "lax",
  112. "none",
  113. ], "samesite must be either 'strict', 'lax' or 'none'"
  114. cookie[key]["samesite"] = samesite
  115. if partitioned:
  116. if sys.version_info < (3, 14):
  117. raise ValueError("Partitioned cookies are only supported in Python 3.14 and above.") # pragma: no cover
  118. cookie[key]["partitioned"] = True # pragma: no cover
  119. cookie_val = cookie.output(header="").strip()
  120. self.raw_headers.append((b"set-cookie", cookie_val.encode("latin-1")))
  121. def delete_cookie(
  122. self,
  123. key: str,
  124. path: str = "/",
  125. domain: str | None = None,
  126. secure: bool = False,
  127. httponly: bool = False,
  128. samesite: Literal["lax", "strict", "none"] | None = "lax",
  129. ) -> None:
  130. self.set_cookie(
  131. key,
  132. max_age=0,
  133. expires=0,
  134. path=path,
  135. domain=domain,
  136. secure=secure,
  137. httponly=httponly,
  138. samesite=samesite,
  139. )
  140. async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
  141. prefix = "websocket." if scope["type"] == "websocket" else ""
  142. await send(
  143. {
  144. "type": prefix + "http.response.start",
  145. "status": self.status_code,
  146. "headers": self.raw_headers,
  147. }
  148. )
  149. await send({"type": prefix + "http.response.body", "body": self.body})
  150. if self.background is not None:
  151. await self.background()
  152. class HTMLResponse(Response):
  153. media_type = "text/html"
  154. class PlainTextResponse(Response):
  155. media_type = "text/plain"
  156. class JSONResponse(Response):
  157. media_type = "application/json"
  158. def __init__(
  159. self,
  160. content: Any,
  161. status_code: int = 200,
  162. headers: Mapping[str, str] | None = None,
  163. media_type: str | None = None,
  164. background: BackgroundTask | None = None,
  165. ) -> None:
  166. super().__init__(content, status_code, headers, media_type, background)
  167. def render(self, content: Any) -> bytes:
  168. return json.dumps(
  169. content,
  170. ensure_ascii=False,
  171. allow_nan=False,
  172. indent=None,
  173. separators=(",", ":"),
  174. ).encode("utf-8")
  175. class RedirectResponse(Response):
  176. def __init__(
  177. self,
  178. url: str | URL,
  179. status_code: int = 307,
  180. headers: Mapping[str, str] | None = None,
  181. background: BackgroundTask | None = None,
  182. ) -> None:
  183. super().__init__(content=b"", status_code=status_code, headers=headers, background=background)
  184. self.headers["location"] = quote(str(url), safe=":/%#?=@[]!$&'()*+,;")
  185. Content = Union[str, bytes, memoryview]
  186. SyncContentStream = Iterable[Content]
  187. AsyncContentStream = AsyncIterable[Content]
  188. ContentStream = Union[AsyncContentStream, SyncContentStream]
  189. class StreamingResponse(Response):
  190. body_iterator: AsyncContentStream
  191. def __init__(
  192. self,
  193. content: ContentStream,
  194. status_code: int = 200,
  195. headers: Mapping[str, str] | None = None,
  196. media_type: str | None = None,
  197. background: BackgroundTask | None = None,
  198. ) -> None:
  199. if isinstance(content, AsyncIterable):
  200. self.body_iterator = content
  201. else:
  202. self.body_iterator = iterate_in_threadpool(content)
  203. self.status_code = status_code
  204. self.media_type = self.media_type if media_type is None else media_type
  205. self.background = background
  206. self.init_headers(headers)
  207. async def listen_for_disconnect(self, receive: Receive) -> None:
  208. while True:
  209. message = await receive()
  210. if message["type"] == "http.disconnect":
  211. break
  212. async def stream_response(self, send: Send) -> None:
  213. await send(
  214. {
  215. "type": "http.response.start",
  216. "status": self.status_code,
  217. "headers": self.raw_headers,
  218. }
  219. )
  220. async for chunk in self.body_iterator:
  221. if not isinstance(chunk, (bytes, memoryview)):
  222. chunk = chunk.encode(self.charset)
  223. await send({"type": "http.response.body", "body": chunk, "more_body": True})
  224. await send({"type": "http.response.body", "body": b"", "more_body": False})
  225. async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
  226. spec_version = tuple(map(int, scope.get("asgi", {}).get("spec_version", "2.0").split(".")))
  227. if spec_version >= (2, 4):
  228. try:
  229. await self.stream_response(send)
  230. except OSError:
  231. raise ClientDisconnect()
  232. else:
  233. with collapse_excgroups():
  234. async with anyio.create_task_group() as task_group:
  235. async def wrap(func: Callable[[], Awaitable[None]]) -> None:
  236. await func()
  237. task_group.cancel_scope.cancel()
  238. task_group.start_soon(wrap, partial(self.stream_response, send))
  239. await wrap(partial(self.listen_for_disconnect, receive))
  240. if self.background is not None:
  241. await self.background()
  242. class MalformedRangeHeader(Exception):
  243. def __init__(self, content: str = "Malformed range header.") -> None:
  244. self.content = content
  245. class RangeNotSatisfiable(Exception):
  246. def __init__(self, max_size: int) -> None:
  247. self.max_size = max_size
  248. _RANGE_PATTERN = re.compile(r"(\d*)-(\d*)")
  249. class FileResponse(Response):
  250. chunk_size = 64 * 1024
  251. def __init__(
  252. self,
  253. path: str | os.PathLike[str],
  254. status_code: int = 200,
  255. headers: Mapping[str, str] | None = None,
  256. media_type: str | None = None,
  257. background: BackgroundTask | None = None,
  258. filename: str | None = None,
  259. stat_result: os.stat_result | None = None,
  260. method: str | None = None,
  261. content_disposition_type: str = "attachment",
  262. ) -> None:
  263. self.path = path
  264. self.status_code = status_code
  265. self.filename = filename
  266. if method is not None:
  267. warnings.warn(
  268. "The 'method' parameter is not used, and it will be removed.",
  269. DeprecationWarning,
  270. )
  271. if media_type is None:
  272. media_type = guess_type(filename or path)[0] or "text/plain"
  273. self.media_type = media_type
  274. self.background = background
  275. self.init_headers(headers)
  276. self.headers.setdefault("accept-ranges", "bytes")
  277. if self.filename is not None:
  278. content_disposition_filename = quote(self.filename)
  279. if content_disposition_filename != self.filename:
  280. content_disposition = f"{content_disposition_type}; filename*=utf-8''{content_disposition_filename}"
  281. else:
  282. content_disposition = f'{content_disposition_type}; filename="{self.filename}"'
  283. self.headers.setdefault("content-disposition", content_disposition)
  284. self.stat_result = stat_result
  285. if stat_result is not None:
  286. self.set_stat_headers(stat_result)
  287. def set_stat_headers(self, stat_result: os.stat_result) -> None:
  288. content_length = str(stat_result.st_size)
  289. last_modified = formatdate(stat_result.st_mtime, usegmt=True)
  290. etag_base = str(stat_result.st_mtime) + "-" + str(stat_result.st_size)
  291. etag = f'"{hashlib.md5(etag_base.encode(), usedforsecurity=False).hexdigest()}"'
  292. self.headers.setdefault("content-length", content_length)
  293. self.headers.setdefault("last-modified", last_modified)
  294. self.headers.setdefault("etag", etag)
  295. async def __call__(self, scope: Scope, receive: Receive, send: Send) -> None:
  296. send_header_only: bool = scope["method"].upper() == "HEAD"
  297. send_pathsend: bool = "http.response.pathsend" in scope.get("extensions", {})
  298. if self.stat_result is None:
  299. try:
  300. stat_result = await anyio.to_thread.run_sync(os.stat, self.path)
  301. self.set_stat_headers(stat_result)
  302. except FileNotFoundError:
  303. raise RuntimeError(f"File at path {self.path} does not exist.")
  304. else:
  305. mode = stat_result.st_mode
  306. if not stat.S_ISREG(mode):
  307. raise RuntimeError(f"File at path {self.path} is not a file.")
  308. else:
  309. stat_result = self.stat_result
  310. headers = Headers(scope=scope)
  311. http_range = headers.get("range")
  312. http_if_range = headers.get("if-range")
  313. if http_range is None or (http_if_range is not None and not self._should_use_range(http_if_range)):
  314. await self._handle_simple(send, send_header_only, send_pathsend)
  315. else:
  316. try:
  317. ranges = self._parse_range_header(http_range, stat_result.st_size)
  318. except MalformedRangeHeader as exc:
  319. return await PlainTextResponse(exc.content, status_code=400)(scope, receive, send)
  320. except RangeNotSatisfiable as exc:
  321. response = PlainTextResponse(status_code=416, headers={"Content-Range": f"*/{exc.max_size}"})
  322. return await response(scope, receive, send)
  323. if len(ranges) == 1:
  324. start, end = ranges[0]
  325. await self._handle_single_range(send, start, end, stat_result.st_size, send_header_only)
  326. else:
  327. await self._handle_multiple_ranges(send, ranges, stat_result.st_size, send_header_only)
  328. if self.background is not None:
  329. await self.background()
  330. async def _handle_simple(self, send: Send, send_header_only: bool, send_pathsend: bool) -> None:
  331. await send({"type": "http.response.start", "status": self.status_code, "headers": self.raw_headers})
  332. if send_header_only:
  333. await send({"type": "http.response.body", "body": b"", "more_body": False})
  334. elif send_pathsend:
  335. await send({"type": "http.response.pathsend", "path": str(self.path)})
  336. else:
  337. async with await anyio.open_file(self.path, mode="rb") as file:
  338. more_body = True
  339. while more_body:
  340. chunk = await file.read(self.chunk_size)
  341. more_body = len(chunk) == self.chunk_size
  342. await send({"type": "http.response.body", "body": chunk, "more_body": more_body})
  343. async def _handle_single_range(
  344. self, send: Send, start: int, end: int, file_size: int, send_header_only: bool
  345. ) -> None:
  346. self.headers["content-range"] = f"bytes {start}-{end - 1}/{file_size}"
  347. self.headers["content-length"] = str(end - start)
  348. await send({"type": "http.response.start", "status": 206, "headers": self.raw_headers})
  349. if send_header_only:
  350. await send({"type": "http.response.body", "body": b"", "more_body": False})
  351. else:
  352. async with await anyio.open_file(self.path, mode="rb") as file:
  353. await file.seek(start)
  354. more_body = True
  355. while more_body:
  356. chunk = await file.read(min(self.chunk_size, end - start))
  357. start += len(chunk)
  358. more_body = len(chunk) == self.chunk_size and start < end
  359. await send({"type": "http.response.body", "body": chunk, "more_body": more_body})
  360. async def _handle_multiple_ranges(
  361. self,
  362. send: Send,
  363. ranges: list[tuple[int, int]],
  364. file_size: int,
  365. send_header_only: bool,
  366. ) -> None:
  367. # In firefox and chrome, they use boundary with 95-96 bits entropy (that's roughly 13 bytes).
  368. boundary = token_hex(13)
  369. content_length, header_generator = self.generate_multipart(
  370. ranges, boundary, file_size, self.headers["content-type"]
  371. )
  372. self.headers["content-range"] = f"multipart/byteranges; boundary={boundary}"
  373. self.headers["content-length"] = str(content_length)
  374. await send({"type": "http.response.start", "status": 206, "headers": self.raw_headers})
  375. if send_header_only:
  376. await send({"type": "http.response.body", "body": b"", "more_body": False})
  377. else:
  378. async with await anyio.open_file(self.path, mode="rb") as file:
  379. for start, end in ranges:
  380. await send({"type": "http.response.body", "body": header_generator(start, end), "more_body": True})
  381. await file.seek(start)
  382. while start < end:
  383. chunk = await file.read(min(self.chunk_size, end - start))
  384. start += len(chunk)
  385. await send({"type": "http.response.body", "body": chunk, "more_body": True})
  386. await send({"type": "http.response.body", "body": b"\n", "more_body": True})
  387. await send(
  388. {
  389. "type": "http.response.body",
  390. "body": f"\n--{boundary}--\n".encode("latin-1"),
  391. "more_body": False,
  392. }
  393. )
  394. def _should_use_range(self, http_if_range: str) -> bool:
  395. return http_if_range == self.headers["last-modified"] or http_if_range == self.headers["etag"]
  396. @staticmethod
  397. def _parse_range_header(http_range: str, file_size: int) -> list[tuple[int, int]]:
  398. ranges: list[tuple[int, int]] = []
  399. try:
  400. units, range_ = http_range.split("=", 1)
  401. except ValueError:
  402. raise MalformedRangeHeader()
  403. units = units.strip().lower()
  404. if units != "bytes":
  405. raise MalformedRangeHeader("Only support bytes range")
  406. ranges = [
  407. (
  408. int(_[0]) if _[0] else file_size - int(_[1]),
  409. int(_[1]) + 1 if _[0] and _[1] and int(_[1]) < file_size else file_size,
  410. )
  411. for _ in _RANGE_PATTERN.findall(range_)
  412. if _ != ("", "")
  413. ]
  414. if len(ranges) == 0:
  415. raise MalformedRangeHeader("Range header: range must be requested")
  416. if any(not (0 <= start < file_size) for start, _ in ranges):
  417. raise RangeNotSatisfiable(file_size)
  418. if any(start > end for start, end in ranges):
  419. raise MalformedRangeHeader("Range header: start must be less than end")
  420. if len(ranges) == 1:
  421. return ranges
  422. # Merge ranges
  423. result: list[tuple[int, int]] = []
  424. for start, end in ranges:
  425. for p in range(len(result)):
  426. p_start, p_end = result[p]
  427. if start > p_end:
  428. continue
  429. elif end < p_start:
  430. result.insert(p, (start, end)) # THIS IS NOT REACHED!
  431. break
  432. else:
  433. result[p] = (min(start, p_start), max(end, p_end))
  434. break
  435. else:
  436. result.append((start, end))
  437. return result
  438. def generate_multipart(
  439. self,
  440. ranges: Sequence[tuple[int, int]],
  441. boundary: str,
  442. max_size: int,
  443. content_type: str,
  444. ) -> tuple[int, Callable[[int, int], bytes]]:
  445. r"""
  446. Multipart response headers generator.
  447. ```
  448. --{boundary}\n
  449. Content-Type: {content_type}\n
  450. Content-Range: bytes {start}-{end-1}/{max_size}\n
  451. \n
  452. ..........content...........\n
  453. --{boundary}\n
  454. Content-Type: {content_type}\n
  455. Content-Range: bytes {start}-{end-1}/{max_size}\n
  456. \n
  457. ..........content...........\n
  458. --{boundary}--\n
  459. ```
  460. """
  461. boundary_len = len(boundary)
  462. static_header_part_len = 44 + boundary_len + len(content_type) + len(str(max_size))
  463. content_length = sum(
  464. (len(str(start)) + len(str(end - 1)) + static_header_part_len) # Headers
  465. + (end - start) # Content
  466. for start, end in ranges
  467. ) + (
  468. 5 + boundary_len # --boundary--\n
  469. )
  470. return (
  471. content_length,
  472. lambda start, end: (
  473. f"--{boundary}\nContent-Type: {content_type}\nContent-Range: bytes {start}-{end - 1}/{max_size}\n\n"
  474. ).encode("latin-1"),
  475. )