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

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

Overview

A server that supports both HTTP1.0 and HTTP1.1 semantics by detecting the version of the request.

Defined Under Namespace

Classes: Request

Constant Summary collapse

HTTPS =
'https'.freeze
SCHEME =
':scheme'.freeze
METHOD =
':method'.freeze
PATH =
':path'.freeze
AUTHORITY =
':authority'.freeze
REASON =
':reason'.freeze
STATUS =
':status'.freeze
VERSION =
'HTTP/2.0'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(controller, stream) ⇒ HTTP2

Returns a new instance of HTTP2.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/async/http/protocol/http2.rb', line 54

def initialize(controller, stream)
  @controller = controller
  @stream = stream
  
  @controller.on(:frame) do |data|
    @stream.write(data)
    @stream.flush
  end
  
  @controller.on(:frame_sent) do |frame|
    Async.logger.debug(self) {"Sent frame: #{frame.inspect}"}
  end
  
  @controller.on(:frame_received) do |frame|
    Async.logger.debug(self) {"Received frame: #{frame.inspect}"}
  end
  
  @goaway = false
  
  @controller.on(:goaway) do |payload|
    Async.logger.error(self) {"goaway: #{payload.inspect}"}
    
    @goaway = true
  end
  
  @count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



82
83
84
# File 'lib/async/http/protocol/http2.rb', line 82

def count
  @count
end

Class Method Details

.client(stream) ⇒ Object



37
38
39
# File 'lib/async/http/protocol/http2.rb', line 37

def self.client(stream)
  self.new(::HTTP2::Client.new, stream)
end

.server(stream) ⇒ Object



41
42
43
# File 'lib/async/http/protocol/http2.rb', line 41

def self.server(stream)
  self.new(::HTTP2::Server.new, stream)
end

Instance Method Details

#call(request) ⇒ Object



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
# File 'lib/async/http/protocol/http2.rb', line 234

def call(request)
  request.version ||= self.version
  
  stream = @controller.new_stream
  @count += 1
  
  headers = Headers::Merged.new({
    SCHEME => HTTPS,
    METHOD => request.method,
    PATH => request.path,
    AUTHORITY => request.authority,
  }, request.headers)
  
  finished = Async::Notification.new
  
  exception = nil
  response = Response.new
  response.version = self.version
  response.headers = Headers.new
  body = Body::Writable.new
  response.body = body
  
  stream.on(:headers) do |headers|
    headers.each do |key, value|
      if key == STATUS
        response.status = value.to_i
      elsif key == REASON
        response.reason = value
      else
        response.headers[key] = value
      end
    end
    
    # At this point, we are now expecting two events: data and close.
    stream.on(:close) do |error|
      # If we receive close after this point, it's not a request error, but a failure we need to signal to the body.
      if error
        body.stop(EOFError.new(error))
      else
        body.finish
      end
    end
    
    finished.signal
  end
  
  stream.on(:data) do |chunk|
    body.write(chunk.to_s) unless chunk.empty?
  end
  
  stream.on(:close) do |error|
    # The remote server has closed the connection while we were sending the request.
    if error
      exception = EOFError.new(error)
      finished.signal
    end
  end
  
  if request.body.nil? or request.body.empty?
    stream.headers(headers, end_stream: true)
    request.body.read if request.body
  else
    begin
      stream.headers(headers)
    rescue
      raise RequestFailed.new
    end
    
    request.body.each do |chunk|
      stream.data(chunk, end_stream: false)
    end
      
    stream.data("")
  end
  
  start_connection
  @stream.flush
  
  Async.logger.debug(self) {"Stream flushed, waiting for signal."}
  finished.wait
  
  if exception
    raise exception
  end
  
  Async.logger.debug(self) {"Stream finished: #{response.inspect}"}
  return response
end

#closeObject



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

def close
  Async.logger.debug(self) {"Closing connection"}
  
  @reader.stop if @reader
  @stream.close
end

#good?Boolean

Can we use this connection to make requests?

Returns:



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

def good?
  @stream.connected?
end

#multiplexObject

Multiple requests can be processed at the same time.



85
86
87
# File 'lib/async/http/protocol/http2.rb', line 85

def multiplex
  @controller.remote_settings[:settings_max_concurrent_streams]
end

#read_in_background(task: Task.current) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
# File 'lib/async/http/protocol/http2.rb', line 106

def read_in_background(task: Task.current)
  task.async do |nested_task|
    nested_task.annotate("#{version} reading data")
    
    while buffer = @stream.read_partial
      @controller << buffer
    end
    
    Async.logger.debug(self) {"Connection reset by peer!"}
  end
end

#receive_requests(task: Task.current, &block) ⇒ Object



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
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/async/http/protocol/http2.rb', line 159

def receive_requests(task: Task.current, &block)
  # emits new streams opened by the client
  @controller.on(:stream) do |stream|
    request = Request.new(stream)
    body = request.body
    
    stream.on(:headers) do |headers|
      begin
        request.assign_headers(headers)
      rescue
        Async.logger.error(self) {$!}
        
        stream.headers({
          STATUS => "400"
        }, end_stream: true)
      else
        task.async do
          generate_response(request, stream, &block)
        end
      end
    end
    
    stream.on(:data) do |chunk|
      body.write(chunk.to_s) unless chunk.empty?
    end
    
    stream.on(:half_close) do
      # We are no longer receiving any more data frames:
      body.finish
    end
    
    stream.on(:close) do |error|
      body.stop(EOFError.new(error)) if error
    end
  end
  
  start_connection
  @reader.wait
end

#reusable?Boolean

Returns:



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

def reusable?
  !@goaway || !@stream.closed?
end

#start_connectionObject



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

def start_connection
  @reader ||= read_in_background
end

#versionObject



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

def version
  VERSION
end