8278972: Improve URL supports

Reviewed-by: skoivu, rhalade, alanb
This commit is contained in:
Daniel Fuchs 2022-01-28 11:28:07 +00:00 committed by Henry Jen
parent 395bb5b7f9
commit 9e051d5396
5 changed files with 656 additions and 81 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2002, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2022, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -27,7 +27,11 @@ package com.sun.jndi.dns;
import java.net.MalformedURLException;
import java.util.Hashtable;
import java.net.URI;
import java.net.URISyntaxException;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Locale;
import java.util.StringTokenizer;
import com.sun.jndi.toolkit.url.Uri;
@ -56,6 +60,24 @@ import com.sun.jndi.toolkit.url.UrlUtil;
public class DnsUrl extends Uri {
private static final String PARSE_MODE_PROP = "com.sun.jndi.dnsURLParsing";
private static final ParseMode DEFAULT_PARSE_MODE = ParseMode.COMPAT;
public static final ParseMode PARSE_MODE;
static {
PrivilegedAction<String> action = () ->
System.getProperty(PARSE_MODE_PROP, DEFAULT_PARSE_MODE.toString());
ParseMode parseMode = DEFAULT_PARSE_MODE;
try {
@SuppressWarnings("removal")
String mode = AccessController.doPrivileged(action);
parseMode = ParseMode.valueOf(mode.toUpperCase(Locale.ROOT));
} catch (Throwable t) {
parseMode = DEFAULT_PARSE_MODE;
} finally {
PARSE_MODE = parseMode;
}
}
private String domain; // domain name of the context
@ -71,19 +93,58 @@ public class DnsUrl extends Uri {
StringTokenizer st = new StringTokenizer(urlList, " ");
while (st.hasMoreTokens()) {
urls[i++] = new DnsUrl(st.nextToken());
try {
urls[i++] = new DnsUrl(validateURI(st.nextToken()));
} catch (URISyntaxException e) {
MalformedURLException mue = new MalformedURLException(e.getMessage());
mue.initCause(e);
throw mue;
}
}
DnsUrl[] trimmed = new DnsUrl[i];
System.arraycopy(urls, 0, trimmed, 0, i);
return trimmed;
}
@Override
protected ParseMode parseMode() {
return PARSE_MODE;
}
@Override
protected final boolean isSchemeOnly(String uri) {
return isDnsSchemeOnly(uri);
}
@Override
protected boolean checkSchemeOnly(String uri, String scheme) {
return uri.equals(scheme + ":") || uri.equals(scheme + "://");
}
@Override
protected final MalformedURLException newInvalidURISchemeException(String uri) {
return new MalformedURLException(
uri + " is not a valid DNS pseudo-URL");
}
private static boolean isDnsSchemeOnly(String uri) {
return "dns:".equals(uri) || "dns://".equals(uri);
}
private static String validateURI(String uri) throws URISyntaxException {
// no validation in legacy parsing mode
if (PARSE_MODE == ParseMode.LEGACY) return uri;
// special case of scheme-only URIs
if (isDnsSchemeOnly(uri)) return uri;
// use java.net.URI to validate the uri syntax
return new URI(uri).toString();
}
public DnsUrl(String url) throws MalformedURLException {
super(url);
if (!scheme.equals("dns")) {
throw new MalformedURLException(
url + " is not a valid DNS pseudo-URL");
throw newInvalidURISchemeException(url);
}
domain = path.startsWith("/")