33 [clojure.contrib.duck-streams :only [read-lines spit]]
44 [clojure.contrib.str-utils :only [str-join]])
55 (:import (java.net URL URLEncoder)
6- (java.io StringReader)))
6+ (java.io StringReader InputStream )))
77
88(def default-headers {" User-Agent" (str " Clojure/" (clojure-version )
99 " (+http://clojure.org)" ),
@@ -15,13 +15,34 @@ representation of text."
1515 [text]
1616 (URLEncoder/encode text " UTF-8" ))
1717
18- (defn- encode-body
19- " Turns a map into a URL-encoded string suitable for a request body,
20- or just send it verbatim if it's a string."
18+ (defn- encode-body-map
19+ " Turns a map into a URL-encoded string suitable for a request body."
2120 [body]
22- (if (string? body)
23- body
24- (str-join " &" (map #(str-join " =" (map url-encode %)) body))))
21+ (str-join " &" (map #(str-join " =" (map url-encode %)) body)))
22+
23+ (defn- send-body
24+ [body connection headers]
25+ (.setDoOutput connection true )
26+ ; ; this isn't perfect, since it doesn't account for
27+ ; ; different capitalization etc
28+ (when (and (map? body)
29+ (not (contains? headers " Content-Type" )))
30+ (.setRequestProperty connection
31+ " Content-Type"
32+ " application/x-www-form-urlencoded" ))
33+
34+ (.connect connection)
35+
36+ (let [out (.getOutputStream connection)]
37+ (cond
38+ (string? body) (spit out body)
39+ (map? body) (spit out (encode-body-map body))
40+ (instance? InputStream body) (let [bytes (make-array Byte/TYPE 1000 )]
41+ (loop [bytes-read (.read body bytes)]
42+ (when (pos? bytes-read)
43+ (.write out bytes 0 bytes-read)
44+ (recur (.read body bytes))))))
45+ (.close out)))
2546
2647(defn url
2748 " If u is an instance of java.net.URL then returns it without
@@ -90,17 +111,7 @@ by a server."
90111 " Cookie"
91112 (create-cookie-string cookies)))
92113 (if body
93- (do
94- (.setDoOutput connection true )
95- ; ; this isn't perfect, since it doesn't account for
96- ; ; different capitalization etc
97- (when-not (contains? headers " Content-Type" )
98- (.setRequestProperty connection
99- " Content-Type"
100- " application/x-www-form-urlencoded" ))
101- (.connect connection)
102- (spit (.getOutputStream connection)
103- (encode-body body)))
114+ (send-body body connection headers)
104115 (.connect connection))
105116
106117 (let [headers (parse-headers connection)]
0 commit comments