responses.py 20 KB

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