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.

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.



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/async/http/protocol/http2.rb', line 51

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
  
  @count = 0
end

Instance Attribute Details

#countObject (readonly)

Returns the value of attribute count.



71
72
73
# File 'lib/async/http/protocol/http2.rb', line 71

def count
  @count
end

Class Method Details

.client(stream) ⇒ Object



34
35
36
# File 'lib/async/http/protocol/http2.rb', line 34

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

.server(stream) ⇒ Object



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

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

Instance Method Details

#call(request) ⇒ Object



174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'lib/async/http/protocol/http2.rb', line 174

def call(request)
  @count += 1
  
  request.version ||= self.version
  
  stream = @controller.new_stream
  
  headers = {
    SCHEME => HTTPS,
    METHOD => request.method.to_s,
    PATH => request.path.to_s,
    AUTHORITY => request.authority.to_s,
  }.merge(request.headers)
  
  finished = Async::Notification.new
  
  response = Response.new
  response.version = self.version
  response.headers = {}
  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
    
    finished.signal
  end
  
  stream.on(:data) do |chunk|
    body.write(chunk.to_s) unless chunk.empty?
  end
  
  stream.on(:close) do
    body.finish
  end
  
  if request.body.nil? or request.body.empty?
    stream.headers(headers, end_stream: true)
    request.body.read if request.body
  else
    stream.headers(headers, end_stream: false)
    
    request.body.each do |chunk|
      stream.data(chunk, end_stream: false)
    end
      
    stream.data("", end_stream: true)
  end
  
  start_connection
  @stream.flush
  
  # Async.logger.debug(self) {"Stream flushed, waiting for signal."}
  finished.wait
  
  # Async.logger.debug(self) {"Stream finished: #{response.inspect}"}
  return response
end

#closeObject



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

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

#multiplexObject

Multiple requests can be processed at the same time.



74
75
76
# File 'lib/async/http/protocol/http2.rb', line 74

def multiplex
  @controller.remote_settings[:settings_max_concurrent_streams]
end

#read_in_background(task: Task.current) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/async/http/protocol/http2.rb', line 90

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



108
109
110
111
112
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/async/http/protocol/http2.rb', line 108

def receive_requests(task: Task.current, &block)
  # emits new streams opened by the client
  @controller.on(:stream) do |stream|
    @count += 1
    
    request = Request.new
    request.version = self.version
    request.headers = {}
    body = Body::Writable.new
    request.body = body
    
    stream.on(:headers) do |headers|
      # puts "Got request headers: #{headers.inspect}"
      headers.each do |key, value|
        if key == METHOD
          request.method = value
        elsif key == PATH
          request.path = value
        elsif key == AUTHORITY
          request.authority = value
        else
          request.headers[key] = value
        end
      end
    end
    
    stream.on(:data) do |chunk|
      # puts "Got request data: #{chunk.inspect}"
      body.write(chunk.to_s) unless chunk.empty?
    end
    
    stream.on(:half_close) do
      # puts "Generating response..."
      response = yield request
      
      # puts "Finishing body..."
      body.finish
      
      # puts "Sending response..."
      # send response
      headers = {STATUS => response.status.to_s}
      headers.update(response.headers)
      
      # puts "Sending headers #{headers}"
      if response.body.nil? or response.body.empty?
        stream.headers(headers, end_stream: true)
        response.body.read if response.body
      else
        stream.headers(headers, end_stream: false)
        
        # puts "Streaming body..."
        response.body.each do |chunk|
          # puts "Sending chunk #{chunk.inspect}"
          stream.data(chunk, end_stream: false)
        end
        
        # puts "Ending stream..."
        stream.data("", end_stream: true)
      end
    end
  end
  
  start_connection
  @reader.wait
end

#reusable?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'lib/async/http/protocol/http2.rb', line 78

def reusable?
  @reader.alive?
end

#start_connectionObject



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

def start_connection
  @reader ||= read_in_background
end

#versionObject



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

def version
  VERSION
end