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