Skip to content

Commit

Permalink
fix: replace other String usage by bavshin-f5's impl
Browse files Browse the repository at this point in the history
  • Loading branch information
JyJyJcr committed Jan 2, 2025
1 parent 0174f10 commit bd0dc17
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 43 deletions.
81 changes: 41 additions & 40 deletions nginx-sys/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,6 @@ use core::fmt;
use core::ptr::copy_nonoverlapping;
use core::slice;

#[cfg(all(not(feature = "std"), feature = "alloc"))]
use alloc::string::{FromUtf8Error, String};
#[cfg(feature = "std")]
use std::string::{FromUtf8Error, String};

#[doc(hidden)]
mod bindings {
#![allow(missing_docs)]
Expand Down Expand Up @@ -122,28 +117,6 @@ impl ngx_str_t {
bytes_to_uchar(pool, src).map(|data| Self { data, len: src.len() })
}

/// Create an `ngx_str_t` instance from a `String`.
///
/// # Arguments
///
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
/// * `data` - The `String` from which to create the nginx string.
///
/// # Safety
/// This function is marked as unsafe because it accepts a raw pointer argument. There is no
/// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to
/// avoid indeterminate behavior.
///
/// # Returns
/// An `ngx_str_t` instance representing the given `String`.
#[cfg(feature = "alloc")]
pub unsafe fn from_string(pool: *mut ngx_pool_t, data: String) -> Self {
ngx_str_t {
data: str_to_uchar(pool, data.as_str()),
len: data.len(),
}
}

/// Create an `ngx_str_t` instance from a string slice (`&str`).
///
/// # Arguments
Expand Down Expand Up @@ -228,22 +201,50 @@ impl TryFrom<ngx_str_t> for &str {
/// let result = add_to_ngx_table(table, pool, key, value);
/// # }
/// ```
#[cfg(feature = "alloc")]
pub unsafe fn add_to_ngx_table(
table: *mut ngx_table_elt_t,
pool: *mut ngx_pool_t,
key: &str,
value: &str,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
) -> Option<()> {
if table.is_null() {
return None;
if let Some(table) = table.as_mut() {
let key = key.as_ref();
table.key = ngx_str_t::from_bytes(pool, key)?;
table.value = ngx_str_t::from_bytes(pool, value.as_ref())?;
table.lowcase_key = ngx_pnalloc(pool, table.key.len).cast();
if table.lowcase_key.is_null() {
return None;
}
table.hash = ngx_hash_strlow(table.lowcase_key, table.key.data, table.key.len);
return Some(());
}
None
}

#[cfg(test)]
mod tests {
extern crate alloc;
use alloc::string::ToString;

use super::*;

#[test]
fn ngx_str_display() {
let pairs: &[(&[u8], &str)] = &[
(b"", ""),
(b"Ferris the \xf0\x9f\xa6\x80", "Ferris the 🦀"),
(b"\xF0\x90\x80", "\\xf0\\x90\\x80"),
(b"\xF0\x90\x80Hello World", "\\xf0\\x90\\x80Hello World"),
(b"Hello \xF0\x90\x80World", "Hello \\xf0\\x90\\x80World"),
(b"Hello World\xF0\x90\x80", "Hello World\\xf0\\x90\\x80"),
];

for (bytes, expected) in pairs {
let str = ngx_str_t {
data: bytes.as_ptr().cast_mut(),
len: bytes.len(),
};
assert_eq!(str.to_string(), *expected);
}
}
table.as_mut().map(|table| {
table.hash = 1;
table.key.len = key.len();
table.key.data = str_to_uchar(pool, key);
table.value.len = value.len();
table.value.data = str_to_uchar(pool, value);
table.lowcase_key = str_to_uchar(pool, String::from(key).to_ascii_lowercase().as_str());
})
}
2 changes: 0 additions & 2 deletions src/http/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,6 @@ impl Request {
/// Add header to the `headers_in` object.
///
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
#[cfg(feature = "alloc")]
pub fn add_header_in(&mut self, key: &str, value: &str) -> Option<()> {
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_in.headers) as _ };
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
Expand All @@ -272,7 +271,6 @@ impl Request {
/// Add header to the `headers_out` object.
///
/// See <https://nginx.org/en/docs/dev/development_guide.html#http_request>
#[cfg(feature = "alloc")]
pub fn add_header_out(&mut self, key: &str, value: &str) -> Option<()> {
let table: *mut ngx_table_elt_t = unsafe { ngx_list_push(&mut self.0.headers_out.headers) as _ };
unsafe { add_to_ngx_table(table, self.0.pool, key, value) }
Expand Down
2 changes: 1 addition & 1 deletion src/http/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ impl InvalidHTTPStatusCode {

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

Expand Down

0 comments on commit bd0dc17

Please sign in to comment.