gadk-platform 0.4.7

Time, UUID, task and environment seams and shared helpers for gadk
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
// Copyright 2026 Chris Olson
// Portions derived from ADK-Go (https://github.com/google/adk-go), Copyright 2026 Google LLC.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! A built-in MIME table (extension to type) and content sniffing ([`detect_content_type`]),
//! shared by the launcher's web UI and the agentanalytics GCS offloader.
//!
//! A file is typed by [`type_by_extension`] on its extension and, when that is empty, by
//! sniffing its first 512 bytes. Only the built-in table is used, not the system's MIME
//! database (`/etc/mime.types` and the like), so every machine answers alike; for the bundle's
//! extensions (`.css`, `.html`, `.js`, `.json`, `.svg`) both give the same types.

/// Extension to MIME type, sorted by extension.
const BUILTIN_TYPES: &[(&str, &str)] = &[
    (".ai", "application/postscript"),
    (".apk", "application/vnd.android.package-archive"),
    (".apng", "image/apng"),
    (".avif", "image/avif"),
    (".bin", "application/octet-stream"),
    (".bmp", "image/bmp"),
    (".com", "application/octet-stream"),
    (".css", "text/css; charset=utf-8"),
    (".csv", "text/csv; charset=utf-8"),
    (".doc", "application/msword"),
    (
        ".docx",
        "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
    ),
    (".ehtml", "text/html; charset=utf-8"),
    (".eml", "message/rfc822"),
    (".eps", "application/postscript"),
    (".exe", "application/octet-stream"),
    (".flac", "audio/flac"),
    (".gif", "image/gif"),
    (".gz", "application/gzip"),
    (".htm", "text/html; charset=utf-8"),
    (".html", "text/html; charset=utf-8"),
    (".ico", "image/vnd.microsoft.icon"),
    (".ics", "text/calendar; charset=utf-8"),
    (".jfif", "image/jpeg"),
    (".jpeg", "image/jpeg"),
    (".jpg", "image/jpeg"),
    (".js", "text/javascript; charset=utf-8"),
    (".json", "application/json"),
    (".m4a", "audio/mp4"),
    (".mjs", "text/javascript; charset=utf-8"),
    (".mp3", "audio/mpeg"),
    (".mp4", "video/mp4"),
    (".oga", "audio/ogg"),
    (".ogg", "audio/ogg"),
    (".ogv", "video/ogg"),
    (".opus", "audio/ogg"),
    (".pdf", "application/pdf"),
    (".pjp", "image/jpeg"),
    (".pjpeg", "image/jpeg"),
    (".png", "image/png"),
    (".ppt", "application/vnd.ms-powerpoint"),
    (
        ".pptx",
        "application/vnd.openxmlformats-officedocument.presentationml.presentation",
    ),
    (".ps", "application/postscript"),
    (".rdf", "application/rdf+xml"),
    (".rtf", "application/rtf"),
    (".shtml", "text/html; charset=utf-8"),
    (".svg", "image/svg+xml"),
    (".text", "text/plain; charset=utf-8"),
    (".tif", "image/tiff"),
    (".tiff", "image/tiff"),
    (".txt", "text/plain; charset=utf-8"),
    (".vtt", "text/vtt; charset=utf-8"),
    (".wasm", "application/wasm"),
    (".wav", "audio/wav"),
    (".weba", "audio/webm"),
    (".webm", "video/webm"),
    (".webp", "image/webp"),
    (".xbl", "text/xml; charset=utf-8"),
    (".xbm", "image/x-xbitmap"),
    (".xht", "application/xhtml+xml"),
    (".xhtml", "application/xhtml+xml"),
    (".xls", "application/vnd.ms-excel"),
    (
        ".xlsx",
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    ),
    (".xml", "text/xml; charset=utf-8"),
    (".xsl", "text/xml; charset=utf-8"),
    (".zip", "application/zip"),
];

