Skip to main content

HttpBuilder

Struct HttpBuilder 

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

The builder behind the server transport.

Every method is optional and every default is safe. The one decision this builder does not make for the caller is which address to bind, and it defaults that to loopback rather than to every interface.

Implementations§

Source§

impl HttpBuilder

Source

pub fn new(server: Server) -> Self

Serves server, on loopback unless told otherwise.

The defaults are the crate’s own strict ones rather than the permissive ones, so a builder that sets nothing is already safe: loopback, the newest handshake-capable revision, and a session id from the system entropy pool.

Source

pub fn legacy_sse(self) -> Self

Also serves the 2024-11-05 HTTP+SSE transport, beside Streamable HTTP.

A client opens a stream with GET /sse, is told where to POST by the endpoint event, and speaks 2024-11-05 over the pair. The same tools, resources and prompts answer on both transports, and the authenticator, Origin policy and rate limit set on this builder apply to both. Use legacy_sse_at to move the paths.

Source

pub fn legacy_sse_at( self, stream: impl Into<String>, message: impl Into<String>, ) -> Self

legacy_sse, on stream and message rather than /sse and /messages.

Both must be absolute paths with no query; serve refuses to start otherwise.

Source

pub const fn addr(self, addr: SocketAddr) -> Self

The address to bind. Defaults to 127.0.0.1:3000.

A non-loopback address is permitted but not made safe: this server speaks plain HTTP, so whatever is in front of it has to terminate TLS for it to be exposed to a network rather than to a machine.

Source

pub const fn version(self, version: ProtocolVersion) -> Self

The revision to gate pre-handshake sessions with.

Source

pub fn config(self, config: ServerConfig) -> Self

The endpoint policy and framing limits.

Source

pub fn ids(self, ids: Arc<dyn IdSource + Send + Sync>) -> Self

Where session ids come from.

Defaults to a counter from this crate’s own source. Supply RandomIds — behind the random-ids feature — for an id a peer cannot guess, which is the right choice anywhere a session id authorises anything.

Source

pub fn with_authenticator( self, authenticator: Arc<dyn Authenticator + Send + Sync>, ) -> Self

Replaces the anonymous default with a real implementation.

Pair with with_policy; a wired authenticator under the default AuthPolicy::Public is the silent fail-open the router’s into_router refuses at mount time. The same contract applies here, surfaced one layer up: a deployment that sets this without with_policy will see the misconfiguration at serve rather than at the first request.

Source

pub const fn with_policy(self, policy: AuthPolicy) -> Self

Replaces the default AuthPolicy::Public policy.

A deployment that wired an authenticator above must also set Require(scopes) here, or into_router will refuse to build the route table. See McpRouter::with_policy for the matching contract.

Source

pub fn with_origin_policy(self, origin: OriginPolicy) -> Self

Replaces the default Origin allow-list.

The default is OriginPolicy::strict(None), which refuses every Origin header sent and allows a request that sent none — see OriginPolicy::check for why absent is not the same as foreign. A deployment serving a browser over a non-loopback address will need to widen this to the page’s origin.

Source

pub fn with_rate_limiter(self, limiter: RateLimiter) -> Self

Replaces the per-subject request allowance.

Defaults to ServerConfig::rate_limit(), so a configuration that set one does not have to set it twice.

Source

pub async fn bind(self) -> Result<(TcpListener, ServeReport), ServeError>

Binds the listener without serving it.

Split from serving because a caller terminating TLS itself needs the listener before anything is served on it, and because binding before accepting removes the window in which a connection is refused for a port this process is about to take.

§Errors

ServeError::Bind when the address is taken or unusable.

Source

pub async fn serve(self) -> Result<ServeReport, ServeError>

Binds and serves until the accept loop fails.

§Errors

ServeError from building the route table, from binding, or from the accept loop itself. A protocol error is not one of those: a peer whose request this server cannot serve is answered on the wire rather than raised here.

Source

pub async fn serve_on( self, listener: TcpListener, ) -> Result<ServeReport, ServeError>

Serves on a listener the caller already bound.

The other half of bind, and the shape a deployment with TLS in front needs: bind, wrap the listener in a TLS acceptor, and serve on whatever that acceptor produces. Binding inside serve would make this impossible, because there would be no moment at which the raw listener was in the caller’s hands.

§Errors

ServeError::Routes when the route table cannot be built, and ServeError::Accept when the loop does.

Trait Implementations§

Source§

impl Debug for HttpBuilder

Source§

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

Prints what was configured and nothing else.

Handwritten for the same reason as HttpHandler’s: a derived Debug would walk the Server and the id source, and neither belongs in a log line. The address and the ceiling are the two facts worth having when a serve has gone wrong.

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more