johanhelsing / bevy_web_asset

Bevy asset loader that transparently supports loading over http(s)
Apache License 2.0
71 stars 17 forks source link

Add field for setting the user agent #24

Open oli-obk opened 9 months ago

oli-obk commented 9 months ago

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

johanhelsing commented 9 months ago

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?

danielfeather commented 7 months ago

@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

johanhelsing commented 7 months ago

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

danielfeather commented 7 months ago

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.

johanhelsing commented 7 months ago

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.