Class: Falcon::Adapters::Output

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

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



44
45
46
47
48
49
50
# File 'lib/falcon/adapters/output.rb', line 44

def initialize(headers, body, length)
  # We don't trust the user to provide the right length to the transport.
  @length = length
  
  @body = body
  @chunks = body.to_enum(:each)
end

Instance Attribute Details

#lengthObject (readonly)

Returns the value of attribute length.



52
53
54
# File 'lib/falcon/adapters/output.rb', line 52

def length
  @length
end

Class Method Details

.wrap(status, headers, body) ⇒ Object

Wraps an array into a buffered body.



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/falcon/adapters/output.rb', line 30

def self.wrap(status, headers, body)
  # In no circumstance do we want this propagating up:
  content_length = headers.delete(CONTENT_LENGTH)
  
  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, (Integer(content_length) rescue nil))
  end
end

Instance Method Details

#empty?Boolean



54
55
56
# File 'lib/falcon/adapters/output.rb', line 54

def empty?
  @length == 0
end

#inspectObject



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

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

#readObject



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

def read
  @chunks.next
rescue StopIteration
  nil
end