Module: Protocol::Rack::Body

Defined in:
lib/protocol/rack/body.rb,
lib/protocol/rack/body/streaming.rb,
lib/protocol/rack/body/enumerable.rb,
lib/protocol/rack/body/input_wrapper.rb

Defined Under Namespace

Classes: Enumerable, InputWrapper, Streaming

Constant Summary collapse

CONTENT_LENGTH =
'content-length'

Class Method Summary collapse

Class Method Details

.wrap(status, headers, body, input = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/protocol/rack/body.rb', line 31

def self.wrap(status, headers, body, input = nil)
  # In no circumstance do we want this header propagating out:
  if length = headers.delete(CONTENT_LENGTH)
    # We don't really trust the user to provide the right length to the transport.
    length = Integer(length)
  end

  # If we have an Async::HTTP body, we return it directly:
  if body.is_a?(::Protocol::HTTP::Body::Readable)
    return body
  elsif status == 200 and body.respond_to?(:to_path)
    begin
      # Don't mangle partial responses (206)
      return ::Protocol::HTTP::Body::File.open(body.to_path).tap do
        body.close if body.respond_to?(:close) # Close the original body.
      end
    rescue Errno::ENOENT
      # If the file is not available, ignore.
    end
  elsif body.respond_to?(:each)
    body = Body::Enumerable.wrap(body, length)
  else
    body = Body::Streaming.new(body, input)
  end
end