Module: HTTPX::Plugins::Upgrade::InstanceMethods

Defined in:
lib/httpx/plugins/upgrade.rb

Instance Method Summary collapse

Instance Method Details

#fetch_response(request, selector, options) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/httpx/plugins/upgrade.rb', line 31

def fetch_response(request, selector, options)
  response = super

  if response
    return response unless response.is_a?(Response)

    return response unless response.headers.key?("upgrade")

    upgrade_protocol = response.headers["upgrade"].split(/ *, */).first

    return response unless upgrade_protocol && options.upgrade_handlers.key?(upgrade_protocol)

    protocol_handler = options.upgrade_handlers[upgrade_protocol]

    return response unless protocol_handler

    log { "upgrading to #{upgrade_protocol}..." }
    connection = find_connection(request.uri, selector, options)

    # do not upgrade already upgraded connections
    return if connection.upgrade_protocol == upgrade_protocol

    protocol_handler.call(connection, request, response)

    # keep in the loop if the server is switching, unless
    # the connection has been hijacked, in which case you want
    # to terminante immediately
    return if response.status == 101 && !connection.hijacked
  end

  response
end