Class: PuppetX::Bodeco::HTTP
- Inherits:
-
Object
- Object
- PuppetX::Bodeco::HTTP
- Defined in:
- lib/puppet_x/bodeco/util.rb
Direct Known Subclasses
Constant Summary collapse
- FOLLOW_LIMIT =
5
- URI_UNSAFE =
%r{[^\-_.!~*'()a-zA-Z\d;/?:@&=+$,\[\]%]}.freeze
Instance Method Summary collapse
- #content(uri, option = { limit: FOLLOW_LIMIT }) ⇒ Object
- #download(uri, file_path, option = { limit: FOLLOW_LIMIT }) ⇒ Object
- #follow_redirect(uri, option = { limit: FOLLOW_LIMIT }, &block) ⇒ Object
- #generate_request(uri) ⇒ Object
-
#initialize(_url, options) ⇒ HTTP
constructor
A new instance of HTTP.
- #safe_escape(uri) ⇒ Object
Constructor Details
#initialize(_url, options) ⇒ HTTP
Returns a new instance of HTTP.
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/puppet_x/bodeco/util.rb', line 70 def initialize(_url, ) @username = [:username] @password = [:password] @cookie = [:cookie] @insecure = [:insecure] if [:proxy_server] uri = URI([:proxy_server]) uri = URI("#{[:proxy_type]}://#{[:proxy_server]}") unless uri.scheme @proxy_addr = uri.hostname @proxy_port = uri.port end ENV['SSL_CERT_FILE'] = File.(File.join(__FILE__, '..', 'cacert.pem')) if Facter.value(:osfamily) == 'windows' && !ENV.key?('SSL_CERT_FILE') end |
Instance Method Details
#content(uri, option = { limit: FOLLOW_LIMIT }) ⇒ Object
131 132 133 134 135 |
# File 'lib/puppet_x/bodeco/util.rb', line 131 def content(uri, option = { limit: FOLLOW_LIMIT }) follow_redirect(uri, option) do |response| return response.body end end |
#download(uri, file_path, option = { limit: FOLLOW_LIMIT }) ⇒ Object
121 122 123 124 125 126 127 128 129 |
# File 'lib/puppet_x/bodeco/util.rb', line 121 def download(uri, file_path, option = { limit: FOLLOW_LIMIT }) follow_redirect(uri, option) do |response| File.open file_path, 'wb' do |io| response.read_body do |chunk| io.write chunk end end end end |
#follow_redirect(uri, option = { limit: FOLLOW_LIMIT }, &block) ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/puppet_x/bodeco/util.rb', line 94 def follow_redirect(uri, option = { limit: FOLLOW_LIMIT }, &block) http_opts = if uri.scheme == 'https' { use_ssl: true, verify_mode: (@insecure ? OpenSSL::SSL::VERIFY_NONE : OpenSSL::SSL::VERIFY_PEER) } else { use_ssl: false } end Net::HTTP.start(uri.host, uri.port, @proxy_addr, @proxy_port, http_opts) do |http| http.request(generate_request(uri)) do |response| case response when Net::HTTPSuccess yield response when Net::HTTPRedirection limit = option[:limit] - 1 raise Puppet::Error, "Redirect limit exceeded, last url: #{uri}" if limit.negative? location = safe_escape(response['location']) new_uri = URI(location) new_uri = URI(uri.to_s + location) if new_uri.relative? follow_redirect(new_uri, limit: limit, &block) else raise Puppet::Error, "HTTP Error Code #{response.code}\nURL: #{uri}\nContent:\n#{response.body}" end end end end |
#generate_request(uri) ⇒ Object
86 87 88 89 90 91 92 |
# File 'lib/puppet_x/bodeco/util.rb', line 86 def generate_request(uri) header = @cookie && { 'Cookie' => @cookie } request = Net::HTTP::Get.new(uri.request_uri, header) request.basic_auth(@username, @password) if @username && @password request end |
#safe_escape(uri) ⇒ Object
137 138 139 140 141 |
# File 'lib/puppet_x/bodeco/util.rb', line 137 def safe_escape(uri) uri.to_s.gsub(URI_UNSAFE) do |match| "%#{match.unpack('H2' * match.bytesize).join('%').upcase}" end end |