junobuild_storage/http/
response.rs1use 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);
51
52 match body {
58 Some(body) => {
59 return HttpResponse {
60 body: body.clone(),
61 headers: headers.clone(),
62 status_code,
63 streaming_strategy: streaming_strategy(
64 key,
65 encoding,
66 encoding_type,
67 &headers,
68 &memory,
69 ),
70 }
71 }
72 None => {
73 error_response(
74 RESPONSE_STATUS_CODE_500,
75 "No chunks found.".to_string(),
76 );
77 }
78 }
79 }
80 Err(err) => {
81 return error_response(
82 RESPONSE_STATUS_CODE_406,
83 ["Permission denied. Invalid headers. ", err].join(""),
84 );
85 }
86 }
87 }
88 }
89
90 error_response(
91 RESPONSE_STATUS_CODE_500,
92 "No asset encoding found.".to_string(),
93 )
94 }
95 None => error_response(RESPONSE_STATUS_CODE_404, "No asset found.".to_string()),
96 }
97}
98
99pub fn build_redirect_response(
100 requested_url: String,
101 certificate_version: Option<u16>,
102 redirect: &StorageConfigRedirect,
103 iframe: &StorageConfigIFrame,
104 certificate: &impl StorageCertificateStrategy,
105) -> HttpResponse {
106 let headers = build_response_redirect_headers(
107 &requested_url,
108 &redirect.location,
109 iframe,
110 &certificate_version,
111 certificate.get_pruned_labeled_sigs_root_hash_tree(),
112 )
113 .unwrap();
114
115 HttpResponse {
116 body: Vec::new().clone(),
117 headers: headers.clone(),
118 status_code: redirect.status_code,
119 streaming_strategy: None,
120 }
121}
122
123pub fn build_redirect_raw_response(
124 redirect_url: &str,
125 iframe: &StorageConfigIFrame,
126) -> HttpResponse {
127 let headers = build_redirect_headers(redirect_url, iframe);
128
129 HttpResponse {
130 body: Vec::new().clone(),
131 headers: headers.clone(),
132 status_code: RESPONSE_STATUS_CODE_308,
133 streaming_strategy: None,
134 }
135}
136
137pub fn error_response(status_code: StatusCode, body: String) -> HttpResponse {
138 HttpResponse {
139 body: body.as_bytes().to_vec(),
140 headers: Vec::new(),
141 status_code,
142 streaming_strategy: None,
143 }
144}