pub struct ResponseBuilder { /* private fields */ }
Expand description
An HTTP response builder
This type can be used to construct an instance of Response
through a
builder-like pattern.
Implementations§
Source§impl ResponseBuilder
impl ResponseBuilder
Sourcepub fn new(status: StatusCode) -> Self
pub fn new(status: StatusCode) -> Self
Create response builder
Sourcepub fn status(&mut self, status: StatusCode) -> &mut Self
pub fn status(&mut self, status: StatusCode) -> &mut Self
Set HTTP status code of this response.
Sourcepub fn header<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
Append a header to existing headers.
use scrappy_http::{http, Request, Response};
fn index(req: Request) -> Response {
Response::Ok()
.header("X-TEST", "value")
.header(http::header::CONTENT_TYPE, "application/json")
.finish()
}
Sourcepub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
pub fn set_header<K, V>(&mut self, key: K, value: V) -> &mut Selfwhere
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
Set a header.
use scrappy_http::{http, Request, Response};
fn index(req: Request) -> Response {
Response::Ok()
.set_header("X-TEST", "value")
.set_header(http::header::CONTENT_TYPE, "application/json")
.finish()
}
Sourcepub fn reason(&mut self, reason: &'static str) -> &mut Self
pub fn reason(&mut self, reason: &'static str) -> &mut Self
Set the custom reason for the response.
Sourcepub fn keep_alive(&mut self) -> &mut Self
pub fn keep_alive(&mut self) -> &mut Self
Set connection type to KeepAlive
Sourcepub fn upgrade<V>(&mut self, value: V) -> &mut Selfwhere
V: IntoHeaderValue,
pub fn upgrade<V>(&mut self, value: V) -> &mut Selfwhere
V: IntoHeaderValue,
Set connection type to Upgrade
Sourcepub fn force_close(&mut self) -> &mut Self
pub fn force_close(&mut self) -> &mut Self
Force close connection, even if it is marked as keep-alive
Sourcepub fn no_chunking(&mut self) -> &mut Self
pub fn no_chunking(&mut self) -> &mut Self
Disable chunked transfer encoding for HTTP/1.1 streaming responses.
Sourcepub fn content_type<V>(&mut self, value: V) -> &mut Self
pub fn content_type<V>(&mut self, value: V) -> &mut Self
Set response content type
Sourcepub fn content_length(&mut self, len: u64) -> &mut Self
pub fn content_length(&mut self, len: u64) -> &mut Self
Set content length
Set a cookie
use scrappy_http::{http, Request, Response};
fn index(req: Request) -> Response {
Response::Ok()
.cookie(
http::Cookie::build("name", "value")
.domain("www.rust-lang.org")
.path("/")
.secure(true)
.http_only(true)
.finish(),
)
.finish()
}
Remove cookie
use scrappy_http::{http, Request, Response, HttpMessage};
fn index(req: Request) -> Response {
let mut builder = Response::Ok();
if let Some(ref cookie) = req.cookie("name") {
builder.del_cookie(cookie);
}
builder.finish()
}
Sourcepub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Selfwhere
F: FnOnce(&mut ResponseBuilder),
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Selfwhere
F: FnOnce(&mut ResponseBuilder),
This method calls provided closure with builder reference if value is true.
Sourcepub fn if_some<T, F>(&mut self, value: Option<T>, f: F) -> &mut Selfwhere
F: FnOnce(T, &mut ResponseBuilder),
pub fn if_some<T, F>(&mut self, value: Option<T>, f: F) -> &mut Selfwhere
F: FnOnce(T, &mut ResponseBuilder),
This method calls provided closure with builder reference if value is Some.
Sourcepub fn extensions(&self) -> Ref<'_, Extensions>
pub fn extensions(&self) -> Ref<'_, Extensions>
Responses extensions
Sourcepub fn extensions_mut(&mut self) -> RefMut<'_, Extensions>
pub fn extensions_mut(&mut self) -> RefMut<'_, Extensions>
Mutable reference to a the response’s extensions
Sourcepub fn body<B: Into<Body>>(&mut self, body: B) -> Response ⓘ
pub fn body<B: Into<Body>>(&mut self, body: B) -> Response ⓘ
Set a body and generate Response
.
ResponseBuilder
can not be used after this call.
Sourcepub fn message_body<B>(&mut self, body: B) -> Response<B> ⓘ
pub fn message_body<B>(&mut self, body: B) -> Response<B> ⓘ
Set a body and generate Response
.
ResponseBuilder
can not be used after this call.
Sourcepub fn streaming<S, E>(&mut self, stream: S) -> Response ⓘ
pub fn streaming<S, E>(&mut self, stream: S) -> Response ⓘ
Set a streaming body and generate Response
.
ResponseBuilder
can not be used after this call.
Sourcepub fn json<T: Serialize>(&mut self, value: T) -> Response ⓘ
pub fn json<T: Serialize>(&mut self, value: T) -> Response ⓘ
Set a json body and generate Response
ResponseBuilder
can not be used after this call.
Sourcepub fn json2<T: Serialize>(&mut self, value: &T) -> Response ⓘ
pub fn json2<T: Serialize>(&mut self, value: &T) -> Response ⓘ
Set a json body and generate Response
ResponseBuilder
can not be used after this call.
Sourcepub fn finish(&mut self) -> Response ⓘ
pub fn finish(&mut self) -> Response ⓘ
Set an empty body and generate Response
ResponseBuilder
can not be used after this call.
Sourcepub fn take(&mut self) -> ResponseBuilder ⓘ
pub fn take(&mut self) -> ResponseBuilder ⓘ
This method construct new ResponseBuilder
Trait Implementations§
Source§impl Debug for ResponseBuilder
impl Debug for ResponseBuilder
Source§impl<'a> From<&'a ResponseHead> for ResponseBuilder
Convert ResponseHead
to a ResponseBuilder
impl<'a> From<&'a ResponseHead> for ResponseBuilder
Convert ResponseHead
to a ResponseBuilder
Source§fn from(head: &'a ResponseHead) -> ResponseBuilder ⓘ
fn from(head: &'a ResponseHead) -> ResponseBuilder ⓘ
Source§impl<B> From<Response<B>> for ResponseBuilder
Convert Response
to a ResponseBuilder
. Body get dropped.
impl<B> From<Response<B>> for ResponseBuilder
Convert Response
to a ResponseBuilder
. Body get dropped.
Source§impl From<ResponseBuilder> for Error
Convert ResponseBuilder to a Error
impl From<ResponseBuilder> for Error
Convert ResponseBuilder to a Error
Source§fn from(res: ResponseBuilder) -> Error
fn from(res: ResponseBuilder) -> Error
Source§impl From<ResponseBuilder> for Response
impl From<ResponseBuilder> for Response
Source§fn from(builder: ResponseBuilder) -> Self
fn from(builder: ResponseBuilder) -> Self
Source§impl Future for ResponseBuilder
impl Future for ResponseBuilder
Auto Trait Implementations§
impl Freeze for ResponseBuilder
impl !RefUnwindSafe for ResponseBuilder
impl !Send for ResponseBuilder
impl !Sync for ResponseBuilder
impl Unpin for ResponseBuilder
impl !UnwindSafe for ResponseBuilder
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> FutureExt for T
impl<T> FutureExt for T
Source§fn map<U, F>(self, f: F) -> Map<Self, F>
fn map<U, F>(self, f: F) -> Map<Self, F>
Source§fn map_into<U>(self) -> MapInto<Self, U>
fn map_into<U>(self) -> MapInto<Self, U>
Source§fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
fn then<Fut, F>(self, f: F) -> Then<Self, Fut, F>
f
. Read moreSource§fn left_future<B>(self) -> Either<Self, B>
fn left_future<B>(self) -> Either<Self, B>
Source§fn right_future<A>(self) -> Either<A, Self>
fn right_future<A>(self) -> Either<A, Self>
Source§fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
fn into_stream(self) -> IntoStream<Self>where
Self: Sized,
Source§fn flatten(self) -> Flatten<Self>
fn flatten(self) -> Flatten<Self>
Source§fn flatten_stream(self) -> FlattenStream<Self>
fn flatten_stream(self) -> FlattenStream<Self>
Source§fn fuse(self) -> Fuse<Self>where
Self: Sized,
fn fuse(self) -> Fuse<Self>where
Self: Sized,
poll
will never again be called once it has
completed. This method can be used to turn any Future
into a
FusedFuture
. Read moreSource§fn inspect<F>(self, f: F) -> Inspect<Self, F>
fn inspect<F>(self, f: F) -> Inspect<Self, F>
Source§fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
fn catch_unwind(self) -> CatchUnwind<Self>where
Self: Sized + UnwindSafe,
Source§fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
fn remote_handle(self) -> (Remote<Self>, RemoteHandle<Self::Output>)where
Self: Sized,
()
on completion and sends
its output to another future on a separate task. Read moreSource§fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
fn boxed<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + Send + 'a>>
Source§fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
fn boxed_local<'a>(self) -> Pin<Box<dyn Future<Output = Self::Output> + 'a>>where
Self: Sized + 'a,
Source§fn unit_error(self) -> UnitError<Self>where
Self: Sized,
fn unit_error(self) -> UnitError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = ()
>.Source§fn never_error(self) -> NeverError<Self>where
Self: Sized,
fn never_error(self) -> NeverError<Self>where
Self: Sized,
Future<Output = T>
into a
TryFuture<Ok = T, Error = Never
>.Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more