File: streaming.ru

package info (click to toggle)
ruby-excon 1.3.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,240 kB
  • sloc: ruby: 7,970; makefile: 5
file content (30 lines) | stat: -rw-r--r-- 651 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
use Rack::ContentType, "text/plain"

app = lambda do |env|
  # streamed pieces to be sent
  pieces = %w{Hello streamy world}

  response_headers = {}

  # set a fixed content length in the header if requested
  if env['REQUEST_PATH'] == '/streamed/fixed_length'
    response_headers['Content-Length'] = pieces.join.length.to_s
  end

  response_headers["rack.hijack"] = lambda do |io|
    # Write directly to IO of the response
    begin
      # return the response in pieces
      pieces.each do |x|
        sleep(0.1)
        io.write(x)
        io.flush
      end
    ensure
      io.close
    end
  end
  [200, response_headers, nil]
end

run app