Skip to content

Commit

Permalink
fix googleapis#398 GenericUrl converts plus sign into space within UR…
Browse files Browse the repository at this point in the history
…I path component
  • Loading branch information
ajaaym committed Dec 4, 2018
1 parent ab627ed commit 3d8032f
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
Expand Up @@ -548,7 +548,7 @@ public static List<String> toPathParts(String encodedPath) {
} else {
sub = encodedPath.substring(cur);
}
result.add(CharEscapers.decodeUri(sub));
result.add(CharEscapers.decode(sub));
cur = slash + 1;
}
return result;
Expand Down
Expand Up @@ -15,6 +15,8 @@
package com.google.api.client.util.escape;

import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLDecoder;

/**
Expand Down Expand Up @@ -96,6 +98,32 @@ public static String decodeUri(String uri) {
}
}

/**
* Percent-decodes a US-ASCII string into a Unicode string. UTF-8 encoding is used to determine
* what characters are represented by any consecutive sequences of the form "%<i>XX</i>".
*
* <p>
* This doesnt replaces each occurrence of '+' with a space, ' '. So this method should not be used for
* application/x-www-form-urlencoded strings such as query.
* </p>
*
* @param uri a percent-encoded US-ASCII string
* @return a Unicode string
*/
public static String decode(String uri) {
if (uri.indexOf('%') > -1) {
if (uri.indexOf('+') > -1) {
try {
return new URI(uri).getPath();
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
return decodeUri(uri);
}
return uri;
}

/**
* Escapes the string value so it can be safely included in URI path segments. For details on
* escaping URIs, see <a href="http://tools.ietf.org/html/rfc3986#section-2.4">RFC 3986 - section
Expand Down
Expand Up @@ -475,6 +475,8 @@ public void testToPathParts() {
subtestToPathParts("/path/to/resource", "", "path", "to", "resource");
subtestToPathParts("/path/to/resource/", "", "path", "to", "resource", "");
subtestToPathParts("/Go%3D%23%2F%25%26%20?%3Co%3Egle/2nd", "", "Go=#/%& ?<o>gle", "2nd");
subtestToPathParts("/path+with+plus/2nd", "", "path+with+plus", "2nd");
subtestToPathParts("/path%2Bwith+plus/2nd", "", "path+with+plus", "2nd");
}

private void subtestToPathParts(String encodedPath, String... expectedDecodedParts) {
Expand Down

0 comments on commit 3d8032f

Please sign in to comment.