Transfer-Encoding: chunked streaming input support
[unicorn.git] / examples / echo.ru
blobe13721a07560ad858631aa1d3270d551619363ae
1 #\-E none
2 # Example application that echoes read data back to the HTTP client.
3 # This emulates the old echo protocol people used to run.
5 # An example of using this in a client would be to run:
6 #   curl -NT- http://host:port/
8 # Then type random stuff in your terminal to watch it get echoed back!
10 Unicorn::HttpRequest::DEFAULTS["unicorn.stream_input"] = true
11 class EchoBody
12   def initialize(input)
13     @input = input
14   end
16   def each(&block)
17     while buf = @input.read(4096)
18       yield buf
19     end
20     self
21   end
23   def close
24     @input = nil
25   end
26 end
28 use Rack::Chunked
29 run lambda { |env|
30   [ 200, { 'Content-Type' => 'application/octet-stream' },
31     EchoBody.new(env['rack.input']) ]