Skip to content

Instantly share code, notes, and snippets.

Created January 3, 2013 21:11
Show Gist options
  • Save anonymous/4447300 to your computer and use it in GitHub Desktop.
Save anonymous/4447300 to your computer and use it in GitHub Desktop.
troubleshooting streaming responses with ring
(ns snapper.core
(:require [ring.adapter.jetty :as jetty]
[clojure.java.io :as io]
[ring.util.io :as ringio])
(:import [java.io PipedOutputStream PipedInputStream PrintWriter])
(:gen-class))
(defn handler
"Respond with a stream"
[request]
(let [input-stream
(ringio/piped-input-stream
(fn [output-stream]
(.write output-stream "Hello")
(Thread/sleep 500)
(.write output-stream "World")
(.close output-stream)
))]
{
:status 200
:headers {}
:body input-stream
}
)
)
(defn -main
[& args]
(jetty/run-jetty handler {:port 8081})
)
oration ~/C/snapper (master*) ➜ curl -v localhost:8081
* About to connect() to localhost port 8081 (#0)
* Trying ::1...
* connected
* Connected to localhost (::1) port 8081 (#0)
> GET / HTTP/1.1
> User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5
> Host: localhost:8081
> Accept: */*
>
< HTTP/1.1 200 OK
< Date: Thu, 03 Jan 2013 21:10:41 GMT
< Content-Length: 0
< Server: Jetty(7.6.1.v20120215)
<
* Connection #0 to host localhost left intact
* Closing connection #0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment