Skip to content

Commit

Permalink
Added support for basic HTTP authentication.
Browse files Browse the repository at this point in the history
URLs of the form:

  http://user:pass@example.com/

now have the credentials sent along in an Authorization header.
  • Loading branch information
marktriggs committed Nov 30, 2009
1 parent e2b7fa1 commit 1338ca0
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/clojure/http/client.clj
@@ -1,7 +1,8 @@
(ns clojure.http.client
(:use [clojure.contrib.java-utils :only [as-str]]
[clojure.contrib.duck-streams :only [read-lines spit]]
[clojure.contrib.str-utils :only [str-join]])
[clojure.contrib.str-utils :only [str-join]]
[clojure.contrib.base64 :as base64])
(:import (java.net URL
URLEncoder
HttpURLConnection)
Expand Down Expand Up @@ -106,8 +107,9 @@ by a server."
[u & [method headers cookies body]]
;; This function *should* throw an exception on non-HTTP URLs.
;; This will happen if the cast fails.
(let [#^HttpURLConnection connection
(cast HttpURLConnection (.openConnection (url u)))
(let [u (url u)
#^HttpURLConnection connection
(cast HttpURLConnection (.openConnection u))
method (.toUpperCase #^String (as-str (or method
"GET")))]
(.setRequestMethod connection method)
Expand All @@ -122,6 +124,13 @@ by a server."
(.setRequestProperty connection
"Cookie"
(create-cookie-string cookies)))

(when (.getUserInfo u)
(.setRequestProperty connection
"Authorization"
(str "Basic "
(base64/encode-str (.getUserInfo u)))))

(if body
(send-body body connection headers)
(.connect connection))
Expand Down

0 comments on commit 1338ca0

Please sign in to comment.