Skip to content

Commit 1338ca0

Browse files
committed
Added support for basic HTTP authentication.
URLs of the form: http://user:[email protected]/ now have the credentials sent along in an Authorization header.
1 parent e2b7fa1 commit 1338ca0

File tree

1 file changed

+12
-3
lines changed

1 file changed

+12
-3
lines changed

src/clojure/http/client.clj

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
(ns clojure.http.client
22
(:use [clojure.contrib.java-utils :only [as-str]]
33
[clojure.contrib.duck-streams :only [read-lines spit]]
4-
[clojure.contrib.str-utils :only [str-join]])
4+
[clojure.contrib.str-utils :only [str-join]]
5+
[clojure.contrib.base64 :as base64])
56
(:import (java.net URL
67
URLEncoder
78
HttpURLConnection)
@@ -106,8 +107,9 @@ by a server."
106107
[u & [method headers cookies body]]
107108
;; This function *should* throw an exception on non-HTTP URLs.
108109
;; This will happen if the cast fails.
109-
(let [#^HttpURLConnection connection
110-
(cast HttpURLConnection (.openConnection (url u)))
110+
(let [u (url u)
111+
#^HttpURLConnection connection
112+
(cast HttpURLConnection (.openConnection u))
111113
method (.toUpperCase #^String (as-str (or method
112114
"GET")))]
113115
(.setRequestMethod connection method)
@@ -122,6 +124,13 @@ by a server."
122124
(.setRequestProperty connection
123125
"Cookie"
124126
(create-cookie-string cookies)))
127+
128+
(when (.getUserInfo u)
129+
(.setRequestProperty connection
130+
"Authorization"
131+
(str "Basic "
132+
(base64/encode-str (.getUserInfo u)))))
133+
125134
(if body
126135
(send-body body connection headers)
127136
(.connect connection))

0 commit comments

Comments
 (0)