SendResponse

Struct SendResponse 

Source
pub struct SendResponse<B: Buf> { /* private fields */ }
Expand description

Send a response back to the client

A SendResponse instance is provided when receiving a request and is used to send the associated response back to the client. It is also used to explicitly reset the stream with a custom reason.

It will also be used to initiate push promises linked with the associated stream.

If the SendResponse instance is dropped without sending a response, then the HTTP/2 stream will be reset.

See module level docs for more details.

Implementations§

Source§

impl<B: Buf> SendResponse<B>

Source

pub fn send_informational( &mut self, response: Response<()>, ) -> Result<(), Error>

Send an interim informational response (1xx status codes)

This method can be called multiple times before calling send_response() to send the final response. Only 1xx status codes are allowed.

Interim informational responses are used to provide early feedback to the client before the final response is ready. Common examples include:

  • 100 Continue: Indicates the client should continue with the request
  • 103 Early Hints: Provides early hints about resources to preload
§Arguments
  • response - HTTP response with 1xx status code and headers
§Returns
  • Ok(()) - Interim Informational response sent successfully
  • Err(Error) - Failed to send (invalid status code, connection error, etc.)
§Examples
use h2::server;
use http::{Response, StatusCode};

// Send 100 Continue before processing request body
let continue_response = Response::builder()
    .status(StatusCode::CONTINUE)
    .body(())
    .unwrap();
send_response.send_informational(continue_response)?;

// Later send the final response
let final_response = Response::builder()
    .status(StatusCode::OK)
    .body(())
    .unwrap();
let _stream = send_response.send_response(final_response, false)?;
§Errors

This method will return an error if:

  • The response status code is not in the 1xx range
  • The final response has already been sent
  • There is a connection-level error
Source

pub fn send_response( &mut self, response: Response<()>, end_of_stream: bool, ) -> Result<SendStream<B>, Error>

Send a response to a client request.

On success, a SendStream instance is returned. This instance can be used to stream the response body and send trailers.

If a body or trailers will be sent on the returned SendStream instance, then end_of_stream must be set to false when calling this function.

The SendResponse instance is already associated with a received request. This function may only be called once per instance and only if send_reset has not been previously called.

Source

pub fn push_request( &mut self, request: Request<()>, ) -> Result<SendPushedResponse<B>, Error>

Push a request and response to the client

On success, a SendResponse instance is returned.

Source

pub fn send_reset(&mut self, reason: Reason)

Send a stream reset to the peer.

This essentially cancels the stream, including any inbound or outbound data streams.

If this function is called before send_response, a call to send_response will result in an error.

If this function is called while a SendStream instance is active, any further use of the instance will result in an error.

This function should only be called once.

Source

pub fn poll_reset( &mut self, cx: &mut Context<'_>, ) -> Poll<Result<Reason, Error>>

Polls to be notified when the client resets this stream.

If stream is still open, this returns Poll::Pending, and registers the task to be notified if a RST_STREAM is received.

If a RST_STREAM frame is received for this stream, calling this method will yield the Reason for the reset.

§Error

Calling this method after having called send_response will return a user error.

Source

pub fn stream_id(&self) -> StreamId

Returns the stream ID of the response stream.

§Panics

If the lock on the stream store has been poisoned.

Trait Implementations§

Source§

impl<B: Debug + Buf> Debug for SendResponse<B>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<B> Freeze for SendResponse<B>

§

impl<B> RefUnwindSafe for SendResponse<B>

§

impl<B> Send for SendResponse<B>
where B: Send,

§

impl<B> Sync for SendResponse<B>
where B: Send,

§

impl<B> Unpin for SendResponse<B>

§

impl<B> UnwindSafe for SendResponse<B>

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> 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, 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.
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