From 004cae762769116abfb7ab0dc5b4ee27b228e16e Mon Sep 17 00:00:00 2001 From: Sergio Benitez Date: Wed, 2 Nov 2016 17:39:41 +0100 Subject: [PATCH] Fix codegen tests for new lib. Make UTF8 charset the default for text content types. --- codegen/tests/run-pass/segments.rs | 3 +- lib/src/http/content_type.rs | 46 +++++++++++++++++++----------- lib/src/http/mod.rs | 1 - lib/src/response/responder.rs | 2 +- 4 files changed, 33 insertions(+), 19 deletions(-) diff --git a/codegen/tests/run-pass/segments.rs b/codegen/tests/run-pass/segments.rs index f9c18980..fb22fd49 100644 --- a/codegen/tests/run-pass/segments.rs +++ b/codegen/tests/run-pass/segments.rs @@ -4,6 +4,7 @@ extern crate rocket; use std::path::PathBuf; +use std::str::Utf8Error; #[post("//")] fn get(a: String, b: PathBuf) -> String { @@ -11,7 +12,7 @@ fn get(a: String, b: PathBuf) -> String { } #[post("//")] -fn get2(a: String, b: Result) -> String { +fn get2(a: String, b: Result) -> String { format!("{}/{}", a, b.unwrap().to_string_lossy()) } diff --git a/lib/src/http/content_type.rs b/lib/src/http/content_type.rs index e4a9f363..985b21ef 100644 --- a/lib/src/http/content_type.rs +++ b/lib/src/http/content_type.rs @@ -4,7 +4,7 @@ use std::str::FromStr; use std::borrow::Borrow; use std::fmt; -use http::mime::{Mime, Param, TopLevel, SubLevel}; +use http::mime::{Mime, Param, Attr, Value, TopLevel, SubLevel}; use router::Collider; /// Typed representation of HTTP Content-Types. @@ -45,7 +45,7 @@ impl ContentType { } /// Constructs a new content type of the given top level and sub level - /// types. + /// types. If the top-level type is `Text`, a charset of UTF-8 is set. /// /// # Examples /// @@ -53,12 +53,26 @@ impl ContentType { /// use rocket::http::ContentType; /// use rocket::http::mime::{TopLevel, SubLevel}; /// - /// let html = ContentType::of(TopLevel::Application, SubLevel::Html); - /// assert!(html.is_html()); + /// let ct = ContentType::of(TopLevel::Text, SubLevel::Html); + /// assert_eq!(ct.to_string(), "text/html; charset=utf-8".to_string()); + /// assert!(ct.is_html()); + /// ``` + /// + /// ```rust + /// use rocket::http::ContentType; + /// use rocket::http::mime::{TopLevel, SubLevel}; + /// + /// let ct = ContentType::of(TopLevel::Application, SubLevel::Json); + /// assert_eq!(ct.to_string(), "application/json".to_string()); + /// assert!(ct.is_json()); /// ``` #[inline(always)] pub fn of(t: TopLevel, s: SubLevel) -> ContentType { - ContentType(t, s, None) + if t == TopLevel::Text { + ContentType(t, s, Some(vec![(Attr::Charset, Value::Utf8)])) + } else { + ContentType(t, s, None) + } } /// Returns the Content-Type associated with the extension `ext`. Not all @@ -88,7 +102,7 @@ impl ContentType { let (top_level, sub_level) = match ext { "txt" => (TopLevel::Text, SubLevel::Plain), "html" | "htm" => (TopLevel::Text, SubLevel::Html), - "xml" => (TopLevel::Application, SubLevel::Xml), + "xml" => (TopLevel::Text, SubLevel::Xml), "js" => (TopLevel::Application, SubLevel::Javascript), "css" => (TopLevel::Text, SubLevel::Css), "json" => (TopLevel::Application, SubLevel::Json), @@ -111,11 +125,11 @@ impl ContentType { /// Returns a `ContentType` representing JSON, i.e, `application/json`. | json: Application/Json, - /// Returns a `ContentType` representing XML, i.e, `application/xml`. - | xml: Application/Xml, + /// Returns a `ContentType` representing XML, i.e, `text/xml`. + | xml: Text/Xml, - /// Returns a `ContentType` representing HTML, i.e, `application/html`. - | html: Application/Html + /// Returns a `ContentType` representing HTML, i.e, `text/html`. + | html: Text/Html } /// Returns true if this content type is not one of the standard content @@ -137,14 +151,14 @@ impl ContentType { /// Returns true if the content type is JSON, i.e: `application/json`. | is_json: Application/Json, - /// Returns true if the content type is XML, i.e: `application/xml`. - | is_xml: Application/Xml, + /// Returns true if the content type is XML, i.e: `text/xml`. + | is_xml: Text/Xml, /// Returns true if the content type is any, i.e.: `*/*`. | is_any: Star/Star, - /// Returns true if the content type is HTML, i.e.: `application/html`. - | is_html: Application/Html, + /// Returns true if the content type is HTML, i.e.: `text/html`. + | is_html: Text/Html, /// Returns true if the content type is that for non-data HTTP forms, /// i.e.: `application/x-www-form-urlencoded`. @@ -287,8 +301,8 @@ impl fmt::Display for ContentType { /// ```rust /// use rocket::http::ContentType; /// - /// let http_ct = format!("{}", ContentType::xml()); - /// assert_eq!(http_ct, "application/xml".to_string()); + /// let ct = format!("{}", ContentType::json()); + /// assert_eq!(ct, "application/json".to_string()); /// ``` fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "{}/{}", self.0.as_str(), self.1.as_str())?; diff --git a/lib/src/http/mod.rs b/lib/src/http/mod.rs index e80f0f79..a3751d43 100644 --- a/lib/src/http/mod.rs +++ b/lib/src/http/mod.rs @@ -19,5 +19,4 @@ pub use self::method::Method; pub use self::hyper::StatusCode; pub use self::content_type::ContentType; -/// Can I document it here? pub use self::cookies::{Cookie, Cookies}; diff --git a/lib/src/response/responder.rs b/lib/src/response/responder.rs index 27ece089..7524a43a 100644 --- a/lib/src/response/responder.rs +++ b/lib/src/response/responder.rs @@ -20,7 +20,7 @@ impl<'a, T, E> IntoOutcome<(), (), (StatusCode, FreshHyperResponse<'a>)> for Res } pub trait Responder { - fn respond<'a>(&mut self, mut res: FreshHyperResponse<'a>) -> Outcome<'a>; + fn respond<'a>(&mut self, res: FreshHyperResponse<'a>) -> Outcome<'a>; } impl<'a> Responder for &'a str {