#NEWLINE# def urinorm( uri ) : #NEWLINE# #INDENT# '\n    Normalize a URI\n    ' #NEWLINE# if isinstance ( uri , bytes ) : #NEWLINE# #INDENT# uri = str ( uri , encoding = 'utf-8' ) #NEWLINE# #UNINDENT# uri = uri . encode ( 'ascii' , errors = 'oid_percent_escape' ) . decode ( 'utf-8' ) #NEWLINE# illegal_mo = uri_illegal_char_re . search ( uri ) #NEWLINE# if illegal_mo : #NEWLINE# #INDENT# raise ValueError ( ( 'Illegal characters in URI: %r at position %s' % ( illegal_mo . group ( ) , illegal_mo . start ( ) ) ) ) #NEWLINE# #UNINDENT# uri_mo = uri_re . match ( uri ) #NEWLINE# scheme = uri_mo . group ( 2 ) #NEWLINE# if ( scheme is None ) : #NEWLINE# #INDENT# raise ValueError ( 'No scheme specified' ) #NEWLINE# #UNINDENT# scheme = scheme . lower ( ) #NEWLINE# if ( scheme not in ( 'http' , 'https' ) ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Not an absolute HTTP or HTTPS URI: %r' % ( uri , ) ) ) #NEWLINE# #UNINDENT# authority = uri_mo . group ( 4 ) #NEWLINE# if ( authority is None ) : #NEWLINE# #INDENT# raise ValueError ( ( 'Not an absolute URI: %r' % ( uri , ) ) ) #NEWLINE# #UNINDENT# authority_mo = authority_re . match ( authority ) #NEWLINE# if ( authority_mo is None ) : #NEWLINE# #INDENT# raise ValueError ( ( 'URI does not have a valid authority: %r' % ( uri_mo , ) ) ) #NEWLINE# #UNINDENT# ( userinfo , host , port ) = authority_mo . groups ( ) #NEWLINE# if ( userinfo is None ) : #NEWLINE# #INDENT# userinfo = '' #NEWLINE# #UNINDENT# if ( '%' in host ) : #NEWLINE# #INDENT# host = host . lower ( ) #NEWLINE# host = pct_encoded_re . sub ( _pct_encoded_replace , host ) #NEWLINE# host = host . encode ( 'idna' ) . decode ( ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# host = host . lower ( ) #NEWLINE# #UNINDENT# if port : #NEWLINE# #INDENT# if ( ( port == ':' ) or ( ( scheme == 'http' ) and ( port == ':80' ) ) or ( ( scheme == 'https' ) and ( port == ':443' ) ) ) : #NEWLINE# #INDENT# port = '' #NEWLINE# #UNINDENT# #UNINDENT# else : #NEWLINE# #INDENT# port = '' #NEWLINE# #UNINDENT# authority = ( ( userinfo + host ) + port ) #NEWLINE# path = uri_mo . group ( 5 ) #NEWLINE# path = pct_encoded_re . sub ( _pct_encoded_replace_unreserved , path ) #NEWLINE# path = remove_dot_segments ( path ) #NEWLINE# if ( not path ) : #NEWLINE# #INDENT# path = '/' #NEWLINE# #UNINDENT# query = uri_mo . group ( 6 ) #NEWLINE# if ( query is None ) : #NEWLINE# #INDENT# query = '' #NEWLINE# #UNINDENT# fragment = uri_mo . group ( 8 ) #NEWLINE# if ( fragment is None ) : #NEWLINE# #INDENT# fragment = '' #NEWLINE# #UNINDENT# return ( ( ( ( ( scheme + '://' ) + authority ) + path ) + query ) + fragment )