Updated README.rdoc again
[feedcatcher.git] / vendor / rails / actionpack / lib / action_controller / vendor / rack-1.0 / rack / chunked.rb
1 require 'rack/utils'
2
3 module Rack
4
5 # Middleware that applies chunked transfer encoding to response bodies
6 # when the response does not include a Content-Length header.
7 class Chunked
8 include Rack::Utils
9
10 def initialize(app)
11 @app = app
12 end
13
14 def call(env)
15 status, headers, body = @app.call(env)
16 headers = HeaderHash.new(headers)
17
18 if env['HTTP_VERSION'] == 'HTTP/1.0' ||
19 STATUS_WITH_NO_ENTITY_BODY.include?(status) ||
20 headers['Content-Length'] ||
21 headers['Transfer-Encoding']
22 [status, headers.to_hash, body]
23 else
24 dup.chunk(status, headers, body)
25 end
26 end
27
28 def chunk(status, headers, body)
29 @body = body
30 headers.delete('Content-Length')
31 headers['Transfer-Encoding'] = 'chunked'
32 [status, headers.to_hash, self]
33 end
34
35 def each
36 term = "\r\n"
37 @body.each do |chunk|
38 size = bytesize(chunk)
39 next if size == 0
40 yield [size.to_s(16), term, chunk, term].join
41 end
42 yield ["0", term, "", term].join
43 end
44
45 def close
46 @body.close if @body.respond_to?(:close)
47 end
48 end
49 end