mirror of
https://github.com/tauri-apps/tauri-plugin-http.git
synced 2026-01-31 00:45:17 +01:00
fix(http): allow User-Agent header to be set (#983)
* fix(http): allow `User-Agent` header to be set closes #966 * lint * fix build Committed via a GitHub action: https://github.com/tauri-apps/plugins-workspace/actions/runs/8022187333 Co-authored-by: amrbashir <amrbashir@users.noreply.github.com>
This commit is contained in:
@@ -51,6 +51,19 @@ async function fetch(input, init) {
|
||||
delete init.proxy;
|
||||
}
|
||||
const signal = init?.signal;
|
||||
const headers = !init?.headers
|
||||
? []
|
||||
: init.headers instanceof Headers
|
||||
? Array.from(init.headers.entries())
|
||||
: Array.isArray(init.headers)
|
||||
? init.headers
|
||||
: Object.entries(init.headers);
|
||||
const mappedHeaders = headers.map(([name, val]) => [
|
||||
name,
|
||||
// we need to ensure we have all values as strings
|
||||
// eslint-disable-next-line
|
||||
typeof val === "string" ? val : val.toString(),
|
||||
]);
|
||||
const req = new Request(input, init);
|
||||
const buffer = await req.arrayBuffer();
|
||||
const reqData = buffer.byteLength ? Array.from(new Uint8Array(buffer)) : null;
|
||||
@@ -58,7 +71,7 @@ async function fetch(input, init) {
|
||||
clientConfig: {
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
headers: Array.from(req.headers.entries()),
|
||||
headers: mappedHeaders,
|
||||
data: reqData,
|
||||
maxRedirections,
|
||||
connectTimeout,
|
||||
@@ -70,7 +83,7 @@ async function fetch(input, init) {
|
||||
rid,
|
||||
});
|
||||
});
|
||||
const { status, statusText, url, headers, rid: responseRid, } = await core.invoke("plugin:http|fetch_send", {
|
||||
const { status, statusText, url, headers: responseHeaders, rid: responseRid, } = await core.invoke("plugin:http|fetch_send", {
|
||||
rid,
|
||||
});
|
||||
const body = await core.invoke("plugin:http|fetch_read_body", {
|
||||
@@ -81,7 +94,7 @@ async function fetch(input, init) {
|
||||
: body instanceof Array && body.length
|
||||
? new Uint8Array(body)
|
||||
: null, {
|
||||
headers,
|
||||
headers: responseHeaders,
|
||||
status,
|
||||
statusText,
|
||||
});
|
||||
|
||||
@@ -49,6 +49,19 @@ async function fetch(input, init) {
|
||||
delete init.proxy;
|
||||
}
|
||||
const signal = init?.signal;
|
||||
const headers = !init?.headers
|
||||
? []
|
||||
: init.headers instanceof Headers
|
||||
? Array.from(init.headers.entries())
|
||||
: Array.isArray(init.headers)
|
||||
? init.headers
|
||||
: Object.entries(init.headers);
|
||||
const mappedHeaders = headers.map(([name, val]) => [
|
||||
name,
|
||||
// we need to ensure we have all values as strings
|
||||
// eslint-disable-next-line
|
||||
typeof val === "string" ? val : val.toString(),
|
||||
]);
|
||||
const req = new Request(input, init);
|
||||
const buffer = await req.arrayBuffer();
|
||||
const reqData = buffer.byteLength ? Array.from(new Uint8Array(buffer)) : null;
|
||||
@@ -56,7 +69,7 @@ async function fetch(input, init) {
|
||||
clientConfig: {
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
headers: Array.from(req.headers.entries()),
|
||||
headers: mappedHeaders,
|
||||
data: reqData,
|
||||
maxRedirections,
|
||||
connectTimeout,
|
||||
@@ -68,7 +81,7 @@ async function fetch(input, init) {
|
||||
rid,
|
||||
});
|
||||
});
|
||||
const { status, statusText, url, headers, rid: responseRid, } = await invoke("plugin:http|fetch_send", {
|
||||
const { status, statusText, url, headers: responseHeaders, rid: responseRid, } = await invoke("plugin:http|fetch_send", {
|
||||
rid,
|
||||
});
|
||||
const body = await invoke("plugin:http|fetch_read_body", {
|
||||
@@ -79,7 +92,7 @@ async function fetch(input, init) {
|
||||
: body instanceof Array && body.length
|
||||
? new Uint8Array(body)
|
||||
: null, {
|
||||
headers,
|
||||
headers: responseHeaders,
|
||||
status,
|
||||
statusText,
|
||||
});
|
||||
|
||||
@@ -115,6 +115,21 @@ export async function fetch(
|
||||
|
||||
const signal = init?.signal;
|
||||
|
||||
const headers = !init?.headers
|
||||
? []
|
||||
: init.headers instanceof Headers
|
||||
? Array.from(init.headers.entries())
|
||||
: Array.isArray(init.headers)
|
||||
? init.headers
|
||||
: Object.entries(init.headers);
|
||||
|
||||
const mappedHeaders: [string, string][] = headers.map(([name, val]) => [
|
||||
name,
|
||||
// we need to ensure we have all values as strings
|
||||
// eslint-disable-next-line
|
||||
typeof val === "string" ? val : (val as any).toString(),
|
||||
]);
|
||||
|
||||
const req = new Request(input, init);
|
||||
const buffer = await req.arrayBuffer();
|
||||
const reqData = buffer.byteLength ? Array.from(new Uint8Array(buffer)) : null;
|
||||
@@ -123,7 +138,7 @@ export async function fetch(
|
||||
clientConfig: {
|
||||
method: req.method,
|
||||
url: req.url,
|
||||
headers: Array.from(req.headers.entries()),
|
||||
headers: mappedHeaders,
|
||||
data: reqData,
|
||||
maxRedirections,
|
||||
connectTimeout,
|
||||
@@ -149,7 +164,7 @@ export async function fetch(
|
||||
status,
|
||||
statusText,
|
||||
url,
|
||||
headers,
|
||||
headers: responseHeaders,
|
||||
rid: responseRid,
|
||||
} = await invoke<FetchSendResponse>("plugin:http|fetch_send", {
|
||||
rid,
|
||||
@@ -169,7 +184,7 @@ export async function fetch(
|
||||
? new Uint8Array(body)
|
||||
: null,
|
||||
{
|
||||
headers,
|
||||
headers: responseHeaders,
|
||||
status,
|
||||
statusText,
|
||||
},
|
||||
|
||||
@@ -1 +1 @@
|
||||
if("__TAURI__"in window){var __TAURI_PLUGIN_HTTP__=function(e){"use strict";async function t(e,t={},n){return window.__TAURI_INTERNALS__.invoke(e,t,n)}return"function"==typeof SuppressedError&&SuppressedError,e.fetch=async function(e,n){const r=n?.maxRedirections,i=n?.connectTimeout,a=n?.proxy;n&&(delete n.maxRedirections,delete n.connectTimeout,delete n.proxy);const o=n?.signal,s=new Request(e,n),u=await s.arrayBuffer(),c=u.byteLength?Array.from(new Uint8Array(u)):null,d=await t("plugin:http|fetch",{clientConfig:{method:s.method,url:s.url,headers:Array.from(s.headers.entries()),data:c,maxRedirections:r,connectTimeout:i,proxy:a}});o?.addEventListener("abort",(()=>{t("plugin:http|fetch_cancel",{rid:d})}));const{status:_,statusText:f,url:l,headers:p,rid:h}=await t("plugin:http|fetch_send",{rid:d}),y=await t("plugin:http|fetch_read_body",{rid:h}),T=new Response(y instanceof ArrayBuffer&&y.byteLength?y:y instanceof Array&&y.length?new Uint8Array(y):null,{headers:p,status:_,statusText:f});return Object.defineProperty(T,"url",{value:l}),T},e}({});Object.defineProperty(window.__TAURI__,"http",{value:__TAURI_PLUGIN_HTTP__})}
|
||||
if("__TAURI__"in window){var __TAURI_PLUGIN_HTTP__=function(e){"use strict";async function t(e,t={},r){return window.__TAURI_INTERNALS__.invoke(e,t,r)}return"function"==typeof SuppressedError&&SuppressedError,e.fetch=async function(e,r){const n=r?.maxRedirections,a=r?.connectTimeout,i=r?.proxy;r&&(delete r.maxRedirections,delete r.connectTimeout,delete r.proxy);const s=r?.signal,o=(r?.headers?r.headers instanceof Headers?Array.from(r.headers.entries()):Array.isArray(r.headers)?r.headers:Object.entries(r.headers):[]).map((([e,t])=>[e,"string"==typeof t?t:t.toString()])),d=new Request(e,r),c=await d.arrayBuffer(),u=c.byteLength?Array.from(new Uint8Array(c)):null,_=await t("plugin:http|fetch",{clientConfig:{method:d.method,url:d.url,headers:o,data:u,maxRedirections:n,connectTimeout:a,proxy:i}});s?.addEventListener("abort",(()=>{t("plugin:http|fetch_cancel",{rid:_})}));const{status:f,statusText:h,url:p,headers:l,rid:y}=await t("plugin:http|fetch_send",{rid:_}),T=await t("plugin:http|fetch_read_body",{rid:y}),w=new Response(T instanceof ArrayBuffer&&T.byteLength?T:T instanceof Array&&T.length?new Uint8Array(T):null,{headers:l,status:f,statusText:h});return Object.defineProperty(w,"url",{value:p}),w},e}({});Object.defineProperty(window.__TAURI__,"http",{value:__TAURI_PLUGIN_HTTP__})}
|
||||
|
||||
@@ -192,11 +192,33 @@ pub async fn fetch<R: Runtime>(
|
||||
|
||||
let mut request = builder.build()?.request(method.clone(), url);
|
||||
|
||||
for (key, value) in &headers {
|
||||
let name = HeaderName::from_bytes(key.as_bytes())?;
|
||||
let v = HeaderValue::from_bytes(value.as_bytes())?;
|
||||
if !matches!(name, header::HOST | header::CONTENT_LENGTH) {
|
||||
request = request.header(name, v);
|
||||
for (name, value) in &headers {
|
||||
let name = HeaderName::from_bytes(name.as_bytes())?;
|
||||
let value = HeaderValue::from_bytes(value.as_bytes())?;
|
||||
if !matches!(
|
||||
name,
|
||||
// forbidden headers per fetch spec https://fetch.spec.whatwg.org/#terminology-headers
|
||||
header::ACCEPT_CHARSET
|
||||
| header::ACCEPT_ENCODING
|
||||
| header::ACCESS_CONTROL_REQUEST_HEADERS
|
||||
| header::ACCESS_CONTROL_REQUEST_METHOD
|
||||
| header::CONNECTION
|
||||
| header::CONTENT_LENGTH
|
||||
| header::COOKIE
|
||||
| header::DATE
|
||||
| header::DNT
|
||||
| header::EXPECT
|
||||
| header::HOST
|
||||
| header::ORIGIN
|
||||
| header::REFERER
|
||||
| header::SET_COOKIE
|
||||
| header::TE
|
||||
| header::TRAILER
|
||||
| header::TRANSFER_ENCODING
|
||||
| header::UPGRADE
|
||||
| header::VIA
|
||||
) {
|
||||
request = request.header(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user