Skip to content

Commit fee5527

Browse files
kovasbdnolen
authored andcommitted
CLJS-1206: Images in HTML don't show up when served from localhost:9000
1 parent 03529c4 commit fee5527

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

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

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
".cljc" "text/x-clojure"
3838
".map" "application/json"})
3939

40+
(def mime-type->encoding
41+
{"text/html" "UTF-8"
42+
"text/css" "UTF-8"
43+
"image/jpeg" "ISO-8859-1"
44+
"image/png" "ISO-8859-1"
45+
"image/gif" "ISO-8859-1"
46+
"text/javascript" "UTF-8"
47+
"text/x-clojure" "UTF-8"
48+
"application/json" "UTF-8"})
49+
4050
(defn- set-return-value-fn
4151
"Save the return value function which will be called when the next
4252
return value is received."
@@ -96,10 +106,16 @@
96106
:else nil)
97107
local-path)]
98108
(if local-path
99-
(server/send-and-close conn 200 (slurp local-path)
100-
(if (some #(.endsWith path %) (keys ext->mime-type))
101-
(get ext->mime-type path)
102-
"text/plain"))
109+
(if-let [ext (some #(if (.endsWith path %) %) (keys ext->mime-type))]
110+
(let [mime-type (ext->mime-type ext "text/plain")
111+
encoding (mime-type->encoding mime-type "UTF-8")]
112+
(server/send-and-close
113+
conn
114+
200
115+
(slurp local-path :encoding encoding)
116+
mime-type
117+
encoding))
118+
(server/send-and-close conn 200 (slurp local-path) "text/plain"))
103119
(server/send-404 conn path)))
104120
(server/send-404 conn path)))
105121

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,20 +108,22 @@
108108
([conn status form]
109109
(send-and-close conn status form "text/html"))
110110
([conn status form content-type]
111-
(let [utf-8-form (.getBytes form "UTF-8")
112-
content-length (count utf-8-form)
111+
(send-and-close conn status form content-type "UTF-8"))
112+
([conn status form content-type encoding]
113+
(let [byte-form (.getBytes form encoding)
114+
content-length (count byte-form)
113115
headers (map #(.getBytes (str % "\r\n"))
114116
[(status-line status)
115117
"Server: ClojureScript REPL"
116118
(str "Content-Type: "
117119
content-type
118-
"; charset=utf-8")
120+
"; charset=" encoding)
119121
(str "Content-Length: " content-length)
120122
""])]
121123
(with-open [os (.getOutputStream conn)]
122124
(doseq [header headers]
123125
(.write os header 0 (count header)))
124-
(.write os utf-8-form 0 content-length)
126+
(.write os byte-form 0 content-length)
125127
(.flush os)
126128
(.close conn)))))
127129

0 commit comments

Comments
 (0)