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.
implement struct Name { ... }
implement trait Trait for Type { ... }
implement u64 { ... }Adds methods to a type outside its declaration. implement struct T (or enum, or a bare primitive) opens an inherent block; implement trait Tr for T supplies a trait's methods for one type. Resolution is at compile time - no dynamic dispatch.
implement struct Name { ... }Names the struct an inherent block extends. The methods inside are interchangeable with ones declared in the struct body.
language referencetype struct BufferedStream<S> {}A buffered connection for asynchronous code: it owns its transport S (a socket or a TLS session) and buffers both directions, so many small reads and writes become few large ones. It implements AsyncRead and AsyncWrite; make one with of.
type parameter Sasync function name(param: Type) -> T { ... }Compiles the function into a stackless state machine. Calling it builds a future whose output is the declared return type; nothing runs until that future is polled. Allowed on free functions, methods, and trait methods.
language referenceasync BufferedStream::read_request(mut &this) -> Result<Request, IoError>
where S: AsyncTransportRead one complete request: request line, headers, and the body as the module note describes. A peer that closes mid-message is UnexpectedEof; one that sends a malformed line or framing is InvalidData; a transfer coding other than chunked is Unsupported.
mut &this
mut &TypeAn exclusive, mutating borrow. As a receiver, mut &this lets the method modify the fields - and callers can see that from the signature alone. As a parameter type, mut &T lets the callee mutate through the reference while the caller keeps ownership.
&this
mut &this
thisThe receiver of a method. Its first parameter declares how the method takes it: &this borrows read-only, mut &this borrows exclusively, and this / mut this move the value in and consume it. In the body, this.field reaches the members.
type enum Result<T, E> with [Eq, Clone, Debug] {
Ok(T);
Err(E);
}The outcome of an operation that can fail: Ok(value) on success, Err(error) on failure.
type struct Request with [Clone] {
method: Method;
path: String;
version: String;
headers: Headers;
body: Array<u8>;
params: HashMap<String, String>;
}An HTTP/1.1 request: method, target path, version text, headers, body, and the path parameters a Router captured. A server receives one from BufferedStream::read_request; a client builds one with new and the with_* builders and writes it with encode_into.
type struct IoError {
kind: IoErrorKind;
os_code: i32;
}The error of a failed I/O operation: its kind, the operating system's own code, and, when the operation named one, what it was doing and the path. It prints as could not open config.toml: not found.
where T: Ord + Clone, U: HashConstrains generic parameters to types that implement the listed traits, which is what makes those traits' methods callable on T. Bounds on one parameter join with +; parameters separate with ,. A where on a single method narrows the type's parameter for that method only.
type parameter Stype trait AsyncTransport {
async read_into(mut &this, buf: Array<u8>) -> Transfer;
async write_from(mut &this, buf: Array<u8>) -> Transfer;
}One raw, unbuffered async byte channel: what BufferedStream<S> needs of a transport, and nothing more.
async BufferedStream::read_response(mut &this) -> Result<Response, IoError>
where S: AsyncTransportRead one complete response, framed the same way.
type struct Response {
status: StatusCode;
version: String;
headers: Headers;
body: Array<u8>;
}An HTTP/1.1 response: status, version text, headers and body. A handler builds one with new, text or json; a client receives one from BufferedStream::read_response. encode_into writes the status line from status and its reason phrase, always as HTTP/1.1.
import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
namespace std::net::http::connection;HTTP/1.1 message framing over a buffered async connection.
module pageIoErrorKind::InvalidDataThe bytes do not have the form the operation needs, such as text that is not UTF-8.
std::io::errorIoErrorKind::UnsupportedThe operation is recognized but not implemented on this build (e.g. a feature stubbed pending implementation, or a platform the stdlib doesn't support yet). Distinct from InvalidInput (caller's fault) and Other (unmapped errno).