-
Notifications
You must be signed in to change notification settings - Fork 985
RFC 7639 ALPN #731
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
Open
arturobernalg
wants to merge
1
commit into
apache:master
Choose a base branch
from
arturobernalg:rfc7639
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+729
−37
Open
RFC 7639 ALPN #731
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
59 changes: 59 additions & 0 deletions
59
httpclient5/src/main/java/org/apache/hc/client5/http/ConnectAlpnProvider.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,59 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| * ==================================================================== | ||
| * | ||
| * This software consists of voluntary contributions made by many | ||
| * individuals on behalf of the Apache Software Foundation. For more | ||
| * information on the Apache Software Foundation, please see | ||
| * <http://www.apache.org/>. | ||
| * | ||
| */ | ||
| package org.apache.hc.client5.http; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| import org.apache.hc.core5.http.HttpHost; | ||
|
|
||
| /** | ||
| * Supplies the Application-Layer Protocol Negotiation (ALPN) protocol IDs | ||
| * to advertise in the HTTP {@code ALPN} header on a {@code CONNECT} request | ||
| * (RFC 7639). | ||
| * | ||
| * <p>If this method returns {@code null} or an empty list, the client will | ||
| * not add the {@code ALPN} header.</p> | ||
| * | ||
| * <p>Implementations should be fast and side-effect free; it may be invoked | ||
| * for each CONNECT attempt.</p> | ||
| * | ||
| * @since 5.6 | ||
| */ | ||
| @FunctionalInterface | ||
| public interface ConnectAlpnProvider { | ||
|
|
||
| /** | ||
| * Returns the ALPN protocol IDs to advertise for a tunnel to {@code target} | ||
| * over the given {@code route}. | ||
| * | ||
| * @param target the origin server the tunnel will connect to (non-null) | ||
| * @param route the planned connection route, including proxy info (non-null) | ||
| * @return list of protocol IDs (e.g., {@code "h2"}, {@code "http/1.1"}); | ||
| * {@code null} or empty to omit the header | ||
| */ | ||
| List<String> getAlpnForTunnel(HttpHost target, HttpRoute route); | ||
| } |
141 changes: 141 additions & 0 deletions
141
httpclient5/src/main/java/org/apache/hc/client5/http/impl/AlpnHeaderSupport.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,141 @@ | ||
| /* | ||
| * ==================================================================== | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you 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. | ||
| * ==================================================================== | ||
| * | ||
| * This software consists of voluntary contributions made by many | ||
| * individuals on behalf of the Apache Software Foundation. For more | ||
| * information on the Apache Software Foundation, please see | ||
| * <http://www.apache.org/>. | ||
| * | ||
| */ | ||
| package org.apache.hc.client5.http.impl; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| import org.apache.hc.core5.annotation.Contract; | ||
| import org.apache.hc.core5.annotation.Internal; | ||
| import org.apache.hc.core5.annotation.ThreadingBehavior; | ||
| import org.apache.hc.core5.http.Header; | ||
| import org.apache.hc.core5.http.HttpHeaders; | ||
| import org.apache.hc.core5.http.message.MessageSupport; | ||
| import org.apache.hc.core5.net.PercentCodec; | ||
| import org.apache.hc.core5.util.Args; | ||
|
|
||
| /** | ||
| * Codec for the HTTP {@code ALPN} header field (RFC 7639). | ||
| * | ||
| * @since 5.7 | ||
| */ | ||
| @Contract(threading = ThreadingBehavior.IMMUTABLE) | ||
| @Internal | ||
| public final class AlpnHeaderSupport { | ||
|
|
||
| private static final char[] HEXADECIMAL = "0123456789ABCDEF".toCharArray(); | ||
|
|
||
| private AlpnHeaderSupport() { | ||
| } | ||
|
|
||
| /** | ||
| * Formats a list of raw ALPN protocol IDs into a single {@code ALPN} header. | ||
| */ | ||
| public static Header formatValue(final List<String> protocolIds) { | ||
| Args.notEmpty(protocolIds, "protocolIds"); | ||
| return MessageSupport.headerOfTokens(HttpHeaders.ALPN, protocolIds, AlpnHeaderSupport::encodeId); | ||
| } | ||
|
|
||
| /** | ||
| * Parses an {@code ALPN} header into decoded protocol IDs. | ||
| */ | ||
| public static List<String> parseValue(final Header header) { | ||
| final List<String> out = new ArrayList<>(); | ||
| MessageSupport.parseTokens(header, token -> out.add(decodeId(token))); | ||
| return out; | ||
| } | ||
|
|
||
| /** | ||
| * Encodes a single raw protocol ID to canonical token form. | ||
| */ | ||
| public static String encodeId(final String id) { | ||
| Args.notBlank(id, "id"); | ||
|
|
||
| final byte[] bytes = id.getBytes(StandardCharsets.UTF_8); | ||
| final StringBuilder sb = new StringBuilder(bytes.length); | ||
| for (final byte aByte : bytes) { | ||
| final int b = aByte & 0xFF; | ||
| if (b == '%' || !isTchar(b)) { | ||
| sb.append('%'); | ||
| sb.append(Character.toUpperCase(Character.forDigit((b >>> 4) & 0x0F, 16))); | ||
| sb.append(Character.toUpperCase(Character.forDigit(b & 0x0F, 16))); | ||
| } else { | ||
| sb.append((char) b); | ||
| } | ||
| } | ||
| return sb.toString(); | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Decodes percent-encoded token to raw ID using UTF-8. | ||
| * Malformed / incomplete sequences are left literal. | ||
| */ | ||
| public static String decodeId(final String token) { | ||
ok2c marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| Args.notBlank(token, "token"); | ||
| return PercentCodec.decode(token, StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| // RFC7230 tchar minus '%' (RFC7639 requires '%' be percent-encoded) | ||
| private static boolean isTchar(final int c) { | ||
| if (c >= '0' && c <= '9') { | ||
| return true; | ||
| } | ||
| if (c >= 'A' && c <= 'Z') { | ||
| return true; | ||
| } | ||
| if (c >= 'a' && c <= 'z') { | ||
| return true; | ||
| } | ||
| switch (c) { | ||
| case '!': | ||
| case '#': | ||
| case '$': | ||
| case '&': | ||
| case '\'': | ||
| case '*': | ||
| case '+': | ||
| case '-': | ||
| case '.': | ||
| case '^': | ||
| case '_': | ||
| case '`': | ||
| case '|': | ||
| case '~': | ||
| return true; | ||
| default: | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| private static void appendPctEncoded(final int b, final StringBuilder sb) { | ||
| sb.append('%'); | ||
| sb.append(HEXADECIMAL[(b >>> 4) & 0x0F]); | ||
| sb.append(HEXADECIMAL[b & 0x0F]); | ||
| } | ||
| } | ||
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
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.
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.
@arturobernalg I asked this already, I have to ask it again. What is the reason for having to re-implement percent coding and not using PercentCodec from core? What is wrong with PercentCodec encoding?
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.
@ok2c RFC 7639 requires canonical token encoding: octets MUST NOT be percent-encoded if they are valid RFC 7230 tchar (except ‘%’), and percent-encoding MUST use uppercase hex.
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.
@arturobernalg There are two publicly accessible instances and they both do not fit the requirements?
This I do not quite understand. Can you give me an example of what
PercentCodecproduces and what this new RFC requires?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.
@arturobernalg At any rate, could you contribute the correct codec to core? That would be the proper way to start.