Skip to content

Commit a1bff29

Browse files
committedMay 17, 2023
docs: prepare ngx to be published to crates.io
1 parent 1405454 commit a1bff29

File tree

10 files changed

+239
-18
lines changed

10 files changed

+239
-18
lines changed
 

‎Cargo.lock

+39-6
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎Cargo.toml

+8
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ name = "ngx"
99
version = "0.3.0-beta"
1010
edition = "2021"
1111
autoexamples = false
12+
categories = ["api-bindings", "network-programming"]
13+
description = "FFI bindings to NGINX"
14+
repository = "https://github.com/nginxinc/ngx-rust"
15+
homepage = "https://github.com/nginxinc/ngx-rust"
1216
license = "Apache-2.0"
17+
keywords = ["nginx", "module", "sys"]
1318

1419
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
1520

@@ -23,3 +28,6 @@ duct = "0.13.6"
2328
ureq = { version = "2.6.2", features = ["tls"] }
2429
flate2 = "1.0.25"
2530
tar = "0.4.38"
31+
32+
[badges]
33+
maintenance = { status = "experimental" }

‎README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ In short, this SDK allows writing NGINX modules using the Rust language.
1212

1313
## Build
1414

15-
NGINX modules can be build against a particular version of NGINX. The following environment variables can be used to
16-
specify particular version of NGINX or an NGINX dependency:
15+
NGINX modules can be built against a particular version of NGINX. The following environment variables can be used to specify a particular version of NGINX or an NGINX dependency:
1716

1817
* `ZLIB_VERSION` (default 1.2.13) -
1918
* `PCRE2_VERSION` (default 10.42)

‎examples/README.md

