mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-27 17:22:57 +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
|
||||
#[inline]
|
||||
pub fn query(&self) -> form_urlencoded::Parse {
|
||||
form_urlencoded::parse(self.query.as_ref())
|
||||
pub fn query(&self) -> HashMap<String, String> {
|
||||
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.
|
||||
@ -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);
|
||||
}
|
||||
|
@ -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]
|
||||
|
Loading…
Reference in New Issue
Block a user