Skip to content
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

remove dependency of urlencoding crate #1615

Merged
merged 17 commits into from
Mar 14, 2024
Merged
Show file tree
Hide file tree
Changes from 13 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
3 changes: 3 additions & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## vNext

### Removed
- Remove `urlencoding` crate dependency. [#1613](https://github.com/open-telemetry/opentelemetry-rust/pull/1613)

## v0.22.0

### Added
Expand Down
1 change: 0 additions & 1 deletion opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ futures-sink = "0.3"
once_cell = { workspace = true }
pin-project-lite = { workspace = true, optional = true }
thiserror = { workspace = true }
urlencoding = "2.1.2"

[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies]
js-sys = "0.3.63"
Expand Down
43 changes: 41 additions & 2 deletions opentelemetry/src/baggage.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use once_cell::sync::Lazy;
use std::collections::{hash_map, HashMap};
use std::fmt;
use std::iter::FromIterator;
use urlencoding::encode;

static DEFAULT_BAGGAGE: Lazy<Baggage> = Lazy::new(Baggage::default);

Expand Down Expand Up @@ -282,10 +281,30 @@ impl FromIterator<KeyValueMetadata> for Baggage {
}
}

fn encode(s: &str) -> String {
let special_characters = ['.', '-', '_', '~'];
let mut encoded_string = String::with_capacity(s.len());

for char in s.chars() {
if char.is_ascii_alphanumeric() || special_characters.contains(&char) {
encoded_string.push(char);
} else if char == ' ' {
encoded_string.push_str("%20");
} else {
let mut buffer = [0; 4];
let encoded_char = char.encode_utf8(&mut buffer);
for byte in encoded_char.as_bytes() {
encoded_string.push_str(&format!("%{:02X}", byte));
}
}
}
encoded_string
}

impl fmt::Display for Baggage {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
for (i, (k, v)) in self.into_iter().enumerate() {
write!(f, "{}={}", k, encode(&v.0.as_str()))?;
write!(f, "{}={}", k, encode(v.0.as_str().as_ref()))?;
if !v.1.as_str().is_empty() {
write!(f, ";{}", v.1)?;
}
Expand Down Expand Up @@ -473,6 +492,26 @@ mod tests {
assert_eq!(baggage.len(), 0, "did not insert invalid key");
}

#[test]
fn test_ascii_values() {
let string1 = "test_ 123";
let string2 = "Hello123";
let string3 = "This & That = More";
let string4 = "Unicode: 😊";
let string5 = "Non-ASCII: áéíóú";
let string6 = "Unsafe: ~!@#$%^&*()_+{}[];:'\\\"<>?,./";

assert_eq!(encode(string1), "test_%20123");
assert_eq!(encode(string2), "Hello123");
assert_eq!(encode(string3), "This%20%26%20That%20%3D%20More");
assert_eq!(encode(string4), "Unicode%3A%20%F0%9F%98%8A");
assert_eq!(
encode(string5),
"Non-ASCII%3A%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA"
);
assert_eq!(encode(string6), "Unsafe%3A%20~%21%40%23%24%25%5E%26%2A%28%29_%2B%7B%7D%5B%5D%3B%3A%27%5C%22%3C%3E%3F%2C.%2F");
}

#[test]
fn insert_too_much_baggage() {
// too many key pairs
Expand Down
Loading