Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
nmattia committed Oct 27, 2023
1 parent e9a07db commit 247f7c3
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
40 changes: 21 additions & 19 deletions src/internet_identity/src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub const IC_CERTIFICATE_EXPRESSION: &str =

#[derive(Debug, Default, Clone)]
pub struct CertifiedAssets {
pub assets: HashMap<String, (Vec<HeaderField>, Box<Vec<u8>>)>,
pub assets: HashMap<String, (Vec<HeaderField>, Vec<u8>)>,
pub certification_v1: RbTree<String, Hash>,
pub certification_v2: NestedTree<Vec<u8>, Vec<u8>>,
}
Expand Down Expand Up @@ -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));
}
});
}
Expand Down Expand Up @@ -271,12 +269,16 @@ fn collect_assets_from_dir(dir: &Dir) -> Vec<(String, Vec<u8>, 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
Expand All @@ -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 **/<foo>.html to **/foo
/// * map **/<foo>.js.gz to **/<foo>.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<String> {
// 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<String> {
if elements.is_empty() && last == "index.html" {
Expand Down
2 changes: 1 addition & 1 deletion src/internet_identity/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down

0 comments on commit 247f7c3

Please sign in to comment.