Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/changes/changes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The <action> type attribute can be add,update,fix,remove.
<body>
<release version="1.22.1" date="YYYY-MM-DD" description="This is a feature and maintenance release. Java 8 or later is required.">
<!-- FIX -->
<action type="fix" issue="CODEC-338" dev="ggregory" due-to="Ruiqi Dong, Gary Gregory">PercentCodec loses literal '+' when plusForSpace is enabled.</action>
<action type="add" issue="CODEC-337" dev="pkarwasz" due-to="Ruiqi Dong, Gary Gregory">Digest ALL reuses System.in, so only the first algorithm sees the real input (#431).</action>
<!-- ADD -->
<!-- UPDATE -->
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/org/apache/commons/codec/net/PercentCodec.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ public class PercentCodec implements BinaryEncoder, BinaryDecoder {
*/
private static final byte ESCAPE_CHAR = '%';

/**
* The plus character used to encode spaces when plusForSpace is true.
*/
private static final byte PLUS_CHAR = '+';

/**
* The bit set used to store the character that should be always encoded.
*/
Expand Down Expand Up @@ -80,6 +85,9 @@ public PercentCodec() {
public PercentCodec(final byte[] alwaysEncodeChars, final boolean plusForSpace) {
this.plusForSpace = plusForSpace;
insertAlwaysEncodeChars(alwaysEncodeChars);
if (plusForSpace) {
insertAlwaysEncodeChar(PLUS_CHAR);
}
}

private boolean canEncode(final byte c) {
Expand Down
22 changes: 22 additions & 0 deletions src/test/java/org/apache/commons/codec/net/PercentCodecTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,28 @@ void testPercentEncoderDecoderWithPlusForSpace() throws Exception {
assertEquals(new String(decode, StandardCharsets.UTF_8), input, "PercentCodec plus for space decoding test");
}

@Test
void testPercentEncoderDecoderWithPlusForSpaceEscapesLiteralPlus() throws Exception {
final String input = "a+b c";
final PercentCodec percentCodec = new PercentCodec(null, true);
final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
final String encodedS = new String(encoded, StandardCharsets.UTF_8);
assertEquals("a%2Bb+c", encodedS, "PercentCodec plus for space should escape literal plus");
final byte[] decode = percentCodec.decode(encoded);
assertEquals(new String(decode, StandardCharsets.UTF_8), input, "PercentCodec literal plus decoding test");
}

@Test
void testPercentEncoderDecoderWithPlusForSpaceEscapesLiteralPlusWithoutSpaces() throws Exception {
final String input = "a+b";
final PercentCodec percentCodec = new PercentCodec(null, true);
final byte[] encoded = percentCodec.encode(input.getBytes(StandardCharsets.UTF_8));
final String encodedS = new String(encoded, StandardCharsets.UTF_8);
assertEquals("a%2Bb", encodedS, "PercentCodec plus for space should escape literal plus without spaces");
final byte[] decode = percentCodec.decode(encoded);
assertEquals(new String(decode, StandardCharsets.UTF_8), input, "PercentCodec literal plus decoding test");
}

@Test
void testSafeCharEncodeDecodeObject() throws Exception {
final PercentCodec percentCodec = new PercentCodec(null, true);
Expand Down
Loading