bevy_web_asset icon indicating copy to clipboard operation
bevy_web_asset copied to clipboard

Add field for setting the user agent

Open oli-obk opened this issue 6 months ago • 5 comments

Servers like the osm tile servers require a user agent to be able to rate limit bots and apps

oli-obk avatar Nov 30 '23 12:11 oli-obk

Thanks! I think it's a useful feature to have, but maybe warn if it's set on wasm because we can't implement it there?

johanhelsing avatar Dec 07 '23 14:12 johanhelsing

@johanhelsing This should be doable on wasm. On the web-sys Window there is a method called fetch_with_str_and_init. RequestInit has a headers field where I am assuming the User-Agent header can be specified.

https://github.com/rustwasm/wasm-bindgen/blob/4b6ef26f8a1dc1cd62329918dbe646cf9c142113/crates/web-sys/src/features/gen_Window.rs#L2991

danielfeather avatar Feb 06 '24 11:02 danielfeather

I thought perhaps browsers didn't let you set that header... Have you tried it?

johanhelsing avatar Feb 06 '24 16:02 johanhelsing

I thought perhaps browsers didn't let you set that header... Have you tried it?

@johanhelsing I hadn't considered that, but after doing a test with fetch and XMLHttpRequest respectively, Chrome based browsers are the outlier and they don't support setting this header and will just drop it if you specify it. But my tests showed It works on Firefox and Safari on iOS though.

User-Agent was restricted historically, however its not classed as a forbidden header now according to the fetch spec.

https://developer.mozilla.org/en-US/docs/Glossary/Forbidden_header_name https://fetch.spec.whatwg.org/#forbidden-request-header

Chromium Bug: https://issues.chromium.org/issues/40450316

Not sure if its worth adding on WASM then, considering this.

danielfeather avatar Feb 17 '24 14:02 danielfeather

Thanks for doing the digging!

Wasm could still be useful for people doing a webview-like thing. I'm fine not supporting it in this PR, but it should at least be big warning, both runtime and in the docs if you try to use it there.

johanhelsing avatar Feb 18 '24 13:02 johanhelsing