Skip to content

Commit

Permalink
Revert "Merge pull request #705 from clj-commons/add-brotli-and-zstd-…
Browse files Browse the repository at this point in the history
…deps"

This reverts commit 544561d, reversing
changes made to b5794d4.
  • Loading branch information
DerGuteMoritz committed Apr 9, 2024
1 parent c905153 commit 6a84128
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 48 deletions.
16 changes: 1 addition & 15 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,7 @@
io.netty/netty-resolver {:mvn/version "4.1.108.Final"},
io.netty/netty-resolver-dns {:mvn/version "4.1.108.Final"},
metosin/malli
{:mvn/version "0.15.0", :exclusions [org.clojure/clojure]},
com.aayushatharva.brotli4j/brotli4j {:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/service {:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-linux-aarch64
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-linux-armv7
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-linux-x86_64
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-osx-aarch64
{:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-osx-x86_64 {:mvn/version "1.16.0"},
com.aayushatharva.brotli4j/native-windows-x86_64
{:mvn/version "1.16.0"},
com.github.luben/zstd-jni {:mvn/version "1.5.6-2"}},
{:mvn/version "0.15.0", :exclusions [org.clojure/clojure]}},
:aliases
{:lein2deps
{:deps
Expand Down
24 changes: 12 additions & 12 deletions project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,7 @@
[io.netty/netty-handler-proxy ~netty-version]
[io.netty/netty-resolver ~netty-version]
[io.netty/netty-resolver-dns ~netty-version]
[metosin/malli "0.15.0" :exclusions [org.clojure/clojure]]
;;[com.aayushatharva.brotli4j/all ~brotli-version]
[com.aayushatharva.brotli4j/brotli4j ~brotli-version]
[com.aayushatharva.brotli4j/service ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-armv7 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-windows-x86_64 ~brotli-version]
[com.github.luben/zstd-jni "1.5.6-2"]]
[metosin/malli "0.15.0" :exclusions [org.clojure/clojure]]]
:profiles {:dev {:dependencies [[criterium "0.4.6"]
[cheshire "5.13.0"]
[org.slf4j/slf4j-simple "2.0.12"]
Expand All @@ -47,7 +37,17 @@
[org.bouncycastle/bcprov-jdk18on "1.77"]
[org.bouncycastle/bcpkix-jdk18on "1.77"]
;;[org.bouncycastle/bctls-jdk18on "1.75"]
[io.netty/netty-tcnative-boringssl-static "2.0.65.Final"]]
[io.netty/netty-tcnative-boringssl-static "2.0.65.Final"]
;;[com.aayushatharva.brotli4j/all ~brotli-version]
[com.aayushatharva.brotli4j/brotli4j ~brotli-version]
[com.aayushatharva.brotli4j/service ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-armv7 ~brotli-version]
[com.aayushatharva.brotli4j/native-linux-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-aarch64 ~brotli-version]
[com.aayushatharva.brotli4j/native-osx-x86_64 ~brotli-version]
[com.aayushatharva.brotli4j/native-windows-x86_64 ~brotli-version]
[com.github.luben/zstd-jni "1.5.6-2"]]
:jvm-opts ["-Dorg.slf4j.simpleLogger.defaultLogLevel=debug"
"-Dorg.slf4j.simpleLogger.showThreadName=false"
"-Dorg.slf4j.simpleLogger.showThreadId=true"
Expand Down
42 changes: 42 additions & 0 deletions src-java/aleph/http/AlephCompressionOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package aleph.http;

import io.netty.handler.codec.compression.BrotliOptions;
import io.netty.handler.codec.compression.DeflateOptions;
import io.netty.handler.codec.compression.GzipOptions;
import io.netty.handler.codec.compression.SnappyOptions;
import io.netty.handler.codec.compression.StandardCompressionOptions;
import io.netty.handler.codec.compression.ZstdOptions;

/**
* {@link AlephCompressionOptions} exists because the Clojure compiler cannot
* distinguish between static fields and static methods without reflection.
*
* This is a problem when using Netty's StandardCompressionOptions, because
* reflection triggers a load of all the methods referencing optional classes,
* which may not exist in the classpath, resulting in a ClassNotFoundException.
*/
public class AlephCompressionOptions {
private AlephCompressionOptions() {
// Prevent outside initialization
}

public static BrotliOptions brotli() {
return StandardCompressionOptions.brotli();
}

public static ZstdOptions zstd() {
return StandardCompressionOptions.zstd();
}

public static SnappyOptions snappy() {
return StandardCompressionOptions.snappy();
}

public static GzipOptions gzip() {
return StandardCompressionOptions.gzip();
}

public static DeflateOptions deflate() {
return StandardCompressionOptions.deflate();
}
}
37 changes: 16 additions & 21 deletions src/aleph/http/compression.clj
Original file line number Diff line number Diff line change
@@ -1,33 +1,31 @@
(ns ^:no-doc aleph.http.compression
"Currently focused on HTTP/2, since Netty offers better support for
compression in its HTTP/1 code.
"Currently only for HTTP/2, since Netty offers better support for
compression in HTTP/1 code.
Best supported compression codecs on the web are Brotli, gzip, and deflate.
Snappy is primarily an internal Google codec, but is supported by some
open-source databases. It's not on track to be a web standard, but is
well-supported by Netty.
Zstd is a promising Facebook codec that is registered with IANA, but is not
yet widely available. (See https://caniuse.com/zstd).
Zstd is a Facebook codec that is registered with IANA, but is not yet
widely available. (See https://caniuse.com/zstd).
See https://www.iana.org/assignments/http-parameters/http-parameters.xml#content-coding"
(:require
[aleph.netty :as netty]
[clj-commons.primitive-math :as p]
[clojure.tools.logging :as log])
(:import
(aleph.http AlephCompressionOptions)
(io.netty.channel ChannelHandler)
(io.netty.handler.codec.compression
Brotli
BrotliOptions
CompressionOptions
BrotliOptions CompressionOptions
DeflateOptions
GzipOptions
SnappyOptions
StandardCompressionOptions
Zstd
ZstdOptions)
Zstd ZstdOptions)
(io.netty.handler.codec.http HttpHeaderNames)
(io.netty.handler.codec.http2 Http2HeadersFrame)
(io.netty.util AsciiString)
Expand All @@ -38,10 +36,6 @@
(def ^:private ^AsciiString head-method (AsciiString. "HEAD"))
(def ^:private ^AsciiString connect-method (AsciiString. "CONNECT"))

;; From 0.7.0-rc2 on, Brotli and Zstd should be available by default
(Brotli/ensureAvailability)
(Zstd/ensureAvailability)

(defn- contains-class?
"Returns true if the class is in the array"
[^"[Lio.netty.handler.codec.compression.CompressionOptions;" a ^Class klazz]
Expand All @@ -57,11 +51,11 @@
available-compressor-options
"A Java array of all available compressor options"
(into-array CompressionOptions
[(StandardCompressionOptions/brotli)
(StandardCompressionOptions/deflate)
(StandardCompressionOptions/gzip)
(StandardCompressionOptions/zstd)
(StandardCompressionOptions/snappy)]))
(cond-> [(AlephCompressionOptions/deflate)
(AlephCompressionOptions/gzip)
(AlephCompressionOptions/snappy)]
(Brotli/isAvailable) (conj (AlephCompressionOptions/brotli))
(Zstd/isAvailable) (conj (AlephCompressionOptions/zstd)))))


(defn- qvalue
Expand Down Expand Up @@ -122,10 +116,12 @@

;; no named encodings were listed, so we'll apply *'s qval to unset ones
(p/> star 0.0)
(cond (p/== br -1.0)
(cond (and (p/== br -1.0)
(Brotli/isAvailable))
"br"

(p/== zstd -1.0)
(and (p/== zstd -1.0)
(Zstd/isAvailable))
"zstd"

(p/== snappy -1.0)
Expand Down Expand Up @@ -219,7 +215,6 @@
(p/== 204 status)
(p/== 304 status)))
(log/debug "Setting content-encoding to:" @encoding)
;; TODO: add "vary" header
(.set headers HttpHeaderNames/CONTENT_ENCODING chosen-encoding))))))

(.write ctx msg promise))))))

0 comments on commit 6a84128

Please sign in to comment.