#http2 #cloudflare #jpeg #optimization #http-header

bin+lib cloudflare-soos

Helper tool for Cloudflare's enhanced HTTP/2 and HTTP/3 prioritization, which makes progressive images load faster. Supports JPEG, GIF, and PNG.

6 stable releases

2.3.1 Nov 14, 2023
2.3.0 Sep 8, 2023
2.2.0 Oct 12, 2022
2.1.0 Oct 7, 2021
1.18.0 Mar 20, 2020

#244 in Images

Download history 80/week @ 2023-12-15 8/week @ 2023-12-22 30/week @ 2023-12-29 9/week @ 2024-01-05 49/week @ 2024-01-12 72/week @ 2024-01-19 107/week @ 2024-01-26 52/week @ 2024-02-02 98/week @ 2024-02-09 75/week @ 2024-02-16 75/week @ 2024-02-23 74/week @ 2024-03-01 85/week @ 2024-03-08 62/week @ 2024-03-15 22/week @ 2024-03-22 54/week @ 2024-03-29

241 downloads per month
Used in mozjpeg-sys

MIT/Apache

80KB
2K SLoC

Scan Offsets of Scans

Progressive image parser for HTTP/2 parallel progressive streaming feature and HTTP/3 prioritization.

Computes optimal offsets for the cf-priority-change and Priority headers to render images progressively with a "preview" and "good enough" stages. In JPEG it tries to render DC channels only (libjpeg-turbo has a special support for nicely blurring the DC-only stage) and then prioritize sending enough data for the image look fully loaded.

In case of GIF animations it prioritizes sending the first frame only. In interlaced PNG it roughly guesses progressive scans. For non-progressive images it tries to send image dimensions before any other data to aid page layout and (improves Cumulative Layout Shift (CLS) metric).

Building and usage

Install Rust 1.60 or later, and run:

cargo install cloudflare-soos
soos-cf-priority image.jpg [--rfc9218]

This will print to stdout Cloudflare's cf-priority (first line) and cf-priority-change (second line) headers with breakpoints for the image.jpg file, an error to stderr. When an --rfc9218 argument provider, it will print out HTTP3 priority header instead, e.g. u=1;i=?0,change=(1000;u=3;i=?0 10000;u=5;i 100000;u=6;i). The image is read from stdin if not path is specified. This tool requires progressive JPEG files, and doesn't do anything for baseline JPEG or other formats.

The same can be done programmatically by using this crate as a library and calling soos::Scans::from_file(jpeg_bytes)?.cf_priority_change_headers()? or soos::Scans::from_file(jpeg_bytes)?.rfc9218_priority_change_headers()? for rfc9218 (h3) priority header.

You can also build for WASM with rustup target add wasm32-unknown-unknown; cargo build --release --target=wasm32-unknown-unknown or use wasm-pack.

Example tools

cargo run --example cf-priority -- image.jpg
cargo run --example rfc9218-priority -- image.jpg
cargo run --example scan -- image.jpg

These tools show debug information used to generate the HTTP priority headers.

Credits

JPEG parsing is based on jpeg-decoder by Ulf Nilsson. GIF parsing is from Piston GIF crate.

Dependencies

~0–485KB