junobuild_storage/http/
utils.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
use crate::certification::cert::{build_asset_certificate_header, build_certified_expression};
use crate::constants::ASSET_ENCODING_NO_COMPRESSION;
use crate::http::headers::{build_headers, build_redirect_headers};
use crate::http::types::{CallbackFunc, HeaderField, StreamingCallbackToken, StreamingStrategy};
use crate::memory::STATE;
use crate::types::config::{StorageConfig, StorageConfigIFrame};
use crate::types::runtime_state::StorageRuntimeState;
use crate::types::store::{Asset, AssetEncoding, AssetKey, EncodingType};
use ic_cdk::id;
use junobuild_collections::types::rules::Memory;
use serde_bytes::ByteBuf;

pub fn streaming_strategy(
    key: &AssetKey,
    encoding: &AssetEncoding,
    encoding_type: &str,
    headers: &[HeaderField],
    memory: &Memory,
) -> Option<StreamingStrategy> {
    let streaming_token: Option<StreamingCallbackToken> =
        create_token(key, 0, encoding, encoding_type, headers, memory);

    streaming_token.map(|streaming_token| StreamingStrategy::Callback {
        callback: CallbackFunc::new(id(), "http_request_streaming_callback".to_string()),
        token: streaming_token,
    })
}

pub fn create_token(
    key: &AssetKey,
    chunk_index: usize,
    encoding: &AssetEncoding,
    encoding_type: &str,
    headers: &[HeaderField],
    memory: &Memory,
) -> Option<StreamingCallbackToken> {
    if chunk_index + 1 >= encoding.content_chunks.len() {
        return None;
    }

    Some(StreamingCallbackToken {
        full_path: key.full_path.clone(),
        token: key.token.clone(),
        headers: headers.to_owned(),
        index: chunk_index + 1,
        sha256: Some(ByteBuf::from(encoding.sha256)),
        encoding_type: encoding_type.to_owned(),
        memory: memory.clone(),
    })
}

pub fn build_response_headers(
    url: &str,
    asset: &Asset,
    encoding: &AssetEncoding,
    encoding_type: &EncodingType,
    certificate_version: &Option<u16>,
    rewrite_source: &Option<String>,
    config: &StorageConfig,
) -> Result<Vec<HeaderField>, &'static str> {
    let asset_headers = build_headers(asset, encoding, encoding_type, config);

    extend_headers_with_certification(asset_headers, url, certificate_version, rewrite_source)
}

pub fn build_response_redirect_headers(
    url: &str,
    location: &str,
    iframe: &StorageConfigIFrame,
    certificate_version: &Option<u16>,
) -> Result<Vec<HeaderField>, &'static str> {
    let asset_headers = build_redirect_headers(location, iframe);

    extend_headers_with_certification(asset_headers, url, certificate_version, &None)
}

fn extend_headers_with_certification(
    asset_headers: Vec<HeaderField>,
    url: &str,
    certificate_version: &Option<u16>,
    rewrite_source: &Option<String>,
) -> Result<Vec<HeaderField>, &'static str> {
    let certified_header = build_certified_headers(url, certificate_version, rewrite_source)?;
    let certified_expression = build_certified_expression(&asset_headers, certificate_version)?;

    match certified_expression {
        None => Ok([asset_headers, vec![certified_header]].concat()),
        Some(certified_expression) => {
            Ok([asset_headers, vec![certified_header, certified_expression]].concat())
        }
    }
}

fn build_certified_headers(
    url: &str,
    certificate_version: &Option<u16>,
    rewrite_source: &Option<String>,
) -> Result<HeaderField, &'static str> {
    STATE.with(|state| {
        build_certified_headers_impl(
            url,
            certificate_version,
            rewrite_source,
            &state.borrow().runtime.storage,
        )
    })
}

fn build_certified_headers_impl(
    url: &str,
    certificate_version: &Option<u16>,
    rewrite_source: &Option<String>,
    state: &StorageRuntimeState,
) -> Result<HeaderField, &'static str> {
    build_asset_certificate_header(
        &state.asset_hashes,
        url.to_owned(),
        certificate_version,
        rewrite_source,
    )
}

pub fn build_encodings(headers: Vec<HeaderField>) -> Vec<String> {
    let mut encodings: Vec<String> = vec![];
    for HeaderField(name, value) in headers.iter() {
        if name.eq_ignore_ascii_case("Accept-Encoding") {
            for v in value.split(',') {
                encodings.push(v.trim().to_string());
            }
        }
    }
    encodings.push(ASSET_ENCODING_NO_COMPRESSION.to_string());

    encodings
}