def get_headers( self, version="1.1", keep_alive=False, keep_alive_timeout=None, body=b"", ): """.. deprecated:: 20.3: This function is not public API and will be removed.""" # self.headers get priority over content_type if self.content_type and "Content-Type" not in self.headers: self.headers["Content-Type"] = self.content_type if keep_alive: self.headers["Connection"] = "keep-alive" if keep_alive_timeout is not None: self.headers["Keep-Alive"] = keep_alive_timeout else: self.headers["Connection"] = "close" if self.status in (304, 412): self.headers = remove_entity_headers(self.headers) return format_http1_response(self.status, self.headers.items(), body)
async def http1_response_header(self, data: bytes, end_stream: bool) -> None: res = self.response # Compatibility with simple response body if not data and getattr(res, "body", None): data, end_stream = res.body, True # type: ignore size = len(data) headers = res.headers status = res.status if not isinstance(status, int) or status < 200: raise RuntimeError(f"Invalid response status {status!r}") if not has_message_body(status): # Header-only response status self.response_func = None if (data or not end_stream or "content-length" in headers or "transfer-encoding" in headers): data, size, end_stream = b"", 0, True headers.pop("content-length", None) headers.pop("transfer-encoding", None) logger.warning( f"Message body set in response on {self.request.path}. " f"A {status} response may only have headers, no body.") elif self.head_only and "content-length" in headers: self.response_func = None elif end_stream: # Non-streaming response (all in one block) headers["content-length"] = size self.response_func = None elif "content-length" in headers: # Streaming response with size known in advance self.response_bytes_left = int(headers["content-length"]) - size self.response_func = self.http1_response_normal else: # Length not known, use chunked encoding headers["transfer-encoding"] = "chunked" data = b"%x\r\n%b\r\n" % (size, data) if size else b"" self.response_func = self.http1_response_chunked if self.head_only: # Head request: don't send body data = b"" self.response_func = self.head_response_ignored headers["connection"] = "keep-alive" if self.keep_alive else "close" ret = format_http1_response(status, res.processed_headers) if data: ret += data # Send a 100-continue if expected and not Expectation Failed if self.expecting_continue: self.expecting_continue = False if status != 417: ret = HTTP_CONTINUE + ret # Send response if self.protocol.access_log: self.log_response() await self._send(ret) self.stage = Stage.IDLE if end_stream else Stage.RESPONSE