Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix GenericUrl to decode path parts correctly #533

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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