/// Represents an HTTP response /// /// An HTTP response consists of a head and a potentially optional body. The body /// component is generic, enabling arbitrary types to represent the HTTP body. /// For example, the body could be `Vec<u8>`, a `Stream` of byte chunks, or a /// value that has been deserialized. /// /// Typically you'll work with responses on the client side as the result of /// sending a `Request` and on the server you'll be generating a `Response` to /// send back to the client. /// /// # Examples /// /// Creating a `Response` to return /// /// ``` /// use http::{Request, Response, StatusCode}; /// /// fn respond_to(req: Request<()>) -> http::Result<Response<()>> { /// let mut builder = Response::builder() /// .header("Foo", "Bar") /// .status(StatusCode::OK); /// /// if req.headers().contains_key("Another-Header") { /// builder = builder.header("Another-Header", "Ack"); /// } /// /// builder.body(()) /// } /// ``` /// /// A simple 404 handler /// /// ``` /// use http::{Request, Response, StatusCode}; /// /// fn not_found(_req: Request<()>) -> http::Result<Response<()>> { /// Response::builder() /// .status(StatusCode::NOT_FOUND) /// .body(()) /// } /// ``` /// /// Or otherwise inspecting the result of a request: /// /// ```no_run /// use http::{Request, Response}; /// /// fn get(url: &str) -> http::Result<Response<()>> { /// // ... /// # panic!() /// } /// /// let response = get("https://www.rust-lang.org/").unwrap(); /// /// if !response.status().is_success() { /// panic!("failed to get a successful response status!"); /// } /// /// if let Some(date) = response.headers().get("Date") { /// // we've got a `Date` header! /// } /// /// let body = response.body(); /// // ... /// ``` /// /// Deserialize a response of bytes via json: /// /// ``` /// # extern crate serde; /// # extern crate serde_json; /// # extern crate http; /// use http::Response; /// use serde::de; /// /// fn deserialize<T>(res: Response<Vec<u8>>) -> serde_json::Result<Response<T>> /// where for<'de> T: de::Deserialize<'de>, /// { /// let (parts, body) = res.into_parts(); /// let body = serde_json::from_slice(&body)?; /// Ok(Response::from_parts(parts, body)) /// } /// # /// # fn main() {} /// ``` /// /// Or alternatively, serialize the body of a response to json /// /// ``` /// # extern crate serde; /// # extern crate serde_json; /// # extern crate http; /// use http::Response; /// use serde::ser; /// /// fn serialize<T>(res: Response<T>) -> serde_json::Result<Response<Vec<u8>>> /// where T: ser::Serialize, /// { /// let (parts, body) = res.into_parts(); /// let body = serde_json::to_vec(&body)?; /// Ok(Response::from_parts(parts, body)) /// } /// # /// # fn main() {} /// ``` pubstruct Response<T> {
head: Parts,
body: T,
}
/// Component parts of an HTTP `Response` /// /// The HTTP response head consists of a status, version, and a set of /// header fields. pubstruct Parts { /// The response's status pub status: StatusCode,
/// The response's version pub version: Version,
/// The response's headers pub headers: HeaderMap<HeaderValue>,
/// The response's extensions pub extensions: Extensions,
_priv: (),
}
/// An HTTP response builder /// /// This type can be used to construct an instance of `Response` through a /// builder-like pattern. #[derive(Debug)] pubstruct Builder {
inner: Result<Parts>,
}
impl Response<()> { /// Creates a new builder-style object to manufacture a `Response` /// /// This method returns an instance of `Builder` which can be used to /// create a `Response`. /// /// # Examples /// /// ``` /// # use http::*; /// let response = Response::builder() /// .status(200) /// .header("X-Custom-Foo", "Bar") /// .body(()) /// .unwrap(); /// ``` #[inline] pubfn builder() -> Builder {
Builder::new()
}
}
impl<T> Response<T> { /// Creates a new blank `Response` with the body /// /// The component ports of this response will be set to their default, e.g. /// the ok status, no headers, etc. /// /// # Examples /// /// ``` /// # use http::*; /// let response = Response::new("hello world"); /// /// assert_eq!(response.status(), StatusCode::OK); /// assert_eq!(*response.body(), "hello world"); /// ``` #[inline] pubfn new(body: T) -> Response<T> {
Response {
head: Parts::new(),
body: body,
}
}
/// Creates a new `Response` with the given head and body /// /// # Examples /// /// ``` /// # use http::*; /// let response = Response::new("hello world"); /// let (mut parts, body) = response.into_parts(); /// /// parts.status = StatusCode::BAD_REQUEST; /// let response = Response::from_parts(parts, body); /// /// assert_eq!(response.status(), StatusCode::BAD_REQUEST); /// assert_eq!(*response.body(), "hello world"); /// ``` #[inline] pubfn from_parts(parts: Parts, body: T) -> Response<T> {
Response {
head: parts,
body: body,
}
}
impl Parts { /// Creates a new default instance of `Parts` fn new() -> Parts {
Parts {
status: StatusCode::default(),
version: Version::default(),
headers: HeaderMap::default(),
extensions: Extensions::default(),
_priv: (),
}
}
}
impl fmt::Debug for Parts { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Parts")
.field("status", &self.status)
.field("version", &self.version)
.field("headers", &self.headers) // omits Extensions because not useful // omits _priv because not useful
.finish()
}
}
impl Builder { /// Creates a new default instance of `Builder` to construct either a /// `Head` or a `Response`. /// /// # Examples /// /// ``` /// # use http::*; /// /// let response = response::Builder::new() /// .status(200) /// .body(()) /// .unwrap(); /// ``` #[inline] pubfn new() -> Builder {
Builder::default()
}
/// Set the HTTP status for this response. /// /// This function will configure the HTTP status code of the `Response` that /// will be returned from `Builder::build`. /// /// By default this is `200`. /// /// # Examples /// /// ``` /// # use http::*; /// /// let response = Response::builder() /// .status(200) /// .body(()) /// .unwrap(); /// ``` pubfn status<T>(self, status: T) -> Builder where
StatusCode: TryFrom<T>,
<StatusCode as TryFrom<T>>::Error: Into<crate::Error>,
{ self.and_then(move |mut head| {
head.status = TryFrom::try_from(status).map_err(Into::into)?;
Ok(head)
})
}
/// Set the HTTP version for this response. /// /// This function will configure the HTTP version of the `Response` that /// will be returned from `Builder::build`. /// /// By default this is HTTP/1.1 /// /// # Examples /// /// ``` /// # use http::*; /// /// let response = Response::builder() /// .version(Version::HTTP_2) /// .body(()) /// .unwrap(); /// ``` pubfn version(self, version: Version) -> Builder { self.and_then(move |mut head| {
head.version = version;
Ok(head)
})
}
/// Appends a header to this response builder. /// /// This function will append the provided key/value as a header to the /// internal `HeaderMap` being constructed. Essentially this is equivalent /// to calling `HeaderMap::append`. /// /// # Examples /// /// ``` /// # use http::*; /// # use http::header::HeaderValue; /// /// let response = Response::builder() /// .header("Content-Type", "text/html") /// .header("X-Custom-Foo", "bar") /// .header("content-length", 0) /// .body(()) /// .unwrap(); /// ``` pubfn header<K, V>(self, key: K, value: V) -> Builder where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<crate::Error>,
HeaderValue: TryFrom<V>,
<HeaderValue as TryFrom<V>>::Error: Into<crate::Error>,
{ self.and_then(move |mut head| { let name = <HeaderName as TryFrom<K>>::try_from(key).map_err(Into::into)?; let value = <HeaderValue as TryFrom<V>>::try_from(value).map_err(Into::into)?;
head.headers.append(name, value);
Ok(head)
})
}
/// Get header on this response builder. /// /// When builder has error returns None. /// /// # Example /// /// ``` /// # use http::Response; /// # use http::header::HeaderValue; /// let res = Response::builder() /// .header("Accept", "text/html") /// .header("X-Custom-Foo", "bar"); /// let headers = res.headers_ref().unwrap(); /// assert_eq!( headers["Accept"], "text/html" ); /// assert_eq!( headers["X-Custom-Foo"], "bar" ); /// ``` pubfn headers_ref(&self) -> Option<&HeaderMap<HeaderValue>> { self.inner.as_ref().ok().map(|h| &h.headers)
}
/// Get header on this response builder. /// when builder has error returns None /// /// # Example /// /// ``` /// # use http::*; /// # use http::header::HeaderValue; /// # use http::response::Builder; /// let mut res = Response::builder(); /// { /// let headers = res.headers_mut().unwrap(); /// headers.insert("Accept", HeaderValue::from_static("text/html")); /// headers.insert("X-Custom-Foo", HeaderValue::from_static("bar")); /// } /// let headers = res.headers_ref().unwrap(); /// assert_eq!( headers["Accept"], "text/html" ); /// assert_eq!( headers["X-Custom-Foo"], "bar" ); /// ``` pubfn headers_mut(&mutself) -> Option<&mut HeaderMap<HeaderValue>> { self.inner.as_mut().ok().map(|h| &mut h.headers)
}
/// Adds an extension to this builder /// /// # Examples /// /// ``` /// # use http::*; /// /// let response = Response::builder() /// .extension("My Extension") /// .body(()) /// .unwrap(); /// /// assert_eq!(response.extensions().get::<&'static str>(), /// Some(&"My Extension")); /// ``` pubfn extension<T>(self, extension: T) -> Builder where
T: Any + Send + Sync + 'static,
{ self.and_then(move |mut head| {
head.extensions.insert(extension);
Ok(head)
})
}
/// Get a reference to the extensions for this response builder. /// /// If the builder has an error, this returns `None`. /// /// # Example /// /// ``` /// # use http::Response; /// let res = Response::builder().extension("My Extension").extension(5u32); /// let extensions = res.extensions_ref().unwrap(); /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension")); /// assert_eq!(extensions.get::<u32>(), Some(&5u32)); /// ``` pubfn extensions_ref(&self) -> Option<&Extensions> { self.inner.as_ref().ok().map(|h| &h.extensions)
}
/// Get a mutable reference to the extensions for this response builder. /// /// If the builder has an error, this returns `None`. /// /// # Example /// /// ``` /// # use http::Response; /// let mut res = Response::builder().extension("My Extension"); /// let mut extensions = res.extensions_mut().unwrap(); /// assert_eq!(extensions.get::<&'static str>(), Some(&"My Extension")); /// extensions.insert(5u32); /// assert_eq!(extensions.get::<u32>(), Some(&5u32)); /// ``` pubfn extensions_mut(&mutself) -> Option<&>mut Extensions> { self.inner.as_mut().ok().map(|h| &mut h.extensions)
}
/// "Consumes" this builder, using the provided `body` to return a /// constructed `Response`. /// /// # Errors /// /// This function may return an error if any previously configured argument /// failed to parse or get converted to the internal representation. For /// example if an invalid `head` was specified via `header("Foo", /// "Bar\r\n")` the error will be returned when this function is called /// rather than when `header` was called. /// /// # Examples /// /// ``` /// # use http::*; /// /// let response = Response::builder() /// .body(()) /// .unwrap(); /// ``` pubfn body<T>(self, body: T) -> Result<Response<T>> { self.inner.map(move |head| {
Response {
head,
body,
}
})
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.