Skip to content

Commit

Permalink
Double-slash URI issue #7474
Browse files Browse the repository at this point in the history
  • Loading branch information
danielkec committed Sep 24, 2023
1 parent 85f5202 commit 8b9e092
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public String rawPathNoParams() {
@Override
public String path() {
if (decodedPath == null) {
decodedPath = decode(rawPath);
decodedPath = decode(rawPath, false);
}
return decodedPath;
}
Expand Down Expand Up @@ -104,11 +104,11 @@ public String toString() {
@Override
public void validate() {
if (decodedPath == null) {
this.decodedPath = URI.create(rawPath).normalize().getPath();
this.decodedPath = decode(rawPath, true);
}
}

private static String decode(String rawPath) {
private static String decode(String rawPath, boolean validate) {
/*
Raw path may:
- be encoded (%20)
Expand All @@ -121,10 +121,15 @@ private static String decode(String rawPath) {
int dot = rawPath.indexOf(".");
int doubleSlash = rawPath.indexOf("//");

if (percent == -1 && doubleSlash == -1 && dot == -1) {
if (!validate && percent == -1 && doubleSlash == -1 && dot == -1) {
return rawPath;
}

if (doubleSlash == 0) {
// JDK-8316799 URI.create("//foo/bar").normalize().getPath() --> "/bar"
rawPath = rawPath.substring(1);
}

return URI.create(rawPath).normalize().getPath();
}
}
14 changes: 14 additions & 0 deletions common/uri/src/test/java/io/helidon/common/uri/UriPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package io.helidon.common.uri;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand All @@ -37,4 +39,16 @@ void testEncodeSpace() {
assertThat(path.path(), is("/my path"));
assertThat(path.rawPath(), is("/my%20path"));
}

@ParameterizedTest
@ValueSource(strings = {
"//foo/bar",
"//foo//bar",
"/foo//bar",
"/foo/bar",
})
void testDoubleSlash(String rawPath) {
UriPath path = UriPath.create(rawPath);
assertThat(path.path(), is("/foo/bar"));
}
}

0 comments on commit 8b9e092

Please sign in to comment.