Skip to content

Commit

Permalink
WIP: updating naming conventions
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Yacobucci committed Sep 1, 2023
1 parent 840d789 commit 3b5b8c5
Show file tree
Hide file tree
Showing 10 changed files with 61 additions and 57 deletions.
2 changes: 1 addition & 1 deletion examples/awssig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl HTTPModule for Module {
type LocConf = ModuleConfig;

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module);
let cmcf = ngx_http_conf_get_module_main_conf_mut_ptr(cf, &ngx_http_core_module);

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_PRECONTENT_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
Expand Down
2 changes: 1 addition & 1 deletion examples/curl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ impl http::HTTPModule for Module {
type LocConf = ModuleConfig;

unsafe extern "C" fn postconfiguration(cf: *mut ngx_conf_t) -> ngx_int_t {
let cmcf = http::ngx_http_conf_get_module_main_conf(cf, &ngx_http_core_module);
let cmcf = http::ngx_http_conf_get_module_main_conf_mut_ptr(cf, &ngx_http_core_module);

let h = ngx_array_push(&mut (*cmcf).phases[ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize].handlers)
as *mut ngx_http_handler_pt;
Expand Down
10 changes: 5 additions & 5 deletions examples/httporigdst.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Default for NgxHttpOrigDstCtx {

impl NgxHttpOrigDstCtx {
pub fn save(&mut self, addr: &str, port: in_port_t, pool: &mut core::Pool) -> core::Status {
let addr_data = pool.alloc(IPV4_STRLEN);
let addr_data = pool.alloc_mut_ptr(IPV4_STRLEN);
if addr_data.is_null() {
return core::Status::NGX_ERROR;
}
Expand All @@ -36,7 +36,7 @@ impl NgxHttpOrigDstCtx {
self.orig_dst_addr.data = addr_data as *mut u8;

let port_str = port.to_string();
let port_data = pool.alloc(port_str.len());
let port_data = pool.alloc_mut_ptr(port_str.len());
if port_data.is_null() {
return core::Status::NGX_ERROR;
}
Expand Down Expand Up @@ -147,7 +147,7 @@ static mut ngx_http_orig_dst_vars: [ngx_http_variable_t; 3] = [
];

unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_port_t), core::Status> {
let c = request.connection();
let c = request.connection_mut_ptr();

if (*c).type_ != libc::SOCK_STREAM {
ngx_log_debug_http!(request, "httporigdst: connection is not type SOCK_STREAM");
Expand Down Expand Up @@ -228,7 +228,7 @@ http_variable_get!(
Ok((ip, port)) => {
// create context,
// set context
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
let new_ctx = request.pool().allocate_mut_ptr::<NgxHttpOrigDstCtx>(Default::default());

if new_ctx.is_null() {
return core::Status::NGX_ERROR;
Expand Down Expand Up @@ -267,7 +267,7 @@ http_variable_get!(
Ok((ip, port)) => {
// create context,
// set context
let new_ctx = request.pool().allocate::<NgxHttpOrigDstCtx>(Default::default());
let new_ctx = request.pool().allocate_mut_ptr::<NgxHttpOrigDstCtx>(Default::default());

if new_ctx.is_null() {
return core::Status::NGX_ERROR;
Expand Down
18 changes: 9 additions & 9 deletions examples/upstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use ngx::{
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,
ngx_http_conf_get_module_srv_conf_mut_ptr, ngx_http_conf_upstream_srv_conf_mut_ptr,
ngx_http_conf_upstream_srv_conf_ptr, HTTPModule, Merge, MergeConfigError, Request,
},
http_upstream_init_peer_pt,
log::DebugMask,
Expand Down Expand Up @@ -146,13 +146,13 @@ http_upstream_init_peer_pt!(
|request: &mut Request, us: *mut ngx_http_upstream_srv_conf_t| {
ngx_log_debug_http!(request, "CUSTOM UPSTREAM request peer init");

let mut hcpd = request.pool().alloc_type::<UpstreamPeerData>();
let mut hcpd = request.pool().alloc_type_mut_ptr::<UpstreamPeerData>();
if hcpd.is_null() {
return Status::NGX_ERROR;
}

let maybe_conf: Option<*const SrvConfig> =
unsafe { ngx_http_conf_upstream_srv_conf_immutable(us, &ngx_http_upstream_custom_module) };
unsafe { ngx_http_conf_upstream_srv_conf_ptr(us, &ngx_http_upstream_custom_module) };
if maybe_conf.is_none() {
return Status::NGX_ERROR;
}
Expand All @@ -163,7 +163,7 @@ http_upstream_init_peer_pt!(
return Status::NGX_ERROR;
}

let maybe_upstream = request.upstream();
let maybe_upstream = request.upstream_mut_ptr();
if maybe_upstream.is_none() {
return Status::NGX_ERROR;
}
Expand All @@ -173,7 +173,7 @@ http_upstream_init_peer_pt!(
(*hcpd).conf = Some(hccf);
(*hcpd).upstream = maybe_upstream;
(*hcpd).data = (*upstream_ptr).peer.data;
(*hcpd).client_connection = Some(request.connection());
(*hcpd).client_connection = Some(request.connection_mut_ptr());
(*hcpd).original_get_peer = (*upstream_ptr).peer.get;
(*hcpd).original_free_peer = (*upstream_ptr).peer.free;

Expand Down Expand Up @@ -244,7 +244,7 @@ unsafe extern "C" fn ngx_http_upstream_init_custom(
ngx_log_debug_mask!(DebugMask::Http, (*cf).log, "CUSTOM UPSTREAM peer init_upstream");

let maybe_conf: Option<*mut SrvConfig> =
ngx_http_conf_upstream_srv_conf_mutable(us, &ngx_http_upstream_custom_module);
ngx_http_conf_upstream_srv_conf_mut_ptr(us, &ngx_http_upstream_custom_module);
if maybe_conf.is_none() {
ngx_conf_log_error(
NGX_LOG_EMERG as usize,
Expand Down Expand Up @@ -309,7 +309,7 @@ unsafe extern "C" fn ngx_http_upstream_commands_set_custom(
}

let uscf: *mut ngx_http_upstream_srv_conf_t =
ngx_http_conf_get_module_srv_conf(cf, &ngx_http_upstream_module) as *mut ngx_http_upstream_srv_conf_t;
ngx_http_conf_get_module_srv_conf_mut_ptr(cf, &ngx_http_upstream_module) as *mut ngx_http_upstream_srv_conf_t;

ccf.original_init_upstream = if (*uscf).peer.init_upstream.is_some() {
(*uscf).peer.init_upstream
Expand All @@ -336,7 +336,7 @@ impl HTTPModule for Module {

unsafe extern "C" fn create_srv_conf(cf: *mut ngx_conf_t) -> *mut c_void {
let mut pool = Pool::from_ngx_pool((*cf).pool);
let conf = pool.alloc_type::<SrvConfig>();
let conf = pool.alloc_type_mut_ptr::<SrvConfig>();
if conf.is_null() {
ngx_conf_log_error(
NGX_LOG_EMERG as usize,
Expand Down
22 changes: 11 additions & 11 deletions src/core/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ use std::slice;
/// 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.
fn as_ngx_buf(&self) -> *const ngx_buf_t;
fn as_ngx_buf_ptr(&self) -> *const ngx_buf_t;

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

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

Expand All @@ -24,7 +24,7 @@ pub trait Buffer {
/// # Safety
/// This function is marked as unsafe because it involves raw pointer manipulation.
fn len(&self) -> usize {
let buf = self.as_ngx_buf();
let buf = self.as_ngx_buf_ptr();
unsafe {
let pos = (*buf).pos;
let last = (*buf).last;
Expand All @@ -44,7 +44,7 @@ pub trait Buffer {
///
/// * `last` - A boolean indicating whether the buffer is the last buffer in a request.
fn set_last_buf(&mut self, last: bool) {
let buf = self.as_ngx_buf_mut();
let buf = self.as_ngx_buf_mut_ptr();
unsafe {
(*buf).set_last_buf(if last { 1 } else { 0 });
}
Expand All @@ -56,7 +56,7 @@ pub trait Buffer {
///
/// * `last` - A boolean indicating whether the buffer is the last buffer in a chain of buffers.
fn set_last_in_chain(&mut self, last: bool) {
let buf = self.as_ngx_buf_mut();
let buf = self.as_ngx_buf_mut_ptr();
unsafe {
(*buf).set_last_in_chain(if last { 1 } else { 0 });
}
Expand All @@ -70,7 +70,7 @@ pub trait MutableBuffer: Buffer {
/// # Safety
/// This function is marked as unsafe because it involves raw pointer manipulation.
fn as_bytes_mut(&mut self) -> &mut [u8] {
let buf = self.as_ngx_buf_mut();
let buf = self.as_ngx_buf_mut_ptr();
unsafe { slice::from_raw_parts_mut((*buf).pos, self.len()) }
}
}
Expand All @@ -91,12 +91,12 @@ impl TemporaryBuffer {

impl Buffer for TemporaryBuffer {
/// Returns the underlying `ngx_buf_t` pointer as a raw pointer.
fn as_ngx_buf(&self) -> *const ngx_buf_t {
fn as_ngx_buf_ptr(&self) -> *const ngx_buf_t {
self.0
}

/// Returns a mutable reference to the underlying `ngx_buf_t` pointer.
fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t {
fn as_ngx_buf_mut_ptr(&mut self) -> *mut ngx_buf_t {
self.0
}
}
Expand Down Expand Up @@ -127,12 +127,12 @@ impl MemoryBuffer {

impl Buffer for MemoryBuffer {
/// Returns the underlying `ngx_buf_t` pointer as a raw pointer.
fn as_ngx_buf(&self) -> *const ngx_buf_t {
fn as_ngx_buf_ptr(&self) -> *const ngx_buf_t {
self.0
}

/// Returns a mutable reference to the underlying `ngx_buf_t` pointer.
fn as_ngx_buf_mut(&mut self) -> *mut ngx_buf_t {
fn as_ngx_buf_mut_ptr(&mut self) -> *mut ngx_buf_t {
self.0
}
}
20 changes: 10 additions & 10 deletions src/core/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ impl Pool {
pub fn create_buffer_from_str(&mut self, str: &str) -> Option<TemporaryBuffer> {
let mut buffer = self.create_buffer(str.len())?;
unsafe {
let mut buf = buffer.as_ngx_buf_mut();
let mut buf = buffer.as_ngx_buf_mut_ptr();
ptr::copy_nonoverlapping(str.as_ptr(), (*buf).pos, str.len());
(*buf).last = (*buf).pos.add(str.len());
}
Expand All @@ -47,7 +47,7 @@ impl Pool {
///
/// Returns `Some(MemoryBuffer)` if the buffer is successfully created, or `None` if allocation fails.
pub fn create_buffer_from_static_str(&mut self, str: &'static str) -> Option<MemoryBuffer> {
let buf = self.calloc_type::<ngx_buf_t>();
let buf = self.calloc_type_mut_ptr::<ngx_buf_t>();
if buf.is_null() {
return None;
}
Expand Down Expand Up @@ -87,29 +87,29 @@ impl Pool {
/// Allocates memory from the pool of the specified size.
///
/// Returns a raw pointer to the allocated memory.
pub fn alloc(&mut self, size: usize) -> *mut c_void {
pub fn alloc_mut_ptr(&mut self, size: usize) -> *mut c_void {
unsafe { ngx_palloc(self.0, size) }
}

/// Allocates memory for a type from the pool.
///
/// Returns a typed pointer to the allocated memory.
pub fn alloc_type<T: Copy>(&mut self) -> *mut T {
self.alloc(mem::size_of::<T>()) as *mut T
pub fn alloc_type_mut_ptr<T: Copy>(&mut self) -> *mut T {
self.alloc_mut_ptr(mem::size_of::<T>()) as *mut T
}

/// Allocates zeroed memory from the pool of the specified size.
///
/// Returns a raw pointer to the allocated memory.
pub fn calloc(&mut self, size: usize) -> *mut c_void {
pub fn calloc_mut_ptr(&mut self, size: usize) -> *mut c_void {
unsafe { ngx_pcalloc(self.0, size) }
}

/// Allocates zeroed memory for a type from the pool.
///
/// Returns a typed pointer to the allocated memory.
pub fn calloc_type<T: Copy>(&mut self) -> *mut T {
self.calloc(mem::size_of::<T>()) as *mut T
pub fn calloc_type_mut_ptr<T: Copy>(&mut self) -> *mut T {
self.calloc_mut_ptr(mem::size_of::<T>()) as *mut T
}

/// Allocates memory for a value of a specified type and adds a cleanup handler to the memory pool.
Expand All @@ -118,9 +118,9 @@ impl Pool {
///
/// # Safety
/// This function is marked as unsafe because it involves raw pointer manipulation.
pub fn allocate<T>(&mut self, value: T) -> *mut T {
pub fn allocate_mut_ptr<T>(&mut self, value: T) -> *mut T {
unsafe {
let p = self.alloc(mem::size_of::<T>()) as *mut T;
let p = self.alloc_mut_ptr(mem::size_of::<T>()) as *mut T;
ptr::write(p, value);
if self.add_cleanup_for_value(p).is_err() {
ptr::drop_in_place(p);
Expand Down
10 changes: 5 additions & 5 deletions src/http/conf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::os::raw::c_void;
/// # Safety
///
/// The caller has provided a valid `ngx_conf_t` that points to valid memory and is non-null.
pub unsafe fn ngx_http_conf_get_module_main_conf(
pub unsafe fn ngx_http_conf_get_module_main_conf_mut_ptr(
cf: *mut ngx_conf_t,
module: &ngx_module_t,
) -> *mut ngx_http_core_main_conf_t {
Expand All @@ -16,15 +16,15 @@ pub unsafe fn ngx_http_conf_get_module_main_conf(
/// # Safety
///
/// The caller has provided a valid `ngx_conf_t` that points to valid memory and is non-null.
pub unsafe fn ngx_http_conf_get_module_srv_conf(cf: *mut ngx_conf_t, module: &ngx_module_t) -> *mut c_void {
pub unsafe fn ngx_http_conf_get_module_srv_conf_mut_ptr(cf: *mut ngx_conf_t, module: &ngx_module_t) -> *mut c_void {
let http_conf_ctx = (*cf).ctx as *mut ngx_http_conf_ctx_t;
*(*http_conf_ctx).srv_conf.add(module.ctx_index)
}

/// # Safety
///
/// The caller has provided a valid `ngx_conf_t` that points to valid memory and is non-null.
pub unsafe fn ngx_http_conf_get_module_loc_conf(
pub unsafe fn ngx_http_conf_get_module_loc_conf_mut_ptr(
cf: *mut ngx_conf_t,
module: &ngx_module_t,
) -> *mut ngx_http_core_loc_conf_t {
Expand All @@ -37,7 +37,7 @@ pub unsafe fn ngx_http_conf_get_module_loc_conf(
/// The caller has provided a value `ngx_http_upstream_srv_conf_t. If the `us` argument is null, a
/// None Option is returned; however, if the `us` internal fields are invalid or the module index
/// is out of bounds failures may still occur.
pub unsafe fn ngx_http_conf_upstream_srv_conf_immutable<T>(
pub unsafe fn ngx_http_conf_upstream_srv_conf_ptr<T>(
us: *const ngx_http_upstream_srv_conf_t,
module: &ngx_module_t,
) -> Option<*const T> {
Expand All @@ -52,7 +52,7 @@ pub unsafe fn ngx_http_conf_upstream_srv_conf_immutable<T>(
/// The caller has provided a value `ngx_http_upstream_srv_conf_t. If the `us` argument is null, a
/// None Option is returned; however, if the `us` internal fields are invalid or the module index
/// is out of bounds failures may still occur.
pub unsafe fn ngx_http_conf_upstream_srv_conf_mutable<T>(
pub unsafe fn ngx_http_conf_upstream_srv_conf_mut_ptr<T>(
us: *const ngx_http_upstream_srv_conf_t,
module: &ngx_module_t,
) -> Option<*mut T> {
Expand Down
6 changes: 3 additions & 3 deletions src/http/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait HTTPModule {
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn create_main_conf(cf: *mut ngx_conf_t) -> *mut c_void {
let mut pool = Pool::from_ngx_pool((*cf).pool);
pool.allocate::<Self::MainConf>(Default::default()) as *mut c_void
pool.allocate_mut_ptr::<Self::MainConf>(Default::default()) as *mut c_void
}

/// # Safety
Expand All @@ -93,7 +93,7 @@ pub trait HTTPModule {
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn create_srv_conf(cf: *mut ngx_conf_t) -> *mut c_void {
let mut pool = Pool::from_ngx_pool((*cf).pool);
pool.allocate::<Self::SrvConf>(Default::default()) as *mut c_void
pool.allocate_mut_ptr::<Self::SrvConf>(Default::default()) as *mut c_void
}

/// # Safety
Expand All @@ -115,7 +115,7 @@ pub trait HTTPModule {
/// guard against null inputs or risk runtime errors.
unsafe extern "C" fn create_loc_conf(cf: *mut ngx_conf_t) -> *mut c_void {
let mut pool = Pool::from_ngx_pool((*cf).pool);
pool.allocate::<Self::LocConf>(Default::default()) as *mut c_void
pool.allocate_mut_ptr::<Self::LocConf>(Default::default()) as *mut c_void
}

/// # Safety
Expand Down
Loading

0 comments on commit 3b5b8c5

Please sign in to comment.