Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

8272702: Resolving URI relative path with no / may lead to incorrect toString #8899

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions src/java.base/share/classes/java/net/URI.java
Original file line number Diff line number Diff line change
@@ -2106,7 +2106,7 @@ private String toString(String scheme,
// -- Normalization, resolution, and relativization --

// RFC2396 5.2 (6)
private static String resolvePath(String base, String child)
private static String resolvePath(String base, String child, boolean absolute)
{
int i = base.lastIndexOf('/');
int cn = child.length();
@@ -2117,12 +2117,13 @@ private static String resolvePath(String base, String child)
if (i >= 0)
path = base.substring(0, i + 1);
} else {
// 5.2 (6a)
if (i >= 0)
// 5.2 (6a-b)
if (i >= 0 || !absolute) {
path = base.substring(0, i + 1).concat(child);
// 5.2 (6b)
else
path = child;
} else {
path = "/".concat(child);
}

}

// 5.2 (6c-f)
@@ -2183,7 +2184,7 @@ private static URI resolve(URI base, URI child) {
ru.path = child.path;
} else {
// 5.2 (6): Resolve relative path
ru.path = resolvePath(base.path, cp);
ru.path = resolvePath(base.path, cp, base.isAbsolute());
}
} else {
ru.authority = child.authority;
26 changes: 24 additions & 2 deletions test/jdk/java/net/URI/Test.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2019, 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
@@ -24,7 +24,7 @@
/* @test
* @summary Unit test for java.net.URI
* @bug 4464135 4505046 4503239 4438319 4991359 4866303 7023363 7041800
* 7171415 6933879
* 7171415 6339649 6933879 8037396 8272072
* @author Mark Reinhold
*/

@@ -1364,6 +1364,7 @@ static void eq(URI u, URI v) throws URISyntaxException {
}

static void eq(String expected, String actual) {
testCount++;
if (expected == null && actual == null) {
return;
}
@@ -1612,9 +1613,11 @@ static void clargs(String base, String uri) {
// miscellaneous bugs/rfes that don't fit in with the test framework

static void bugs() {
header("Bugs");
b6339649();
b6933879();
b8037396();
b8272072();
}

// 6339649 - include detail message from nested exception
@@ -1626,6 +1629,7 @@ private static void b6339649() {
throw new RuntimeException ("No detail message");
}
}
testCount++;
}

// 6933879 - check that "." and "_" characters are allowed in IPv6 scope_id.
@@ -1673,6 +1677,24 @@ private static void b8037396() {
eq("a%20b[c%20d]", u.getRawFragment());
}

// 8272072 - Resolving URI relative path with no "/" may lead to incorrect toString
private static void b8272072() {
try {
URI baseURI = new URI("http://example.com");
URI relativeURI = new URI("test");
URI resolvedURI = baseURI.resolve(relativeURI);

eq(new URI("http://example.com/test"), resolvedURI);

baseURI = new URI("relativeBase");
resolvedURI = baseURI.resolve(relativeURI);

eq(new URI("test"), resolvedURI);
} catch (URISyntaxException e) {
throw new AssertionError("shouldn't ever happen", e);
}
}

public static void main(String[] args) throws Exception {
switch (args.length) {