Skip to content

Commit 5f08aaf

Browse files
author
dnolen
committed
builtin webserver now gzips when run standalone
1 parent 7c71115 commit 5f08aaf

File tree

2 files changed

+32
-17
lines changed

2 files changed

+32
-17
lines changed

src/main/clojure/cljs/repl/browser.clj

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@
132132
"<script src=\"" output-to "\"></script>"
133133
"</body></html>"))
134134

135-
(defn send-static [{path :path :as request} conn {:keys [static-dir host port] :as opts}]
135+
(defn send-static [{path :path :as request} conn {:keys [static-dir host port gzip?] :as opts}]
136136
(if (and static-dir (not= "/favicon.ico" path))
137137
(let [path (if (= "/" path) "/index.html" path)
138138
local-path
@@ -157,13 +157,9 @@
157157
local-path
158158
(if-let [ext (some #(if (.endsWith path %) %) (keys ext->mime-type))]
159159
(let [mime-type (ext->mime-type ext "text/plain")
160-
encoding (mime-type->encoding mime-type "UTF-8")]
161-
(server/send-and-close
162-
conn
163-
200
164-
(slurp local-path :encoding encoding)
165-
mime-type
166-
encoding))
160+
encoding (mime-type->encoding mime-type "UTF-8")]
161+
(server/send-and-close conn 200 (slurp local-path :encoding encoding)
162+
mime-type encoding (and gzip? (= "text/javascript" mime-type))))
167163
(server/send-and-close conn 200 (slurp local-path) "text/plain"))
168164
;; "/index.html" doesn't exist, provide our own
169165
(= path "/index.html")
@@ -282,7 +278,8 @@
282278
server/state (atom {:socket nil :connection nil :promised-conn nil})]
283279
(server/start
284280
(merge opts
285-
{:static-dir (cond-> ["." "out/"] output-dir (conj output-dir))}))))
281+
{:static-dir (cond-> ["." "out/"] output-dir (conj output-dir))
282+
:gzip? true}))))
286283

287284
;; =============================================================================
288285
;; BrowserEnv

src/main/clojure/cljs/repl/server.clj

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
(:require [clojure.string :as str])
1212
(:import java.io.BufferedReader
1313
java.io.InputStreamReader
14+
java.io.ByteArrayOutputStream
15+
java.util.zip.GZIPOutputStream
1416
java.net.ServerSocket))
1517

1618
(def ^:dynamic state nil)
@@ -130,6 +132,18 @@
130132
404 "HTTP/1.1 404 Not Found"
131133
"HTTP/1.1 500 Error"))
132134

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+
133147
(defn send-and-close
134148
"Use the passed connection to send a form to the browser. Send a
135149
proper HTTP response."
@@ -138,16 +152,20 @@
138152
([conn status form content-type]
139153
(send-and-close conn status form content-type "UTF-8"))
140154
([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)
142158
content-length (count byte-form)
143159
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 "")))]
151169
(with-open [os (.getOutputStream conn)]
152170
(doseq [header headers]
153171
(.write os header 0 (count header)))

0 commit comments

Comments
 (0)