Skip to content

Commit

Permalink
fix #948 Fix IllegalArgumentException while resolving the path
Browse files Browse the repository at this point in the history
Additional fixes
  • Loading branch information
violetagg committed Feb 17, 2020
1 parent 31273cf commit 327e9b3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 21 deletions.
4 changes: 2 additions & 2 deletions src/main/java/reactor/netty/http/HttpInfos.java
Expand Up @@ -61,9 +61,9 @@ public interface HttpInfos {
HttpMethod method();

/**
* Returns a normalized {@link #uri()} without the leading and trailing '/' if present
* Returns the decoded path portion from the {@link #uri()} without the leading and trailing '/' if present
*
* @return a normalized {@link #uri()} without the leading and trailing '/' if present
* @return the decoded path portion from the {@link #uri()} without the leading and trailing '/' if present
*/
String path();

Expand Down
23 changes: 6 additions & 17 deletions src/main/java/reactor/netty/http/HttpOperations.java
Expand Up @@ -291,33 +291,22 @@ protected final boolean markSentHeaderAndBody() {
}

/**
* Returns a normalized uri without the leading and trailing '/' if present
* Returns the decoded path portion from the provided {@code uri} without the leading and trailing '/' if present
*
* @return a normalized uri without the leading and trailing '/' if present
* @param uri an HTTP URL that may contain a path with query/fragment
* @return the decoded path portion from the provided {@code uri} without the leading and trailing '/' if present
*/
public static String resolvePath(String uri) {
if (uri.isEmpty()) {
return uri;
}

String path;
if (uri.charAt(0) == '/') {
path = uri;
for (int i = 0; i < path.length(); i++) {
char c = path.charAt(i);
if (c == '?' || c == '#') {
path = path.substring(0, i);
break;
}
}
}
else {
path = URI.create(uri).getPath();
}
String path = URI.create(uri.charAt(0) == '/' ? "http://localhost:8080" + uri : uri)
.getPath();
if (!path.isEmpty()) {
if(path.charAt(0) == '/'){
path = path.substring(1);
if(path.length() <= 1){
if (path.isEmpty()) {
return path;
}
}
Expand Down
8 changes: 6 additions & 2 deletions src/test/java/reactor/netty/http/HttpOperationsTest.java
Expand Up @@ -98,24 +98,28 @@ public void httpAndJsonDecoders() {
public void testIssue948() {
assertEquals("", HttpOperations.resolvePath("http://localhost:8080"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080/"));
assertEquals("/", HttpOperations.resolvePath("http://localhost:8080//"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080//"));
assertEquals("/", HttpOperations.resolvePath("http://localhost:8080///"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a/"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080/?b"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a?b"));
assertEquals("", HttpOperations.resolvePath("http://localhost:8080/#b"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a#b"));
assertEquals("a", HttpOperations.resolvePath("http://localhost:8080/a?b#c"));
assertEquals("a b", HttpOperations.resolvePath("http://localhost:8080/a%20b"));

assertEquals("", HttpOperations.resolvePath(""));
assertEquals("", HttpOperations.resolvePath("/"));
assertEquals("/", HttpOperations.resolvePath("//"));
assertEquals("", HttpOperations.resolvePath("//"));
assertEquals("/", HttpOperations.resolvePath("///"));
assertEquals("a", HttpOperations.resolvePath("/a"));
assertEquals("a", HttpOperations.resolvePath("/a/"));
assertEquals("", HttpOperations.resolvePath("/?b"));
assertEquals("a", HttpOperations.resolvePath("/a?b"));
assertEquals("", HttpOperations.resolvePath("/#b"));
assertEquals("a", HttpOperations.resolvePath("/a#b"));
assertEquals("a", HttpOperations.resolvePath("/a?b#c"));
assertEquals("a b", HttpOperations.resolvePath("/a%20b"));
}
}

0 comments on commit 327e9b3

Please sign in to comment.