cloudflare-soos 2.3.1

Helper tool for Cloudflare's enhanced HTTP/2 and HTTP/3 prioritization, which makes progressive images load faster. Supports JPEG, GIF, and PNG.
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
//! Scans a progressive JPEG file to find which byte ranges of the file are critical for displaying it at key stages.
//! This knowledge can be used to serve JPEGs optimally over HTTP/2 connections. This library can generate cf-priority-change headers
//! compatible with [prioritization syntax used by Cloudflare](https://blog.cloudflare.com/parallel-streaming-of-progressive-images/).

use std::fmt::Display;

mod error;
pub use crate::error::*;

/// For advanced usage, low-level access to the basic JPEG structure
pub mod jpeg;

#[cfg(feature = "gif")]
mod gif;
mod png;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

/// Key positions in a progressive image file
///
/// ```rust,no_run
/// # let input_file = vec![];
/// cloudflare_soos::Scans::from_file(&input_file)?.cf_priority_change_headers()?;
/// # Ok::<_, cloudflare_soos::Error>(())
/// ```
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Scans {
    /// Byte position where metadata ends.
    /// This many bytes are preceeding start of pixel data.
    /// It's usually <200 bytes, unless the image has color profiles or other bloat.
    pub metadata_end: Option<usize>,
    /// All metadata + minimum amount of data to make browsers render _anything_
    /// (in case they don't reserve space based on metadata)
    pub frame_render_start: Option<usize>,
    /// Byte position where the first (lowest-quality) progressive scan ends.
    /// This many bytes are needed to display anything on screen.
    /// It's usually 12-15% of the file size.
    pub first_scan_end: Option<usize>,
    /// Byte position where most of ok-quality pixel data ends.
    /// This many bytes are needed to display a good-enough image.
    /// It's usually about 50% of the file size.
    pub good_scan_end: Option<usize>,
    /// Size of the whole image file, in bytes. The size is only used for heuristics,
    /// so the value may be approximate, but it must be greater than all other
    /// positions set in this struct.
    pub file_size: usize,
}

#[cfg(target_arch = "wasm32")]
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn cf_priority_change_header_wasm(image: &[u8]) -> Option<(String, String)> {
    Scans::from_file(image).and_then(|v| v.cf_priority_change_headers()).ok()
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn rfc9218_priority_change_headers_wasm(image: &[u8]) -> Option<String> {
    Scans::from_file(image).and_then(|v| v.rfc9218_priority_change_headers()).ok()
}

// minimum chunk size for h2 and rfc9218 (h3) prioritization changes
// there's no point changing priority for 20 bytes, H/2 frame takes half of that
const MIN_H2_CHUNK_SIZE: usize = 20;
// there's no point changing priority for 32 bytes, H/3 frame's headers takes 16 bytes
const MIN_H3_CHUNK_SIZE: usize = 32;

#[derive(Debug)]
enum Concurrency {
    ExclusiveSequential, // 0
    SharedSequential,    // 1
    Shared,              // n
}

impl Default for Concurrency {
    fn default() -> Self {
        Self::ExclusiveSequential
    }
}

impl Display for Concurrency {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let value = match self {
            Concurrency::ExclusiveSequential => "0",
            Concurrency::SharedSequential => "1",
            Concurrency::Shared => "n",
        };
        write!(f, "{value}")
    }
}

#[derive(Default)]
struct PriorityChanges {
    next_offset: usize,
    changes: Vec<PriorityChange>,
}

struct PriorityChange {
    offset: usize,
    http2_priority: u8,
    concurrency: Concurrency,
}

impl PriorityChange {
    fn get_http2_priority_chunk(&self) -> String {
        format!("{}:{}", self.offset, self.get_http2_priority())
    }

    #[inline(always)]
    fn get_http2_priority(&self) -> String {
        format!("{}/{}", self.http2_priority, self.concurrency)
    }

    fn get_http3_priority_chunk(&self) -> String {
        format!("{};{}", self.offset, self.get_http3_priority())
    }

