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

View file

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

View file

@ -49,8 +49,14 @@ public class URLUtil {
String protocol = url.getProtocol(); String protocol = url.getProtocol();
if (protocol != null) { if (protocol != null) {
/* protocol is compared case-insensitive, so convert to lowercase */ /* protocol is compared case-insensitive, so convert to lowercase
protocol = protocol.toLowerCase(); * 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(protocol);
strForm.append("://"); strForm.append("://");
} }
@ -58,8 +64,9 @@ public class URLUtil {
String host = url.getHost(); String host = url.getHost();
if (host != null) { if (host != null) {
/* host is compared case-insensitive, so convert to lowercase */ /* host is compared case-insensitive, so convert to lowercase */
host = host.toLowerCase(); if (!host.isEmpty()) {
strForm.append(host); strForm.append(host.toLowerCase());
}
int port = url.getPort(); int port = url.getPort();
if (port == -1) { if (port == -1) {