Repository navigation
Expand file tree
/
Copy pathcompression_middleware.py
More file actions
360 lines (308 loc) · 13.1 KB
/
Copy pathcompression_middleware.py
File metadata and controls
360 lines (308 loc) · 13.1 KB
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
"""Smart HTTP compression middleware (Brotli + gzip with content-type exclusion).
Replaces starlette's GZipMiddleware, which had three measured problems on this
app:
1. **API JSON was gzip-only.** GZipMiddleware never serves Brotli even when the
client sends `Accept-Encoding: br`. For text/JSON, Brotli is ~20-30% smaller
than gzip. This middleware negotiates `br` first, then `gzip`.
2. **woff2 fonts were gzip-RECOMPRESSED, making them slightly bigger** (woff2 is
already a compressed container; gzip just adds framing overhead — measured
14976 B on disk → 14999 B over the wire). GZipMiddleware can't exclude by
content-type. This middleware skips an explicit set of already-compressed
content types (fonts/images/video/archives) so they are served `identity`.
3. **Double compression of precompressed static assets.** The `/static` handler
serves prebuilt `.br`/`.gz` siblings and sets `Content-Encoding`. Like
GZipMiddleware, this middleware skips any response that already carries a
`Content-Encoding` header, so those are not re-compressed.
It also leaves Range/206/416 responses untouched (so media range requests keep
working) and always advertises `Vary: Accept-Encoding`.
Pure-ASGI, no dependency on `brotli-asgi`; uses the already-installed `brotli`
package (with a graceful gzip-only fallback if brotli is somehow missing).
"""
from __future__ import annotations
import gzip
import io
from typing import Iterable
try: # `brotli` is installed in the shared venv; degrade to gzip-only if not.
import brotli # type: ignore
_HAVE_BROTLI = True
except Exception: # pragma: no cover - defensive
brotli = None # type: ignore
_HAVE_BROTLI = False
# Content types that are ALREADY compressed — compressing them again wastes CPU
# and usually makes them marginally larger (framing overhead). Matched by the
# bare media type (before any `; charset=...`). woff2/woff are the headline fix.
_INCOMPRESSIBLE_TYPES = frozenset(
{
# fonts (woff2/woff are compressed containers)
"font/woff2",
"font/woff",
"application/font-woff2",
"application/font-woff",
"application/x-font-woff",
# images
"image/png",
"image/jpeg",
"image/jpg",
"image/gif",
"image/webp",
"image/avif",
"image/heic",
"image/heif",
# video / streaming segments
"video/mp4",
"video/webm",
"video/iso.segment",
"video/mp2t",
"application/vnd.apple.mpegurl",
"audio/mpeg",
"audio/mp4",
"audio/aac",
# archives / already-compressed blobs
"application/zip",
"application/gzip",
"application/x-gzip",
"application/x-brotli",
"application/octet-stream",
}
)
# Extra guard by extension for the `application/octet-stream` / mislabelled
# cases (e.g. .m4s segments served as octet-stream). Not strictly required once
# content types are correct, but cheap insurance.
_INCOMPRESSIBLE_SUFFIXES = (
".woff2",
".woff",
".png",
".jpg",
".jpeg",
".gif",
".webp",
".avif",
".mp4",
".m4s",
".ts",
".zip",
".br",
".gz",
)
def _is_compressible_type(content_type: str) -> bool:
"""True if a response of this Content-Type is worth compressing."""
if not content_type:
# No content-type → don't guess; leave it alone.
return False
base = content_type.split(";", 1)[0].strip().lower()
if base in _INCOMPRESSIBLE_TYPES:
return False
# Allowlist the obviously-text families plus the structured-suffix types
# (+json, +xml, +svg). Everything else (the incompressible binary blobs
# above and unknown binaries) is left uncompressed.
if base.startswith("text/"):
return True
if base.startswith(("application/json", "application/javascript",
"application/xml", "application/manifest+json",
"application/ld+json", "application/wasm")):
# wasm is compressible (it's not pre-compressed)
return base != "application/octet-stream"
if base.endswith(("+json", "+xml")):
return True
if base in ("application/x-javascript", "application/rss+xml",
"application/atom+xml", "image/svg+xml"):
return True
return False
def _parse_accept_encoding(value: str) -> set[str]:
"""Return the set of accepted encodings (ignoring q-values for our needs;
a q=0 explicit rejection is honoured by dropping that token)."""
accepted: set[str] = set()
for part in value.split(","):
token = part.strip().lower()
if not token:
continue
name, _, params = token.partition(";")
name = name.strip()
# Honour `br;q=0` style rejections.
q = params.strip()
if q.startswith("q=") and q[2:].strip() in ("0", "0.0", "0.00", "0.000"):
continue
accepted.add(name)
return accepted
def _choose_encoding(accept_encoding: str) -> str | None:
"""Pick the best supported encoding: brotli > gzip > none."""
accepted = _parse_accept_encoding(accept_encoding)
if _HAVE_BROTLI and "br" in accepted:
return "br"
if "gzip" in accepted:
return "gzip"
return None
def _compress(encoding: str, body: bytes, *, quality: int, compresslevel: int) -> bytes:
if encoding == "br":
# Brotli text mode (1) tuned quality; good ratio at moderate CPU.
return brotli.compress(body, quality=quality, mode=brotli.MODE_TEXT) # type: ignore[union-attr]
buf = io.BytesIO()
with gzip.GzipFile(mode="wb", fileobj=buf, compresslevel=compresslevel,
mtime=0) as gz:
gz.write(body)
return buf.getvalue()
class SmartCompressionMiddleware:
"""ASGI middleware: negotiate Brotli/gzip for compressible text responses.
Parameters mirror GZipMiddleware where sensible:
- minimum_size: don't compress bodies smaller than this (bytes)
- gzip_level: gzip compresslevel (1-9); 6 ≈ 4x faster than 9 for ~5% size
- brotli_quality: brotli quality (0-11); 5 is a good text default
Skips compression when ANY of:
- request method doesn't accept (we only act on responses with bodies)
- response already has a Content-Encoding (precompressed static .br/.gz)
- response is a 204/304 or a 206 Partial Content (range requests)
- Content-Type is in the incompressible set (woff2/png/mp4/…)
- client didn't send br/gzip in Accept-Encoding
- buffered body is below minimum_size
"""
def __init__(self, app, *, minimum_size: int = 500,
gzip_level: int = 6, brotli_quality: int = 5):
self.app = app
self.minimum_size = minimum_size
self.gzip_level = gzip_level
self.brotli_quality = brotli_quality
async def __call__(self, scope, receive, send):
if scope["type"] != "http":
await self.app(scope, receive, send)
return
headers = scope.get("headers") or []
accept_encoding = ""
for name, value in headers:
if name == b"accept-encoding":
accept_encoding = value.decode("latin-1")
break
encoding = _choose_encoding(accept_encoding)
if encoding is None:
# Client wants identity (or only unsupported encodings) — pass
# through untouched, but still advertise Vary so caches don't pin.
await self.app(scope, receive, _vary_only_send(send))
return
responder = _CompressionResponder(
send, encoding,
minimum_size=self.minimum_size,
gzip_level=self.gzip_level,
brotli_quality=self.brotli_quality,
)
await self.app(scope, receive, responder)
def _vary_only_send(send):
"""Wrap `send` to append `Vary: Accept-Encoding` to the start message,
without compressing (used when the client accepts no encoding we support)."""
started = {"done": False}
async def wrapped(message):
if message["type"] == "http.response.start" and not started["done"]:
started["done"] = True
message = dict(message)
message["headers"] = _add_vary(list(message.get("headers", [])))
await send(message)
return wrapped
def _add_vary(raw_headers: list[tuple[bytes, bytes]]) -> list[tuple[bytes, bytes]]:
"""Ensure `Vary: Accept-Encoding` is present (merging with an existing Vary)."""
out: list[tuple[bytes, bytes]] = []
have_vary = False
for name, value in raw_headers:
if name.lower() == b"vary":
have_vary = True
tokens = {t.strip().lower() for t in value.split(b",")}
if b"accept-encoding" not in tokens:
value = value + b", Accept-Encoding"
out.append((name, value))
if not have_vary:
out.append((b"vary", b"Accept-Encoding"))
return out
class _CompressionResponder:
"""Buffers the response body, then compresses on the final chunk if eligible.
Buffering the full body (rather than streaming-compress) keeps the logic
simple and correct for the small text payloads this app emits (API JSON up
to ~5 MB atlas). Streaming responses set Content-Encoding themselves or are
media/range responses we deliberately skip.
"""
def __init__(self, send, encoding, *, minimum_size, gzip_level, brotli_quality):
self.send = send
self.encoding = encoding
self.minimum_size = minimum_size
self.gzip_level = gzip_level
self.brotli_quality = brotli_quality
self.start_message: dict | None = None
self.should_compress = True
self.body_chunks: list[bytes] = []
async def __call__(self, message):
mtype = message["type"]
if mtype == "http.response.start":
self.start_message = message
raw_headers = message.get("headers", [])
self.should_compress = self._eligible(message["status"], raw_headers)
# Defer sending start until we know the compressed length.
return
if mtype != "http.response.body":
await self.send(message)
return
if not self.should_compress:
# Pass through, but make sure start was sent (with Vary added).
await self._flush_passthrough(message)
return
body = message.get("body", b"")
more = message.get("more_body", False)
if body:
self.body_chunks.append(body)
if more:
# keep buffering
return
full = b"".join(self.body_chunks)
if len(full) < self.minimum_size:
await self._flush_passthrough(
{"type": "http.response.body", "body": full, "more_body": False}
)
return
try:
compressed = _compress(
self.encoding, full,
quality=self.brotli_quality, compresslevel=self.gzip_level,
)
except Exception: # pragma: no cover - never fail a response over compression
await self._flush_passthrough(
{"type": "http.response.body", "body": full, "more_body": False}
)
return
assert self.start_message is not None
headers = _mutate_headers_for_compression(
self.start_message.get("headers", []), self.encoding, len(compressed)
)
start = dict(self.start_message)
start["headers"] = headers
await self.send(start)
await self.send(
{"type": "http.response.body", "body": compressed, "more_body": False}
)
def _eligible(self, status: int, raw_headers: Iterable[tuple[bytes, bytes]]) -> bool:
# Never touch responses without a normal 2xx body, or partial/redirect-ish.
if status in (204, 304) or status == 206:
return False
content_type = ""
for name, value in raw_headers:
lname = name.lower()
if lname == b"content-encoding":
# Already encoded (precompressed static .br/.gz, or upstream).
return False
if lname == b"content-type":
content_type = value.decode("latin-1")
return _is_compressible_type(content_type)
async def _flush_passthrough(self, body_message):
if self.start_message is not None:
start = dict(self.start_message)
start["headers"] = _add_vary(list(start.get("headers", [])))
await self.send(start)
self.start_message = None
await self.send(body_message)
def _mutate_headers_for_compression(raw_headers, encoding: str, new_length: int):
"""Strip Content-Length, set Content-Encoding + new length, add Vary."""
out: list[tuple[bytes, bytes]] = []
for name, value in raw_headers:
lname = name.lower()
if lname == b"content-length":
continue # replaced below
if lname == b"content-encoding":
continue # we own this now
out.append((name, value))
out.append((b"content-encoding", encoding.encode("latin-1")))
out.append((b"content-length", str(new_length).encode("latin-1")))
return _add_vary(out)