aboutsummaryrefslogtreecommitdiff
path: root/src/clojure/contrib/http/agent.clj
blob: 698ea4a81b32c2ea37e08467c266ce1d75b3274f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
;;; http/agent.clj: agent-based asynchronous HTTP client

;; by Stuart Sierra, http://stuartsierra.com/
;; June 8, 2009

;; Copyright (c) Stuart Sierra, 2009. All rights reserved.  The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution.  By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license.  You must not
;; remove this notice, or any other, from this software.


(ns #^{:doc "Agent-based asynchronous HTTP client."}
  clojure.contrib.http.agent
  (:require [clojure.contrib.http.connection :as c]
            [clojure.contrib.duck-streams :as duck])
  (:import (java.io ByteArrayOutputStream)))

(defn- setup-http-connection
  [conn options]
  (.setRequestMethod conn (:method options))
  (.setInstanceFollowRedirects conn (:follow-redirects options))
  (doseq [[name value] (:headers options)]
    (.setRequestProperty conn name value)))

(defn- connection-success? [conn]
  ;; Is the response in the 2xx range?
  (= 2 (unchecked-divide (.getResponseCode conn) 100)))

(defn- start-request [state options]
  (prn "start-request")
  (let [conn (::connection state)]
    (setup-http-connection conn options)
    (c/start-http-connection conn (:body options))
    (assoc state ::state ::started)))

(defn- open-response [state options]
  (prn "open-response")
  (let [conn (::connection state)]
    (assoc state
      ::response-stream (if (connection-success? conn)
                          (.getInputStream conn)
                          (.getErrorStream conn))
      ::state ::receiving)))

(defn- handle-response [state success-handler failure-handler options]
  (prn "handle-response")
  (let [conn (::connection state)]
    (assoc state
      ::result (if (connection-success? conn)
                   (success-handler)
                   (failure-handler))
      ::state ::finished)))

(defn- disconnect [state options]
  (prn "disconnect")
  (.close (::response-stream state))
  (.disconnect (::connection state))
  (assoc state
    ::response-stream nil
    ::state ::disconnected))

(defn response-body-stream
  "Returns an InputStream of the HTTP response body."
  [http-agnt]
  (let [a @http-agnt]
    (if (= (::state a) ::receiving)
      (::response-stream a)
      (throw (Exception. "response-body-stream may only be called from a callback function passed to http-agent")))))

(defn buffer-bytes
  "The default HTTP agent result handler; it collects the response
  body in a java.io.ByteArrayOutputStream."
  [http-agnt]
  (let [output (ByteArrayOutputStream.)]
    (duck/copy (response-body-stream http-agnt) output)
    output))

(def *http-agent-defaults*
  {:method "GET"
   :headers {}
   :body nil
   :connect-timeout 0
   :read-timeout 0
   :follow-redirects true
   :on-success buffer-bytes
   :on-failure buffer-bytes})

(defn http-agent
  "Creates (and immediately returns) an Agent representing an HTTP
  request running in a new thread.

  options are key/value pairs:

  :method string

  The HTTP method name.  Default is \"GET\".

  :headers h

  HTTP headers, as a Map or a sequence of pairs like 
  ([key1,value1], [key2,value2])  Default is nil.

  :body b
  
  HTTP request entity body, one of nil, String, byte[], InputStream,
  Reader, or File.  Default is nil.

  :connect-timeout int

  Timeout value, in milliseconds, when opening a connection to the
  URL.  Default is zero, meaning no timeout.

  :read-timeout int

  Timeout value, in milliseconds, when reading data from the
  connection.  Default is zero, meaning no timeout.

  :follow-redirects boolean

  If true, HTTP 3xx redirects will be followed automatically.  Default
  is true.

  :on-success f

  Function to be called when the request succeeds with a 2xx response
  code.  The function will be called with the HTTP agent as its
  argument, and can use the response-body-stream function to read the
  response body.  The return value of this function will be stored in
  the state of the agent and can be retrieved with the 'result'
  function.  Any exceptions thrown by this function will be added to
  the agent's error queue (see agent-errors).  The default function
  collects the response stream into a byte array.

  :on-failure f

  Like :on-success but this function will be called when the request
  fails with a 4xx or 5xx response code.
  "
  ([url & options]
     (let [opts (merge *http-agent-defaults* (apply array-map options))]
       (let [a (agent {::connection (c/http-connection url)
                       ::state ::created
                       ::url url
                       ::options opts})]
         (send-off a start-request opts)
         (send-off a open-response opts)
         (send-off a handle-response
                   (partial (:on-success opts) a)
                   (partial (:on-failure opts) a)
                   opts)
         (send-off a disconnect opts)))))

(defn result
  "Returns the value returned by the :on-success or :on-failure
  handler function of the HTTP agent; nil if the handler function has
  not yet finished.  The default handler function returns a
  ByteArrayOutputStream."
  [http-agnt]
  (when (#{::disconnected ::finished} (::state @http-agnt))
    (::result @http-agnt)))

(defn response-body-bytes
  "Returns a Java byte array of the content returned by the server;
  nil if the content is not yet available."
  [http-agnt]
  (when-let [buffer (result http-agnt)]
    (if (instance? ByteArrayOutputStream buffer)
      (.toByteArray buffer)
      (throw (Exception. "Result was not a ByteArrayOutputStream")))))

(defn response-body-str
  "Returns the HTTP response body as a string, using the given
  encoding.

  If no encoding is given, uses the encoding specified in the server
  headers, or clojure.contrib.duck-streams/*default-encoding* if it is
  not specified."
  ([http-agnt]
     (response-body-str http-agnt
                        (or (.getContentEncoding (::connection @http-agnt))
                            duck/*default-encoding*)))
  ([http-agnt encoding]
     (when-let [buffer (result http-agnt)]
       (if (instance? ByteArrayOutputStream buffer)
         (.toString buffer encoding)
         (throw (Exception. "Result was not a ByteArrayOutputStream"))))))

(defn response-status
  "Returns the Integer response status code (e.g. 200, 404) for this request."
  [a]
  (when (= (::state @a) ::completed)
    (.getResponseCode (::connection @a))))

(defn response-message
  "Returns the HTTP response message (e.g. 'Not Found'), for this request."
  [a]
  (when (= (::state @a) ::completed)
    (.getResponseMessage (::connection @a))))

(defn response-headers
  "Returns a String=>String map of HTTP response headers.  Header
  names are converted to all lower-case.  If a header appears more
  than once, only the last value is returned."
  [a]
  (reduce (fn [m [#^String k v]]
            (assoc m (when k (.toLowerCase k)) (last v)))
          {} (.getHeaderFields (::connection @a))))

(defn response-headers-seq
  "Returns the HTTP response headers in order as a sequence of
  [String,String] pairs.  The first 'header' name may be null for the
  HTTP status line."
  [http-agnt]
  (let [conn (::connection @http-agnt)
        f (fn thisfn [i]
            ;; Get value first because first key may be nil.
            (when-let [value (.getHeaderField conn i)]
              (cons [(.getHeaderFieldKey conn i) value]
                    (thisfn (inc i)))))]
    (lazy-seq (f 0))))

(defn- response-in-range? [digit http-agnt]
  (= digit (unchecked-divide (.getResponseCode (::connection @http-agnt))
                             100)))

(defn success?
  "Returns true if the HTTP response code was in the 200-299 range."
  [http-agnt]
  (response-in-range? 2 http-agnt))

(defn redirect?
  "Returns true if the HTTP response code was in the 300-399 range.

  Note: if the :follow-redirects option was true (the default),
  redirects will be followed automatically and a the agent will never
  return a 3xx response code."
  [http-agnt]
  (response-in-range? 3 http-agnt))

(defn client-error?
  "Returns true if the HTTP response code was in the 400-499 range."
  [http-agnt]
  (response-in-range? 4 http-agnt))

(defn server-error?
  "Returns true if the HTTP response code was in the 500-599 range."
  [http-agnt]
  (response-in-range? 5 http-agnt))

(defn error?
  "Returns true if the HTTP response code was in the 400-499 range OR
  the 500-599 range."
  [http-agnt]
  (or (client-error? http-agnt)
      (server-error? http-agnt)))