From b2670c94f4392de7af1d9e5a335663af4262d115 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Sun, 29 Oct 2017 06:03:21 -0700 Subject: [PATCH] added HttpRequest::content_type(), query() method returns HashMap --- src/httprequest.rs | 21 +++++++++++++++++++-- tests/test_httprequest.rs | 5 ++--- 2 files changed, 21 insertions(+), 5 deletions(-) diff --git a/src/httprequest.rs b/src/httprequest.rs index 7287e4faf..d9ca2c65c 100644 --- a/src/httprequest.rs +++ b/src/httprequest.rs @@ -85,8 +85,12 @@ impl HttpRequest { /// Return a new iterator that yields pairs of `Cow` for query parameters #[inline] - pub fn query(&self) -> form_urlencoded::Parse { - form_urlencoded::parse(self.query.as_ref()) + pub fn query(&self) -> HashMap { + let mut q: HashMap = HashMap::new(); + for (key, val) in form_urlencoded::parse(self.query.as_ref()) { + q.insert(key.to_string(), val.to_string()); + } + q } /// The query string in the URL. @@ -155,6 +159,16 @@ impl HttpRequest { } } + /// Read the request content type + pub fn content_type(&self) -> &str { + if let Some(content_type) = self.headers.get(header::CONTENT_TYPE) { + if let Ok(content_type) = content_type.to_str() { + return content_type + } + } + "" + } + /// Check if request requires connection upgrade pub(crate) fn upgrade(&self) -> bool { if let Some(conn) = self.headers().get(header::CONNECTION) { @@ -241,6 +255,9 @@ impl fmt::Debug for HttpRequest { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { let res = write!(f, "\nHttpRequest {:?} {}:{}\n", self.version, self.method, self.path); + if !self.query_string().is_empty() { + let _ = write!(f, " query: ?{:?}\n", self.query_string()); + } if !self.params.is_empty() { let _ = write!(f, " params: {:?}\n", self.params); } diff --git a/tests/test_httprequest.rs b/tests/test_httprequest.rs index 298fcf9dc..6f058443e 100644 --- a/tests/test_httprequest.rs +++ b/tests/test_httprequest.rs @@ -72,9 +72,8 @@ fn test_request_query() { Version::HTTP_11, HeaderMap::new(), "id=test".to_owned()); assert_eq!(req.query_string(), "id=test"); - let query: Vec<_> = req.query().collect(); - assert_eq!(query[0].0.as_ref(), "id"); - assert_eq!(query[0].1.as_ref(), "test"); + let query = req.query(); + assert_eq!(query.get("id").unwrap(), "test"); } #[test]