/// The MIME type for `ext` over the built-in table: `ext` (with its leading dot) looked up as
/// given, then lower-cased; `""` when unknown.
pub fn type_by_extension(ext: &str) -> &'static str {
    let find = |e: &str| {
        BUILTIN_TYPES
            .binary_search_by(|(k, _)| (*k).cmp(e))
            .map_or("", |i| BUILTIN_TYPES[i].1)
    };
    match find(ext) {
        "" => find(&ext.to_lowercase()),
        found => found,
    }
}

/// The sniffer reads at most this many bytes.
pub const SNIFF_LEN: usize = 512;

/// Detects a content type with the WHATWG MIME sniffing algorithm over the first [`SNIFF_LEN`]
/// bytes, `application/octet-stream` when nothing matches.
pub fn detect_content_type(data: &[u8]) -> &'static str {
    let data = &data[..data.len().min(SNIFF_LEN)];
    let first_non_ws = data.iter().position(|&b| !is_ws(b)).unwrap_or(data.len());
    SNIFF_SIGNATURES
        .iter()
        .find_map(|sig| sig.matches(data, first_non_ws))
        .unwrap_or("application/octet-stream")
}

/// True for the whitespace bytes of the MIME sniffing spec.
fn is_ws(b: u8) -> bool {
    matches!(b, b'\t' | b'\n' | b'\x0c' | b'\r' | b' ')
}

/// True for a tag-terminating byte.
fn is_tt(b: u8) -> bool {
    matches!(b, b' ' | b'>')
}

