2016-11-03 17:54:37 +00:00
|
|
|
//! Contains types that set the Content-Type of a response.
|
|
|
|
//!
|
|
|
|
//! # Usage
|
|
|
|
//!
|
|
|
|
//! Each type wraps a given responder. The `Responder` implementation of each
|
|
|
|
//! type simply sets the Content-Type and delegates the remainder of the
|
|
|
|
//! response to the wrapped responder. This is useful for setting the
|
|
|
|
//! Content-Type of a type that doesn't set it itself or for overriding one that
|
|
|
|
//! does.
|
|
|
|
//!
|
|
|
|
//! # Example
|
|
|
|
//!
|
|
|
|
//! The following snippet creates an `HTML` content response for a string.
|
|
|
|
//! Normally, raw strings set their response Content-Type to `text/plain`. By
|
|
|
|
//! using the `HTML` content response, the Content-Type will be set to
|
|
|
|
//! `text/html` instead.
|
|
|
|
//!
|
|
|
|
//! ```rust
|
|
|
|
//! use rocket::response::content;
|
|
|
|
//!
|
|
|
|
//! let response = content::HTML("<h1>Hello, world!</h1>");
|
|
|
|
//! ```
|
|
|
|
|
2016-12-15 08:47:31 +00:00
|
|
|
use response::{Response, Responder};
|
|
|
|
use http::{Status, ContentType};
|
2016-09-25 10:06:02 +00:00
|
|
|
|
2016-11-03 17:54:37 +00:00
|
|
|
/// Set the Content-Type to any arbitrary value.
|
|
|
|
///
|
|
|
|
/// Delagates the remainder of the response to the wrapped responder.
|
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// Set the Content-Type of a string to be PDF.
|
|
|
|
///
|
|
|
|
/// ```rust
|
|
|
|
/// use rocket::response::content::Content;
|
|
|
|
/// use rocket::http::ContentType;
|
|
|
|
///
|
|
|
|
/// let response = Content(ContentType::from_extension("pdf"), "Hi.");
|
|
|
|
/// ```
|
2016-11-03 14:09:01 +00:00
|
|
|
#[derive(Debug)]
|
2016-12-15 08:47:31 +00:00
|
|
|
pub struct Content<R>(pub ContentType, pub R);
|
2016-09-25 10:06:02 +00:00
|
|
|
|
2016-11-03 17:54:37 +00:00
|
|
|
/// Sets the Content-Type of the response to the wrapped `ContentType` then
|
|
|
|
/// delegates the remainder of the response to the wrapped responder.
|
2016-12-15 08:47:31 +00:00
|
|
|
impl<'r, R: Responder<'r>> Responder<'r> for Content<R> {
|
|
|
|
#[inline(always)]
|
|
|
|
fn respond(self) -> Result<Response<'r>, Status> {
|
|
|
|
Response::build()
|
|
|
|
.merge(self.1.respond()?)
|
|
|
|
.header(self.0)
|
|
|
|
.ok()
|
2016-09-25 10:06:02 +00:00
|
|
|
}
|
|
|
|
}
|
2016-08-27 01:37:28 +00:00
|
|
|
|
2016-11-03 17:54:37 +00:00
|
|
|
macro_rules! ctrs {
|
2016-12-15 08:47:31 +00:00
|
|
|
($($name:ident: $name_str:expr, $ct_str:expr),+) => {
|
2016-11-03 17:54:37 +00:00
|
|
|
$(
|
2016-12-15 08:47:31 +00:00
|
|
|
#[doc="Set the `Content-Type` of the response to <b>"]
|
|
|
|
#[doc=$name_str]
|
|
|
|
#[doc="</b>, or <i>"]
|
|
|
|
#[doc=$ct_str]
|
|
|
|
#[doc="</i>."]
|
2016-11-03 17:54:37 +00:00
|
|
|
///
|
|
|
|
/// Delagates the remainder of the response to the wrapped responder.
|
|
|
|
#[derive(Debug)]
|
2016-12-15 08:47:31 +00:00
|
|
|
pub struct $name<R>(pub R);
|
2016-11-03 17:54:37 +00:00
|
|
|
|
|
|
|
/// Sets the Content-Type of the response then delegates the
|
|
|
|
/// remainder of the response to the wrapped responder.
|
2016-12-15 08:47:31 +00:00
|
|
|
impl<'r, R: Responder<'r>> Responder<'r> for $name<R> {
|
|
|
|
fn respond(self) -> Result<Response<'r>, Status> {
|
|
|
|
Content(ContentType::$name, self.0).respond()
|
2016-11-03 17:54:37 +00:00
|
|
|
}
|
2016-12-15 08:47:31 +00:00
|
|
|
}
|
|
|
|
)+
|
2016-11-03 17:54:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctrs! {
|
2016-12-15 08:47:31 +00:00
|
|
|
JSON: "JSON", "application/json",
|
|
|
|
XML: "XML", "text/xml",
|
|
|
|
HTML: "HTML", "text/html",
|
|
|
|
Plain: "plain text", "text/plain",
|
|
|
|
CSS: "CSS", "text/css",
|
|
|
|
JavaScript: "JavaScript", "application/javascript"
|
2016-08-27 01:37:28 +00:00
|
|
|
}
|
|
|
|
|