Skip to content

Commit cb052ed

Browse files
committed
Merged changes from tomo fixing cookie/header behaviour.
2 parents 88867f1 + 6becb1a commit cb052ed

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

src/clojure/http/client.clj

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
(.setProperty "http.proxyHost" host)
2121
(.setProperty "http.proxyPort" (str port)))
2222
nil)
23-
23+
2424
(defn url-encode
2525
"Wrapper around java.net.URLEncoder returning a (UTF-8) URL encoded
2626
representation of argument, either a string or map."
@@ -77,7 +77,7 @@ or the error stream of connection, whichever is appropriate."
7777
"Returns a map of the response headers from connection."
7878
[#^HttpURLConnection connection]
7979
(let [hs (.getHeaderFields connection)]
80-
(into {} (for [[k v] hs :when k] [k (first v)]))))
80+
(into {} (for [[k v] hs :when k] [(keyword (.toLowerCase k)) (seq v)]))))
8181

8282
(defn- parse-cookies
8383
"Returns a map of cookies when given the Set-Cookie string sent
@@ -86,7 +86,7 @@ by a server."
8686
(when cookie-string
8787
(into {}
8888
(for [#^String cookie (.split cookie-string ";")]
89-
(let [keyval (map (fn [#^String x] (.trim x)) (.split cookie "="))]
89+
(let [keyval (map (fn [#^String x] (.trim x)) (.split cookie "=" 2))]
9090
[(first keyval) (second keyval)])))))
9191

9292
(defn- create-cookie-string
@@ -102,12 +102,10 @@ by a server."
102102
(defn request
103103
"Perform an HTTP request on URL u."
104104
[u & [method headers cookies body]]
105-
106105
;; This function *should* throw an exception on non-HTTP URLs.
107106
;; This will happen if the cast fails.
108107
(let [#^HttpURLConnection connection
109108
(cast HttpURLConnection (.openConnection (url u)))
110-
111109
method (.toUpperCase #^String (as-str (or method
112110
"GET")))]
113111
(.setRequestMethod connection method)
@@ -121,7 +119,6 @@ by a server."
121119
(.setRequestProperty connection
122120
"Cookie"
123121
(create-cookie-string cookies)))
124-
125122
(if body
126123
(send-body body connection headers)
127124
(.connect connection))
@@ -131,8 +128,8 @@ by a server."
131128
:code (.getResponseCode connection)
132129
:msg (.getResponseMessage connection)
133130
:method method
134-
:headers (dissoc headers "Set-Cookie")
131+
:headers (dissoc headers :set-cookie)
135132
;; This correctly implements case-insensitive lookup.
136133
:get-header #(.getHeaderField connection #^String (as-str %))
137-
:cookies (parse-cookies (headers "Set-Cookie"))
134+
:cookies (apply merge (map parse-cookies (headers :set-cookie)))
138135
:url (str (.getURL connection))})))

0 commit comments

Comments
 (0)