/// The kinds of sniffing signature.
enum Sig {
    /// An HTML tag name, matched case-insensitively after leading whitespace and followed by a
    /// tag-terminating byte.
    Html(&'static [u8]),
    /// A byte pattern under a mask, optionally after leading whitespace.
    Masked {
        mask: &'static [u8],
        pat: &'static [u8],
        skip_ws: bool,
        ct: &'static str,
    },
    /// A fixed byte prefix.
    Exact(&'static [u8], &'static str),
    /// An MP4 file-type box.
    Mp4,
    /// Text: no binary data byte after leading whitespace.
    Text,
}

const fn masked(mask: &'static [u8], pat: &'static [u8], ct: &'static str) -> Sig {
    Sig::Masked {
        mask,
        pat,
        skip_ws: false,
        ct,
    }
}

/// The signatures, in the order they are tried.
const SNIFF_SIGNATURES: &[Sig] = &[
    Sig::Html(b"<!DOCTYPE HTML"),
    Sig::Html(b"<HTML"),
    Sig::Html(b"<HEAD"),
    Sig::Html(b"<SCRIPT"),
    Sig::Html(b"<IFRAME"),
    Sig::Html(b"<H1"),
    Sig::Html(b"<DIV"),
    Sig::Html(b"<FONT"),
    Sig::Html(b"<TABLE"),
    Sig::Html(b"<A"),
    Sig::Html(b"<STYLE"),
    Sig::Html(b"<TITLE"),
    Sig::Html(b"<B"),
    Sig::Html(b"<BODY"),
    Sig::Html(b"<BR"),
    Sig::Html(b"<P"),
    Sig::Html(b"<!--"),
    Sig::Masked {
        mask: b"\xFF\xFF\xFF\xFF\xFF",
        pat: b"<?xml",
        skip_ws: true,
        ct: "text/xml; charset=utf-8",
    },
    Sig::Exact(b"%PDF-", "application/pdf"),
    Sig::Exact(b"%!PS-Adobe-", "application/postscript"),
    // UTF BOMs.
    masked(
        b"\xFF\xFF\x00\x00",
        b"\xFE\xFF\x00\x00",
        "text/plain; charset=utf-16be",
    ),
    masked(
        b"\xFF\xFF\x00\x00",
        b"\xFF\xFE\x00\x00",
        "text/plain; charset=utf-16le",
    ),
    masked(
        b"\xFF\xFF\xFF\x00",
        b"\xEF\xBB\xBF\x00",
        "text/plain; charset=utf-8",
    ),
    // Image types.
    Sig::Exact(b"\x00\x00\x01\x00", "image/x-icon"),
    Sig::Exact(b"\x00\x00\x02\x00", "image/x-icon"),
    Sig::Exact(b"BM", "image/bmp"),
    Sig::Exact(b"GIF87a", "image/gif"),
    Sig::Exact(b"GIF89a", "image/gif"),
    masked(
        b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF\xFF\xFF",
        b"RIFF\x00\x00\x00\x00WEBPVP",
        "image/webp",
    ),
    Sig::Exact(b"\x89PNG\x0D\x0A\x1A\x0A", "image/png"),
    Sig::Exact(b"\xFF\xD8\xFF", "image/jpeg"),
    // Audio and video types.
    masked(
        b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF",
        b"FORM\x00\x00\x00\x00AIFF",
        "audio/aiff",
    ),
    masked(b"\xFF\xFF\xFF", b"ID3", "audio/mpeg"),
    masked(b"\xFF\xFF\xFF\xFF\xFF", b"OggS\x00", "application/ogg"),
    masked(
        b"\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
        b"MThd\x00\x00\x00\x06",
        "audio/midi",
    ),
    masked(
        b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF",
        b"RIFF\x00\x00\x00\x00AVI ",
        "video/avi",
    ),
    masked(
        b"\xFF\xFF\xFF\xFF\x00\x00\x00\x00\xFF\xFF\xFF\xFF",
        b"RIFF\x00\x00\x00\x00WAVE",
        "audio/wave",
    ),
    Sig::Mp4,
    Sig::Exact(b"\x1A\x45\xDF\xA3", "video/webm"),
    // Font types: 34 NUL bytes (masked out) followed by "LP".
    masked(
        b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xFF\xFF",
        b"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00LP",
        "application/vnd.ms-fontobject",
    ),
    Sig::Exact(b"\x00\x01\x00\x00", "font/ttf"),
    Sig::Exact(b"OTTO", "font/otf"),
    Sig::Exact(b"ttcf", "font/collection"),
    Sig::Exact(b"wOFF", "font/woff"),
    Sig::Exact(b"wOF2", "font/woff2"),
    // Archive types.
    Sig::Exact(b"\x1F\x8B\x08", "application/x-gzip"),
    Sig::Exact(b"PK\x03\x04", "application/zip"),
    Sig::Exact(b"Rar!\x1A\x07\x00", "application/x-rar-compressed"),
    Sig::Exact(b"Rar!\x1A\x07\x01\x00", "application/x-rar-compressed"),
    Sig::Exact(b"\x00\x61\x73\x6D", "application/wasm"),
    // Should be last.
    Sig::Text,
];

impl Sig {
    /// The content type, or `None` when the signature does not match.
    fn matches(&self, data: &[u8], first_non_ws: usize) -> Option<&'static str> {
        match self {
            Sig::Exact(sig, ct) => data.starts_with(sig).then_some(*ct),
            Sig::Masked {
                mask,
                pat,
                skip_ws,
                ct,
            } => {
                let data = if *skip_ws {
                    &data[first_non_ws..]
                } else {
                    data
                };
                if pat.len() != mask.len() || data.len() < pat.len() {
                    return None;
                }
                pat.iter()
                    .zip(mask.iter())
                    .zip(data)
                    .all(|((p, m), d)| d & m == *p)
                    .then_some(*ct)
            }
            Sig::Html(h) => {
                let data = &data[first_non_ws..];
                if data.len() < h.len() + 1 {
                    return None;
                }
                for (&b, &d) in h.iter().zip(data) {
                    let d = if b.is_ascii_uppercase() { d & 0xDF } else { d };
                    if b != d {
                        return None;
                    }
                }
                // Next byte must be a tag-terminating byte.
                is_tt(data[h.len()]).then_some("text/html; charset=utf-8")
            }
            Sig::Mp4 => mp4(data),
            Sig::Text => data[first_non_ws..]
                .iter()
                .all(|&b| !matches!(b, 0x00..=0x08 | 0x0B | 0x0E..=0x1A | 0x1C..=0x1F))
                .then_some("text/plain; charset=utf-8"),
        }
    }
}

