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
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
3 changes: 3 additions & 0 deletions opentelemetry/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -2,6 +2,9 @@

## vNext

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

## v0.22.0

### Added
1 change: 0 additions & 1 deletion opentelemetry/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
42 changes: 40 additions & 2 deletions opentelemetry/src/baggage.rs
Original file line number Diff line number Diff line change
@@ -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);

@@ -282,10 +281,25 @@ impl FromIterator<KeyValueMetadata> for Baggage {
}
}

fn encode(s: &str) -> String {
let mut encoded_string = String::with_capacity(s.len());

for byte in s.as_bytes() {
match *byte {
b'a'..=b'z' | b'A'..=b'Z' | b'0'..=b'9' | b'.' | b'-' | b'_' | b'~' => {
encoded_string.push(*byte as char)
}
b' ' => encoded_string.push_str("%20"),
_ => 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)?;
}
@@ -473,6 +487,30 @@ 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: ~!@#$%^&*()_+{}[];:'\\\"<>?,./";
let string7: &str = "🚀Unicode:";
let string8 = "ΑΒΓ";

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");
assert_eq!(encode(string7), "%F0%9F%9A%80Unicode%3A");
assert_eq!(encode(string8), "%CE%91%CE%92%CE%93");
}

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