Class: HTTPX::Plugins::Proxy::Parameters

Inherits:
Object
  • Object
show all
Defined in:
lib/httpx/plugins/proxy.rb,
sig/plugins/proxy.rbs

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri: nil, scheme: nil, username: nil, password: nil, no_proxy: nil, **extra) ⇒ Parameters

Returns a new instance of Parameters.

Parameters:

  • uri: (generic_uri, Array[generic_uri]) (defaults to: nil)
  • scheme: (String) (defaults to: nil)
  • username: (String) (defaults to: nil)
  • password: (String) (defaults to: nil)
  • no_proxy: (Array[generic_uri], generic_uri) (defaults to: nil)
  • (Object)


45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/httpx/plugins/proxy.rb', line 45

def initialize(uri: nil, scheme: nil, username: nil, password: nil, no_proxy: nil, **extra)
  @no_proxy = Array(no_proxy) if no_proxy
  @uris = Array(uri)
  uri = @uris.first

  @username = username
  @password = password

  @ns = 0

  if uri
    @uri = uri.is_a?(URI::Generic) ? uri : URI(uri)
    @username ||= @uri.user
    @password ||= @uri.password
  end

  @scheme = scheme

  return unless @uri && @username && @password

  @authenticator = nil
  @scheme ||= infer_default_auth_scheme(@uri)

  return unless @scheme

  @username = CGI.unescape(@username) if @username
  @password = CGI.unescape(@password) if @password

  @authenticator = load_authenticator(@scheme, @username, @password, **extra)
end

Instance Attribute Details

#no_proxyArray[String]? (readonly)

Returns the value of attribute no_proxy.

Returns:

  • (Array[String], nil)


43
44
45
# File 'lib/httpx/plugins/proxy.rb', line 43

def no_proxy
  @no_proxy
end

#passwordString? (readonly)

Returns the value of attribute password.

Returns:

  • (String, nil)


43
44
45
# File 'lib/httpx/plugins/proxy.rb', line 43

def password
  @password
end

#schemeString? (readonly)

Returns the value of attribute scheme.

Returns:

  • (String, nil)


43
44
45
# File 'lib/httpx/plugins/proxy.rb', line 43

def scheme
  @scheme
end

#uriURI::Generic? (readonly)

Returns the value of attribute uri.

Returns:

  • (URI::Generic, nil)


43
44
45
# File 'lib/httpx/plugins/proxy.rb', line 43

def uri
  @uri
end

#usernameString? (readonly)

Returns the value of attribute username.

Returns:

  • (String, nil)


43
44
45
# File 'lib/httpx/plugins/proxy.rb', line 43

def username
  @username
end

Instance Method Details

#==(other) ⇒ Boolean

Parameters:

  • (Object)

Returns:

  • (Boolean)


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/httpx/plugins/proxy.rb', line 107

def ==(other)
  case other
  when Parameters
    @uri == other.uri &&
      @username == other.username &&
      @password == other.password &&
      @scheme == other.scheme
  when URI::Generic, String
    proxy_uri = @uri.dup
    proxy_uri.user = @username
    proxy_uri.password = @password
    other_uri = other.is_a?(URI::Generic) ? other : URI.parse(other)
    proxy_uri == other_uri
  else
    super
  end
end

#authenticate(*args) ⇒ String?

Parameters:

  • (Object)

Returns:

  • (String, nil)


101
102
103
104
105
# File 'lib/httpx/plugins/proxy.rb', line 101

def authenticate(*args)
  return unless @authenticator

  @authenticator.authenticate(*args)
end

#can_authenticate?(*args) ⇒ Boolean

Parameters:

  • (Object)

Returns:

  • (Boolean)


95
96
97
98
99
# File 'lib/httpx/plugins/proxy.rb', line 95

def can_authenticate?(*args)
  return false unless @authenticator && @authenticator.respond_to?(:can_authenticate?)

  @authenticator.can_authenticate?(*args)
end

#infer_default_auth_scheme(uri) ⇒ String?

Parameters:

  • uri (URI::Generic)

Returns:

  • (String, nil)


127
128
129
130
131
132
133
134
# File 'lib/httpx/plugins/proxy.rb', line 127

def infer_default_auth_scheme(uri)
  case uri.scheme
  when "socks5"
    uri.scheme
  when "http", "https"
    "basic"
  end
end

#load_authenticator(scheme, username, password, **extra) ⇒ _Authenticator

Parameters:

  • scheme (String)
  • username (String)
  • password (String)
  • (Object)

Returns:



136
137
138
139
140
141
142
# File 'lib/httpx/plugins/proxy.rb', line 136

def load_authenticator(scheme, username, password, **extra)
  auth_scheme = scheme.to_s.capitalize

  require_relative "auth/#{scheme}" unless defined?(Authentication) && Authentication.const_defined?(auth_scheme, false)

  Authentication.const_get(auth_scheme).new(username, password, **extra)
end

#shiftvoid

This method returns an undefined value.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/httpx/plugins/proxy.rb', line 76

def shift
  # TODO: this operation must be synchronized
  @ns += 1
  @uri = @uris[@ns]

  return unless @uri

  @uri = URI(@uri) unless @uri.is_a?(URI::Generic)

  scheme = infer_default_auth_scheme(@uri)

  return unless scheme != @scheme

  @scheme = scheme
  @username = username || @uri.user
  @password = password || @uri.password
  @authenticator = load_authenticator(scheme, @username, @password)
end