junobuild_storage/http/
response.rs

1use crate::constants::{
2    RESPONSE_STATUS_CODE_308, RESPONSE_STATUS_CODE_404, RESPONSE_STATUS_CODE_406,
3    RESPONSE_STATUS_CODE_500,
4};
5use crate::http::headers::build_redirect_headers;
6use crate::http::types::{HeaderField, HttpResponse, StatusCode};
7use crate::http::utils::{
8    build_encodings, build_response_headers, build_response_redirect_headers, streaming_strategy,
9};
10use crate::strategies::{StorageCertificateStrategy, StorageStateStrategy};
11use crate::types::config::{StorageConfigIFrame, StorageConfigRedirect};
12use crate::types::store::Asset;
13use junobuild_collections::types::rules::Memory;
14
15#[allow(clippy::too_many_arguments)]
16pub fn build_asset_response(
17    requested_url: String,
18    requested_headers: Vec<HeaderField>,
19    certificate_version: Option<u16>,
20    asset: Option<(Asset, Memory)>,
21    rewrite_source: Option<String>,
22    status_code: StatusCode,
23    storage_state: &impl StorageStateStrategy,
24    certificate: &impl StorageCertificateStrategy,
25) -> HttpResponse {
26    match asset {
27        Some((asset, memory)) => {
28            let encodings = build_encodings(requested_headers);
29
30            for encoding_type in encodings.iter() {
31                if let Some(encoding) = asset.encodings.get(encoding_type) {
32                    let headers = build_response_headers(
33                        &requested_url,
34                        &asset,
35                        encoding,
36                        encoding_type,
37                        &certificate_version,
38                        &rewrite_source,
39                        &storage_state.get_config(),
40                        certificate.get_pruned_labeled_sigs_root_hash_tree(),
41                    );
42
43                    let Asset { key, .. } = &asset;
44
45                    match headers {
46                        Ok(headers) => {
47                            let body = storage_state.get_content_chunks(encoding, 0, &memory);
48
49                            // TODO: support for HTTP response 304
50                            // On hold til DFINITY foundation implements:
51                            // "Add etag support to icx-proxy" - https://dfinity.atlassian.net/browse/BOUN-446
52                            // See const STATUS_CODES_TO_CERTIFY: [u16; 2] = [200, 304]; in sdk certified asset canister for implementation reference
53
54                            match body {
55                                Some(body) => {
56                                    return HttpResponse {
57                                        body: body.clone(),
58                                        headers: headers.clone(),
59                                        status_code,
60                                        streaming_strategy: streaming_strategy(
61                                            key,
62                                            encoding,
63                                            encoding_type,
64                                            &headers,
65                                            &memory,
66                                        ),
67                                    }
68                                }
69                                None => {
70                                    error_response(
71                                        RESPONSE_STATUS_CODE_500,
72                                        "No chunks found.".to_string(),
73                                    );
74                                }
75                            }
76                        }
77                        Err(err) => {
78                            return error_response(
79                                RESPONSE_STATUS_CODE_406,
80                                ["Permission denied. Invalid headers. ", err].join(""),
81                            );
82                        }
83                    }
84                }
85            }
86
87            error_response(
88                RESPONSE_STATUS_CODE_500,
89                "No asset encoding found.".to_string(),
90            )
91        }
92        None => error_response(RESPONSE_STATUS_CODE_404, "No asset found.".to_string()),
93    }
94}
95
96pub fn build_redirect_response(
97    requested_url: String,
98    certificate_version: Option<u16>,
99    redirect: &StorageConfigRedirect,
100    iframe: &StorageConfigIFrame,
101    certificate: &impl StorageCertificateStrategy,
102) -> HttpResponse {
103    let headers = build_response_redirect_headers(
104        &requested_url,
105        &redirect.location,
106        iframe,
107        &certificate_version,
108        certificate.get_pruned_labeled_sigs_root_hash_tree(),
109    )
110    .unwrap();
111
112    HttpResponse {
113        body: Vec::new().clone(),
114        headers: headers.clone(),
115        status_code: redirect.status_code,
116        streaming_strategy: None,
117    }
118}
119
120pub fn build_redirect_raw_response(
121    redirect_url: &str,
122    iframe: &StorageConfigIFrame,
123) -> HttpResponse {
124    let headers = build_redirect_headers(redirect_url, iframe);
125
126    HttpResponse {
127        body: Vec::new().clone(),
128        headers: headers.clone(),
129        status_code: RESPONSE_STATUS_CODE_308,
130        streaming_strategy: None,
131    }
132}
133
134pub fn error_response(status_code: StatusCode, body: String) -> HttpResponse {
135    HttpResponse {
136        body: body.as_bytes().to_vec(),
137        headers: Vec::new(),
138        status_code,
139        streaming_strategy: None,
140    }
141}