Skip to content
Merged
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
15 changes: 8 additions & 7 deletions examples/async.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
use std::ffi::{c_char, c_void};
use std::ptr::{addr_of, addr_of_mut};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Instant;

use ngx::core;
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_cycle, ngx_event_t, ngx_http_core_module,
ngx_http_core_run_phases, ngx_http_handler_pt, ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE,
ngx_http_request_t, ngx_int_t, ngx_module_t, ngx_posted_events, ngx_queue_s, ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1,
NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
};
use ngx::http::MergeConfigError;
use ngx::{core, core::Status, http, http::HTTPModule};
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
use std::os::raw::{c_char, c_void};
use std::ptr::{addr_of, addr_of_mut};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use std::time::Instant;
use tokio::runtime::Runtime;

struct Module;
Expand Down
14 changes: 8 additions & 6 deletions examples/awssig.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
use std::ffi::{c_char, c_void};
use std::ptr::addr_of;

use http::HeaderMap;
use ngx::core;
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt,
ngx_http_module_t, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_http_request_t, ngx_int_t, ngx_module_t,
ngx_str_t, ngx_uint_t, NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF,
NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
ngx_http_module_t, ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_HTTP_SRV_CONF, NGX_RS_HTTP_LOC_CONF_OFFSET,
NGX_RS_MODULE_SIGNATURE,
};
use ngx::{core, core::Status, http::*};
use ngx::http::*;
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
use std::os::raw::{c_char, c_void};
use std::ptr::addr_of;

struct Module;

Expand Down
14 changes: 7 additions & 7 deletions examples/curl.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::ffi::{c_char, c_void};
use std::ptr::addr_of;

use ngx::core;
use ngx::ffi::{
nginx_version, ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_core_module, ngx_http_handler_pt,
ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_http_request_t, ngx_int_t, ngx_module_t, ngx_str_t,
ngx_uint_t, NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET,
NGX_RS_MODULE_SIGNATURE,
ngx_http_module_t, ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
NGX_CONF_TAKE1, NGX_HTTP_LOC_CONF, NGX_HTTP_MODULE, NGX_RS_HTTP_LOC_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
};
use ngx::http::MergeConfigError;
use ngx::{core, core::Status, http, http::HTTPModule};
use ngx::http::{self, HTTPModule, MergeConfigError};
use ngx::{http_request_handler, ngx_log_debug_http, ngx_null_command, ngx_string};
use std::os::raw::{c_char, c_void};
use std::ptr::addr_of;

struct Module;

Expand Down
13 changes: 7 additions & 6 deletions examples/httporigdst.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
use std::ffi::{c_char, c_int, c_void};
use std::ptr::addr_of;

use ngx::core;
use ngx::ffi::{
in_port_t, nginx_version, ngx_conf_t, ngx_connection_local_sockaddr, ngx_http_add_variable, ngx_http_module_t,
ngx_http_request_t, ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t,
ngx_uint_t, ngx_variable_value_t, sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE,
NGX_RS_MODULE_SIGNATURE,
ngx_http_variable_t, ngx_inet_get_port, ngx_int_t, ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_uint_t,
ngx_variable_value_t, sockaddr, sockaddr_storage, INET_ADDRSTRLEN, NGX_HTTP_MODULE, NGX_RS_MODULE_SIGNATURE,
};
use ngx::{core, core::Status, http, http::HTTPModule};
use ngx::http::{self, HTTPModule};
use ngx::{http_variable_get, ngx_http_null_variable, ngx_log_debug_http, ngx_null_string, ngx_string};
use std::os::raw::{c_char, c_int, c_void};
use std::ptr::addr_of;

const IPV4_STRLEN: usize = INET_ADDRSTRLEN as usize;

