@@ -116,27 +116,6 @@ impl ngx_str_t {
116
116
bytes_to_uchar ( pool, src) . map ( |data| Self { data, len : src. len ( ) } )
117
117
}
118
118
119
- /// Create an `ngx_str_t` instance from a `String`.
120
- ///
121
- /// # Arguments
122
- ///
123
- /// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
124
- /// * `data` - The `String` from which to create the nginx string.
125
- ///
126
- /// # Safety
127
- /// This function is marked as unsafe because it accepts a raw pointer argument. There is no
128
- /// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to
129
- /// avoid indeterminate behavior.
130
- ///
131
- /// # Returns
132
- /// An `ngx_str_t` instance representing the given `String`.
133
- pub unsafe fn from_string ( pool : * mut ngx_pool_t , data : String ) -> Self {
134
- ngx_str_t {
135
- data : str_to_uchar ( pool, data. as_str ( ) ) ,
136
- len : data. len ( ) ,
137
- }
138
- }
139
-
140
119
/// Create an `ngx_str_t` instance from a string slice (`&str`).
141
120
///
142
121
/// # Arguments
@@ -221,18 +200,46 @@ impl TryFrom<ngx_str_t> for &str {
221
200
pub unsafe fn add_to_ngx_table (
222
201
table : * mut ngx_table_elt_t ,
223
202
pool : * mut ngx_pool_t ,
224
- key : & str ,
225
- value : & str ,
203
+ key : impl AsRef < [ u8 ] > ,
204
+ value : impl AsRef < [ u8 ] > ,
226
205
) -> Option < ( ) > {
227
- if table. is_null ( ) {
228
- return None ;
206
+ if let Some ( table) = table. as_mut ( ) {
207
+ let key = key. as_ref ( ) ;
208
+ table. key = ngx_str_t:: from_bytes ( pool, key) ?;
209
+ table. value = ngx_str_t:: from_bytes ( pool, value. as_ref ( ) ) ?;
210
+ table. lowcase_key = ngx_pnalloc ( pool, table. key . len ) . cast ( ) ;
211
+ if table. lowcase_key . is_null ( ) {
212
+ return None ;
213
+ }
214
+ table. hash = ngx_hash_strlow ( table. lowcase_key , table. key . data , table. key . len ) ;
215
+ return Some ( ( ) ) ;
216
+ }
217
+ None
218
+ }
219
+
220
+ #[ cfg( test) ]
221
+ mod tests {
222
+ use std:: string:: ToString ;
223
+
224
+ use super :: * ;
225
+
226
+ #[ test]
227
+ fn ngx_str_display ( ) {
228
+ let pairs: & [ ( & [ u8 ] , & str ) ] = & [
229
+ ( b"" , "" ) ,
230
+ ( b"Ferris the \xf0 \x9f \xa6 \x80 " , "Ferris the 🦀" ) ,
231
+ ( b"\xF0 \x90 \x80 " , "\\ xf0\\ x90\\ x80" ) ,
232
+ ( b"\xF0 \x90 \x80 Hello World" , "\\ xf0\\ x90\\ x80Hello World" ) ,
233
+ ( b"Hello \xF0 \x90 \x80 World" , "Hello \\ xf0\\ x90\\ x80World" ) ,
234
+ ( b"Hello World\xF0 \x90 \x80 " , "Hello World\\ xf0\\ x90\\ x80" ) ,
235
+ ] ;
236
+
237
+ for ( bytes, expected) in pairs {
238
+ let str = ngx_str_t {
239
+ data : bytes. as_ptr ( ) . cast_mut ( ) ,
240
+ len : bytes. len ( ) ,
241
+ } ;
242
+ assert_eq ! ( str . to_string( ) , * expected) ;
243
+ }
229
244
}
230
- table. as_mut ( ) . map ( |table| {
231
- table. hash = 1 ;
232
- table. key . len = key. len ( ) ;
233
- table. key . data = str_to_uchar ( pool, key) ;
234
- table. value . len = value. len ( ) ;
235
- table. value . data = str_to_uchar ( pool, value) ;
236
- table. lowcase_key = str_to_uchar ( pool, String :: from ( key) . to_ascii_lowercase ( ) . as_str ( ) ) ;
237
- } )
238
245
}
0 commit comments