    #[inline(always)]
    fn get_http3_priority(&self) -> String {
        // the lower urgency level the more priority it has over other frames
        let urgency = match self.http2_priority {
            0..=9 => 6,
            10..=19 => 5,
            20..=29 => 4,
            30..=39 => 3,
            40..=49 => 2,
            50..=59 => 1,
            60..=63 => 0,
            _ => 7, // not supported
        };

        let (urgency, incremental) = match self.concurrency {
            Concurrency::ExclusiveSequential => (urgency, false),
            // Shared concurrency reduces priority (urgency is a reversed measure)
            Concurrency::SharedSequential => (urgency + 1, false),
            Concurrency::Shared => (urgency + 1, true),
        };

        if incremental {
            format!("u={urgency};i")
        } else {
            format!("u={urgency};i=?0")
        }
    }
}

impl PriorityChanges {
    fn add(&mut self, offset: usize, http2_priority: u8, concurrency: Concurrency) {
        self.changes.push(PriorityChange {
            offset: self.next_offset,
            http2_priority,
            concurrency,
        });

        self.next_offset = offset;
    }

    fn has_changes(&self) -> bool {
        self.changes.len() > 1
    }

    fn get_http2_priority(&self) -> Result<String> {
        self.changes
            .first()
            .map(PriorityChange::get_http2_priority)
            .ok_or(Error::Format("no priority header"))
    }

    fn get_http2_priority_changes(&self) -> Result<String> {
        let mut min_pos = 0;
        let change = self
            .changes
            .iter()
            // return changes w/o first priority
            .skip(1)
            .filter(|&p| {
                // filter out small chunks
                let keep = p.offset > min_pos;
                min_pos = p.offset + MIN_H2_CHUNK_SIZE;
                keep
            })
            .map(PriorityChange::get_http2_priority_chunk)
            .collect::<Vec<String>>()
            .join(",");

        if change.is_empty() {
            return Err(Error::Format("Can't find useful scans"));
        }

        Ok(change)
    }

    fn get_http3_priority(&self) -> Result<String> {
        self.changes
            .first()
            .map(PriorityChange::get_http3_priority)
            .ok_or(Error::Format("no priority header"))
    }

    fn get_rfc9218_priority_changes(&self) -> Result<String> {
        let mut min_pos = 0;
        let change = self
            .changes
            .iter()
            // returns changes w/o first priority
            .skip(1)
            .filter(|&p| {
                // filter out small chunks
                let keep = p.offset > min_pos;
                min_pos = p.offset + MIN_H3_CHUNK_SIZE;
                keep
            })
            .map(PriorityChange::get_http3_priority_chunk)
            .collect::<Vec<String>>()
            .join(" ");

        if change.is_empty() {
            return Err(Error::Format("Can't find useful scans"));
        }

        Ok(format!("cf-chb=({change})"))
    }
}

impl Scans {
    /// Analyze an image file to find byte ranges of its metadata and progressive scans
    pub fn from_file(input_file: &[u8]) -> Result<Self> {
        match input_file {
            [0xff, ..] => crate::jpeg::scans(input_file),
            #[cfg(feature = "gif")]
            [b'G', ..] => crate::gif::scans(input_file),
            [0x89, ..] => crate::png::scans(input_file),
            _ => Err(Error::Unsupported),
        }
    }

    /// Assumes the HTTP2 priorities are:
    ///
    ///  * 50 = critical js/css, fonts + image metadata
    ///  * 30 = regular js/css, followed by other images + JPEG DC
    ///  * 20 = low-priority image bodies
    ///  * 10 = idle
    ///
    /// Returns `cf-priority` and `cf-priority-change` header values, respectively.
    pub fn cf_priority_change_headers(&self) -> Result<(String, String)> {
        let priority_changes = self.get_priority_changes()?;
        Ok((
            priority_changes.get_http2_priority()?,
            priority_changes.get_http2_priority_changes()?,
        ))
    }

    /// <https://www.rfc-editor.org/rfc/rfc9218.html>
    pub fn rfc9218_priority_change_headers(&self) -> Result<String> {
        let priority_changes = self.get_priority_changes()?;
        Ok(format!("{},{}",
            priority_changes.get_http3_priority()?,
            priority_changes.get_rfc9218_priority_changes()?,
        ))
    }

