Skip to content

Commit 058f992

Browse files
authored
Merge branch 'main' into nathan/add-response-convenience-methods
2 parents e87970d + 673d319 commit 058f992

File tree

15 files changed

+1168
-459
lines changed

15 files changed

+1168
-459
lines changed

Cargo.lock

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dfx.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"dfx": "0.23.0",
2+
"dfx": "0.24.3",
33
"output_env_file": ".env",
44
"version": 1,
55
"networks": {

examples/http-certification/custom-assets/README.md

+51-75
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@ The lifecycle hooks are set up similarly to the JSON API.
6767
```rust
6868
#[init]
6969
fn init() {
70-
prepare_cel_exprs();
7170
certify_all_assets();
7271
}
7372

@@ -79,30 +78,17 @@ fn post_upgrade() {
7978

8079
## CEL Expressions
8180

82-
CEL expressions are also stored similarly to the [JSON API example](https://internetcomputer.org/docs/current/developer-docs/http-compatible-canisters/serving-json-over-http).
81+
The CEL expression definition is simpler in the case of assets compared to the [JSON API example](https://internetcomputer.org/docs/current/developer-docs/http-compatible-canisters/serving-json-over-http) as the same CEL expression is used for every asset, including the fallback response.
8382

8483
```rust
85-
thread_local! {
86-
static CEL_EXPRS: RefCell<HashMap<String, (DefaultResponseOnlyCelExpression<'static>, String)>> = RefCell::new(HashMap::new());
87-
}
88-
```
89-
90-
The CEL expression definition is simpler in the case of assets as the same CEL expression is used for every asset, including the fallback response.
91-
92-
```rust
93-
fn prepare_cel_exprs() {
94-
let asset_cel_expr_def = DefaultCelBuilder::response_only_certification()
95-
.with_response_certification(DefaultResponseCertification::response_header_exclusions(vec![]))
96-
.build();
97-
98-
let asset_cel_expr = asset_cel_expr_def.to_string();
99-
100-
CEL_EXPRS.with_borrow_mut(|exprs| {
101-
exprs.insert(
102-
ASSET_CEL_EXPR_PATH.to_string(),
103-
(asset_cel_expr_def, asset_cel_expr),
104-
);
105-
});
84+
lazy_static! {
85+
static ref ASSET_CEL_EXPR_DEF: DefaultResponseOnlyCelExpression<'static> =
86+
DefaultCelBuilder::response_only_certification()
87+
.with_response_certification(DefaultResponseCertification::response_header_exclusions(
88+
vec![],
89+
))
90+
.build();
91+
static ref ASSET_CEL_EXPR: String = ASSET_CEL_EXPR_DEF.to_string();
10692
}
10793
```
10894

@@ -185,35 +171,30 @@ fn certify_asset_response(
185171
asset_tree_path: &HttpCertificationPath,
186172
asset_req_path: String,
187173
) {
188-
CEL_EXPRS.with_borrow(|cel_exprs| {
189-
// get the relevant CEL expression
190-
let (cel_expr_def, cel_expr_str) = cel_exprs.get(*ASSET_CEL_EXPR_PATH).unwrap();
174+
// create the response
175+
let response = create_asset_response(additional_headers, body, ASSET_CEL_EXPR.clone());
191176

192-
// create the response
193-
let response = create_asset_response(additional_headers, body, cel_expr_str.to_string());
194-
195-
// certify the response
196-
let certification =
197-
HttpCertification::response_only(cel_expr_def, &response, None).unwrap();
177+
// certify the response
178+
let certification =
179+
HttpCertification::response_only(&ASSET_CEL_EXPR_DEF, &response, None).unwrap();
198180

199-
HTTP_TREE.with_borrow_mut(|http_tree| {
200-
// add the certification to the certification tree
201-
http_tree.insert(&HttpCertificationTreeEntry::new(
202-
asset_tree_path,
203-
&certification,
204-
));
205-
});
181+
HTTP_TREE.with_borrow_mut(|http_tree| {
182+
// add the certification to the certification tree
183+
http_tree.insert(&HttpCertificationTreeEntry::new(
184+
asset_tree_path,
185+
&certification,
186+
));
187+
});
206188

207-
RESPONSES.with_borrow_mut(|responses| {
208-
// store the response for later retrieval
209-
responses.insert(
210-
asset_req_path,
211-
CertifiedHttpResponse {
212-
response,
213-
certification,
214-
},
215-
);
216-
});
189+
RESPONSES.with_borrow_mut(|responses| {
190+
// store the response for later retrieval
191+
responses.insert(
192+
asset_req_path,
193+
CertifiedHttpResponse {
194+
response,
195+
certification,
196+
},
197+
);
217198
});
218199
}
219200
```
@@ -240,35 +221,30 @@ fn certify_asset_with_encoding(
240221
let mut headers = vec![("content-encoding".to_string(), encoding.to_string())];
241222
headers.extend(additional_headers);
242223

243-
CEL_EXPRS.with_borrow(|cel_exprs| {
244-
// get the relevant CEL expression
245-
let (cel_expr_def, cel_expr_str) = cel_exprs.get(*ASSET_CEL_EXPR_PATH).unwrap();
246-
247-
// create the response
248-
let response = create_asset_response(headers, body, cel_expr_str.to_string());
224+
// create the response
225+
let response = create_asset_response(headers, body, ASSET_CEL_EXPR.clone());
249226

250-
// certify the response
251-
let certification =
252-
HttpCertification::response_only(cel_expr_def, &response, None).unwrap();
227+
// certify the response
228+
let certification =
229+
HttpCertification::response_only(&ASSET_CEL_EXPR_DEF, &response, None).unwrap();
253230

254-
HTTP_TREE.with_borrow_mut(|http_tree| {
255-
// add the certification to the certification tree
256-
http_tree.insert(&HttpCertificationTreeEntry::new(
257-
asset_tree_path,
258-
&certification,
259-
));
260-
});
231+
HTTP_TREE.with_borrow_mut(|http_tree| {
232+
// add the certification to the certification tree
233+
http_tree.insert(&HttpCertificationTreeEntry::new(
234+
asset_tree_path,
235+
&certification,
236+
));
237+
});
261238

262-
ENCODED_RESPONSES.with_borrow_mut(|responses| {
263-
// store the response for later retrieval
264-
responses.insert(
265-
(asset_req_path, encoding.to_string()),
266-
CertifiedHttpResponse {
267-
response,
268-
certification,
269-
},
270-
);
271-
});
239+
ENCODED_RESPONSES.with_borrow_mut(|responses| {
240+
// store the response for later retrieval
241+
responses.insert(
242+
(asset_req_path, encoding.to_string()),
243+
CertifiedHttpResponse {
244+
response,
245+
certification,
246+
},
247+
);
272248
});
273249
};
274250
}

examples/http-certification/custom-assets/src/backend/src/lib.rs

+44-67
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::{cell::RefCell, collections::HashMap};
1616

1717
#[init]
1818
fn init() {
19-
prepare_cel_exprs();
2019
certify_all_assets();
2120
}
2221

@@ -41,38 +40,26 @@ thread_local! {
4140
static HTTP_TREE: RefCell<HttpCertificationTree> = RefCell::new(HttpCertificationTree::default());
4241
static ENCODED_RESPONSES: RefCell<HashMap<(String, String), CertifiedHttpResponse<'static>>> = RefCell::new(HashMap::new());
4342
static RESPONSES: RefCell<HashMap<String, CertifiedHttpResponse<'static>>> = RefCell::new(HashMap::new());
44-
static CEL_EXPRS: RefCell<HashMap<String, (DefaultResponseOnlyCelExpression<'static>, String)>> = RefCell::new(HashMap::new());
4543
}
4644

4745
static ASSETS_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../frontend/dist");
4846

4947
lazy_static! {
50-
static ref ASSET_CEL_EXPR_PATH: &'static str = "assets";
5148
static ref INDEX_REQ_PATH: &'static str = "";
5249
static ref INDEX_TREE_PATH: HttpCertificationPath<'static> =
5350
HttpCertificationPath::wildcard(*INDEX_REQ_PATH);
5451
static ref INDEX_FILE_PATH: &'static str = "index.html";
52+
static ref ASSET_CEL_EXPR_DEF: DefaultResponseOnlyCelExpression<'static> =
53+
DefaultCelBuilder::response_only_certification()
54+
.with_response_certification(DefaultResponseCertification::response_header_exclusions(
55+
vec![],
56+
))
57+
.build();
58+
static ref ASSET_CEL_EXPR: String = ASSET_CEL_EXPR_DEF.to_string();
5559
}
5660

5761
// Certification
5862

59-
fn prepare_cel_exprs() {
60-
let asset_cel_expr_def = DefaultCelBuilder::response_only_certification()
61-
.with_response_certification(DefaultResponseCertification::response_header_exclusions(
62-
vec![],
63-
))
64-
.build();
65-
66-
let asset_cel_expr = asset_cel_expr_def.to_string();
67-
68-
CEL_EXPRS.with_borrow_mut(|exprs| {
69-
exprs.insert(
70-
ASSET_CEL_EXPR_PATH.to_string(),
71-
(asset_cel_expr_def, asset_cel_expr),
72-
);
73-
});
74-
}
75-
7663
fn certify_all_assets() {
7764
add_certification_skips();
7865

@@ -202,55 +189,12 @@ fn certify_asset_with_encoding(
202189
let mut headers = vec![("content-encoding".to_string(), encoding.to_string())];
203190
headers.extend(additional_headers);
204191

205-
CEL_EXPRS.with_borrow(|cel_exprs| {
206-
// get the relevant CEL expression
207-
let (cel_expr_def, cel_expr_str) = cel_exprs.get(*ASSET_CEL_EXPR_PATH).unwrap();
208-
209-
// create the response
210-
let response = create_asset_response(headers, body, cel_expr_str.to_string());
211-
212-
// certify the response
213-
let certification =
214-
HttpCertification::response_only(cel_expr_def, &response, None).unwrap();
215-
216-
HTTP_TREE.with_borrow_mut(|http_tree| {
217-
// add the certification to the certification tree
218-
http_tree.insert(&HttpCertificationTreeEntry::new(
219-
asset_tree_path,
220-
&certification,
221-
));
222-
});
223-
224-
ENCODED_RESPONSES.with_borrow_mut(|responses| {
225-
// store the response for later retrieval
226-
responses.insert(
227-
(asset_req_path, encoding.to_string()),
228-
CertifiedHttpResponse {
229-
response,
230-
certification,
231-
},
232-
);
233-
});
234-
});
235-
};
236-
}
237-
238-
fn certify_asset_response(
239-
body: &'static [u8],
240-
additional_headers: Vec<HeaderField>,
241-
asset_tree_path: &HttpCertificationPath,
242-
asset_req_path: String,
243-
) {
244-
CEL_EXPRS.with_borrow(|cel_exprs| {
245-
// get the relevant CEL expression
246-
let (cel_expr_def, cel_expr_str) = cel_exprs.get(*ASSET_CEL_EXPR_PATH).unwrap();
247-
248192
// create the response
249-
let response = create_asset_response(additional_headers, body, cel_expr_str.to_string());
193+
let response = create_asset_response(headers, body, ASSET_CEL_EXPR.clone());
250194

251195
// certify the response
252196
let certification =
253-
HttpCertification::response_only(cel_expr_def, &response, None).unwrap();
197+
HttpCertification::response_only(&ASSET_CEL_EXPR_DEF, &response, None).unwrap();
254198

255199
HTTP_TREE.with_borrow_mut(|http_tree| {
256200
// add the certification to the certification tree
@@ -260,16 +204,49 @@ fn certify_asset_response(
260204
));
261205
});
262206

263-
RESPONSES.with_borrow_mut(|responses| {
207+
ENCODED_RESPONSES.with_borrow_mut(|responses| {
264208
// store the response for later retrieval
265209
responses.insert(
266-
asset_req_path,
210+
(asset_req_path, encoding.to_string()),
267211
CertifiedHttpResponse {
268212
response,
269213
certification,
270214
},
271215
);
272216
});
217+
};
218+
}
219+
220+
fn certify_asset_response(
221+
body: &'static [u8],
222+
additional_headers: Vec<HeaderField>,
223+
asset_tree_path: &HttpCertificationPath,
224+
asset_req_path: String,
225+
) {
226+
// create the response
227+
let response = create_asset_response(additional_headers, body, ASSET_CEL_EXPR.clone());
228+
229+
// certify the response
230+
let certification =
231+
HttpCertification::response_only(&ASSET_CEL_EXPR_DEF, &response, None).unwrap();
232+
233+
HTTP_TREE.with_borrow_mut(|http_tree| {
234+
// add the certification to the certification tree
235+
http_tree.insert(&HttpCertificationTreeEntry::new(
236+
asset_tree_path,
237+
&certification,
238+
));
239+
});
240+
241+
RESPONSES.with_borrow_mut(|responses| {
242+
// store the response for later retrieval
243+
responses.insert(
244+
asset_req_path,
245+
CertifiedHttpResponse {
246+
response,
247+
certification,
248+
},
249+
);
273250
});
274251
}
275252

0 commit comments

Comments
 (0)