-
Notifications
You must be signed in to change notification settings - Fork 464
fix: UriTemplate expansion reserved ("+") and fragment("#") should not encode already percent encoded parts #2108
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9cb79d1
Added new escaper for already percent encoded inputs
alexander-asenov 5096211
Added the new escaper to the CharEscapers utility class
alexander-asenov e1bbc29
Fixed the inconsistency with rfc6570#section-3.2.1
alexander-asenov 25ad44d
Fix linter error
alexander-asenov ef69ce2
Merge branch 'main' into main
diegomarquezp 8b82d17
Update documentation to clarify support for URI Template levels
alexander-asenov b2e87cf
Change PercentEncodedEscaper class to package-private
alexander-asenov 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 hidden or 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 hidden or 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
57 changes: 57 additions & 0 deletions
57
...le-http-client/src/main/java/com/google/api/client/util/escape/PercentEncodedEscaper.java
This file contains hidden or 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,57 @@ | ||
package com.google.api.client.util.escape; | ||
|
||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
/** | ||
* An {@link Escaper} implementation that preserves percent-encoded sequences in the input string. | ||
* | ||
* <p>This escaper applies the provided {@link Escaper} to all parts of the input string except for | ||
* valid percent-encoded sequences (e.g., <code>%20</code>), which are left unchanged. | ||
*/ | ||
final class PercentEncodedEscaper extends Escaper { | ||
|
||
/** Pattern to match valid percent-encoded sequences (e.g., %20). */ | ||
static final Pattern PCT_ENCODE_PATTERN = Pattern.compile("%[0-9A-Fa-f]{2}"); | ||
|
||
private final Escaper escaper; | ||
|
||
public PercentEncodedEscaper(Escaper escaper) { | ||
if (escaper == null) { | ||
throw new NullPointerException("Escaper cannot be null"); | ||
} | ||
this.escaper = escaper; | ||
} | ||
|
||
/** | ||
* Escapes the input string using the provided {@link Escaper}, preserving valid percent-encoded | ||
* sequences. | ||
* | ||
* @param string the input string to escape | ||
* @return the escaped string with percent-encoded sequences left unchanged | ||
*/ | ||
@Override | ||
public String escape(String string) { | ||
if (string == null || string.isEmpty()) { | ||
return string; | ||
} | ||
|
||
Matcher matcher = PCT_ENCODE_PATTERN.matcher(string); | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
int lastEnd = 0; | ||
while (matcher.find()) { | ||
sb.append(escaper.escape(string.substring(lastEnd, matcher.start()))); | ||
|
||
sb.append(string.substring(matcher.start(), matcher.end())); | ||
|
||
lastEnd = matcher.end(); | ||
} | ||
|
||
if (lastEnd < string.length()) { | ||
sb.append(escaper.escape(string.substring(lastEnd))); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
} |
This file contains hidden or 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
32 changes: 32 additions & 0 deletions
32
...ttp-client/src/test/java/com/google/api/client/util/escape/PercentEncodedEscaperTest.java
This file contains hidden or 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,32 @@ | ||
package com.google.api.client.util.escape; | ||
|
||
import junit.framework.TestCase; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public class PercentEncodedEscaperTest extends TestCase { | ||
@Test | ||
public void testEscape() { | ||
PercentEncodedEscaper escaper = | ||
new PercentEncodedEscaper( | ||
new PercentEscaper(PercentEscaper.SAFE_PLUS_RESERVED_CHARS_URLENCODER)); | ||
String input = "Hello%20World+/?#[]"; | ||
|
||
String actual = escaper.escape(input); | ||
assertEquals(input, actual); // No change expected since it's already percent-encoded | ||
} | ||
|
||
@Test | ||
public void testEscapeEncode() { | ||
PercentEncodedEscaper escaper = | ||
new PercentEncodedEscaper( | ||
new PercentEscaper(PercentEscaper.SAFE_PLUS_RESERVED_CHARS_URLENCODER)); | ||
String input = "Hello World%"; | ||
String expected = "Hello%20World%25"; | ||
|
||
String actual = escaper.escape(input); | ||
assertEquals(expected, actual); | ||
} | ||
} |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.