Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ homepage = "https://github.com/nginx/ngx-rust"
repository = "https://github.com/nginx/ngx-rust"
rust-version = "1.81.0"

[workspace.lints.rust]
rust-2024-compatibility = "warn"
edition-2024-expr-fragment-specifier = "allow"

[package]
name = "ngx"
version = "0.5.0-beta"
Expand Down Expand Up @@ -42,6 +46,7 @@ async-task = { version = "4.7.1", optional = true }
lock_api = "0.4.13"
nginx-sys = { path = "nginx-sys", version = "0.5.0-beta"}
pin-project-lite = { version = "0.2.16", optional = true }
futures-channel = { version = "0.3.31", optional = true }

[features]
default = ["std"]
Expand All @@ -50,6 +55,7 @@ async = [
"alloc",
"dep:async-task",
"dep:pin-project-lite",
"dep:futures-channel",
]
# Provides APIs that require allocations via the `alloc` crate.
alloc = ["allocator-api2/alloc"]
Expand All @@ -70,3 +76,6 @@ maintenance = { status = "experimental" }

[dev-dependencies]
tempfile = { version = "3.20.0", default-features = false }

[lints]
workspace = true
3 changes: 3 additions & 0 deletions examples/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,6 @@ default = ["export-modules", "ngx/vendored"]
# See https://github.com/rust-lang/rust/issues/20267
export-modules = []
linux = []

[lints]
workspace = true
34 changes: 18 additions & 16 deletions examples/async.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,21 @@ impl http::HttpModule for Module {
}

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
// SAFETY: this function is called with non-NULL cf always
let cf = &mut *cf;
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");

let h = ngx_array_push(
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
unsafe {
// SAFETY: this function is called with non-NULL cf always
let cf = &mut *cf;
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");

let h = ngx_array_push(
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
}
// set an Access phase handler
*h = Some(async_access_handler);
core::Status::NGX_OK.into()
}
// set an Access phase handler
*h = Some(async_access_handler);
core::Status::NGX_OK.into()
}
}

Expand Down Expand Up @@ -98,16 +100,16 @@ impl http::Merge for ModuleConfig {

unsafe extern "C" fn check_async_work_done(event: *mut ngx_event_t) {
let ctx = ngx::ngx_container_of!(event, RequestCTX, event);
let c: *mut ngx_connection_t = (*event).data.cast();
let c: *mut ngx_connection_t = unsafe { (*event).data.cast() };

if (*ctx).done.load(Ordering::Relaxed) {
if unsafe { (*ctx).done.load(Ordering::Relaxed) } {
// Triggering async_access_handler again
ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events));
unsafe { ngx_post_event((*c).write, addr_of_mut!(ngx_posted_events)) };
} else {
// this doesn't have have good performance but works as a simple thread-safe example and
// doesn't causes segfault. The best method that provides both thread-safety and
// performance requires an nginx patch.
ngx_post_event(event, addr_of_mut!(ngx_posted_next_events));
unsafe { ngx_post_event(event, addr_of_mut!(ngx_posted_next_events)) };
}
}

Expand Down
42 changes: 26 additions & 16 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// Allow following allow to work in MSRV 1.81
#![allow(unknown_lints)]
// Suppress spurrions lint for 2024 compatibility
#![allow(tail_expr_drop_order)]

use std::ffi::{c_char, c_void};

use http::HeaderMap;
Expand All @@ -19,19 +24,21 @@ impl HttpModule for Module {
}

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
// SAFETY: this function is called with non-NULL cf always
let cf = &mut *cf;
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");

let h = ngx_array_push(
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers,
) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
unsafe {
// SAFETY: this function is called with non-NULL cf always
let cf = &mut *cf;
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");

let h = ngx_array_push(
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers,
) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
}
// set an phase handler
*h = Some(awssigv4_header_handler);
core::Status::NGX_OK.into()
}
// set an phase handler
*h = Some(awssigv4_header_handler);
core::Status::NGX_OK.into()
}
}

Expand Down Expand Up @@ -298,10 +305,13 @@ http_request_handler!(awssigv4_header_handler, |request: &mut Request| {
for (name, value) in request.headers_in_iterator() {
if let Ok(name) = name.to_str() {
if name.to_lowercase() == "host" {
if let Ok(value) = http::HeaderValue::from_bytes(value.as_bytes()) {
headers.insert(http::header::HOST, value);
} else {
return core::Status::NGX_DECLINED;
match http::HeaderValue::from_bytes(value.as_bytes()) {
Ok(value) => {
headers.insert(http::header::HOST, value);
}
_ => {
return core::Status::NGX_DECLINED;
}
}
}
} else {
Expand Down
26 changes: 14 additions & 12 deletions examples/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,21 @@ impl http::HttpModule for Module {
}

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
// SAFETY: this function is called with non-NULL cf always
let cf = &mut *cf;
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");

let h = ngx_array_push(
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
unsafe {
// SAFETY: this function is called with non-NULL cf always
let cf = &mut *cf;
let cmcf = NgxHttpCoreModule::main_conf_mut(cf).expect("http core main conf");

let h = ngx_array_push(
&mut cmcf.phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers,
) as *mut ngx_http_handler_pt;
if h.is_null() {
return core::Status::NGX_ERROR.into();
}
// set an Access phase handler
*h = Some(curl_access_handler);
core::Status::NGX_OK.into()
}
// set an Access phase handler
*h = Some(curl_access_handler);
core::Status::NGX_OK.into()
}
}

Expand Down
Loading
Loading