Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import java.nio.charset.StandardCharsets;
import java.util.BitSet;

import org.apache.hc.core5.annotation.Internal;

/**
* Percent-encoding.
*
Expand Down Expand Up @@ -113,6 +115,36 @@ public class PercentCodec {
RFC5987_UNRESERVED.set('~');
}

static final BitSet RFC7639_UNRESERVED = new BitSet(256);

static {
// RFC 7230 tchar minus '%'
for (int i = 'a'; i <= 'z'; i++) {
RFC7639_UNRESERVED.set(i);
}
for (int i = 'A'; i <= 'Z'; i++) {
RFC7639_UNRESERVED.set(i);
}
for (int i = '0'; i <= '9'; i++) {
RFC7639_UNRESERVED.set(i);
}

RFC7639_UNRESERVED.set('!');
RFC7639_UNRESERVED.set('#');
RFC7639_UNRESERVED.set('$');
RFC7639_UNRESERVED.set('&');
RFC7639_UNRESERVED.set('\'');
RFC7639_UNRESERVED.set('*');
RFC7639_UNRESERVED.set('+');
RFC7639_UNRESERVED.set('-');
RFC7639_UNRESERVED.set('.');
RFC7639_UNRESERVED.set('^');
RFC7639_UNRESERVED.set('_');
RFC7639_UNRESERVED.set('`');
RFC7639_UNRESERVED.set('|');
RFC7639_UNRESERVED.set('~');
}

static final BitSet PCHAR = new BitSet(256);
static final BitSet USERINFO = new BitSet(256);
static final BitSet REG_NAME = new BitSet(256);
Expand Down Expand Up @@ -217,10 +249,12 @@ public static String decode(final CharSequence content, final Charset charset) {

public static final PercentCodec RFC3986 = new PercentCodec(UNRESERVED);
public static final PercentCodec RFC5987 = new PercentCodec(RFC5987_UNRESERVED);
public static final PercentCodec RFC7230_TOKEN = new PercentCodec(RFC7639_UNRESERVED);

private final BitSet unreserved;

private PercentCodec(final BitSet unreserved) {
@Internal
public PercentCodec(final BitSet unreserved) {
this.unreserved = unreserved;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,4 +98,30 @@ void verifyRfc5987EncodingandDecoding() {
assertThat(PercentCodec.RFC5987.decode(PercentCodec.RFC5987.encode(s)), CoreMatchers.equalTo(s));
}

@Test
void testRfc7639CanonicalAlpnTokenEncoding() {
// RFC 7639 requires protocol-id to be a token and applies additional canonical constraints:
// - Octets not allowed in tokens MUST be percent-encoded (RFC 3986).
// - '%' MUST be percent-encoded.
// - Octets that are valid token characters MUST NOT be percent-encoded (except '%').
// - Uppercase hex digits MUST be used.
assertEquals("h2", PercentCodec.RFC7230_TOKEN.encode("h2"));
assertEquals("http%2F1.1", PercentCodec.RFC7230_TOKEN.encode("http/1.1"));
assertEquals("%25", PercentCodec.RFC7230_TOKEN.encode("%"));
assertEquals("foo+bar", PercentCodec.RFC7230_TOKEN.encode("foo+bar"));
assertEquals("!#$&'*+-.^_`|~", PercentCodec.RFC7230_TOKEN.encode("!#$&'*+-.^_`|~"));
assertEquals("foo bar", PercentCodec.RFC7230_TOKEN.decode("foo%20bar"));
assertEquals("ws/é", PercentCodec.RFC7230_TOKEN.decode("ws%2F%C3%A9"));
}

@Test
void testPercentCodecEncodeIsNotRfc7639Canonical() {
// PercentCodec.encode(..) uses RFC 3986 UNRESERVED as the safe set.
// This percent-encodes valid RFC 7230 tchar like '+', '*', '!', '|', which RFC 7639 forbids.
assertEquals("foo%2Bbar", PercentCodec.encode("foo+bar", StandardCharsets.UTF_8));
assertEquals("%2A", PercentCodec.encode("*", StandardCharsets.UTF_8));
assertEquals("%21", PercentCodec.encode("!", StandardCharsets.UTF_8));
assertEquals("%7C", PercentCodec.encode("|", StandardCharsets.UTF_8));
}

}