Class: H2::Server::Stream::Response
- Inherits:
-
Object
- Object
- H2::Server::Stream::Response
- Includes:
- HeaderStringifier
- Defined in:
- lib/h2/server/stream/response.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#content_length ⇒ Object
readonly
Returns the value of attribute content_length.
-
#headers ⇒ Object
readonly
Returns the value of attribute headers.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#stream ⇒ Object
readonly
Returns the value of attribute stream.
Instance Method Summary collapse
-
#init_content_length ⇒ Object
sets the content length in the headers by the byte size of @body.
-
#initialize(stream:, status:, headers: {}, body: '') ⇒ H2::Server::Stream::Response
constructor
build a new
Response
object. -
#request ⇒ Object
the corresponding
Request
to thisResponse
. -
#respond_on(s) ⇒ Object
send the headers and body out on
s
, anHTTP2::Stream
object, and close the stream when complete. - #to_s ⇒ Object (also: #to_str)
Constructor Details
#initialize(stream:, status:, headers: {}, body: '') ⇒ H2::Server::Stream::Response
build a new Response
object
20 21 22 23 24 25 26 27 |
# File 'lib/h2/server/stream/response.rb', line 20 def initialize stream:, status:, headers: {}, body: '' @stream = stream @headers = headers @body = body @status = status init_content_length end |
Instance Attribute Details
#body ⇒ Object (readonly)
Returns the value of attribute body.
7 8 9 |
# File 'lib/h2/server/stream/response.rb', line 7 def body @body end |
#content_length ⇒ Object (readonly)
Returns the value of attribute content_length.
7 8 9 |
# File 'lib/h2/server/stream/response.rb', line 7 def content_length @content_length end |
#headers ⇒ Object (readonly)
Returns the value of attribute headers.
7 8 9 |
# File 'lib/h2/server/stream/response.rb', line 7 def headers @headers end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
7 8 9 |
# File 'lib/h2/server/stream/response.rb', line 7 def status @status end |
#stream ⇒ Object (readonly)
Returns the value of attribute stream.
7 8 9 |
# File 'lib/h2/server/stream/response.rb', line 7 def stream @stream end |
Instance Method Details
#init_content_length ⇒ Object
sets the content length in the headers by the byte size of @body
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/h2/server/stream/response.rb', line 31 def init_content_length return if @headers.any? {|k,_| k.downcase == CONTENT_LENGTH_KEY} return if @body.respond_to?(:each) @content_length = case when String === @body @body.bytesize when NilClass '0' else raise TypeError, "can't render #{@body.class} as a response body" end @headers[CONTENT_LENGTH_KEY] = @content_length end |
#request ⇒ Object
the corresponding Request
to this Response
48 49 50 |
# File 'lib/h2/server/stream/response.rb', line 48 def request stream.request end |
#respond_on(s) ⇒ Object
send the headers and body out on s
, an HTTP2::Stream
object, and close the stream when complete.
NOTE: :status
must come first?
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/h2/server/stream/response.rb', line 57 def respond_on s headers = { STATUS_KEY => @status.to_s }.merge @headers s.headers stringify_headers(headers) if String === @body s.data @body else stream.log :error, "unexpected @body: #{caller[0]}" end rescue ::HTTP2::Error::StreamClosed => sc stream.log :warn, "stream closed early by client" end |
#to_s ⇒ Object Also known as: to_str
69 70 71 |
# File 'lib/h2/server/stream/response.rb', line 69 def to_s %{#{request.addr} "#{request.method} #{request.path} HTTP/2" #{status} #{content_length}} end |