private String fixupAuthority ( String uriAuthority , String charset ) throws URIException { // Lowercase the host part of the uriAuthority; don't destroy any // userinfo capitalizations. Make sure no illegal characters in // domainlabel substring of the uri authority. if ( uriAuthority != null ) { // Get rid of any trailing escaped spaces: // http://www.archive.org%20. Rare but happens. // TODO: reevaluate: do IE or firefox do such mid-URI space-removal? // if not, we shouldn't either. while ( uriAuthority . endsWith ( ESCAPED_SPACE ) ) { uriAuthority = uriAuthority . substring ( 0 , uriAuthority . length ( ) - 3 ) ; } // lowercase & IDN-punycode only the domain portion int atIndex = uriAuthority . indexOf ( COMMERCIAL_AT ) ; int portColonIndex = uriAuthority . indexOf ( COLON , ( atIndex < 0 ) ? 0 : atIndex ) ; if ( atIndex < 0 && portColonIndex < 0 ) { // most common case: neither userinfo nor port return fixupDomainlabel ( uriAuthority ) ; } else if ( atIndex < 0 && portColonIndex > - 1 ) { // next most common: port but no userinfo String domain = fixupDomainlabel ( uriAuthority . substring ( 0 , portColonIndex ) ) ; String port = uriAuthority . substring ( portColonIndex ) ; return domain + port ; } else if ( atIndex > - 1 && portColonIndex < 0 ) { // uncommon: userinfo, no port String userinfo = ensureMinimalEscaping ( uriAuthority . substring ( 0 , atIndex + 1 ) , charset ) ; String domain = fixupDomainlabel ( uriAuthority . substring ( atIndex + 1 ) ) ; return userinfo + domain ; } else { // uncommon: userinfo, port String userinfo = ensureMinimalEscaping ( uriAuthority . substring ( 0 , atIndex + 1 ) , charset ) ; String domain = fixupDomainlabel ( uriAuthority . substring ( atIndex + 1 , portColonIndex ) ) ; String port = uriAuthority . substring ( portColonIndex ) ; return userinfo + domain + port ; } } return uriAuthority ; }
Fixup authority portion of URI by removing any stray encoded spaces lowercasing any domain names and applying IDN - punycoding to Unicode domains .