Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added support for basic HTTP authentication.

URLs of the form:

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

now have the credentials sent along in an Authorization header.
  • Loading branch information...
commit 1338ca03f11a9927aa96231ab07e6d344eed92f1 1 parent e2b7fa1
Mark Triggs authored November 30, 2009

Showing 1 changed file with 12 additions and 3 deletions. Show diff stats Hide diff stats

  1. 15  src/clojure/http/client.clj
15  src/clojure/http/client.clj
... ...
@@ -1,7 +1,8 @@
1 1
 (ns clojure.http.client
2 2
   (:use [clojure.contrib.java-utils :only [as-str]]
3 3
         [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])
5 6
   (:import (java.net URL
6 7
                      URLEncoder
7 8
                      HttpURLConnection)
@@ -106,8 +107,9 @@ by a server."
106 107
   [u & [method headers cookies body]]
107 108
   ;; This function *should* throw an exception on non-HTTP URLs.
108 109
   ;; 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))
111 113
         method (.toUpperCase #^String (as-str (or method
112 114
                                                   "GET")))]
113 115
     (.setRequestMethod connection method)
@@ -122,6 +124,13 @@ by a server."
122 124
       (.setRequestProperty connection
123 125
                            "Cookie"
124 126
                            (create-cookie-string cookies)))
  127
+
  128
+    (when (.getUserInfo u)
  129
+      (.setRequestProperty connection
  130
+                           "Authorization"
  131
+                           (str "Basic "
  132
+                                (base64/encode-str (.getUserInfo u)))))
  133
+
125 134
     (if body
126 135
       (send-body body connection headers)
127 136
       (.connect connection))

0 notes on commit 1338ca0

Please sign in to comment.
Something went wrong with that request. Please try again.