Mercurial > lasercutter
view src/clojure/contrib/http/agent.clj @ 10:ef7dbbd6452c
added clojure source goodness
author | Robert McIntyre <rlm@mit.edu> |
---|---|
date | Sat, 21 Aug 2010 06:25:44 -0400 |
parents | |
children |
line wrap: on
line source
1 ;;; http/agent.clj: agent-based asynchronous HTTP client3 ;; by Stuart Sierra, http://stuartsierra.com/4 ;; August 17, 20096 ;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use7 ;; and distribution terms for this software are covered by the Eclipse8 ;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)9 ;; which can be found in the file epl-v10.html at the root of this10 ;; distribution. By using this software in any fashion, you are11 ;; agreeing to be bound by the terms of this license. You must not12 ;; remove this notice, or any other, from this software.14 ;; DEPRECATED IN 1.2. Use direct Java bits, or take a look at15 ;; http://github.com/technomancy/clojure-http-client17 (ns ^{:deprecated "1.2"18 :doc "Agent-based asynchronous HTTP client.20 This is a HTTP client library based on Java's HttpURLConnection21 class and Clojure's Agent system. It allows you to make multiple22 HTTP requests in parallel.24 Start an HTTP request with the 'http-agent' function, which25 immediately returns a Clojure Agent. You will never deref this26 agent; that is handled by the accessor functions. The agent will27 execute the HTTP request on a separate thread.29 If you pass a :handler function to http-agent, that function will be30 called as soon as the HTTP response body is ready. The handler31 function is called with one argument, the HTTP agent itself. The32 handler can read the response body by calling the 'stream' function33 on the agent.35 The value returned by the handler function becomes part of the state36 of the agent, and you can retrieve it with the 'result' function.37 If you call 'result' before the HTTP request has finished, it will38 block until the handler function returns.40 If you don't provide a handler function, the default handler will41 buffer the entire response body in memory, which you can retrieve42 with the 'bytes', 'string', or 'stream' functions. Like 'result',43 these functions will block until the HTTP request is completed.45 If you want to check if an HTTP request is finished without46 blocking, use the 'done?' function.48 A single GET request could be as simple as:50 (string (http-agent \"http://www.stuartsierra.com/\"))52 A simple POST might look like:54 (http-agent \"http...\" :method \"POST\" :body \"foo=1\")56 And you could write the response directly to a file like this:58 (require '[clojure.contrib.io :as d])60 (http-agent \"http...\"61 :handler (fn [agnt]62 (with-open [w (d/writer \"/tmp/out\")]63 (d/copy (stream agnt) w))))64 "65 :author "Stuart Sierra"66 }68 clojure.contrib.http.agent69 (:refer-clojure :exclude [bytes])70 (:require [clojure.contrib.http.connection :as c]71 [clojure.contrib.io :as duck])72 (:import (java.io InputStream ByteArrayOutputStream73 ByteArrayInputStream)74 (java.net HttpURLConnection)))77 ;;; PRIVATE79 (declare result stream)81 (defn- setup-http-connection82 "Sets the instance method, redirect behavior, and request headers of83 the HttpURLConnection."84 [^HttpURLConnection conn options]85 (when-let [t (:connect-timeout options)]86 (.setConnectTimeout conn t))87 (when-let [t (:read-timeout options)]88 (.setReadTimeout conn t))89 (.setRequestMethod conn (:method options))90 (.setInstanceFollowRedirects conn (:follow-redirects options))91 (doseq [[name value] (:headers options)]92 (.setRequestProperty conn name value)))94 (defn- start-request95 "Agent action that starts sending the HTTP request."96 [state options]97 (let [conn (::connection state)]98 (setup-http-connection conn options)99 (c/start-http-connection conn (:body options))100 (assoc state ::state ::started)))102 (defn- connection-success? [^HttpURLConnection conn]103 "Returns true if the HttpURLConnection response code is in the 2xx104 range."105 (= 2 (quot (.getResponseCode conn) 100)))107 (defn- open-response108 "Agent action that opens the response body stream on the HTTP109 request; this will block until the response stream is available." ;110 [state options]111 (let [^HttpURLConnection conn (::connection state)]112 (assoc state113 ::response-stream (if (connection-success? conn)114 (.getInputStream conn)115 (.getErrorStream conn))116 ::state ::receiving)))118 (defn- handle-response119 "Agent action that calls the provided handler function, with no120 arguments, and sets the ::result key of the agent to the handler's121 return value."122 [state handler options]123 (let [conn (::connection state)]124 (assoc state125 ::result (handler)126 ::state ::finished)))128 (defn- disconnect129 "Agent action that closes the response body stream and disconnects130 the HttpURLConnection."131 [state options]132 (when (::response-stream state)133 (.close ^InputStream (::response-stream state)))134 (.disconnect ^HttpURLConnection (::connection state))135 (assoc state136 ::response-stream nil137 ::state ::disconnected))139 (defn- status-in-range?140 "Returns true if the response status of the HTTP agent begins with141 digit, an Integer."142 [digit http-agnt]143 (= digit (quot (.getResponseCode144 ^HttpURLConnection (::connection @http-agnt))145 100)))147 (defn- ^ByteArrayOutputStream get-byte-buffer [http-agnt]148 (let [buffer (result http-agnt)]149 (if (instance? ByteArrayOutputStream buffer)150 buffer151 (throw (Exception. "Handler result was not a ByteArrayOutputStream")))))154 (defn buffer-bytes155 "The default HTTP agent result handler; it collects the response156 body in a java.io.ByteArrayOutputStream, which can later be157 retrieved with the 'stream', 'string', and 'bytes' functions."158 [http-agnt]159 (let [output (ByteArrayOutputStream.)]160 (duck/copy (or (stream http-agnt) "") output)161 output))164 ;;; CONSTRUCTOR166 (def *http-agent-defaults*167 {:method "GET"168 :headers {}169 :body nil170 :connect-timeout 0171 :read-timeout 0172 :follow-redirects true173 :handler buffer-bytes})175 (defn http-agent176 "Creates (and immediately returns) an Agent representing an HTTP177 request running in a new thread.179 options are key/value pairs:181 :method string183 The HTTP method name. Default is \"GET\".185 :headers h187 HTTP headers, as a Map or a sequence of pairs like188 ([key1,value1], [key2,value2]) Default is nil.190 :body b192 HTTP request entity body, one of nil, String, byte[], InputStream,193 Reader, or File. Default is nil.195 :connect-timeout int197 Timeout value, in milliseconds, when opening a connection to the198 URL. Default is zero, meaning no timeout.200 :read-timeout int202 Timeout value, in milliseconds, when reading data from the203 connection. Default is zero, meaning no timeout.205 :follow-redirects boolean207 If true, HTTP 3xx redirects will be followed automatically. Default208 is true.210 :handler f212 Function to be called when the HTTP response body is ready. If you213 do not provide a handler function, the default is to buffer the214 entire response body in memory.216 The handler function will be called with the HTTP agent as its217 argument, and can use the 'stream' function to read the response218 body. The return value of this function will be stored in the state219 of the agent and can be retrieved with the 'result' function. Any220 exceptions thrown by this function will be added to the agent's221 error queue (see agent-errors). The default function collects the222 response stream in a memory buffer.223 "224 ([uri & options]225 (let [opts (merge *http-agent-defaults* (apply array-map options))]226 (let [a (agent {::connection (c/http-connection uri)227 ::state ::created228 ::uri uri229 ::options opts})]230 (send-off a start-request opts)231 (send-off a open-response opts)232 (send-off a handle-response (partial (:handler opts) a) opts)233 (send-off a disconnect opts)))))236 ;;; RESPONSE BODY ACCESSORS238 (defn result239 "Returns the value returned by the :handler function of the HTTP240 agent; blocks until the HTTP request is completed. The default241 handler function returns a ByteArrayOutputStream."242 [http-agnt]243 (await http-agnt)244 (::result @http-agnt))246 (defn stream247 "Returns an InputStream of the HTTP response body. When called by248 the handler function passed to http-agent, this is the raw249 HttpURLConnection stream.251 If the default handler function was used, this function returns a252 ByteArrayInputStream on the buffered response body."253 [http-agnt]254 (let [a @http-agnt]255 (if (= (::state a) ::receiving)256 (::response-stream a)257 (ByteArrayInputStream.258 (.toByteArray (get-byte-buffer http-agnt))))))260 (defn bytes261 "Returns a Java byte array of the content returned by the server;262 nil if the content is not yet available."263 [http-agnt]264 (.toByteArray (get-byte-buffer http-agnt)))266 (defn string267 "Returns the HTTP response body as a string, using the given268 encoding.270 If no encoding is given, uses the encoding specified in the server271 headers, or clojure.contrib.io/*default-encoding* if it is272 not specified."273 ([http-agnt]274 (await http-agnt) ;; have to wait for Content-Encoding275 (string http-agnt (or (.getContentEncoding276 ^HttpURLConnection (::connection @http-agnt))277 duck/*default-encoding*)))278 ([http-agnt ^String encoding]279 (.toString (get-byte-buffer http-agnt) encoding)))282 ;;; REQUEST ACCESSORS284 (defn request-uri285 "Returns the URI/URL requested by this HTTP agent, as a String."286 [http-agnt]287 (::uri @http-agnt))289 (defn request-headers290 "Returns the request headers specified for this HTTP agent."291 [http-agnt]292 (:headers (::options @http-agnt)))294 (defn method295 "Returns the HTTP method name used by this HTTP agent, as a String."296 [http-agnt]297 (:method (::options @http-agnt)))299 (defn request-body300 "Returns the HTTP request body given to this HTTP agent.302 Note: if the request body was an InputStream or a Reader, it will no303 longer be usable."304 [http-agnt]305 (:body (::options @http-agnt)))308 ;;; RESPONSE ACCESSORS310 (defn done?311 "Returns true if the HTTP request/response has completed."312 [http-agnt]313 (if (#{::finished ::disconnected} (::state @http-agnt))314 true false))316 (defn status317 "Returns the HTTP response status code (e.g. 200, 404) for this318 request, as an Integer, or nil if the status has not yet been319 received."320 [http-agnt]321 (when (done? http-agnt)322 (.getResponseCode ^HttpURLConnection (::connection @http-agnt))))324 (defn message325 "Returns the HTTP response message (e.g. 'Not Found'), for this326 request, or nil if the response has not yet been received."327 [http-agnt]328 (when (done? http-agnt)329 (.getResponseMessage ^HttpURLConnection (::connection @http-agnt))))331 (defn headers332 "Returns a map of HTTP response headers. Header names are converted333 to keywords in all lower-case Header values are strings. If a334 header appears more than once, only the last value is returned."335 [http-agnt]336 (reduce (fn [m [^String k v]]337 (assoc m (when k (keyword (.toLowerCase k))) (last v)))338 {} (.getHeaderFields339 ^HttpURLConnection (::connection @http-agnt))))341 (defn headers-seq342 "Returns the HTTP response headers in order as a sequence of343 [String,String] pairs. The first 'header' name may be null for the344 HTTP status line."345 [http-agnt]346 (let [^HttpURLConnection conn (::connection @http-agnt)347 f (fn thisfn [^Integer i]348 ;; Get value first because first key may be nil.349 (when-let [value (.getHeaderField conn i)]350 (cons [(.getHeaderFieldKey conn i) value]351 (thisfn (inc i)))))]352 (lazy-seq (f 0))))355 ;;; RESPONSE STATUS CODE ACCESSORS357 (defn success?358 "Returns true if the HTTP response code was in the 200-299 range."359 [http-agnt]360 (status-in-range? 2 http-agnt))362 (defn redirect?363 "Returns true if the HTTP response code was in the 300-399 range.365 Note: if the :follow-redirects option was true (the default),366 redirects will be followed automatically and a the agent will never367 return a 3xx response code."368 [http-agnt]369 (status-in-range? 3 http-agnt))371 (defn client-error?372 "Returns true if the HTTP response code was in the 400-499 range."373 [http-agnt]374 (status-in-range? 4 http-agnt))376 (defn server-error?377 "Returns true if the HTTP response code was in the 500-599 range."378 [http-agnt]379 (status-in-range? 5 http-agnt))381 (defn error?382 "Returns true if the HTTP response code was in the 400-499 range OR383 the 500-599 range."384 [http-agnt]385 (or (client-error? http-agnt)386 (server-error? http-agnt)))