Struct Builder

struct Builder { ... }

A configuration builder for HTTP/1 server connections.

Note: The default values of options are not considered stable. They are subject to change at any time.

Example

# use std::time::Duration;
# use hyper::server::conn::http1::Builder;
# fn main() {
let mut http = Builder::new();
// Set options one at a time
http.half_close(false);

// Or, chain multiple options
http.keep_alive(false).title_case_headers(true).max_buf_size(8192);

# }

Use Builder::serve_connection to bind the built connection to a service.

Implementations

impl Builder

fn new() -> Self

Create a new connection builder.

fn half_close(self: &mut Self, val: bool) -> &mut Self

Set whether HTTP/1 connections should support half-closures.

Clients can chose to shutdown their write-side while waiting for the server to respond. Setting this to true will prevent closing the connection immediately if read detects an EOF in the middle of a request.

Default is false.

fn keep_alive(self: &mut Self, val: bool) -> &mut Self

Enables or disables HTTP/1 keep-alive.

Default is true.

fn title_case_headers(self: &mut Self, enabled: bool) -> &mut Self

Set whether HTTP/1 connections will write header names as title case at the socket level.

Default is false.

fn ignore_invalid_headers(self: &mut Self, enabled: bool) -> &mut Builder

Set whether HTTP/1 connections will silently ignored malformed header lines.

If this is enabled and a header line does not start with a valid header name, or does not include a colon at all, the line will be silently ignored and no error will be reported.

Default is false.

fn preserve_header_case(self: &mut Self, enabled: bool) -> &mut Self

Set whether to support preserving original header cases.

Currently, this will record the original cases received, and store them in a private extension on the Request. It will also look for and use such an extension in any provided Response.

Since the relevant extension is still private, there is no way to interact with the original cases. The only effect this can have now is to forward the cases in a proxy-like fashion.

Default is false.

fn max_headers(self: &mut Self, val: usize) -> &mut Self

Set the maximum number of headers.

When a request is received, the parser will reserve a buffer to store headers for optimal performance.

If server receives more headers than the buffer size, it responds to the client with "431 Request Header Fields Too Large".

Note that headers is allocated on the stack by default, which has higher performance. After setting this value, headers will be allocated in heap memory, that is, heap memory allocation will occur for each request, and there will be a performance drop of about 5%.

Default is 100.

fn header_read_timeout<impl Into<Option<Duration>>: Into<Option<Duration>>>(self: &mut Self, read_timeout: impl Into<Option<Duration>>) -> &mut Self

Set a timeout for reading client request headers. If a client does not transmit the entire header within this time, the connection is closed.

Requires a Timer set by Builder::timer to take effect. Panics if header_read_timeout is configured without a Timer.

Pass None to disable.

Default is 30 seconds.

fn writev(self: &mut Self, val: bool) -> &mut Self

Set whether HTTP/1 connections should try to use vectored writes, or always flatten into a single buffer.

Note that setting this to false may mean more copies of body data, but may also improve performance when an IO transport doesn't support vectored writes well, such as most TLS implementations.

Setting this to true will force hyper to use queued strategy which may eliminate unnecessary cloning on some TLS backends

Default is auto. In this mode hyper will try to guess which mode to use

fn max_buf_size(self: &mut Self, max: usize) -> &mut Self

Set the maximum buffer size for the connection.

Default is ~400kb.

Panics

The minimum value allowed is 8192. This method panics if the passed max is less than the minimum.

fn auto_date_header(self: &mut Self, enabled: bool) -> &mut Self

Set whether the date header should be included in HTTP responses.

Note that including the date header is recommended by RFC 7231.

Default is true.

fn pipeline_flush(self: &mut Self, enabled: bool) -> &mut Self

Aggregates flushes to better support pipelined responses.

Experimental, may have bugs.

Default is false.

fn timer<M>(self: &mut Self, timer: M) -> &mut Self
where
    M: Timer + Send + Sync + 'static

Set the timer used in background tasks.

fn serve_connection<I, S>(self: &Self, io: I, service: S) -> Connection<I, S>
where
    S: HttpService<IncomingBody>,
    <S as >::Error: Into<Box<dyn StdError + Send + Sync>>,
    <S as >::ResBody: 'static,
    <<S as >::ResBody as Body>::Error: Into<Box<dyn StdError + Send + Sync>>,
    I: Read + Write + Unpin

Bind a connection together with a Service.

This returns a Future that must be polled in order for HTTP to be driven on the connection.

Panics

If a timeout option has been configured, but a timer has not been provided, calling serve_connection will panic.

Example

# use hyper::{body::Incoming, Request, Response};
# use hyper::service::Service;
# use hyper::server::conn::http1::Builder;
# use hyper::rt::{Read, Write};
# async fn run<I, S>(some_io: I, some_service: S)
# where
#     I: Read + Write + Unpin + Send + 'static,
#     S: Service<hyper::Request<Incoming>, Response=hyper::Response<Incoming>> + Send + 'static,
#     S::Error: Into<Box<dyn std::error::Error + Send + Sync>>,
#     S::Future: Send,
# {
let http = Builder::new();
let conn = http.serve_connection(some_io, some_service);

if let Err(e) = conn.await {
    eprintln!("server connection error: {}", e);
}
# }
# fn main() {}

impl Clone for Builder

fn clone(self: &Self) -> Builder

impl Debug for Builder

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl Freeze for Builder

impl RefUnwindSafe for Builder

impl Send for Builder

impl Sync for Builder

impl Unpin for Builder

impl UnwindSafe for Builder

impl<T> Any for Builder

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for Builder

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for Builder

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for Builder

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> From for Builder

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> Instrument for Builder

impl<T> ToOwned for Builder

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> WithSubscriber for Builder

impl<T, U> Into for Builder

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for Builder

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for Builder

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>