fix(core): HTTP headers being overwritten by reqwest, closes #2032 (#2036)

This commit is contained in:
Lucas Fernandes Nogueira 2021-06-21 13:47:21 -03:00 committed by GitHub
parent 285bf64bf9
commit 1006c1cf3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 27 deletions

View File

@ -0,0 +1,5 @@
---
"tauri": patch
---
Fixes HTTP API headers being overwritten when using the `reqwest` client.

View File

@ -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.

View File

@ -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 {
@ -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(