From 7426c0c2c15b024947ed05cc5cc57afcae6cc307 Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Tue, 14 Jan 2020 10:43:03 +0200 Subject: [PATCH] fix #948 Fix IllegalArgumentException while resolving the path --- .../reactor/netty/http/HttpOperations.java | 19 +++++++++++- .../netty/http/HttpOperationsTest.java | 30 ++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/main/java/reactor/netty/http/HttpOperations.java b/src/main/java/reactor/netty/http/HttpOperations.java index 26f9c15b79..dfd4d0f968 100644 --- a/src/main/java/reactor/netty/http/HttpOperations.java +++ b/src/main/java/reactor/netty/http/HttpOperations.java @@ -297,7 +297,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); diff --git a/src/test/java/reactor/netty/http/HttpOperationsTest.java b/src/test/java/reactor/netty/http/HttpOperationsTest.java index 66679e3cfe..dc2ee671ca 100644 --- a/src/test/java/reactor/netty/http/HttpOperationsTest.java +++ b/src/test/java/reactor/netty/http/HttpOperationsTest.java @@ -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; /** @@ -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")); + } }