@@ -19,7 +19,6 @@ use once_cell::sync::Lazy;
19
19
use std:: collections:: { hash_map, HashMap } ;
20
20
use std:: fmt;
21
21
use std:: iter:: FromIterator ;
22
- use urlencoding:: encode;
23
22
24
23
static DEFAULT_BAGGAGE : Lazy < Baggage > = Lazy :: new ( Baggage :: default) ;
25
24
@@ -282,10 +281,25 @@ impl FromIterator<KeyValueMetadata> for Baggage {
282
281
}
283
282
}
284
283
284
+ fn encode ( s : & str ) -> String {
285
+ let mut encoded_string = String :: with_capacity ( s. len ( ) ) ;
286
+
287
+ for byte in s. as_bytes ( ) {
288
+ match * byte {
289
+ b'a' ..=b'z' | b'A' ..=b'Z' | b'0' ..=b'9' | b'.' | b'-' | b'_' | b'~' => {
290
+ encoded_string. push ( * byte as char )
291
+ }
292
+ b' ' => encoded_string. push_str ( "%20" ) ,
293
+ _ => encoded_string. push_str ( & format ! ( "%{:02X}" , byte) ) ,
294
+ }
295
+ }
296
+ encoded_string
297
+ }
298
+
285
299
impl fmt:: Display for Baggage {
286
300
fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
287
301
for ( i, ( k, v) ) in self . into_iter ( ) . enumerate ( ) {
288
- write ! ( f, "{}={}" , k, encode( & v. 0 . as_str( ) ) ) ?;
302
+ write ! ( f, "{}={}" , k, encode( v. 0 . as_str( ) . as_ref ( ) ) ) ?;
289
303
if !v. 1 . as_str ( ) . is_empty ( ) {
290
304
write ! ( f, ";{}" , v. 1 ) ?;
291
305
}
@@ -473,6 +487,30 @@ mod tests {
473
487
assert_eq ! ( baggage. len( ) , 0 , "did not insert invalid key" ) ;
474
488
}
475
489
490
+ #[ test]
491
+ fn test_ascii_values ( ) {
492
+ let string1 = "test_ 123" ;
493
+ let string2 = "Hello123" ;
494
+ let string3 = "This & That = More" ;
495
+ let string4 = "Unicode: 😊" ;
496
+ let string5 = "Non-ASCII: áéíóú" ;
497
+ let string6 = "Unsafe: ~!@#$%^&*()_+{}[];:'\\ \" <>?,./" ;
498
+ let string7: & str = "🚀Unicode:" ;
499
+ let string8 = "ΑΒΓ" ;
500
+
501
+ assert_eq ! ( encode( string1) , "test_%20123" ) ;
502
+ assert_eq ! ( encode( string2) , "Hello123" ) ;
503
+ assert_eq ! ( encode( string3) , "This%20%26%20That%20%3D%20More" ) ;
504
+ assert_eq ! ( encode( string4) , "Unicode%3A%20%F0%9F%98%8A" ) ;
505
+ assert_eq ! (
506
+ encode( string5) ,
507
+ "Non-ASCII%3A%20%C3%A1%C3%A9%C3%AD%C3%B3%C3%BA"
508
+ ) ;
509
+ 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" ) ;
510
+ assert_eq ! ( encode( string7) , "%F0%9F%9A%80Unicode%3A" ) ;
511
+ assert_eq ! ( encode( string8) , "%CE%91%CE%92%CE%93" ) ;
512
+ }
513
+
476
514
#[ test]
477
515
fn insert_too_much_baggage ( ) {
478
516
// too many key pairs
0 commit comments