Expand Down
40 changes: 18 additions & 22 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,26 @@
* The NGINX authors are grateful to @gabihodoroaga for their contributions
* to the community at large.
*/
use ngx::{
core::{Pool, Status},
ffi::{
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_log_error, ngx_conf_t, ngx_connection_t,
ngx_event_free_peer_pt, ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_request_t,
ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt, ngx_http_upstream_init_round_robin,
ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t, ngx_int_t, ngx_module_t,
ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1, NGX_CONF_UNSET, NGX_ERROR,
NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET, NGX_RS_MODULE_SIGNATURE,
},
http::{
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
ngx_http_conf_upstream_srv_conf_mutable, HTTPModule, Merge, MergeConfigError, Request,
},
http_upstream_init_peer_pt,
log::DebugMask,
ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command, ngx_string,
use std::ffi::{c_char, c_void};
use std::mem;
use std::ptr::addr_of;
use std::slice;

use ngx::core::{Pool, Status};
use ngx::ffi::{
nginx_version, ngx_atoi, ngx_command_t, ngx_conf_log_error, ngx_conf_t, ngx_connection_t, ngx_event_free_peer_pt,
ngx_event_get_peer_pt, ngx_http_module_t, ngx_http_upstream_init_peer_pt, ngx_http_upstream_init_pt,
ngx_http_upstream_init_round_robin, ngx_http_upstream_module, ngx_http_upstream_srv_conf_t, ngx_http_upstream_t,
ngx_int_t, ngx_module_t, ngx_peer_connection_t, ngx_str_t, ngx_uint_t, NGX_CONF_NOARGS, NGX_CONF_TAKE1,
NGX_CONF_UNSET, NGX_ERROR, NGX_HTTP_MODULE, NGX_HTTP_UPS_CONF, NGX_LOG_EMERG, NGX_RS_HTTP_SRV_CONF_OFFSET,
NGX_RS_MODULE_SIGNATURE,
};
use std::{
mem,
os::raw::{c_char, c_void},
ptr::addr_of,
slice,
use ngx::http::{
ngx_http_conf_get_module_srv_conf, ngx_http_conf_upstream_srv_conf_immutable,
ngx_http_conf_upstream_srv_conf_mutable, HTTPModule, Merge, MergeConfigError, Request,
};
use ngx::log::DebugMask;
use ngx::{http_upstream_init_peer_pt, ngx_log_debug_http, ngx_log_debug_mask, ngx_null_command, ngx_string};

#[derive(Clone, Copy, Debug)]
#[repr(C)]
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# group_imports = "StdExternalCrate" # unstable
max_width = 120
4 changes: 2 additions & 2 deletions src/core/buffer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ffi::*;

use std::slice;

use crate::ffi::*;

/// The `Buffer` trait provides methods for working with an nginx buffer (`ngx_buf_t`).
pub trait Buffer {
/// Returns a raw pointer to the underlying `ngx_buf_t` of the buffer.
Expand Down
6 changes: 3 additions & 3 deletions src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub use string::*;
#[macro_export]
macro_rules! ngx_null_command {
() => {
ngx_command_t {
$crate::ffi::ngx_command_t {
name: $crate::ngx_null_string!(),
type_: 0,
set: None,
Expand All @@ -35,7 +35,7 @@ macro_rules! ngx_null_command {
#[macro_export]
macro_rules! ngx_http_null_variable {
() => {
ngx_http_variable_t {
$crate::ffi::ngx_http_variable_t {
name: $crate::ngx_null_string!(),
set_handler: None,
get_handler: None,
Expand All @@ -54,7 +54,7 @@ macro_rules! ngx_http_null_variable {
#[macro_export]
macro_rules! ngx_stream_null_variable {
() => {
ngx_stream_variable_t {
$crate::ffi::ngx_stream_variable_t {
name: $crate::ngx_null_string!(),
set_handler: None,
get_handler: None,
Expand Down
6 changes: 3 additions & 3 deletions src/core/pool.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::ffi::c_void;
use std::{mem, ptr};

use crate::core::buffer::{Buffer, MemoryBuffer, TemporaryBuffer};
use crate::ffi::*;

use std::os::raw::c_void;
use std::{mem, ptr};

/// Wrapper struct for an `ngx_pool_t` pointer, providing methods for working with memory pools.
pub struct Pool(*mut ngx_pool_t);

Expand Down
3 changes: 2 additions & 1 deletion src/core/status.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::ffi::*;
use std::fmt;

use crate::ffi::*;

/// Status
///
/// Rust native wrapper for NGINX status codes.
Expand Down
4 changes: 2 additions & 2 deletions src/core/string.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::ffi::*;

use std::borrow::Cow;
use std::slice;
use std::str::{self, Utf8Error};

use crate::ffi::*;

/// Static string initializer for [`ngx_str_t`].
///
/// The resulting byte string is always nul-terminated (just like a C string).
Expand Down
4 changes: 2 additions & 2 deletions src/http/conf.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::ffi::*;
use std::ffi::c_void;

use std::os::raw::c_void;
use crate::ffi::*;

/// # Safety
///
Expand Down
6 changes: 3 additions & 3 deletions src/http/module.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use std::ffi::{c_char, c_void};
use std::ptr;

use crate::core::NGX_CONF_ERROR;
use crate::core::*;
use crate::ffi::*;

use core::ptr;
use std::os::raw::{c_char, c_void};

/// MergeConfigError - configuration cannot be merged with levels above.
#[derive(Debug)]
pub enum MergeConfigError {
Expand Down
35 changes: 24 additions & 11 deletions src/http/request.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use std::error::Error;
use std::ffi::c_void;
use std::fmt;
use std::str::FromStr;

use crate::core::*;
use crate::ffi::*;
use crate::http::status::*;
use crate::ngx_null_string;
use std::fmt;
use std::os::raw::c_void;

use std::error::Error;
use std::str::FromStr;

/// Define a static request handler.
///
Expand All @@ -15,8 +15,9 @@ use std::str::FromStr;
macro_rules! http_request_handler {
( $name: ident, $handler: expr ) => {
#[no_mangle]
extern "C" fn $name(r: *mut ngx_http_request_t) -> ngx_int_t {
let status: Status = $handler(unsafe { &mut $crate::http::Request::from_ngx_http_request(r) });
extern "C" fn $name(r: *mut $crate::ffi::ngx_http_request_t) -> $crate::ffi::ngx_int_t {
let status: $crate::core::Status =
$handler(unsafe { &mut $crate::http::Request::from_ngx_http_request(r) });
status.0
}
};
Expand All @@ -29,7 +30,11 @@ macro_rules! http_request_handler {
macro_rules! http_subrequest_handler {
( $name: ident, $handler: expr ) => {
#[no_mangle]
unsafe extern "C" fn $name(r: *mut ngx_http_request_t, data: *mut c_void, rc: ngx_int_t) -> ngx_int_t {
unsafe extern "C" fn $name(
r: *mut $crate::ffi::ngx_http_request_t,
data: *mut ::std::ffi::c_void,
rc: $crate::ffi::ngx_int_t,
) -> $crate::ffi::ngx_int_t {
$handler(r, data, rc)
}
};
Expand All @@ -44,7 +49,11 @@ macro_rules! http_subrequest_handler {
macro_rules! http_variable_set {
( $name: ident, $handler: expr ) => {
#[no_mangle]
unsafe extern "C" fn $name(r: *mut ngx_http_request_t, v: *mut ngx_variable_value_t, data: usize) {
unsafe extern "C" fn $name(
r: *mut $crate::ffi::ngx_http_request_t,
v: *mut $crate::ffi::ngx_variable_value_t,
data: usize,
) {
$handler(
unsafe { &mut $crate::http::Request::from_ngx_http_request(r) },
v,
Expand All @@ -64,8 +73,12 @@ macro_rules! http_variable_set {
macro_rules! http_variable_get {
( $name: ident, $handler: expr ) => {
#[no_mangle]
unsafe extern "C" fn $name(r: *mut ngx_http_request_t, v: *mut ngx_variable_value_t, data: usize) -> ngx_int_t {
let status: Status = $handler(
unsafe extern "C" fn $name(
r: *mut $crate::ffi::ngx_http_request_t,
v: *mut $crate::ffi::ngx_variable_value_t,
data: usize,
) -> $crate::ffi::ngx_int_t {
let status: $crate::core::Status = $handler(
unsafe { &mut $crate::http::Request::from_ngx_http_request(r) },
v,
data,
Expand Down
5 changes: 3 additions & 2 deletions src/http/status.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use crate::core::Status;
use crate::ffi::*;
use std::error::Error;
use std::fmt;

use crate::core::Status;
use crate::ffi::*;

/// Represents an HTTP status code.
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HTTPStatus(pub ngx_uint_t);
Expand Down
10 changes: 8 additions & 2 deletions src/http/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,14 @@
macro_rules! http_upstream_init_peer_pt {
( $name: ident, $handler: expr ) => {
#[no_mangle]
extern "C" fn $name(r: *mut ngx_http_request_t, us: *mut ngx_http_upstream_srv_conf_t) -> ngx_int_t {
let status: Status = $handler(unsafe { &mut Request::from_ngx_http_request(r) }, us);
extern "C" fn $name(
r: *mut $crate::ffi::ngx_http_request_t,
us: *mut $crate::ffi::ngx_http_upstream_srv_conf_t,
) -> $crate::ffi::ngx_int_t {
let status: $crate::core::Status = $handler(
unsafe { &mut $crate::http::Request::from_ngx_http_request(r) },
us,
);
status.0
}
};
Expand Down
16 changes: 8 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,20 +64,20 @@ pub mod log;
macro_rules! ngx_modules {
($( $mod:ident ),+) => {
#[no_mangle]
pub static mut ngx_modules: [*const ngx_module_t; $crate::count!($( $mod, )+) + 1] = [
$( unsafe { &$mod } as *const ngx_module_t, )+
std::ptr::null()
pub static mut ngx_modules: [*const $crate::ffi::ngx_module_t; $crate::count!($( $mod, )+) + 1] = [
$( unsafe { &$mod } as *const $crate::ffi::ngx_module_t, )+
::std::ptr::null()
];

#[no_mangle]
pub static mut ngx_module_names: [*const c_char; $crate::count!($( $mod, )+) + 1] = [
$( concat!(stringify!($mod), "\0").as_ptr() as *const c_char, )+
std::ptr::null()
pub static mut ngx_module_names: [*const ::std::ffi::c_char; $crate::count!($( $mod, )+) + 1] = [
$( concat!(stringify!($mod), "\0").as_ptr() as *const ::std::ffi::c_char, )+
::std::ptr::null()
];

#[no_mangle]
pub static mut ngx_module_order: [*const c_char; 1] = [
std::ptr::null()
pub static mut ngx_module_order: [*const ::std::ffi::c_char; 1] = [
::std::ptr::null()
];
};
}
Expand Down
3 changes: 2 additions & 1 deletion tests/log_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,10 @@ impl Nginx {

#[cfg(test)]
mod tests {
use super::*;
use std::env;

use super::*;

const TEST_NGINX_CONFIG: &str = "tests/nginx.conf";

#[test]
Expand Down
Loading