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.42 for NGINX 1.22.0 and later, or 8.45 for earlier) - PCRE1 or PCRE2 version
13//! * `OPENSSL_VERSION` (default 3.2.4 for NGINX 1.22.0 and later, or 1.1.1w for earlier) - OpenSSL version
14//! * `NGX_VERSION` (default 1.26.3) - 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
96/// The core module.
97///
98/// This module provides fundamental utilities needed to interface with many NGINX primitives.
99/// String conversions, the pool (memory interface) object, and buffer APIs are covered here. These
100/// utilities will generally align with the NGINX 'core' files and APIs.
101pub mod core;
102
103/// The ffi module.
104///
105/// This module provides scoped FFI bindings for NGINX symbols.
106pub mod ffi;
107
108/// The http module.
109///
110/// This modules provides wrappers and utilities to NGINX http APIs, such as requests,
111/// configuration access, and statuses.
112pub mod http;
113
114/// The log module.
115///
116/// This module provides an interface into the NGINX logger framework.
117pub mod log;
118
119/// Define modules exported by this library.
120///
121/// These are normally generated by the Nginx module system, but need to be
122/// defined when building modules outside of it.
123#[macro_export]
124macro_rules! ngx_modules {
125    ($( $mod:ident ),+) => {
126        #[no_mangle]
127        #[allow(non_upper_case_globals)]
128        pub static mut ngx_modules: [*const $crate::ffi::ngx_module_t; $crate::count!($( $mod, )+) + 1] = [
129            $( unsafe { &$mod } as *const $crate::ffi::ngx_module_t, )+
130            ::core::ptr::null()
131        ];
132
133        #[no_mangle]
134        #[allow(non_upper_case_globals)]
135        pub static mut ngx_module_names: [*const ::core::ffi::c_char; $crate::count!($( $mod, )+) + 1] = [
136            $( concat!(stringify!($mod), "\0").as_ptr() as *const ::core::ffi::c_char, )+
137            ::core::ptr::null()
138        ];
139
140        #[no_mangle]
141        #[allow(non_upper_case_globals)]
142        pub static mut ngx_module_order: [*const ::core::ffi::c_char; 1] = [
143            ::core::ptr::null()
144        ];
145    };
146}
147
148/// Count number of arguments
149#[macro_export]
150macro_rules! count {
151    () => { 0usize };
152    ($x:tt, $( $xs:tt ),*) => { 1usize + $crate::count!($( $xs, )*) };
153}