From 571e2ac845dd389c1f292a49aa860513bfc6d7b1 Mon Sep 17 00:00:00 2001 From: Jeb Rosen Date: Sun, 15 Dec 2019 11:01:10 -0800 Subject: [PATCH] Revert incoming request URI and header parsing to more closely match 0.4. --- core/http/src/hyper.rs | 2 +- core/lib/src/request/request.rs | 17 ++++++++--------- core/lib/src/rocket.rs | 2 +- 3 files changed, 10 insertions(+), 11 deletions(-) diff --git a/core/http/src/hyper.rs b/core/http/src/hyper.rs index 13725130..c84102e9 100644 --- a/core/http/src/hyper.rs +++ b/core/http/src/hyper.rs @@ -17,7 +17,7 @@ #[doc(hidden)] pub use http::request::Parts as RequestParts; #[doc(hidden)] pub use http::response::Builder as ResponseBuilder; #[doc(hidden)] pub use http::status::StatusCode; -#[doc(hidden)] pub use http::uri::Uri; +#[doc(hidden)] pub use http::uri::{Uri, Parts as UriParts}; /// Reexported http header types. pub mod header { diff --git a/core/lib/src/request/request.rs b/core/lib/src/request/request.rs index bb3a0b03..3bc260d2 100644 --- a/core/lib/src/request/request.rs +++ b/core/lib/src/request/request.rs @@ -822,13 +822,14 @@ impl<'r> Request<'r> { rocket: &'r Rocket, h_method: hyper::Method, h_headers: hyper::HeaderMap, - h_uri: hyper::Uri, + h_uri: &'r hyper::Uri, h_addr: SocketAddr, ) -> Result, String> { - // TODO.async: Can we avoid this allocation? - // TODO.async: Assert that uri is "absolute" - // Get a copy of the URI for later use. - let uri = h_uri.to_string(); + // Get a copy of the URI (only supports path-and-query) for later use. + let uri = match (h_uri.scheme(), h_uri.authority(), h_uri.path_and_query()) { + (None, None, Some(paq)) => paq.as_str(), + _ => return Err(format!("Bad URI: {}", h_uri)), + }; // Ensure that the method is known. TODO: Allow made-up methods? let method = match Method::from_hyp(&h_method) { @@ -837,7 +838,7 @@ impl<'r> Request<'r> { }; // We need to re-parse the URI since we don't trust Hyper... :( - let uri = Origin::parse_owned(format!("{}", uri)).map_err(|e| e.to_string())?; + let uri = Origin::parse(uri).map_err(|e| e.to_string())?; // Construct the request object. let mut request = Request::new(rocket, method, uri); @@ -846,9 +847,7 @@ impl<'r> Request<'r> { // Set the request cookies, if they exist. let mut cookie_jar = CookieJar::new(); for header in h_headers.get_all("Cookie") { - // TODO.async: This used to only allow UTF-8 but now only allows ASCII - // (needs verification) - let raw_str = match header.to_str() { + let raw_str = match std::str::from_utf8(header.as_bytes()) { Ok(string) => string, Err(_) => continue }; diff --git a/core/lib/src/rocket.rs b/core/lib/src/rocket.rs index 8160047f..1598ec6c 100644 --- a/core/lib/src/rocket.rs +++ b/core/lib/src/rocket.rs @@ -65,7 +65,7 @@ fn hyper_service_fn( let (h_parts, h_body) = hyp_req.into_parts(); // Convert the Hyper request into a Rocket request. - let req_res = Request::from_hyp(&rocket, h_parts.method, h_parts.headers, h_parts.uri, h_addr); + let req_res = Request::from_hyp(&rocket, h_parts.method, h_parts.headers, &h_parts.uri, h_addr); let mut req = match req_res { Ok(req) => req, Err(e) => {