Skip to content

Commit f945c6d

Browse files
committed
fix: replace other String usage by bavshin-f5\'s impl
1 parent a79f3e8 commit f945c6d

File tree

2 files changed

+41
-34
lines changed

2 files changed

+41
-34
lines changed

nginx-sys/src/lib.rs

Lines changed: 40 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -116,27 +116,6 @@ impl ngx_str_t {
116116
bytes_to_uchar(pool, src).map(|data| Self { data, len: src.len() })
117117
}
118118

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-
140119
/// Create an `ngx_str_t` instance from a string slice (`&str`).
141120
///
142121
/// # Arguments
@@ -221,18 +200,46 @@ impl TryFrom<ngx_str_t> for &str {
221200
pub unsafe fn add_to_ngx_table(
222201
table: *mut ngx_table_elt_t,
223202
pool: *mut ngx_pool_t,
224-
key: &str,
225-
value: &str,
203+
key: impl AsRef<[u8]>,
204+
value: impl AsRef<[u8]>,
226205
) -> 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\x80Hello World", "\\xf0\\x90\\x80Hello World"),
233+
(b"Hello \xF0\x90\x80World", "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+
}
229244
}
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-
})
238245
}

src/http/status.rs

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

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

0 commit comments

Comments
 (0)