Skip to main content

webview_bundle_protocol/
uri.rs

1use webview_bundle::http::Uri;
2
3pub trait UriResolver: Send + Sync {
4  fn resolve_bundle(&self, uri: &Uri) -> Option<String>;
5  fn resolve_path(&self, uri: &Uri) -> String;
6}
7
8#[derive(Default)]
9pub struct DefaultUriResolver;
10
11impl UriResolver for DefaultUriResolver {
12  fn resolve_bundle(&self, uri: &Uri) -> Option<String> {
13    uri
14      .host()
15      .and_then(|x| x.split('.').next())
16      .map(|x| x.to_string())
17  }
18
19  fn resolve_path(&self, uri: &Uri) -> String {
20    let mut path = percent_encoding::percent_decode(uri.path().as_bytes())
21      .decode_utf8_lossy()
22      .to_string();
23    if path.ends_with('/') {
24      path.push_str("index.html");
25      return path;
26    }
27    if let Some(last) = path.rsplit('/').next() {
28      if !last.is_empty() && !last.contains('.') {
29        path.push_str("/index.html");
30      }
31    }
32    path
33  }
34}