Struct OutgoingResponse

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

Represents an outgoing HTTP response.

OutgoingResponse is used in conjunction with ResponseOutparam in cases where you want to stream the response body. In cases where you don’t need to stream, it is often simpler to use Response.

§Examples

Send a streaming response to an incoming request:

async fn handle_request(req: IncomingRequest, response_outparam: ResponseOutparam) {
    use futures::SinkExt;
    use std::io::Read;

    let response_headers = Fields::from_list(&[
        ("content-type".to_owned(), "text/plain".into())
    ]).unwrap();

    let response = OutgoingResponse::new(response_headers);
    response.set_status_code(200).unwrap();
    let mut response_body = response.take_body();

    response_outparam.set(response);

    let mut file = std::fs::File::open("war-and-peace.txt").unwrap();

    loop {
        let mut buf = vec![0; 1024];
        let count = file.read(&mut buf).unwrap();

        if count == 0 {
            break;  // end of file
        }

        buf.truncate(count);
        response_body.send(buf).await.unwrap();
    }
}

Send a response then continue processing:

async fn handle_request(req: IncomingRequest, response_outparam: ResponseOutparam) {
    use futures::SinkExt;

    let response_headers = Fields::from_list(&[
        ("content-type".to_owned(), "text/plain".into())
    ]).unwrap();

    let response = OutgoingResponse::new(response_headers);
    response.set_status_code(200).unwrap();
    let mut response_body = response.take_body();

    response_outparam.set(response);

    response_body
        .send("Request accepted".as_bytes().to_vec())
        .await
        .unwrap();

    // End the HTTP response so the client deems it complete.
    response_body.flush().await.unwrap();
    response_body.close().await.unwrap();
    drop(response_body);

    // Perform any additional processing
    println!("While the cat's away, the mice will play");
}

Represents an outgoing HTTP Response.

Implementations§

Source§

impl OutgoingResponse

Source

pub fn new(headers: Headers) -> Self

Construct an outgoing-response, with a default status-code of 200. If a different status-code is needed, it must be set via the set-status-code method.

  • headers is the HTTP Headers for the Response.
Source§

impl OutgoingResponse

Source

pub fn status_code(&self) -> StatusCode

Get the HTTP Status Code for the Response.

Source§

impl OutgoingResponse

Source

pub fn set_status_code(&self, status_code: StatusCode) -> Result<(), ()>

Set the HTTP Status Code for the Response. Fails if the status-code given is not a valid http status code.

Source§

impl OutgoingResponse

Source

pub fn headers(&self) -> Headers

Get the headers associated with the Request.

The returned headers resource is immutable: set, append, and delete operations will fail with header-error.immutable.

This headers resource is a child: it must be dropped before the parent outgoing-request is dropped, or its ownership is transfered to another component by e.g. outgoing-handler.handle.

Source§

impl OutgoingResponse

Source

pub fn body(&self) -> Result<OutgoingBody, ()>

Returns the resource corresponding to the outgoing Body for this Response.

Returns success on the first call: the outgoing-body resource for this outgoing-response can be retrieved at most once. Subsequent calls will return error.

Source§

impl OutgoingResponse

Source

pub fn take_body(&self) -> impl Sink<Vec<u8>, Error = StreamError>

Construct a Sink which writes chunks to the body of the specified response.

§Panics

Panics if the body was already taken.

Trait Implementations§

Source§

impl Debug for OutgoingResponse

Source§

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

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

impl TryFrom<Response> for OutgoingResponse

Source§

type Error = Error

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

fn try_from(response: Response) -> Result<Self>

Performs the conversion.
Source§

impl WasmResource for OutgoingResponse

Source§

unsafe fn drop(_handle: u32)

Invokes the [resource-drop]... intrinsic.

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.