emilk / egui

egui: an easy-to-use immediate mode GUI in Rust that runs on both web and native
https://www.egui.rs/
Apache License 2.0
21.93k stars 1.58k forks source link

Add a User-Agent header to HTTP image loader #4491

Open MathiasYde opened 4 months ago

MathiasYde commented 4 months ago

Is your feature request related to a problem? Please describe.

I'm trying to make an egui program that shows images loaded from an CDN, but it requires that the request has a user agent as a header.

Describe the solution you'd like

Either hardcore the user agent header into the code handling HTTP image loading or add a way to allow passing in a custom user agent

Describe alternatives you've considered

Loading the image using reqwest as bytes and use ImageSource::Bytes in Image

Additional context

emilk commented 4 months ago

Sounds good - PRs welcome!

MathiasYde commented 4 months ago

Are we going for a hard coded user agent or a configurable one? I could take a crack at it

MathiasYde commented 4 months ago

I've been looking through the code a bit and found out that egui-extras uses ehttp for the http ImageLoader which has hardcoded headers of just ("Accept", "*/*")

Do you want to simply pass the headers into ehttp::Request::get function, a builder pattern? (ehttp::Request::get("http://..").with_headers(..).build()) or perhaps something else?

MathiasYde commented 4 months ago

So a few hours of debugging and I found out that the URL I use to fetch images has unescaped qoutes ("\"https://..\"") which caused images to fail due to calling to_string() directly on serde_json::Value instead of something like as_str().unwrap_or("null").to_string() :upside_down_face:

emilk commented 4 months ago

A hard-coded User-Agent: egui is a good start, but adding some way of changing it for EhttpLoader should be fairly easy.