Skip to content

Commit bd0dc17

Browse files
committed
fix: replace other String usage by bavshin-f5's impl
1 parent 0174f10 commit bd0dc17

File tree

3 files changed

+42
-43
lines changed

3 files changed

+42
-43
lines changed

nginx-sys/src/lib.rs

Lines changed: 41 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@ use core::fmt;
66
use core::ptr::copy_nonoverlapping;
77
use core::slice;
88

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-
149
#[doc(hidden)]
1510
mod bindings {
1611
#![allow(missing_docs)]
@@ -122,28 +117,6 @@ impl ngx_str_t {
122117
bytes_to_uchar(pool, src).map(|data| Self { data, len: src.len() })
123118
}
124119

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-
147120
/// Create an `ngx_str_t` instance from a string slice (`&str`).
148121
///
149122
/// # Arguments
@@ -228,22 +201,50 @@ impl TryFrom<ngx_str_t> for &str {
228201
/// let result = add_to_ngx_table(table, pool, key, value);
229202
/// # }
230203
/// ```
231-
#[cfg(feature = "alloc")]
232204
pub unsafe fn add_to_ngx_table(
233205
table: *mut ngx_table_elt_t,
234206
pool: *mut ngx_pool_t,
235-
key: &str,
236-
value: &str,
207+
key: impl AsRef<[u8]>,
208+
value: impl AsRef<[u8]>,
237209
) -> 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\x80Hello World", "\\xf0\\x90\\x80Hello World"),
238+
(b"Hello \xF0\x90\x80World", "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+
}
240249
}
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-
})
249250
}

src/http/request.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,6 @@ impl Request {
263263
/// Add header to the `headers_in` object.
264264
///
265265
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
266-
#[cfg(feature = "alloc")]
267266
pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> {
268267
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_in.headers) as _ };
269268
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
@@ -272,7 +271,6 @@ impl Request {
272271
/// Add header to the `headers_out` object.
273272
///
274273
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
275-
#[cfg(feature = "alloc")]
276274
pub fn add_header_out(&mut self, key: &str, value: &str) -> Option<()> {
277275
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_out.headers) as _ };
278276
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }

src/http/status.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ impl InvalidHTTPStatusCode {
2424

2525
impl fmt::Display for InvalidHTTPStatusCode {
2626
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
27-
f.write_str("invalid status code".to_string().as_str())
27+
f.write_str("invalid status code")
2828
}
2929
}
3030

0 commit comments

Comments
 (0)