Skip to main content

HttpServiceConfig

Struct HttpServiceConfig 

Source
pub struct HttpServiceConfig { /* private fields */ }
Expand description

Configuration shared by HTTP/1 and HTTP/2 server services.

The default configuration enables persistent HTTP/1 connections with a five-second idle timeout, allows 96 headers, limits the request or status line to 16 KiB and the message-head buffer to 64 KiB, and applies a one-second initial request-header timeout.

Implementations§

Source§

impl HttpServiceConfig

Source

pub fn new() -> HttpServiceConfig

Creates an HTTP service configuration with default settings.

Source

pub fn set_max_headers(self, val: u16) -> Self

Sets the maximum number of headers in a message.

Every header line counts, including repeated header names.

Requests exceeding this limit are rejected with 431 Request Header Fields Too Large. The default is 96.

Source

pub fn set_max_buf_size(self, val: usize) -> Self

Sets the maximum cumulative size of an HTTP message head.

The request or response line, headers, and terminating empty line may occupy up to and including this number of bytes. Larger message heads are rejected. The default is 64 KiB.

Source

pub fn set_max_start_line_size(self, val: usize) -> Self

Sets the maximum size of an HTTP/1 request or status line.

The line, including its line end, may occupy up to and including this number of bytes. Requests with a longer request line are rejected with 414 URI Too Long. The line is also limited by set_max_buf_size. The default is 16 KiB.

Source

pub fn set_keepalive<W: Into<KeepAlive>>(self, val: W) -> Self

Sets the server keep-alive behavior.

By default, idle persistent connections are closed after five seconds. The keep-alive timeout does not apply before the first request. If request-head timing is disabled, it also bounds a partially received request head after the first request.

Source

pub fn set_keepalive_timeout(self, timeout: Seconds) -> Self

Sets the keep-alive timeout.

A zero duration disables persistent connections rather than selecting an unlimited timeout. Use KeepAlive::Os with set_keepalive to leave connection lifetime to the peer or operating system. The default is five seconds.

Source

pub fn set_client_timeout(self, timeout: Seconds) -> Self

Sets the initial timeout for reading request headers.

A new connection must send the first byte of its first request within this period, otherwise it is rejected with 408 Request Timeout. The request-head read rate starts with that byte, and this period is also its measurement interval. A zero duration disables header-read timing. A new connection can then wait indefinitely for its first request, while on a persistent connection the keep-alive timeout bounds both waiting for the next request and reading its head. The default is one second.

HTTP/1 timers have one-second resolution, a timeout can expire up to one second later than configured.

This sets the measurement interval of the request-head read rate. The cumulative limit and required rate configured by set_headers_read_rate are kept. If header-read timing was disabled, it is enabled again with the default rate of 256 bytes and a cumulative limit of timeout plus 15 seconds.

Source

pub fn set_write_timeout(self, timeout: Seconds) -> Self

Sets the HTTP/1 write backpressure timeout.

Write backpressure is enabled when outstanding output reaches the I/O write buffer high watermark, and disabled once the peer has accepted enough of it. If backpressure is still enabled when the timeout expires, the connection is closed, and the HTTP/1 control service receives a peer-gone event with an io::ErrorKind::TimedOut error. An unfinished request payload stream receives a PayloadError::Io with the same error kind. Each backpressure period starts a fresh timeout.

Without a write timeout, a client that stops reading responses can hold the connection open indefinitely. Payload read-rate timing is paused during write backpressure and resumes once it is disabled.

Timers have one-second resolution, the timeout can expire up to one second later than configured. A zero duration disables the timeout. It is disabled by default.

Source

pub fn set_half_close(self, enabled: bool) -> Self

Keeps streaming an HTTP/1 response after the client half-closes the connection.

A client that closes its side of the connection is treated as gone: when the read side reaches EOF, all buffered requests are handled and the response body has no data ready, the dispatcher drops the body and closes the connection. Otherwise an idle streaming response, e.g. server-sent events, would hold the connection until the body produces its next chunk.

Enable this for clients that shut down their write side after sending the request and still expect the full response. Such a response then ends only when the body completes or a write fails. Responses that are ready are sent either way. This setting does not affect HTTP/2. It is disabled by default.

Source

pub fn set_headers_vec(self, enabled: bool) -> Self

Preserves headers in their original order and casing.

When enabled, decoded headers are additionally copied into RequestHead::headers_vec or ResponseHead::headers_vec. The normal header map remains populated. This is disabled by default.

Source

