From db051fa1a3f659bf3ec4565541ea5179a7032840 Mon Sep 17 00:00:00 2001 From: Matthew Yacobucci Date: Fri, 1 Sep 2023 15:32:28 -0600 Subject: [PATCH] WIP: updating naming conventions --- examples/awssig.rs | 2 +- examples/curl.rs | 2 +- examples/httporigdst.rs | 10 +++++----- examples/upstream.rs | 18 +++++++++--------- src/core/buffer.rs | 22 +++++++++++----------- src/core/pool.rs | 20 ++++++++++---------- src/http/conf.rs | 10 +++++----- src/http/module.rs | 6 +++--- src/http/request.rs | 26 +++++++++++++++----------- src/log.rs | 2 +- 10 files changed, 61 insertions(+), 57 deletions(-) diff --git a/examples/awssig.rs b/examples/awssig.rs index e5aa70d..ef801d4 100644 --- a/examples/awssig.rs +++ b/examples/awssig.rs @@ -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; diff --git a/examples/curl.rs b/examples/curl.rs index d0bbb43..735eedc 100644 --- a/examples/curl.rs +++ b/examples/curl.rs @@ -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; diff --git a/examples/httporigdst.rs b/examples/httporigdst.rs index 1e62e27..2590197 100644 --- a/examples/httporigdst.rs +++ b/examples/httporigdst.rs @@ -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; } @@ -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; } @@ -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"); @@ -228,7 +228,7 @@ http_variable_get!( Ok((ip, port)) => { // create context, // set context - let new_ctx = request.pool().allocate::(Default::default()); + let new_ctx = request.pool().allocate_mut_ptr::(Default::default()); if new_ctx.is_null() { return core::Status::NGX_ERROR; @@ -267,7 +267,7 @@ http_variable_get!( Ok((ip, port)) => { // create context, // set context - let new_ctx = request.pool().allocate::(Default::default()); + let new_ctx = request.pool().allocate_mut_ptr::(Default::default()); if new_ctx.is_null() { return core::Status::NGX_ERROR; diff --git a/examples/upstream.rs b/examples/upstream.rs index efcf0ec..7b49932 100644 --- a/examples/upstream.rs +++ b/examples/upstream.rs @@ -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, @@ -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::(); + let mut hcpd = request.pool().alloc_type_mut_ptr::(); 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; } @@ -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; } @@ -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; @@ -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, @@ -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 @@ -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::(); + let conf = pool.alloc_type_mut_ptr::(); if conf.is_null() { ngx_conf_log_error( NGX_LOG_EMERG as usize, diff --git a/src/core/buffer.rs b/src/core/buffer.rs index 0dd1c10..9d2f98e 100644 --- a/src/core/buffer.rs +++ b/src/core/buffer.rs @@ -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()) } } @@ -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; @@ -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 }); } @@ -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 }); } @@ -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()) } } } @@ -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 } } @@ -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 } } diff --git a/src/core/pool.rs b/src/core/pool.rs index 388a96a..0d07d87 100644 --- a/src/core/pool.rs +++ b/src/core/pool.rs @@ -36,7 +36,7 @@ impl Pool { pub fn create_buffer_from_str(&mut self, str: &str) -> Option { let mut buffer = self.create_buffer(str.len())?; unsafe { - let buf = buffer.as_ngx_buf_mut(); + let buf = buffer.as_ngx_buf_mut_ptr(); ptr::copy_nonoverlapping(str.as_ptr(), (*buf).pos, str.len()); (*buf).last = (*buf).pos.add(str.len()); } @@ -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 { - let buf = self.calloc_type::(); + let buf = self.calloc_type_mut_ptr::(); if buf.is_null() { return None; } @@ -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(&mut self) -> *mut T { - self.alloc(mem::size_of::()) as *mut T + pub fn alloc_type_mut_ptr(&mut self) -> *mut T { + self.alloc_mut_ptr(mem::size_of::()) 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(&mut self) -> *mut T { - self.calloc(mem::size_of::()) as *mut T + pub fn calloc_type_mut_ptr(&mut self) -> *mut T { + self.calloc_mut_ptr(mem::size_of::()) as *mut T } /// Allocates memory for a value of a specified type and adds a cleanup handler to the memory pool. @@ -118,9 +118,9 @@ impl Pool { /// /// # Safety /// This function is marked as unsafe because it involves raw pointer manipulation. - pub fn allocate(&mut self, value: T) -> *mut T { + pub fn allocate_mut_ptr(&mut self, value: T) -> *mut T { unsafe { - let p = self.alloc(mem::size_of::()) as *mut T; + let p = self.alloc_mut_ptr(mem::size_of::()) as *mut T; ptr::write(p, value); if self.add_cleanup_for_value(p).is_err() { ptr::drop_in_place(p); diff --git a/src/http/conf.rs b/src/http/conf.rs index a135e9f..de57718 100644 --- a/src/http/conf.rs +++ b/src/http/conf.rs @@ -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 { @@ -16,7 +16,7 @@ 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) } @@ -24,7 +24,7 @@ pub unsafe fn ngx_http_conf_get_module_srv_conf(cf: *mut ngx_conf_t, module: &ng /// # 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 { @@ -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( +pub unsafe fn ngx_http_conf_upstream_srv_conf_ptr( us: *const ngx_http_upstream_srv_conf_t, module: &ngx_module_t, ) -> Option<*const T> { @@ -52,7 +52,7 @@ pub unsafe fn ngx_http_conf_upstream_srv_conf_immutable( /// 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( +pub unsafe fn ngx_http_conf_upstream_srv_conf_mut_ptr( us: *const ngx_http_upstream_srv_conf_t, module: &ngx_module_t, ) -> Option<*mut T> { diff --git a/src/http/module.rs b/src/http/module.rs index 96f4a81..1d25d78 100644 --- a/src/http/module.rs +++ b/src/http/module.rs @@ -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::(Default::default()) as *mut c_void + pool.allocate_mut_ptr::(Default::default()) as *mut c_void } /// # Safety @@ -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::(Default::default()) as *mut c_void + pool.allocate_mut_ptr::(Default::default()) as *mut c_void } /// # Safety @@ -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::(Default::default()) as *mut c_void + pool.allocate_mut_ptr::(Default::default()) as *mut c_void } /// # Safety diff --git a/src/http/request.rs b/src/http/request.rs index f4efab8..734e237 100644 --- a/src/http/request.rs +++ b/src/http/request.rs @@ -123,7 +123,7 @@ impl Request { /// [`ngx_http_upstream_t`]: is best described in /// https://nginx.org/en/docs/dev/development_guide.html#http_request /// https://nginx.org/en/docs/dev/development_guide.html#http_load_balancing - pub fn upstream(&self) -> Option<*mut ngx_http_upstream_t> { + pub fn upstream_mut_ptr(&self) -> Option<*mut ngx_http_upstream_t> { if self.0.upstream.is_null() { return None; } @@ -133,25 +133,25 @@ impl Request { /// Pointer to a [`ngx_connection_t`] client connection object. /// /// [`ngx_connection_t`]: https://nginx.org/en/docs/dev/development_guide.html#connection - pub fn connection(&self) -> *mut ngx_connection_t { + pub fn connection_mut_ptr(&self) -> *mut ngx_connection_t { self.0.connection } /// Pointer to a [`ngx_log_t`]. /// /// [`ngx_log_t`]: https://nginx.org/en/docs/dev/development_guide.html#logging - pub fn log(&self) -> *mut ngx_log_t { - unsafe { (*self.connection()).log } + pub fn log_mut_ptr(&self) -> *mut ngx_log_t { + unsafe { (*self.connection_mut_ptr()).log } } /// Module location configuration. - fn get_module_loc_conf_ptr(&self, module: &ngx_module_t) -> *mut c_void { + fn get_module_loc_conf_mut_ptr(&self, module: &ngx_module_t) -> *mut c_void { unsafe { *self.0.loc_conf.add(module.ctx_index) } } /// Module location configuration. pub fn get_module_loc_conf(&self, module: &ngx_module_t) -> Option<&T> { - let lc_prt = self.get_module_loc_conf_ptr(module) as *mut T; + let lc_prt = self.get_module_loc_conf_mut_ptr(module) as *mut T; if lc_prt.is_null() { return None; } @@ -160,13 +160,13 @@ impl Request { } /// Get Module context pointer - fn get_module_ctx_ptr(&self, module: &ngx_module_t) -> *mut c_void { + fn get_module_ctx_mut_ptr(&self, module: &ngx_module_t) -> *mut c_void { unsafe { *self.0.ctx.add(module.ctx_index) } } /// Get Module context pub fn get_module_ctx(&self, module: &ngx_module_t) -> Option<&T> { - let cf = self.get_module_ctx_ptr(module) as *mut T; + let cf = self.get_module_ctx_mut_ptr(module) as *mut T; if cf.is_null() { return None; @@ -318,13 +318,15 @@ impl Request { let uri_ptr = unsafe { &mut ngx_str_t::from_str(self.0.pool, uri) as *mut _ }; // ------------- // allocate memory and set values for ngx_http_post_subrequest_t - let sub_ptr = self.pool().alloc(std::mem::size_of::()); + let sub_ptr = self + .pool() + .alloc_mut_ptr(std::mem::size_of::()); // assert!(sub_ptr.is_null()); let post_subreq = sub_ptr as *const ngx_http_post_subrequest_t as *mut ngx_http_post_subrequest_t; unsafe { (*post_subreq).handler = Some(post_callback); - (*post_subreq).data = self.get_module_ctx_ptr(module); // WARN: safety! ensure that ctx is already set + (*post_subreq).data = self.get_module_ctx_mut_ptr(module); // WARN: safety! ensure that ctx is already set } // ------------- @@ -347,7 +349,9 @@ impl Request { * allocate fake request body to avoid attempts to read it and to make * sure real body file (if already read) won't be closed by upstream */ - sr.request_body = self.pool().alloc(std::mem::size_of::()) as *mut _; + sr.request_body = self + .pool() + .alloc_mut_ptr(std::mem::size_of::()) as *mut _; if sr.request_body.is_null() { return Status::NGX_ERROR; diff --git a/src/log.rs b/src/log.rs index 0311446..68ec0de 100644 --- a/src/log.rs +++ b/src/log.rs @@ -33,7 +33,7 @@ macro_rules! ngx_log_debug { #[macro_export] macro_rules! ngx_log_debug_http { ( $request:expr, $($arg:tt)* ) => { - let log = unsafe { (*$request.connection()).log }; + let log = unsafe { (*$request.connection_mut_ptr()).log }; $crate::ngx_log_debug!(log, $($arg)*); } }