Skip to content

Commit d594ea2

Browse files
authored
Merge branch 'main' into nathan/switch-shared-pnpm-action
2 parents 0d56e53 + aaa4264 commit d594ea2

File tree

19 files changed

+961
-179
lines changed

19 files changed

+961
-179
lines changed

Cargo.lock

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

examples/http-certification/assets/README.md

+54-15
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ thread_local! {
174174
}
175175

176176
const IMMUTABLE_ASSET_CACHE_CONTROL: &str = "public, max-age=31536000, immutable";
177+
const NO_CACHE_ASSET_CACHE_CONTROL: &str = "public, no-cache, no-store";
177178

178179
fn certify_all_assets() {
179180
// 1. Define the asset certification configurations.
@@ -188,7 +189,7 @@ fn certify_all_assets() {
188189
content_type: Some("text/html".to_string()),
189190
headers: get_asset_headers(vec![(
190191
"cache-control".to_string(),
191-
"public, no-cache, no-store".to_string(),
192+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
192193
)]),
193194
fallback_for: vec![AssetFallbackConfig {
194195
scope: "/".to_string(),
@@ -224,19 +225,17 @@ fn certify_all_assets() {
224225
)]),
225226
encodings: vec![],
226227
},
227-
AssetConfig::Pattern {
228-
pattern: "**/*.svg".to_string(),
229-
content_type: Some("image/svg+xml".to_string()),
230-
headers: get_asset_headers(vec![(
231-
"cache-control".to_string(),
232-
IMMUTABLE_ASSET_CACHE_CONTROL.to_string(),
233-
)]),
234-
encodings: vec![],
235-
},
236228
AssetConfig::Redirect {
237229
from: "/old-url".to_string(),
238230
to: "/".to_string(),
239231
kind: AssetRedirectKind::Permanent,
232+
headers: get_asset_headers(vec![
233+
("content-type".to_string(), "text/plain".to_string()),
234+
(
235+
"cache-control".to_string(),
236+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
237+
),
238+
]),
240239
},
241240
];
242241

@@ -304,9 +303,21 @@ fn serve_metrics() -> HttpResponse<'static> {
304303
cycle_balance: canister_balance(),
305304
};
306305
let body = serde_json::to_vec(&metrics).expect("Failed to serialize metrics");
306+
let headers = get_asset_headers(vec![
307+
(
308+
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
309+
DefaultCelBuilder::skip_certification().to_string(),
310+
),
311+
("content-type".to_string(), "application/json".to_string()),
312+
(
313+
"cache-control".to_string(),
314+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
315+
),
316+
]);
307317
let mut response = HttpResponse::builder()
308318
.with_status_code(200)
309319
.with_body(body)
320+
.with_headers(headers)
310321
.build();
311322