+55-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,65 @@
1+
- [Examples](#examples)
2+
- [CURL](#curl)
3+
- [AWSSIG](#awssig)
4+
- [HTTPORIGDST - NGINX Destination IP recovery module for HTTP](#httporigdst----nginx-destination-ip-recovery-module-for-http)
5+
- [Dependencies](#dependencies)
6+
- [Example Configuration](#example-configuration)
7+
- [HTTP](#http)
8+
- [Embedded Variables](#embedded-variables)
9+
- [Usage](#usage)
10+
- [Caveats](#caveats)
11+
12+
113
# Examples
14+
This crate provides a couple of example using [ngx](https://crates.io/crates/ngx) crate:
15+
16+
- [awssig.rs](./awssig.rs) - An example of NGINX dynamic module that can sign GET request using AWS Signature v4.
17+
- [curl](./curl.rs) - An example of the Access Phase NGINX dynamic module that blocks HTTP requests if `user-agent` header starts with `curl`.
18+
- [httporigdst](./httporigdst.rs) - A dynamic module recovers the original IP address and port number of the destination packet.
19+
20+
To build all these examples simply run:
21+
22+
```
23+
cargo build --package=examples --examples
24+
```
25+
26+
27+
## CURL
28+
29+
This module demonstrates how to create a minimal dynamic module with `http_request_handler`, that checks for User-Agent headers and returns status code 403 if UA starts with `curl`, if a module is disabled then uses `core::Status::NGX_DECLINED` to indicate the operation is rejected, for example, because it is disabled in the configuration (`curl off`). Additionally, it demonstrates how to write a defective parser.
30+
31+
An example of nginx configuration file that uses that module can be found at [curl.conf](./curl.conf).
232

3-
## NGINX Destination IP recovery module for HTTP
33+
How to build and run in a [Docker](../Dockerfile) container curl example:
34+
```
35+
# build all dynamic modules examples and specify NGINX version to use
36+
docker buildx build --build-arg NGX_VERSION=1.23.3 -t ngx-rust .
37+
38+
# start NGINX using curl.conf module example:
39+
docker run --rm -d -p 8000:8000 ngx-rust nginx -c examples/curl.conf
40+
41+
# test it - you should see 403 Forbidden
42+
curl http://127.0.0.1:8000 -v -H "user-agent: curl"
43+
44+
45+
# test it - you should see 404 Not Found
46+
curl http://127.0.0.1:8000 -v -H "user-agent: foo"
47+
```
448

5-
This dynamic module recovers original IP address and port number of the destination packet. It is useful, for example, with container sidecars where all outgoing traffic is redirected to a separate container with iptables before reaching the target.
49+
## AWSSIG
50+
51+
This module uses [NGX_HTTP_PRECONTENT_PHASE](https://nginx.org/en/docs/dev/development_guide.html#http_phases) and provides examples, of how to use external dependency and manipulate HTTP headers before sending client requests upstream.
52+
53+
An example of nginx configuration file that uses that module can be found at [awssig.conf](./awssig.conf).
54+
55+
## HTTPORIGDST - NGINX Destination IP recovery module for HTTP
56+
57+
This dynamic module recovers the original IP address and port number of the destination packet. It is useful, for example, with container sidecars where all outgoing traffic is redirected to a separate container with iptables before reaching the target.
658

759
This module can only be built with the "linux" feature enabled, and will only successfully build on a Linux OS.
860

961
### Dependencies
10-
11-
This modules uses the Rust crate libc and Linux **getsockopt** socket API.
62+
This module uses the Rust crate libc and Linux **getsockopt** socket API.
1263

1364
### Example Configuration
1465
#### HTTP

‎src/core/buffer.rs

+45
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,27 @@ use crate::ffi::*;
22

33
use std::slice;
44

5+
/// The `Buffer` trait provides methods for working with an nginx buffer (`ngx_buf_t`).
56
pub trait Buffer {
7+
/// Returns a raw pointer to the underlying `ngx_buf_t` of the buffer.
68
fn as_ngx_buf(&self) -> *const ngx_buf_t;
79

10+
/// Returns a mutable raw pointer to the underlying `ngx_buf_t` of the buffer.
811
fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t;
912

13+
/// Returns the buffer contents as a byte slice.
14+
///
15+
/// # Safety
16+
/// This function is marked as unsafe because it involves raw pointer manipulation.
1017
fn as_bytes(&self) -> &[u8] {
1118
let buf = self.as_ngx_buf();
1219
unsafe { slice::from_raw_parts((*buf).pos, self.len()) }
1320
}
1421

22+
/// Returns the length of the buffer contents.
23+
///
24+
/// # Safety
25+
/// This function is marked as unsafe because it involves raw pointer manipulation.
1526
fn len(&self) -> usize {
1627
let buf = self.as_ngx_buf();
1728
unsafe {
@@ -22,17 +33,28 @@ pub trait Buffer {
2233
}
2334
}
2435

36+
/// Returns `true` if the buffer is empty, i.e., it has zero length.
2537
fn is_empty(&self) -> bool {
2638
self.len() == 0
2739
}
2840

41+
/// Sets the `last_buf` flag of the buffer.
42+
///
43+
/// # Arguments
44+
///
45+
/// * `last` - A boolean indicating whether the buffer is the last buffer in a request.
2946
fn set_last_buf(&mut self, last: bool) {
3047
let buf = self.as_ngx_buf_mut();
3148
unsafe {
3249
(*buf).set_last_buf(if last { 1 } else { 0 });
3350
}
3451
}
3552

53+
/// Sets the `last_in_chain` flag of the buffer.
54+
///
55+
/// # Arguments
56+
///
57+
/// * `last` - A boolean indicating whether the buffer is the last buffer in a chain of buffers.
3658
fn set_last_in_chain(&mut self, last: bool) {
3759
let buf = self.as_ngx_buf_mut();
3860
unsafe {
@@ -41,52 +63,75 @@ pub trait Buffer {
4163
}
4264
}
4365

66+
/// The `MutableBuffer` trait extends the `Buffer` trait and provides methods for working with a mutable buffer.
4467
pub trait MutableBuffer: Buffer {
68+
/// Returns a mutable reference to the buffer contents as a byte slice.
69+
///
70+
/// # Safety
71+
/// This function is marked as unsafe because it involves raw pointer manipulation.
4572
fn as_bytes_mut(&mut self) -> &mut [u8] {
4673
let buf = self.as_ngx_buf_mut();
4774
unsafe { slice::from_raw_parts_mut((*buf).pos, self.len()) }
4875
}
4976
}
5077

78+
/// Wrapper struct for a temporary buffer, providing methods for working with an `ngx_buf_t`.
5179
pub struct TemporaryBuffer(*mut ngx_buf_t);
5280

5381
impl TemporaryBuffer {
82+
/// Creates a new `TemporaryBuffer` from an `ngx_buf_t` pointer.
83+
///
84+
/// # Panics
85+
/// Panics if the given buffer pointer is null.
5486
pub fn from_ngx_buf(buf: *mut ngx_buf_t) -> TemporaryBuffer {
5587
assert!(!buf.is_null());
5688
TemporaryBuffer(buf)
5789
}
5890
}
5991

6092
impl Buffer for TemporaryBuffer {
93+
/// Returns the underlying `ngx_buf_t` pointer as a raw pointer.
6194
fn as_ngx_buf(&self) -> *const ngx_buf_t {
6295
self.0
6396
}
6497

98+
/// Returns a mutable reference to the underlying `ngx_buf_t` pointer.
6599
fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t {
66100
self.0
67101
}
68102
}
69103

70104
impl MutableBuffer for TemporaryBuffer {
105+
/// Returns a mutable reference to the buffer contents as a byte slice.
106+
///
107+
/// # Safety
108+
/// This function is marked as unsafe because it involves raw pointer manipulation.
71109
fn as_bytes_mut(&mut self) -> &mut [u8] {
72110
unsafe { slice::from_raw_parts_mut((*self.0).pos, self.len()) }
73111
}
74112
}
75113

114+
/// Wrapper struct for a memory buffer, providing methods for working with an `ngx_buf_t`.
76115
pub struct MemoryBuffer(*mut ngx_buf_t);
77116

78117
impl MemoryBuffer {
118+
/// Creates a new `MemoryBuffer` from an `ngx_buf_t` pointer.
119+
///
120+
/// # Panics
121+
/// Panics if the given buffer pointer is null.
79122
pub fn from_ngx_buf(buf: *mut ngx_buf_t) -> MemoryBuffer {
80123
assert!(!buf.is_null());
81124
MemoryBuffer(buf)
82125
}
83126
}
84127

85128
impl Buffer for MemoryBuffer {
129+
/// Returns the underlying `ngx_buf_t` pointer as a raw pointer.
86130
fn as_ngx_buf(&self) -> *const ngx_buf_t {
87131
self.0
88132
}
89133

134+
/// Returns a mutable reference to the underlying `ngx_buf_t` pointer.
90135
fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t {
91136
self.0
92137
}

‎src/core/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ macro_rules! ngx_http_null_variable {
5050
///
5151
/// This is typically used to terminate an array of Stream variable types.
5252
///
53-
/// [`ngx_stream_variable_t`]: TODO: find appropriate link
53+
/// [`ngx_stream_variable_t`]: https://github.com/nginx/nginx/blob/1a8ef991d92d22eb8aded7f49595dd31a639e8a4/src/stream/ngx_stream_variables.h#L21
5454
#[macro_export]
5555
macro_rules! ngx_stream_null_variable {
5656
() => {

0 commit comments

Comments
 (0)
Failed to load comments.