class H2::Server::Stream::Response

Attributes

body[R]
content_length[R]
headers[R]
status[R]
stream[R]

Public Class Methods

new(stream:, status:, headers: {}) click to toggle source

build a new Response object

@param [H2::Server::Stream] stream: Stream instance associated with this response @param [Integer] status: HTTP status code @param [Hash] headers: response headers @param [String,#each] body: response body. NOTE: may be any object that

`respond_to? :each` which both yields and returns
String objects.

@return [H2::Server::Stream::Response]

# File lib/h2/server/stream/response.rb, line 21
def initialize stream:, status:, headers: {}, body: ''
  @stream  = stream
  @headers = headers
  @body    = body
  @status  = status

  check_accept_encoding
  init_content_length
end

Public Instance Methods

init_content_length() click to toggle source

sets the content length in the headers by the byte size of +@body+

# File lib/h2/server/stream/response.rb, line 33
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() click to toggle source

the corresponding Request to this Response

# File lib/h2/server/stream/response.rb, line 50
def request
  stream.request
end
respond_on(s) click to toggle source

send the headers and body out on s, an HTTP2::Stream object, and close the stream when complete.

NOTE: :status must come first?

# File lib/h2/server/stream/response.rb, line 59
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
  stream.log :warn, "stream closed early by client"
end
to_s() click to toggle source
# File lib/h2/server/stream/response.rb, line 71
def to_s
  %{#{request.addr} "#{request.method} #{request.path} HTTP/2" #{status} #{content_length}}
end
Also aliased as: to_str
to_str()
Alias for: to_s