Skip to content

Commit

Permalink
fix #948 Fix IllegalArgumentException while resolving the path
Browse files Browse the repository at this point in the history
  • Loading branch information
violetagg committed Feb 17, 2020
1 parent 3e7d8a5 commit 31273cf
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
19 changes: 18 additions & 1 deletion src/main/java/reactor/netty/http/HttpOperations.java
Expand Up @@ -296,7 +296,24 @@ protected final boolean markSentHeaderAndBody() {
* @return a normalized uri without the leading and trailing '/' if present
*/
public static String resolvePath(String uri) {
String path = URI.create(uri).getPath();
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();
}
if (!path.isEmpty()) {
if(path.charAt(0) == '/'){
path = path.substring(1);
Expand Down
30 changes: 29 additions & 1 deletion src/test/java/reactor/netty/http/HttpOperationsTest.java
Expand Up @@ -33,7 +33,11 @@
import org.junit.Test;
import reactor.netty.Connection;

import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

/**
Expand Down Expand Up @@ -90,4 +94,28 @@ public void httpAndJsonDecoders() {
assertThat(t, nullValue());
}

@Test
public void testIssue948() {
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("", 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"));
}
}

0 comments on commit 31273cf

Please sign in to comment.