Skip to content

Commit 3d7df98

Browse files
committed
remove dependency of urlencoding crate
implement the url encode fn
1 parent e816cb9 commit 3d7df98

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

opentelemetry/Cargo.toml

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ futures-sink = "0.3"
2626
once_cell = { workspace = true }
2727
pin-project-lite = { workspace = true, optional = true }
2828
thiserror = { workspace = true }
29-
urlencoding = "2.1.2"
3029

3130
[target.'cfg(all(target_arch = "wasm32", not(target_os = "wasi")))'.dependencies]
3231
js-sys = "0.3.63"

opentelemetry/src/baggage.rs

+41-2
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
//! [W3C Baggage]: https://w3c.github.io/baggage
1717
use crate::{Context, Key, KeyValue, Value};
1818
use once_cell::sync::Lazy;
19+
use std::borrow::Cow;
1920
use std::collections::{hash_map, HashMap};
2021
use std::fmt;
2122
use std::iter::FromIterator;
22-
use urlencoding::encode;
2323

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

@@ -282,10 +282,29 @@ impl FromIterator<KeyValueMetadata> for Baggage {
282282
}
283283
}
284284

285+
fn encode(s: Cow<'_, str>) -> String {
286+
let special_characters = ['.', '-', '_', '~'];
287+
let mut encoded_string = String::new();
288+
289+
for char in s.as_ref().chars() {
290+
if char.is_ascii_alphanumeric() || special_characters.contains(&char) {
291+
encoded_string.push(char);
292+
} else if char == ' ' {
293+
encoded_string.push_str("%20");
294+
} else {
295+
// Convert to a two-digit hexadecimal representation
296+
for byte in char.to_string().as_bytes() {
297+
encoded_string.push_str(&format!("%{:02X}", byte));
298+
}
299+
}
300+
}
301+
encoded_string
302+
}
303+
285304
impl fmt::Display for Baggage {
286305
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
287306
for (i, (k, v)) in self.into_iter().enumerate() {
288-
write!(f, "{}={}", k, encode(&v.0.as_str()))?;
307+
write!(f, "{}={}", k, encode(v.0.as_str()))?;
289308
if !v.1.as_str().is_empty() {
290309
write!(f, ";{}", v.1)?;
291310
}
@@ -473,6 +492,26 @@ mod tests {
473492
assert_eq!(baggage.len(), 0, "did not insert invalid key");
474493
}
475494

495+
#[test]
496+
fn test_ascii_values() {
497+
let string1 = Cow::Borrowed("test_ 123");
498+
let string2 = Cow::Borrowed("Hello123");
499+
let string3 = Cow::Borrowed("This & That = More");
500+
let string4 = Cow::Borrowed("Unicode: 😊");
501+
let string5 = Cow::Borrowed("Non-ASCII: áéíóú");
502+
let string6 = Cow::Borrowed("Unsafe: ~!@#$%^&*()_+{}[];:'\\\"<>?,./");
503+
504+
assert_eq!(encode(string1), "test_%20123");
505+
assert_eq!(encode(string2), "Hello123");
506+
assert_eq!(encode(string3), "This%20%26%20That%20%3D%20More");
507+
assert_eq!(encode(string4), "Unicode%3A%20%F0%9F%98%8A");
508+
assert_eq!(
509+
encode(string5),
510+
"Non-ASCII%3A%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA"
511+
);
512+
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");
513+
}
514+
476515
#[test]
477516
fn insert_too_much_baggage() {
478517
// too many key pairs

0 commit comments

Comments
 (0)