[][src]Struct tophat::server::ResponseWriter

pub struct ResponseWriter<W> where
    W: AsyncWrite + Clone + Send + Sync + Unpin + 'static, 
{ /* fields omitted */ }

ResponseWriter has two responsibilities:

  • Hold a Response which can be modified or replaced.
  • Expose a send method which will immediately write the Response to the Http connection.

A ResponseWriter is initialized with a Response that contains:

  • An empty body
  • No headers (except that content-type defaults to application/octet-stream if not specified and there's a body)`
  • A 200 OK status

You can modify the Response as they see fit. Note, however, that a Body is not necessarily in sync with the content-type headers that are sent. for example, it's possible to set the Body using a string, and then set the content-type header on the Response to be `content-type: video/mp4'. The power is in the your hands.

There are two convenience methods which will set the content-type:

  • set_text, because there's no guess as to content-type, and
  • set_sse, because the content-type text/event-stream is required.

If you wish to create a Response separately and then apply it to the ResponseWriter, you can use tophat::http::Response and tophat::Body, and then ReponseWriter::response_mut.

All methods on ResponseWriter should list what headers they modify in the document string, and the type of the parameter should be reflected in the function name (i.e. text takes a string, not a stream or reader).

Possible body types:

  • &str/String,
  • AsyncRead,
  • Stream (StreamExt),

Implementations

impl<W> ResponseWriter<W> where
    W: AsyncWrite + Clone + Send + Sync + Unpin + 'static, 
[src]

pub async fn send(self) -> Result<ResponseWritten, Glitch>[src]

send response, and return number of bytes written

pub async fn send_code(self, code: u16) -> Result<ResponseWritten, Glitch>[src]

Sets response to specified code and immediately sends.

Devised as a shortcut so it would be easier to send a response with an empty body and status code. But if body is present, it will send that. (There's no effect on anything besides the status code)

Internally panics if status code is incorrect (use at your own risk! For something safer, try set_status.

pub fn set_status(&mut self, status: StatusCode) -> &mut Self[src]

Set response to specified status_code.

pub fn set_code(&mut self, code: u16) -> &mut Self[src]

Set response to specified code.

Internally panics if code is incorrect (use at your own risk! For something safer, try set_status.

pub fn set_body(&mut self, body: Body) -> &mut Self[src]

Set response to specified body.

Does not change content-type, that must be set separately in headers.

pub fn append_header(
    &mut self,
    header_name: impl IntoHeaderName,
    header_value: HeaderValue
) -> &mut Self
[src]

Append header to response. Will not replace a header with the same header name.

pub fn insert_header(
    &mut self,
    header_name: impl IntoHeaderName,
    header_value: HeaderValue
) -> &mut Self
[src]

Insert header to response. Replaces a header with the same header name.

pub fn response_mut(&mut self) -> &mut Response[src]

Mutable access to the full response. This way, if you like you can create the Response separately, and then set it in the ResponseWriter

async fn handler<W>(req: Request, mut resp_wtr: ResponseWriter<W>) -> Result<ResponseWritten>
    where W: AsyncRead + AsyncWrite + Clone + Send + Sync + Unpin + 'static,
{
    let resp = Response::new(Body::empty());
    *resp_wtr.response_mut() = resp;
    resp_wtr.send().await
}

pub fn response(&self) -> &Response[src]

Retrieve a reference to the Response in the ResponseWriter

pub fn set_text(&mut self, text: String) -> &mut Self[src]

Set response to:

  • 200 OK
  • Content-type text/plain
  • Body from String

pub fn set_sse<S>(&mut self, stream: S) where
    S: TryStreamExt<Error = Error> + Send + Sync + Unpin + 'static,
    S::Ok: AsRef<[u8]> + Send + Sync
[src]

Sets the response body as a Server Sent Events response stream. Adds the content-type header for SSE.

Takes a futures::Stream, and futures::TryStreamExt must be in scope.

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> Same<T> for T

type Output = T

Should always be Self

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.