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