# `tornado.http1connection` – HTTP/1.x client/server implementation

Client and server implementations of HTTP/1.x.

Added in version 4.0.

class tornado.http1connection.HTTP1ConnectionParameters(no_keep_alive: bool = False, chunk_size: int | None = None, max_header_size: int | None = None, header_timeout: float | None = None, max_body_size: int | None = None, body_timeout: float | None = None, decompress: bool = False)

  Parameters for `HTTP1Connection` and `HTTP1ServerConnection`.

  Parameters:

    - no_keep_alive (bool) – If true, always close the connection after one request.

    - chunk_size (int) – how much data to read into memory at once

    - max_header_size (int) – maximum amount of data for HTTP headers

    - header_timeout (float) – how long to wait for all headers (seconds)

    - max_body_size (int) – maximum amount of data for body

    - body_timeout (float) – how long to wait while reading body (seconds)

    - decompress (bool) – if true, decode incoming `Content-Encoding: gzip`

class tornado.http1connection.HTTP1Connection(stream: IOStream, is_client: bool, params: HTTP1ConnectionParameters | None = None, context: object | None = None)

  Implements the HTTP/1.x protocol.

  This class can be on its own for clients, or via `HTTP1ServerConnection` for servers.

  Parameters:

    - stream – an `IOStream`

    - is_client (bool) – client or server

    - params – a `HTTP1ConnectionParameters` instance or `None`

    - context – an opaque application-defined object that can be accessed as `connection.context`.

  read_response(delegate: HTTPMessageDelegate) → Awaitable[bool]

    Read a single HTTP response.

    Typical client-mode usage is to write a request using `write_headers`, `write`, and `finish`, and then call `read_response`.

    Parameters:

      delegate – a `HTTPMessageDelegate`

    Returns a `Future` that resolves to a bool after the full response has been read. The result is true if the stream is still open.

  set_close_callback(callback: Callable[[], None] | None) → None

    Sets a callback that will be run when the connection is closed.

    Note that this callback is slightly different from `HTTPMessageDelegate.on_connection_close`: The `HTTPMessageDelegate` method is called when the connection is closed while receiving a message. This callback is used when there is not an active delegate (for example, on the server side this callback is used if the client closes the connection after sending its request but before receiving all the response.

  detach() → IOStream

    Take control of the underlying stream.

    Returns the underlying `IOStream` object and stops all further HTTP processing. May only be called during `HTTPMessageDelegate.headers_received`. Intended for implementing protocols like websockets that tunnel over an HTTP handshake.

  set_body_timeout(timeout: float) → None

    Sets the body timeout for a single request.

    Overrides the value from `HTTP1ConnectionParameters`.

  set_max_body_size(max_body_size: int) → None

    Sets the body size limit for a single request.

    Overrides the value from `HTTP1ConnectionParameters`.

  write_headers(start_line: RequestStartLine | ResponseStartLine, headers: HTTPHeaders, chunk: bytes | None = None) → Future[None]

    Implements `HTTPConnection.write_headers`.

  write(chunk: bytes) → Future[None]

    Implements `HTTPConnection.write`.

    For backwards compatibility it is allowed but deprecated to skip `write_headers` and instead call `write()` with a pre-encoded header block.

  finish() → None

    Implements `HTTPConnection.finish`.

class tornado.http1connection.HTTP1ServerConnection(stream: IOStream, params: HTTP1ConnectionParameters | None = None, context: object | None = None)

  An HTTP/1.x server.

  Parameters:

    - stream – an `IOStream`

    - params – a `HTTP1ConnectionParameters` or None

    - context – an opaque application-defined object that is accessible as `connection.context`

  async close() → None

    Closes the connection.

    Returns a `Future` that resolves after the serving loop has exited.

  start_serving(delegate: HTTPServerConnectionDelegate) → None

    Starts serving requests on this connection.

    Parameters:

      delegate – a `HTTPServerConnectionDelegate`

tornado.http1connection.parse_int(s: str) → int

  Parse a non-negative integer from a string.

tornado.http1connection.parse_hex_int(s: str) → int

  Parse a non-negative hexadecimal integer from a string.

tornado.http1connection.is_transfer_encoding_chunked(headers: HTTPHeaders) → bool

  Returns true if the headers specify Transfer-Encoding: chunked.

  Raise httputil.HTTPInputError if any other transfer encoding is used.