#![doc = include_str!("../README.md")]
#![warn(missing_docs)]
#![no_std]
pub mod detail;
mod event;
#[cfg(ngx_feature = "http")]
mod http;
mod queue;
mod rbtree;
#[cfg(ngx_feature = "stream")]
mod stream;
mod string;
use core::ptr;
#[doc(hidden)]
mod bindings {
#![allow(unknown_lints)] #![allow(missing_docs)]
#![allow(non_upper_case_globals)]
#![allow(non_camel_case_types)]
#![allow(non_snake_case)]
#![allow(dead_code)]
#![allow(clippy::all)]
#![allow(improper_ctypes)]
#![allow(rustdoc::broken_intra_doc_links)]
#![allow(unnecessary_transmutes)]
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
}
#[doc(no_inline)]
pub use bindings::*;
pub use event::*;
#[cfg(ngx_feature = "http")]
pub use http::*;
pub use queue::*;
pub use rbtree::*;
#[cfg(ngx_feature = "stream")]
pub use stream::*;
pub const NGX_ALIGNMENT: usize = NGX_RS_ALIGNMENT;
const _: () = assert!(core::mem::align_of::<ngx_str_t>() <= NGX_ALIGNMENT);
impl ngx_array_t {
pub unsafe fn as_slice<T>(&self) -> &[T] {
debug_assert_eq!(
core::mem::size_of::<T>(),
self.size,
"ngx_array_t::as_slice(): element size mismatch"
);
if self.nelts == 0 {
&[]
} else {
core::slice::from_raw_parts(self.elts.cast(), self.nelts)
}
}
pub unsafe fn as_slice_mut<T>(&mut self) -> &mut [T] {
debug_assert_eq!(
core::mem::size_of::<T>(),
self.size,
"ngx_array_t::as_slice_mut(): element size mismatch"
);
if self.nelts == 0 {
&mut []
} else {
core::slice::from_raw_parts_mut(self.elts.cast(), self.nelts)
}
}
}
impl ngx_command_t {
pub const fn empty() -> Self {
Self {
name: ngx_str_t::empty(),
type_: 0,
set: None,
conf: 0,
offset: 0,
post: ptr::null_mut(),
}
}
}
impl ngx_module_t {
pub const fn default() -> Self {
Self {
ctx_index: ngx_uint_t::MAX,
index: ngx_uint_t::MAX,
name: ptr::null_mut(),
spare0: 0,
spare1: 0,
version: nginx_version as ngx_uint_t,
signature: NGX_RS_MODULE_SIGNATURE.as_ptr(),
ctx: ptr::null_mut(),
commands: ptr::null_mut(),
type_: 0,
init_master: None,
init_module: None,
init_process: None,
init_thread: None,
exit_thread: None,
exit_process: None,
exit_master: None,
spare_hook0: 0,
spare_hook1: 0,
spare_hook2: 0,
spare_hook3: 0,
spare_hook4: 0,
spare_hook5: 0,
spare_hook6: 0,
spare_hook7: 0,
}
}
}
impl ngx_variable_value_t {
pub fn as_bytes(&self) -> &[u8] {
match self.len() {
0 => &[],
len => unsafe { core::slice::from_raw_parts(self.data, len as usize) },
}
}
}
impl AsRef<[u8]> for ngx_variable_value_t {
fn as_ref(&self) -> &[u8] {
self.as_bytes()
}
}
#[inline]
pub fn ngx_errno() -> ngx_err_t {
#[cfg(windows)]
let err = unsafe { GetLastError() };
#[cfg(not(windows))]
let err = errno::errno().0;
err as ngx_err_t
}
#[inline]
pub fn ngx_set_errno(err: ngx_err_t) {
#[cfg(windows)]
unsafe {
SetLastError(err as _)
}
#[cfg(not(windows))]
errno::set_errno(errno::Errno(err as _))
}
#[inline]
pub fn ngx_socket_errno() -> ngx_err_t {
#[cfg(windows)]
let err = unsafe { WSAGetLastError() };
#[cfg(not(windows))]
let err = errno::errno().0;
err as ngx_err_t
}
#[inline]
pub fn ngx_set_socket_errno(err: ngx_err_t) {
#[cfg(windows)]
unsafe {
WSASetLastError(err as _)
}
#[cfg(not(windows))]
errno::set_errno(errno::Errno(err as _))
}
#[inline]
pub fn ngx_random() -> core::ffi::c_long {
#[cfg(windows)]
unsafe {
let x: u32 = ((rand() as u32) << 16) ^ ((rand() as u32) << 8) ^ (rand() as u32);
(0x7fffffff & x) as _
}
#[cfg(not(windows))]
unsafe {
random()
}
}
#[inline]
pub fn ngx_sched_yield() {
#[cfg(windows)]
unsafe {
SwitchToThread()
};
#[cfg(all(not(windows), ngx_feature = "have_sched_yield"))]
unsafe {
sched_yield()
};
#[cfg(not(any(windows, ngx_feature = "have_sched_yield")))]
unsafe {
usleep(1)
}
}
#[inline]
pub fn ngx_time() -> time_t {
unsafe { (*ngx_cached_time).sec }
}
#[inline]
pub fn ngx_timeofday() -> &'static ngx_time_t {
unsafe { &*ngx_cached_time }
}
#[inline]
pub unsafe fn ngx_list_init(
list: *mut ngx_list_t,
pool: *mut ngx_pool_t,
n: ngx_uint_t,
size: usize,
) -> ngx_int_t {
unsafe {
(*list).part.elts = ngx_palloc(pool, n * size);
if (*list).part.elts.is_null() {
return NGX_ERROR as ngx_int_t;
}
(*list).part.nelts = 0;
(*list).part.next = ptr::null_mut();
(*list).last = ptr::addr_of_mut!((*list).part);
(*list).size = size;
(*list).nalloc = n;
(*list).pool = pool;
NGX_OK as ngx_int_t
}
}
pub unsafe fn add_to_ngx_table(
table: *mut ngx_table_elt_t,
pool: *mut ngx_pool_t,
key: impl AsRef<[u8]>,
value: impl AsRef<[u8]>,
) -> Option<()> {
if let Some(table) = table.as_mut() {
let key = key.as_ref();
table.key = ngx_str_t::from_bytes(pool, key)?;
table.value = ngx_str_t::from_bytes(pool, value.as_ref())?;
table.lowcase_key = ngx_pnalloc(pool, table.key.len).cast();
if table.lowcase_key.is_null() {
return None;
}
table.hash = ngx_hash_strlow(table.lowcase_key, table.key.data, table.key.len);
return Some(());
}
None
}