From 247f7c3f8261c5a2dbe65fc582d1c588f11267ab Mon Sep 17 00:00:00 2001 From: Nicolas Mattia Date: Fri, 27 Oct 2023 14:26:49 +0200 Subject: [PATCH] wip --- src/internet_identity/src/assets.rs | 40 +++++++++++++++-------------- src/internet_identity/src/http.rs | 2 +- 2 files changed, 22 insertions(+), 20 deletions(-) diff --git a/src/internet_identity/src/assets.rs b/src/internet_identity/src/assets.rs index 1fd6e61113..1cd438d2c2 100644 --- a/src/internet_identity/src/assets.rs +++ b/src/internet_identity/src/assets.rs @@ -28,7 +28,7 @@ pub const IC_CERTIFICATE_EXPRESSION: &str = #[derive(Debug, Default, Clone)] pub struct CertifiedAssets { - pub assets: HashMap, Box>)>, + pub assets: HashMap, Vec)>, pub certification_v1: RbTree, pub certification_v2: NestedTree, Vec>, } @@ -164,9 +164,7 @@ pub fn init_assets() { body_hash, ); - certified_assets - .assets - .insert(path, (headers, Box::new(content))); + certified_assets.assets.insert(path, (headers, content)); } }); } @@ -271,12 +269,16 @@ fn collect_assets_from_dir(dir: &Dir) -> Vec<(String, Vec, ContentEncoding, ), }; - let asspaths = filepath_to_urlpaths(asset.path().to_str().unwrap().to_string()); - - // TODO: add canister/lightweight test to ensure assets are loaded up - // & certified successfully - for asspath in asspaths { - assets.push((asspath, content.clone(), encoding, content_type)); + let urlpaths = filepath_to_urlpaths(asset.path().to_str().unwrap().to_string()); + for urlpath in urlpaths { + // XXX: we clone the content for each asset instead of doing something smarter + // for simplicity & because the only assets that currently may be duplicated are + // small HTML files. + // + // XXX: the behavior is undefined for assets with overlapping URL paths (e.g. "foo.html" & + // "foo/index.html"). This assumes that the bundler creating the assets directory + // creates sensible assets. + assets.push((urlpath, content.clone(), encoding, content_type)); } } assets @@ -299,16 +301,16 @@ fn file_extension<'a>(asset: &'a File) -> &'a str { .1 } -/// Returns the asset path for a given file: -/// * TODO: -/// * make relative path absolute -/// * map **/index.html to **/ -/// * map **/.html to **/foo -/// * map **/.js.gz to **/.js +/// Returns the URL paths for a given asset filepath. For instance: +/// +/// * "index.html" -> "/", "/index.html" +/// * "foo/bar.html" -> "/foo/bar", "/foo/bar/", "foo/bar/index.html" +/// +/// NOTE: The behavior is undefined if the argument is NOT relative, i.e. if +/// the filepath has a leading slash. +/// +/// NOTE: The returned paths will always start with a slash. fn filepath_to_urlpaths(file_path: String) -> Vec { - // TODO: error out if starting with /? - // TODO: explain always return starting with / - // Create paths, WITHOUT leading slash (leading lash is prepended later) fn inner(elements: Vec<&str>, last: &str) -> Vec { if elements.is_empty() && last == "index.html" { diff --git a/src/internet_identity/src/http.rs b/src/internet_identity/src/http.rs index 6364853867..2fdef4e186 100644 --- a/src/internet_identity/src/http.rs +++ b/src/internet_identity/src/http.rs @@ -93,7 +93,7 @@ pub fn http_request(req: HttpRequest) -> HttpResponse { HttpResponse { status_code: 200, headers, - body: ByteBuf::from(*data.clone()), + body: ByteBuf::from(data.clone()), upgrade: None, streaming_strategy: None, }