8294626: Improve URL protocol lower casing

Reviewed-by: dfuchs
This commit is contained in:
Claes Redestad 2022-09-30 16:03:53 +00:00
parent 052a924985
commit 3efbd5f0fa
3 changed files with 22 additions and 11 deletions

View File

@ -440,7 +440,7 @@ public final class URL implements java.io.Serializable {
}
}
protocol = toLowerCase(protocol);
protocol = lowerCaseProtocol(protocol);
this.protocol = protocol;
if (host != null) {
@ -633,7 +633,7 @@ public final class URL implements java.io.Serializable {
for (i = start ; !aRef && (i < limit) &&
((c = spec.charAt(i)) != '/') ; i++) {
if (c == ':') {
String s = toLowerCase(spec.substring(start, i));
String s = lowerCaseProtocol(spec.substring(start, i));
if (isValidProtocol(s)) {
newProtocol = s;
start = i + 1;
@ -1384,9 +1384,13 @@ public final class URL implements java.io.Serializable {
* Returns the protocol in lower case. Special cases known protocols
* to avoid loading locale classes during startup.
*/
static String toLowerCase(String protocol) {
if (protocol.equals("jrt") || protocol.equals("file") || protocol.equals("jar")) {
return protocol;
static String lowerCaseProtocol(String protocol) {
if (protocol.equals("jrt")) {
return "jrt";
} else if (protocol.equals("file")) {
return "file";
} else if (protocol.equals("jar")) {
return "jar";
} else {
return protocol.toLowerCase(Locale.ROOT);
}

View File

@ -1092,7 +1092,7 @@ public abstract class URLConnection {
* @since 9
*/
public static void setDefaultUseCaches(String protocol, boolean defaultVal) {
protocol = protocol.toLowerCase(Locale.US);
protocol = URL.lowerCaseProtocol(protocol);
defaultCaching.put(protocol, defaultVal);
}
@ -1108,7 +1108,7 @@ public abstract class URLConnection {
* @since 9
*/
public static boolean getDefaultUseCaches(String protocol) {
Boolean protoDefault = defaultCaching.get(protocol.toLowerCase(Locale.US));
Boolean protoDefault = defaultCaching.get(URL.lowerCaseProtocol(protocol));
if (protoDefault != null) {
return protoDefault.booleanValue();
} else {

View File

@ -49,8 +49,14 @@ public class URLUtil {
String protocol = url.getProtocol();
if (protocol != null) {
/* protocol is compared case-insensitive, so convert to lowercase */
protocol = protocol.toLowerCase();
/* protocol is compared case-insensitive, so convert to lowercase
* if needed. URL will store from lower-cased String literals for
* built-in protocols, so avoid calling toLowerCase for these and
* use identity tests for speed
*/
if (protocol != "file" && protocol != "jrt" && protocol != "jar") {
protocol = protocol.toLowerCase();
}
strForm.append(protocol);
strForm.append("://");
}
@ -58,8 +64,9 @@ public class URLUtil {
String host = url.getHost();
if (host != null) {
/* host is compared case-insensitive, so convert to lowercase */
host = host.toLowerCase();
strForm.append(host);
if (!host.isEmpty()) {
strForm.append(host.toLowerCase());
}
int port = url.getPort();
if (port == -1) {