Skip to content

Commit

Permalink
Move ring.core.protocols into its own library
Browse files Browse the repository at this point in the history
Move the ring.core.protocols namespace into its own library, thereby
allowing third-party adapters to use the core Ring protocols without
needing to depend on all of the ring/ring-core package.

Add a dependency on the new org.ring-clojure/ring-core-protocols
library to ring/ring-core to maintain backward compatibility.
  • Loading branch information
weavejester committed Feb 2, 2024
1 parent ca7b259 commit a7b0b50
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 4 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ jobs:
key: cljdeps-${{ hashFiles('project.clj', 'ring-*/project.clj') }}
restore-keys: cljdeps-

- name: Install core protocols project locally
run: lein install
working-directory: ./ring-core-protocols

- name: Install websocket protocols project locally
run: lein install
working-directory: ./ring-websocket-protocols
Expand Down
9 changes: 9 additions & 0 deletions ring-core-protocols/project.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
(defproject org.ring-clojure/ring-core-protocols "1.11.0"
:description "Ring core protocols."
:url "https://github.com/ring-clojure/ring"
:scm {:dir ".."}
:license {:name "The MIT License"
:url "http://opensource.org/licenses/MIT"}
:dependencies []
:profiles
{:dev {:dependencies [[org.clojure/clojure "1.7.0"]]}})
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
"Protocols necessary for Ring."
{:added "1.6"}
(:import [java.io Writer OutputStream])
(:require [clojure.java.io :as io]
[ring.util.response :as response]))
(:require [clojure.java.io :as io]))

(defprotocol ^{:added "1.6"} StreamableResponseBody
"A protocol for writing data to the response body via an output stream."
Expand All @@ -12,8 +11,27 @@
will be closed after the value had been written. The stream may be written
asynchronously."))

(defn- ^Writer response-writer [response output-stream]
(if-let [charset (response/get-charset response)]
;; The following private functions are replicated from ring.util.response in
;; order to allow third-party adapters to use StreamableResponseBody without the
;; need for a ring-core dependency.

(def ^:private re-charset
#"(?x);(?:.*\s)?(?i:charset)=(?:
([!\#$%&'*\-+.0-9A-Z\^_`a-z\|~]+)| # token
\"((?:\\\"|[^\"])*)\" # quoted
)\s*(?:;|$)")

(defn- find-charset-in-content-type [content-type]
(when-let [m (re-find re-charset content-type)]
(or (m 1) (m 2))))

(defn- response-charset [response]
(some->> (:headers response)
(some #(when (.equalsIgnoreCase "content-type" (key %)) (val %)))
(find-charset-in-content-type)))

(defn- response-writer ^Writer [response output-stream]
(if-let [charset (response-charset response)]
(io/writer output-stream :encoding charset)
(io/writer output-stream)))

Expand Down
1 change: 1 addition & 0 deletions ring-core-protocols/test/ring/assets/hello world.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello World
File renamed without changes.
1 change: 1 addition & 0 deletions ring-core/project.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
:license {:name "The MIT License"
:url "http://opensource.org/licenses/MIT"}
:dependencies [[org.clojure/clojure "1.7.0"]
[org.ring-clojure/ring-core-protocols "1.11.0"]
[org.ring-clojure/ring-websocket-protocols "1.11.0"]
[ring/ring-codec "1.2.0"]
[commons-io "2.15.0"]
Expand Down

0 comments on commit a7b0b50

Please sign in to comment.