-
Notifications
You must be signed in to change notification settings - Fork 1k
Adds '+' character to allowed characters in baggage value #4898
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 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c8fc3ff
Adds '+' character to allowed characters in baggade value
lmonkiewicz 8ea223c
Formatting fix
lmonkiewicz a10cd4a
Adda BaggageCodec implementation
lmonkiewicz c7dc4ae
Additional cleanup
lmonkiewicz 8d50633
Removal of Nullable method parameters.
lmonkiewicz ac938dc
Merge remote-tracking branch 'origin/main' into issue-4892
lmonkiewicz 992e63b
Additional tests for baggage decoding
lmonkiewicz 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
144 changes: 144 additions & 0 deletions
144
api/all/src/main/java/io/opentelemetry/api/baggage/propagation/BaggageCodec.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,144 @@ | ||
| /* | ||
| * Copyright The OpenTelemetry Authors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package io.opentelemetry.api.baggage.propagation; | ||
|
|
||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.Serializable; | ||
| import java.nio.charset.Charset; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.BitSet; | ||
| import javax.annotation.Nullable; | ||
|
|
||
| /** | ||
| * Note: This class is based on code from Apache Commons Codec. It is comprised of code from these | ||
| * classes: | ||
| * | ||
| * <ul> | ||
| * <li><a | ||
| * href="https://github.com/apache/commons-codec/blob/482df6cabfb288acb6ab3e4a732fdb93aecfa7c2/src/main/java/org/apache/commons/codec/net/URLCodec.java">org.apache.commons.codec.net.URLCodec</a> | ||
| * <li><a | ||
| * href="https://github.com/apache/commons-codec/blob/482df6cabfb288acb6ab3e4a732fdb93aecfa7c2/src/main/java/org/apache/commons/codec/net/Utils.java">org.apache.commons.codec.net.Utils</a> | ||
| * </ul> | ||
| * | ||
| * <p>Implements baggage-octet decoding in accordance with th <a | ||
| * href="https://w3c.github.io/baggage/#definition">Baggage header content</a> specification. All | ||
| * US-ASCII characters excluding CTLs, whitespace, DQUOTE, comma, semicolon and backslash are | ||
| * encoded in `www-form-urlencoded` encoding scheme. | ||
| */ | ||
| class BaggageCodec { | ||
|
|
||
| private static final byte ESCAPE_CHAR = '%'; | ||
|
|
||
| private static final BitSet WWW_FORM_URL_SAFE = new BitSet(256); | ||
|
|
||
| // Static initializer for www_form_url | ||
| static { | ||
| // alpha characters | ||
| for (int i = 'a'; i <= 'z'; i++) { | ||
| WWW_FORM_URL_SAFE.set(i); | ||
| } | ||
| for (int i = 'A'; i <= 'Z'; i++) { | ||
| WWW_FORM_URL_SAFE.set(i); | ||
| } | ||
| // numeric characters | ||
| for (int i = '0'; i <= '9'; i++) { | ||
| WWW_FORM_URL_SAFE.set(i); | ||
| } | ||
| // special chars | ||
| WWW_FORM_URL_SAFE.set('-'); | ||
| WWW_FORM_URL_SAFE.set('_'); | ||
| WWW_FORM_URL_SAFE.set('.'); | ||
| WWW_FORM_URL_SAFE.set('*'); | ||
| // blank to be replaced with + | ||
| WWW_FORM_URL_SAFE.set(' '); | ||
| } | ||
|
|
||
| private BaggageCodec() {} | ||
|
|
||
| /** | ||
| * Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped | ||
| * characters are converted back to their original representation. | ||
| * | ||
| * @param bytes array of URL safe characters | ||
| * @return array of original bytes | ||
| */ | ||
| @Nullable | ||
| static byte[] decode(@Nullable byte[] bytes) { | ||
|
jack-berg marked this conversation as resolved.
Outdated
|
||
| if (bytes == null) { | ||
| return null; | ||
| } | ||
| ByteArrayOutputStream buffer = new ByteArrayOutputStream(); | ||
| for (int i = 0; i < bytes.length; i++) { | ||
| int b = bytes[i]; | ||
| if (b == ESCAPE_CHAR) { | ||
|
jack-berg marked this conversation as resolved.
|
||
| try { | ||
| int u = Utils.digit16(bytes[++i]); | ||
| int l = Utils.digit16(bytes[++i]); | ||
| buffer.write((char) ((u << 4) + l)); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| throw new DecoderException("Invalid URL encoding: ", e); | ||
| } | ||
| } else { | ||
| buffer.write(b); | ||
| } | ||
| } | ||
| return buffer.toByteArray(); | ||
| } | ||
|
|
||
| /** | ||
| * Decodes an array of URL safe 7-bit characters into an array of original bytes. Escaped | ||
| * characters are converted back to their original representation. | ||
| * | ||
| * @param value string of URL safe characters | ||
| * @param charset encoding of given string | ||
| * @return decoded value | ||
| */ | ||
| @Nullable | ||
| static String decode(@Nullable String value, Charset charset) { | ||
|
Member
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. Looks like Same recommendation applies to |
||
| if (value == null) { | ||
| return null; | ||
| } | ||
|
|
||
| byte[] bytes = decode(value.getBytes(StandardCharsets.US_ASCII)); | ||
| return new String(bytes, charset); | ||
| } | ||
|
|
||
| static class Utils { | ||
|
jack-berg marked this conversation as resolved.
Outdated
|
||
|
|
||
| /** Radix used in encoding and decoding. */ | ||
| private static final int RADIX = 16; | ||
|
|
||
| private Utils() {} | ||
|
|
||
| /** | ||
| * Returns the numeric value of the character {@code b} in radix 16. | ||
| * | ||
| * @param b The byte to be converted. | ||
| * @return The numeric value represented by the character in radix 16. | ||
| */ | ||
| static int digit16(byte b) { | ||
| int i = Character.digit((char) b, RADIX); | ||
| if (i == -1) { | ||
| throw new DecoderException( | ||
| "Invalid URL encoding: not a valid digit (radix " + RADIX + "): " + b); | ||
| } | ||
| return i; | ||
| } | ||
| } | ||
|
|
||
| public static class DecoderException extends RuntimeException implements Serializable { | ||
|
jack-berg marked this conversation as resolved.
Outdated
|
||
|
|
||
| private static final long serialVersionUID = 4717632118051490483L; | ||
|
|
||
| public DecoderException(String message, Throwable cause) { | ||
| super(message, cause); | ||
| } | ||
|
|
||
| public DecoderException(String message) { | ||
| super(message); | ||
| } | ||
| } | ||
| } | ||
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,7 @@ | ||
| Comparing source compatibility of against | ||
| No changes. | ||
| +++ NEW CLASS: PUBLIC(+) STATIC(+) io.opentelemetry.api.baggage.propagation.BaggageCodec$DecoderException (compatible) | ||
| +++ CLASS FILE FORMAT VERSION: 52.0 <- n.a. | ||
| +++ NEW INTERFACE: java.io.Serializable | ||
| +++ NEW SUPERCLASS: java.lang.RuntimeException | ||
| +++ NEW CONSTRUCTOR: PUBLIC(+) BaggageCodec$DecoderException(java.lang.String) | ||
| +++ NEW CONSTRUCTOR: PUBLIC(+) BaggageCodec$DecoderException(java.lang.String, java.lang.Throwable) |
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.