Class: Tipi::HTTP2Adapter
- Inherits:
-
Object
- Object
- Tipi::HTTP2Adapter
- Defined in:
- lib/tipi/http2_adapter.rb
Overview
HTTP2 server adapter
Constant Summary collapse
- UPGRADE_MESSAGE =
<<~HTTP.gsub("\n", "\r\n") HTTP/1.1 101 Switching Protocols Connection: Upgrade Upgrade: h2c HTTP
Class Method Summary collapse
Instance Method Summary collapse
- #close ⇒ Object
-
#each(&block) ⇒ Object
Iterates over incoming requests.
- #finalize_client_loop ⇒ Object
- #get_rx_count ⇒ Object
- #get_tx_count ⇒ Object
-
#initialize(conn, opts, upgrade_headers = nil, upgrade_body = nil) ⇒ HTTP2Adapter
constructor
A new instance of HTTP2Adapter.
- #send_frame(data) ⇒ Object
- #set_request_for_transfer_count(request) ⇒ Object
- #start_stream(stream, &block) ⇒ Object
- #unset_request_for_transfer_count(request) ⇒ Object
- #upgrade ⇒ Object
Constructor Details
#initialize(conn, opts, upgrade_headers = nil, upgrade_body = nil) ⇒ HTTP2Adapter
Returns a new instance of HTTP2Adapter.
25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/tipi/http2_adapter.rb', line 25 def initialize(conn, opts, upgrade_headers = nil, upgrade_body = nil) @conn = conn @opts = opts @upgrade_headers = upgrade_headers @upgrade_body = upgrade_body @first = true @rx = (upgrade_headers && upgrade_headers[':rx']) || 0 @tx = (upgrade_headers && upgrade_headers[':tx']) || 0 @interface = ::HTTP2::Server.new @connection_fiber = Fiber.current @interface.on(:frame, &method(:send_frame)) @streams = {} end |
Class Method Details
.upgrade_each(socket, opts, headers, body, &block) ⇒ Object
20 21 22 23 |
# File 'lib/tipi/http2_adapter.rb', line 20 def self.upgrade_each(socket, opts, headers, body, &block) adapter = new(socket, opts, headers, body) adapter.each(&block) end |
Instance Method Details
#close ⇒ Object
107 108 109 110 |
# File 'lib/tipi/http2_adapter.rb', line 107 def close @conn.shutdown if @conn.respond_to?(:shutdown) rescue nil @conn.close end |
#each(&block) ⇒ Object
Iterates over incoming requests
68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/tipi/http2_adapter.rb', line 68 def each(&block) @interface.on(:stream) { |stream| start_stream(stream, &block) } upgrade if @upgrade_headers @conn.recv_loop do |data| @rx += data.bytesize @interface << data end rescue SystemCallError, IOError, HTTP2::Error::Error # ignore ensure finalize_client_loop end |
#finalize_client_loop ⇒ Object
100 101 102 103 104 105 |
# File 'lib/tipi/http2_adapter.rb', line 100 def finalize_client_loop @interface = nil @streams.each_key(&:stop) @conn.shutdown if @conn.respond_to?(:shutdown) rescue nil @conn.close end |
#get_rx_count ⇒ Object
82 83 84 85 86 |
# File 'lib/tipi/http2_adapter.rb', line 82 def get_rx_count count = @rx @rx = 0 count end |
#get_tx_count ⇒ Object
88 89 90 91 92 |
# File 'lib/tipi/http2_adapter.rb', line 88 def get_tx_count count = @tx @tx = 0 count end |
#send_frame(data) ⇒ Object
40 41 42 43 44 45 46 47 48 49 |
# File 'lib/tipi/http2_adapter.rb', line 40 def send_frame(data) if @transfer_count_request @transfer_count_request.tx_incr(data.bytesize) end @conn << data rescue Polyphony::BaseException raise rescue Exception => e @connection_fiber.transfer e end |
#set_request_for_transfer_count(request) ⇒ Object
112 113 114 |
# File 'lib/tipi/http2_adapter.rb', line 112 def set_request_for_transfer_count(request) @transfer_count_request = request end |
#start_stream(stream, &block) ⇒ Object
94 95 96 97 98 |
# File 'lib/tipi/http2_adapter.rb', line 94 def start_stream(stream, &block) stream = HTTP2StreamHandler.new(self, stream, @conn, @first, &block) @first = nil if @first @streams[stream] = true end |
#unset_request_for_transfer_count(request) ⇒ Object
116 117 118 119 120 |
# File 'lib/tipi/http2_adapter.rb', line 116 def unset_request_for_transfer_count(request) return unless @transfer_count_request == request @transfer_count_request = nil end |
#upgrade ⇒ Object
58 59 60 61 62 63 64 65 |
# File 'lib/tipi/http2_adapter.rb', line 58 def upgrade @conn << UPGRADE_MESSAGE @tx += UPGRADE_MESSAGE.bytesize settings = @upgrade_headers['http2-settings'] @interface.upgrade(settings, @upgrade_headers, @upgrade_body || '') ensure @upgrade_headers = nil end |