Skip to content

Commit a7b0b50

Browse files
committed
Move ring.core.protocols into its own library
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.
1 parent ca7b259 commit a7b0b50

6 files changed

Lines changed: 37 additions & 4 deletions

File tree

.github/workflows/test.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ jobs:
2525
key: cljdeps-${{ hashFiles('project.clj', 'ring-*/project.clj') }}
2626
restore-keys: cljdeps-
2727

28+
- name: Install core protocols project locally
29+
run: lein install
30+
working-directory: ./ring-core-protocols
31+
2832
- name: Install websocket protocols project locally
2933
run: lein install
3034
working-directory: ./ring-websocket-protocols

ring-core-protocols/project.clj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
(defproject org.ring-clojure/ring-core-protocols "1.11.0"
2+
:description "Ring core protocols."
3+
:url "https://github.com/ring-clojure/ring"
4+
:scm {:dir ".."}
5+
:license {:name "The MIT License"
6+
:url "http://opensource.org/licenses/MIT"}
7+
:dependencies []
8+
:profiles
9+
{:dev {:dependencies [[org.clojure/clojure "1.7.0"]]}})

ring-core/src/ring/core/protocols.clj renamed to ring-core-protocols/src/ring/core/protocols.clj

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
"Protocols necessary for Ring."
33
{:added "1.6"}
44
(:import [java.io Writer OutputStream])
5-
(:require [clojure.java.io :as io]
6-
[ring.util.response :as response]))
5+
(:require [clojure.java.io :as io]))
76

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

15-
(defn- ^Writer response-writer [response output-stream]
16-
(if-let [charset (response/get-charset response)]
14+
;; The following private functions are replicated from ring.util.response in
15+
;; order to allow third-party adapters to use StreamableResponseBody without the
16+
;; need for a ring-core dependency.
17+
18+
(def ^:private re-charset
19+
#"(?x);(?:.*\s)?(?i:charset)=(?:
20+
([!\#$%&'*\-+.0-9A-Z\^_`a-z\|~]+)| # token
21+
\"((?:\\\"|[^\"])*)\" # quoted
22+
)\s*(?:;|$)")
23+
24+
(defn- find-charset-in-content-type [content-type]
25+
(when-let [m (re-find re-charset content-type)]
26+
(or (m 1) (m 2))))
27+
28+
(defn- response-charset [response]
29+
(some->> (:headers response)
30+
(some #(when (.equalsIgnoreCase "content-type" (key %)) (val %)))
31+
(find-charset-in-content-type)))
32+
33+
(defn- response-writer ^Writer [response output-stream]
34+
(if-let [charset (response-charset response)]
1735
(io/writer output-stream :encoding charset)
1836
(io/writer output-stream)))
1937

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World
File renamed without changes.

ring-core/project.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:license {:name "The MIT License"
66
:url "http://opensource.org/licenses/MIT"}
77
:dependencies [[org.clojure/clojure "1.7.0"]
8+
[org.ring-clojure/ring-core-protocols "1.11.0"]
89
[org.ring-clojure/ring-websocket-protocols "1.11.0"]
910
[ring/ring-codec "1.2.0"]
1011
[commons-io "2.15.0"]

0 commit comments

Comments
 (0)