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

Attributes inherited from Request

#connection

Instance Method Summary collapse

Methods inherited from Request

#peer, #remote_address, #remote_address=

Constructor Details

#initialize(stream) ⇒ Request

Returns a new instance of Request.



109
110
111
112
113
# File 'lib/async/http/protocol/http2/request.rb', line 109

def initialize(stream)
  super(nil, nil, nil, nil, VERSION, nil)
  
  @stream = stream
end

Instance Attribute Details

#streamObject (readonly)

Returns the value of attribute stream.



115
116
117
# File 'lib/async/http/protocol/http2/request.rb', line 115

def stream
  @stream
end

Instance Method Details

#hijack?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/async/http/protocol/http2/request.rb', line 121

def hijack?
  false
end

#push(path, headers = nil, scheme = @scheme, authority = @authority) ⇒ Stream

Returns the promised stream, on which to send data.

Returns:

  • (Stream)

    the promised stream, on which to send data.

Raises:

  • (ArgumentError)


130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/async/http/protocol/http2/request.rb', line 130

def push(path, headers = nil, scheme = @scheme, authority = @authority)
  raise ArgumentError, "Missing scheme!" unless scheme
  raise ArgumentError, "Missing authority!" unless authority
  
  push_headers = [
    [SCHEME, scheme],
    [METHOD, ::Protocol::HTTP::Methods::GET],
    [PATH, path],
    [AUTHORITY, authority]
  ]
  
  if headers
    push_headers = Headers::Merged.new(
      push_headers,
      headers
    )
  end
  
  @stream.send_push_promise(push_headers)
end

#push?Boolean

Returns:

  • (Boolean)


125
126
127
# File 'lib/async/http/protocol/http2/request.rb', line 125

def push?
  @stream.connection.enable_push?
end

#send_response(response) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/async/http/protocol/http2/request.rb', line 155

def send_response(response)
  if response.nil?
    @stream.send_headers(nil, NO_RESPONSE, ::Protocol::HTTP2::END_STREAM)
  elsif response.body?
    pseudo_headers = [
      [STATUS, response.status],
    ]
    
    if protocol = response.protocol
      pseudo_headers << [PROTOCOL, protocol]
    end
    
    if length = response.body.length
      pseudo_headers << [CONTENT_LENGTH, length]
    end
    
    headers = ::Protocol::HTTP::Headers::Merged.new(
      pseudo_headers,
      response.headers
    )
    
    @stream.send_headers(nil, headers)
    @stream.send_body(response.body)
  else
    headers = ::Protocol::HTTP::Headers::Merged.new([
      [STATUS, response.status],
    ], response.headers)
    
    @stream.send_headers(nil, headers, ::Protocol::HTTP2::END_STREAM)
  end
end

#valid?Boolean

Returns:

  • (Boolean)


117
118
119
# File 'lib/async/http/protocol/http2/request.rb', line 117

def valid?
  @scheme and @method and @path
end