Class: Falcon::Adapters::Output
- Inherits:
-
Protocol::HTTP::Body::Readable
- Object
- Protocol::HTTP::Body::Readable
- Falcon::Adapters::Output
- 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
-
#body ⇒ Object
readonly
The rack response body.
-
#length ⇒ Object
readonly
The content length of the rack response body.
Class Method Summary collapse
-
.wrap(status, headers, body) ⇒ Object
Wraps an array into a buffered body.
Instance Method Summary collapse
- #close(error = nil) ⇒ Object
- #each(&block) ⇒ Object
- #empty? ⇒ Boolean
-
#initialize(headers, body, length) ⇒ Output
constructor
A new instance of Output.
- #inspect ⇒ Object
- #read ⇒ Object
Constructor Details
#initialize(headers, body, length) ⇒ Output
Returns a new instance of Output.
53 54 55 56 57 58 |
# File 'lib/falcon/adapters/output.rb', line 53 def initialize(headers, body, length) @length = length @body = body @chunks = nil end |
Instance Attribute Details
#body ⇒ Object (readonly)
The rack response body.
61 62 63 |
# File 'lib/falcon/adapters/output.rb', line 61 def body @body end |
#length ⇒ Object (readonly)
The content length of the rack response body.
64 65 66 |
# File 'lib/falcon/adapters/output.rb', line 64 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 48 49 50 51 |
# 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 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 body.is_a?(::Protocol::HTTP::Body::Readable) return body elsif status == 200 and body.respond_to?(:to_path) # Don't mangle partial responsese (206) return ::Protocol::HTTP::Body::File.open(body.to_path) elsif body.is_a? Array # TODO after dropping 2.3, change to #sum length ||= body.inject(0){|sum, chunk| sum + chunk.bytesize} return self.new(headers, body, length) else return self.new(headers, body, length) end end |
Instance Method Details
#close(error = nil) ⇒ Object
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/falcon/adapters/output.rb', line 70 def close(error = nil) if @body and @body.respond_to?(:close) @body.close end @body = nil @chunks = nil super end |
#each(&block) ⇒ Object
81 82 83 84 85 |
# File 'lib/falcon/adapters/output.rb', line 81 def each(&block) @body.each(&block) ensure self.close($!) end |
#empty? ⇒ Boolean
66 67 68 |
# File 'lib/falcon/adapters/output.rb', line 66 def empty? @length == 0 or (@body.respond_to?(:empty?) and @body.empty?) end |
#inspect ⇒ Object
95 96 97 |
# File 'lib/falcon/adapters/output.rb', line 95 def inspect "\#<#{self.class} length=#{@length.inspect} body=#{@body.class}>" end |
#read ⇒ Object
87 88 89 90 91 92 93 |
# File 'lib/falcon/adapters/output.rb', line 87 def read @chunks ||= @body.to_enum(:each) return @chunks.next rescue StopIteration return nil end |