Permalink
Browse files

initial commit

  • Loading branch information...
0 parents commit 2f0f1aeb2752599b1902f14000ce7806d5d46b08 @mikejs committed Sep 14, 2010
Showing with 49 additions and 0 deletions.
  1. +4 −0 .gitignore
  2. +17 −0 README
  3. +4 −0 project.clj
  4. +24 −0 src/ring/middleware/gzip.clj
@@ -0,0 +1,4 @@
+pom.xml
+*jar
+lib
+classes
17 README
@@ -0,0 +1,17 @@
+# ring-gzip-middleware
+
+FIXME: write description
+
+## Usage
+
+FIXME: write
+
+## Installation
+
+FIXME: write
+
+## License
+
+Copyright (C) 2010 FIXME
+
+Distributed under the Eclipse Public License, the same as Clojure.
@@ -0,0 +1,4 @@
+(defproject org.clojars.mikejs/ring-gzip-middleware "0.1.0-SNAPSHOT"
+ :description "Ring gzip encoding middleware"
+ :dependencies [[org.clojure/clojure "1.2.0"]
+ [org.clojure/clojure-contrib "1.2.0"]])
@@ -0,0 +1,24 @@
+(ns ring.middleware.gzip
+ (:import (java.util.zip GZIPOutputStream)
+ (java.io ByteArrayInputStream ByteArrayOutputStream)))
+
+(defn gzipped-response [resp]
+ (let [bout (ByteArrayOutputStream.)
+ out (GZIPOutputStream. bout)
+ headers (assoc (resp :headers) "content-encoding" "gzip")]
+ (.write out (.getBytes (resp :body)))
+ (.close out)
+ (merge resp {:body (ByteArrayInputStream. (.toByteArray bout))
+ :headers headers})))
+
+(defn wrap-gzip [handler]
+ (fn [req]
+ (let [resp (handler req)]
+ (if (string? (resp :body))
+ (let [accepts (get (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))))
+ (gzipped-response resp)
+ resp))
+ resp))))

0 comments on commit 2f0f1ae

Please sign in to comment.