Mercurial > lasercutter
diff 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 diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/clojure/contrib/http/connection.clj Sat Aug 21 06:25:44 2010 -0400 1.3 @@ -0,0 +1,62 @@ 1.4 +;;; http/connection.clj: low-level HTTP client API around HttpURLConnection 1.5 + 1.6 +;; by Stuart Sierra, http://stuartsierra.com/ 1.7 +;; June 8, 2009 1.8 + 1.9 +;; Copyright (c) Stuart Sierra, 2009. All rights reserved. The use 1.10 +;; and distribution terms for this software are covered by the Eclipse 1.11 +;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 1.12 +;; which can be found in the file epl-v10.html at the root of this 1.13 +;; distribution. By using this software in any fashion, you are 1.14 +;; agreeing to be bound by the terms of this license. You must not 1.15 +;; remove this notice, or any other, from this software. 1.16 + 1.17 +;; DEPRECATED IN 1.2. Use direct Java bits, or take a look at 1.18 +;; http://github.com/technomancy/clojure-http-client 1.19 + 1.20 +(ns ^{:deprecated "1.2" 1.21 + :doc "Low-level HTTP client API around HttpURLConnection"} 1.22 + clojure.contrib.http.connection 1.23 + (:require [clojure.contrib.io :as duck]) 1.24 + (:import (java.net URI URL HttpURLConnection) 1.25 + (java.io File InputStream Reader))) 1.26 + 1.27 +(defn http-connection 1.28 + "Opens an HttpURLConnection at the URL, handled by as-url." 1.29 + [url] 1.30 + (.openConnection (duck/as-url url))) 1.31 + 1.32 +(defmulti 1.33 + ^{:doc "Transmits a request entity body."} 1.34 + send-request-entity (fn [conn entity] (type entity))) 1.35 + 1.36 +(defmethod send-request-entity duck/*byte-array-type* [^HttpURLConnection conn entity] 1.37 + (.setFixedLengthStreamingMode conn (count entity)) 1.38 + (.connect conn) 1.39 + (duck/copy entity (.getOutputStream conn))) 1.40 + 1.41 +(defmethod send-request-entity String [conn ^String entity] 1.42 + (send-request-entity conn (.getBytes entity duck/*default-encoding*))) 1.43 + 1.44 +(defmethod send-request-entity File [^HttpURLConnection conn ^File entity] 1.45 + (.setFixedLengthStreamingMode conn (.length entity)) 1.46 + (.connect conn) 1.47 + (duck/copy entity (.getOutputStream conn))) 1.48 + 1.49 +(defmethod send-request-entity InputStream [^HttpURLConnection conn entity] 1.50 + (.setChunkedStreamingMode conn -1) 1.51 + (.connect conn) 1.52 + (duck/copy entity (.getOutputStream conn))) 1.53 + 1.54 +(defmethod send-request-entity Reader [^HttpURLConnection conn entity] 1.55 + (.setChunkedStreamingMode conn -1) 1.56 + (.connect conn) 1.57 + (duck/copy entity (.getOutputStream conn))) 1.58 + 1.59 +(defn start-http-connection 1.60 + ([^HttpURLConnection conn] (.connect conn)) 1.61 + ([^HttpURLConnection conn request-entity-body] 1.62 + (if request-entity-body 1.63 + (do (.setDoOutput conn true) 1.64 + (send-request-entity conn request-entity-body)) 1.65 + (.connect conn))))