class PuppetForgeServer::Http::HttpClient

Public Class Methods

new(cache = nil) click to toggle source
# File lib/puppet_forge_server/http/http_client.rb, line 28
def initialize(cache = nil)
  cache = cache_instance if cache.nil?
  cache.extend(PuppetForgeServer::Utils::FilteringInspecter)
  @log = PuppetForgeServer::Logger.get
  @cache = cache
  @uri_options= {
    'User-Agent' => "Puppet-Forge-Server/#{PuppetForgeServer::VERSION}",
    :allow_redirections => :safe,
  }
  # OpenURI does not work with  http_proxy=http://username:password@proxyserver:port/
  # so split the proxy_url and feed it basic authentication.
  if ENV.has_key?('http_proxy')
    proxy = URI.parse(ENV['http_proxy'])
    if proxy.userinfo != nil
      @uri_options[:proxy_http_basic_authentication] = [
        "#{proxy.scheme}://#{proxy.host}:#{proxy.port}",
        proxy.userinfo.split(':')[0],
        proxy.userinfo.split(':')[1]
      ]
    end
  end

end

Public Instance Methods

download(url) click to toggle source
# File lib/puppet_forge_server/http/http_client.rb, line 69
def download(url)
  open_uri(url)
end
get(url) click to toggle source
# File lib/puppet_forge_server/http/http_client.rb, line 65
def get(url)
  open_uri(url).read
end
inspect() click to toggle source
# File lib/puppet_forge_server/http/http_client.rb, line 73
def inspect
  cache_inspected = @cache.inspect_without [ :@data ]
  cache_inspected.gsub!(/>$/, ", @size=#{@cache.size}>")
  inspected = inspect_without [ :@cache ]
  inspected.gsub(/>$/, ", @cache=#{cache_inspected}>")
end
post_file(url, file_hash, options = {}) click to toggle source
# File lib/puppet_forge_server/http/http_client.rb, line 52
def post_file(url, file_hash, options = {})
  options = { :http => {}, :headers => {}}.merge(options)

  uri = URI.parse(url)
  http = Net::HTTP.new(uri.host, uri.port)
  options[:http].each {|k,v| http.call(k, v) }

  req = Net::HTTP::Post::Multipart.new uri.path, "file" => UploadIO.new(File.open(file_hash[:tempfile]), file_hash[:type], file_hash[:filename])
  options[:headers].each {|k,v| req[k] = v }

  http.request(req)
end

Private Instance Methods

open_uri(url) click to toggle source
# File lib/puppet_forge_server/http/http_client.rb, line 82
def open_uri(url)
  hit_or_miss = @cache.include?(url) ? 'HIT' : 'MISS'
  @log.info "Cache in RAM memory size: #{@cache.size}, #{hit_or_miss} for url: #{url}"
  contents = @cache.fetch(url) do
    tmpfile = ::Timeout.timeout(10) do
      PuppetForgeServer::Logger.get.debug "Fetching data for url: #{url} from remote server"
      open(url, @uri_options)
    end
    contents = tmpfile.read
    tmpfile.close
    contents
  end
  @log.debug "Data for url: #{url} fetched, #{contents.size} bytes"
  StringIO.new(contents)
end