|
5 | 5 | pub mod detail;
|
6 | 6 | mod event;
|
7 | 7 | mod queue;
|
| 8 | +mod string; |
8 | 9 |
|
9 |
| -use core::fmt; |
10 | 10 | use core::mem::offset_of;
|
11 |
| -use core::ptr::{self, copy_nonoverlapping}; |
12 |
| -use core::slice; |
| 11 | +use core::ptr; |
13 | 12 |
|
14 | 13 | #[doc(hidden)]
|
15 | 14 | mod bindings {
|
@@ -45,165 +44,6 @@ pub const NGX_HTTP_SRV_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, srv_
|
45 | 44 | /// This is used to access the location configuration context for an HTTP module.
|
46 | 45 | pub const NGX_HTTP_LOC_CONF_OFFSET: usize = offset_of!(ngx_http_conf_ctx_t, loc_conf);
|
47 | 46 |
|
48 |
| -/// Convert a byte slice to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool. |
49 |
| -/// |
50 |
| -/// # Safety |
51 |
| -/// |
52 |
| -/// The caller must provide a valid pointer to the memory pool. |
53 |
| -pub unsafe fn bytes_to_uchar(pool: *mut ngx_pool_t, data: &[u8]) -> Option<*mut u_char> { |
54 |
| - let ptr: *mut u_char = ngx_pnalloc(pool, data.len()) as _; |
55 |
| - if ptr.is_null() { |
56 |
| - return None; |
57 |
| - } |
58 |
| - copy_nonoverlapping(data.as_ptr(), ptr, data.len()); |
59 |
| - Some(ptr) |
60 |
| -} |
61 |
| - |
62 |
| -/// Convert a string slice (`&str`) to a raw pointer (`*mut u_char`) allocated in the given nginx |
63 |
| -/// memory pool. |
64 |
| -/// |
65 |
| -/// # Arguments |
66 |
| -/// |
67 |
| -/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`). |
68 |
| -/// * `data` - The string slice to convert to a raw pointer. |
69 |
| -/// |
70 |
| -/// # Safety |
71 |
| -/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory |
72 |
| -/// allocation using `ngx_pnalloc`. |
73 |
| -/// |
74 |
| -/// # Returns |
75 |
| -/// A raw pointer (`*mut u_char`) to the allocated memory containing the converted string data. |
76 |
| -/// |
77 |
| -/// # Example |
78 |
| -/// ```rust,ignore |
79 |
| -/// let pool: *mut ngx_pool_t = ...; // Obtain a pointer to the nginx memory pool |
80 |
| -/// let data: &str = "example"; // The string to convert |
81 |
| -/// let ptr = str_to_uchar(pool, data); |
82 |
| -/// ``` |
83 |
| -pub unsafe fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char { |
84 |
| - let ptr: *mut u_char = ngx_pnalloc(pool, data.len()) as _; |
85 |
| - debug_assert!(!ptr.is_null()); |
86 |
| - copy_nonoverlapping(data.as_ptr(), ptr, data.len()); |
87 |
| - ptr |
88 |
| -} |
89 |
| - |
90 |
| -impl ngx_str_t { |
91 |
| - /// Returns the contents of this `ngx_str_t` as a byte slice. |
92 |
| - /// |
93 |
| - /// The returned slice will **not** contain the optional nul terminator that `ngx_str_t.data` |
94 |
| - /// may have. |
95 |
| - #[inline] |
96 |
| - pub fn as_bytes(&self) -> &[u8] { |
97 |
| - if self.is_empty() { |
98 |
| - &[] |
99 |
| - } else { |
100 |
| - // SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer |
101 |
| - unsafe { slice::from_raw_parts(self.data, self.len) } |
102 |
| - } |
103 |
| - } |
104 |
| - |
105 |
| - /// Returns the contents of this `ngx_str_t` as a mutable byte slice. |
106 |
| - #[inline] |
107 |
| - pub fn as_bytes_mut(&mut self) -> &mut [u8] { |
108 |
| - if self.is_empty() { |
109 |
| - &mut [] |
110 |
| - } else { |
111 |
| - // SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer |
112 |
| - unsafe { slice::from_raw_parts_mut(self.data, self.len) } |
113 |
| - } |
114 |
| - } |
115 |
| - |
116 |
| - /// Returns `true` if the string has a length of 0. |
117 |
| - #[inline] |
118 |
| - pub fn is_empty(&self) -> bool { |
119 |
| - self.len == 0 |
120 |
| - } |
121 |
| - |
122 |
| - /// Convert the nginx string to a string slice (`&str`). |
123 |
| - /// |
124 |
| - /// # Panics |
125 |
| - /// This function panics if the `ngx_str_t` is not valid UTF-8. |
126 |
| - /// |
127 |
| - /// # Returns |
128 |
| - /// A string slice (`&str`) representing the nginx string. |
129 |
| - pub fn to_str(&self) -> &str { |
130 |
| - core::str::from_utf8(self.as_bytes()).unwrap() |
131 |
| - } |
132 |
| - |
133 |
| - /// Creates an empty `ngx_str_t` instance. |
134 |
| - /// |
135 |
| - /// This method replaces the `ngx_null_string` C macro. |
136 |
| - pub const fn empty() -> Self { |
137 |
| - ngx_str_t { |
138 |
| - len: 0, |
139 |
| - data: ptr::null_mut(), |
140 |
| - } |
141 |
| - } |
142 |
| - |
143 |
| - /// Create an `ngx_str_t` instance from a byte slice. |
144 |
| - /// |
145 |
| - /// # Safety |
146 |
| - /// |
147 |
| - /// The caller must provide a valid pointer to a memory pool. |
148 |
| - pub unsafe fn from_bytes(pool: *mut ngx_pool_t, src: &[u8]) -> Option<Self> { |
149 |
| - bytes_to_uchar(pool, src).map(|data| Self { |
150 |
| - data, |
151 |
| - len: src.len(), |
152 |
| - }) |
153 |
| - } |
154 |
| - |
155 |
| - /// Create an `ngx_str_t` instance from a string slice (`&str`). |
156 |
| - /// |
157 |
| - /// # Arguments |
158 |
| - /// |
159 |
| - /// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`). |
160 |
| - /// * `data` - The string slice from which to create the nginx string. |
161 |
| - /// |
162 |
| - /// # Safety |
163 |
| - /// This function is marked as unsafe because it accepts a raw pointer argument. There is no |
164 |
| - /// way to know if `pool` is pointing to valid memory. The caller must provide a valid pool to |
165 |
| - /// avoid indeterminate behavior. |
166 |
| - /// |
167 |
| - /// # Returns |
168 |
| - /// An `ngx_str_t` instance representing the given string slice. |
169 |
| - pub unsafe fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self { |
170 |
| - ngx_str_t { |
171 |
| - data: str_to_uchar(pool, data), |
172 |
| - len: data.len(), |
173 |
| - } |
174 |
| - } |
175 |
| -} |
176 |
| - |
177 |
| -impl Default for ngx_str_t { |
178 |
| - fn default() -> Self { |
179 |
| - Self::empty() |
180 |
| - } |
181 |
| -} |
182 |
| - |
183 |
| -impl From<ngx_str_t> for &[u8] { |
184 |
| - fn from(s: ngx_str_t) -> Self { |
185 |
| - if s.len == 0 || s.data.is_null() { |
186 |
| - return Default::default(); |
187 |
| - } |
188 |
| - unsafe { slice::from_raw_parts(s.data, s.len) } |
189 |
| - } |
190 |
| -} |
191 |
| - |
192 |
| -impl fmt::Display for ngx_str_t { |
193 |
| - #[inline] |
194 |
| - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
195 |
| - detail::display_bytes(f, self.as_bytes()) |
196 |
| - } |
197 |
| -} |
198 |
| - |
199 |
| -impl TryFrom<ngx_str_t> for &str { |
200 |
| - type Error = core::str::Utf8Error; |
201 |
| - |
202 |
| - fn try_from(s: ngx_str_t) -> Result<Self, Self::Error> { |
203 |
| - core::str::from_utf8(s.into()) |
204 |
| - } |
205 |
| -} |
206 |
| - |
207 | 47 | impl ngx_command_t {
|
208 | 48 | /// Creates a new empty [`ngx_command_t`] instance.
|
209 | 49 | ///
|
|
0 commit comments