|
60 | 60 | (dissoc "Content-Length")))
|
61 | 61 | (update-in [:body] piped-gzipped-input-stream)))
|
62 | 62 |
|
63 |
| -(defn wrap-gzip [handler] |
64 |
| - (fn [req] |
65 |
| - (let [{:keys [body status] :as resp} (handler req)] |
66 |
| - (if (and (= status 200) |
67 |
| - (not (get-in resp [:headers "Content-Encoding"])) |
68 |
| - (or |
69 |
| - (and (string? body) (> (count body) 200)) |
70 |
| - (and (seq? body) @flushable-gzip?) |
71 |
| - (instance? InputStream body) |
72 |
| - (instance? File body))) |
73 |
| - (let [accepts (get-in req [:headers "accept-encoding"] "") |
74 |
| - match (re-find #"(gzip|\*)(;q=((0|1)(.\d+)?))?" accepts)] |
75 |
| - (if (and match (not (contains? #{"0" "0.0" "0.00" "0.000"} |
76 |
| - (match 3)))) |
77 |
| - (gzipped-response resp) |
78 |
| - resp)) |
79 |
| - resp)))) |
| 63 | +(defn- gzip-response [req {:keys [body status] :as resp}] |
| 64 | + (if (and (= status 200) |
| 65 | + (not (get-in resp [:headers "Content-Encoding"])) |
| 66 | + (or |
| 67 | + (and (string? body) (> (count body) 200)) |
| 68 | + (and (seq? body) @flushable-gzip?) |
| 69 | + (instance? InputStream body) |
| 70 | + (instance? File body))) |
| 71 | + (let [accepts (get-in req [:headers "accept-encoding"] "") |
| 72 | + match (re-find #"(gzip|\*)(;q=((0|1)(.\d+)?))?" accepts)] |
| 73 | + (if (and match (not (contains? #{"0" "0.0" "0.00" "0.000"} |
| 74 | + (match 3)))) |
| 75 | + (gzipped-response resp) |
| 76 | + resp)) |
| 77 | + resp)) |
| 78 | + |
| 79 | +(defn wrap-gzip |
| 80 | + "Ring middleware that GZIPs response if client can handle it." |
| 81 | + [handler] |
| 82 | + (fn |
| 83 | + ([request] |
| 84 | + (gzip-response request (handler request))) |
| 85 | + ([request respond raise] |
| 86 | + (handler |
| 87 | + request |
| 88 | + (fn [response] |
| 89 | + (respond (gzip-response request response))) |
| 90 | + raise)))) |
0 commit comments