pub fn set_host_validation(self, enabled: bool) -> Self

Enables validation of the HTTP/1 Host request header.

When enabled, requests are rejected with 400 Bad Request if they contain more than one Host header or a Host value that is not a valid host and optional port. HTTP/1.1 requests without a Host header are rejected as well, see RFC 9112 section 3.2. An empty Host value is accepted. This setting does not affect HTTP/2. It is enabled by default.

Source

pub fn set_headers_read_rate( self, timeout: Seconds, max_timeout: Seconds, rate: u32, ) -> Self

Sets read-rate limits for request headers.

This setting protects HTTP/1 connections from clients that send a request line or headers too slowly. The timer starts when the first bytes of a request head arrive, on a new connection as well as on a persistent one. Until the first byte of the first request arrives, a new connection waits for at most one timeout interval, the client timeout, without rate extension.

timeout is the duration of one measurement interval. When an interval expires, the dispatcher grants another interval only if more than rate new bytes were received. The request head must complete before the cumulative max_timeout is exhausted. All newly received request-head bytes count toward progress, including request-line and header bytes that the incremental parser has already consumed.

A zero timeout disables request-head timing. The first request of a connection is then unbounded, and the keep-alive timeout bounds waiting for and reading each following request head. A zero max_timeout removes the cumulative limit, allowing the deadline to be extended indefinitely while the required read rate is maintained. When max_timeout is not an exact multiple of timeout, the final measurement interval is shortened so the cumulative limit is not exceeded. Intervals have one-second resolution and can expire up to one second later than configured.

If the request head misses its deadline, the HTTP/1 control service receives ProtocolError::SlowRequestTimeout. The default control service responds with 408 Request Timeout and closes the connection.

By default, the timeout is 1 second and the maximum timeout is 16 seconds, with more than 256 bytes required for each extension.

§Example
use ntex::http::HttpServiceConfig;
use ntex::time::Seconds;

let config = HttpServiceConfig::new().set_headers_read_rate(
    Seconds(2),  // measurement interval
    Seconds(10), // maximum time for one request head
    512,         // bytes required to extend the deadline for next 2 seconds
);
Source

pub fn set_payload_read_rate( self, timeout: Seconds, max_timeout: Seconds, rate: u32, ) -> Self

Sets read-rate limits for request payloads.

This setting protects HTTP/1 connections from clients that send a request body too slowly. The timer starts when the dispatcher begins decoding a request payload. For a request with Expect: 100-continue, it starts only once 100 Continue has been sent, the request has been passed to the application, or a response has been sent and the rest of the payload is read, because the client does not send the body before that. At the end of each timeout interval, another interval is granted only if more than rate bytes were decoded.

The timer runs only while the dispatcher can read and forward payload data. It is paused while application payload backpressure or response write backpressure prevents further reads, so those conditions are not treated as a slow network peer. Write backpressure is bounded by the write timeout. Pausing preserves the unused portion of the current measurement interval, and resuming continues that interval rather than starting a new one, so the bytes decoded before and after the pause are measured together. If the interval expired before the pause, already received payload data is decoded on resume and then the read rate is checked. The timer stops when the complete payload has been decoded.

A zero timeout disables payload timing. A zero max_timeout removes the cumulative limit, allowing the deadline to be extended indefinitely while the required read rate is maintained. When max_timeout is not an exact multiple of timeout, the final measurement interval is shortened so the cumulative limit is not exceeded. Intervals have one-second resolution and can expire up to one second later than configured.

If the payload misses its deadline, its stream receives a timed-out PayloadError, and the HTTP/1 control service receives ProtocolError::SlowPayloadTimeout. The default control service responds with 408 Request Timeout and closes the connection.

Payload read-rate limiting is disabled by default.

§Example
use ntex::http::HttpServiceConfig;
use ntex::time::Seconds;

let config = HttpServiceConfig::new().set_payload_read_rate(
    Seconds(2),  // measurement interval
    Seconds(30), // maximum time for one request payload
    1024,        // bytes required to extend the deadline
);

Trait Implementations§

Source§

impl Configuration for HttpServiceConfig

Source§

const NAME: &str = "Http service configuration"

Human-readable configuration name used in diagnostics.
Source§

fn ctx(&self) -> &CfgContext

Returns the shared context associated with this value.
Source§

fn set_ctx(&mut self, ctx: CfgContext)

Associates this value with a shared configuration context.
Source§

impl Debug for HttpServiceConfig

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for HttpServiceConfig

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

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

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.