Skip to content
CryoCryo home
Stdlibnet::http

connection

import std::net::http::connection; · source

Types

Reading messages off a connection

implement struct BufferedStream<S> {
    async read_request(mut &this) -> Result<Request, IoError>
    where S: AsyncTransport;

    async read_response(mut &this) -> Result<Response, IoError>
    where S: AsyncTransport;
}

net::http::connection extends BufferedStream with the two message readers: request line or status line, then headers until the blank line, then the body. Both cap what they will accept — header lines at MAX_HEADER_LINE, the header block at MAX_HEADER_COUNT, bodies at MAX_BODY — and fail with InvalidData past that, or on a malformed line; a peer that closes mid-message is UnexpectedEof.

A body is framed by Content-Length or by the chunked transfer coding — size-prefixed chunks ending with an empty one and an optional trailer section, which is read and dropped. A message that names both is refused with InvalidData, since a peer and a proxy could each read it by a different one; a transfer coding other than chunked is Unsupported. A request with neither has no body. A response with neither runs to the end of the connection, except one that never has a body (1xx, 204, 304).

They are methods on the connection rather than free functions for a reason that recurs across the async layers: a parser written as a static taking mut &BufferedStream<S> cannot be called from an async caller holding the connection as a local, because handing the address of a local to a future that outlives the current poll is refused (E0455). Only the receiver is re-supplied on every poll, so the reader has to be a method. Writing needs no counterpart here: encoding is synchronous, so a message is assembled into pending() with encode_into and sent with one flush.

The server and client are written against these, and so can anything else that speaks HTTP/1.1 over a buffered transport — plaintext or TLS alike.