1
- #![ allow( non_upper_case_globals) ]
2
- #![ allow( non_camel_case_types) ]
3
- #![ allow( non_snake_case) ]
4
- #![ allow( dead_code) ]
5
- #![ allow( clippy:: all) ]
6
- #![ allow( improper_ctypes) ]
7
-
8
- include ! ( concat!( env!( "OUT_DIR" ) , "/bindings.rs" ) ) ;
1
+ //! # nginx-sys
2
+ //!
3
+ //! The `nginx-sys` crate provides low-level bindings for the nginx C API, allowing Rust applications to interact with nginx servers and modules.
4
+ //!
5
+ //! ## Usage
6
+ //!
7
+ //! Add `nginx-sys` as a dependency in your `Cargo.toml`:
8
+ //!
9
+ //! ```toml
10
+ //! [dependencies]
11
+ //! nginx-sys = "0.1.0"
12
+ //! ```
13
+ //!
14
+ //! ## Features
15
+ //!
16
+ //! - `build`: Enables the build scripts to compile and link against the nginx C library. This feature is enabled by default.
17
+ //!
18
+ //! ## Examples
19
+ //!
20
+ //! ### Get Nginx Version
21
+ //!
22
+ //! This example demonstrates how to retrieve the version of the nginx server.
23
+ //!
24
+ //! ```rust,no_run
25
+ //! use nginx_sys::nginx_version;
26
+ //!
27
+ //! fn main() {
28
+ //! let version = unsafe { nginx_version() };
29
+ //! println!("Nginx version: {}", version);
30
+ //! }
31
+ //! ```
32
+ //!
33
+ #![ warn( missing_docs) ]
9
34
10
35
use std:: fmt;
11
36
use std:: ptr:: copy_nonoverlapping;
12
37
use std:: slice;
13
38
39
+ #[ doc( hidden) ]
40
+ mod bindings {
41
+ #![ allow( missing_docs) ]
42
+ #![ allow( non_upper_case_globals) ]
43
+ #![ allow( non_camel_case_types) ]
44
+ #![ allow( non_snake_case) ]
45
+ #![ allow( dead_code) ]
46
+ #![ allow( clippy:: all) ]
47
+ #![ allow( improper_ctypes) ]
48
+ #[ allow( rustdoc:: broken_intra_doc_links) ]
49
+ include ! ( concat!( env!( "OUT_DIR" ) , "/bindings.rs" ) ) ;
50
+ }
51
+ #[ doc( no_inline) ]
52
+ pub use bindings:: * ;
53
+
54
+ /// Convert a string slice (`&str`) to a raw pointer (`*mut u_char`) allocated in the given nginx memory pool.
55
+ ///
56
+ /// # Arguments
57
+ ///
58
+ /// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
59
+ /// * `data` - The string slice to convert to a raw pointer.
60
+ ///
61
+ /// # Safety
62
+ /// This function is marked as unsafe because it involves raw pointer manipulation and direct memory allocation using `ngx_palloc`.
63
+ ///
64
+ /// # Returns
65
+ /// A raw pointer (`*mut u_char`) to the allocated memory containing the converted string data.
66
+ ///
67
+ /// # Example
68
+ /// ```rust
69
+ /// let pool: *mut ngx_pool_t = ...; // Obtain a pointer to the nginx memory pool
70
+ /// let data: &str = "example"; // The string to convert
71
+ /// let ptr = str_to_uchar(pool, data);
72
+ /// ```
14
73
pub fn str_to_uchar ( pool : * mut ngx_pool_t , data : & str ) -> * mut u_char {
15
74
let ptr: * mut u_char = unsafe { ngx_palloc ( pool, data. len ( ) as _ ) as _ } ;
16
75
unsafe {
@@ -20,28 +79,54 @@ pub fn str_to_uchar(pool: *mut ngx_pool_t, data: &str) -> *mut u_char {
20
79
}
21
80
22
81
impl ngx_str_t {
23
- // convert nginx string to str slice
82
+ /// Convert the nginx string to a string slice (`&str`).
83
+ ///
84
+ /// # Safety
85
+ /// This function is marked as unsafe because it involves raw pointer manipulation.
86
+ /// It assumes that the underlying `data` pointer is valid and points to a valid UTF-8 encoded string.
87
+ ///
88
+ /// # Returns
89
+ /// A string slice (`&str`) representing the nginx string.
24
90
pub fn to_str ( & self ) -> & str {
25
91
unsafe {
26
92
let slice = slice:: from_raw_parts ( self . data , self . len as usize ) ;
27
93
return std:: str:: from_utf8 ( slice) . unwrap ( ) ;
28
94
}
29
95
}
30
96
31
- // get string
97
+ /// Convert the nginx string to a `String` by copying its contents.
98
+ ///
99
+ /// # Returns
100
+ /// A new `String` containing the contents of the nginx string.
32
101
pub fn to_string ( & self ) -> String {
33
102
return String :: from ( self . to_str ( ) ) ;
34
103
}
35
104
36
- /// create from string
105
+ /// Create an `ngx_str_t` instance from a `String`.
106
+ ///
107
+ /// # Arguments
108
+ ///
109
+ /// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
110
+ /// * `data` - The `String` from which to create the nginx string.
111
+ ///
112
+ /// # Returns
113
+ /// An `ngx_str_t` instance representing the given `String`.
37
114
pub fn from_string ( pool : * mut ngx_pool_t , data : String ) -> Self {
38
115
ngx_str_t {
39
116
data : str_to_uchar ( pool, data. as_str ( ) ) ,
40
117
len : data. len ( ) as _ ,
41
118
}
42
119
}
43
120
44
- /// create from string
121
+ /// Create an `ngx_str_t` instance from a string slice (`&str`).
122
+ ///
123
+ /// # Arguments
124
+ ///
125
+ /// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`).
126
+ /// * `data` - The string slice from which to create the nginx string.
127
+ ///
128
+ /// # Returns
129
+ /// An `ngx_str_t` instance representing the given string slice.
45
130
pub fn from_str ( pool : * mut ngx_pool_t , data : & str ) -> Self {
46
131
ngx_str_t {
47
132
data : str_to_uchar ( pool, data) ,
@@ -82,6 +167,29 @@ impl TryFrom<ngx_str_t> for &str {
82
167
}
83
168
}
84
169
170
+ /// Add a key-value pair to an nginx table entry (`ngx_table_elt_t`) in the given nginx memory pool.
171
+ ///
172
+ /// # Arguments
173
+ ///
174
+ /// * `table` - A pointer to the nginx table entry (`ngx_table_elt_t`) to modify.
175
+ /// * `pool` - A pointer to the nginx memory pool (`ngx_pool_t`) for memory allocation.
176
+ /// * `key` - The key string to add to the table entry.
177
+ /// * `value` - The value string to add to the table entry.
178
+ ///
179
+ /// # Safety
180
+ /// This function is marked as unsafe because it involves raw pointer manipulation and direct memory allocation using `str_to_uchar`.
181
+ ///
182
+ /// # Returns
183
+ /// An `Option<()>` representing the result of the operation. `Some(())` indicates success, while `None` indicates a null table pointer.
184
+ ///
185
+ /// # Example
186
+ /// ```rust
187
+ /// let table: *mut ngx_table_elt_t = ...; // Obtain a pointer to the nginx table entry
188
+ /// let pool: *mut ngx_pool_t = ...; // Obtain a pointer to the nginx memory pool
189
+ /// let key: &str = "key"; // The key to add
190
+ /// let value: &str = "value"; // The value to add
191
+ /// let result = add_to_ngx_table(table, pool, key, value);
192
+ /// ```
85
193
pub fn add_to_ngx_table ( table : * mut ngx_table_elt_t , pool : * mut ngx_pool_t , key : & str , value : & str ) -> Option < ( ) > {
86
194
if table. is_null ( ) {
87
195
return None ;
0 commit comments