Module: Puppet::HTTP::Proxy

Defined in:
lib/puppet/http/proxy.rb

Overview

API:

  • public

Class Method Summary collapse

Class Method Details

.http_proxy_env ⇒ Object

API:

  • public



19
20
21
22
23
24
25
26
27
28
# File 'lib/puppet/http/proxy.rb', line 19

def self.http_proxy_env
  # Returns a URI object if proxy is set, or nil
  proxy_env = ENV.fetch("http_proxy", nil) || ENV.fetch("HTTP_PROXY", nil)
  begin
    return URI.parse(proxy_env) if proxy_env
  rescue URI::InvalidURIError
    return nil
  end
  nil
end

.http_proxy_host ⇒ Object

API:

  • public



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/puppet/http/proxy.rb', line 74

def self.http_proxy_host
  env = http_proxy_env

  if env and env.host
    return env.host
  end

  if Puppet.settings[:http_proxy_host] == 'none'
    return nil
  end

  Puppet.settings[:http_proxy_host]
end

.http_proxy_password ⇒ Object

API:

  • public



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/puppet/http/proxy.rb', line 112

def self.http_proxy_password
  env = http_proxy_env

  if env and env.password
    return env.password
  end

  if Puppet.settings[:http_proxy_user] == 'none' or Puppet.settings[:http_proxy_password] == 'none'
    return nil
  end

  Puppet.settings[:http_proxy_password]
end

.http_proxy_port ⇒ Object

API:

  • public



88
89
90
91
92
93
94
95
96
# File 'lib/puppet/http/proxy.rb', line 88

def self.http_proxy_port
  env = http_proxy_env

  if env and env.port
    return env.port
  end

  Puppet.settings[:http_proxy_port]
end

.http_proxy_user ⇒ Object

API:

  • public



98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/puppet/http/proxy.rb', line 98

def self.http_proxy_user
  env = http_proxy_env

  if env and env.user
    return env.user
  end

  if Puppet.settings[:http_proxy_user] == 'none'
    return nil
  end

  Puppet.settings[:http_proxy_user]
end

.no_proxy ⇒ Object

API:

  • public



126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/puppet/http/proxy.rb', line 126

def self.no_proxy
  no_proxy_env = ENV.fetch("no_proxy", nil) || ENV.fetch("NO_PROXY", nil)

  if no_proxy_env
    return no_proxy_env
  end

  if Puppet.settings[:no_proxy] == 'none'
    return nil
  end

  Puppet.settings[:no_proxy]
end

.no_proxy?(dest) ⇒ Boolean

The documentation around the format of the no_proxy variable seems inconsistent. Some suggests the use of the * as a way of matching any hosts under a domain, e.g.:

*.example.com

Other documentation suggests that just a leading '.' indicates a domain level exclusion, e.g.:

.example.com

We'll accommodate both here.

Returns:

API:

  • public



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
65
66
67
68
69
70
71
72
# File 'lib/puppet/http/proxy.rb', line 38

def self.no_proxy?(dest)
  no_proxy = self.no_proxy
  unless no_proxy
    return false
  end

  unless dest.is_a? URI
    begin
      dest = URI.parse(dest)
    rescue URI::InvalidURIError
      return false
    end
  end

  no_proxy.split(/\s*,\s*/).each do |d|
    host, port = d.split(':')
    host = Regexp.escape(host).gsub('\*', '.*')

    # If this no_proxy entry specifies a port, we want to match it against
    # the destination port.  Otherwise just match hosts.
    if port
      no_proxy_regex  = /#{host}:#{port}$/
      dest_string     = "#{dest.host}:#{dest.port}"
    else
      no_proxy_regex  = /#{host}$/
      dest_string     = dest.host.to_s
    end

    if no_proxy_regex.match(dest_string)
      return true
    end
  end

  false
end

.proxy(uri) ⇒ Object

API:

  • public



7
8
9
10
11
12
13
14
15
16
17
# File 'lib/puppet/http/proxy.rb', line 7

def self.proxy(uri)
  if http_proxy_host && !no_proxy?(uri)
    Net::HTTP.new(uri.host, uri.port, http_proxy_host, http_proxy_port, http_proxy_user, http_proxy_password)
  else
    http = Net::HTTP.new(uri.host, uri.port, nil, nil, nil, nil)
    # Net::HTTP defaults the proxy port even though we said not to
    # use one. Set it to nil so caller is not surprised
    http.proxy_port = nil
    http
  end
end