Skip to content

Commit ab00c86

Browse files
slipsetdnolen
authored andcommitted
CLJS-2760 Make browser repl web-severs mime-type case-insensitive
- introduce a function `path->mime-type` which calculates the mime-type for a given path - use this function and simplify the send-static function
1 parent 2ad1470 commit ab00c86

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@
2020
[cljs.repl.server :as server]
2121
[cljs.stacktrace :as st]
2222
[cljs.analyzer :as ana]
23-
[cljs.build.api :as build])
23+
[cljs.build.api :as build]
24+
[clojure.string :as str])
2425
(:import [java.util.concurrent Executors ConcurrentHashMap]))
2526

2627
(def ^:dynamic browser-state nil)
@@ -140,6 +141,15 @@
140141
"<script src=\"" output-to "\"></script>"
141142
"</body></html>"))
142143

144+
(defn- path->mime-type [ext->mime-type path default]
145+
(let [lc-path (str/lower-case path)
146+
last-dot (.lastIndexOf path ".")]
147+
(if (pos? last-dot)
148+
(-> lc-path
149+
(subs last-dot)
150+
(ext->mime-type default))
151+
default)))
152+
143153
(defn send-static
144154
[{path :path :as request} conn
145155
{:keys [static-dir output-to output-dir host port gzip?] :or {output-dir "out"} :as opts}]
@@ -165,12 +175,10 @@
165175
local-path)]
166176
(cond
167177
local-path
168-
(if-let [ext (some #(if (.endsWith path %) %) (keys ext->mime-type))]
169-
(let [mime-type (ext->mime-type ext "text/plain")
170-
encoding (mime-type->encoding mime-type "UTF-8")]
171-
(server/send-and-close conn 200 (slurp local-path :encoding encoding)
172-
mime-type encoding (and gzip? (= "text/javascript" mime-type))))
173-
(server/send-and-close conn 200 (slurp local-path) "text/plain"))
178+
(let [mime-type (path->mime-type ext->mime-type path "text/plain")
179+
encoding (mime-type->encoding mime-type "UTF-8")]
180+
(server/send-and-close conn 200 (slurp local-path :encoding encoding)
181+
mime-type encoding (and gzip? (= "text/javascript" mime-type))))
174182
;; "/index.html" doesn't exist, provide our own
175183
(= path "/index.html")
176184
(server/send-and-close conn 200
@@ -203,7 +211,7 @@
203211

204212
(server/dispatch-on :get
205213
(fn [{:keys [path]} _ _]
206-
(or (= path "/") (some #(.endsWith path %) (keys ext->mime-type))))
214+
(or (= path "/") (path->mime-type ext->mime-type path nil)))
207215
send-static)
208216

209217
(defmulti handle-post (fn [m _ _ ] (:type m)))

0 commit comments

Comments
 (0)