Skip to content

Commit

Permalink
fix(http): allow User-Agent header to be set (#983)
Browse files Browse the repository at this point in the history
* fix(http): allow `User-Agent` header to be set

closes #966

* lint

* fix build
  • Loading branch information
amrbashir authored Feb 23, 2024
1 parent 267e39e commit ae56b13
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .changes/http-user-agent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"http": "patch"
"http-js": "patch"
---

Allow `User-Agent` header to be set.
21 changes: 18 additions & 3 deletions plugins/http/guest-js/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand All @@ -149,7 +164,7 @@ export async function fetch(
status,
statusText,
url,
headers,
headers: responseHeaders,
rid: responseRid,
} = await invoke<FetchSendResponse>("plugin:http|fetch_send", {
rid,
Expand All @@ -169,7 +184,7 @@ export async function fetch(
? new Uint8Array(body)
: null,
{
headers,
headers: responseHeaders,
status,
statusText,
},
Expand Down
2 changes: 1 addition & 1 deletion plugins/http/src/api-iife.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 27 additions & 5 deletions plugins/http/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down

0 comments on commit ae56b13

Please sign in to comment.