Skip to content

Commit 749d480

Browse files
Dhamoder Nalladfuch
authored andcommitted
8305763: Parsing a URI with an underscore goes through a silent exception, negatively impacting performance
Reviewed-by: dfuchs
1 parent 3ccb3c0 commit 749d480

File tree

1 file changed

+29
-9
lines changed
  • src/java.base/share/classes/java/net

1 file changed

+29
-9
lines changed

src/java.base/share/classes/java/net/URI.java

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3276,6 +3276,7 @@ private int parseAuthority(int start, int n)
32763276

32773277
boolean serverChars;
32783278
boolean regChars;
3279+
boolean skipParseException;
32793280

32803281
if (scan(p, n, "]") > p) {
32813282
// contains a literal IPv6 address, therefore % is allowed
@@ -3291,15 +3292,28 @@ private int parseAuthority(int start, int n)
32913292
return n;
32923293
}
32933294

3295+
// When parsing a URI, skip creating exception objects if the server-based
3296+
// authority is not required and the registry parse is successful.
3297+
//
3298+
skipParseException = (!requireServerAuthority && regChars);
32943299
if (serverChars) {
32953300
// Might be (probably is) a server-based authority, so attempt
32963301
// to parse it as such. If the attempt fails, try to treat it
32973302
// as a registry-based authority.
32983303
try {
3299-
q = parseServer(p, n);
3300-
if (q < n)
3301-
failExpecting("end of authority", q);
3302-
authority = input.substring(p, n);
3304+
q = parseServer(p, n, skipParseException);
3305+
if (q < n) {
3306+
if (skipParseException) {
3307+
userInfo = null;
3308+
host = null;
3309+
port = -1;
3310+
q = p;
3311+
} else {
3312+
failExpecting("end of authority", q);
3313+
}
3314+
} else {
3315+
authority = input.substring(p, n);
3316+
}
33033317
} catch (URISyntaxException x) {
33043318
// Undo results of failed parse
33053319
userInfo = null;
@@ -3337,7 +3351,7 @@ private int parseAuthority(int start, int n)
33373351

33383352
// [<userinfo>@]<host>[:<port>]
33393353
//
3340-
private int parseServer(int start, int n)
3354+
private int parseServer(int start, int n, boolean skipParseException)
33413355
throws URISyntaxException
33423356
{
33433357
int p = start;
@@ -3377,7 +3391,7 @@ private int parseServer(int start, int n)
33773391
} else {
33783392
q = parseIPv4Address(p, n);
33793393
if (q <= p)
3380-
q = parseHostname(p, n);
3394+
q = parseHostname(p, n, skipParseException);
33813395
p = q;
33823396
}
33833397

@@ -3394,7 +3408,10 @@ private int parseServer(int start, int n)
33943408
}
33953409
p = q;
33963410
}
3411+
} else if (p < n && skipParseException) {
3412+
return p;
33973413
}
3414+
33983415
if (p < n)
33993416
failExpecting("port number", p);
34003417

@@ -3497,7 +3514,7 @@ private int parseIPv4Address(int start, int n) {
34973514
// domainlabel = alphanum | alphanum *( alphanum | "-" ) alphanum
34983515
// toplabel = alpha | alpha *( alphanum | "-" ) alphanum
34993516
//
3500-
private int parseHostname(int start, int n)
3517+
private int parseHostname(int start, int n, boolean skipParseException)
35013518
throws URISyntaxException
35023519
{
35033520
int p = start;
@@ -3523,9 +3540,12 @@ private int parseHostname(int start, int n)
35233540
p = q;
35243541
} while (p < n);
35253542

3526-
if ((p < n) && !at(p, n, ':'))
3543+
if ((p < n) && !at(p, n, ':')) {
3544+
if (skipParseException) {
3545+
return p;
3546+
}
35273547
fail("Illegal character in hostname", p);
3528-
3548+
}
35293549
if (l < 0)
35303550
failExpecting("hostname", start);
35313551

0 commit comments

Comments
 (0)