ic-asset-certification 3.0.0

Certification for static assets served over HTTP on the Internet Computer
Documentation
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
# Asset Certification

## Overview

Asset certification is a specialized form of
[HTTP Certification](https://internetcomputer.org/docs/current/developer-docs/http-compatible-canisters/custom-http-canisters)
purpose-built for certifying static assets in [ICP](https://internetcomputer.org/) canisters.

The `ic-asset-certification` crate provides the necessary functionality to
certify and serve static assets from Rust canisters.

This is implemented in the following steps:

1. [Preparing assets]#preparing-assets
2. [Configuring asset certification]#configuring-asset-certification
3. [Inserting assets into the asset router]#inserting-assets-into-the-asset-router
4. [Serving assets]#serving-assets
5. [Deleting assets]#deleting-assets
6. [Querying assets]#querying-assets

For canisters that need it, it's also possible to [delete assets](#deleting-assets).

## Preparing assets

This library is unopinionated about where assets come from, so that is not
covered in detail here, but there are three main options:

- Embedding assets in the canister at compile time:
  - [include_bytes!]https://doc.rust-lang.org/std/macro.include_bytes.html
  - [include_dir!]https://docs.rs/include_dir/latest/include_dir/index.html
- Uploading assets via canister endpoints at runtime.
  - The [DFX asset canister]https://github.com/dfinity/sdk/blob/master/docs/design/asset-canister-interface.md is a good example of this approach.
- Generating assets dynamically in code, at runtime.

With the assets in memory, they can be converted into the `Asset` type:

```rust
use ic_asset_certification::Asset;

let asset = Asset::new(
    "index.html",
    b"<html><body><h1>Hello World!</h1></body></html>".as_slice(),
);
```

It is recommended to use references when including assets directly into the
canister, to avoid duplicating the content. This is particularly important for
larger assets.

```rust
use ic_asset_certification::Asset;

let pretty_big_asset = include_bytes!("lib.rs");
let asset = Asset::new(
    "assets/pretty-big-asset.gz",
    pretty_big_asset.as_slice(),
);
```

In some cases, it may be necessary to use owned values, such as when assets are
dynamically generated or modified at runtime.

```rust
use ic_asset_certification::Asset;

let name = "World";
let asset = Asset::new(
    "index.html",
    format!("<html><body><h1>Hello {name}!</h1></body></html>").into_bytes(),
);
```

## Configuring asset certification

`AssetConfig` defines the configuration for any files that will be certified.
The configuration can either be matched to an individual file by path, or to
many files by a glob.

In both cases, the following options can be configured for each asset:

- `content_type`
  - Providing this option will certify and serve a `Content-Type` header with
    the provided value.
  - If this value is not provided, the `Content-Type` header will not be
    inserted.
  - If the `Content-Type` header is not sent to the browser, the browser will
    try to guess the content type based on the file extension, unless an
    `X-Content-Type-Options: nosniff` header is sent.
  - Not certifying the `Content-Type` header will also allow a malicious replica
    to insert its own `Content-Type` header, which could lead to a security
    vulnerability.
- `headers`
  - Any additional headers provided will be certified and served with the
    asset.
  - It's important to include any headers that can affect browser behavior,
    particularly [security headers]https://owasp.org/www-project-secure-headers/index.html.
- `encodings`
  - A list of alternative encodings that can be used to serve the asset.
  - Each entry is a tuple of the [encoding name]AssetEncoding and the file
    extension used in the file path, that can be conveniently created with
    the `default` factory method. For example, to include Brotli and Gzip encodings:
    `vec![AssetEncoding::Brotli.default(), AssetEncoding::Gzip.default()]`.
  - The default file extensions for each encoding are:
    - Brotli: `br`
    - Gzip: `gz`
    - Deflate: `zz`
    - Zstd: `zst`
  - Alternatively, a custom file extension can be provided for each encoding
    by using the `custom` factory method. For example, to include a custom
    file extension for Brotli and Gzip encodings:
    `vec![AssetEncoding::Brotli.custom("brotli"), AssetEncoding::Gzip.custom("gzip")]`.
  - Each encoding referenced must be provided to the asset router as a
    separate file with the same filename as the original file, but with an
    additional file extension matching the configuration. For example, if the
    current matched file is named `file.html`, then the asset router will
    look for `file.html.br` and `file.html.gz`.
  - If the file is found, the asset will be certified and served with the
    provided encoding according to the `Accept-Encoding`.
  - Encodings are prioritized in the following order:
    - Brotli
    - Zstd
    - Gzip
    - Deflate
    - Identity
  - The asset router will return the highest priority encoding that has been
    certified and is supported by the client.

### Configuring individual files

When configuring an individual file, the `path` property is provided and must
match the path passed into the `Asset` constructor in the previous step.

In addition to the common configuration options, individual assets also have
the option of registering the asset as a fallback response for a particular
scope. This can be used to configure 404 pages or single-page application
entry points, for example.

When serving assets, if a requested path does not exactly match any assets then
a search is conducted for an asset configured with the fallback scope that most
closely matches the requested asset's path.

For example, if a request is made for `/app.js` and no asset with that exact
path is found, an attempt will be made to serve an asset configured with a
fallback scope of `/`.

This will be done recursively until it's no longer
possible to find a valid fallback. For example, if a request is made for
`/assets/js/app/core/index.js` and no asset with that exact path is found, then
the search will check for assets configured with the following fallback scopes,
in order:

- `/assets/js/app/core`
- `/assets/js/app`
- `/assets/js`
- `/assets`
- `/`

If multiple fallback assets are configured, the first one found will be used,
since that will be the most specific one available for that path. If no asset is
found with any of these fallback scopes, no response will be returned.

It's also possible to register aliases for an asset. This can be useful for
configuring multiple paths that should serve the same asset. For example, if an
asset is configured with the path `index.html`, it can be aliased by the path
`/`.

The following example configures an individual HTML file to be served by the
on the `/index.html` path, in addition to serving as the fallback for the `/`
scope and setting `/` as an alias for this asset.

```rust
use ic_http_certification::StatusCode;
use ic_asset_certification::{AssetConfig, AssetFallbackConfig};

let config = AssetConfig::File {
    path: "index.html".to_string(),
    content_type: Some("text/html".to_string()),
    headers: vec![
        ("Cache-Control".to_string(), "public, no-cache, no-store".to_string()),
    ],
    fallback_for: vec![AssetFallbackConfig {
        scope: "/".to_string(),
        status_code: Some(StatusCode::OK),
    }],
    aliased_by: vec!["/".to_string()],
    encodings: vec![
        AssetEncoding::Brotli.default(),
        AssetEncoding::Gzip.default()
    ],
};
```

It's also possible to configure multiple fallbacks for a single asset. The
following example configures an individual HTML file to be served by the on the
`/404.html` path, in addition to serving as the fallback for the `/js` and `/css`
scopes.

Any request to paths starting in `/js` and `/css` directories that don't exactly
match an asset will be routed to the `/404.html` asset.

Multiple aliases are also configured for this asset, namely:

- `/404`,
- `/404/`,
- `/404.html`
- `/not-found`
- `/not-found/`
- `/not-found/index.html`

Requests to any of those aliases will serve the `/404.html` asset.

```rust
use ic_http_certification::StatusCode;
use ic_asset_certification::{AssetConfig, AssetFallbackConfig};

let config = AssetConfig::File {
    path: "404.html".to_string(),
    content_type: Some("text/html".to_string()),
    headers: vec![
        ("Cache-Control".to_string(), "public, no-cache, no-store".to_string()),
    ],
    fallback_for: vec![
        AssetFallbackConfig {
            scope: "/css".to_string(),
            status_code: Some(StatusCode::NOT_FOUND),
        },
        AssetFallbackConfig {
            scope: "/js".to_string(),
            status_code: Some(StatusCode::NOT_FOUND),
        },
    ],
    aliased_by: vec![
        "/404".to_string(),
        "/404/".to_string(),
        "/404.html".to_string(),
        "/not-found".to_string(),
        "/not-found/".to_string(),
        "/not-found/index.html".to_string(),
    ],
    encodings: vec![
        AssetEncoding::Brotli.default(),
        AssetEncoding::Gzip.default(),
    ],
};
```

### Configuring file patterns

When configuring file patterns, the `pattern` property is provided. This
property is a glob pattern that will be used to match multiple files.

Standard Unix-style glob syntax is supported:

- `?` matches any single character.
- `*` matches zero or more characters.
- `**` recursively matches directories but is only legal in three
  situations.
  - If the glob starts with `**/`, then it matches all directories.
    For example, `**/foo` matches `foo` and `bar/foo` but not
    `foo/bar`.
  - If the glob ends with `/**`, then it matches all sub-entries.
    For example, `foo/**` matches `foo/a` and `foo/a/b`, but not
    `foo`.
  - If the glob contains `/**/` anywhere within the pattern, then it
    matches zero or more directories.
  - Using `**` anywhere else is illegal.
  - The glob `**` is allowed and means "match everything".
- `{a,b}` matches `a` or `b` where `a` and `b` are arbitrary glob
  patterns. (N.B. Nesting `{...}` is not currently allowed.)
- `[ab]` matches `a` or `b` where `a` and `b` are characters.
- `[!ab]` to match any character except for `a` and `b`.
- Metacharacters such as `*` and `?` can be escaped with character
  class notation. e.g., `[*]` matches `*`.

For example, the following pattern will match all `.js` files in the `js`
directory:

```rust
use ic_http_certification::StatusCode;
use ic_asset_certification::AssetConfig;

let config = AssetConfig::Pattern {
    pattern: "js/*.js".to_string(),
    content_type: Some("application/javascript".to_string()),
    headers: vec![
        ("Cache-Control".to_string(), "public, max-age=31536000, immutable".to_string()),
    ],
    encodings: vec![
        AssetEncoding::Brotli.default(),
        AssetEncoding::Gzip.default(),
    ],
};
```

### Configuring redirects

Redirects can be configured using the `AssetConfig::Redirect` variant. This
variant takes a `from` and `to` paths, and a redirect `kind`.
When a request is made to the `from` path, the client will be redirected to the
`to` path. The `AssetConfig::Redirect` config is not matched against any `Asset`s.

Redirects can be configured as either permanent
or temporary.

The browser will cache permanent redirects and will not request the old
location again. This is useful when the resource has permanently moved to a new
location. The browser will update its bookmarks and search engine results.

See the
[MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301)
for more information on permanent redirects.

The browser will not cache temporary redirects and will request
the old location again. This is useful when the resource has temporarily moved
to a new location. The browser will not update its bookmarks and search engine
results.

See the
[MDN Web Docs](https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/307)
for more information on temporary redirects.

The following example configures a permanent redirect from `/old` to `/new`:

```rust
use ic_asset_certification::{AssetConfig, AssetRedirectKind};

let config = AssetConfig::Redirect {
  from: "/old".to_string(),
  to: "/new".to_string(),
  kind: AssetRedirectKind::Permanent,
  headers: vec![(
    "content-type".to_string(),
    "text/plain; charset=utf-8".to_string(),
  )],
};
```

## Inserting assets into the asset router

The `AssetRouter` is responsible for certifying responses and routing requests to
the appropriate response.

Assets can be inserted using the `certify_assets` method:

```rust
use ic_http_certification::StatusCode;
use ic_asset_certification::{Asset, AssetConfig, AssetFallbackConfig, AssetRouter, AssetRedirectKind};

let mut asset_router = AssetRouter::default();

let assets = vec![
    Asset::new(
        "index.html",
        b"<html><body><h1>Hello World!</h1></body></html>".as_slice(),
    ),
    Asset::new(
        "index.html.gz",
        [0, 1, 2, 3, 4, 5]
    ),
    Asset::new(
        "index.html.br",
        [6, 7, 8, 9, 10, 11]
    ),
    Asset::new(
        "app.js",
        b"console.log('Hello World!');".as_slice(),
    ),
    Asset::new(
        "app.js.gz",
        [12, 13, 14, 15, 16, 17],
    ),
    Asset::new(
        "app.js.br",
        [18, 19, 20, 21, 22, 23],
    ),
    Asset::new(
      "css/app-ba74b708.css",
      b"html,body{min-height:100vh;}".as_slice(),
    ),
    Asset::new(
        "css/app-ba74b708.css.gz",
        [24, 25, 26, 27, 28, 29],
    ),
    Asset::new(
        "css/app-ba74b708.css.br",
        [30, 31, 32, 33, 34, 35],
    ),
];

let asset_configs = vec![
    AssetConfig::File {
        path: "index.html".to_string(),
        content_type: Some("text/html".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, no-cache, no-store".to_string(),
        )],
        fallback_for: vec![AssetFallbackConfig {
            scope: "/".to_string(),
            status_code: Some(StatusCode::OK),
        }],
        aliased_by: vec!["/".to_string()],
        encodings: vec![
            AssetEncoding::Brotli.default(),
            AssetEncoding::Gzip.default(),
        ],
    },
    AssetConfig::Pattern {
        pattern: "**/*.js".to_string(),
        content_type: Some("text/javascript".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, max-age=31536000, immutable".to_string(),
        )],
        encodings: vec![
            AssetEncoding::Brotli.default(),
            AssetEncoding::Gzip.default(),
        ],
    },
    AssetConfig::Pattern {
        pattern: "**/*.css".to_string(),
        content_type: Some("text/css".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, max-age=31536000, immutable".to_string(),
        )],
        encodings: vec![
            AssetEncoding::Brotli.default(),
            AssetEncoding::Gzip.default(),
        ],
    },
    AssetConfig::Redirect {
        from: "/old".to_string(),
        to: "/new".to_string(),
        kind: AssetRedirectKind::Permanent,
        headers: vec![(
            "content-type".to_string(),
            "text/plain; charset=utf-8".to_string(),
        )],
    },
];

asset_router.certify_assets(assets, asset_configs).unwrap();
```

After certifying assets, make sure to set the canister's
certified data:

```rust
use ic_cdk::api::set_certified_data;

set_certified_data(&asset_router.root_hash());
```

After creating the `AssetRouter`, it's also possible to initialize the router
with an `HttpCertificationTree`. This is useful when direct access to the
`HttpCertificationTree` is required for certifying `HttpRequest`s and
`HttpResponse`s outside of the `AssetRouter`.

The initialization of the `AssetRouter` must be done before certifying any assets
as the initialization function will reset the internal state of the `AssetRouter`.

```rust
use std::{cell::RefCell, rc::Rc};
use ic_http_certification::HttpCertificationTree;
use ic_asset_certification::AssetRouter;

let mut http_certification_tree: Rc<RefCell<HttpCertificationTree>> = Default::default();
let mut asset_router = AssetRouter::default();

asset_router.init_with_tree(http_certification_tree.clone());
```

## Serving assets

Assets can be served by calling the `serve_asset` method on the `AssetRouter`.
This method will return a response, a witness and an expression path, which can be used
alongside the canister's data certificate to add the required certificate header to the response.

```rust
use ic_http_certification::{HttpRequest, utils::add_v2_certificate_header, StatusCode};
use ic_asset_certification::{Asset, AssetConfig, AssetFallbackConfig, AssetRouter};

let mut asset_router = AssetRouter::default();

let asset = Asset::new(
    "index.html",
    b"<html><body><h1>Hello World!</h1></body></html>".as_slice(),
);

let asset_config = AssetConfig::File {
    path: "index.html".to_string(),
    content_type: Some("text/html".to_string()),
    headers: vec![
        ("Cache-Control".to_string(), "public, no-cache, no-store".to_string()),
    ],
    fallback_for: vec![AssetFallbackConfig {
        scope: "/".to_string(),
        status_code: Some(StatusCode::OK),
    }],
    aliased_by: vec!["/".to_string()],
    encodings: vec![],
};

let http_request = HttpRequest::get("/").build();

asset_router.certify_assets(vec![asset], vec![asset_config]).unwrap();

let (mut response, witness, expr_path) = asset_router.serve_asset(&http_request).unwrap();

// this should normally be retrieved using `ic_cdk::api::data_certificate()`.
let data_certificate = vec![1, 2, 3];

add_v2_certificate_header(
    data_certificate,
    &mut response,
    &witness,
    &expr_path,
);
```

## Deleting assets

There are three ways to delete assets from the asset router:

1. [By configuration]#deleting-assets-by-configuration.
1. [By path]#deleting-assets-by-path.
1. [All at once]#deleting-all-assets.

### Deleting assets by configuration

Deleting assets by configuration is similar to (certifying them)[#inserting-assets-into-the-asset-router].

Depending on the configuration provided to the `certify_assets` function,
multiple responses may be generated for the same asset. To ensure that all generated responses are deleted,
the `delete_assets` function accepts the same configuration.

If a configuration different to the one used to certify assets in the first place is provided,
one of two things can happen.

If the configuration inclues a file that was not certified in the first place, it will be silently ignored.
For example, if the configuration provided to `certify_assets` includes the Brotli and Gzip encodings, but the
configuration provided to `delete_assets` includes Brotli, Gzip and Deflate, the Brotli and Gzip encoded files will be deleted, while the Deflate file is ignored, since it doesn't exist.

If the configuration excludes a file that was certified, it will not be deleted. For example, if the configuration,
provided to `certify_assets` includes the Brotli and Gzip encodings, but the configuration provided to `delete_assets`
only includes Brotli, then the Gzip file will not be deleted.

Assuming the same base example used above to demonstrate certifying assets:

```rust
use ic_http_certification::StatusCode;
use ic_asset_certification::{Asset, AssetConfig, AssetFallbackConfig, AssetRouter, AssetRedirectKind, AssetEncoding};

let mut asset_router = AssetRouter::default();

let assets = vec![
    Asset::new(
        "index.html",
        b"<html><body><h1>Hello World!</h1></body></html>".as_slice(),
    ),
    Asset::new(
        "index.html.gz",
        &[0, 1, 2, 3, 4, 5]
    ),
    Asset::new(
        "index.html.br",
        &[6, 7, 8, 9, 10, 11]
    ),
    Asset::new(
        "app.js",
        b"console.log('Hello World!');".as_slice(),
    ),
    Asset::new(
        "app.js.gz",
        &[12, 13, 14, 15, 16, 17],
    ),
    Asset::new(
        "app.js.br",
        &[18, 19, 20, 21, 22, 23],
    ),
    Asset::new(
        "css/app-ba74b708.css",
        b"html,body{min-height:100vh;}".as_slice(),
    ),
    Asset::new(
        "css/app-ba74b708.css.gz",
        &[24, 25, 26, 27, 28, 29],
    ),
    Asset::new(
        "css/app-ba74b708.css.br",
        &[30, 31, 32, 33, 34, 35],
    ),
];

let asset_configs = vec![
    AssetConfig::File {
        path: "index.html".to_string(),
        content_type: Some("text/html".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, no-cache, no-store".to_string(),
        )],
        fallback_for: vec![AssetFallbackConfig {
            scope: "/".to_string(),
            status_code: Some(StatusCode::OK),
        }],
        aliased_by: vec!["/".to_string()],
        encodings: vec![
            AssetEncoding::Brotli.default_config(),
            AssetEncoding::Gzip.default_config(),
        ],
    },
    AssetConfig::Pattern {
        pattern: "**/*.js".to_string(),
        content_type: Some("text/javascript".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, max-age=31536000, immutable".to_string(),
        )],
        encodings: vec![
            AssetEncoding::Brotli.default_config(),
            AssetEncoding::Gzip.default_config(),
        ],
    },
    AssetConfig::Pattern {
        pattern: "**/*.css".to_string(),
        content_type: Some("text/css".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, max-age=31536000, immutable".to_string(),
        )],
        encodings: vec![
            AssetEncoding::Brotli.default_config(),
            AssetEncoding::Gzip.default_config(),
        ],
    },
    AssetConfig::Redirect {
        from: "/old".to_string(),
        to: "/new".to_string(),
        kind: AssetRedirectKind::Permanent,
        headers: vec![(
            "content-type".to_string(),
            "text/plain; charset=utf-8".to_string(),
        )],
    },
];

asset_router.certify_assets(assets, asset_configs).unwrap();
```

To delete the `index.html` asset, along with the fallback configuration for the `/` scope, the alias `/` and the alternative encodings:

```rust
asset_router
    .delete_assets(
        vec![
            Asset::new(
                "index.html",
                b"<html><body><h1>Hello World!</h1></body></html>".as_slice(),
            ),
            Asset::new("index.html.gz", &[0, 1, 2, 3, 4, 5]),
            Asset::new("index.html.br", &[6, 7, 8, 9, 10, 11]),
        ],
        vec![AssetConfig::File {
            path: "index.html".to_string(),
            content_type: Some("text/html".to_string()),
            headers: vec![(
                "cache-control".to_string(),
                "public, no-cache, no-store".to_string(),
            )],
            fallback_for: vec![AssetFallbackConfig {
                scope: "/".to_string(),
                status_code: Some(StatusCode::OK),
            }],
            aliased_by: vec!["/".to_string()],
            encodings: vec![
                AssetEncoding::Brotli.default_config(),
                AssetEncoding::Gzip.default_config(),
            ],
        }],
    )
    .unwrap();
```

To delete the `app.js` asset, along with the alternative encodings:

```rust
asset_router
    .delete_assets(
        vec![
            Asset::new("app.js", b"console.log('Hello World!');".as_slice()),
            Asset::new("app.js.gz", &[12, 13, 14, 15, 16, 17]),
            Asset::new("app.js.br", &[18, 19, 20, 21, 22, 23]),
        ],
        vec![AssetConfig::Pattern {
            pattern: "**/*.js".to_string(),
            content_type: Some("text/javascript".to_string()),
            headers: vec![(
                "cache-control".to_string(),
                "public, max-age=31536000, immutable".to_string(),
            )],
            encodings: vec![
                AssetEncoding::Brotli.default_config(),
                AssetEncoding::Gzip.default_config(),
            ],
        }],
    )
    .unwrap();
```

To delete the `css/app-ba74b708.css` asset, along with the alternative encodings:

```rust
asset_router.delete_assets(
    vec![
        Asset::new(
            "css/app-ba74b708.css",
            b"html,body{min-height:100vh;}".as_slice(),
        ),
        Asset::new(
            "css/app-ba74b708.css.gz",
            &[24, 25, 26, 27, 28, 29],
        ),
        Asset::new(
            "css/app-ba74b708.css.br",
            &[30, 31, 32, 33, 34, 35],
        ),
    ],
    vec![
        AssetConfig::Pattern {
            pattern: "**/*.css".to_string(),
            content_type: Some("text/css".to_string()),
            headers: vec![(
                "cache-control".to_string(),
                "public, max-age=31536000, immutable".to_string(),
            )],
            encodings: vec![
                AssetEncoding::Brotli.default_config(),
                AssetEncoding::Gzip.default_config(),
            ],
        },
    ]
).unwrap();
```

And finally, to delete the `/old` redirect:

```rust
asset_router
    .delete_assets(
        vec![],
        vec![AssetConfig::Redirect {
            from: "/old".to_string(),
            to: "/new".to_string(),
            kind: AssetRedirectKind::Permanent,
            headers: vec![(
                "content-type".to_string(),
                "text/plain; charset=utf-8".to_string(),
            )],
        }],
    )
    .unwrap();
```

After deleting any assets, make sure to set the canister's
certified data again:

```rust
use ic_cdk::api::set_certified_data;

set_certified_data(&asset_router.root_hash());
```

### Deleting assets by path

To delete assets by path, use the
[delete_assets_by_path](AssetRouter::delete_assets_by_path) function.

Depending on the configuration provided to the [certify_assets](AssetRouter::certify_assets) function,
multiple responses may be generated for the same asset. These assets may exist on different paths,
for example if the `alias` configuration is used. If `alias` paths are not passed to this function,
they will not be deleted.

If multiple encodings exist for a path, all encodings will be deleted.

Fallbacks are also not deleted, to delete them, use the
[delete_fallback_assets_by_path](AssetRouter::delete_fallback_assets_by_path) function.

Assuming the same base example used above to demonstrate certifying assets:

```rust
use ic_http_certification::StatusCode;
use ic_asset_certification::{Asset, AssetConfig, AssetFallbackConfig, AssetRouter, AssetRedirectKind, AssetEncoding};

let mut asset_router = AssetRouter::default();

let assets = vec![
    Asset::new(
        "index.html",
        b"<html><body><h1>Hello World!</h1></body></html>".as_slice(),
    ),
    Asset::new(
        "index.html.gz",
        &[0, 1, 2, 3, 4, 5]
    ),
    Asset::new(
        "index.html.br",
        &[6, 7, 8, 9, 10, 11]
    ),
    Asset::new(
        "app.js",
        b"console.log('Hello World!');".as_slice(),
    ),
    Asset::new(
        "app.js.gz",
        &[12, 13, 14, 15, 16, 17],
    ),
    Asset::new(
        "app.js.br",
        &[18, 19, 20, 21, 22, 23],
    ),
    Asset::new(
        "css/app-ba74b708.css",
        b"html,body{min-height:100vh;}".as_slice(),
    ),
    Asset::new(
        "css/app-ba74b708.css.gz",
        &[24, 25, 26, 27, 28, 29],
    ),
    Asset::new(
        "css/app-ba74b708.css.br",
        &[30, 31, 32, 33, 34, 35],
    ),
];

let asset_configs = vec![
    AssetConfig::File {
        path: "index.html".to_string(),
        content_type: Some("text/html".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, no-cache, no-store".to_string(),
        )],
        fallback_for: vec![AssetFallbackConfig {
            scope: "/".to_string(),
            status_code: Some(StatusCode::OK),
        }],
        aliased_by: vec!["/".to_string()],
        encodings: vec![
            AssetEncoding::Brotli.default_config(),
            AssetEncoding::Gzip.default_config(),
        ],
    },
    AssetConfig::Pattern {
        pattern: "**/*.js".to_string(),
        content_type: Some("text/javascript".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, max-age=31536000, immutable".to_string(),
        )],
        encodings: vec![
            AssetEncoding::Brotli.default_config(),
            AssetEncoding::Gzip.default_config(),
        ],
    },
    AssetConfig::Pattern {
        pattern: "**/*.css".to_string(),
        content_type: Some("text/css".to_string()),
        headers: vec![(
            "cache-control".to_string(),
            "public, max-age=31536000, immutable".to_string(),
        )],
        encodings: vec![
            AssetEncoding::Brotli.default_config(),
            AssetEncoding::Gzip.default_config(),
        ],
    },
    AssetConfig::Redirect {
        from: "/old".to_string(),
        to: "/new".to_string(),
        kind: AssetRedirectKind::Permanent,
        headers: vec![("content-type".to_string(), "text/plain".to_string())],
    },
];

asset_router.certify_assets(assets, asset_configs).unwrap();
```

To delete the `index.html` asset, along with the fallback configuration for the `/` scope, the alias `/` and the alternative encodings:

```rust
asset_router
    .delete_assets_by_path(
        vec![
            "/index.html", // deletes the index.html asset, along with all encodings
            "/" // deletes the `/` alias for index.html, along with all encodings
        ],
    )
    .unwrap();

asset_router
    .delete_fallback_assets_by_path(
       vec![
          "/" // deletes the fallback configuration for the `/` scope, along with all encodings
      ]
   )
  .unwrap();
```

To delete the `app.js`asset, along with the alternative encodings:

```rust
asset_router.delete_assets(vec!["/app.js"]).unwrap();
```

To delete the `css/app-ba74b708.css` asset, along with the alternative encodings:

```rust
asset_router.delete_assets(vec!["/css/app-ba74b708.css"]).unwrap();
```

And finally, to delete the `/old` redirect:

```rust
asset_router.delete_assets_by_path(vec!["/old"]).unwrap();
```

After deleting any assets, make sure to set the canister's
certified data again:

```rust
use ic_cdk::api::set_certified_data;

set_certified_data(&asset_router.root_hash());
```

### Deleting all assets

It's also possible to delete all assets and their certification in one go:

```rust
asset_router.delete_all_assets();
```

After deleting any assets, make sure to set the canister's
certified data again:

```rust
use ic_cdk::api::set_certified_data;

set_certified_data(&asset_router.root_hash());
```

## Querying assets

The `AssetRouter` has two functions to retrieve an `AssetMap` containing assets.

The `get_assets()` function returns all standard assets, while the `get_fallback_assets()` function returns all fallback assets.

The `AssetMap` can be used to query assets by `path`, `encoding`, and `starting_range`.

For standard assets, the path refers to the asset's path, e.g. `/index.html`.

For fallback assets, the path refers to the scope that the fallback is valid for, e.g. `/`. See the `fallback_for` config option for more information on fallback scopes.

For all types of assets, the encoding refers to the encoding of the asset, see `AssetEncoding`.

Assets greater than 2mb are split into multiple ranges, the starting range allows retrieval of
individual chunks of these large assets. The first range is `Some(0)`, the second range is
`Some(ASSET_CHUNK_SIZE)`, the third range is `Some(ASSET_CHUNK_SIZE * 2)`, and so on. The entire asset can
also be retrieved by passing `None` as the `starting_range`. Note that `ASSET_CHUNK_SIZE` is a constant defined in the `ic_asset_certification` crate.