Skip to content

Commit

Permalink
fix(core): HTTP headers being overwritten by reqwest, closes #2032 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasfernog authored Jun 21, 2021
1 parent 285bf64 commit 1006c1c
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 27 deletions.
5 changes: 5 additions & 0 deletions .changes/fix-reqwest-headers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"tauri": patch
---

Fixes HTTP API headers being overwritten when using the `reqwest` client.
6 changes: 5 additions & 1 deletion core/tauri/src/api/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ pub enum Error {
/// HTTP method error.
#[error("{0}")]
HttpMethod(#[from] http::method::InvalidMethod),
/// Invalid HTTO header.
/// Invalid HTTP header value.
#[cfg(feature = "reqwest-client")]
#[error("{0}")]
HttpHeaderValue(#[from] http::header::InvalidHeaderValue),
/// Invalid HTTP header value.
#[error("{0}")]
HttpHeader(#[from] http::header::InvalidHeaderName),
/// Failed to serialize header value as string.
Expand Down
45 changes: 19 additions & 26 deletions core/tauri/src/api/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,32 +148,15 @@ impl Client {
request_builder = request_builder.query(&query);
}

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);
}
}

if let Some(timeout) = request.timeout {
request_builder = request_builder.timeout(Duration::from_secs(timeout));
}

let response = if let Some(body) = request.body {
match body {
Body::Bytes(data) => {
request_builder
.body(bytes::Bytes::from(data))
.send()
.await?
}
Body::Text(text) => {
request_builder
.body(bytes::Bytes::from(text))
.send()
.await?
}
Body::Json(json) => request_builder.json(&json).send().await?,
if let Some(body) = request.body {
request_builder = match body {
Body::Bytes(data) => request_builder.body(bytes::Bytes::from(data)),
Body::Text(text) => request_builder.body(bytes::Bytes::from(text)),
Body::Json(json) => request_builder.json(&json),
Body::Form(form_body) => {
let mut form = Vec::new();
for (name, part) in form_body.0 {
Expand All @@ -183,12 +166,22 @@ impl Client {
FormPart::Text(text) => form.push((name, text)),
}
}
request_builder.form(&form).send().await?
request_builder.form(&form)
}
};
}

let mut http_request = request_builder.build()?;
if let Some(headers) = request.headers {
for (header, value) in headers.iter() {
http_request.headers_mut().insert(
HeaderName::from_bytes(header.as_bytes())?,
http::header::HeaderValue::from_str(value)?,
);
}
} else {
request_builder.send().await?
};
}

let response = self.0.execute(http_request).await?;

if response.status().is_success() {
Ok(Response(
Expand Down

0 comments on commit 1006c1c

Please sign in to comment.