From 1cf33831d9837fb778b01c92d07b497fec025129 Mon Sep 17 00:00:00 2001 From: Martichou Date: Mon, 7 Dec 2020 22:01:11 +0100 Subject: [PATCH] Wrong parsing made by serde_urlencoded for Option, replacing with serde_qs --- Cargo.toml | 2 +- actix-http-test/Cargo.toml | 2 +- actix-http/Cargo.toml | 2 +- actix-http/src/error.rs | 2 +- awc/Cargo.toml | 2 +- awc/src/request.rs | 7 ++----- awc/src/sender.rs | 2 +- src/error.rs | 4 ++-- src/test.rs | 2 +- src/types/form.rs | 8 +++----- src/types/query.rs | 4 ++-- 11 files changed, 16 insertions(+), 21 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 31c4cca7e..5228368aa 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -102,7 +102,7 @@ pin-project = "1.0.0" regex = "1.4" serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" -serde_urlencoded = "0.7" +serde_qs = "0.8.1" time = { version = "0.2.7", default-features = false, features = ["std"] } url = "2.1" open-ssl = { package = "openssl", version = "0.10", optional = true } diff --git a/actix-http-test/Cargo.toml b/actix-http-test/Cargo.toml index 8b23bef1c..777dfa944 100644 --- a/actix-http-test/Cargo.toml +++ b/actix-http-test/Cargo.toml @@ -47,7 +47,7 @@ socket2 = "0.3" serde = "1.0" serde_json = "1.0" slab = "0.4" -serde_urlencoded = "0.7" +serde_qs = "0.8.1" time = { version = "0.2.7", default-features = false, features = ["std"] } open-ssl = { version = "0.10", package = "openssl", optional = true } diff --git a/actix-http/Cargo.toml b/actix-http/Cargo.toml index 7375c6eb3..156160f84 100644 --- a/actix-http/Cargo.toml +++ b/actix-http/Cargo.toml @@ -78,7 +78,7 @@ serde = "1.0" serde_json = "1.0" sha-1 = "0.9" slab = "0.4" -serde_urlencoded = "0.7" +serde_qs = "0.8.1" time = { version = "0.2.7", default-features = false, features = ["std"] } # compression diff --git a/actix-http/src/error.rs b/actix-http/src/error.rs index e93c077af..c69618e83 100644 --- a/actix-http/src/error.rs +++ b/actix-http/src/error.rs @@ -17,7 +17,7 @@ use http::uri::InvalidUri; use http::{header, Error as HttpError, StatusCode}; use serde::de::value::Error as DeError; use serde_json::error::Error as JsonError; -use serde_urlencoded::ser::Error as FormError; +use serde_qs::Error as FormError; // re-export for convenience use crate::body::Body; diff --git a/awc/Cargo.toml b/awc/Cargo.toml index 3c1963d6b..a68177980 100644 --- a/awc/Cargo.toml +++ b/awc/Cargo.toml @@ -53,7 +53,7 @@ percent-encoding = "2.1" rand = "0.7" serde = "1.0" serde_json = "1.0" -serde_urlencoded = "0.7" +serde_qs = "0.8.1" open-ssl = { version = "0.10", package = "openssl", optional = true } rust-tls = { version = "0.18.0", package = "rustls", optional = true, features = ["dangerous_configuration"] } diff --git a/awc/src/request.rs b/awc/src/request.rs index 1e49aae3c..cc1260e3f 100644 --- a/awc/src/request.rs +++ b/awc/src/request.rs @@ -383,14 +383,11 @@ impl ClientRequest { } /// Sets the query part of the request - pub fn query( - mut self, - query: &T, - ) -> Result { + pub fn query(mut self, query: &T) -> Result { let mut parts = self.head.uri.clone().into_parts(); if let Some(path_and_query) = parts.path_and_query { - let query = serde_urlencoded::to_string(query)?; + let query = serde_qs::to_string(query)?; let path = path_and_query.path(); parts.path_and_query = format!("{}?{}", path, query).parse().ok(); diff --git a/awc/src/sender.rs b/awc/src/sender.rs index 0bcdf4307..37a0c4389 100644 --- a/awc/src/sender.rs +++ b/awc/src/sender.rs @@ -231,7 +231,7 @@ impl RequestSender { config: &ClientConfig, value: &T, ) -> SendClientRequest { - let body = match serde_urlencoded::to_string(value) { + let body = match serde_qs::to_string(value) { Ok(body) => body, Err(e) => return Error::from(e).into(), }; diff --git a/src/error.rs b/src/error.rs index 60af8fa11..a062ed05e 100644 --- a/src/error.rs +++ b/src/error.rs @@ -120,7 +120,7 @@ impl ResponseError for PathError { pub enum QueryPayloadError { /// Deserialize error #[display(fmt = "Query deserialize error: {}", _0)] - Deserialize(serde::de::value::Error), + Deserialize(serde_qs::Error), } impl std::error::Error for QueryPayloadError {} @@ -188,7 +188,7 @@ mod tests { #[test] fn test_query_payload_error() { let resp: HttpResponse = QueryPayloadError::Deserialize( - serde_urlencoded::from_str::("bad query").unwrap_err(), + serde_qs::from_str::("bad query").unwrap_err(), ) .error_response(); assert_eq!(resp.status(), StatusCode::BAD_REQUEST); diff --git a/src/test.rs b/src/test.rs index cff6c3e51..1f8c3f030 100644 --- a/src/test.rs +++ b/src/test.rs @@ -496,7 +496,7 @@ impl TestRequest { /// Serialize `data` to a URL encoded form and set it as the request payload. The `Content-Type` /// header is set to `application/x-www-form-urlencoded`. pub fn set_form(mut self, data: &T) -> Self { - let bytes = serde_urlencoded::to_string(data) + let bytes = serde_qs::to_string(data) .expect("Failed to serialize test data as a urlencoded form"); self.req.set_payload(bytes); self.req.set(ContentType::form_url_encoded()); diff --git a/src/types/form.rs b/src/types/form.rs index 2a7101287..2aa41f68b 100644 --- a/src/types/form.rs +++ b/src/types/form.rs @@ -162,7 +162,7 @@ impl Responder for Form { type Future = Ready>; fn respond_to(self, _: &HttpRequest) -> Self::Future { - let body = match serde_urlencoded::to_string(&self.0) { + let body = match serde_qs::to_string(&self.0) { Ok(body) => body, Err(e) => return err(e.into()), }; @@ -359,15 +359,13 @@ where } if encoding == UTF_8 { - serde_urlencoded::from_bytes::(&body) - .map_err(|_| UrlencodedError::Parse) + serde_qs::from_bytes::(&body).map_err(|_| UrlencodedError::Parse) } else { let body = encoding .decode_without_bom_handling_and_without_replacement(&body) .map(|s| s.into_owned()) .ok_or(UrlencodedError::Parse)?; - serde_urlencoded::from_str::(&body) - .map_err(|_| UrlencodedError::Parse) + serde_qs::from_str::(&body).map_err(|_| UrlencodedError::Parse) } } .boxed_local(), diff --git a/src/types/query.rs b/src/types/query.rs index 7eded49c5..2345471b6 100644 --- a/src/types/query.rs +++ b/src/types/query.rs @@ -64,7 +64,7 @@ impl Query { where T: de::DeserializeOwned, { - serde_urlencoded::from_str::(query_str) + serde_qs::from_str::(query_str) .map(|val| Ok(Query(val))) .unwrap_or_else(move |e| Err(QueryPayloadError::Deserialize(e))) } @@ -144,7 +144,7 @@ where .map(|c| c.ehandler.clone()) .unwrap_or(None); - serde_urlencoded::from_str::(req.query_string()) + serde_qs::from_str::(req.query_string()) .map(|val| ok(Query(val))) .unwrap_or_else(move |e| { let e = QueryPayloadError::Deserialize(e);