Skip to content

Commit 1bb3119

Browse files
committed
Change wrap-not-modified to remove Content-Length
Fixes #509.
1 parent eca2462 commit 1bb3119

File tree

2 files changed

+22
-4
lines changed

2 files changed

+22
-4
lines changed

ring-core/src/ring/middleware/not_modified.clj

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"Middleware that returns a 304 Not Modified response for responses with
33
Last-Modified headers."
44
(:require [ring.util.time :refer [parse-date]]
5-
[ring.util.response :refer [get-header header]]
5+
[ring.util.response :refer [get-header find-header header]]
66
[ring.util.io :refer [close!]]))
77

88
(defn- etag-match? [request response]
@@ -35,6 +35,11 @@
3535
(or (not-modified-since? request response)
3636
(etag-match? request response)))))
3737

38+
(defn- dissoc-header [response header]
39+
(if-some [[k _] (find-header response header)]
40+
(update response :headers dissoc k)
41+
response))
42+
3843
(defn not-modified-response
3944
"Returns 304 or original response based on response and request.
4045
See: wrap-not-modified."
@@ -46,7 +51,7 @@
4651
(do (close! (:body response))
4752
(-> response
4853
(assoc :status 304)
49-
(header "Content-Length" 0)
54+
(dissoc-header "Content-Length")
5055
(assoc :body nil)))
5156
response))
5257

ring-core/test/ring/middleware/test/not_modified.clj

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,9 @@
7171
last-modified "Sun, 23 Sep 2012 11:00:00 GMT"
7272
h-resp {:status 200 :headers {"Last-Modified" last-modified} :body ""}
7373
resp (not-modified-response h-resp (req last-modified))]
74+
(is (= 304 (:status resp)))
7475
(is (nil? (:body resp)))
75-
(is (= (get-in resp [:headers "Content-Length"]) "0"))))
76+
(is (nil? (get-in resp [:headers "Content-Length"])))))
7677

7778
(testing "no modification info"
7879
(let [response {:status 200 :headers {} :body ""}]
@@ -111,4 +112,16 @@
111112
200 304
112113
302 302
113114
404 404
114-
500 500))))
115+
500 500)))
116+
117+
(testing "content-type header is removed"
118+
(let [req #(hash-map :request-method :get :headers {"if-modified-since" %})
119+
last-modified "Sun, 23 Sep 2012 11:00:00 GMT"
120+
h-resp {:status 200
121+
:headers {"Last-Modified" last-modified
122+
"Content-Length" "11"}
123+
:body "hello world"}
124+
resp (not-modified-response h-resp (req last-modified))]
125+
(is (= 304 (:status resp)))
126+
(is (nil? (:body resp)))
127+
(is (nil? (get-in resp [:headers "Content-Length"]))))))

0 commit comments

Comments
 (0)