123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- require 'uri'
- require 'net/http'
- require 'openssl'
- DEFAULT_USERAGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
- DEFAULT_SSL_NO_VERIFY = false
- class ResponseCheck
- ## Class Methods with class variables.
- def self.useragent=(useragent)
- case useragent
- when nil
- @@useragent = DEFAULT_USERAGENT
- else
- @@useragent = useragent
- end
- end
- def self.useragent
- @@useragent
- end
- # a class accessor
- def self.ssl_no_verify=(verify)
- @@ssl_no_verify = verify
- end
- def self.ssl_no_verify
- @@ssl_no_verify
- end
- ## Instance Methods following:
- attr_reader :get_uri, :parsed_uri
- attr_accessor :ssl_no_verify, :useragent, :wanted_dst_uri
- def initialize(options)
- # required at init
- @get_uri = options[:get_uri] || abort("#{self.class.name}: no get_uri given")
- # optional at init. can be set by accessor.
- @wanted_dst_uri = options[:wanted_dst_uri] ||= nil
- @ssl_no_verify = options[:ssl_no_verify] ||= @@ssl_no_verify ||= DEFAULT_SSL_NO_VERIFY
- @useragent = options[:useragent] ||= @@useragent ||= DEFAULT_USERAGENT
- # those are just reader
- @parsed_uri ||= uri(@get_uri)
- end
- ### response code mappings following here.
- ### also see: https://en.wikipedia.org/wiki/URL_redirection
- def http2xx?
- response.is_a?(Net::HTTPSuccess) # any http 2xx code
- end
- def success?
- http2xx?
- end
- def http200?
- response.is_a?(Net::HTTPOK)
- end
- def ok?
- http200?
- end
- def http3xx?
- response.is_a?(Net::HTTPRedirection) # any http 3xx code
- end
- def redirect? # an alias for 'http3xx?'
- http3xx?
- end
- def http301?
- http3xx? && response.is_a?(Net::HTTPMovedPermanently) # 301
- end
- def permanent_redirect? # an alias for 'http301?'
- http301?
- end
- def http302?
- http3xx? && response.is_a?(Net::HTTPFound) # 302
- end
- def http303?
- http3xx? && response.is_a?(Net::HTTPMovedTemporarily) # 303
- end
- def see_other_temp_redirect? # an alias for 'http303?'
- http303?
- end
- def http304?
- http3xx? && response.is_a?(Net::HTTPNotModified) # 304
- end
- def http305?
- http3xx? && response.is_a?(Net::HTTPUseProxy) # 305
- end
- def http307?
- http3xx? && response.is_a?(Net::HTTPTemporaryRedirect) # 307
- end
- def strict_temp_redirect? # an alias for 'http307?'
- http307?
- end
- def body
- response.body
- end
- def body_contains?(contains)
- begin
- if body.to_s.match(contains)
- true
- else
- false
- end
- rescue
- false
- end
- end
- def redirected_path
- response['location'].sub(/#{Regexp.escape("#{uri.scheme}://#{uri.host}")}/, '') if redirect?
- end
- def redirected_uri
- response['location'] if redirect?
- end
- def response
- if @useragent.to_s.size < 5
- abort "#{self.class.name}: useragent '" + @useragent + "' is very short. Thus, just wrong."
- #else
- # puts "Using UA: " + @useragent
- end
- @verify_mode ||= OpenSSL::SSL::VERIFY_NONE if @ssl_no_verify
- @response ||= Net::HTTP.start(@parsed_uri.host,
- @parsed_uri.port,
- :use_ssl => @parsed_uri.scheme == 'https',
- :verify_mode => @verify_mode
- ) {|http|
- http.request(Net::HTTP::Get.new(@parsed_uri.path, {'User-Agent' => @useragent}) )
- }
- return @response
- end
- private
- def uri(uri)
- check_uri = URI.parse(uri.to_s)
- if not check_uri.path.to_s.size > 0
- check_uri.path = '/'
- end
- if not check_uri.scheme =~ /http[s]?/
- abort "Must be either http or https"
- end
- URI.parse(check_uri.to_s)
- end
- end
|