Class: Async::HTTP::Protocol::HTTP2::Request

Inherits:
Request
  • Object
show all
Defined in:
lib/async/http/protocol/http2/request.rb

Overview

Typically used on the server side to represent an incoming request, and write the response.

Defined Under Namespace

Classes: Stream

Constant Summary collapse

NO_RESPONSE =
[
  [STATUS, "500"],
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Request

#inspect, #peer, #remote_address

Constructor Details

#initialize(stream) ⇒ Request

Returns a new instance of Request.



89
90
91
92
93
# File 'lib/async/http/protocol/http2/request.rb', line 89

def initialize(stream)
  super(nil, nil, nil, nil, VERSION, nil, nil, nil, self.public_method(:write_interim_response))
  
  @stream = stream
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



95
96
97
# File 'lib/async/http/protocol/http2/request.rb', line 95

def stream
  @stream
end

Instance Method Details

#connectionObject



97
98
99
# File 'lib/async/http/protocol/http2/request.rb', line 97

def connection
  @stream.connection
end

#hijack?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'lib/async/http/protocol/http2/request.rb', line 105

def hijack?
  false
end

#send_response(response) ⇒ Object



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/async/http/protocol/http2/request.rb', line 113

def send_response(response)
  if response.nil?
    return @stream.send_headers(NO_RESPONSE, ::Protocol::HTTP2::END_STREAM)
  end
  
  protocol_headers = [
    [STATUS, response.status],
  ]
  
  if length = response.body&.length
    protocol_headers << [CONTENT_LENGTH, length]
  end
  
  headers = ::Protocol::HTTP::Headers::Merged.new(protocol_headers, response.headers)
  
  if body = response.body and !self.head?
    # This function informs the headers object that any subsequent headers are going to be trailer. Therefore, it must be called *before* sending the headers, to avoid any race conditions.
    trailer = response.headers.trailer!
    
    @stream.send_headers(headers)
    
    @stream.send_body(body, trailer)
  else
    # Ensure the response body is closed if we are ending the stream:
    response.close
    
    @stream.send_headers(headers, ::Protocol::HTTP2::END_STREAM)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


101
102
103
# File 'lib/async/http/protocol/http2/request.rb', line 101

def valid?
  @scheme and @method and (@path or @method == ::Protocol::HTTP::Methods::CONNECT)
end

#write_interim_response(status, headers = nil) ⇒ Object



143
144
145
146
147
148
149
150
151
152
153
# File 'lib/async/http/protocol/http2/request.rb', line 143

def write_interim_response(status, headers = nil)
  interim_response_headers = [
    [STATUS, status]
  ]
  
  if headers
    interim_response_headers = ::Protocol::HTTP::Headers::Merged.new(interim_response_headers, headers)
  end
  
  @stream.send_headers(interim_response_headers)
end