ngx/
lib.rs

1//! Bindings to NGINX
2//!
3//! This project provides Rust SDK interfaces to the [NGINX](https://nginx.com) proxy allowing the creation of NGINX
4//! dynamic modules completely in Rust.
5//!
6//! ## Build
7//!
8//! NGINX modules can be built against a particular version of NGINX. The following environment variables can be used
9//! to specify a particular version of NGINX or an NGINX dependency:
10//!
11//! * `ZLIB_VERSION` (default 1.3.1) - zlib version
12//! * `PCRE2_VERSION` (default 10.45 for NGINX 1.22.0 and later, or 8.45 for earlier) - PCRE1 or PCRE2 version
13//! * `OPENSSL_VERSION` (default 3.5.0 for NGINX 1.22.0 and later, or 1.1.1w for earlier) - OpenSSL version
14//! * `NGX_VERSION` (default 1.28.0) - NGINX OSS version
15//! * `NGX_DEBUG` (default to false) -  if set to true, then will compile NGINX `--with-debug` option
16//!
17//! For example, this is how you would compile the [examples](https://github.com/nginx/ngx-rust/tree/master/examples) using a specific version of NGINX and enabling
18//! debugging:
19//! ```sh
20//! NGX_DEBUG=true NGX_VERSION=1.23.0 cargo build --package=examples --examples --release
21//! ```
22//!
23//! To build Linux-only modules, use the "linux" feature:
24//! ```sh
25//! cargo build --package=examples --examples --features=linux --release
26//! ```
27//!
28//! After compilation, the modules can be found in the path `target/release/examples/` ( with the `.so` file extension for
29//! Linux or `.dylib` for MacOS).
30//!
31//! Additionally, the folder  `.cache/nginx/{NGX_VERSION}/{TARGET}` (`{TARGET}` means rustc's target triple string)
32//! will contain the compiled version of NGINX used to build the SDK.
33//! You can start NGINX directly from this directory if you want to test the module or add it to `$PATH`
34//! ```not_rust
35//! $ export NGX_VERSION=1.23.3
36//! $ cargo build --package=examples --examples --features=linux --release
37//! $ export PATH=$PATH:$PWD/.cache/nginx/$NGX_VERSION/x86_64-unknown-linux-gnu/sbin
38//! $ nginx -V
39//! $ ls -la ./target/release/examples/
40//! # now you can use dynamic modules with the NGINX
41//! ```
42//!
43//! The following environment variables can be used to change locations of cache directory and NGINX directory:
44//!
45//! * `CACHE_DIR` (default `[nginx-sys's root directory]/.cache`) - the directory containing cache files, means PGP keys, tarballs, PGP signatures, and unpacked source codes. It also contains compiled NGINX in default configuration.
46//! * `NGINX_INSTALL_ROOT_DIR` (default `{CACHE_DIR}/nginx`) - the directory containing the series of compiled NGINX in its subdirectories
47//! * `NGINX_INSTALL_DIR` (default `{NGINX_INSTALL_BASE_DIR}/{NGX_VERSION}/{TARGET}`) - the directory containing the NGINX compiled in the build
48//!
49//! ### Mac OS dependencies
50//!
51//! In order to use the optional GNU make build process on MacOS, you will need to install additional tools. This can be
52//! done via [homebrew](https://brew.sh/) with the following command:
53//! ```sh
54//! brew install make openssl grep
55//! ```
56//!
57//! Additionally, you may need to set up LLVM and clang. Typically, this is done as follows:
58//!
59//! ```sh
60//! # make sure xcode tools are installed
61//! xcode-select --install
62//! # instal llvm
63//! brew install --with-toolchain llvm
64//! ```
65//!
66//! ### Linux dependencies
67//!
68//! See the [Dockerfile] for dependencies as an example of required packages on Debian Linux.
69//!
70//! [Dockerfile]: https://github.com/nginxinc/ngx-rust/blob/master/Dockerfile
71//!
72//! ### Build with external NGINX source tree
73//!
74//! If you require a customized NGINX configuration, you can build a module against an existing pre-configured source tree.
75//! To do that, you need to set the `NGX_OBJS` variable to an _absolute_ path of the NGINX build directory (`--builddir`, defaults to the `objs` in the source directory).
76//! Only the `./configure` step of the NGINX build is mandatory because bindings don't depend on any of the artifacts generated by `make`.
77//!
78//! ```sh
79//! NGINX_BUILD_DIR=$PWD/../nginx/objs cargo build --package=examples --examples
80//! ```
81//!
82//! Furthermore, this approach can be leveraged to build a module as a part of the NGINX build process by adding the `--add-module`/`--add-dynamic-module` options to the configure script.
83//! See the following example integration scripts: [`examples/config`] and [`examples/config.make`].
84//!
85//! [`examples/config`]: https://github.com/nginxinc/ngx-rust/blob/master/examples/config
86//! [`examples/config.make`]: https://github.com/nginxinc/ngx-rust/blob/master/examples/config.make
87
88#![warn(missing_docs)]
89// support both std and no_std
90#![no_std]
91#[cfg(all(not(feature = "std"), feature = "alloc"))]
92extern crate alloc;
93#[cfg(feature = "std")]
94extern crate std;
95
96pub mod allocator;
97#[cfg(feature = "async")]
98pub mod async_;
99pub mod collections;
100
101/// The core module.
102///
103/// This module provides fundamental utilities needed to interface with many NGINX primitives.
104/// String conversions, the pool (memory interface) object, and buffer APIs are covered here. These
105/// utilities will generally align with the NGINX 'core' files and APIs.
106pub mod core;
107
108/// The ffi module.
109///
110/// This module provides scoped FFI bindings for NGINX symbols.
111pub mod ffi;
112
113/// The http module.
114///
115/// This modules provides wrappers and utilities to NGINX http APIs, such as requests,
116/// configuration access, and statuses.
117pub mod http;
118
119/// The log module.
120///
121/// This module provides an interface into the NGINX logger framework.
122pub mod log;
123
124pub mod sync;
125
126/// Define modules exported by this library.
127///
128/// These are normally generated by the Nginx module system, but need to be
129/// defined when building modules outside of it.
130#[macro_export]
131macro_rules! ngx_modules {
132    ($( $mod:ident ),+) => {
133        #[no_mangle]
134        #[allow(non_upper_case_globals)]
135        pub static mut ngx_modules: [*const $crate::ffi::ngx_module_t; $crate::count!($( $mod, )+) + 1] = [
136            $( unsafe { &$mod } as *const $crate::ffi::ngx_module_t, )+
137            ::core::ptr::null()
138        ];
139
140        #[no_mangle]
141        #[allow(non_upper_case_globals)]
142        pub static mut ngx_module_names: [*const ::core::ffi::c_char; $crate::count!($( $mod, )+) + 1] = [
143            $( concat!(stringify!($mod), "\0").as_ptr() as *const ::core::ffi::c_char, )+
144            ::core::ptr::null()
145        ];
146
147        #[no_mangle]
148        #[allow(non_upper_case_globals)]
149        pub static mut ngx_module_order: [*const ::core::ffi::c_char; 1] = [
150            ::core::ptr::null()
151        ];
152    };
153}
154
155/// Count number of arguments
156#[macro_export]
157macro_rules! count {
158    () => { 0usize };
159    ($x:tt, $( $xs:tt ),*) => { 1usize + $crate::count!($( $xs, )*) };
160}