|
| 1 | +# adapted from feedvalidator, original copyright license is |
| 2 | +# |
| 3 | +# Copyright (c) 2002-2006, Sam Ruby, Mark Pilgrim, Joseph Walton, and Phil Ringnalda |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +importre |
| 24 | + |
| 25 | +iana_schemes= [# http://www.iana.org/assignments/uri-schemes.html |
| 26 | +"ftp","http","gopher","mailto","news","nntp","telnet","wais", |
| 27 | +"file","prospero","z39.50s","z39.50r","cid","mid","vemmi", |
| 28 | +"service","imap","nfs","acap","rtsp","tip","pop","data","dav", |
| 29 | +"opaquelocktoken","sip","sips","tel","fax","modem","ldap", |
| 30 | +"https","soap.beep","soap.beeps","xmlrpc.beep","xmlrpc.beeps", |
| 31 | +"urn","go","h323","ipp","tftp","mupdate","pres","im","mtqp", |
| 32 | +"iris.beep","dict","snmp","crid","tag","dns","info" |
| 33 | +] |
| 34 | + |
| 35 | +rfc2396_re=re.compile("([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}"+ |
| 36 | +"[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%,#]*$") |
| 37 | +rfc2396_full_re=re.compile("[a-zA-Z][0-9a-zA-Z+\\-\\.]*:(//)?"+ |
| 38 | +"[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%,#]+$") |
| 39 | +urn_re=re.compile(r"^[Uu][Rr][Nn]:[a-zA-Z0-9][a-zA-Z0-9-]{1,31}:([a-zA-Z0-9()+,\.:=@;$_!*'\-]|%[0-9A-Fa-f]{2})+$") |
| 40 | +tag_re=re.compile(r"^tag:([a-z0-9\-\._]+?@)?[a-z0-9\.\-]+?,\d{4}(-\d{2}(-\d{2})?)?:[0-9a-zA-Z;/\?:@&=+$\.\-_!~*'\(\)%,]*(#[0-9a-zA-Z;/\?:@&=+$\.\-_!~*'\(\)%,]*)?$") |
| 41 | + |
| 42 | +defisValidURI(value,uriPattern=rfc2396_re): |
| 43 | +scheme=value.split(':')[0].lower() |
| 44 | +ifscheme=='tag': |
| 45 | +ifnottag_re.match(value): |
| 46 | +returnFalse,"invalid-tag-uri" |
| 47 | +elifscheme=="urn": |
| 48 | +ifnoturn_re.match(value): |
| 49 | +returnFalse,"invalid-urn" |
| 50 | +elifnoturiPattern.match(value): |
| 51 | +urichars_re=re.compile("[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%,#]") |
| 52 | +forcinvalue: |
| 53 | +iford(c)<128andnoturichars_re.match(c): |
| 54 | +returnFalse,"invalid-uri-char" |
| 55 | +else: |
| 56 | +try: |
| 57 | +ifuriPattern.match(value.encode('idna')): |
| 58 | +returnFalse,"uri-not-iri" |
| 59 | +except: |
| 60 | +pass |
| 61 | +returnFalse,"invalid-uri" |
| 62 | +elifschemein ['http','ftp']: |
| 63 | +ifnotre.match('^\w+://[^/].*',value): |
| 64 | +returnFalse,"invalid-http-or-ftp-uri" |
| 65 | +elifvalue.find(':')>=0andscheme.isalpha()andschemenotiniana_schemes: |
| 66 | +returnFalse,"unregistered-scheme" |
| 67 | +returnTrue,"" |
| 68 | + |
| 69 | +defisValidIRI(value): |
| 70 | +try: |
| 71 | +ifvalue:value=value.encode('idna') |
| 72 | +except: |
| 73 | +pass |
| 74 | +returnisValidURI(value) |
| 75 | + |
| 76 | +defisValidFullyQualifiedURI(value): |
| 77 | +returnisValidURI(value,rfc2396_full_re) |
| 78 | + |