|
11 | 11 | (:require [clojure.string :as str])
|
12 | 12 | (:import java.io.BufferedReader
|
13 | 13 | java.io.InputStreamReader
|
| 14 | + java.io.ByteArrayOutputStream |
| 15 | + java.util.zip.GZIPOutputStream |
14 | 16 | java.net.ServerSocket))
|
15 | 17 |
|
16 | 18 | (def ^:dynamic state nil)
|
|
130 | 132 | 404 "HTTP/1.1 404 Not Found"
|
131 | 133 | "HTTP/1.1 500 Error"))
|
132 | 134 |
|
| 135 | +(defn ^bytes gzip [^bytes bytes] |
| 136 | + (let [baos (ByteArrayOutputStream. (count bytes))] |
| 137 | + (try |
| 138 | + (let [gzos (GZIPOutputStream. baos)] |
| 139 | + (try |
| 140 | + (.write gzos bytes) |
| 141 | + (finally |
| 142 | + (.close gzos)))) |
| 143 | + (finally |
| 144 | + (.close baos))) |
| 145 | + (.toByteArray baos))) |
| 146 | + |
133 | 147 | (defn send-and-close
|
134 | 148 | "Use the passed connection to send a form to the browser. Send a
|
135 | 149 | proper HTTP response."
|
|
138 | 152 | ([conn status form content-type]
|
139 | 153 | (send-and-close conn status form content-type "UTF-8"))
|
140 | 154 | ([conn status form content-type encoding]
|
141 |
| - (let [byte-form (.getBytes form encoding) |
| 155 | + (send-and-close conn status form content-type encoding false)) |
| 156 | + ([conn status form content-type encoding gzip?] |
| 157 | + (let [byte-form (cond-> (.getBytes form encoding) gzip? gzip) |
142 | 158 | content-length (count byte-form)
|
143 | 159 | headers (map #(.getBytes (str % "\r\n"))
|
144 |
| - [(status-line status) |
145 |
| - "Server: ClojureScript REPL" |
146 |
| - (str "Content-Type: " |
147 |
| - content-type |
148 |
| - "; charset=" encoding) |
149 |
| - (str "Content-Length: " content-length) |
150 |
| - ""])] |
| 160 | + (cond-> |
| 161 | + [(status-line status) |
| 162 | + "Server: ClojureScript REPL" |
| 163 | + (str "Content-Type: " |
| 164 | + content-type |
| 165 | + "; charset=" encoding) |
| 166 | + (str "Content-Length: " content-length)] |
| 167 | + gzip? (conj "Content-Encoding: gzip") |
| 168 | + true (conj "")))] |
151 | 169 | (with-open [os (.getOutputStream conn)]
|
152 | 170 | (doseq [header headers]
|
153 | 171 | (.write os header 0 (count header)))
|
|
0 commit comments