rlm@10
|
1 ;;; http/connection.clj: low-level HTTP client API around HttpURLConnection
|
rlm@10
|
2
|
rlm@10
|
3 ;; by Stuart Sierra, http://stuartsierra.com/
|
rlm@10
|
4 ;; June 8, 2009
|
rlm@10
|
5
|
rlm@10
|
6 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use
|
rlm@10
|
7 ;; and distribution terms for this software are covered by the Eclipse
|
rlm@10
|
8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
|
rlm@10
|
9 ;; which can be found in the file epl-v10.html at the root of this
|
rlm@10
|
10 ;; distribution. By using this software in any fashion, you are
|
rlm@10
|
11 ;; agreeing to be bound by the terms of this license. You must not
|
rlm@10
|
12 ;; remove this notice, or any other, from this software.
|
rlm@10
|
13
|
rlm@10
|
14 ;; DEPRECATED IN 1.2. Use direct Java bits, or take a look at
|
rlm@10
|
15 ;; http://github.com/technomancy/clojure-http-client
|
rlm@10
|
16
|
rlm@10
|
17 (ns ^{:deprecated "1.2"
|
rlm@10
|
18 :doc "Low-level HTTP client API around HttpURLConnection"}
|
rlm@10
|
19 clojure.contrib.http.connection
|
rlm@10
|
20 (:require [clojure.contrib.io :as duck])
|
rlm@10
|
21 (:import (java.net URI URL HttpURLConnection)
|
rlm@10
|
22 (java.io File InputStream Reader)))
|
rlm@10
|
23
|
rlm@10
|
24 (defn http-connection
|
rlm@10
|
25 "Opens an HttpURLConnection at the URL, handled by as-url."
|
rlm@10
|
26 [url]
|
rlm@10
|
27 (.openConnection (duck/as-url url)))
|
rlm@10
|
28
|
rlm@10
|
29 (defmulti
|
rlm@10
|
30 ^{:doc "Transmits a request entity body."}
|
rlm@10
|
31 send-request-entity (fn [conn entity] (type entity)))
|
rlm@10
|
32
|
rlm@10
|
33 (defmethod send-request-entity duck/*byte-array-type* [^HttpURLConnection conn entity]
|
rlm@10
|
34 (.setFixedLengthStreamingMode conn (count entity))
|
rlm@10
|
35 (.connect conn)
|
rlm@10
|
36 (duck/copy entity (.getOutputStream conn)))
|
rlm@10
|
37
|
rlm@10
|
38 (defmethod send-request-entity String [conn ^String entity]
|
rlm@10
|
39 (send-request-entity conn (.getBytes entity duck/*default-encoding*)))
|
rlm@10
|
40
|
rlm@10
|
41 (defmethod send-request-entity File [^HttpURLConnection conn ^File entity]
|
rlm@10
|
42 (.setFixedLengthStreamingMode conn (.length entity))
|
rlm@10
|
43 (.connect conn)
|
rlm@10
|
44 (duck/copy entity (.getOutputStream conn)))
|
rlm@10
|
45
|
rlm@10
|
46 (defmethod send-request-entity InputStream [^HttpURLConnection conn entity]
|
rlm@10
|
47 (.setChunkedStreamingMode conn -1)
|
rlm@10
|
48 (.connect conn)
|
rlm@10
|
49 (duck/copy entity (.getOutputStream conn)))
|
rlm@10
|
50
|
rlm@10
|
51 (defmethod send-request-entity Reader [^HttpURLConnection conn entity]
|
rlm@10
|
52 (.setChunkedStreamingMode conn -1)
|
rlm@10
|
53 (.connect conn)
|
rlm@10
|
54 (duck/copy entity (.getOutputStream conn)))
|
rlm@10
|
55
|
rlm@10
|
56 (defn start-http-connection
|
rlm@10
|
57 ([^HttpURLConnection conn] (.connect conn))
|
rlm@10
|
58 ([^HttpURLConnection conn request-entity-body]
|
rlm@10
|
59 (if request-entity-body
|
rlm@10
|
60 (do (.setDoOutput conn true)
|
rlm@10
|
61 (send-request-entity conn request-entity-body))
|
rlm@10
|
62 (.connect conn))))
|