|
1 | 1 | (ns ^:no-doc aleph.http.compression
|
2 |
| - "Currently focused on HTTP/2, since Netty offers better support for |
3 |
| - compression in its HTTP/1 code. |
| 2 | + "Currently only for HTTP/2, since Netty offers better support for |
| 3 | + compression in HTTP/1 code. |
4 | 4 |
|
5 | 5 | Best supported compression codecs on the web are Brotli, gzip, and deflate.
|
6 | 6 |
|
7 | 7 | Snappy is primarily an internal Google codec, but is supported by some
|
8 | 8 | open-source databases. It's not on track to be a web standard, but is
|
9 | 9 | well-supported by Netty.
|
10 | 10 |
|
11 |
| - Zstd is a promising Facebook codec that is registered with IANA, but is not |
12 |
| - yet widely available. (See https://caniuse.com/zstd). |
| 11 | + Zstd is a Facebook codec that is registered with IANA, but is not yet |
| 12 | + widely available. (See https://caniuse.com/zstd). |
13 | 13 |
|
14 | 14 | See https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding"
|
15 | 15 | (:require
|
16 | 16 | [aleph.netty :as netty]
|
17 | 17 | [clj-commons.primitive-math :as p]
|
18 | 18 | [clojure.tools.logging :as log])
|
19 | 19 | (:import
|
| 20 | + (aleph.http AlephCompressionOptions) |
20 | 21 | (io.netty.channel ChannelHandler)
|
21 | 22 | (io.netty.handler.codec.compression
|
22 | 23 | Brotli
|
23 |
| - BrotliOptions |
24 |
| - CompressionOptions |
| 24 | + BrotliOptions CompressionOptions |
25 | 25 | DeflateOptions
|
26 | 26 | GzipOptions
|
27 | 27 | SnappyOptions
|
28 |
| - StandardCompressionOptions |
29 |
| - Zstd |
30 |
| - ZstdOptions) |
| 28 | + Zstd ZstdOptions) |
31 | 29 | (io.netty.handler.codec.http HttpHeaderNames)
|
32 | 30 | (io.netty.handler.codec.http2 Http2HeadersFrame)
|
33 | 31 | (io.netty.util AsciiString)
|
|
38 | 36 | (def ^:private ^AsciiString head-method (AsciiString. "HEAD"))
|
39 | 37 | (def ^:private ^AsciiString connect-method (AsciiString. "CONNECT"))
|
40 | 38 |
|
41 |
| -;; From 0.7.0-rc2 on, Brotli and Zstd should be available by default |
42 |
| -(Brotli/ensureAvailability) |
43 |
| -(Zstd/ensureAvailability) |
44 |
| - |
45 | 39 | (defn- contains-class?
|
46 | 40 | "Returns true if the class is in the array"
|
47 | 41 | [^"[Lio.netty.handler.codec.compression.CompressionOptions;" a ^Class klazz]
|
|
57 | 51 | available-compressor-options
|
58 | 52 | "A Java array of all available compressor options"
|
59 | 53 | (into-array CompressionOptions
|
60 |
| - [(StandardCompressionOptions/brotli) |
61 |
| - (StandardCompressionOptions/deflate) |
62 |
| - (StandardCompressionOptions/gzip) |
63 |
| - (StandardCompressionOptions/zstd) |
64 |
| - (StandardCompressionOptions/snappy)])) |
| 54 | + (cond-> [(AlephCompressionOptions/deflate) |
| 55 | + (AlephCompressionOptions/gzip) |
| 56 | + (AlephCompressionOptions/snappy)] |
| 57 | + (Brotli/isAvailable) (conj (AlephCompressionOptions/brotli)) |
| 58 | + (Zstd/isAvailable) (conj (AlephCompressionOptions/zstd))))) |
65 | 59 |
|
66 | 60 |
|
67 | 61 | (defn- qvalue
|
|
122 | 116 |
|
123 | 117 | ;; no named encodings were listed, so we'll apply *'s qval to unset ones
|
124 | 118 | (p/> star 0.0)
|
125 |
| - (cond (p/== br -1.0) |
| 119 | + (cond (and (p/== br -1.0) |
| 120 | + (Brotli/isAvailable)) |
126 | 121 | "br"
|
127 | 122 |
|
128 |
| - (p/== zstd -1.0) |
| 123 | + (and (p/== zstd -1.0) |
| 124 | + (Zstd/isAvailable)) |
129 | 125 | "zstd"
|
130 | 126 |
|
131 | 127 | (p/== snappy -1.0)
|
|
219 | 215 | (p/== 204 status)
|
220 | 216 | (p/== 304 status)))
|
221 | 217 | (log/debug "Setting content-encoding to:" @encoding)
|
222 |
| - ;; TODO: add "vary" header |
223 | 218 | (.set headers HttpHeaderNames/CONTENT_ENCODING chosen-encoding))))))
|
224 | 219 |
|
225 | 220 | (.write ctx msg promise))))))
|
0 commit comments