ソースを参照

feat(core): improve HeaderValue compatibility, closes #2162 (#2438)

Lucas Fernandes Nogueira 4 年 前
コミット
1635798a66
3 ファイル変更25 行追加15 行削除
  1. 5 0
      .changes/header-value-bytes.md
  2. 11 11
      core/tauri/src/api/error.rs
  3. 9 4
      core/tauri/src/api/http.rs

+ 5 - 0
.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.

+ 11 - 11
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<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}")]

+ 9 - 4
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();