Skip to content
This repository was archived by the owner on Jan 23, 2018. It is now read-only.

Commit 18f15bc

Browse files
committed
Merge branch 'master' of git://github.com/technomancy/clojure-http-client
Conflicts: src/clojure/http/client.clj
2 parents 1e68635 + 7e1e501 commit 18f15bc

File tree

4 files changed

+69
-26
lines changed

4 files changed

+69
-26
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
1-
target
1+
target
2+
local.properties
3+
classes
4+
*.jar

README.markdown

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ more towards interactions with REST-based APIs.
2121
(resourcefully/put "http://localhost:5984/my-db/doc1"
2222
{} (json-str {:hello "world"}))
2323

24-
(resourcefully/with-cookies
24+
(resourcefully/with-cookies {}
2525
(resourcefully/post "http://localhost:3000/login"
2626
{} {"user" user "password" password})
2727
(resourcefully/get "http://localhost:3000/my-secret-page))

build.xml

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<project name="saxon" default="jar">
2+
<description>
3+
Clojure HTTP Client.
4+
</description>
5+
6+
<property name="build.dir" location="classes"/>
7+
<property name="source.dir" location="src"/>
8+
<property name="jar.name" location="clojure-http-client.jar"/>
9+
<property file="local.properties"/>
10+
<available property="hasclojure" file="${clojure.jar}"/>
11+
<available property="hascontrib" file="${contrib.jar}"/>
12+
13+
<target name="checkforjars" depends="init">
14+
<condition property="hasjars">
15+
<and>
16+
<isset property="hasclojure"/>
17+
<isset property="hascontrib"/>
18+
</and>
19+
</condition>
20+
</target>
21+
22+
<target name="clean" description="Remove generated classfiles">
23+
<delete dir="${build.dir}"/>
24+
</target>
25+
26+
<target name="init" depends="clean">
27+
<tstamp/>
28+
<mkdir dir="${build.dir}"/>
29+
</target>
30+
31+
<target name="compile" depends="checkforjars" description="Compile sources" if="hasjars">
32+
<java classname="clojure.lang.Compile">
33+
<classpath>
34+
<path location="${clojure.jar}"/>
35+
<path location="${contrib.jar}"/>
36+
<path location="${build.dir}"/>
37+
<path location="${source.dir}"/>
38+
</classpath>
39+
<sysproperty key="clojure.compile.path" value="${build.dir}"/>
40+
<arg value="clojure.http.client"/>
41+
<arg value="clojure.http.resourcefully"/>
42+
</java>
43+
</target>
44+
45+
<target name="jar" description="Create jar file" depends="compile">
46+
<jar jarfile="${jar.name}" index="true">
47+
<path location="MIT.txt"/>
48+
<fileset dir="${source.dir}" includes="**/*.clj"/>
49+
<fileset dir="${build.dir}" includes="**/*.class"/>
50+
</jar>
51+
</target>
52+
53+
</project>

src/clojure/http/client.clj

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,11 @@
1111

1212
(defn url-encode
1313
"Wrapper around java.net.URLEncoder returning a (UTF-8) URL encoded
14-
representation of text."
15-
[text]
16-
(URLEncoder/encode text "UTF-8"))
17-
18-
(defn encode-body-map
19-
"Turns a map into a URL-encoded string suitable for a request body."
20-
[body]
21-
(str-join "&"
22-
(map (fn [m]
23-
(str-join "="
24-
(map #(url-encode (as-str %))
25-
m)))
26-
body)))
14+
representation of argument, either a string or map."
15+
[arg]
16+
(if (map? arg)
17+
(str-join \& (map #(str-join \= (map url-encode %)) arg))
18+
(URLEncoder/encode (as-str arg) "UTF-8")))
2719

2820
(defn- send-body
2921
[body connection headers]
@@ -41,7 +33,7 @@ representation of text."
4133
(let [out (.getOutputStream connection)]
4234
(cond
4335
(string? body) (spit out body)
44-
(map? body) (spit out (encode-body-map body))
36+
(map? body) (spit out (url-encode body))
4537
(instance? InputStream body) (let [bytes (make-array Byte/TYPE 1000)]
4638
(loop [bytes-read (.read body bytes)]
4739
(when (pos? bytes-read)
@@ -71,22 +63,17 @@ or the error stream of connection, whichever is appropriate."
7163
"Returns a map of the response headers from connection."
7264
[connection]
7365
(let [hs (.getHeaderFields connection)]
74-
(apply merge (map (fn [e] (when-let [k (key e)]
75-
{k (first (val e))}))
76-
hs))))
66+
(into {} (for [[k v] hs :when k] [k (first v)]))))
7767

7868
(defn- parse-cookies
7969
"Returns a map of cookies when given the Set-Cookie string sent
8070
by a server."
8171
[cookie-string]
8272
(when cookie-string
83-
(apply merge
84-
(map (fn [cookie]
85-
(apply hash-map
86-
(map (fn [c]
87-
(.trim c))
88-
(.split cookie "="))))
89-
(.split cookie-string ";")))))
73+
(into {}
74+
(for [cookie (.split cookie-string ";")]
75+
(let [keyval (map #(.trim %) (.split cookie "="))]
76+
[(first keyval) (second keyval)])))))
9077

9178
(defn- create-cookie-string
9279
"Returns a string suitable for sending to the server in the

0 commit comments

Comments
 (0)