response_check.rb 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. require 'uri'
  2. require 'net/http'
  3. require 'openssl'
  4. 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'
  5. DEFAULT_SSL_NO_VERIFY = false
  6. class ResponseCheck
  7. ## Class Methods with class variables.
  8. def self.useragent=(useragent)
  9. case useragent
  10. when nil
  11. @@useragent = DEFAULT_USERAGENT
  12. else
  13. @@useragent = useragent
  14. end
  15. end
  16. def self.useragent
  17. @@useragent
  18. end
  19. # a class accessor
  20. def self.ssl_no_verify=(verify)
  21. @@ssl_no_verify = verify
  22. end
  23. def self.ssl_no_verify
  24. @@ssl_no_verify
  25. end
  26. ## Instance Methods following:
  27. attr_reader :get_uri, :parsed_uri
  28. attr_accessor :ssl_no_verify, :useragent, :wanted_dst_uri
  29. def initialize(options)
  30. # required at init
  31. @get_uri = options[:get_uri] || abort("#{self.class.name}: no get_uri given")
  32. # optional at init. can be set by accessor.
  33. @wanted_dst_uri = options[:wanted_dst_uri] ||= nil
  34. @ssl_no_verify = options[:ssl_no_verify] ||= @@ssl_no_verify ||= DEFAULT_SSL_NO_VERIFY
  35. @useragent = options[:useragent] ||= @@useragent ||= DEFAULT_USERAGENT
  36. # those are just reader
  37. @parsed_uri ||= uri(@get_uri)
  38. end
  39. ### response code mappings following here.
  40. ### also see: https://en.wikipedia.org/wiki/URL_redirection
  41. def http2xx?
  42. response.is_a?(Net::HTTPSuccess) # any http 2xx code
  43. end
  44. def success?
  45. http2xx?
  46. end
  47. def http200?
  48. response.is_a?(Net::HTTPOK)
  49. end
  50. def ok?
  51. http200?
  52. end
  53. def http3xx?
  54. response.is_a?(Net::HTTPRedirection) # any http 3xx code
  55. end
  56. def redirect? # an alias for 'http3xx?'
  57. http3xx?
  58. end
  59. def http301?
  60. http3xx? && response.is_a?(Net::HTTPMovedPermanently) # 301
  61. end
  62. def permanent_redirect? # an alias for 'http301?'
  63. http301?
  64. end
  65. def http302?
  66. http3xx? && response.is_a?(Net::HTTPFound) # 302
  67. end
  68. def http303?
  69. http3xx? && response.is_a?(Net::HTTPMovedTemporarily) # 303
  70. end
  71. def see_other_temp_redirect? # an alias for 'http303?'
  72. http303?
  73. end
  74. def http304?
  75. http3xx? && response.is_a?(Net::HTTPNotModified) # 304
  76. end
  77. def http305?
  78. http3xx? && response.is_a?(Net::HTTPUseProxy) # 305
  79. end
  80. def http307?
  81. http3xx? && response.is_a?(Net::HTTPTemporaryRedirect) # 307
  82. end
  83. def strict_temp_redirect? # an alias for 'http307?'
  84. http307?
  85. end
  86. def body
  87. response.body
  88. end
  89. def body_contains?(contains)
  90. begin
  91. if body.to_s.match(contains)
  92. true
  93. else
  94. false
  95. end
  96. rescue
  97. false
  98. end
  99. end
  100. def redirected_path
  101. response['location'].sub(/#{Regexp.escape("#{uri.scheme}://#{uri.host}")}/, '') if redirect?
  102. end
  103. def redirected_uri
  104. response['location'] if redirect?
  105. end
  106. def response
  107. if @useragent.to_s.size < 5
  108. abort "#{self.class.name}: useragent '" + @useragent + "' is very short. Thus, just wrong."
  109. #else
  110. # puts "Using UA: " + @useragent
  111. end
  112. @verify_mode ||= OpenSSL::SSL::VERIFY_NONE if @ssl_no_verify
  113. @response ||= Net::HTTP.start(@parsed_uri.host,
  114. @parsed_uri.port,
  115. :use_ssl => @parsed_uri.scheme == 'https',
  116. :verify_mode => @verify_mode
  117. ) {|http|
  118. http.request(Net::HTTP::Get.new(@parsed_uri.path, {'User-Agent' => @useragent}) )
  119. }
  120. return @response
  121. end
  122. private
  123. def uri(uri)
  124. check_uri = URI.parse(uri.to_s)
  125. if not check_uri.path.to_s.size > 0
  126. check_uri.path = '/'
  127. end
  128. if not check_uri.scheme =~ /http[s]?/
  129. abort "Must be either http or https"
  130. end
  131. URI.parse(check_uri.to_s)
  132. end
  133. end