Skip to content

Commit cee84f5

Browse files
docs(ic-http-certification): simplify examples and improve docs (#396)
Co-authored-by: przydatek <[email protected]>
1 parent fefe1dc commit cee84f5

File tree

6 files changed

+222
-298
lines changed

6 files changed

+222
-298
lines changed

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

@@ -189,35 +175,30 @@ fn certify_asset_response(
189175
asset_tree_path: &HttpCertificationPath,
190176
asset_req_path: String,
191177
) {
192-
CEL_EXPRS.with_borrow(|cel_exprs| {
193-
// get the relevant CEL expression
194-
let (cel_expr_def, cel_expr_str) = cel_exprs.get(*ASSET_CEL_EXPR_PATH).unwrap();
178+
// create the response
179+
let response = create_asset_response(additional_headers, body, ASSET_CEL_EXPR.clone());
195180

196-
// create the response
197-
let response = create_asset_response(additional_headers, body, cel_expr_str.to_string());
198-
199-
// certify the response
200-
let certification =
201-
HttpCertification::response_only(cel_expr_def, &response, None).unwrap();
181+
// certify the response
182+
let certification =
183+
HttpCertification::response_only(&ASSET_CEL_EXPR_DEF, &response, None).unwrap();
202184

203-
HTTP_TREE.with_borrow_mut(|http_tree| {
204-
// add the certification to the certification tree
205-
http_tree.insert(&HttpCertificationTreeEntry::new(
206-
asset_tree_path,
207-
&certification,
208-
));
209-
});
185+
HTTP_TREE.with_borrow_mut(|http_tree| {
186+
// add the certification to the certification tree
187+
http_tree.insert(&HttpCertificationTreeEntry::new(
188+
asset_tree_path,
189+
&certification,
190+
));
191+
});
210192

211-
RESPONSES.with_borrow_mut(|responses| {
212-
// store the response for later retrieval
213-
responses.insert(
214-
asset_req_path,
215-
CertifiedHttpResponse {
216-
response,
217-
certification,
218-
},
219-
);
220-
});
193+
RESPONSES.with_borrow_mut(|responses| {
194+
// store the response for later retrieval
195+
responses.insert(
196+
asset_req_path,
197+
CertifiedHttpResponse {
198+
response,
199+
certification,
200+
},
201+
);
221202
});
222203
}
223204
```
@@ -244,35 +225,30 @@ fn certify_asset_with_encoding(
244225
let mut headers = vec![("content-encoding".to_string(), encoding.to_string())];
245226
headers.extend(additional_headers);
246227

247-
CEL_EXPRS.with_borrow(|cel_exprs| {
248-
// get the relevant CEL expression
249-
let (cel_expr_def, cel_expr_str) = cel_exprs.get(*ASSET_CEL_EXPR_PATH).unwrap();
250-
251-
// create the response
252-
let response = create_asset_response(headers, body, cel_expr_str.to_string());
228+
// create the response
229+
let response = create_asset_response(headers, body, ASSET_CEL_EXPR.clone());
253230

254-
// certify the response
255-
let certification =
256-
HttpCertification::response_only(cel_expr_def, &response, None).unwrap();
231+
// certify the response
232+
let certification =
233+
HttpCertification::response_only(&ASSET_CEL_EXPR_DEF, &response, None).unwrap();
257234

258-
HTTP_TREE.with_borrow_mut(|http_tree| {
259-
// add the certification to the certification tree
260-
http_tree.insert(&HttpCertificationTreeEntry::new(
261-
asset_tree_path,
262-
&certification,
263-
));
264-
});
235+
HTTP_TREE.with_borrow_mut(|http_tree| {
236+
// add the certification to the certification tree
237+
http_tree.insert(&HttpCertificationTreeEntry::new(
238+
asset_tree_path,
239+
&certification,
240+
));
241+
});
265242

266-
ENCODED_RESPONSES.with_borrow_mut(|responses| {
267-
// store the response for later retrieval
268-
responses.insert(
269-
(asset_req_path, encoding.to_string()),
270-
CertifiedHttpResponse {
271-
response,
272-
certification,
273-
},
274-
);
275-
});
243+
ENCODED_RESPONSES.with_borrow_mut(|responses| {
244+
// store the response for later retrieval
245+
responses.insert(
246+
(asset_req_path, encoding.to_string()),
247+
CertifiedHttpResponse {
248+
response,
249+
certification,
250+
},
251+
);
276252
});
277253
};
278254
}

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)