mirror of
https://github.com/fafhrd91/actix-web
synced 2024-12-04 12:21:56 +01:00
added HttpRequest::content_type(), query() method returns HashMap
This commit is contained in:
parent
d93244aa4f
commit
b2670c94f4
@ -85,8 +85,12 @@ impl HttpRequest {
|
|||||||
|
|
||||||
/// Return a new iterator that yields pairs of `Cow<str>` for query parameters
|
/// Return a new iterator that yields pairs of `Cow<str>` for query parameters
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn query(&self) -> form_urlencoded::Parse {
|
pub fn query(&self) -> HashMap<String, String> {
|
||||||
form_urlencoded::parse(self.query.as_ref())
|
let mut q: HashMap<String, String> = 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.
|
/// 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
|
/// Check if request requires connection upgrade
|
||||||
pub(crate) fn upgrade(&self) -> bool {
|
pub(crate) fn upgrade(&self) -> bool {
|
||||||
if let Some(conn) = self.headers().get(header::CONNECTION) {
|
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 {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
let res = write!(f, "\nHttpRequest {:?} {}:{}\n",
|
let res = write!(f, "\nHttpRequest {:?} {}:{}\n",
|
||||||
self.version, self.method, self.path);
|
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() {
|
if !self.params.is_empty() {
|
||||||
let _ = write!(f, " params: {:?}\n", self.params);
|
let _ = write!(f, " params: {:?}\n", self.params);
|
||||||
}
|
}
|
||||||
|
@ -72,9 +72,8 @@ fn test_request_query() {
|
|||||||
Version::HTTP_11, HeaderMap::new(), "id=test".to_owned());
|
Version::HTTP_11, HeaderMap::new(), "id=test".to_owned());
|
||||||
|
|
||||||
assert_eq!(req.query_string(), "id=test");
|
assert_eq!(req.query_string(), "id=test");
|
||||||
let query: Vec<_> = req.query().collect();
|
let query = req.query();
|
||||||
assert_eq!(query[0].0.as_ref(), "id");
|
assert_eq!(query.get("id").unwrap(), "test");
|
||||||
assert_eq!(query[0].1.as_ref(), "test");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
Loading…
Reference in New Issue
Block a user