Path: | lib/feed_tools/vendor/html5/lib/html5/filters/rfc3987.rb |
Last Update: | Tue Mar 25 21:42:12 +0000 2008 |
adapted from feedvalidator, original copyright license is
Copyright (c) 2002-2006, Sam Ruby, Mark Pilgrim, Joseph Walton, and Phil Ringnalda
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
ALLOWED_SCHEMES | = | iana_schemes + ['javascript'] |
RFC2396 | = | Regexp.new("^([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%,#]*$", Regexp::MULTILINE) |
URN | = | Regexp.new("^[Uu][Rr][Nn]:[a-zA-Z0-9][a-zA-Z0-9-]{1,31}:([a-zA-Z0-9()+,\.:=@;$_!*'\-]|%[0-9A-Fa-f]{2})+$") |
TAG | = | Regexp.new("^tag:([a-z0-9\\-\._]+?@)?[a-z0-9\.\-]+?,\d{4}(-\d{2}(-\d{2})?)?:[0-9a-zA-Z;/\?:@&=+$\.\-_!~*'\(\)%,]*(#[0-9a-zA-Z;/\?:@&=+$\.\-_!~*'\(\)%,]*)?$") |
# File lib/feed_tools/vendor/html5/lib/html5/filters/rfc3987.rb, line 87 87: def is_valid_fully_qualified_uri(value) 88: is_valid_uri(value, rfc2396_full) 89: end
# File lib/feed_tools/vendor/html5/lib/html5/filters/rfc3987.rb, line 77 77: def is_valid_iri(value) 78: begin 79: if value.length > 0 80: value = value.encode('idna') 81: end 82: rescue 83: end 84: is_valid_uri(value) 85: end
# File lib/feed_tools/vendor/html5/lib/html5/filters/rfc3987.rb, line 39 39: def is_valid_uri(value, uri_pattern = RFC2396) 40: scheme = value.split(':').first 41: scheme.downcase! if scheme 42: if scheme == 'tag' 43: if !TAG.match(value) 44: return false, "invalid-tag-uri" 45: end 46: elsif scheme == "urn" 47: if !URN.match(value) 48: return false, "invalid-urn" 49: end 50: elsif uri_pattern.match(value).to_a.reject{|i| i == ''}.compact.length == 0 || uri_pattern.match(value)[0] != value 51: urichars = Regexp.new("^[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%,#]$", Regexp::MULTILINE) 52: if value.length > 0 53: value.each_byte do |b| 54: if b < 128 and !urichars.match([b].pack('c*')) 55: return false, "invalid-uri-char" 56: end 57: end 58: else 59: begin 60: if uri_pattern.match(value.encode('idna')) 61: return false, "uri-not-iri" 62: end 63: rescue 64: end 65: return false, "invalid-uri" 66: end 67: elsif ['http','ftp'].include?(scheme) 68: if !value.match(%r{^\w+://[^/].*}) 69: return false, "invalid-http-or-ftp-uri" 70: end 71: elsif value.index(':') && scheme.match(/^[a-z]+$/) && !ALLOWED_SCHEMES.include?(scheme) 72: return false, "invalid-scheme" 73: end 74: return true, "" 75: end