Skip to content

Commit

Permalink
Change wrap-not-modified to remove Content-Length
Browse files Browse the repository at this point in the history
Fixes #509.
  • Loading branch information
weavejester committed Oct 19, 2024
1 parent eca2462 commit 1bb3119
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
9 changes: 7 additions & 2 deletions ring-core/src/ring/middleware/not_modified.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"Middleware that returns a 304 Not Modified response for responses with
Last-Modified headers."
(:require [ring.util.time :refer [parse-date]]
[ring.util.response :refer [get-header header]]
[ring.util.response :refer [get-header find-header header]]
[ring.util.io :refer [close!]]))

(defn- etag-match? [request response]
Expand Down Expand Up @@ -35,6 +35,11 @@
(or (not-modified-since? request response)
(etag-match? request response)))))

(defn- dissoc-header [response header]
(if-some [[k _] (find-header response header)]
(update response :headers dissoc k)
response))

(defn not-modified-response
"Returns 304 or original response based on response and request.
See: wrap-not-modified."
Expand All @@ -46,7 +51,7 @@
(do (close! (:body response))
(-> response
(assoc :status 304)
(header "Content-Length" 0)
(dissoc-header "Content-Length")
(assoc :body nil)))
response))

Expand Down
17 changes: 15 additions & 2 deletions ring-core/test/ring/middleware/test/not_modified.clj
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@
last-modified "Sun, 23 Sep 2012 11:00:00 GMT"
h-resp {:status 200 :headers {"Last-Modified" last-modified} :body ""}
resp (not-modified-response h-resp (req last-modified))]
(is (= 304 (:status resp)))
(is (nil? (:body resp)))
(is (= (get-in resp [:headers "Content-Length"]) "0"))))
(is (nil? (get-in resp [:headers "Content-Length"])))))

(testing "no modification info"
(let [response {:status 200 :headers {} :body ""}]
Expand Down Expand Up @@ -111,4 +112,16 @@
200 304
302 302
404 404
500 500))))
500 500)))

(testing "content-type header is removed"
(let [req #(hash-map :request-method :get :headers {"if-modified-since" %})
last-modified "Sun, 23 Sep 2012 11:00:00 GMT"
h-resp {:status 200
:headers {"Last-Modified" last-modified
"Content-Length" "11"}
:body "hello world"}
resp (not-modified-response h-resp (req last-modified))]
(is (= 304 (:status resp)))
(is (nil? (:body resp)))
(is (nil? (get-in resp [:headers "Content-Length"]))))))

0 comments on commit 1bb3119

Please sign in to comment.