    fn get_priority_changes(&self) -> Result<PriorityChanges> {
        let mut metadata_end = self.metadata_end.unwrap_or(0);
        let is_progressive = self
            .first_scan_end
            .map_or(false, |len| len < self.file_size / 2);

        // if there's a fat color profile or Adobe Garbage,
        // then sending "just" the metadata is not fast, and shouldn't be prioritized.
        let fat_metadata_limit = ((self.file_size / 8) + 180).min(2000);
        let fat_frame_start_limit = ((self.file_size / 8) + 500).min(8000);

        // if the metadata can cheaply include first frame/scan info, then send both at once (reduce H/2 framing overhead)
        let rendered_anything = self
            .frame_render_start
            .or(self.first_scan_end)
            .or(self.good_scan_end);
        if let Some(rendered_anything) = rendered_anything {
            if rendered_anything < fat_metadata_limit
                && rendered_anything < metadata_end + metadata_end / 8 + 100
            {
                metadata_end = rendered_anything;
            }
        }

        // let mut chunks = Vec::with_capacity(8);
        let mut priority_changes = PriorityChanges::default();

        // This is important, because it decides when the whole image starts sending
        if is_progressive && metadata_end < fat_metadata_limit {
            // "50/0" fast and worth accelerating
            priority_changes.add(metadata_end, 50, Concurrency::ExclusiveSequential);
        } else if self.file_size < 1200
            || is_progressive
            || rendered_anything.map_or(false, |n| n < 2000)
        {
            // "30/1" at least we can show something quick
            priority_changes.add(metadata_end, 30, Concurrency::SharedSequential);
        } else {
            // "21/n" lost cause: baseline image with bloated metadata
            priority_changes.add(metadata_end, 21, Concurrency::Shared);
        };

        // Browsers don't always reserve space based on metadata availability alone,
        // so here's trying again, with more data
        if let Some(frame_render_start) = self.frame_render_start {
            if frame_render_start > metadata_end
                && self
                    .first_scan_end
                    .map_or(true, |dc| frame_render_start < dc)
            {
                if frame_render_start < fat_frame_start_limit {
                    if frame_render_start < 1000 {
                        priority_changes.add(frame_render_start, 50, Concurrency::SharedSequential);
                    } else {
                        priority_changes.add(frame_render_start, 40, Concurrency::SharedSequential);
                    }
                } else {
                    priority_changes.add(frame_render_start, 30, Concurrency::Shared);
                }
            }
        }

        if let Some(first_scan_end) = self.first_scan_end {
            // small DC can be downloaded in one go, to save on re-rendering
            priority_changes.add(
                first_scan_end,
                30,
                if first_scan_end < 25000 {
                    Concurrency::ExclusiveSequential
                } else {
                    Concurrency::SharedSequential
                },
            );
        }

        if let Some(good_scan_end) = self.good_scan_end {
            priority_changes.add(
                good_scan_end,
                20,
                if good_scan_end < 100_000 {
                    Concurrency::SharedSequential
                } else {
                    Concurrency::Shared
                },
            );
        }

        let rendered_already = self.first_scan_end.is_some() || self.good_scan_end.is_some();
        let bytes_left = self
            .file_size
            .saturating_sub(self.good_scan_end.or(self.first_scan_end).unwrap_or(0));
        let is_big = bytes_left > 80_000;
        let is_tiny = bytes_left < 1_000;

        let (priority, concurrency) = if rendered_already {
            // if it's on screen, it's not urgent to send anything more
            (
                10,
                if is_big {
                    Concurrency::Shared
                } else {
                    Concurrency::SharedSequential
                },
            )
        } else if is_tiny {
            (30, Concurrency::SharedSequential)
        } else if is_big {
            (20, Concurrency::Shared)
        } else {
            (20, Concurrency::SharedSequential)
        };

        priority_changes.add(self.file_size, priority, concurrency);
        if !priority_changes.has_changes() {
            return Err(Error::Format("Can't find useful scans"));
        }
        Ok(priority_changes)
    }
}

#[cfg(test)]
fn s(a: &str, b: &str) -> (String, String) {
    (a.into(), b.into())
}

