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