class Yawast::Shared::Http

Public Class Methods

check_not_found(uri, file) click to toggle source
# File lib/shared/http.rb, line 117
def self.check_not_found(uri, file)
  fake_uri = uri.copy

  fake_uri.path = if file
                    "/#{SecureRandom.hex}.html"
                  else
                    "/#{SecureRandom.hex}/"
                  end

  if Yawast::Shared::Http.get_status_code(fake_uri) != '404'
    # crazy 404 handling
    return false
  end

  true
end
get(uri, headers = nil) click to toggle source
# File lib/shared/http.rb, line 65
def self.get(uri, headers = nil)
  get_with_code(uri, headers)[:body]
end
get_headers(extra_headers = nil) click to toggle source

noinspection RubyStringKeysInHashInspection

# File lib/shared/http.rb, line 135
def self.get_headers(extra_headers = nil)
  headers = if @cookie.nil?
              {'User-Agent' => HTTP_UA}
            else
              {'User-Agent' => HTTP_UA, 'Cookie' => @cookie}
            end

  headers.merge! extra_headers unless extra_headers.nil?

  headers
end
get_http(uri) click to toggle source
# File lib/shared/http.rb, line 107
def self.get_http(uri)
  req = if @proxy
          Net::HTTP.new(uri.host, uri.port, @proxy_host, @proxy_port)
        else
          Net::HTTP.new(uri.host, uri.port)
        end

  req
end
get_json(uri) click to toggle source
# File lib/shared/http.rb, line 69
def self.get_json(uri)
  body = ''

  begin
    req = get_http(uri)
    req.use_ssl = uri.scheme == 'https'
    res = req.request_get(uri, {'User-Agent' => "YAWAST/#{Yawast::VERSION}"})
    body = res.read_body
  rescue # rubocop:disable Style/RescueStandardError, Lint/HandleExceptions
    # do nothing for now
  end

  JSON.parse body
end
get_raw(uri, headers = nil) click to toggle source
# File lib/shared/http.rb, line 38
def self.get_raw(uri, headers = nil)
  res = nil

  begin
    req = get_http(uri)
    req.use_ssl = uri.scheme == 'https'
    res = req.request_get(uri, get_headers(headers))
  rescue => e # rubocop:disable Style/RescueStandardError
    Yawast::Utilities.puts_error "Error sending request to #{uri} - '#{e.message}'"
  end

  res
end
get_status_code(uri) click to toggle source
# File lib/shared/http.rb, line 99
def self.get_status_code(uri)
  req = get_http(uri)
  req.use_ssl = uri.scheme == 'https'
  res = req.head(uri, get_headers)

  res.code
end
get_with_code(uri, headers = nil) click to toggle source
# File lib/shared/http.rb, line 52
def self.get_with_code(uri, headers = nil)
  res = get_raw(uri, headers)
  body = ''
  code = nil

  unless res.nil?
    body = res.read_body
    code = res.code
  end

  {body: body, code: code}
end
head(uri) click to toggle source
# File lib/shared/http.rb, line 23
def self.head(uri)
  begin
    req = get_http(uri)
    req.use_ssl = uri.scheme == 'https'
    req.head(uri, get_headers)
  rescue # rubocop:disable Style/RescueStandardError
    # if we get here, the HEAD failed - but GET may work
    # so we silently fail back to using GET instead
    req = get_http(uri)
    req.use_ssl = uri.scheme == 'https'
    res = req.request_get(uri, get_headers)
    res
  end
end
put(uri, body, headers = nil) click to toggle source
# File lib/shared/http.rb, line 84
def self.put(uri, body, headers = nil)
  ret = nil

  begin
    req = get_http(uri)
    req.use_ssl = uri.scheme == 'https'
    res = req.request_put(uri, body, get_headers(headers))
    ret = res.read_body
  rescue # rubocop:disable Style/RescueStandardError, Lint/HandleExceptions
    # do nothing for now
  end

  ret
end
setup(proxy, cookie) click to toggle source
# File lib/shared/http.rb, line 9
def self.setup(proxy, cookie)
  if !proxy.nil? && proxy.include?(':')
    @proxy_host, @proxy_port = proxy.split(':')
    @proxy = true

    puts "Using Proxy: #{proxy}"
  else
    @proxy = false
  end

  @cookie = cookie
  puts "Using Cookie: #{@cookie}" unless @cookie.nil?
end