Skip to content

Commit

Permalink
File support, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejs committed Sep 15, 2010
1 parent 8cb8bfe commit 25987d7
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions src/ring/middleware/gzip.clj
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
(ns ring.middleware.gzip
(:require [clojure.contrib.io :as io])
(:import (java.util.zip GZIPOutputStream)
(java.io InputStream ByteArrayInputStream ByteArrayOutputStream)))
(java.io InputStream
File
ByteArrayInputStream
ByteArrayOutputStream)))

(defn gzipped-response [resp]
(let [body (resp :body)
bout (ByteArrayOutputStream.)
out (GZIPOutputStream. bout)
headers (assoc (resp :headers) "content-encoding" "gzip")]
resp (assoc-in resp [:headers "content-encoding"] "gzip")]
(io/copy (resp :body) out)
(.close out)
(if (instance? InputStream body) (.close body))
(merge resp {:body (ByteArrayInputStream. (.toByteArray bout))
:headers headers})))
(if (instance? InputStream body)
(.close body))
(assoc resp :body (ByteArrayInputStream. (.toByteArray bout)))))

(defn wrap-gzip [handler]
(fn [req]
(let [resp (handler req)
body (resp :body)]
(if (and (= (resp :status) 200)
(not ((get resp :headers {}) "content-encoding"))
(let [{body :body
status :status
:as resp} (handler req)]
(if (and (= status 200)
(not (get-in resp [:headers "content-encoding"]))
(or
(and (string? body) (> (count body) 200))
(instance? InputStream body)))
(let [accepts (get (req :headers) "accept-encoding" "")
(instance? InputStream body)
(instance? File body)))
(let [accepts (get-in req [:headers "accept-encoding"] "")
match (re-find #"(gzip|\*)(;q=((0|1)(.\d+)?))?" accepts)]
(if (and match (not (contains? #{"0" "0.0" "0.00" "0.000"}
(match 3))))
Expand Down

0 comments on commit 25987d7

Please sign in to comment.