-
Notifications
You must be signed in to change notification settings - Fork 453
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
feat: decode uri path components correctly #912
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ | |
|
||
import java.io.UnsupportedEncodingException; | ||
import java.net.URLDecoder; | ||
import java.nio.ByteBuffer; | ||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* Utility functions for dealing with {@code CharEscaper}s, and some commonly used {@code | ||
|
@@ -90,6 +92,59 @@ public static String decodeUri(String uri) { | |
} | ||
} | ||
|
||
/** | ||
* Decodes the path component of a URI. This must be done via a method that does not try to | ||
* convert + into spaces(the behavior of {@link java.net.URLDecoder#decode(String, String)}). This | ||
* method will transform URI encoded value into their decoded symbols. | ||
* | ||
* <p>i.e: {@code decodePath("%3Co%3E")} would return {@code "<o>"} | ||
* | ||
* @param path the value to be decoded | ||
* @return decoded version of {@code path} | ||
*/ | ||
public static String decodeUriPath(String path) { | ||
if (path == null) { | ||
return null; | ||
} | ||
ByteBuffer buf = null; | ||
int length = path.length(); | ||
StringBuilder sb = new StringBuilder(length); | ||
|
||
char c; | ||
for (int i = 0; i < length; i++) { | ||
c = path.charAt(i); | ||
if (c == '%') { | ||
if (i + 2 < length) { | ||
if (buf == null) { | ||
buf = ByteBuffer.allocate(Integer.SIZE / 8); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you could initialize this in line 109 and avoid the if-else here |
||
} else { | ||
buf.clear(); | ||
} | ||
try { | ||
int v = Integer.parseInt(path.substring(i + 1, i + 3), 16); | ||
if (v < 0) { | ||
throw new IllegalArgumentException( | ||
"Illegal parsed value from escaped sequence, most be positive"); | ||
} | ||
buf.put((byte) v); | ||
i += 2; | ||
sb.append(new String(buf.array(), 0, buf.position(), StandardCharsets.UTF_8)); | ||
|
||
} catch (NumberFormatException e) { | ||
throw new IllegalArgumentException( | ||
"Illegal length following escape sequence, must be in the form %xy"); | ||
} | ||
|
||
} else { | ||
throw new IllegalArgumentException("Illegal remaining length following escape sequence"); | ||
} | ||
} else { | ||
sb.append(c); | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* 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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
google-http-client/src/test/java/com/google/api/client/util/escape/CharEscapersTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* Copyright 2019 Google LLC | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under the License | ||
* is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express | ||
* or implied. See the License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
|
||
package com.google.api.client.util.escape; | ||
|
||
import junit.framework.TestCase; | ||
|
||
public class CharEscapersTest extends TestCase { | ||
|
||
public void testDecodeUriPath() { | ||
subtestDecodeUriPath(null, null); | ||
subtestDecodeUriPath("", ""); | ||
subtestDecodeUriPath("abc", "abc"); | ||
subtestDecodeUriPath("a+b%2Bc", "a+b+c"); | ||
subtestDecodeUriPath("Go%3D%23%2F%25%26%20?%3Co%3Egle", "Go=#/%& ?<o>gle"); | ||
} | ||
|
||
private void subtestDecodeUriPath(String input, String expected) { | ||
String actual = CharEscapers.decodeUriPath(input); | ||
assertEquals(expected, actual); | ||
} | ||
|
||
public void testDecodeUri_IllegalArgumentException() { | ||
subtestDecodeUri_IllegalArgumentException("abc%-1abc"); | ||
subtestDecodeUri_IllegalArgumentException("%JJ"); | ||
subtestDecodeUri_IllegalArgumentException("abc%0"); | ||
} | ||
|
||
private void subtestDecodeUri_IllegalArgumentException(String input) { | ||
boolean thrown = false; | ||
try { | ||
CharEscapers.decodeUriPath(input); | ||
} catch (IllegalArgumentException e) { | ||
thrown = true; | ||
} | ||
assertTrue(thrown); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this should be inside the for loop