Class: Loob::UriValidator

Inherits:
Object show all
Defined in:
lib/loob/uri_validator.rb

Constant Summary collapse

DEFAULT_HTTP_OK_CODES =

possible error statuses: :invalid_scheme :invalid_host :invalid_content_type :invalid_format :moved_permanently :not_accessible

[
  Net::HTTPMovedPermanently,
  Net::HTTPOK,
  Net::HTTPCreated,
  Net::HTTPAccepted,
  Net::HTTPNonAuthoritativeInformation,
  Net::HTTPPartialContent,
  Net::HTTPFound,
  Net::HTTPTemporaryRedirect,
  Net::HTTPSeeOther
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ UriValidator

Returns a new instance of UriValidator.



43
44
45
46
47
48
49
# File 'lib/loob/uri_validator.rb', line 43

def initialize(options = {})
  self.schemes = options[:schemes] || []
  self.hosts = options[:hosts] || []
  self.content_types = options[:content_types] || []
  self.response_codes = options[:response_codes] || DEFAULT_HTTP_OK_CODES
  self.port = options[:port] || 80
end

Instance Attribute Details

#content_typesObject

Returns the value of attribute content_types.



27
28
29
# File 'lib/loob/uri_validator.rb', line 27

def content_types
  @content_types
end

#errorObject (readonly)

Returns the value of attribute error.



28
29
30
# File 'lib/loob/uri_validator.rb', line 28

def error
  @error
end

#hostsObject

Returns the value of attribute hosts.



27
28
29
# File 'lib/loob/uri_validator.rb', line 27

def hosts
  @hosts
end

#portObject

Returns the value of attribute port.



27
28
29
# File 'lib/loob/uri_validator.rb', line 27

def port
  @port
end

#response_codesObject

Returns the value of attribute response_codes.



27
28
29
# File 'lib/loob/uri_validator.rb', line 27

def response_codes
  @response_codes
end

#schemesObject

Returns the value of attribute schemes.



27
28
29
# File 'lib/loob/uri_validator.rb', line 27

def schemes
  @schemes
end

Class Method Details

.valid_domain?(str, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/loob/uri_validator.rb', line 37

def valid_domain?(str, options = {})
  self.new(options).valid_domain?(str)
end

.valid_uri?(str, options = {}) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/loob/uri_validator.rb', line 32

def valid_uri?(str, options = {})
  self.new(options).valid_uri?(str)
end

.valid_url?Boolean

Returns:

  • (Boolean)


35
36
37
# File 'lib/loob/uri_validator.rb', line 35

def valid_uri?(str, options = {})
  self.new(options).valid_uri?(str)
end

Instance Method Details

#valid_domain?(str) ⇒ Boolean

Returns:

  • (Boolean)


100
101
102
103
104
105
106
107
# File 'lib/loob/uri_validator.rb', line 100

def valid_domain?(str)
  begin
    uri = URI.parse(str)
    return valid_uri?("#{uri.scheme || 'http'}://#{uri.host || str}/")
  rescue URI::InvalidURIError, URI::NameError
    return set_error(:invalid_format)
  end
end

#valid_uri?(str) ⇒ Boolean Also known as: valid_url?

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/loob/uri_validator.rb', line 55

def valid_uri?(str)
  reset_error!
  begin
    moved_retry ||= false
not_allowed_retry ||= false
    
    uri = URI.parse(str)
    uri.path = '/' if uri.path.length < 1        
    return set_error(:invalid_scheme) unless validate_scheme(uri)
    return set_error(:invalid_host) unless validate_host(uri)
    
    http = Net::HTTP.new(uri.host, (uri.scheme == 'https') ? 443 : port)
    if uri.scheme == 'https'
      http.use_ssl = true
      http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    end
    response = not_allowed_retry ? http.request_get(uri.path) {|r|} : http.request_head(uri.path)        
    raise unless validate_response_code(response)
    return set_error(:invalid_content_type) unless validate_content_type(response)      
  rescue URI::InvalidURIError
    return set_error(:invalid_format)
  rescue
    if response.is_a?(Net::HTTPMovedPermanently)
      unless moved_retry
        moved_retry = true
        str += '/' # In case webserver is just adding a /
        retry
      else
        return set_error(:moved_permanently)            
      end
    elsif response.is_a?(Net::HTTPMethodNotAllowed)
      unless not_allowed_retry # Retry with a GET 
        not_allowed_retry = true
        retry
      else
        return set_error(:not_accessible)
      end
    else       
      return set_error(:not_accessible)
    end
  end
  return true
end