#[test]
fn test_baseline() {
    {
        let scans = Scans {
            metadata_end: None,
            frame_render_start: None,
            first_scan_end: None,
            good_scan_end: None,
            file_size: 100_000,
        };

        let res = scans.cf_priority_change_headers();
        assert!(res.is_err(), "expected error, h/2: {res:?}");

        let res = scans.rfc9218_priority_change_headers();
        assert!(res.is_err(), "expected error, h/3: {res:?}");
    }

    {
        // regular baseline image
        let scans = Scans {
            metadata_end: Some(101),
            frame_render_start: Some(181),
            first_scan_end: None,
            good_scan_end: None,
            file_size: 100_000,
        };

        assert_eq!(
            s("30/1", "181:20/n"),
            scans.cf_priority_change_headers().unwrap(),
            "regular baseline image, h/2"
        );

        assert_eq!(
            "u=4;i=?0,cf-chb=(181;u=5;i)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "regular baseline image, h/3"
        );
    }

    {
        // tiny image
        let scans = Scans {
            metadata_end: Some(101),
            frame_render_start: None,
            first_scan_end: None,
            good_scan_end: None,
            file_size: 1000,
        };

        assert_eq!(
            s("30/1", "101:20/1"),
            scans.cf_priority_change_headers().unwrap(),
            "tiny image, h/2"
        );
        assert_eq!(
            "u=4;i=?0,cf-chb=(101;u=5;i=?0)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "tiny image, h/3"
        );
    }

    {
        // fat metadata
        let scans = Scans {
            metadata_end: Some(9999),
            frame_render_start: None,
            first_scan_end: None,
            good_scan_end: None,
            file_size: 100_000,
        };

        assert_eq!(
            s("21/n", "9999:20/n"),
            scans.cf_priority_change_headers().unwrap(),
            "fat metadata, h/2"
        );

        assert_eq!(
            "u=5;i,cf-chb=(9999;u=5;i)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "fat metadata, h/3"
        );
    }

    {
        // relatively fat metadata
        let scans = Scans {
            metadata_end: Some(1000),
            frame_render_start: None,
            first_scan_end: None,
            good_scan_end: None,
            file_size: 3000,
        };
        assert_eq!(
            s("21/n", "1000:20/1"),
            scans.cf_priority_change_headers().unwrap(),
            "relatively fat metadata, h/2"
        );

        assert_eq!(
            "u=5;i,cf-chb=(1000;u=5;i=?0)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "relatively fat metadata, h/3"
        );
    }
}

#[test]
fn test_progressive() {
    {
        let scans = Scans {
            metadata_end: Some(1_000),
            frame_render_start: None,
            first_scan_end: Some(10_000),
            good_scan_end: Some(100_000),
            file_size: 200_000,
        };
        assert_eq!(
            s("50/0", "1000:30/0,10000:20/n,100000:10/n"),
            scans.cf_priority_change_headers().unwrap(),
            "scan, h/2"
        );

        assert_eq!(
            "u=1;i=?0,cf-chb=(1000;u=3;i=?0 10000;u=5;i 100000;u=6;i)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "scan, h/3"
        );
    }

    {
        // fat metadata
        let scans = Scans {
            metadata_end: Some(4_000),
            frame_render_start: None,
            first_scan_end: Some(10_000),
            good_scan_end: Some(100_000),
            file_size: 200_000,
        };

        assert_eq!(
            s("30/1", "4000:30/0,10000:20/n,100000:10/n"),
            scans.cf_priority_change_headers().unwrap(),
            "fat metadata, h/2"
        );

        assert_eq!(
            "u=4;i=?0,cf-chb=(4000;u=3;i=?0 10000;u=5;i 100000;u=6;i)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "fat metadata, h/3"
        );
    }

    {
        // fat DC
        let scans = Scans {
            metadata_end: Some(1_000),
            frame_render_start: None,
            first_scan_end: Some(50_000),
            good_scan_end: Some(100_000),
            file_size: 200_000,
        };

        assert_eq!(
            s("50/0", "1000:30/1,50000:20/n,100000:10/n"),
            scans.cf_priority_change_headers().unwrap(),
            "fat DC, h/2"
        );

        assert_eq!(
            "u=1;i=?0,cf-chb=(1000;u=4;i=?0 50000;u=5;i 100000;u=6;i)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "fat DC, h/3"
        );
    }

    {
        // small good scan
        let scans = Scans {
            metadata_end: Some(1_000),
            frame_render_start: None,
            first_scan_end: Some(10_000),
            good_scan_end: Some(11_000),
            file_size: 200_000,
        };

        assert_eq!(
            s("50/0", "1000:30/0,10000:20/1,11000:10/n"),
            scans.cf_priority_change_headers().unwrap(),
            "small good scan, h/2"
        );

        assert_eq!(
            "u=1;i=?0,cf-chb=(1000;u=3;i=?0 10000;u=5;i=?0 11000;u=6;i)".to_string(),
            scans.rfc9218_priority_change_headers().unwrap(),
            "small good scan, h/3"
        );
    }
}