Implements `.HTTPConnection.write_headers`.
(
self,
start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine],
headers: httputil.HTTPHeaders,
chunk: Optional[bytes] = None,
)
| 377 | self._max_body_size = max_body_size |
| 378 | |
| 379 | def write_headers( |
| 380 | self, |
| 381 | start_line: Union[httputil.RequestStartLine, httputil.ResponseStartLine], |
| 382 | headers: httputil.HTTPHeaders, |
| 383 | chunk: Optional[bytes] = None, |
| 384 | ) -> "Future[None]": |
| 385 | """Implements `.HTTPConnection.write_headers`.""" |
| 386 | lines = [] |
| 387 | if self.is_client: |
| 388 | assert isinstance(start_line, httputil.RequestStartLine) |
| 389 | self._request_start_line = start_line |
| 390 | lines.append(utf8("%s %s HTTP/1.1" % (start_line[0], start_line[1]))) |
| 391 | # Client requests with a non-empty body must have either a |
| 392 | # Content-Length or a Transfer-Encoding. |
| 393 | self._chunking_output = ( |
| 394 | start_line.method in ("POST", "PUT", "PATCH") |
| 395 | and "Content-Length" not in headers |
| 396 | and ( |
| 397 | "Transfer-Encoding" not in headers |
| 398 | or headers["Transfer-Encoding"] == "chunked" |
| 399 | ) |
| 400 | ) |
| 401 | else: |
| 402 | assert isinstance(start_line, httputil.ResponseStartLine) |
| 403 | assert self._request_start_line is not None |
| 404 | assert self._request_headers is not None |
| 405 | self._response_start_line = start_line |
| 406 | lines.append(utf8("HTTP/1.1 %d %s" % (start_line[1], start_line[2]))) |
| 407 | self._chunking_output = ( |
| 408 | # TODO: should this use |
| 409 | # self._request_start_line.version or |
| 410 | # start_line.version? |
| 411 | self._request_start_line.version == "HTTP/1.1" |
| 412 | # Omit payload header field for HEAD request. |
| 413 | and self._request_start_line.method != "HEAD" |
| 414 | # 1xx, 204 and 304 responses have no body (not even a zero-length |
| 415 | # body), and so should not have either Content-Length or |
| 416 | # Transfer-Encoding headers. |
| 417 | and start_line.code not in (204, 304) |
| 418 | and (start_line.code < 100 or start_line.code >= 200) |
| 419 | # No need to chunk the output if a Content-Length is specified. |
| 420 | and "Content-Length" not in headers |
| 421 | # Applications are discouraged from touching Transfer-Encoding, |
| 422 | # but if they do, leave it alone. |
| 423 | and "Transfer-Encoding" not in headers |
| 424 | ) |
| 425 | # If connection to a 1.1 client will be closed, inform client |
| 426 | if ( |
| 427 | self._request_start_line.version == "HTTP/1.1" |
| 428 | and self._disconnect_on_finish |
| 429 | ): |
| 430 | headers["Connection"] = "close" |
| 431 | # If a 1.0 client asked for keep-alive, add the header. |
| 432 | if ( |
| 433 | self._request_start_line.version == "HTTP/1.0" |
| 434 | and self._request_headers.get("Connection", "").lower() == "keep-alive" |
| 435 | ): |
| 436 | headers["Connection"] = "Keep-Alive" |
nothing calls this directly
no test coverage detected