/// Matches an MP4 file-type box (mimesniff § 6.2.1).
fn mp4(data: &[u8]) -> Option<&'static str> {
    if data.len() < 12 {
        return None;
    }
    let box_size =
        usize::try_from(u32::from_be_bytes([data[0], data[1], data[2], data[3]])).ok()?;
    if data.len() < box_size || box_size % 4 != 0 {
        return None;
    }
    if &data[4..8] != b"ftyp" {
        return None;
    }
    let mut st = 8;
    while st < box_size {
        // Bytes 12..16 are the major brand's version number.
        if st != 12 && &data[st..st + 3] == b"mp4" {
            return Some("video/mp4");
        }
        st += 4;
    }
    None
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn builtin_table_is_sorted_and_go_lookups_hold() {
        assert!(BUILTIN_TYPES.windows(2).all(|w| w[0].0 < w[1].0));
        assert_eq!(BUILTIN_TYPES.len(), 65);
        assert_eq!(type_by_extension(".js"), "text/javascript; charset=utf-8");
        assert_eq!(type_by_extension(".css"), "text/css; charset=utf-8");
        assert_eq!(type_by_extension(".html"), "text/html; charset=utf-8");
        assert_eq!(type_by_extension(".svg"), "image/svg+xml");
        assert_eq!(type_by_extension(".json"), "application/json");
        // Case-insensitive fallback (Go: case-sensitive first, then lower-cased).
        assert_eq!(type_by_extension(".PNG"), "image/png");
        assert_eq!(type_by_extension(".Html"), "text/html; charset=utf-8");
        // Unicode lower-casing: KELVIN SIGN lower-cases to `k`, as in Go's `strings.ToLower`.
        assert_eq!(
            type_by_extension(".ap\u{212A}"),
            "application/vnd.android.package-archive"
        );
        assert_eq!(type_by_extension(".map"), "");
        assert_eq!(type_by_extension(".woff2"), "");
        assert_eq!(type_by_extension(""), "");
        assert_eq!(type_by_extension("js"), "");
    }

    /// Go `net/http/sniff_test.go` `sniffTests`.
    #[test]
    fn detect_content_type_matches_go_sniff_tests() {
        let cases: &[(&str, &[u8], &str)] = &[
            ("Empty", b"", "text/plain; charset=utf-8"),
            ("Binary", &[1, 2, 3], "application/octet-stream"),
            (
                "HTML document #1",
                b"<HtMl><bOdY>blah blah blah</body></html>",
                "text/html; charset=utf-8",
            ),
            (
                "HTML document #2",
                b"<HTML></HTML>",
                "text/html; charset=utf-8",
            ),
            (
                "HTML document #3 (leading whitespace)",
                b"   <!DOCTYPE HTML>...",
                "text/html; charset=utf-8",
            ),
            (
                "HTML document #4 (leading CRLF)",
                b"\r\n<html>...",
                "text/html; charset=utf-8",
            ),
            (
                "Plain text",
                "This is not HTML. It has \u{2603} though.".as_bytes(),
                "text/plain; charset=utf-8",
            ),
            ("XML", b"\n<?xml!", "text/xml; charset=utf-8"),
            ("Windows icon", b"\x00\x00\x01\x00", "image/x-icon"),
            ("Windows cursor", b"\x00\x00\x02\x00", "image/x-icon"),
            ("BMP image", b"BM...", "image/bmp"),
            ("GIF 87a", b"GIF87a", "image/gif"),
            ("GIF 89a", b"GIF89a...", "image/gif"),
            ("WEBP image", b"RIFF\x00\x00\x00\x00WEBPVP", "image/webp"),
            ("PNG image", b"\x89PNG\x0D\x0A\x1A\x0A", "image/png"),
            ("JPEG image", b"\xFF\xD8\xFF", "image/jpeg"),
            ("MIDI audio", b"MThd\x00\x00\x00\x06\x00\x01", "audio/midi"),
            (
                "MP3 audio/MPEG audio",
                b"ID3\x03\x00\x00\x00\x00\x0f",
                "audio/mpeg",
            ),
            (
                "WAV audio #1",
                b"RIFFb\xb8\x00\x00WAVEfmt \x12\x00\x00\x00\x06",
                "audio/wave",
            ),
            (
                "WAV audio #2",
                b"RIFF,\x00\x00\x00WAVEfmt \x12\x00\x00\x00\x06",
                "audio/wave",
            ),
            (
                "AIFF audio #1",
                b"FORM\x00\x00\x00\x00AIFFCOMM\x00\x00\x00\x12\x00\x01\x00\x00\x57\x55\x00\x10\x40\x0d\xf3\x34",
                "audio/aiff",
            ),
            (
                "OGG audio",
                b"OggS\x00\x02\x00\x00\x00\x00\x00\x00\x00\x00\x7e\x46\x00\x00\x00\x00\x00\x00\x1f\xf6\xb4\xfc\x01\x1e\x01\x76\x6f\x72",
                "application/ogg",
            ),
            ("Must not match OGG", b"owow\x00", "application/octet-stream"),
            ("Must not match OGG", b"oooS\x00", "application/octet-stream"),
            ("Must not match OGG", b"oggS\x00", "application/octet-stream"),
            (
                "MP4 video",
                b"\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp42isom<\x06t\xbfmdat",
                "video/mp4",
            ),
            (
                "AVI video #1",
                b"RIFF,O\n\x00AVI LIST\xc3\x80",
                "video/avi",
            ),
            (
                "AVI video #2",
                b"RIFF,\n\x00\x00AVI LIST\xc3\x80",
                "video/avi",
            ),
            (
                "TTF sample  I",
                b"\x00\x01\x00\x00\x00\x17\x01\x00\x00\x04\x01\x60\x4f",
                "font/ttf",
            ),
            (
                "TTF sample II",
                b"\x00\x01\x00\x00\x00\x0e\x00\x80\x00\x03\x00\x60\x46",
                "font/ttf",
            ),
            (
                "OTTO sample  I",
                b"\x4f\x54\x54\x4f\x00\x0e\x00\x80\x00\x03\x00\x60\x42\x41\x53\x45",
                "font/otf",
            ),
            (
                "woff sample  I",
                b"\x77\x4f\x46\x46\x00\x01\x00\x00\x00\x00\x30\x54\x00\x0d\x00\x00",
                "font/woff",
            ),
            (
                "woff2 sample",
                b"\x77\x4f\x46\x32\x00\x01\x00\x00\x00",
                "font/woff2",
            ),
            ("wasm sample", b"\x00\x61\x73\x6d\x01\x00", "application/wasm"),
            (
                "RAR v1.5-v4.0",
                b"Rar!\x1A\x07\x00",
                "application/x-rar-compressed",
            ),
            (
                "RAR v5+",
                b"Rar!\x1A\x07\x01\x00",
                "application/x-rar-compressed",
            ),
            (
                "Incorrect RAR v1.5-v4.0",
                b"Rar \x1A\x07\x00",
                "application/octet-stream",
            ),
            (
                "Incorrect RAR v5+",
                b"Rar \x1A\x07\x01\x00",
                "application/octet-stream",
            ),
        ];
        for (desc, data, want) in cases {
            assert_eq!(detect_content_type(data), *want, "{desc}");
        }
    }

    #[test]
    fn detect_content_type_reads_at_most_512_bytes() {
        // A control byte past the sniff window does not make the text binary.
        let mut data = vec![b'a'; SNIFF_LEN];
        data.push(0x01);
        assert_eq!(detect_content_type(&data), "text/plain; charset=utf-8");
        data[SNIFF_LEN - 1] = 0x01;
        assert_eq!(detect_content_type(&data), "application/octet-stream");
        // An HTML signature needs its tag-terminating byte.
        assert_eq!(detect_content_type(b"<html"), "text/plain; charset=utf-8");
        assert_eq!(detect_content_type(b"  <!-- x"), "text/html; charset=utf-8");
    }
}