Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ic-asset-certification): add Range as a certified request header #388

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 39 additions & 7 deletions packages/ic-asset-certification/src/asset_router.rs
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,7 @@ impl<'content> AssetRouter<'content> {
status_code,
Cow::Owned(vec![]),
headers,
vec![],
)?;

Ok(CertifiedAssetResponse {
Expand Down Expand Up @@ -798,6 +799,7 @@ impl<'content> AssetRouter<'content> {
headers.push(("content-encoding".to_string(), encoding.to_string()));
}

let mut request_headers = vec![];
if let Some(range_begin) = range_begin {
let total_length = content.len();
let range_end = cmp::min(range_begin + ASSET_CHUNK_SIZE, total_length) - 1;
Expand All @@ -807,27 +809,35 @@ impl<'content> AssetRouter<'content> {
http::header::CONTENT_RANGE.to_string(),
format!("bytes {range_begin}-{range_end}/{total_length}"),
));
request_headers.push(http::header::RANGE.to_string());
};

Self::prepare_response_and_certification(
asset.url.to_string(),
status_code,
content,
headers,
request_headers,
)
}

fn prepare_response_and_certification(
url: String,
status_code: u16,
body: Cow<'content, [u8]>,
additional_headers: Vec<(String, String)>,
additional_response_headers: Vec<(String, String)>,
certified_request_headers: Vec<String>,
) -> AssetCertificationResult<(HttpResponse<'content>, HttpCertification)> {
let mut headers = vec![("content-length".to_string(), body.len().to_string())];

headers.extend(additional_headers);

headers.extend(additional_response_headers);
let cel_expr = DefaultCelBuilder::full_certification()
.with_request_headers(
certified_request_headers
.iter()
.map(|s| s.as_str())
.collect::<Vec<&str>>(),
)
.with_response_certification(DefaultResponseCertification::response_header_exclusions(
vec![],
))
Expand Down Expand Up @@ -1137,7 +1147,7 @@ mod tests {
let request = HttpRequest::get(&req_url).build();
let mut expected_response = build_206_response(
asset_body[0..ASSET_CHUNK_SIZE].to_vec(),
asset_cel_expr(),
asset_range_chunk_cel_expr(),
vec![
(
"cache-control".to_string(),
Expand Down Expand Up @@ -1184,7 +1194,7 @@ mod tests {
let expected_range_end = cmp::min(asset_len_so_far + ASSET_CHUNK_SIZE, asset_len) - 1;
let mut expected_response = build_206_response(
asset_body[asset_len_so_far..=expected_range_end].to_vec(),
asset_cel_expr(),
asset_range_chunk_cel_expr(),
vec![
(
"cache-control".to_string(),
Expand Down Expand Up @@ -1337,7 +1347,7 @@ mod tests {
.build();
let mut expected_response = build_206_response(
asset_body[0..ASSET_CHUNK_SIZE].to_vec(),
encoded_asset_cel_expr(),
encoded_range_chunk_asset_cel_expr(),
vec![
(
"cache-control".to_string(),
Expand Down Expand Up @@ -1390,7 +1400,7 @@ mod tests {
let expected_range_end = cmp::min(asset_len_so_far + ASSET_CHUNK_SIZE, asset_len) - 1;
let mut expected_response = build_206_response(
asset_body[asset_len_so_far..=expected_range_end].to_vec(),
asset_cel_expr(),
encoded_range_chunk_asset_cel_expr(),
vec![
(
"cache-control".to_string(),
Expand Down Expand Up @@ -3171,6 +3181,17 @@ mod tests {
.to_string()
}

#[fixture]
fn asset_range_chunk_cel_expr() -> String {
DefaultFullCelExpressionBuilder::default()
.with_request_headers(vec!["range"])
.with_response_certification(DefaultResponseCertification::response_header_exclusions(
vec![],
))
.build()
.to_string()
}

#[fixture]
fn encoded_asset_cel_expr() -> String {
DefaultFullCelExpressionBuilder::default()
Expand All @@ -3181,6 +3202,17 @@ mod tests {
.to_string()
}

#[fixture]
fn encoded_range_chunk_asset_cel_expr() -> String {
DefaultFullCelExpressionBuilder::default()
.with_request_headers(vec!["range"])
.with_response_certification(DefaultResponseCertification::response_header_exclusions(
vec![],
))
.build()
.to_string()
}

#[fixture]
fn index_html_config() -> AssetConfig {
AssetConfig::File {
Expand Down
Loading