forked from clj-commons/ring-gzip-middleware
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 2f0f1ae
Showing
4 changed files
with
49 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
pom.xml | ||
*jar | ||
lib | ||
classes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)))) |