Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
mikejs committed Sep 14, 2010
0 parents commit 2f0f1ae
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pom.xml
*jar
lib
classes
17 changes: 17 additions & 0 deletions README
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 4 additions & 0 deletions project.clj
Original file line number Diff line number Diff line change
@@ -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"]])
24 changes: 24 additions & 0 deletions src/ring/middleware/gzip.clj
Original file line number Diff line number Diff line change
@@ -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.