diff --git a/core/lib/src/response/status.rs b/core/lib/src/response/status.rs index 22215f74..34de8ce8 100644 --- a/core/lib/src/response/status.rs +++ b/core/lib/src/response/status.rs @@ -111,6 +111,31 @@ impl<'r, R: Responder<'r>> Responder<'r> for Accepted { } } +/// Sets the status of the response to 204 (No Content). +/// +/// The response body will be empty. +/// +/// # Example +/// +/// A 204 No Content response: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::NoContent; +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct NoContent; + +/// Sets the status code of the response to 204 No Content. +impl<'r> Responder<'r> for NoContent { + fn respond_to(self, _: &Request<'_>) -> Result, Status> { + let mut build = Response::build(); + build.status(Status::NoContent).ok() + } +} + /// Sets the status of the response to 400 (Bad Request). /// /// If a responder is supplied, the remainder of the response is delegated to @@ -151,6 +176,86 @@ impl<'r, R: Responder<'r>> Responder<'r> for BadRequest { } } +/// Sets the status of the response to 401 (Unauthorized). +/// +/// If a responder is supplied, the remainder of the response is delegated to +/// it. If there is no responder, the body of the response will be empty. +/// +/// # Examples +/// +/// A 401 Unauthorized response without a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::Unauthorized::<()>(None); +/// ``` +/// +/// A 401 Unauthorized response _with_ a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::Unauthorized(Some("error message")); +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct Unauthorized(pub Option); + +/// Sets the status code of the response to 401 Unauthorized. If the responder is +/// `Some`, it is used to finalize the response. +impl<'r, R: Responder<'r>> Responder<'r> for Unauthorized { + fn respond_to(self, req: &Request<'_>) -> Result, Status> { + let mut build = Response::build(); + if let Some(responder) = self.0 { + build.merge(responder.respond_to(req)?); + } + + build.status(Status::Unauthorized).ok() + } +} + +/// Sets the status of the response to 403 (Forbidden). +/// +/// If a responder is supplied, the remainder of the response is delegated to +/// it. If there is no responder, the body of the response will be empty. +/// +/// # Examples +/// +/// A 403 Forbidden response without a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::Forbidden::<()>(None); +/// ``` +/// +/// A 403 Forbidden response _with_ a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::Forbidden(Some("error message")); +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct Forbidden(pub Option); + +/// Sets the status code of the response to 403 Forbidden. If the responder is +/// `Some`, it is used to finalize the response. +impl<'r, R: Responder<'r>> Responder<'r> for Forbidden { + fn respond_to(self, req: &Request<'_>) -> Result, Status> { + let mut build = Response::build(); + if let Some(responder) = self.0 { + build.merge(responder.respond_to(req)?); + } + + build.status(Status::Forbidden).ok() + } +} + /// Sets the status of the response to 404 (Not Found). /// /// The remainder of the response is delegated to the wrapped `Responder`. @@ -175,6 +280,47 @@ impl<'r, R: Responder<'r>> Responder<'r> for NotFound { } } + +/// Sets the status of the response to 409 (Conflict). +/// +/// If a responder is supplied, the remainder of the response is delegated to +/// it. If there is no responder, the body of the response will be empty. +/// +/// # Examples +/// +/// A 409 Conflict response without a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::Conflict::<()>(None); +/// ``` +/// +/// A 409 Conflict response _with_ a body: +/// +/// ```rust +/// use rocket::response::status; +/// +/// # #[allow(unused_variables)] +/// let response = status::Conflict(Some("error message")); +/// ``` +#[derive(Debug, Clone, PartialEq)] +pub struct Conflict(pub Option); + +/// Sets the status code of the response to 409 Conflict. If the responder is +/// `Some`, it is used to finalize the response. +impl<'r, R: Responder<'r>> Responder<'r> for Conflict { + fn respond_to(self, req: &Request<'_>) -> Result, Status> { + let mut build = Response::build(); + if let Some(responder) = self.0 { + build.merge(responder.respond_to(req)?); + } + + build.status(Status::Conflict).ok() + } +} + /// Creates a response with the given status code and underlying responder. /// /// # Example