1
1
use std:: str:: { self , FromStr } ;
2
2
3
+ use http:: HeaderValue ;
4
+
5
+ pub trait HeaderValueExt {
6
+ fn to_utf8_str ( & self ) -> Result < & str , std:: str:: Utf8Error > ;
7
+ }
8
+
9
+ impl HeaderValueExt for HeaderValue {
10
+ // Some header values may contain utf8 characters (e.g authdata) so we use `str::from_utf8`
11
+ // rather than using `HeaderValues::to_str` which errors on non-visible ASCII characters.
12
+ fn to_utf8_str ( & self ) -> Result < & str , std:: str:: Utf8Error > {
13
+ std:: str:: from_utf8 ( self . as_bytes ( ) )
14
+ }
15
+ }
16
+
3
17
#[ derive( Debug , Copy , Clone , Hash , Eq , PartialEq ) ]
4
18
pub enum MetaKey {
5
19
TraceParent ,
@@ -78,17 +92,15 @@ impl MetaMapMut for reqwest::header::HeaderMap {
78
92
79
93
impl MetaMap for axum:: http:: HeaderMap {
80
94
fn get_meta ( & self , key : MetaKey ) -> Option < & str > {
81
- // Some meta values may contain utf8 characters (e.g authdata) so we use `str::from_utf8`
82
- // rather than using `HeaderValues::to_str` which errors on non-visible ASCII characters.
83
95
self . get ( key. header_key ( ) )
84
- . and_then ( |val| std :: str :: from_utf8 ( val. as_bytes ( ) ) . ok ( ) )
96
+ . and_then ( |val| val. to_utf8_str ( ) . ok ( ) )
85
97
}
86
98
87
99
fn meta_values < ' a > ( & ' a self , key : MetaKey ) -> Box < dyn Iterator < Item = & ' a str > + ' a > {
88
100
Box :: new (
89
101
self . get_all ( key. header_key ( ) )
90
102
. iter ( )
91
- . filter_map ( |v| v. to_str ( ) . ok ( ) ) ,
103
+ . filter_map ( |v| v. to_utf8_str ( ) . ok ( ) ) ,
92
104
)
93
105
}
94
106
@@ -113,15 +125,15 @@ impl MetaMap for pingora::http::RequestHeader {
113
125
fn get_meta ( & self , key : MetaKey ) -> Option < & str > {
114
126
self . headers
115
127
. get ( key. header_key ( ) )
116
- . and_then ( |v| v. to_str ( ) . ok ( ) )
128
+ . and_then ( |v| v. to_utf8_str ( ) . ok ( ) )
117
129
}
118
130
119
131
fn meta_values < ' a > ( & ' a self , key : MetaKey ) -> Box < dyn Iterator < Item = & ' a str > + ' a > {
120
132
Box :: new (
121
133
self . headers
122
134
. get_all ( key. header_key ( ) )
123
135
. iter ( )
124
- . filter_map ( |v| v. to_str ( ) . ok ( ) ) ,
136
+ . filter_map ( |v| v. to_utf8_str ( ) . ok ( ) ) ,
125
137
)
126
138
}
127
139
0 commit comments