Class: H2::Server::Stream::Response

Inherits:
Object
  • Object
show all
Includes:
HeaderStringifier, ContentEncoder
Defined in:
lib/h2/server/stream/response.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ContentEncoder

#check_accept_encoding

Constructor Details

#initialize(stream:, status:, headers: {}, body: '') ⇒ H2::Server::Stream::Response

build a new Response object

Parameters:

  • stream: (H2::Server::Stream)

    Stream instance associated with this response

  • status: (Integer)

    HTTP status code

  • headers: (Hash) (defaults to: {})

    response headers

  • body: (String, #each) (defaults to: '')

    response body. NOTE: may be any object that ‘respond_to? :each` which both yields and returns String objects.



21
22
23
24
25
26
27
28
29
# 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

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



8
9
10
# File 'lib/h2/server/stream/response.rb', line 8

def body
  @body
end

#content_lengthObject (readonly)

Returns the value of attribute content_length.



8
9
10
# File 'lib/h2/server/stream/response.rb', line 8

def content_length
  @content_length
end

#headersObject (readonly)

Returns the value of attribute headers.



8
9
10
# File 'lib/h2/server/stream/response.rb', line 8

def headers
  @headers
end

#statusObject (readonly)

Returns the value of attribute status.



8
9
10
# File 'lib/h2/server/stream/response.rb', line 8

def status
  @status
end

#streamObject (readonly)

Returns the value of attribute stream.



8
9
10
# File 'lib/h2/server/stream/response.rb', line 8

def stream
  @stream
end

Instance Method Details

#init_content_lengthObject

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



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# 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

#requestObject

the corresponding Request to this Response



50
51
52
# File 'lib/h2/server/stream/response.rb', line 50

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?



59
60
61
62
63
64
65
66
67
68
69
# 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_sObject Also known as: to_str



71
72
73
# File 'lib/h2/server/stream/response.rb', line 71

def to_s
  %{#{request.addr} "#{request.method} #{request.path} HTTP/2" #{status} #{content_length}}
end