2016-03-28 09:34:09 +00:00
|
|
|
use std::fs::File;
|
2019-08-20 01:13:49 +00:00
|
|
|
use std::io::Cursor;
|
2016-03-28 09:34:09 +00:00
|
|
|
|
2019-06-13 01:48:02 +00:00
|
|
|
use crate::http::{Status, ContentType, StatusClass};
|
2020-06-19 13:01:10 +00:00
|
|
|
use crate::response::{self, Response};
|
2019-06-13 01:48:02 +00:00
|
|
|
use crate::request::Request;
|
2016-10-25 11:03:50 +00:00
|
|
|
|
2016-12-21 08:09:22 +00:00
|
|
|
/// Trait implemented by types that generate responses for clients.
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// Any type that implements `Responder` can be used as the return type of a
|
|
|
|
/// handler:
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2018-10-06 13:25:17 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// # type T = ();
|
|
|
|
/// #
|
2021-06-26 19:28:39 +00:00
|
|
|
/// // This works for any `T` that implements `Responder`.
|
2016-11-03 14:09:01 +00:00
|
|
|
/// #[get("/")]
|
2018-10-06 13:25:17 +00:00
|
|
|
/// fn index() -> T { /* ... */ }
|
2016-11-03 14:09:01 +00:00
|
|
|
/// ```
|
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// # Deriving
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// This trait can, and largely _should_, be automatically derived. The derive
|
|
|
|
/// can handle all simple cases and most complex cases, too. When deriving
|
|
|
|
/// `Responder`, the first field of the annotated structure (or of each variant
|
|
|
|
/// if an `enum`) is used to generate a response while the remaining fields are
|
|
|
|
/// used as response headers:
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// # #[cfg(feature = "json")] mod _main {
|
|
|
|
/// # type Template = String;
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
/// use rocket::serde::{Serialize, json::Json};
|
|
|
|
///
|
|
|
|
/// #[derive(Responder)]
|
|
|
|
/// enum Error<T> {
|
|
|
|
/// #[response(status = 400)]
|
|
|
|
/// Unauthorized(Json<T>),
|
|
|
|
/// #[response(status = 404)]
|
|
|
|
/// NotFound(Template, ContentType),
|
|
|
|
/// }
|
|
|
|
/// # }
|
|
|
|
/// ```
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// For full details on deriving `Responder`, see the [`Responder` derive].
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// [`Responder` derive]: derive@crate::Responder
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2016-11-03 16:05:41 +00:00
|
|
|
/// # Provided Implementations
|
|
|
|
///
|
|
|
|
/// Rocket implements `Responder` for several standard library types. Their
|
|
|
|
/// behavior is documented here. Note that the `Result` implementation is
|
|
|
|
/// overloaded, allowing for two `Responder`s to be used at once, depending on
|
|
|
|
/// the variant.
|
|
|
|
///
|
2016-12-15 08:47:31 +00:00
|
|
|
/// * **&str**
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
2016-12-20 04:40:21 +00:00
|
|
|
/// Sets the `Content-Type` to `text/plain`. The string is used as the body
|
2016-12-15 08:47:31 +00:00
|
|
|
/// of the response, which is fixed size and not streamed. To stream a raw
|
|
|
|
/// string, use `Stream::from(Cursor::new(string))`.
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
2016-12-15 08:47:31 +00:00
|
|
|
/// * **String**
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
2017-09-22 01:48:39 +00:00
|
|
|
/// Sets the `Content-Type` to `text/plain`. The string is used as the body
|
2016-12-15 08:47:31 +00:00
|
|
|
/// of the response, which is fixed size and not streamed. To stream a
|
|
|
|
/// string, use `Stream::from(Cursor::new(string))`.
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
2018-07-14 20:48:56 +00:00
|
|
|
/// * **&\[u8\]**
|
|
|
|
///
|
|
|
|
/// Sets the `Content-Type` to `application/octet-stream`. The slice
|
|
|
|
/// is used as the body of the response, which is fixed size and not
|
|
|
|
/// streamed. To stream a slice of bytes, use
|
|
|
|
/// `Stream::from(Cursor::new(data))`.
|
|
|
|
///
|
|
|
|
/// * **Vec<u8>**
|
2017-09-22 01:48:39 +00:00
|
|
|
///
|
|
|
|
/// Sets the `Content-Type` to `application/octet-stream`. The vector's data
|
|
|
|
/// is used as the body of the response, which is fixed size and not
|
|
|
|
/// streamed. To stream a vector of bytes, use
|
|
|
|
/// `Stream::from(Cursor::new(vec))`.
|
|
|
|
///
|
2016-12-15 08:47:31 +00:00
|
|
|
/// * **File**
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
2017-07-04 08:34:43 +00:00
|
|
|
/// Responds with a streamed body containing the data in the `File`. No
|
2017-05-19 10:29:08 +00:00
|
|
|
/// `Content-Type` is set. To automatically have a `Content-Type` set based
|
2021-05-22 23:21:19 +00:00
|
|
|
/// on the file's extension, use [`NamedFile`](crate::fs::NamedFile).
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
2016-12-20 04:40:21 +00:00
|
|
|
/// * **()**
|
2016-12-10 03:53:13 +00:00
|
|
|
///
|
2017-05-19 10:29:08 +00:00
|
|
|
/// Responds with an empty body. No `Content-Type` is set.
|
2016-12-10 03:53:13 +00:00
|
|
|
///
|
2016-12-15 08:47:31 +00:00
|
|
|
/// * **Option<T>**
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
|
|
|
/// If the `Option` is `Some`, the wrapped responder is used to respond to
|
2016-12-20 04:40:21 +00:00
|
|
|
/// the client. Otherwise, an `Err` with status **404 Not Found** is
|
|
|
|
/// returned and a warning is printed to the console.
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
2019-12-20 02:11:32 +00:00
|
|
|
/// * **Result<T, E>**
|
2016-11-03 16:05:41 +00:00
|
|
|
///
|
|
|
|
/// If the `Result` is `Ok`, the wrapped `Ok` responder is used to respond
|
2016-12-15 08:47:31 +00:00
|
|
|
/// to the client. If the `Result` is `Err`, the wrapped `Err` responder is
|
2016-11-03 16:05:41 +00:00
|
|
|
/// used to respond to the client.
|
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// # Return Value
|
|
|
|
///
|
|
|
|
/// A `Responder` returns a `Future` whose output type is a `Result<Response,
|
|
|
|
/// Status>`.
|
|
|
|
///
|
|
|
|
/// * An `Ok(Response)` indicates success. The `Response` will be written out
|
|
|
|
/// to the client.
|
|
|
|
///
|
|
|
|
/// * An `Err(Status)` indicates failure. The error catcher for `Status` will
|
|
|
|
/// be invoked to generate a response.
|
|
|
|
///
|
2016-11-03 14:09:01 +00:00
|
|
|
/// # Implementation Tips
|
|
|
|
///
|
|
|
|
/// This section describes a few best practices to take into account when
|
|
|
|
/// implementing `Responder`.
|
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// 1. Avoid Manual Implementations
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// The [`Responder` derive] is a powerful mechanism that eliminates the need
|
|
|
|
/// to implement `Responder` in almost all cases. We encourage you to explore
|
|
|
|
/// using the derive _before_ attempting to implement `Responder` directly.
|
|
|
|
/// It allows you to leverage existing `Responder` implementations through
|
|
|
|
/// composition, decreasing the opportunity for mistakes or performance
|
|
|
|
/// degradation.
|
2016-12-10 04:59:58 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// 2. Joining and Merging
|
2017-05-19 10:29:08 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// When chaining/wrapping other `Responder`s, start with
|
|
|
|
/// [`Response::build_from()`] and/or use the [`merge()`](Response::merge())
|
|
|
|
/// or [`join()`](Response::join()) methods on the `Response` or
|
|
|
|
/// `ResponseBuilder` struct. Ensure that you document merging or joining
|
|
|
|
/// behavior appropriatse.
|
|
|
|
///
|
|
|
|
/// 3. Inspecting Requests
|
|
|
|
///
|
|
|
|
/// While tempting, a `Responder` that varies its functionality based on the
|
|
|
|
/// incoming request sacrifices its functionality being understood based
|
|
|
|
/// purely on its type. By implication, gleaming the functionality of a
|
|
|
|
/// _handler_ from its type signature also becomes more difficult. You should
|
|
|
|
/// avoid varying responses based on the `Request` value as much as possible.
|
2017-05-19 10:29:08 +00:00
|
|
|
///
|
2020-06-19 13:01:10 +00:00
|
|
|
/// ## Lifetimes
|
2020-02-03 08:30:22 +00:00
|
|
|
///
|
2021-06-26 19:28:39 +00:00
|
|
|
/// `Responder` has two lifetimes: `Responder<'r, 'o: 'r>`.
|
|
|
|
///
|
|
|
|
/// * `'r` bounds the reference to the `&'r Request`.
|
|
|
|
///
|
|
|
|
/// * `'o` bounds the returned `Response<'o>` to values that live at least as
|
|
|
|
/// long as the request.
|
|
|
|
///
|
|
|
|
/// This includes borrows from the `Request` itself (where `'o` would be
|
|
|
|
/// `'r` as in `impl<'r> Responder<'r, 'r>`) as well as `'static` data
|
|
|
|
/// (where `'o` would be `'static` as in `impl<'r> Responder<'r, 'static>`).
|
|
|
|
///
|
|
|
|
/// In practice, you are likely choosing between four signatures:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// # use rocket::request::Request;
|
|
|
|
/// # use rocket::response::{self, Responder};
|
|
|
|
/// # struct A;
|
|
|
|
/// // If the response contains no borrowed data.
|
|
|
|
/// impl<'r> Responder<'r, 'static> for A {
|
|
|
|
/// fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
|
|
|
/// todo!()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # struct B<'r>(&'r str);
|
|
|
|
/// // If the response borrows from the request.
|
|
|
|
/// impl<'r> Responder<'r, 'r> for B<'r> {
|
|
|
|
/// fn respond_to(self, _: &'r Request<'_>) -> response::Result<'r> {
|
|
|
|
/// todo!()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # struct C;
|
|
|
|
/// // If the response is or wraps a borrow that may outlive the request.
|
|
|
|
/// impl<'r, 'o: 'r> Responder<'r, 'o> for &'o C {
|
|
|
|
/// fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> {
|
|
|
|
/// todo!()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// # struct D<R>(R);
|
|
|
|
/// // If the response wraps an existing responder.
|
|
|
|
/// impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for D<R> {
|
|
|
|
/// fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> {
|
|
|
|
/// todo!()
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// ```
|
2020-02-03 08:30:22 +00:00
|
|
|
///
|
2016-12-10 04:59:58 +00:00
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Say that you have a custom type, `Person`:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// struct Person {
|
|
|
|
/// name: String,
|
|
|
|
/// age: u16
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You'd like to use `Person` as a `Responder` so that you can return a
|
|
|
|
/// `Person` directly from a handler:
|
|
|
|
///
|
2020-02-03 08:30:22 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # #[macro_use] extern crate rocket;
|
|
|
|
/// # type Person = String;
|
2016-12-10 04:59:58 +00:00
|
|
|
/// #[get("/person/<id>")]
|
|
|
|
/// fn person(id: usize) -> Option<Person> {
|
2020-02-03 08:30:22 +00:00
|
|
|
/// # /*
|
2016-12-10 04:59:58 +00:00
|
|
|
/// Person::from_id(id)
|
2020-02-03 08:30:22 +00:00
|
|
|
/// # */ None
|
2016-12-10 04:59:58 +00:00
|
|
|
/// }
|
2020-02-03 08:30:22 +00:00
|
|
|
/// # fn main() {}
|
2016-12-10 04:59:58 +00:00
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// You want the `Person` responder to set two header fields: `X-Person-Name`
|
|
|
|
/// and `X-Person-Age` as well as supply a custom representation of the object
|
|
|
|
/// (`Content-Type: application/x-person`) in the body of the response. The
|
|
|
|
/// following `Responder` implementation accomplishes this:
|
|
|
|
///
|
|
|
|
/// ```rust
|
2018-09-20 04:14:30 +00:00
|
|
|
/// # #[macro_use] extern crate rocket;
|
2016-12-21 02:07:14 +00:00
|
|
|
/// #
|
2016-12-10 04:59:58 +00:00
|
|
|
/// # #[derive(Debug)]
|
|
|
|
/// # struct Person { name: String, age: u16 }
|
|
|
|
/// #
|
2016-12-15 08:47:31 +00:00
|
|
|
/// use std::io::Cursor;
|
2016-12-10 04:59:58 +00:00
|
|
|
///
|
2017-05-19 10:29:08 +00:00
|
|
|
/// use rocket::request::Request;
|
2016-12-15 08:47:31 +00:00
|
|
|
/// use rocket::response::{self, Response, Responder};
|
2016-12-10 04:59:58 +00:00
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
2020-06-19 13:01:10 +00:00
|
|
|
/// impl<'r> Responder<'r, 'static> for Person {
|
2021-06-26 19:28:39 +00:00
|
|
|
/// fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
|
|
|
|
/// let string = format!("{}:{}", self.name, self.age);
|
|
|
|
/// Response::build_from(string.respond_to(req)?)
|
2020-02-03 08:30:22 +00:00
|
|
|
/// .raw_header("X-Person-Name", self.name)
|
|
|
|
/// .raw_header("X-Person-Age", self.age.to_string())
|
|
|
|
/// .header(ContentType::new("application", "x-person"))
|
|
|
|
/// .ok()
|
2016-12-10 04:59:58 +00:00
|
|
|
/// }
|
|
|
|
/// }
|
2016-12-21 08:09:22 +00:00
|
|
|
/// #
|
2016-12-21 02:07:14 +00:00
|
|
|
/// # #[get("/person")]
|
|
|
|
/// # fn person() -> Person { Person { name: "a".to_string(), age: 20 } }
|
|
|
|
/// # fn main() { }
|
2016-12-10 04:59:58 +00:00
|
|
|
/// ```
|
2021-06-26 19:28:39 +00:00
|
|
|
///
|
|
|
|
/// Note that the implementation could have instead been derived if structured
|
|
|
|
/// in a slightly different manner:
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::http::Header;
|
|
|
|
/// use rocket::response::Responder;
|
|
|
|
///
|
|
|
|
/// #[derive(Responder)]
|
|
|
|
/// #[response(content_type = "application/x-person")]
|
|
|
|
/// struct Person {
|
|
|
|
/// text: String,
|
|
|
|
/// name: Header<'static>,
|
|
|
|
/// age: Header<'static>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl Person {
|
|
|
|
/// fn new(name: &str, age: usize) -> Person {
|
|
|
|
/// Person {
|
|
|
|
/// text: format!("{}:{}", name, age),
|
|
|
|
/// name: Header::new("X-Person-Name", name.to_string()),
|
|
|
|
/// age: Header::new("X-Person-Age", age.to_string())
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
/// #
|
|
|
|
/// # #[rocket::get("/person")]
|
|
|
|
/// # fn person() -> Person { Person::new("Bob", 29) }
|
|
|
|
/// ```
|
2020-06-19 13:01:10 +00:00
|
|
|
pub trait Responder<'r, 'o: 'r> {
|
2016-12-20 21:40:02 +00:00
|
|
|
/// Returns `Ok` if a `Response` could be generated successfully. Otherwise,
|
|
|
|
/// returns an `Err` with a failing `Status`.
|
2016-11-03 14:09:01 +00:00
|
|
|
///
|
2017-05-19 10:29:08 +00:00
|
|
|
/// The `request` parameter is the `Request` that this `Responder` is
|
|
|
|
/// responding to.
|
|
|
|
///
|
2016-12-20 21:40:02 +00:00
|
|
|
/// When using Rocket's code generation, if an `Ok(Response)` is returned,
|
|
|
|
/// the response will be written out to the client. If an `Err(Status)` is
|
|
|
|
/// returned, the error catcher for the given status is retrieved and called
|
|
|
|
/// to generate a final error response, which is then written out to the
|
|
|
|
/// client.
|
2020-06-19 13:01:10 +00:00
|
|
|
fn respond_to(self, request: &'r Request<'_>) -> response::Result<'o>;
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
|
2016-12-20 21:40:02 +00:00
|
|
|
/// Returns a response with Content-Type `text/plain` and a fixed-size body
|
|
|
|
/// containing the string `self`. Always returns `Ok`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r, 'o: 'r> Responder<'r, 'o> for &'o str {
|
|
|
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> {
|
2020-02-03 08:30:22 +00:00
|
|
|
Response::build()
|
|
|
|
.header(ContentType::Plain)
|
2020-06-19 13:01:10 +00:00
|
|
|
.sized_body(self.len(), Cursor::new(self))
|
2020-02-03 08:30:22 +00:00
|
|
|
.ok()
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-14 07:43:24 +00:00
|
|
|
/// Returns a response with Content-Type `text/plain` and a fixed-size body
|
2016-12-20 21:40:02 +00:00
|
|
|
/// containing the string `self`. Always returns `Ok`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r> Responder<'r, 'static> for String {
|
|
|
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
2020-02-03 08:30:22 +00:00
|
|
|
Response::build()
|
|
|
|
.header(ContentType::Plain)
|
2020-06-19 13:01:10 +00:00
|
|
|
.sized_body(self.len(), Cursor::new(self))
|
2020-02-03 08:30:22 +00:00
|
|
|
.ok()
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-14 20:48:56 +00:00
|
|
|
/// Returns a response with Content-Type `application/octet-stream` and a
|
|
|
|
/// fixed-size body containing the data in `self`. Always returns `Ok`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r, 'o: 'r> Responder<'r, 'o> for &'o [u8] {
|
|
|
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'o> {
|
2020-02-03 08:30:22 +00:00
|
|
|
Response::build()
|
|
|
|
.header(ContentType::Binary)
|
2020-06-19 13:01:10 +00:00
|
|
|
.sized_body(self.len(), Cursor::new(self))
|
2020-02-03 08:30:22 +00:00
|
|
|
.ok()
|
2018-07-14 20:48:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:48:39 +00:00
|
|
|
/// Returns a response with Content-Type `application/octet-stream` and a
|
|
|
|
/// fixed-size body containing the data in `self`. Always returns `Ok`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r> Responder<'r, 'static> for Vec<u8> {
|
|
|
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
2020-02-03 08:30:22 +00:00
|
|
|
Response::build()
|
|
|
|
.header(ContentType::Binary)
|
2020-06-19 13:01:10 +00:00
|
|
|
.sized_body(self.len(), Cursor::new(self))
|
2020-02-03 08:30:22 +00:00
|
|
|
.ok()
|
2017-09-22 01:48:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-19 10:29:08 +00:00
|
|
|
/// Returns a response with a sized body for the file. Always returns `Ok`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r> Responder<'r, 'static> for File {
|
|
|
|
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'static> {
|
|
|
|
tokio::fs::File::from(self).respond_to(req)
|
2020-01-29 22:58:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns a response with a sized body for the file. Always returns `Ok`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r> Responder<'r, 'static> for tokio::fs::File {
|
|
|
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
|
|
|
Response::build().sized_body(None, self).ok()
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 21:40:02 +00:00
|
|
|
/// Returns an empty, default `Response`. Always returns `Ok`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r> Responder<'r, 'static> for () {
|
|
|
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
2020-02-03 08:30:22 +00:00
|
|
|
Ok(Response::new())
|
2016-12-10 03:53:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 06:07:22 +00:00
|
|
|
/// Responds with the inner `Responder` in `Cow`.
|
|
|
|
impl<'r, 'o: 'r, R: ?Sized + ToOwned> Responder<'r, 'o> for std::borrow::Cow<'o, R>
|
|
|
|
where &'o R: Responder<'r, 'o> + 'o, <R as ToOwned>::Owned: Responder<'r, 'o> + 'r
|
|
|
|
{
|
|
|
|
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
|
|
|
|
match self {
|
|
|
|
std::borrow::Cow::Borrowed(b) => b.respond_to(req),
|
|
|
|
std::borrow::Cow::Owned(o) => o.respond_to(req),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-20 21:40:02 +00:00
|
|
|
/// If `self` is `Some`, responds with the wrapped `Responder`. Otherwise prints
|
|
|
|
/// a warning message and returns an `Err` of `Status::NotFound`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r, 'o: 'r, R: Responder<'r, 'o>> Responder<'r, 'o> for Option<R> {
|
|
|
|
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
|
2019-09-06 16:56:06 +00:00
|
|
|
match self {
|
|
|
|
Some(r) => r.respond_to(req),
|
|
|
|
None => {
|
|
|
|
warn_!("Response was `None`.");
|
2020-06-19 13:01:10 +00:00
|
|
|
Err(Status::NotFound)
|
2019-09-06 16:56:06 +00:00
|
|
|
},
|
|
|
|
}
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-03 08:30:22 +00:00
|
|
|
// Responds with the wrapped `Responder` in `self`, whether it is `Ok` or
|
2016-12-20 21:40:02 +00:00
|
|
|
/// `Err`.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r, 'o: 'r, 't: 'o, 'e: 'o, T, E> Responder<'r, 'o> for Result<T, E>
|
|
|
|
where T: Responder<'r, 't>, E: Responder<'r, 'e>
|
|
|
|
{
|
|
|
|
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
|
2019-09-06 16:56:06 +00:00
|
|
|
match self {
|
|
|
|
Ok(responder) => responder.respond_to(req),
|
|
|
|
Err(responder) => responder.respond_to(req),
|
|
|
|
}
|
2016-03-28 09:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2018-10-31 10:51:45 +00:00
|
|
|
|
2021-03-30 04:58:18 +00:00
|
|
|
// Responds with the wrapped `Responder` in `self`, whether it is `Left` or
|
|
|
|
/// `Right`.
|
|
|
|
impl<'r, 'o: 'r, 't: 'o, 'e: 'o, T, E> Responder<'r, 'o> for crate::Either<T, E>
|
|
|
|
where T: Responder<'r, 't>, E: Responder<'r, 'e>
|
|
|
|
{
|
|
|
|
fn respond_to(self, req: &'r Request<'_>) -> response::Result<'o> {
|
|
|
|
match self {
|
|
|
|
crate::Either::Left(r) => r.respond_to(req),
|
|
|
|
crate::Either::Right(r) => r.respond_to(req),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-31 10:51:45 +00:00
|
|
|
/// The response generated by `Status` depends on the status code itself. The
|
|
|
|
/// table below summarizes the functionality:
|
|
|
|
///
|
|
|
|
/// | Status Code Range | Response |
|
|
|
|
/// |-------------------|---------------------------------------|
|
|
|
|
/// | [400, 599] | Forwards to catcher for given status. |
|
|
|
|
/// | 100, [200, 205] | Empty with status of `self`. |
|
|
|
|
/// | All others. | Invalid. Errors to `500` catcher. |
|
|
|
|
///
|
|
|
|
/// In short, a client or server error status codes will forward to the
|
|
|
|
/// corresponding error catcher, a successful status code less than `206` or
|
|
|
|
/// `100` responds with any empty body and the given status code, and all other
|
|
|
|
/// status code emit an error message and forward to the `500` (internal server
|
|
|
|
/// error) catcher.
|
2020-06-19 13:01:10 +00:00
|
|
|
impl<'r> Responder<'r, 'static> for Status {
|
|
|
|
fn respond_to(self, _: &'r Request<'_>) -> response::Result<'static> {
|
2020-02-03 08:30:22 +00:00
|
|
|
match self.class() {
|
|
|
|
StatusClass::ClientError | StatusClass::ServerError => Err(self),
|
|
|
|
StatusClass::Success if self.code < 206 => {
|
|
|
|
Response::build().status(self).ok()
|
2018-10-31 10:51:45 +00:00
|
|
|
}
|
2020-02-03 08:30:22 +00:00
|
|
|
StatusClass::Informational if self.code == 100 => {
|
|
|
|
Response::build().status(self).ok()
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
error_!("Invalid status used as responder: {}.", self);
|
|
|
|
Err(Status::InternalServerError)
|
|
|
|
}
|
|
|
|
}
|
2018-10-31 10:51:45 +00:00
|
|
|
}
|
|
|
|
}
|