Add Response::content_type() method.

This commit is contained in:
Sergio Benitez 2017-04-14 14:35:22 -07:00
parent 3bebdcc53d
commit 6641e9b92d
2 changed files with 23 additions and 6 deletions

View File

@ -310,10 +310,10 @@ impl<'r> Request<'r> {
self.extra.session = RefCell::new(jar);
}
/// Returns `Some` of the Content-Type header of `self`. If the header is
/// not present, returns `None`. The Content-Type header is cached after the
/// first call to this function. As a result, subsequent calls will always
/// return the same value.
/// Returns the Content-Type header of `self`. If the header is not present,
/// returns `None`. The Content-Type header is cached after the first call
/// to this function. As a result, subsequent calls will always return the
/// same value.
///
/// # Example
///

View File

@ -1,9 +1,8 @@
use std::{io, fmt, str};
use std::borrow::Cow;
use http::{Header, HeaderMap};
use response::Responder;
use http::Status;
use http::{Header, HeaderMap, Status, ContentType};
/// The default size, in bytes, of a chunk for streamed responses.
pub const DEFAULT_CHUNK_SIZE: u64 = 4096;
@ -641,6 +640,24 @@ impl<'r> Response<'r> {
self.status = Some(status);
}
/// Returns the Content-Type header of `self`. If the header is not present
/// or is malformed, returns `None`.
///
/// # Example
///
/// ```rust
/// use rocket::Response;
/// use rocket::http::ContentType;
///
/// let mut response = Response::new();
/// response.set_header(ContentType::HTML);
/// assert_eq!(response.content_type(), Some(ContentType::HTML));
/// ```
#[inline(always)]
pub fn content_type(&self) -> Option<ContentType> {
self.headers().get_one("Content-Type").and_then(|v| v.parse().ok())
}
/// Sets the status of `self` to a custom `status` with status code `code`
/// and reason phrase `reason`. This method should be used sparingly; prefer
/// to use [set_status](#method.set_status) instead.