junobuild_storage/http/
response.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
use crate::constants::{
    RESPONSE_STATUS_CODE_308, RESPONSE_STATUS_CODE_404, RESPONSE_STATUS_CODE_406,
    RESPONSE_STATUS_CODE_500,
};
use crate::http::headers::build_redirect_headers;
use crate::http::types::{HeaderField, HttpResponse, StatusCode};
use crate::http::utils::{
    build_encodings, build_response_headers, build_response_redirect_headers, streaming_strategy,
};
use crate::strategies::StorageStateStrategy;
use crate::types::config::{StorageConfigIFrame, StorageConfigRedirect};
use crate::types::store::Asset;
use junobuild_collections::types::rules::Memory;

pub fn build_asset_response(
    requested_url: String,
    requested_headers: Vec<HeaderField>,
    certificate_version: Option<u16>,
    asset: Option<(Asset, Memory)>,
    rewrite_source: Option<String>,
    status_code: StatusCode,
    storage_state: &impl StorageStateStrategy,
) -> HttpResponse {
    match asset {
        Some((asset, memory)) => {
            let encodings = build_encodings(requested_headers);

            for encoding_type in encodings.iter() {
                if let Some(encoding) = asset.encodings.get(encoding_type) {
                    let headers = build_response_headers(
                        &requested_url,
                        &asset,
                        encoding,
                        encoding_type,
                        &certificate_version,
                        &rewrite_source,
                        &storage_state.get_config(),
                    );

                    let Asset { key, .. } = &asset;

                    match headers {
                        Ok(headers) => {
                            let body = storage_state.get_content_chunks(encoding, 0, &memory);

                            // TODO: support for HTTP response 304
                            // On hold til DFINITY foundation implements:
                            // "Add etag support to icx-proxy" - https://dfinity.atlassian.net/browse/BOUN-446
                            // See const STATUS_CODES_TO_CERTIFY: [u16; 2] = [200, 304]; in sdk certified asset canister for implementation reference

                            match body {
                                Some(body) => {
                                    return HttpResponse {
                                        body: body.clone(),
                                        headers: headers.clone(),
                                        status_code,
                                        streaming_strategy: streaming_strategy(
                                            key,
                                            encoding,
                                            encoding_type,
                                            &headers,
                                            &memory,
                                        ),
                                    }
                                }
                                None => {
                                    error_response(
                                        RESPONSE_STATUS_CODE_500,
                                        "No chunks found.".to_string(),
                                    );
                                }
                            }
                        }
                        Err(err) => {
                            return error_response(
                                RESPONSE_STATUS_CODE_406,
                                ["Permission denied. Invalid headers. ", err].join(""),
                            );
                        }
                    }
                }
            }

            error_response(
                RESPONSE_STATUS_CODE_500,
                "No asset encoding found.".to_string(),
            )
        }
        None => error_response(RESPONSE_STATUS_CODE_404, "No asset found.".to_string()),
    }
}

pub fn build_redirect_response(
    requested_url: String,
    certificate_version: Option<u16>,
    redirect: &StorageConfigRedirect,
    iframe: &StorageConfigIFrame,
) -> HttpResponse {
    let headers = build_response_redirect_headers(
        &requested_url,
        &redirect.location,
        iframe,
        &certificate_version,
    )
    .unwrap();

    HttpResponse {
        body: Vec::new().clone(),
        headers: headers.clone(),
        status_code: redirect.status_code,
        streaming_strategy: None,
    }
}

pub fn build_redirect_raw_response(
    redirect_url: &str,
    iframe: &StorageConfigIFrame,
) -> HttpResponse {
    let headers = build_redirect_headers(redirect_url, iframe);

    HttpResponse {
        body: Vec::new().clone(),
        headers: headers.clone(),
        status_code: RESPONSE_STATUS_CODE_308,
        streaming_strategy: None,
    }
}

pub fn error_response(status_code: StatusCode, body: String) -> HttpResponse {
    HttpResponse {
        body: body.as_bytes().to_vec(),
        headers: Vec::new(),
        status_code,
        streaming_strategy: None,
    }
}