From 327e9b3b9b0ed07ff38e29207a0ab66c658636fd Mon Sep 17 00:00:00 2001 From: Violeta Georgieva Date: Tue, 14 Jan 2020 15:29:13 +0200 Subject: [PATCH] fix #948 Fix IllegalArgumentException while resolving the path Additional fixes --- .../java/reactor/netty/http/HttpInfos.java | 4 ++-- .../reactor/netty/http/HttpOperations.java | 23 +++++-------------- .../netty/http/HttpOperationsTest.java | 8 +++++-- 3 files changed, 14 insertions(+), 21 deletions(-) diff --git a/src/main/java/reactor/netty/http/HttpInfos.java b/src/main/java/reactor/netty/http/HttpInfos.java index cb33497605..b38951ee97 100644 --- a/src/main/java/reactor/netty/http/HttpInfos.java +++ b/src/main/java/reactor/netty/http/HttpInfos.java @@ -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(); diff --git a/src/main/java/reactor/netty/http/HttpOperations.java b/src/main/java/reactor/netty/http/HttpOperations.java index d7f2a96825..bb554f9e9c 100644 --- a/src/main/java/reactor/netty/http/HttpOperations.java +++ b/src/main/java/reactor/netty/http/HttpOperations.java @@ -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; } } diff --git a/src/test/java/reactor/netty/http/HttpOperationsTest.java b/src/test/java/reactor/netty/http/HttpOperationsTest.java index dc2ee671ca..b97f25b46f 100644 --- a/src/test/java/reactor/netty/http/HttpOperationsTest.java +++ b/src/test/java/reactor/netty/http/HttpOperationsTest.java @@ -98,7 +98,8 @@ 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")); @@ -106,10 +107,12 @@ public void testIssue948() { 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")); @@ -117,5 +120,6 @@ public void testIssue948() { assertEquals("", HttpOperations.resolvePath("/#b")); assertEquals("a", HttpOperations.resolvePath("/a#b")); assertEquals("a", HttpOperations.resolvePath("/a?b#c")); + assertEquals("a b", HttpOperations.resolvePath("/a%20b")); } }