def parse(self, uri): u"""parses a URI in the form of <scheme>://<username>:<password>@<host>:<port>/<path>?<query>#<fragment> """ if uri == b'*': self.path = u'*' return if not uri: raise InvalidURI(u'empty URI') uri, _, fragment = uri.partition(b'#') uri, _, query_string = uri.partition(b'?') scheme, scheme_exists, uri = uri.rpartition(b'://') if not scheme_exists and uri.startswith(b'//'): uri = uri[2:] authority, _, path = uri.partition(b'/') path = b'/%s' % path userinfo, _, hostport = authority.rpartition(b'@') username, _, password = userinfo.partition(b':') if hostport.endswith(b']') and hostport.startswith(b'['): host, port = hostport, b'' else: host, _, port = hostport.rpartition(b':') unquote = self.unquote path = u'/'.join([unquote(seq).replace(u'/', u'%2f') for seq in path.split(b'/')]) if path.startswith(u'//'): raise InvalidURI(u'Invalid path: must not start with "//"') try: scheme = scheme.decode('ascii') except UnicodeDecodeError: raise InvalidURI(u'Invalid scheme: must be ascii') self.tuple = ( scheme, unquote(username), unquote(password), unquote(host), port, path, QueryString.encode(QueryString.decode(query_string)), unquote(fragment) )
def query(self, query): self.query_string = QueryString.encode(query, self.encoding)
def query(self): return tuple(QueryString.decode(self.query_string, self.encoding))
def parse(self, uri): r"""Parses a well formed absolute or relative URI. foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment | _____________________|__ / \ / \ urn:example:animal:ferret:nose https://username:password@[::1]:8090/some/path?query#fragment <scheme>://<username>:<password>@<host>:<port>/<path>?<query>#<fragment> [<scheme>:][//[<username>[:<password>]@][<host>][:<port>]/]<path>[?<query>][#<fragment>] """ if isinstance(uri, Unicode): try: uri = uri.encode('ascii') except UnicodeEncodeError: raise TypeError('URI must be ASCII bytes.') if type(self) is URI and b':' in uri: self.scheme = uri.split(b':', 1)[0].lower() if type(self) is not URI: return self.parse(uri) if uri and uri.strip(b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'): raise InvalidURI(_(u'Invalid URI: must consist of printable ASCII characters without whitespace.')) uri, __, fragment = uri.partition(b'#') uri, __, query_string = uri.partition(b'?') scheme, authority_exists, uri = uri.rpartition(b'://') if not authority_exists and uri.startswith(b'//'): uri = uri[2:] authority_exists = True if not authority_exists and b':' in uri: scheme, __, uri = uri.partition(b':') authority, path = b'', uri if authority_exists: authority, __, path = uri.partition(b'/') path = b'%s%s' % (__, path) userinfo, __, hostport = authority.rpartition(b'@') username, __, password = userinfo.partition(b':') if ':' in hostport and not hostport.endswith(b']'): host, __, port = hostport.rpartition(b':') else: host, port = hostport, b'' unquote = self.unquote path = u'/'.join([unquote(seq).replace(u'/', u'%2f') for seq in path.split(b'/')]) try: scheme = scheme.decode('ascii').lower() except UnicodeDecodeError: raise InvalidURI(_(u'Invalid scheme: must be ASCII.')) if scheme and scheme.strip(u'abcdefghijklmnopqrstuvwxyz0123456789.-+'): raise InvalidURI(_(u'Invalid scheme: must only contain alphanumeric letters or plus, dash, dot.')) if query_string: query_string = QueryString.encode(QueryString.decode(query_string, self.encoding), self.encoding) self.tuple = ( scheme, unquote(username), unquote(password), self._unquote_host(host), port, path, query_string.decode(self.encoding), unquote(fragment) )
def parse(self, uri): r"""Parses a well formed absolute or relative URI. foo://example.com:8042/over/there?name=ferret#nose \_/ \______________/\_________/ \_________/ \__/ | | | | | scheme authority path query fragment | _____________________|__ / \ / \ urn:example:animal:ferret:nose https://username:password@[::1]:8090/some/path?query#fragment <scheme>://<username>:<password>@<host>:<port>/<path>?<query>#<fragment> [<scheme>:][//[<username>[:<password>]@][<host>][:<port>]/]<path>[?<query>][#<fragment>] """ if isinstance(uri, Unicode): try: uri = uri.encode('ascii') except UnicodeEncodeError: raise TypeError('URI must be ASCII bytes.') if type(self) is URI and b':' in uri: self.scheme = uri.split(b':', 1)[0].lower() if type(self) is not URI: return self.parse(uri) if uri and uri.strip(b'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'): raise InvalidURI(_(u'Invalid URI: must consist of printable ASCII characters without whitespace.')) uri, __, fragment = uri.partition(b'#') uri, __, query_string = uri.partition(b'?') scheme, authority_exists, uri = uri.rpartition(b'://') if not authority_exists and uri.startswith(b'//'): uri = uri[2:] authority_exists = True if not authority_exists and b':' in uri: scheme, __, uri = uri.partition(b':') authority, path = b'', uri if authority_exists: authority, __, path = uri.partition(b'/') path = b'%s%s' % (__, path) userinfo, __, hostport = authority.rpartition(b'@') username, __, password = userinfo.partition(b':') if b':' in hostport and not hostport.endswith(b']'): host, __, port = hostport.rpartition(b':') else: host, port = hostport, b'' unquote = self.unquote path = u'/'.join([unquote(seq).replace(u'/', u'%2f') for seq in path.split(b'/')]) try: scheme = scheme.decode('ascii').lower() except UnicodeDecodeError: raise InvalidURI(_(u'Invalid scheme: must be ASCII.')) if scheme and scheme.strip(u'abcdefghijklmnopqrstuvwxyz0123456789.-+'): raise InvalidURI(_(u'Invalid scheme: must only contain alphanumeric letters or plus, dash, dot.')) if query_string: query_string = QueryString.encode(QueryString.decode(query_string, self.encoding), self.encoding) self.tuple = ( scheme, unquote(username), unquote(password), self._unquote_host(host), port, path, query_string.decode(self.encoding), unquote(fragment) )