7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/puppet_x/aco/util.rb', line 7
def self.request(uri_str, method = nil, cookies = [], limit = 10)
raise ArgumentError, 'too many HTTP redirects' if limit == 0
uri = URI(uri_str)
case method
when 'POST' then
reqmethod = Net::HTTP::Post
when 'HEAD' then
reqmethod = Net::HTTP::Head
else
reqmethod = Net::HTTP::Get
end
request = reqmethod.new(uri.request_uri, {'user-agent' => 'Mozilla/5.0 (Puppet)', 'cookie' => cookies.join('; ')})
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
response_cookies = response.get_fields('set-cookie')
if !response_cookies.nil?
response_cookies.each { |c| cookies.push(c.split('; ')[0]) }
end
case response
when Net::HTTPSuccess then
return uri, response, cookies
when Net::HTTPRedirection then
location = response['location']
request(location, method = method, cookies, limit - 1)
else
return response.value, nil
end
end
|