Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8294626: Improve URL protocol lower casing
Reviewed-by: dfuchs
  • Loading branch information
cl4es committed Sep 30, 2022
1 parent 052a924 commit 3efbd5f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 11 deletions.
14 changes: 9 additions & 5 deletions src/java.base/share/classes/java/net/URL.java
Expand Up @@ -440,7 +440,7 @@ public URL(String protocol, String host, int port, String file,
}
}

protocol = toLowerCase(protocol);
protocol = lowerCaseProtocol(protocol);
this.protocol = protocol;
if (host != null) {

Expand Down Expand Up @@ -633,7 +633,7 @@ public URL(URL context, String spec, URLStreamHandler handler)
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;
Expand Down Expand Up @@ -1384,9 +1384,13 @@ public URLStreamHandler run() {
* 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);
}
Expand Down
4 changes: 2 additions & 2 deletions src/java.base/share/classes/java/net/URLConnection.java
Expand Up @@ -1092,7 +1092,7 @@ public void setDefaultUseCaches(boolean defaultusecaches) {
* @since 9
*/
public static void setDefaultUseCaches(String protocol, boolean defaultVal) {
protocol = protocol.toLowerCase(Locale.US);
protocol = URL.lowerCaseProtocol(protocol);
defaultCaching.put(protocol, defaultVal);
}

Expand All @@ -1108,7 +1108,7 @@ public static void setDefaultUseCaches(String protocol, boolean defaultVal) {
* @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 {
Expand Down
15 changes: 11 additions & 4 deletions src/java.base/share/classes/sun/net/util/URLUtil.java
Expand Up @@ -49,17 +49,24 @@ public static String urlNoFragString(URL url) {

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("://");
}

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) {
Expand Down

0 comments on commit 3efbd5f

Please sign in to comment.