Skip to content

Commit

Permalink
feat(core): improve HeaderValue compatibility, closes #2162 (#2438)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog committed Aug 15, 2021
1 parent 6be3f43 commit 1635798
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .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.
22 changes: 11 additions & 11 deletions core/tauri/src/api/error.rs
Expand Up @@ -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<bincode::ErrorKind>),
/// 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}")]
Expand Down
13 changes: 9 additions & 4 deletions core/tauri/src/api/http.rs
Expand Up @@ -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(),
);
}
}

Expand Down Expand Up @@ -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())?,
);
}
}
Expand Down Expand Up @@ -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();

Expand Down

0 comments on commit 1635798

Please sign in to comment.