From 1635798a669ced825577941dc66f1334c5904b43 Mon Sep 17 00:00:00 2001 From: Lucas Fernandes Nogueira Date: Sun, 15 Aug 2021 18:14:40 -0300 Subject: [PATCH] feat(core): improve HeaderValue compatibility, closes #2162 (#2438) --- .changes/header-value-bytes.md | 5 +++++ core/tauri/src/api/error.rs | 22 +++++++++++----------- core/tauri/src/api/http.rs | 13 +++++++++---- 3 files changed, 25 insertions(+), 15 deletions(-) create mode 100644 .changes/header-value-bytes.md diff --git a/.changes/header-value-bytes.md b/.changes/header-value-bytes.md new file mode 100644 index 000000000..a80e96f82 --- /dev/null +++ b/.changes/header-value-bytes.md @@ -0,0 +1,5 @@ +--- +"tauri": patch +--- + +Use `HeaderValue::from_bytes` instead of `HeaderValue::from_str` and `HeaderValue#to_bytes` instead of `HeaderValue#to_str` to improve compatibility. diff --git a/core/tauri/src/api/error.rs b/core/tauri/src/api/error.rs index 62800cbef..627923261 100644 --- a/core/tauri/src/api/error.rs +++ b/core/tauri/src/api/error.rs @@ -34,43 +34,43 @@ pub enum Error { #[error("Network Error: {0}")] Network(#[from] reqwest::Error), /// HTTP method error. - #[error("{0}")] + #[error(transparent)] HttpMethod(#[from] http::method::InvalidMethod), /// Invalid HTTP header value. #[cfg(feature = "reqwest-client")] #[cfg_attr(doc_cfg, doc(cfg(feature = "reqwest-client")))] - #[error("{0}")] + #[error(transparent)] HttpHeaderValue(#[from] http::header::InvalidHeaderValue), /// Invalid HTTP header value. - #[error("{0}")] + #[error(transparent)] HttpHeader(#[from] http::header::InvalidHeaderName), /// Failed to serialize header value as string. - #[error("failed to convert response header value to string")] - HttpHeaderToString(#[from] http::header::ToStrError), + #[error(transparent)] + Utf8(#[from] std::string::FromUtf8Error), /// HTTP form to must be an object. #[error("http form must be an object")] InvalidHttpForm, /// Semver error. - #[error("{0}")] + #[error(transparent)] Semver(#[from] semver::Error), /// JSON error. - #[error("{0}")] + #[error(transparent)] Json(#[from] serde_json::Error), /// Bincode error. - #[error("{0}")] + #[error(transparent)] Bincode(#[from] Box), /// IO error. - #[error("{0}")] + #[error(transparent)] Io(#[from] std::io::Error), /// Ignore error. #[error("failed to walkdir: {0}")] Ignore(#[from] ignore::Error), /// ZIP error. - #[error("{0}")] + #[error(transparent)] Zip(#[from] zip::result::ZipError), /// Notification error. #[cfg(notification_all)] - #[error("{0}")] + #[error(transparent)] Notification(#[from] notify_rust::error::Error), /// failed to detect the current platform. #[error("failed to detect platform: {0}")] diff --git a/core/tauri/src/api/http.rs b/core/tauri/src/api/http.rs index 16a038241..e7c91803a 100644 --- a/core/tauri/src/api/http.rs +++ b/core/tauri/src/api/http.rs @@ -88,8 +88,10 @@ impl Client { if let Some(headers) = request.headers { for (header, header_value) in headers.iter() { - request_builder = - request_builder.header(HeaderName::from_bytes(header.as_bytes())?, header_value); + request_builder = request_builder.header( + HeaderName::from_bytes(header.as_bytes())?, + header_value.as_bytes(), + ); } } @@ -169,7 +171,7 @@ impl Client { for (header, value) in headers.iter() { http_request.headers_mut().insert( HeaderName::from_bytes(header.as_bytes())?, - http::header::HeaderValue::from_str(value)?, + http::header::HeaderValue::from_bytes(value.as_bytes())?, ); } } @@ -348,7 +350,10 @@ impl Response { let mut headers = HashMap::new(); for (name, value) in self.1.headers() { - headers.insert(name.as_str().to_string(), value.to_str()?.to_string()); + headers.insert( + name.as_str().to_string(), + String::from_utf8(value.as_bytes().to_vec())?, + ); } let status = self.1.status().as_u16();