From 34df40d07060a40e0af760b2c19f9bc69643b512 Mon Sep 17 00:00:00 2001 From: Max Penet Date: Fri, 15 Sep 2023 09:26:18 +0200 Subject: [PATCH 1/7] m2 init --- .github/workflows/test.yml | 2 +- deps.edn | 10 +-- src/s_exp/mina.clj | 19 ++++- src/s_exp/mina/handler.clj | 29 +++---- src/s_exp/mina/options.clj | 147 +++++++++++++++++++----------------- src/s_exp/mina/request.clj | 33 ++++---- src/s_exp/mina/response.clj | 21 +++--- test/s_exp/mina_test.clj | 58 +++++++------- 8 files changed, 167 insertions(+), 152 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index fad325a..20d7331 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,7 @@ jobs: uses: actions/setup-java@v3 with: distribution: temurin - java-version: '20' + java-version: '21' - name: Install clojure tools uses: DeLaGuardo/setup-clojure@10.0 diff --git a/deps.edn b/deps.edn index 4d3eeea..97c44d4 100644 --- a/deps.edn +++ b/deps.edn @@ -8,14 +8,12 @@ :paths ["src" "resources"] :deps {org.clojure/clojure {:mvn/version "1.12.0-alpha1"} com.github.strojure/zmap {:mvn/version "1.3.26"} - - io.helidon.nima/helidon-nima {:mvn/version "4.0.0-ALPHA6"} - io.helidon.nima.webserver/helidon-nima-webserver {:mvn/version "4.0.0-ALPHA6"} - io.helidon.nima.http2/helidon-nima-http2-webserver {:mvn/version "4.0.0-ALPHA6"}} + io.helidon.http/helidon-http {:mvn/version "4.0.0-M2"} + io.helidon.webserver/helidon-webserver {:mvn/version "4.0.0-M2"} + io.helidon.webserver/helidon-webserver-http2 {:mvn/version "4.0.0-M2"}} :aliases - {:test {:jvm-opts ["--enable-preview"] - :extra-paths ["test"] + {:test {:extra-paths ["test"] :extra-deps {org.clojure/test.check {:mvn/version "1.1.1"} less-awful-ssl/less-awful-ssl {:mvn/version "1.0.6"} eftest/eftest {:mvn/version "0.6.0"} diff --git a/src/s_exp/mina.clj b/src/s_exp/mina.clj index c376426..228a4f7 100644 --- a/src/s_exp/mina.clj +++ b/src/s_exp/mina.clj @@ -1,20 +1,32 @@ (ns s-exp.mina (:require [s-exp.mina.handler] [s-exp.mina.options :as options]) - (:import (io.helidon.nima.webserver WebServer WebServer$Builder))) + (:import (io.helidon.webserver WebServer WebServerConfig WebServerConfig$Builder))) (set! *warn-on-reflection* true) (def default-options {:connection-provider false}) (defn- server-builder - ^WebServer$Builder + ^WebServerConfig$Builder [options] (reduce (fn [builder [k v]] (options/set-server-option! builder k v options)) - (WebServer/builder) + (WebServerConfig/builder) options)) +;; (clojure.reflect/reflect (WebServerConfig/builder)) + +;; (import 'io.helidon.config.Config) + +;; (clojure.reflect/reflect (io.helidon.config.Config/builder)) + +;; (let [c (io.helidon.config.Config/create) +;; b (WebServerConfig/builder) +;; p (.buildPrototype (.config b (.get c "server")))] +;; (clojure.reflect/reflect p) +;; (.writeQueueLength p)) + (defn start! "Starts a new server. @@ -31,6 +43,7 @@ (start! (assoc options :handler handler))) ([options] (-> (server-builder (merge default-options options)) + .build (.start)))) (defn stop! diff --git a/src/s_exp/mina/handler.clj b/src/s_exp/mina/handler.clj index fb5add5..c3173f0 100644 --- a/src/s_exp/mina/handler.clj +++ b/src/s_exp/mina/handler.clj @@ -2,23 +2,24 @@ (:require [s-exp.mina.options :as options] [s-exp.mina.request :as request] [s-exp.mina.response :as response]) - (:import (io.helidon.nima.webserver WebServer$Builder) - (io.helidon.nima.webserver.http Handler - HttpRouting))) + (:import (io.helidon.webserver WebServerConfig$Builder) + (io.helidon.webserver.http Handler + HttpRouting))) -(defn set-ring1-handler! ^WebServer$Builder - [^WebServer$Builder builder handler _options] +(defn set-ring1-handler! ^WebServerConfig$Builder + [^WebServerConfig$Builder builder handler _options] (doto builder (.addRouting - (doto (HttpRouting/builder) - (.any - (into-array Handler - [(reify Handler - (handle [_ server-request server-response] - (->> (request/ring-request server-request server-response) - handler - (response/set-response! server-response))))])))))) + (.build + (doto (HttpRouting/builder) + (.any + (into-array Handler + [(reify Handler + (handle [_ server-request server-response] + (->> (request/ring-request server-request server-response) + handler + (response/set-response! server-response))))]))))))) (defmethod options/set-server-option! :handler - [^WebServer$Builder builder _ handler options] + [^WebServerConfig$Builder builder _ handler options] (set-ring1-handler! builder handler options)) diff --git a/src/s_exp/mina/options.clj b/src/s_exp/mina/options.clj index 2ea495d..7236fa3 100644 --- a/src/s_exp/mina/options.clj +++ b/src/s_exp/mina/options.clj @@ -1,90 +1,95 @@ (ns s-exp.mina.options (:import (io.helidon.common.socket SocketOptions$Builder) - (io.helidon.nima.webserver ListenerConfiguration$Builder WebServer$Builder) + (io.helidon.webserver ;; ListenerConfiguration$Builder + ServerListener + WebServerConfig$Builder) (java.time Duration))) +;; (ServerListener/config) + +(set! *warn-on-reflection* true) (defmulti set-server-option! (fn [_builder k _v _options] k)) (defmethod set-server-option! :default [builder _ _ _] builder) (defmethod set-server-option! :host - [^WebServer$Builder builder _ host _] + [^WebServerConfig$Builder builder _ host _] (.host builder host)) - +;; io.helidon.webserver.WebServerConfig$BuilderBaseio.helidon.webserver.WebServerConfig$BuilderBase (defmethod set-server-option! :port - [^WebServer$Builder builder _ port _] + [^WebServerConfig$Builder builder _ port _] (.port builder (int port))) -(defn- set-connection-options! - [^SocketOptions$Builder socket-options-builder - {:keys [socket-receive-buffer-size socket-send-buffer-size - socket-reuse-address socket-keep-alive tcp-no-delay - read-timeout connect-timeout]}] - (when socket-receive-buffer-size - (.socketReceiveBufferSize socket-options-builder - (int socket-receive-buffer-size))) - - (when socket-send-buffer-size - (.socketSendBufferSize socket-options-builder - (int socket-send-buffer-size))) - - (when socket-reuse-address - (.socketReuseAddress socket-options-builder - (boolean socket-reuse-address))) - - (when socket-keep-alive - (.socketKeepAlive socket-options-builder - (boolean socket-keep-alive))) - (when tcp-no-delay - (.tcpNoDelay socket-options-builder - (boolean tcp-no-delay))) - - (when read-timeout - (.readTimeout socket-options-builder - (Duration/ofMillis read-timeout))) - (when connect-timeout - (.connectTimeout socket-options-builder - (Duration/ofMillis connect-timeout)))) - -(defn- set-listener-configuration! - [^ListenerConfiguration$Builder listener-configuration-builder - {:keys [write-queue-length backlog max-payload-size receive-buffer-size - connection-options]}] - (when backlog - (.backlog listener-configuration-builder - (int backlog))) - - (when max-payload-size - (.maxPayloadSize listener-configuration-builder - (long max-payload-size))) - - (when write-queue-length - (.writeQueueLength listener-configuration-builder - (int write-queue-length))) - - (when receive-buffer-size - (.receiveBufferSize listener-configuration-builder - (int receive-buffer-size))) - - (when (seq connection-options) - (.connectionOptions listener-configuration-builder - (reify java.util.function.Consumer - (accept [_ socket-options-builder] - (set-connection-options! socket-options-builder - connection-options)))))) - -(defmethod set-server-option! :default-socket - [^WebServer$Builder builder _ default-socket _] - (doto builder - (.defaultSocket - (reify java.util.function.Consumer - (accept [_ listener-configuration-builder] - (set-listener-configuration! listener-configuration-builder - default-socket)))))) +#_(defn- set-connection-options! + [^SocketOptions$Builder socket-options-builder + {:keys [socket-receive-buffer-size socket-send-buffer-size + socket-reuse-address socket-keep-alive tcp-no-delay + read-timeout connect-timeout]}] + (when socket-receive-buffer-size + (.socketReceiveBufferSize socket-options-builder + (int socket-receive-buffer-size))) + + (when socket-send-buffer-size + (.socketSendBufferSize socket-options-builder + (int socket-send-buffer-size))) + + (when socket-reuse-address + (.socketReuseAddress socket-options-builder + (boolean socket-reuse-address))) + + (when socket-keep-alive + (.socketKeepAlive socket-options-builder + (boolean socket-keep-alive))) + (when tcp-no-delay + (.tcpNoDelay socket-options-builder + (boolean tcp-no-delay))) + + (when read-timeout + (.readTimeout socket-options-builder + (Duration/ofMillis read-timeout))) + (when connect-timeout + (.connectTimeout socket-options-builder + (Duration/ofMillis connect-timeout)))) + +#_(defn- set-listener-configuration! + [^ListenerConfiguration$Builder listener-configuration-builder + {:keys [write-queue-length backlog max-payload-size receive-buffer-size + connection-options]}] + (when backlog + (.backlog listener-configuration-builder + (int backlog))) + + (when max-payload-size + (.maxPayloadSize listener-configuration-builder + (long max-payload-size))) + + (when write-queue-length + (.writeQueueLength listener-configuration-builder + (int write-queue-length))) + + (when receive-buffer-size + (.receiveBufferSize listener-configuration-builder + (int receive-buffer-size))) + + #_(when (seq connection-options) + (.connectionOptions listener-configuration-builder + (reify java.util.function.Consumer + (accept [_ socket-options-builder] + (set-connection-options! socket-options-builder + connection-options)))))) + +#_(defmethod set-server-option! :default-socket + [^WebServerConfig$Builder builder _ default-socket _] + (doto builder + (.defaultSocket + (reify java.util.function.Consumer + (accept [_ listener-configuration-builder] + (set-listener-configuration! listener-configuration-builder + default-socket)))))) (defmethod set-server-option! :tls - [^WebServer$Builder builder _ tls _] + [^WebServerConfig$Builder builder _ tls _] (doto builder (.tls tls))) diff --git a/src/s_exp/mina/request.clj b/src/s_exp/mina/request.clj index 6747f6b..e0e3947 100644 --- a/src/s_exp/mina/request.clj +++ b/src/s_exp/mina/request.clj @@ -10,37 +10,39 @@ MapEquivalence PersistentHashMap Util) - (io.helidon.common.http + (io.helidon.http + Headers Http$Header Http$HeaderName - Http$HeaderValue + Http$HeaderNames ServerRequestHeaders) - (io.helidon.nima.webserver.http ServerRequest ServerResponse) + (io.helidon.webserver.http ServerRequest ServerResponse) (java.util Map))) (defn header-name ^Http$HeaderName [s] - (Http$Header/createFromLowercase s)) + (Http$HeaderNames/createFromLowercase s)) (defn header->value* - ([^Http$Header header header-name] + ([^Headers header header-name] (header->value* header header-name nil)) - ([^Http$Header header header-name not-found] + ([^Headers header + ^Http$HeaderName header-name not-found] (-> header (.value header-name) (.orElse not-found)))) (defn header->value - ([^Http$Header h k] + ([^Headers h k] (header->value h k nil)) - ([^Http$Header h k not-found] + ([^Headers h k not-found] (header->value* h (header-name k) not-found))) (defn ring-headers* - [^ServerRequestHeaders headers] - (-> (reduce (fn [m ^Http$HeaderValue h] + [^Headers headers] + (-> (reduce (fn [m ^Http$Header h] (assoc! m (.lowerCase (.headerName h)) (.value h))) @@ -49,7 +51,7 @@ persistent!)) (defprotocol RingHeaders - (ring-headers [_])) + (^clojure.lang.APersistentMap ring-headers [_])) (defn ring-method [^ServerRequest server-request] @@ -80,7 +82,7 @@ "2.0" "HTTP/2")) ;; inspired by ring-undertow -(deftype HeaderMapProxy [^ServerRequestHeaders headers +(deftype HeaderMapProxy [^Headers headers ^:volatile-mutable persistent-copy] Map (size [_] @@ -144,11 +146,10 @@ (iterator [_] (->> headers - (eduction (map (fn [header] + .iterator + (eduction (map (fn [^Http$Header header] (MapEntry. (.lowerCase (.headerName header)) - (.value header))))) - - .iterator)) + (.value header))))))) IKVReduce (kvreduce [this f init] diff --git a/src/s_exp/mina/response.clj b/src/s_exp/mina/response.clj index 2a62359..39ad928 100644 --- a/src/s_exp/mina/response.clj +++ b/src/s_exp/mina/response.clj @@ -1,7 +1,9 @@ (ns s-exp.mina.response - (:require [s-exp.mina.utils :as u]) - (:import (io.helidon.common.http HeaderEnum Http$Header Http$Status) - (io.helidon.nima.webserver.http ServerResponse) + (:import (io.helidon.http Http$Headers + Http$HeaderNames + Http$HeaderName + Http$Status) + (io.helidon.webserver.http ServerResponse) (java.io FileInputStream InputStream OutputStream))) (defprotocol BodyWriter @@ -34,21 +36,16 @@ (write-body! [o server-response] (.send ^ServerResponse server-response o))) -(def header-name - (eval `(fn [ring-header-key#] - (case ring-header-key# - ~@(mapcat (fn [[k v]] - [k (symbol "io.helidon.common.http.Http$Header" (.name v))]) - (u/enum->map HeaderEnum)) - (Http$Header/create (name ring-header-key#)))))) +(defn header-name ^Http$HeaderName [ring-header-name] + (Http$HeaderNames/createFromLowercase (name ring-header-name))) (defn set-headers! [^ServerResponse server-response headers] (when headers (run! (fn [[k v]] (.header server-response - (Http$Header/create (header-name k) - v))) + (Http$Headers/create (header-name k) + v))) headers))) (defn- set-status! diff --git a/test/s_exp/mina_test.clj b/test/s_exp/mina_test.clj index 12316d7..54c403e 100644 --- a/test/s_exp/mina_test.clj +++ b/test/s_exp/mina_test.clj @@ -5,7 +5,8 @@ [clojure.test :refer :all] [less.awful.ssl :as ls] [s-exp.mina :as m]) - (:import (io.helidon.nima.common.tls Tls TlsClientAuth))) + ;; (:import (io.helidon.nima.common.tls Tls TlsClientAuth)) + ) (def ^:dynamic *endpoint*) @@ -22,7 +23,6 @@ (deftest test-headers (with-server {:handler (fn [req] {:headers {:foo "bar"}})} (is (-> (client/get *endpoint*) :headers :foo (= "bar")))) - (with-server {:handler (fn [req] {:headers {:foo ["bar" "baz"]}})} (is (-> (client/get *endpoint*) :headers :foo (= ["bar" "baz"]))))) @@ -75,33 +75,33 @@ (with-server {:handler (fn [req] {:body (java.io.ByteArrayInputStream. (.getBytes "yes"))})} (is (-> (client/get *endpoint*) :body (= "yes"))))) -(defn tls [] - (-> (Tls/builder) - (.tlsClientAuth TlsClientAuth/REQUIRED) - (.trustAll true) - (.sslContext (ls/ssl-context "test/server.key" - "test/server.crt" - "test/server.crt")) - (.endpointIdentificationAlgorithm (Tls/ENDPOINT_IDENTIFICATION_NONE)) - (.build))) - -(deftest test-ssl-context - (with-server {:handler (fn [req] {}) - :tls (tls)} - (let [endpoint (str/replace *endpoint* "http://" "https://")] - (is (thrown? Exception (client/get endpoint))) - (is (status-ok? (client/get endpoint {:insecure? true}))) - (is (status-ok? (client/get endpoint - {:insecure? true - :keystore "test/keystore.jks" - :keystore-pass "password" - :trust-store "test/keystore.jks" - :trust-store-pass "password"}))))) - - (with-server {:handler (fn [req] {:body (str (:scheme req))}) :tls (tls)} - (is (-> (client/get (str/replace *endpoint* "http" "https") - {:insecure? true}) - :body (= ":https"))))) +#_(defn tls [] + (-> (Tls/builder) + (.tlsClientAuth TlsClientAuth/REQUIRED) + (.trustAll true) + (.sslContext (ls/ssl-context "test/server.key" + "test/server.crt" + "test/server.crt")) + (.endpointIdentificationAlgorithm (Tls/ENDPOINT_IDENTIFICATION_NONE)) + (.build))) + +#_(deftest test-ssl-context + (with-server {:handler (fn [req] {}) + :tls (tls)} + (let [endpoint (str/replace *endpoint* "http://" "https://")] + (is (thrown? Exception (client/get endpoint))) + (is (status-ok? (client/get endpoint {:insecure? true}))) + (is (status-ok? (client/get endpoint + {:insecure? true + :keystore "test/keystore.jks" + :keystore-pass "password" + :trust-store "test/keystore.jks" + :trust-store-pass "password"}))))) + + (with-server {:handler (fn [req] {:body (str (:scheme req))}) :tls (tls)} + (is (-> (client/get (str/replace *endpoint* "http" "https") + {:insecure? true}) + :body (= ":https"))))) From 7a3b5d6e1a47195a0f0b5a01753740c05739df16 Mon Sep 17 00:00:00 2001 From: Max Penet Date: Fri, 15 Sep 2023 15:59:05 +0200 Subject: [PATCH 2/7] fixup! m2 init --- src/s_exp/mina.clj | 26 ++----- src/s_exp/mina/options.clj | 138 +++++++++++++++++-------------------- 2 files changed, 71 insertions(+), 93 deletions(-) diff --git a/src/s_exp/mina.clj b/src/s_exp/mina.clj index 228a4f7..d7ddb3e 100644 --- a/src/s_exp/mina.clj +++ b/src/s_exp/mina.clj @@ -15,18 +15,6 @@ (WebServerConfig/builder) options)) -;; (clojure.reflect/reflect (WebServerConfig/builder)) - -;; (import 'io.helidon.config.Config) - -;; (clojure.reflect/reflect (io.helidon.config.Config/builder)) - -;; (let [c (io.helidon.config.Config/create) -;; b (WebServerConfig/builder) -;; p (.buildPrototype (.config b (.get c "server")))] -;; (clojure.reflect/reflect p) -;; (.writeQueueLength p)) - (defn start! "Starts a new server. @@ -52,13 +40,13 @@ (.stop server)) ;; (def r {:status 200}) -;; ;; (def h (fn [req] -;; ;; (prn (counted? (:headers req))) -;; ;; r)) -;; ;; (def h (fn [_] -;; ;; ;; (prn :aasdf ((:headers _) "accept")) -;; ;; ;; (prn (:headers _)) -;; ;; r)) +;; (def h (fn [req] +;; (prn (counted? (:headers req))) +;; r)) +;; (def h (fn [_] +;; ;; (prn :aasdf ((:headers _) "accept")) +;; ;; (prn (:headers _)) +;; r)) ;; (def s (start! ;; r ;; {:host "0.0.0.0" :port 8080 :default-socket {:write-queue-length 10240}})) diff --git a/src/s_exp/mina/options.clj b/src/s_exp/mina/options.clj index 7236fa3..7fa0b0d 100644 --- a/src/s_exp/mina/options.clj +++ b/src/s_exp/mina/options.clj @@ -1,12 +1,10 @@ (ns s-exp.mina.options (:import (io.helidon.common.socket SocketOptions$Builder) - (io.helidon.webserver ;; ListenerConfiguration$Builder - ServerListener - WebServerConfig$Builder) + (io.helidon.common.tls TlsConfig) + (io.helidon.webserver ListenerConfig$Builder + WebServerConfig$Builder) (java.time Duration))) -;; (ServerListener/config) - (set! *warn-on-reflection* true) (defmulti set-server-option! (fn [_builder k _v _options] k)) @@ -16,80 +14,72 @@ (defmethod set-server-option! :host [^WebServerConfig$Builder builder _ host _] (.host builder host)) -;; io.helidon.webserver.WebServerConfig$BuilderBaseio.helidon.webserver.WebServerConfig$BuilderBase + (defmethod set-server-option! :port [^WebServerConfig$Builder builder _ port _] (.port builder (int port))) -#_(defn- set-connection-options! - [^SocketOptions$Builder socket-options-builder - {:keys [socket-receive-buffer-size socket-send-buffer-size - socket-reuse-address socket-keep-alive tcp-no-delay - read-timeout connect-timeout]}] - (when socket-receive-buffer-size - (.socketReceiveBufferSize socket-options-builder - (int socket-receive-buffer-size))) - - (when socket-send-buffer-size - (.socketSendBufferSize socket-options-builder - (int socket-send-buffer-size))) - - (when socket-reuse-address - (.socketReuseAddress socket-options-builder - (boolean socket-reuse-address))) - - (when socket-keep-alive - (.socketKeepAlive socket-options-builder - (boolean socket-keep-alive))) - (when tcp-no-delay - (.tcpNoDelay socket-options-builder - (boolean tcp-no-delay))) - - (when read-timeout - (.readTimeout socket-options-builder - (Duration/ofMillis read-timeout))) - (when connect-timeout - (.connectTimeout socket-options-builder - (Duration/ofMillis connect-timeout)))) - -#_(defn- set-listener-configuration! - [^ListenerConfiguration$Builder listener-configuration-builder - {:keys [write-queue-length backlog max-payload-size receive-buffer-size - connection-options]}] - (when backlog - (.backlog listener-configuration-builder - (int backlog))) - - (when max-payload-size - (.maxPayloadSize listener-configuration-builder - (long max-payload-size))) - - (when write-queue-length - (.writeQueueLength listener-configuration-builder - (int write-queue-length))) - - (when receive-buffer-size - (.receiveBufferSize listener-configuration-builder - (int receive-buffer-size))) - - #_(when (seq connection-options) - (.connectionOptions listener-configuration-builder - (reify java.util.function.Consumer - (accept [_ socket-options-builder] - (set-connection-options! socket-options-builder - connection-options)))))) - -#_(defmethod set-server-option! :default-socket - [^WebServerConfig$Builder builder _ default-socket _] - (doto builder - (.defaultSocket - (reify java.util.function.Consumer - (accept [_ listener-configuration-builder] - (set-listener-configuration! listener-configuration-builder - default-socket)))))) +(defmethod set-server-option! :backlog + [^WebServerConfig$Builder builder _ backlog _] + (.backlog builder (int backlog))) + +(defmethod set-server-option! :backlog + [^WebServerConfig$Builder builder _ backlog _] + (.backlog builder (int backlog))) + +(defmethod set-server-option! :max-payload-size + [^WebServerConfig$Builder builder _ max-payload-size _] + (.maxPayloadSize builder (long max-payload-size))) + +(defmethod set-server-option! :write-queue-length + [^WebServerConfig$Builder builder _ write-queue-length _] + (.writeQueueLength builder (long write-queue-length))) + +(defmethod set-server-option! :receive-buffer-size + [^WebServerConfig$Builder builder _ receive-buffer-size _] + (.receiveBufferSize builder (int receive-buffer-size))) + +(defn- set-connection-options! + [^SocketOptions$Builder socket-options-builder + {:keys [socket-receive-buffer-size socket-send-buffer-size + socket-reuse-address socket-keep-alive tcp-no-delay + read-timeout connect-timeout]}] + (when socket-receive-buffer-size + (.socketReceiveBufferSize socket-options-builder + (int socket-receive-buffer-size))) + + (when socket-send-buffer-size + (.socketSendBufferSize socket-options-builder + (int socket-send-buffer-size))) + + (when socket-reuse-address + (.socketReuseAddress socket-options-builder + (boolean socket-reuse-address))) + + (when socket-keep-alive + (.socketKeepAlive socket-options-builder + (boolean socket-keep-alive))) + (when tcp-no-delay + (.tcpNoDelay socket-options-builder + (boolean tcp-no-delay))) + + (when read-timeout + (.readTimeout socket-options-builder + (Duration/ofMillis read-timeout))) + (when connect-timeout + (.connectTimeout socket-options-builder + (Duration/ofMillis connect-timeout)))) + +(defmethod set-server-option! :connection-options + [^WebServerConfig$Builder builder _ connection-options _] + (.connectionOptions builder + (reify java.util.function.Consumer + (accept [_ socket-options-builder] + (set-connection-options! socket-options-builder + connection-options))))) (defmethod set-server-option! :tls - [^WebServerConfig$Builder builder _ tls _] - (doto builder (.tls tls))) + [^WebServerConfig$Builder builder _ ^TlsConfig tls-config _] + (doto builder (.tls tls-config))) From b6bd2ee615f767561b1e51083a989c79ff0191b1 Mon Sep 17 00:00:00 2001 From: Max Penet Date: Fri, 15 Sep 2023 19:09:28 +0200 Subject: [PATCH 3/7] fixup! fixup! m2 init --- README.md | 15 ++++++++--- src/s_exp/mina.clj | 6 +++-- src/s_exp/mina/options.clj | 15 +++++------ test/s_exp/mina_test.clj | 52 ++++++++++++++++---------------------- 4 files changed, 44 insertions(+), 44 deletions(-) diff --git a/README.md b/README.md index 630ba8d..9f4ec78 100644 --- a/README.md +++ b/README.md @@ -35,7 +35,17 @@ middlewares out there. * `:port` - port the server listens to, defaults to random free port -* `:default-socket` - map-of `:write-queue-length` `:backlog` `:max-payload-size` `:receive-buffer-size` `:connection-options`(map-of `:socket-receive-buffer-size` `:socket-send-buffer-size` `:socket-reuse-address` `:socket-keep-alive` `:tcp-no-delay` `:read-timeout` `:connect-timeout`) +* `:write-queue-length` + +* `:backlog` + +* `:max-payload-size` + +* `:write-queue-length` + +* `:receive-buffer-size` + +* `:connection-options`(map-of `:socket-receive-buffer-size` `:socket-send-buffer-size` `:socket-reuse-address` `:socket-keep-alive` `:tcp-no-delay` `:read-timeout` `:connect-timeout`) * `:tls` - A `io.helidon.nima.common.tls.Tls` instance @@ -49,8 +59,7 @@ it will do the protocol switch automatically. ## Installation -Note: You need to use java20 and add `:jvm-opts ["--enable-preview"]` to the -alias you will use to be able to run it. +Note: You need to use java **21** https://clojars.org/com.s-exp/mina diff --git a/src/s_exp/mina.clj b/src/s_exp/mina.clj index d7ddb3e..4e0cb5e 100644 --- a/src/s_exp/mina.clj +++ b/src/s_exp/mina.clj @@ -42,14 +42,16 @@ ;; (def r {:status 200}) ;; (def h (fn [req] ;; (prn (counted? (:headers req))) -;; r)) + ;; r)) ;; (def h (fn [_] ;; ;; (prn :aasdf ((:headers _) "accept")) ;; ;; (prn (:headers _)) ;; r)) ;; (def s (start! ;; r -;; {:host "0.0.0.0" :port 8080 :default-socket {:write-queue-length 10240}})) +;; {:host "0.0.0.0" :port 8080 +;; :write-queue-length 10240 +;; :connection-options {:socket-send-buffer-size 1024}})) ;; (stop! s) diff --git a/src/s_exp/mina/options.clj b/src/s_exp/mina/options.clj index 7fa0b0d..466b9b6 100644 --- a/src/s_exp/mina/options.clj +++ b/src/s_exp/mina/options.clj @@ -1,11 +1,11 @@ (ns s-exp.mina.options (:import (io.helidon.common.socket SocketOptions$Builder) - (io.helidon.common.tls TlsConfig) - (io.helidon.webserver ListenerConfig$Builder - WebServerConfig$Builder) + (io.helidon.common.tls Tls) + (io.helidon.webserver WebServerConfig$Builder) (java.time Duration))) (set! *warn-on-reflection* true) + (defmulti set-server-option! (fn [_builder k _v _options] k)) (defmethod set-server-option! :default [builder _ _ _] @@ -23,10 +23,6 @@ [^WebServerConfig$Builder builder _ backlog _] (.backlog builder (int backlog))) -(defmethod set-server-option! :backlog - [^WebServerConfig$Builder builder _ backlog _] - (.backlog builder (int backlog))) - (defmethod set-server-option! :max-payload-size [^WebServerConfig$Builder builder _ max-payload-size _] (.maxPayloadSize builder (long max-payload-size))) @@ -75,11 +71,12 @@ (.connectionOptions builder (reify java.util.function.Consumer (accept [_ socket-options-builder] + (prn :sob socket-options-builder) (set-connection-options! socket-options-builder connection-options))))) (defmethod set-server-option! :tls - [^WebServerConfig$Builder builder _ ^TlsConfig tls-config _] - (doto builder (.tls tls-config))) + [^WebServerConfig$Builder builder _ tls-config _] + (doto builder (.tls ^Tls tls-config))) diff --git a/test/s_exp/mina_test.clj b/test/s_exp/mina_test.clj index 54c403e..f89d1d2 100644 --- a/test/s_exp/mina_test.clj +++ b/test/s_exp/mina_test.clj @@ -1,12 +1,11 @@ (ns s-exp.mina-test - "Tests vars run in parallel" (:require [clj-http.client :as client] [clojure.string :as str] [clojure.test :refer :all] [less.awful.ssl :as ls] [s-exp.mina :as m]) - ;; (:import (io.helidon.nima.common.tls Tls TlsClientAuth)) - ) + (:import (io.helidon.common.tls Tls TlsClientAuth) + (io.helidon.common.tls TlsConfig))) (def ^:dynamic *endpoint*) @@ -75,33 +74,26 @@ (with-server {:handler (fn [req] {:body (java.io.ByteArrayInputStream. (.getBytes "yes"))})} (is (-> (client/get *endpoint*) :body (= "yes"))))) -#_(defn tls [] - (-> (Tls/builder) - (.tlsClientAuth TlsClientAuth/REQUIRED) - (.trustAll true) - (.sslContext (ls/ssl-context "test/server.key" - "test/server.crt" - "test/server.crt")) - (.endpointIdentificationAlgorithm (Tls/ENDPOINT_IDENTIFICATION_NONE)) - (.build))) - -#_(deftest test-ssl-context - (with-server {:handler (fn [req] {}) - :tls (tls)} - (let [endpoint (str/replace *endpoint* "http://" "https://")] - (is (thrown? Exception (client/get endpoint))) - (is (status-ok? (client/get endpoint {:insecure? true}))) - (is (status-ok? (client/get endpoint - {:insecure? true - :keystore "test/keystore.jks" - :keystore-pass "password" - :trust-store "test/keystore.jks" - :trust-store-pass "password"}))))) - - (with-server {:handler (fn [req] {:body (str (:scheme req))}) :tls (tls)} - (is (-> (client/get (str/replace *endpoint* "http" "https") - {:insecure? true}) - :body (= ":https"))))) +(defn tls [] + (let [b (doto (TlsConfig/builder) + (.sslContext (ls/ssl-context "test/server.key" + "test/server.crt" + "test/server.crt")) + (.clientAuth TlsClientAuth/REQUIRED) + (.trustAll true) + (.endpointIdentificationAlgorithm (Tls/ENDPOINT_IDENTIFICATION_NONE)))] + (.build b))) + +(deftest test-ssl-context + (with-server {:handler (fn [req] {}) :tls (tls)} + (let [endpoint (str/replace *endpoint* "http://" "https://")] + (is (thrown? Exception (client/get endpoint))) + (is (status-ok? (client/get endpoint + {:insecure? true + :keystore "test/keystore.jks" + :keystore-pass "password" + :trust-store "test/keystore.jks" + :trust-store-pass "password"})))))) From 85a871aad7e19b6e8a9ca97eb64641dd094d8caa Mon Sep 17 00:00:00 2001 From: Max Penet Date: Fri, 15 Sep 2023 19:11:39 +0200 Subject: [PATCH 4/7] fixup! fixup! fixup! m2 init --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 20d7331..e001c61 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: - name: Prepare java uses: actions/setup-java@v3 with: - distribution: temurin + distribution: openjdk java-version: '21' - name: Install clojure tools From 13a073d61af4265aba732b9b43fe13fba3f19cab Mon Sep 17 00:00:00 2001 From: Max Penet Date: Fri, 15 Sep 2023 19:13:49 +0200 Subject: [PATCH 5/7] fixup! fixup! fixup! fixup! m2 init --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e001c61..9254816 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: - name: Prepare java uses: actions/setup-java@v3 with: - distribution: openjdk + distribution: 'oracle' java-version: '21' - name: Install clojure tools From aa50316c0362f9e5a53ed4f055771ce8e4fe3003 Mon Sep 17 00:00:00 2001 From: Max Penet Date: Fri, 15 Sep 2023 19:16:03 +0200 Subject: [PATCH 6/7] fixup! fixup! fixup! fixup! fixup! m2 init --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9254816..456c101 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -13,7 +13,7 @@ jobs: uses: actions/setup-java@v3 with: distribution: 'oracle' - java-version: '21' + java-version: '21-ea' - name: Install clojure tools uses: DeLaGuardo/setup-clojure@10.0 From 066fa9418d703f49a11c556e8b9c0fffa47e7cff Mon Sep 17 00:00:00 2001 From: Max Penet Date: Fri, 15 Sep 2023 19:17:04 +0200 Subject: [PATCH 7/7] fixup! fixup! fixup! fixup! fixup! fixup! m2 init --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 456c101..4f899bc 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -12,7 +12,7 @@ jobs: - name: Prepare java uses: actions/setup-java@v3 with: - distribution: 'oracle' + distribution: 'zulu' java-version: '21-ea' - name: Install clojure tools