web_response.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  1. import asyncio
  2. import collections.abc
  3. import datetime
  4. import enum
  5. import json
  6. import math
  7. import time
  8. import warnings
  9. import zlib
  10. from concurrent.futures import Executor
  11. from http import HTTPStatus
  12. from http.cookies import SimpleCookie
  13. from typing import (
  14. TYPE_CHECKING,
  15. Any,
  16. Dict,
  17. Iterator,
  18. MutableMapping,
  19. Optional,
  20. Union,
  21. cast,
  22. )
  23. from multidict import CIMultiDict, istr
  24. from . import hdrs, payload
  25. from .abc import AbstractStreamWriter
  26. from .compression_utils import ZLibCompressor
  27. from .helpers import (
  28. ETAG_ANY,
  29. QUOTED_ETAG_RE,
  30. ETag,
  31. HeadersMixin,
  32. must_be_empty_body,
  33. parse_http_date,
  34. rfc822_formatted_time,
  35. sentinel,
  36. should_remove_content_length,
  37. validate_etag_value,
  38. )
  39. from .http import SERVER_SOFTWARE, HttpVersion10, HttpVersion11
  40. from .payload import Payload
  41. from .typedefs import JSONEncoder, LooseHeaders
  42. REASON_PHRASES = {http_status.value: http_status.phrase for http_status in HTTPStatus}
  43. LARGE_BODY_SIZE = 1024**2
  44. __all__ = ("ContentCoding", "StreamResponse", "Response", "json_response")
  45. if TYPE_CHECKING:
  46. from .web_request import BaseRequest
  47. BaseClass = MutableMapping[str, Any]
  48. else:
  49. BaseClass = collections.abc.MutableMapping
  50. # TODO(py311): Convert to StrEnum for wider use
  51. class ContentCoding(enum.Enum):
  52. # The content codings that we have support for.
  53. #
  54. # Additional registered codings are listed at:
  55. # https://www.iana.org/assignments/http-parameters/http-parameters.xhtml#content-coding
  56. deflate = "deflate"
  57. gzip = "gzip"
  58. identity = "identity"
  59. CONTENT_CODINGS = {coding.value: coding for coding in ContentCoding}
  60. ############################################################
  61. # HTTP Response classes
  62. ############################################################
  63. class StreamResponse(BaseClass, HeadersMixin):
  64. _body: Union[None, bytes, bytearray, Payload]
  65. _length_check = True
  66. _body = None
  67. _keep_alive: Optional[bool] = None
  68. _chunked: bool = False
  69. _compression: bool = False
  70. _compression_strategy: int = zlib.Z_DEFAULT_STRATEGY
  71. _compression_force: Optional[ContentCoding] = None
  72. _req: Optional["BaseRequest"] = None
  73. _payload_writer: Optional[AbstractStreamWriter] = None
  74. _eof_sent: bool = False
  75. _must_be_empty_body: Optional[bool] = None
  76. _body_length = 0
  77. _cookies: Optional[SimpleCookie] = None
  78. def __init__(
  79. self,
  80. *,
  81. status: int = 200,
  82. reason: Optional[str] = None,
  83. headers: Optional[LooseHeaders] = None,
  84. _real_headers: Optional[CIMultiDict[str]] = None,
  85. ) -> None:
  86. """Initialize a new stream response object.
  87. _real_headers is an internal parameter used to pass a pre-populated
  88. headers object. It is used by the `Response` class to avoid copying
  89. the headers when creating a new response object. It is not intended
  90. to be used by external code.
  91. """
  92. self._state: Dict[str, Any] = {}
  93. if _real_headers is not None:
  94. self._headers = _real_headers
  95. elif headers is not None:
  96. self._headers: CIMultiDict[str] = CIMultiDict(headers)
  97. else:
  98. self._headers = CIMultiDict()
  99. self._set_status(status, reason)
  100. @property
  101. def prepared(self) -> bool:
  102. return self._eof_sent or self._payload_writer is not None
  103. @property
  104. def task(self) -> "Optional[asyncio.Task[None]]":
  105. if self._req:
  106. return self._req.task
  107. else:
  108. return None
  109. @property
  110. def status(self) -> int:
  111. return self._status
  112. @property
  113. def chunked(self) -> bool:
  114. return self._chunked
  115. @property
  116. def compression(self) -> bool:
  117. return self._compression
  118. @property
  119. def reason(self) -> str:
  120. return self._reason
  121. def set_status(
  122. self,
  123. status: int,
  124. reason: Optional[str] = None,
  125. ) -> None:
  126. assert (
  127. not self.prepared
  128. ), "Cannot change the response status code after the headers have been sent"
  129. self._set_status(status, reason)
  130. def _set_status(self, status: int, reason: Optional[str]) -> None:
  131. self._status = int(status)
  132. if reason is None:
  133. reason = REASON_PHRASES.get(self._status, "")
  134. elif "\n" in reason:
  135. raise ValueError("Reason cannot contain \\n")
  136. self._reason = reason
  137. @property
  138. def keep_alive(self) -> Optional[bool]:
  139. return self._keep_alive
  140. def force_close(self) -> None:
  141. self._keep_alive = False
  142. @property
  143. def body_length(self) -> int:
  144. return self._body_length
  145. @property
  146. def output_length(self) -> int:
  147. warnings.warn("output_length is deprecated", DeprecationWarning)
  148. assert self._payload_writer
  149. return self._payload_writer.buffer_size
  150. def enable_chunked_encoding(self, chunk_size: Optional[int] = None) -> None:
  151. """Enables automatic chunked transfer encoding."""
  152. if hdrs.CONTENT_LENGTH in self._headers:
  153. raise RuntimeError(
  154. "You can't enable chunked encoding when a content length is set"
  155. )
  156. if chunk_size is not None:
  157. warnings.warn("Chunk size is deprecated #1615", DeprecationWarning)
  158. self._chunked = True
  159. def enable_compression(
  160. self,
  161. force: Optional[Union[bool, ContentCoding]] = None,
  162. strategy: int = zlib.Z_DEFAULT_STRATEGY,
  163. ) -> None:
  164. """Enables response compression encoding."""
  165. # Backwards compatibility for when force was a bool <0.17.
  166. if isinstance(force, bool):
  167. force = ContentCoding.deflate if force else ContentCoding.identity
  168. warnings.warn(
  169. "Using boolean for force is deprecated #3318", DeprecationWarning
  170. )
  171. elif force is not None:
  172. assert isinstance(
  173. force, ContentCoding
  174. ), "force should one of None, bool or ContentEncoding"
  175. self._compression = True
  176. self._compression_force = force
  177. self._compression_strategy = strategy
  178. @property
  179. def headers(self) -> "CIMultiDict[str]":
  180. return self._headers
  181. @property
  182. def cookies(self) -> SimpleCookie:
  183. if self._cookies is None:
  184. self._cookies = SimpleCookie()
  185. return self._cookies
  186. def set_cookie(
  187. self,
  188. name: str,
  189. value: str,
  190. *,
  191. expires: Optional[str] = None,
  192. domain: Optional[str] = None,
  193. max_age: Optional[Union[int, str]] = None,
  194. path: str = "/",
  195. secure: Optional[bool] = None,
  196. httponly: Optional[bool] = None,
  197. version: Optional[str] = None,
  198. samesite: Optional[str] = None,
  199. ) -> None:
  200. """Set or update response cookie.
  201. Sets new cookie or updates existent with new value.
  202. Also updates only those params which are not None.
  203. """
  204. if self._cookies is None:
  205. self._cookies = SimpleCookie()
  206. self._cookies[name] = value
  207. c = self._cookies[name]
  208. if expires is not None:
  209. c["expires"] = expires
  210. elif c.get("expires") == "Thu, 01 Jan 1970 00:00:00 GMT":
  211. del c["expires"]
  212. if domain is not None:
  213. c["domain"] = domain
  214. if max_age is not None:
  215. c["max-age"] = str(max_age)
  216. elif "max-age" in c:
  217. del c["max-age"]
  218. c["path"] = path
  219. if secure is not None:
  220. c["secure"] = secure
  221. if httponly is not None:
  222. c["httponly"] = httponly
  223. if version is not None:
  224. c["version"] = version
  225. if samesite is not None:
  226. c["samesite"] = samesite
  227. def del_cookie(
  228. self,
  229. name: str,
  230. *,
  231. domain: Optional[str] = None,
  232. path: str = "/",
  233. secure: Optional[bool] = None,
  234. httponly: Optional[bool] = None,
  235. samesite: Optional[str] = None,
  236. ) -> None:
  237. """Delete cookie.
  238. Creates new empty expired cookie.
  239. """
  240. # TODO: do we need domain/path here?
  241. if self._cookies is not None:
  242. self._cookies.pop(name, None)
  243. self.set_cookie(
  244. name,
  245. "",
  246. max_age=0,
  247. expires="Thu, 01 Jan 1970 00:00:00 GMT",
  248. domain=domain,
  249. path=path,
  250. secure=secure,
  251. httponly=httponly,
  252. samesite=samesite,
  253. )
  254. @property
  255. def content_length(self) -> Optional[int]:
  256. # Just a placeholder for adding setter
  257. return super().content_length
  258. @content_length.setter
  259. def content_length(self, value: Optional[int]) -> None:
  260. if value is not None:
  261. value = int(value)
  262. if self._chunked:
  263. raise RuntimeError(
  264. "You can't set content length when chunked encoding is enable"
  265. )
  266. self._headers[hdrs.CONTENT_LENGTH] = str(value)
  267. else:
  268. self._headers.pop(hdrs.CONTENT_LENGTH, None)
  269. @property
  270. def content_type(self) -> str:
  271. # Just a placeholder for adding setter
  272. return super().content_type
  273. @content_type.setter
  274. def content_type(self, value: str) -> None:
  275. self.content_type # read header values if needed
  276. self._content_type = str(value)
  277. self._generate_content_type_header()
  278. @property
  279. def charset(self) -> Optional[str]:
  280. # Just a placeholder for adding setter
  281. return super().charset
  282. @charset.setter
  283. def charset(self, value: Optional[str]) -> None:
  284. ctype = self.content_type # read header values if needed
  285. if ctype == "application/octet-stream":
  286. raise RuntimeError(
  287. "Setting charset for application/octet-stream "
  288. "doesn't make sense, setup content_type first"
  289. )
  290. assert self._content_dict is not None
  291. if value is None:
  292. self._content_dict.pop("charset", None)
  293. else:
  294. self._content_dict["charset"] = str(value).lower()
  295. self._generate_content_type_header()
  296. @property
  297. def last_modified(self) -> Optional[datetime.datetime]:
  298. """The value of Last-Modified HTTP header, or None.
  299. This header is represented as a `datetime` object.
  300. """
  301. return parse_http_date(self._headers.get(hdrs.LAST_MODIFIED))
  302. @last_modified.setter
  303. def last_modified(
  304. self, value: Optional[Union[int, float, datetime.datetime, str]]
  305. ) -> None:
  306. if value is None:
  307. self._headers.pop(hdrs.LAST_MODIFIED, None)
  308. elif isinstance(value, (int, float)):
  309. self._headers[hdrs.LAST_MODIFIED] = time.strftime(
  310. "%a, %d %b %Y %H:%M:%S GMT", time.gmtime(math.ceil(value))
  311. )
  312. elif isinstance(value, datetime.datetime):
  313. self._headers[hdrs.LAST_MODIFIED] = time.strftime(
  314. "%a, %d %b %Y %H:%M:%S GMT", value.utctimetuple()
  315. )
  316. elif isinstance(value, str):
  317. self._headers[hdrs.LAST_MODIFIED] = value
  318. @property
  319. def etag(self) -> Optional[ETag]:
  320. quoted_value = self._headers.get(hdrs.ETAG)
  321. if not quoted_value:
  322. return None
  323. elif quoted_value == ETAG_ANY:
  324. return ETag(value=ETAG_ANY)
  325. match = QUOTED_ETAG_RE.fullmatch(quoted_value)
  326. if not match:
  327. return None
  328. is_weak, value = match.group(1, 2)
  329. return ETag(
  330. is_weak=bool(is_weak),
  331. value=value,
  332. )
  333. @etag.setter
  334. def etag(self, value: Optional[Union[ETag, str]]) -> None:
  335. if value is None:
  336. self._headers.pop(hdrs.ETAG, None)
  337. elif (isinstance(value, str) and value == ETAG_ANY) or (
  338. isinstance(value, ETag) and value.value == ETAG_ANY
  339. ):
  340. self._headers[hdrs.ETAG] = ETAG_ANY
  341. elif isinstance(value, str):
  342. validate_etag_value(value)
  343. self._headers[hdrs.ETAG] = f'"{value}"'
  344. elif isinstance(value, ETag) and isinstance(value.value, str):
  345. validate_etag_value(value.value)
  346. hdr_value = f'W/"{value.value}"' if value.is_weak else f'"{value.value}"'
  347. self._headers[hdrs.ETAG] = hdr_value
  348. else:
  349. raise ValueError(
  350. f"Unsupported etag type: {type(value)}. "
  351. f"etag must be str, ETag or None"
  352. )
  353. def _generate_content_type_header(
  354. self, CONTENT_TYPE: istr = hdrs.CONTENT_TYPE
  355. ) -> None:
  356. assert self._content_dict is not None
  357. assert self._content_type is not None
  358. params = "; ".join(f"{k}={v}" for k, v in self._content_dict.items())
  359. if params:
  360. ctype = self._content_type + "; " + params
  361. else:
  362. ctype = self._content_type
  363. self._headers[CONTENT_TYPE] = ctype
  364. async def _do_start_compression(self, coding: ContentCoding) -> None:
  365. if coding is ContentCoding.identity:
  366. return
  367. assert self._payload_writer is not None
  368. self._headers[hdrs.CONTENT_ENCODING] = coding.value
  369. self._payload_writer.enable_compression(
  370. coding.value, self._compression_strategy
  371. )
  372. # Compressed payload may have different content length,
  373. # remove the header
  374. self._headers.popall(hdrs.CONTENT_LENGTH, None)
  375. async def _start_compression(self, request: "BaseRequest") -> None:
  376. if self._compression_force:
  377. await self._do_start_compression(self._compression_force)
  378. return
  379. # Encoding comparisons should be case-insensitive
  380. # https://www.rfc-editor.org/rfc/rfc9110#section-8.4.1
  381. accept_encoding = request.headers.get(hdrs.ACCEPT_ENCODING, "").lower()
  382. for value, coding in CONTENT_CODINGS.items():
  383. if value in accept_encoding:
  384. await self._do_start_compression(coding)
  385. return
  386. async def prepare(self, request: "BaseRequest") -> Optional[AbstractStreamWriter]:
  387. if self._eof_sent:
  388. return None
  389. if self._payload_writer is not None:
  390. return self._payload_writer
  391. self._must_be_empty_body = must_be_empty_body(request.method, self.status)
  392. return await self._start(request)
  393. async def _start(self, request: "BaseRequest") -> AbstractStreamWriter:
  394. self._req = request
  395. writer = self._payload_writer = request._payload_writer
  396. await self._prepare_headers()
  397. await request._prepare_hook(self)
  398. await self._write_headers()
  399. return writer
  400. async def _prepare_headers(self) -> None:
  401. request = self._req
  402. assert request is not None
  403. writer = self._payload_writer
  404. assert writer is not None
  405. keep_alive = self._keep_alive
  406. if keep_alive is None:
  407. keep_alive = request.keep_alive
  408. self._keep_alive = keep_alive
  409. version = request.version
  410. headers = self._headers
  411. if self._cookies:
  412. for cookie in self._cookies.values():
  413. value = cookie.output(header="")[1:]
  414. headers.add(hdrs.SET_COOKIE, value)
  415. if self._compression:
  416. await self._start_compression(request)
  417. if self._chunked:
  418. if version != HttpVersion11:
  419. raise RuntimeError(
  420. "Using chunked encoding is forbidden "
  421. "for HTTP/{0.major}.{0.minor}".format(request.version)
  422. )
  423. if not self._must_be_empty_body:
  424. writer.enable_chunking()
  425. headers[hdrs.TRANSFER_ENCODING] = "chunked"
  426. elif self._length_check: # Disabled for WebSockets
  427. writer.length = self.content_length
  428. if writer.length is None:
  429. if version >= HttpVersion11:
  430. if not self._must_be_empty_body:
  431. writer.enable_chunking()
  432. headers[hdrs.TRANSFER_ENCODING] = "chunked"
  433. elif not self._must_be_empty_body:
  434. keep_alive = False
  435. # HTTP 1.1: https://tools.ietf.org/html/rfc7230#section-3.3.2
  436. # HTTP 1.0: https://tools.ietf.org/html/rfc1945#section-10.4
  437. if self._must_be_empty_body:
  438. if hdrs.CONTENT_LENGTH in headers and should_remove_content_length(
  439. request.method, self.status
  440. ):
  441. del headers[hdrs.CONTENT_LENGTH]
  442. # https://datatracker.ietf.org/doc/html/rfc9112#section-6.1-10
  443. # https://datatracker.ietf.org/doc/html/rfc9112#section-6.1-13
  444. if hdrs.TRANSFER_ENCODING in headers:
  445. del headers[hdrs.TRANSFER_ENCODING]
  446. elif (writer.length if self._length_check else self.content_length) != 0:
  447. # https://www.rfc-editor.org/rfc/rfc9110#section-8.3-5
  448. headers.setdefault(hdrs.CONTENT_TYPE, "application/octet-stream")
  449. headers.setdefault(hdrs.DATE, rfc822_formatted_time())
  450. headers.setdefault(hdrs.SERVER, SERVER_SOFTWARE)
  451. # connection header
  452. if hdrs.CONNECTION not in headers:
  453. if keep_alive:
  454. if version == HttpVersion10:
  455. headers[hdrs.CONNECTION] = "keep-alive"
  456. elif version == HttpVersion11:
  457. headers[hdrs.CONNECTION] = "close"
  458. async def _write_headers(self) -> None:
  459. request = self._req
  460. assert request is not None
  461. writer = self._payload_writer
  462. assert writer is not None
  463. # status line
  464. version = request.version
  465. status_line = f"HTTP/{version[0]}.{version[1]} {self._status} {self._reason}"
  466. await writer.write_headers(status_line, self._headers)
  467. async def write(self, data: Union[bytes, bytearray, memoryview]) -> None:
  468. assert isinstance(
  469. data, (bytes, bytearray, memoryview)
  470. ), "data argument must be byte-ish (%r)" % type(data)
  471. if self._eof_sent:
  472. raise RuntimeError("Cannot call write() after write_eof()")
  473. if self._payload_writer is None:
  474. raise RuntimeError("Cannot call write() before prepare()")
  475. await self._payload_writer.write(data)
  476. async def drain(self) -> None:
  477. assert not self._eof_sent, "EOF has already been sent"
  478. assert self._payload_writer is not None, "Response has not been started"
  479. warnings.warn(
  480. "drain method is deprecated, use await resp.write()",
  481. DeprecationWarning,
  482. stacklevel=2,
  483. )
  484. await self._payload_writer.drain()
  485. async def write_eof(self, data: bytes = b"") -> None:
  486. assert isinstance(
  487. data, (bytes, bytearray, memoryview)
  488. ), "data argument must be byte-ish (%r)" % type(data)
  489. if self._eof_sent:
  490. return
  491. assert self._payload_writer is not None, "Response has not been started"
  492. await self._payload_writer.write_eof(data)
  493. self._eof_sent = True
  494. self._req = None
  495. self._body_length = self._payload_writer.output_size
  496. self._payload_writer = None
  497. def __repr__(self) -> str:
  498. if self._eof_sent:
  499. info = "eof"
  500. elif self.prepared:
  501. assert self._req is not None
  502. info = f"{self._req.method} {self._req.path} "
  503. else:
  504. info = "not prepared"
  505. return f"<{self.__class__.__name__} {self.reason} {info}>"
  506. def __getitem__(self, key: str) -> Any:
  507. return self._state[key]
  508. def __setitem__(self, key: str, value: Any) -> None:
  509. self._state[key] = value
  510. def __delitem__(self, key: str) -> None:
  511. del self._state[key]
  512. def __len__(self) -> int:
  513. return len(self._state)
  514. def __iter__(self) -> Iterator[str]:
  515. return iter(self._state)
  516. def __hash__(self) -> int:
  517. return hash(id(self))
  518. def __eq__(self, other: object) -> bool:
  519. return self is other
  520. class Response(StreamResponse):
  521. _compressed_body: Optional[bytes] = None
  522. def __init__(
  523. self,
  524. *,
  525. body: Any = None,
  526. status: int = 200,
  527. reason: Optional[str] = None,
  528. text: Optional[str] = None,
  529. headers: Optional[LooseHeaders] = None,
  530. content_type: Optional[str] = None,
  531. charset: Optional[str] = None,
  532. zlib_executor_size: Optional[int] = None,
  533. zlib_executor: Optional[Executor] = None,
  534. ) -> None:
  535. if body is not None and text is not None:
  536. raise ValueError("body and text are not allowed together")
  537. if headers is None:
  538. real_headers: CIMultiDict[str] = CIMultiDict()
  539. else:
  540. real_headers = CIMultiDict(headers)
  541. if content_type is not None and "charset" in content_type:
  542. raise ValueError("charset must not be in content_type argument")
  543. if text is not None:
  544. if hdrs.CONTENT_TYPE in real_headers:
  545. if content_type or charset:
  546. raise ValueError(
  547. "passing both Content-Type header and "
  548. "content_type or charset params "
  549. "is forbidden"
  550. )
  551. else:
  552. # fast path for filling headers
  553. if not isinstance(text, str):
  554. raise TypeError("text argument must be str (%r)" % type(text))
  555. if content_type is None:
  556. content_type = "text/plain"
  557. if charset is None:
  558. charset = "utf-8"
  559. real_headers[hdrs.CONTENT_TYPE] = content_type + "; charset=" + charset
  560. body = text.encode(charset)
  561. text = None
  562. elif hdrs.CONTENT_TYPE in real_headers:
  563. if content_type is not None or charset is not None:
  564. raise ValueError(
  565. "passing both Content-Type header and "
  566. "content_type or charset params "
  567. "is forbidden"
  568. )
  569. elif content_type is not None:
  570. if charset is not None:
  571. content_type += "; charset=" + charset
  572. real_headers[hdrs.CONTENT_TYPE] = content_type
  573. super().__init__(status=status, reason=reason, _real_headers=real_headers)
  574. if text is not None:
  575. self.text = text
  576. else:
  577. self.body = body
  578. self._zlib_executor_size = zlib_executor_size
  579. self._zlib_executor = zlib_executor
  580. @property
  581. def body(self) -> Optional[Union[bytes, Payload]]:
  582. return self._body
  583. @body.setter
  584. def body(self, body: Any) -> None:
  585. if body is None:
  586. self._body = None
  587. elif isinstance(body, (bytes, bytearray)):
  588. self._body = body
  589. else:
  590. try:
  591. self._body = body = payload.PAYLOAD_REGISTRY.get(body)
  592. except payload.LookupError:
  593. raise ValueError("Unsupported body type %r" % type(body))
  594. headers = self._headers
  595. # set content-type
  596. if hdrs.CONTENT_TYPE not in headers:
  597. headers[hdrs.CONTENT_TYPE] = body.content_type
  598. # copy payload headers
  599. if body.headers:
  600. for key, value in body.headers.items():
  601. if key not in headers:
  602. headers[key] = value
  603. self._compressed_body = None
  604. @property
  605. def text(self) -> Optional[str]:
  606. if self._body is None:
  607. return None
  608. return self._body.decode(self.charset or "utf-8")
  609. @text.setter
  610. def text(self, text: str) -> None:
  611. assert text is None or isinstance(
  612. text, str
  613. ), "text argument must be str (%r)" % type(text)
  614. if self.content_type == "application/octet-stream":
  615. self.content_type = "text/plain"
  616. if self.charset is None:
  617. self.charset = "utf-8"
  618. self._body = text.encode(self.charset)
  619. self._compressed_body = None
  620. @property
  621. def content_length(self) -> Optional[int]:
  622. if self._chunked:
  623. return None
  624. if hdrs.CONTENT_LENGTH in self._headers:
  625. return int(self._headers[hdrs.CONTENT_LENGTH])
  626. if self._compressed_body is not None:
  627. # Return length of the compressed body
  628. return len(self._compressed_body)
  629. elif isinstance(self._body, Payload):
  630. # A payload without content length, or a compressed payload
  631. return None
  632. elif self._body is not None:
  633. return len(self._body)
  634. else:
  635. return 0
  636. @content_length.setter
  637. def content_length(self, value: Optional[int]) -> None:
  638. raise RuntimeError("Content length is set automatically")
  639. async def write_eof(self, data: bytes = b"") -> None:
  640. if self._eof_sent:
  641. return
  642. if self._compressed_body is None:
  643. body: Optional[Union[bytes, Payload]] = self._body
  644. else:
  645. body = self._compressed_body
  646. assert not data, f"data arg is not supported, got {data!r}"
  647. assert self._req is not None
  648. assert self._payload_writer is not None
  649. if body is None or self._must_be_empty_body:
  650. await super().write_eof()
  651. elif isinstance(self._body, Payload):
  652. await self._body.write(self._payload_writer)
  653. await super().write_eof()
  654. else:
  655. await super().write_eof(cast(bytes, body))
  656. async def _start(self, request: "BaseRequest") -> AbstractStreamWriter:
  657. if hdrs.CONTENT_LENGTH in self._headers:
  658. if should_remove_content_length(request.method, self.status):
  659. del self._headers[hdrs.CONTENT_LENGTH]
  660. elif not self._chunked:
  661. if isinstance(self._body, Payload):
  662. if self._body.size is not None:
  663. self._headers[hdrs.CONTENT_LENGTH] = str(self._body.size)
  664. else:
  665. body_len = len(self._body) if self._body else "0"
  666. # https://www.rfc-editor.org/rfc/rfc9110.html#section-8.6-7
  667. if body_len != "0" or (
  668. self.status != 304 and request.method not in hdrs.METH_HEAD_ALL
  669. ):
  670. self._headers[hdrs.CONTENT_LENGTH] = str(body_len)
  671. return await super()._start(request)
  672. async def _do_start_compression(self, coding: ContentCoding) -> None:
  673. if self._chunked or isinstance(self._body, Payload):
  674. return await super()._do_start_compression(coding)
  675. if coding is ContentCoding.identity:
  676. return
  677. # Instead of using _payload_writer.enable_compression,
  678. # compress the whole body
  679. compressor = ZLibCompressor(
  680. encoding=coding.value,
  681. max_sync_chunk_size=self._zlib_executor_size,
  682. executor=self._zlib_executor,
  683. )
  684. assert self._body is not None
  685. if self._zlib_executor_size is None and len(self._body) > LARGE_BODY_SIZE:
  686. warnings.warn(
  687. "Synchronous compression of large response bodies "
  688. f"({len(self._body)} bytes) might block the async event loop. "
  689. "Consider providing a custom value to zlib_executor_size/"
  690. "zlib_executor response properties or disabling compression on it."
  691. )
  692. self._compressed_body = (
  693. await compressor.compress(self._body) + compressor.flush()
  694. )
  695. self._headers[hdrs.CONTENT_ENCODING] = coding.value
  696. self._headers[hdrs.CONTENT_LENGTH] = str(len(self._compressed_body))
  697. def json_response(
  698. data: Any = sentinel,
  699. *,
  700. text: Optional[str] = None,
  701. body: Optional[bytes] = None,
  702. status: int = 200,
  703. reason: Optional[str] = None,
  704. headers: Optional[LooseHeaders] = None,
  705. content_type: str = "application/json",
  706. dumps: JSONEncoder = json.dumps,
  707. ) -> Response:
  708. if data is not sentinel:
  709. if text or body:
  710. raise ValueError("only one of data, text, or body should be specified")
  711. else:
  712. text = dumps(data)
  713. return Response(
  714. text=text,
  715. body=body,
  716. status=status,
  717. reason=reason,
  718. headers=headers,
  719. content_type=content_type,
  720. )