Let protocol handlers decide if they are fetchable (#33573)

This adds a 'is_fetchable()' method on the ProtocolHandler trait that is then used in the fetch code.
The 'data:' protocol handler is updated to return true instead of hardcoding the scheme comparison, as well
as the 'urlinfo:' handler since it's just a testing one.

Signed-off-by: webbeef <me@webbeef.org>
This commit is contained in:
webbeef 2024-09-28 12:38:49 -07:00 committed by GitHub
parent 5d269a9036
commit f57ae60056
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 34 additions and 5 deletions

View file

@ -29,12 +29,24 @@ use file::FileProtocolHander;
static FORBIDDEN_SCHEMES: [&str; 4] = ["http", "https", "chrome", "about"];
pub trait ProtocolHandler: Send + Sync {
/// Triggers the load of a resource for this protocol and returns a future
/// that will produce a Response. Even if the protocol is not backed by a
/// http endpoint, it is recommended to a least provide:
/// - A relevant status code.
/// - A Content Type.
fn load(
&self,
request: &mut Request,
done_chan: &mut DoneChannel,
context: &FetchContext,
) -> Pin<Box<dyn Future<Output = Response> + Send>>;
/// Specify if resources served by that protocol can be retrieved
/// with `fetch()` without no-cors mode to allow the caller direct
/// access to the resource content.
fn is_fetchable(&self) -> bool {
false
}
}
#[derive(Default)]
@ -80,6 +92,13 @@ impl ProtocolRegistry {
self.handlers.entry(scheme).or_insert(handler);
}
}
pub fn is_fetchable(&self, scheme: &str) -> bool {
self.handlers
.get(scheme)
.map(|handler| handler.is_fetchable())
.unwrap_or(false)
}
}
pub fn range_not_satisfiable_error(response: &mut Response) {