From 12f29fd8a9e50203acc12ccf794cf73c0abf5703 Mon Sep 17 00:00:00 2001 From: Diwaker Gupta Date: Thu, 1 Aug 2013 09:44:59 -0700 Subject: [PATCH] JCLOUDS-217: Only decode strings that are actually encoded. * Update HttpRequestTests to account for change in urlDecode. * Related to JCLOUDS-200 --- .../main/java/org/jclouds/util/Strings2.java | 7 ++++- .../org/jclouds/http/HttpRequestTest.java | 27 +++---------------- .../java/org/jclouds/util/Strings2Test.java | 7 ++++- 3 files changed, 15 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/org/jclouds/util/Strings2.java b/core/src/main/java/org/jclouds/util/Strings2.java index c4561acb2d..d29adf98df 100644 --- a/core/src/main/java/org/jclouds/util/Strings2.java +++ b/core/src/main/java/org/jclouds/util/Strings2.java @@ -115,8 +115,13 @@ public class Strings2 { public static String urlDecode(@Nullable String in) { if (in == null) return null; + String input = in.toString(); + // Don't double decode + if (!isUrlEncoded(input)) { + return input; + } try { - return URLDecoder.decode(in, "UTF-8"); + return URLDecoder.decode(input, "UTF-8"); } catch (UnsupportedEncodingException e) { throw new IllegalStateException("Bad encoding on input: " + in, e); } diff --git a/core/src/test/java/org/jclouds/http/HttpRequestTest.java b/core/src/test/java/org/jclouds/http/HttpRequestTest.java index a586547989..4004120323 100644 --- a/core/src/test/java/org/jclouds/http/HttpRequestTest.java +++ b/core/src/test/java/org/jclouds/http/HttpRequestTest.java @@ -67,45 +67,24 @@ public class HttpRequestTest { .builder().method("GET").endpoint("http://foo").payload(payload).build()); } - // the following caused issues for the fgcp provider - // (see RequestAuthenticator#addQueryParamsToRequest) - // base64 symbols should be url encoded in query param - // note that + ends up encoded as %20 (space), not %2B (plus) - public void testAddingBase64EncodedQueryParamCausingPlusToUrlEncodedSpaceConversion() { - String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; - URI uri = URI - .create("http://goo.com:443?header1=valueWithUrlEncoded%2BPlus"); - HttpRequest request = HttpRequest.builder().method("GET").endpoint(uri) - // addQueryParam invocation causes %2B's in prev. params to - // convert to %20. - .addQueryParam("header2", base64Chars).build(); - - assertEquals( - request.getRequestLine(), - "GET http://goo.com:443?header1=valueWithUrlEncoded%20Plus&header2=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%20/%3D HTTP/1.1"); - } - - // note that + ends up encoded as %20 (space) in the first param, %2B (plus) - // in the last param and %2F converts back into slash + // note that + ends up encoded as %2B (plus) and %2F converts back into slash public void testAddBase64AndUrlEncodedQueryParams() { String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B%2F%3D"; URI uri = URI.create("http://goo.com:443?header1=" + base64Chars); HttpRequest request = HttpRequest.builder() .method("GET") .endpoint(uri) - // the addition of another param causes %2B's in prev. params to - // convert to %20. .addQueryParam("header2", base64Chars) .build(); assertEquals( request.getRequestLine(), - "GET http://goo.com:443?header1=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%20/%3D&header2=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B/%3D HTTP/1.1"); + "GET http://goo.com:443?header1=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B/%3D&header2=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789%2B/%3D HTTP/1.1"); } // base64 symbols with newline separator should be url encoded in query param public void testAddBase64EncodedQueryParamWithNewlines() { - String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789%2B/="; + String base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n0123456789+/="; URI uri = URI.create("http://goo.com:443?header1=value1"); HttpRequest request = HttpRequest.builder().method("GET").endpoint(uri) .addQueryParam("header2", base64Chars).build(); diff --git a/core/src/test/java/org/jclouds/util/Strings2Test.java b/core/src/test/java/org/jclouds/util/Strings2Test.java index 5b1c9a0289..5440699673 100644 --- a/core/src/test/java/org/jclouds/util/Strings2Test.java +++ b/core/src/test/java/org/jclouds/util/Strings2Test.java @@ -40,7 +40,12 @@ public class Strings2Test { "/read-tests/%73%6f%6d%65%20%66%69%6c%65"); assertEquals(urlEncode("/read-tests/ tep", '/'), "/read-tests/%20tep"); } - + + public void testNoDoubleDecode() { + assertEquals(urlDecode("foo%20bar%2Bbaz"), "foo bar+baz"); + assertEquals(urlDecode("foo bar+baz"), "foo bar+baz"); + } + public void testReplaceTokens() { assertEquals(Strings2.replaceTokens("hello {where}", ImmutableMap.of("where", "world")), "hello world"); }