-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathlib.rs
334 lines (310 loc) · 10 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![no_std]
mod event;
#[cfg(ngx_feature = "http")]
mod http;
mod queue;
#[cfg(ngx_feature = "stream")]
mod stream;
use core::fmt;
use core::ptr::{self, copy_nonoverlapping};
use core::slice;
#[doc(hidden)]
mod bindings {
#![allow(missing_docs)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(clippy::all)]
#![allow(improper_ctypes)]
#![allow(rustdoc::broken_intra_doc_links)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
#[doc(no_inline)]
pub use bindings::*;
pub use event::*;
#[cfg(ngx_feature = "http")]
pub use http::*;
pub use queue::*;
#[cfg(ngx_feature = "stream")]
pub use stream::*;
/// Convert a byte slice to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
///
/// # Safety
///
/// The caller must provide a valid pointer to the memory pool.
pub unsafe fn bytes_to_uchar(pool: *mut ngx_pool_t, data: &[u8]) -> Option<*mut u_char> {
let ptr: *mut u_char = ngx_pnalloc(pool, data.len()) as _;
if ptr.is_null() {
return None;
}
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
Some(ptr)
}
/// Convert a string slice (`&str`) to a raw pointer (`*mut u_char`) allocated in the given nginx
/// memory pool.
///
/// # Arguments
///
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
/// * `data` - The string slice to convert to a raw pointer.
///
/// # Safety
/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory
/// allocation using `ngx_pnalloc`.
///
/// # Returns
/// A raw pointer (`*mut u_char`) to the allocated memory containing the converted string data.
///
/// # Example
/// ```rust,ignore
/// let pool: *mut ngx_pool_t = ...; // Obtain a pointer to the nginx memory pool
/// let data: &str = "example"; // The string to convert
/// let ptr = str_to_uchar(pool, data);
/// ```
pub unsafe fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
let ptr: *mut u_char = ngx_pnalloc(pool, data.len()) as _;
debug_assert!(!ptr.is_null());
copy_nonoverlapping(data.as_ptr(), ptr, data.len());
ptr
}
impl ngx_str_t {
/// Returns the contents of this `ngx_str_t` as a byte slice.
///
/// The returned slice will **not** contain the optional nul terminator that `ngx_str_t.data`
/// may have.
#[inline]
pub fn as_bytes(&self) -> &[u8] {
if self.is_empty() {
&[]
} else {
// SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer
unsafe { slice::from_raw_parts(self.data, self.len) }
}
}
/// Returns the contents of this `ngx_str_t` as a mutable byte slice.
#[inline]
pub fn as_bytes_mut(&mut self) -> &mut [u8] {
if self.is_empty() {
&mut []
} else {
// SAFETY: `ngx_str_t` with non-zero len must contain a valid correctly aligned pointer
unsafe { slice::from_raw_parts_mut(self.data, self.len) }
}
}
/// Returns `true` if the string has a length of 0.
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Convert the nginx string to a string slice (`&str`).
///
/// # Panics
/// This function panics if the `ngx_str_t` is not valid UTF-8.
///
/// # Returns
/// A string slice (`&str`) representing the nginx string.
pub fn to_str(&self) -> &str {
core::str::from_utf8(self.as_bytes()).unwrap()
}
/// Creates an empty `ngx_str_t` instance.
///
/// This method replaces the `ngx_null_string` C macro.
pub const fn empty() -> Self {
ngx_str_t {
len: 0,
data: ptr::null_mut(),
}
}
/// Create an `ngx_str_t` instance from a byte slice.
///
/// # Safety
///
/// The caller must provide a valid pointer to a memory pool.
pub unsafe fn from_bytes(pool: *mut ngx_pool_t, src: &[u8]) -> Option<Self> {
bytes_to_uchar(pool, src).map(|data| Self {
data,
len: src.len(),
})
}
/// Create an `ngx_str_t` instance from a string slice (`&str`).
///
/// # Arguments
///
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
/// * `data` - The string slice 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 slice.
pub unsafe fn from_str(pool: *mut ngx_pool_t, data: &str) -> Self {
ngx_str_t {
data: str_to_uchar(pool, data),
len: data.len(),
}
}
}
impl Default for ngx_str_t {
fn default() -> Self {
Self::empty()
}
}
impl From<ngx_str_t> for &[u8] {
fn from(s: ngx_str_t) -> Self {
if s.len == 0 || s.data.is_null() {
return Default::default();
}
unsafe { slice::from_raw_parts(s.data, s.len) }
}
}
impl fmt::Display for ngx_str_t {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// The implementation is similar to an inlined `String::from_utf8_lossy`, with two
// important differences:
//
// - it writes directly to the Formatter instead of allocating a temporary String
// - invalid sequences are represented as escaped individual bytes
for chunk in self.as_bytes().utf8_chunks() {
f.write_str(chunk.valid())?;
for byte in chunk.invalid() {
f.write_str("\\x")?;
fmt::LowerHex::fmt(byte, f)?;
}
}
Ok(())
}
}
impl TryFrom<ngx_str_t> for &str {
type Error = core::str::Utf8Error;
fn try_from(s: ngx_str_t) -> Result<Self, Self::Error> {
core::str::from_utf8(s.into())
}
}
impl ngx_command_t {
/// Creates a new empty [`ngx_command_t`] instance.
///
/// This method replaces the `ngx_null_command` C macro. This is typically used to terminate an
/// array of configuration directives.
///
/// [`ngx_command_t`]: https://nginx.org/en/docs/dev/development_guide.html#config_directives
pub const fn empty() -> Self {
Self {
name: ngx_str_t::empty(),
type_: 0,
set: None,
conf: 0,
offset: 0,
post: ptr::null_mut(),
}
}
}
impl ngx_module_t {
/// Create a new `ngx_module_t` instance with default values.
pub const fn default() -> Self {
Self {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr(),
ctx: ptr::null_mut(),
commands: ptr::null_mut(),
type_: 0,
init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,
spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
}
}
}
/// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool.
///
/// # Arguments
///
/// * `table` - A pointer to the nginx table entry (`ngx_table_elt_t`) to modify.
/// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`) for memory allocation.
/// * `key` - The key string to add to the table entry.
/// * `value` - The value string to add to the table entry.
///
/// # Safety
/// This function is marked as unsafe because it involves raw pointer manipulation and direct memory
/// allocation using `str_to_uchar`.
///
/// # Returns
/// An `Option<()>` representing the result of the operation. `Some(())` indicates success, while
/// `None` indicates a null table pointer.
///
/// # Example
/// ```rust
/// # use nginx_sys::*;
/// # unsafe fn example(pool: *mut ngx_pool_t, headers: *mut ngx_list_t) {
/// // Obtain a pointer to the nginx table entry
/// let table: *mut ngx_table_elt_t = ngx_list_push(headers).cast();
/// assert!(!table.is_null());
/// let key: &str = "key"; // The key to add
/// let value: &str = "value"; // The value to add
/// let result = add_to_ngx_table(table, pool, key, value);
/// # }
/// ```
pub unsafe fn add_to_ngx_table(
table: *mut ngx_table_elt_t,
pool: *mut ngx_pool_t,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
) -> Option<()> {
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);
}
}
}