|
20 | 20 | [cljs.repl.server :as server] |
21 | 21 | [cljs.stacktrace :as st] |
22 | 22 | [cljs.analyzer :as ana] |
23 | | - [cljs.build.api :as build]) |
| 23 | + [cljs.build.api :as build] |
| 24 | + [clojure.string :as str]) |
24 | 25 | (:import [java.util.concurrent Executors ConcurrentHashMap])) |
25 | 26 |
|
26 | 27 | (def ^:dynamic browser-state nil) |
|
140 | 141 | "<script src=\"" output-to "\"></script>" |
141 | 142 | "</body></html>")) |
142 | 143 |
|
| 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 | + |
143 | 153 | (defn send-static |
144 | 154 | [{path :path :as request} conn |
145 | 155 | {:keys [static-dir output-to output-dir host port gzip?] :or {output-dir "out"} :as opts}] |
|
165 | 175 | local-path)] |
166 | 176 | (cond |
167 | 177 | 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)))) |
174 | 182 | ;; "/index.html" doesn't exist, provide our own |
175 | 183 | (= path "/index.html") |
176 | 184 | (server/send-and-close conn 200 |
|
203 | 211 |
|
204 | 212 | (server/dispatch-on :get |
205 | 213 | (fn [{:keys [path]} _ _] |
206 | | - (or (= path "/") (some #(.endsWith path %) (keys ext->mime-type)))) |
| 214 | + (or (= path "/") (path->mime-type ext->mime-type path nil))) |
207 | 215 | send-static) |
208 | 216 |
|
209 | 217 | (defmulti handle-post (fn [m _ _ ] (:type m))) |
|
0 commit comments