web_response.py 26 KB

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