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



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
63
64
# File 'lib/httpx/plugins/upgrade.rb', line 33

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

  return unless 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 fetch_response(request, selector, options) if response.status == 101 && !connection.hijacked

  response
end