Skip to content

Commit 1635798

Browse files
authored
feat(core): improve HeaderValue compatibility, closes #2162 (#2438)
1 parent 6be3f43 commit 1635798

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

.changes/header-value-bytes.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Use `HeaderValue::from_bytes` instead of `HeaderValue::from_str` and `HeaderValue#to_bytes` instead of `HeaderValue#to_str` to improve compatibility.

core/tauri/src/api/error.rs

+11-11
Original file line numberDiff line numberDiff line change
@@ -34,43 +34,43 @@ pub enum Error {
3434
#[error("Network Error: {0}")]
3535
Network(#[from] reqwest::Error),
3636
/// HTTP method error.
37-
#[error("{0}")]
37+
#[error(transparent)]
3838
HttpMethod(#[from] http::method::InvalidMethod),
3939
/// Invalid HTTP header value.
4040
#[cfg(feature = "reqwest-client")]
4141
#[cfg_attr(doc_cfg, doc(cfg(feature = "reqwest-client")))]
42-
#[error("{0}")]
42+
#[error(transparent)]
4343
HttpHeaderValue(#[from] http::header::InvalidHeaderValue),
4444
/// Invalid HTTP header value.
45-
#[error("{0}")]
45+
#[error(transparent)]
4646
HttpHeader(#[from] http::header::InvalidHeaderName),
4747
/// Failed to serialize header value as string.
48-
#[error("failed to convert response header value to string")]
49-
HttpHeaderToString(#[from] http::header::ToStrError),
48+
#[error(transparent)]
49+
Utf8(#[from] std::string::FromUtf8Error),
5050
/// HTTP form to must be an object.
5151
#[error("http form must be an object")]
5252
InvalidHttpForm,
5353
/// Semver error.
54-
#[error("{0}")]
54+
#[error(transparent)]
5555
Semver(#[from] semver::Error),
5656
/// JSON error.
57-
#[error("{0}")]
57+
#[error(transparent)]
5858
Json(#[from] serde_json::Error),
5959
/// Bincode error.
60-
#[error("{0}")]
60+
#[error(transparent)]
6161
Bincode(#[from] Box<bincode::ErrorKind>),
6262
/// IO error.
63-
#[error("{0}")]
63+
#[error(transparent)]
6464
Io(#[from] std::io::Error),
6565
/// Ignore error.
6666
#[error("failed to walkdir: {0}")]
6767
Ignore(#[from] ignore::Error),
6868
/// ZIP error.
69-
#[error("{0}")]
69+
#[error(transparent)]
7070
Zip(#[from] zip::result::ZipError),
7171
/// Notification error.
7272
#[cfg(notification_all)]
73-
#[error("{0}")]
73+
#[error(transparent)]
7474
Notification(#[from] notify_rust::error::Error),
7575
/// failed to detect the current platform.
7676
#[error("failed to detect platform: {0}")]

core/tauri/src/api/http.rs

+9-4
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ impl Client {
8888

8989
if let Some(headers) = request.headers {
9090
for (header, header_value) in headers.iter() {
91-
request_builder =
92-
request_builder.header(HeaderName::from_bytes(header.as_bytes())?, header_value);
91+
request_builder = request_builder.header(
92+
HeaderName::from_bytes(header.as_bytes())?,
93+
header_value.as_bytes(),
94+
);
9395
}
9496
}
9597

@@ -169,7 +171,7 @@ impl Client {
169171
for (header, value) in headers.iter() {
170172
http_request.headers_mut().insert(
171173
HeaderName::from_bytes(header.as_bytes())?,
172-
http::header::HeaderValue::from_str(value)?,
174+
http::header::HeaderValue::from_bytes(value.as_bytes())?,
173175
);
174176
}
175177
}
@@ -348,7 +350,10 @@ impl Response {
348350

349351
let mut headers = HashMap::new();
350352
for (name, value) in self.1.headers() {
351-
headers.insert(name.as_str().to_string(), value.to_str()?.to_string());
353+
headers.insert(
354+
name.as_str().to_string(),
355+
String::from_utf8(value.as_bytes().to_vec())?,
356+
);
352357
}
353358
let status = self.1.status().as_u16();
354359

0 commit comments

Comments
 (0)