Class: Async::HTTP::Protocol::HTTP2
- Inherits:
-
Object
- Object
- Async::HTTP::Protocol::HTTP2
- 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
- RESPONSE_VERSION =
'HTTP/2'.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(controller, stream) ⇒ HTTP2
constructor
A new instance of HTTP2.
-
#multiplex ⇒ Object
Multiple requests can be processed at the same time.
- #read_in_background(task: Task.current) ⇒ Object
- #receive_requests(&block) ⇒ Object
- #reusable? ⇒ Boolean
- #send_request(authority, method, path, headers = {}, body = nil) ⇒ Object
Constructor Details
#initialize(controller, stream) ⇒ HTTP2
Returns a new instance of HTTP2.
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/async/http/protocol/http2.rb', line 50 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 if @controller.is_a? ::HTTP2::Client @controller.send_connection_preface @reader = read_in_background end 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
#close ⇒ Object
94 95 96 97 98 |
# File 'lib/async/http/protocol/http2.rb', line 94 def close Async.logger.debug(self) {"Closing connection"} @reader.stop @stream.close end |
#multiplex ⇒ Object
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
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/async/http/protocol/http2.rb', line 82 def read_in_background(task: Task.current) task.async do |nested_task| buffer = Async::IO::BinaryString.new while data = @stream.io.read(1024*8, buffer) @controller << data end Async.logger.debug(self) {"Connection reset by peer!"} end end |
#receive_requests(&block) ⇒ Object
100 101 102 103 104 105 106 107 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 |
# File 'lib/async/http/protocol/http2.rb', line 100 def receive_requests(&block) # emits new streams opened by the client @controller.on(:stream) do |stream| request = Request.new request.version = "HTTP/2.0" request.headers = Headers.new # stream.on(:active) { } # fires when stream transitions to open state # stream.on(:close) { } # stream is closed by client and server stream.on(:headers) do |headers| headers.each do |key, value| if key == METHOD request.method = value elsif key == PATH request.path = value elsif key == AUTHORITY request. = value else request.headers[key] = value end end end stream.on(:data) do |body| request.body = body end stream.on(:half_close) do response = yield request # send response stream.headers(STATUS => response[0].to_s) stream.headers(response[1]) unless response[1].empty? response[2].each do |chunk| stream.data(chunk, end_stream: false) end stream.data("", end_stream: true) end end while data = @stream.io.read(1024) @controller << data end end |
#reusable? ⇒ Boolean
78 79 80 |
# File 'lib/async/http/protocol/http2.rb', line 78 def reusable? @reader.alive? end |
#send_request(authority, method, path, headers = {}, body = nil) ⇒ Object
151 152 153 154 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 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 |
# File 'lib/async/http/protocol/http2.rb', line 151 def send_request(, method, path, headers = {}, body = nil) stream = @controller.new_stream internal_headers = { SCHEME => HTTPS, METHOD => method, PATH => path, AUTHORITY => , }.merge(headers) stream.headers(internal_headers, end_stream: true) # if body # body.each do |chunk| # stream.data(chunk, end_stream: false) # end # # stream.data("", end_stream: true) # end response = Response.new response.version = RESPONSE_VERSION response.headers = Headers.new response.body = Async::IO::BinaryString.new stream.on(:headers) do |headers| # Async.logger.debug(self) {"Stream headers: #{headers.inspect}"} 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 end stream.on(:data) do |body| # Async.logger.debug(self) {"Stream data: #{body.size} bytes"} response.body << body end finished = Async::Notification.new stream.on(:half_close) do # Async.logger.debug(self) {"Stream half-closed."} end stream.on(:close) do # Async.logger.debug(self) {"Stream closed, sending signal."} finished.signal end @stream.flush # Async.logger.debug(self) {"Stream flushed, waiting for signal."} finished.wait # Async.logger.debug(self) {"Stream finished: #{response.inspect}"} return response end |