312323
HTTP_TREE.with(|tree| {
@@ -323,13 +334,41 @@ fn serve_metrics() -> HttpResponse<'static> {
323334
&metrics_tree_path.to_expr_path(),
324335
);
325336

326-
let headers = get_asset_headers(vec![(
327-
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
328-
DefaultCelBuilder::skip_certification().to_string(),
329-
)]);
330-
response.headers_mut().extend_from_slice(&headers);
331337
response
332338
})
333339
})
334340
}
335341
```
342+
343+
## Testing the canister
344+
345+
To test the canister, you can use the `dfx` command-line tool. First, run DFX:
346+
347+
```shell
348+
dfx start --background --clean
349+
```
350+
351+
Then, deploy the canister:
352+
353+
```shell
354+
dfx deploy http_certification_assets_backend
355+
```
356+
357+
You can now access the canister's assets by navigating to the canister's URL in a web browser. The URL can also be found using the following command:
358+
359+
```shell
360+
echo "http://$(dfx canister id http_certification_assets_backend).localhost:$(dfx info webserver-port)"
361+
```
362+
363+
Alternatively, to make a request with `curl`:
364+
365+
```shell
366+
curl "http://$(dfx canister id http_certification_assets_backend).localhost:$(dfx info webserver-port)" --resolve "$(dfx canister id http_certification_assets_backend).localhost:$(dfx info webserver-port):127.0.0.1"
367+
```
368+
369+
## Resources
370+
371+
- [Example source code](https://github.com/dfinity/response-verification/tree/main/examples/http-certification/assets).
372+
- [`ic-asset-certification` crate](https://crates.io/crates/ic-asset-certification).
373+
- [`ic-asset-certification` docs](https://docs.rs/ic-asset-certification/latest/ic_asset_certification).
374+
- [`ic-asset-certification` source code](https://github.com/dfinity/response-verification/tree/main/packages/ic-asset-certification).

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

+21-15
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ thread_local! {
6262

6363
static ASSETS_DIR: Dir<'_> = include_dir!("$CARGO_MANIFEST_DIR/../frontend/dist");
6464
const IMMUTABLE_ASSET_CACHE_CONTROL: &str = "public, max-age=31536000, immutable";
65+
const NO_CACHE_ASSET_CACHE_CONTROL: &str = "public, no-cache, no-store";
6566

6667
/// Rescursively collect all assets from the provided directory
6768
fn collect_assets<'content, 'path>(
@@ -91,7 +92,7 @@ fn certify_all_assets() {
9192
content_type: Some("text/html".to_string()),
9293
headers: get_asset_headers(vec![(
9394
"cache-control".to_string(),
94-
"public, no-cache, no-store".to_string(),
95+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
9596
)]),
9697
fallback_for: vec![AssetFallbackConfig {
9798
scope: "/".to_string(),
@@ -127,19 +128,17 @@ fn certify_all_assets() {
127128
)]),
128129
encodings: vec![],
129130
},
130-
AssetConfig::Pattern {
131-
pattern: "**/*.svg".to_string(),
132-
content_type: Some("image/svg+xml".to_string()),
133-
headers: get_asset_headers(vec![(
134-
"cache-control".to_string(),
135-
IMMUTABLE_ASSET_CACHE_CONTROL.to_string(),
136-
)]),
137-
encodings: vec![],
138-
},
139131
AssetConfig::Redirect {
140132
from: "/old-url".to_string(),
141133
to: "/".to_string(),
142134
kind: AssetRedirectKind::Permanent,
135+
headers: get_asset_headers(vec![
136+
("content-type".to_string(), "text/plain".to_string()),
137+
(
138+
"cache-control".to_string(),
139+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
140+
),
141+
]),
143142
},
144143
];
145144

@@ -179,9 +178,21 @@ fn serve_metrics() -> HttpResponse<'static> {
179178
cycle_balance: canister_balance(),
180179
};
181180
let body = serde_json::to_vec(&metrics).expect("Failed to serialize metrics");
181+
let headers = get_asset_headers(vec![
182+
(
183+
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
184+
DefaultCelBuilder::skip_certification().to_string(),
185+
),
186+
("content-type".to_string(), "application/json".to_string()),
187+
(
188+
"cache-control".to_string(),
189+
NO_CACHE_ASSET_CACHE_CONTROL.to_string(),
190+
),
191+
]);
182192
let mut response = HttpResponse::builder()
183193
.with_status_code(StatusCode::OK)
184194
.with_body(body)
195+
.with_headers(headers)
185196
.build();
186197

187198
HTTP_TREE.with(|tree| {
@@ -198,11 +209,6 @@ fn serve_metrics() -> HttpResponse<'static> {
198209
&metrics_tree_path.to_expr_path(),
199210
);
200211

201-
let headers = get_asset_headers(vec![(
202-
CERTIFICATE_EXPRESSION_HEADER_NAME.to_string(),
203-
DefaultCelBuilder::skip_certification().to_string(),
204-
)]);
205-
response.headers_mut().extend_from_slice(&headers);
206212
response
207213
})
208214
})

0 commit comments

Comments
 (0)