mirror of
https://github.com/emilk/egui.git
synced 2026-07-04 05:47:26 +00:00
Add arbitrary request headers to EhttpLoader (#8121)
* Closes <https://github.com/emilk/egui/issues/4491> * [x] I have followed the instructions in the PR template Lets you create an `EhttpLoader` with arbitrary headers like so: ```rust cc.egui_ctx.add_bytes_loader(std::sync::Arc::new( egui_extras::loaders::http_loader::EhttpLoader::default().with_headers(&[ ("User-Agent", "foo"), ]) )); ``` I'm not sure if there are any problems with installing a second `EhttpLoader` (in addition to the one installed by default when using `egui_extras::install_image_loaders(&cc.egui_ctx)`. But I wasn't sure how else to pass in configuration options to `install_image_loaders`.
This commit is contained in:
@@ -42,10 +42,23 @@ type Entry = Poll<Result<File, String>>;
|
|||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct EhttpLoader {
|
pub struct EhttpLoader {
|
||||||
cache: Arc<Mutex<HashMap<String, Entry>>>,
|
cache: Arc<Mutex<HashMap<String, Entry>>>,
|
||||||
|
request_template: Option<Box<dyn Fn(ehttp::Request) -> ehttp::Request + Send + Sync>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl EhttpLoader {
|
impl EhttpLoader {
|
||||||
pub const ID: &'static str = egui::generate_loader_id!(EhttpLoader);
|
pub const ID: &'static str = egui::generate_loader_id!(EhttpLoader);
|
||||||
|
|
||||||
|
/// Provide a request template to modify requests before they're sent,
|
||||||
|
/// e.g. to add headers.
|
||||||
|
pub fn with_request_template<
|
||||||
|
F: Fn(ehttp::Request) -> ehttp::Request + Send + Sync + 'static,
|
||||||
|
>(
|
||||||
|
mut self,
|
||||||
|
request_template: F,
|
||||||
|
) -> Self {
|
||||||
|
self.request_template = Some(Box::new(request_template));
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const PROTOCOLS: &[&str] = &["http://", "https://"];
|
const PROTOCOLS: &[&str] = &["http://", "https://"];
|
||||||
@@ -82,41 +95,47 @@ impl BytesLoader for EhttpLoader {
|
|||||||
cache.insert(uri.clone(), Poll::Pending);
|
cache.insert(uri.clone(), Poll::Pending);
|
||||||
drop(cache);
|
drop(cache);
|
||||||
|
|
||||||
ehttp::fetch(ehttp::Request::get(uri.clone()), {
|
ehttp::fetch(
|
||||||
let ctx = ctx.clone();
|
match &self.request_template {
|
||||||
let cache = Arc::clone(&self.cache);
|
Some(templ) => templ(ehttp::Request::get(uri.clone())),
|
||||||
move |response| {
|
None => ehttp::Request::get(uri.clone()),
|
||||||
let result = match response {
|
},
|
||||||
Ok(response) => File::from_response(&uri, response),
|
{
|
||||||
Err(err) => {
|
let ctx = ctx.clone();
|
||||||
// Log details; return summary
|
let cache = Arc::clone(&self.cache);
|
||||||
log::error!("Failed to load {uri:?}: {err}");
|
move |response| {
|
||||||
Err(format!("Failed to load {uri:?}"))
|
let result = match response {
|
||||||
|
Ok(response) => File::from_response(&uri, response),
|
||||||
|
Err(err) => {
|
||||||
|
// Log details; return summary
|
||||||
|
log::error!("Failed to load {uri:?}: {err}");
|
||||||
|
Err(format!("Failed to load {uri:?}"))
|
||||||
|
}
|
||||||
|
};
|
||||||
|
let repaint = {
|
||||||
|
let mut cache = cache.lock();
|
||||||
|
if let std::collections::hash_map::Entry::Occupied(mut entry) =
|
||||||
|
cache.entry(uri.clone())
|
||||||
|
{
|
||||||
|
let entry = entry.get_mut();
|
||||||
|
*entry = Poll::Ready(result);
|
||||||
|
log::trace!("Finished loading {uri:?}");
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
log::trace!(
|
||||||
|
"Canceled loading {uri:?}\nNote: This can happen if `forget_image` is called while the image is still loading."
|
||||||
|
);
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// We may not lock Context while the cache lock is held (see ImageLoader::load
|
||||||
|
// for details).
|
||||||
|
if repaint {
|
||||||
|
ctx.request_repaint();
|
||||||
}
|
}
|
||||||
};
|
|
||||||
let repaint = {
|
|
||||||
let mut cache = cache.lock();
|
|
||||||
if let std::collections::hash_map::Entry::Occupied(mut entry) =
|
|
||||||
cache.entry(uri.clone())
|
|
||||||
{
|
|
||||||
let entry = entry.get_mut();
|
|
||||||
*entry = Poll::Ready(result);
|
|
||||||
log::trace!("Finished loading {uri:?}");
|
|
||||||
true
|
|
||||||
} else {
|
|
||||||
log::trace!(
|
|
||||||
"Canceled loading {uri:?}\nNote: This can happen if `forget_image` is called while the image is still loading."
|
|
||||||
);
|
|
||||||
false
|
|
||||||
}
|
|
||||||
};
|
|
||||||
// We may not lock Context while the cache lock is held (see ImageLoader::load
|
|
||||||
// for details).
|
|
||||||
if repaint {
|
|
||||||
ctx.request_repaint();
|
|
||||||
}
|
}
|
||||||
}
|
},
|
||||||
});
|
);
|
||||||
|
|
||||||
Ok(BytesPoll::Pending { size: None })
|
Ok(BytesPoll::Pending { size: None })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user