Sha256: 062de2df231b8d34a4873f813807b9e2a3db53b96392e151100a8754aada6bce

Contents?: true

Size: 1.5 KB

Versions: 11

Compression:

Stored size: 1.5 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Plugins
    #
    # This plugin adds support for upgrading an HTTP/1.1 connection to HTTP/2
    # via an Upgrade: h2 response declaration
    #
    # https://gitlab.com/os85/httpx/wikis/Connection-Upgrade#h2
    #
    module H2
      class << self
        def extra_options(options)
          options.merge(upgrade_handlers: options.upgrade_handlers.merge("h2" => self))
        end

        def call(connection, _request, _response)
          connection.upgrade_to_h2
        end
      end

      module ConnectionMethods
        using URIExtensions

        def upgrade_to_h2
          prev_parser = @parser

          if prev_parser
            prev_parser.reset
            @inflight -= prev_parser.requests.size
          end

          @parser = Connection::HTTP2.new(@write_buffer, @options)
          set_parser_callbacks(@parser)
          @upgrade_protocol = "h2"

          # what's happening here:
          # a deviation from the state machine is done to perform the actions when a
          # connection is closed, without transitioning, so the connection is kept in the pool.
          # the state is reset to initial, so that the socket reconnect works out of the box,
          # while the parser is already here.
          purge_after_closed
          transition(:idle)

          prev_parser.requests.each do |req|
            req.transition(:idle)
            send(req)
          end
        end
      end
    end
    register_plugin(:"upgrade/h2", H2)
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
httpx-1.3.4 lib/httpx/plugins/upgrade/h2.rb
httpx-1.3.3 lib/httpx/plugins/upgrade/h2.rb
httpx-1.3.2 lib/httpx/plugins/upgrade/h2.rb
httpx-1.3.1 lib/httpx/plugins/upgrade/h2.rb
httpx-1.3.0 lib/httpx/plugins/upgrade/h2.rb
httpx-1.2.6 lib/httpx/plugins/upgrade/h2.rb
httpx-1.2.4 lib/httpx/plugins/upgrade/h2.rb
httpx-1.2.3 lib/httpx/plugins/upgrade/h2.rb
httpx-1.2.2 lib/httpx/plugins/upgrade/h2.rb
httpx-1.2.1 lib/httpx/plugins/upgrade/h2.rb
httpx-1.2.0 lib/httpx/plugins/upgrade/h2.rb