From fa52b524d398e06294145099e37132e9bc9608d5 Mon Sep 17 00:00:00 2001 From: James Dempsey Date: Fri, 19 Jul 2019 11:15:38 +1000 Subject: [PATCH] #116 - Allow multiple trailing slash on base url --- src/uws/service/UWSUrl.java | 5 +++++ src/uws/service/error/DefaultUWSErrorWriter.java | 1 - test/uws/service/TestUWSUrl.java | 16 ++++++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/uws/service/UWSUrl.java b/src/uws/service/UWSUrl.java index 5b0cea96..78215b9c 100644 --- a/src/uws/service/UWSUrl.java +++ b/src/uws/service/UWSUrl.java @@ -148,6 +148,11 @@ protected static final String normalizeURI(String uri){ uri = uri.trim(); if (uri.length() > 0 && uri.charAt(0) != '/') uri = "/" + uri; + + // Trim out duplicate leading slashes + while(uri.length() >= 2 && uri.charAt(1) == '/') + uri = uri.substring(1); + while(uri.length() >= 1 && uri.charAt(uri.length() - 1) == '/') uri = uri.substring(0, uri.length() - 1).trim(); return uri.trim(); diff --git a/src/uws/service/error/DefaultUWSErrorWriter.java b/src/uws/service/error/DefaultUWSErrorWriter.java index 8d7e7820..a58f700e 100644 --- a/src/uws/service/error/DefaultUWSErrorWriter.java +++ b/src/uws/service/error/DefaultUWSErrorWriter.java @@ -33,7 +33,6 @@ import org.json.JSONException; import org.json.JSONWriter; -import tap.TAPException; import uws.AcceptHeader; import uws.UWSException; import uws.UWSToolBox; diff --git a/test/uws/service/TestUWSUrl.java b/test/uws/service/TestUWSUrl.java index a83b883f..fabff1cb 100644 --- a/test/uws/service/TestUWSUrl.java +++ b/test/uws/service/TestUWSUrl.java @@ -557,4 +557,20 @@ public void testLoadHttpServletRequest(){ } } + @Test + public void testNormalizeURI() { + + assertEquals("Leading slash", "/async", UWSUrl.normalizeURI("/async")); + assertEquals("No leading slash", "/async1", UWSUrl.normalizeURI("async1")); + assertEquals("Double leading slash", "/async", UWSUrl.normalizeURI("//async ")); + assertEquals("Quad leading slash", "/async1", UWSUrl.normalizeURI("////async1")); + assertEquals("Leading and trailing space", "/async2", UWSUrl.normalizeURI(" /async2 ")); + + assertEquals("Leading and trailing slash", "/async", UWSUrl.normalizeURI("/async/")); + assertEquals("No leading slash, with trailing slash", "/async1", UWSUrl.normalizeURI("async1/")); + assertEquals("Double leading and trailing slash", "/async", UWSUrl.normalizeURI("//async//")); + assertEquals("Quad leading and trailing slash", "/async1", UWSUrl.normalizeURI("////async1////")); + assertEquals("Leading and trailing space with trailing slash", "/async2", UWSUrl.normalizeURI(" /async2/ ")); + } + }