Skip to content

Commit 5e1ca77

Browse files
potetmswannodette
authored andcommitted
Properly parse url string into file-path, query-string, and ref.
1 parent 41326b9 commit 5e1ca77

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

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

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@
6060
(fn [old]
6161
(update-in old [method] #(conj (vec %) m))))))
6262

63+
(defn parse-file-parts [file]
64+
;; This is a port of java.net.URL.Parts, which is package private.
65+
(let [ref-idx (str/index-of file "#")
66+
[file ref] (if ref-idx
67+
[(subs file 0 ref-idx) (subs file (inc ref-idx))]
68+
[file nil])
69+
q-idx (str/last-index-of file \?)]
70+
(merge {:ref ref}
71+
(if q-idx
72+
{:path (subs file 0 q-idx)
73+
:query-str (subs file (inc q-idx))}
74+
{:path file}))))
75+
6376
;;; assumes first line already consumed
6477
(defn parse-headers
6578
"Parse the headers of an HTTP POST request."
@@ -80,21 +93,27 @@
8093
(conj header-lines next-line)))))
8194

8295
(defn read-post [line rdr]
83-
(let [[_ path _] (str/split line #" ")
96+
(let [[_ file _] (str/split line #" ")
97+
{:keys [path ref query-str]} (parse-file-parts file)
8498
headers (parse-headers (read-headers rdr))
8599
content-length (Integer/parseInt (:content-length headers))
86100
content (char-array content-length)]
87101
(io! (.read rdr content 0 content-length)
88102
{:method :post
89103
:path path
104+
:ref ref
105+
:query-str query-str
90106
:headers headers
91107
:content (String. content)})))
92108

93109
(defn read-get [line rdr]
94-
(let [[_ path _] (str/split line #" ")
110+
(let [[_ file _] (str/split line #" ")
111+
{:keys [path ref query-str]} (parse-file-parts file)
95112
headers (parse-headers (read-headers rdr))]
96113
{:method :get
97114
:path path
115+
:ref ref
116+
:query-str query-str
98117
:headers headers}))
99118

100119
(defn read-request [rdr]

0 commit comments

Comments
 (0)