responses.py 20 KB

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