Class: Falcon::Adapters::Output

Inherits:
Async::HTTP::Body::Readable
  • Object
show all
Defined in:
lib/falcon/adapters/output.rb

Overview

Wraps the rack response body. The Body must respond to each and must only yield String values. The Body itself should not be an instance of String, as this will break in Ruby 1.9. If the Body responds to close, it will be called after iteration. If the body is replaced by a middleware after action, the original body must be closed first, if it responds to close. If the Body responds to to_path, it must return a String identifying the location of a file whose contents are identical to that produced by calling each; this may be used by the server as an alternative, possibly more efficient way to transport the response. The Body commonly is an Array of Strings, the application instance itself, or a File-like object.

Constant Summary collapse

CONTENT_LENGTH =
'content-length'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(headers, body, length) ⇒ Output

Returns a new instance of Output.



49
50
51
52
53
54
55
# File 'lib/falcon/adapters/output.rb', line 49

def initialize(headers, body, length)
  @length = length
  @body = body
  
  # An enumerator over the rack response body:
  @chunks = body.to_enum(:each)
end

Instance Attribute Details

#bodyObject (readonly)

The rack response body.



58
59
60
# File 'lib/falcon/adapters/output.rb', line 58

def body
  @body
end

#lengthObject (readonly)

The content length of the rack response body.



61
62
63
# File 'lib/falcon/adapters/output.rb', line 61

def length
  @length
end

Class Method Details

.wrap(status, headers, body) ⇒ Object

Wraps an array into a buffered body.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/falcon/adapters/output.rb', line 32

def self.wrap(status, headers, body)
  # In no circumstance do we want this header propagating out:
  if content_length = headers.delete(CONTENT_LENGTH)
    # We don't really trust the user to provide the right length to the transport.
    content_length = Integer(content_length)
  end
  
  if body.is_a?(Async::HTTP::Body::Readable)
    return body
  elsif status == 200 and body.respond_to?(:to_path)
    # Don't mangle partial responsese (206)
    return Async::HTTP::Body::File.open(body.to_path)
  else
    return self.new(headers, body, content_length)
  end
end

Instance Method Details

#close(error = nil) ⇒ Object



67
68
69
70
71
72
73
74
75
76
# File 'lib/falcon/adapters/output.rb', line 67

def close(error = nil)
  if @body and @body.respond_to?(:close)
    @body.close
    @body = nil
  end
  
  @chunks = nil
  
  super
end

#empty?Boolean

Returns:

  • (Boolean)


63
64
65
# File 'lib/falcon/adapters/output.rb', line 63

def empty?
  @length == 0 or (@body.respond_to?(:empty?) and @body.empty?)
end

#inspectObject



86
87
88
# File 'lib/falcon/adapters/output.rb', line 86

def inspect
  "\#<#{self.class} length=#{@length.inspect} body=#{@body.class}>"
end

#readObject



78
79
80
81
82
83
84
# File 'lib/falcon/adapters/output.rb', line 78

def read
  if @chunks
    return @chunks.next
  end
rescue StopIteration
  nil
end