@@ -8,7 +8,7 @@ use ngx::ffi::{
88 ngx_http_add_variable, ngx_http_module_t, ngx_http_variable_t, ngx_inet_get_port, ngx_int_t,
99 ngx_module_t, ngx_sock_ntop, ngx_str_t, ngx_variable_value_t, sockaddr, sockaddr_storage,
1010} ;
11- use ngx:: http:: { self , HttpModule } ;
11+ use ngx:: http:: { self , HttpModule , RequestWithContext } ;
1212use ngx:: { http_variable_get, ngx_log_debug_http, ngx_string} ;
1313
1414const IPV4_STRLEN : usize = INET_ADDRSTRLEN as usize ;
@@ -196,93 +196,65 @@ unsafe fn ngx_get_origdst(request: &mut http::Request) -> Result<(String, in_por
196196
197197http_variable_get ! (
198198 ngx_http_orig_dst_addr_variable,
199- |request: & mut http:: Request , v: * mut ngx_variable_value_t, _: usize | {
200- let ctx = request. get_module_ctx:: <NgxHttpOrigDstCtx >( Module :: module( ) ) ;
201- if let Some ( obj) = ctx {
202- ngx_log_debug_http!( request, "httporigdst: found context and binding variable" , ) ;
203- unsafe { obj. bind_addr( v) } ;
204- return Status :: NGX_OK ;
205- }
206- // lazy initialization:
207- // get original dest information
208- // create context
209- // set context
210- // bind address
211- ngx_log_debug_http!( request, "httporigdst: context not found, getting address" ) ;
212- let r = unsafe { ngx_get_origdst( request) } ;
213- match r {
214- Err ( e) => {
215- return e;
199+ |mut request: & mut http:: Request , v: * mut ngx_variable_value_t, _: usize | {
200+ let rctx: & mut RequestWithContext <Module , NgxHttpOrigDstCtx >;
201+ ( rctx, request) = RequestWithContext :: from_request( request) ;
202+ match rctx. get( ) {
203+ Some ( ctx) => {
204+ ngx_log_debug_http!( request, "httporigdst: found context and binding variable" ) ;
205+ unsafe { ctx. bind_addr( v) } ;
206+ Status :: NGX_OK
216207 }
217- Ok ( ( ip, port) ) => {
218- // create context,
219- // set context
220- let new_ctx = request
221- . pool( )
222- . allocate:: <NgxHttpOrigDstCtx >( Default :: default ( ) ) ;
223-
224- if new_ctx. is_null( ) {
225- return Status :: NGX_ERROR ;
226- }
227-
228- ngx_log_debug_http!(
229- request,
230- "httporigdst: saving ip - {:?}, port - {}" ,
231- ip,
232- port,
233- ) ;
234- unsafe { ( * new_ctx) . save( & ip, port, & request. pool( ) ) } ;
235- unsafe { ( * new_ctx) . bind_addr( v) } ;
236- request. set_module_ctx( new_ctx as * mut c_void, Module :: module( ) ) ;
208+ None => {
209+ ngx_log_debug_http!( request, "httporigdst: context not found, getting address" ) ;
210+ unsafe { ngx_get_origdst( request) } . map_or_else(
211+ |e| e,
212+ |( ip, port) | {
213+ rctx. set( NgxHttpOrigDstCtx :: default ( ) )
214+ . map_or( Status :: NGX_ERROR , |ctx| {
215+ unsafe { ctx. bind_addr( v) } ;
216+ ngx_log_debug_http!(
217+ request,
218+ "httporigdst: saving ip - {ip}, port - {port}" ,
219+ ) ;
220+ ctx. save( & ip, port, & request. pool( ) )
221+ } )
222+ } ,
223+ )
237224 }
238225 }
239- Status :: NGX_OK
240226 }
241227) ;
242228
243229http_variable_get ! (
244230 ngx_http_orig_dst_port_variable,
245- |request: & mut http:: Request , v: * mut ngx_variable_value_t, _: usize | {
246- let ctx = request. get_module_ctx:: <NgxHttpOrigDstCtx >( Module :: module( ) ) ;
247- if let Some ( obj) = ctx {
248- ngx_log_debug_http!( request, "httporigdst: found context and binding variable" , ) ;
249- unsafe { obj. bind_port( v) } ;
250- return Status :: NGX_OK ;
251- }
252- // lazy initialization:
253- // get original dest information
254- // create context
255- // set context
256- // bind port
257- ngx_log_debug_http!( request, "httporigdst: context not found, getting address" ) ;
258- let r = unsafe { ngx_get_origdst( request) } ;
259- match r {
260- Err ( e) => {
261- return e;
231+ |mut request: & mut http:: Request , v: * mut ngx_variable_value_t, _: usize | {
232+ let rctx: & mut RequestWithContext <Module , NgxHttpOrigDstCtx >;
233+ ( rctx, request) = RequestWithContext :: from_request( request) ;
234+ match rctx. get( ) {
235+ Some ( ctx) => {
236+ ngx_log_debug_http!( request, "httporigdst: found context and binding variable" ) ;
237+ unsafe { ctx. bind_port( v) } ;
238+ Status :: NGX_OK
262239 }
263- Ok ( ( ip, port) ) => {
264- // create context,
265- // set context
266- let new_ctx = request
267- . pool( )
268- . allocate:: <NgxHttpOrigDstCtx >( Default :: default ( ) ) ;
269-
270- if new_ctx. is_null( ) {
271- return Status :: NGX_ERROR ;
272- }
273-
274- ngx_log_debug_http!(
275- request,
276- "httporigdst: saving ip - {:?}, port - {}" ,
277- ip,
278- port,
279- ) ;
280- unsafe { ( * new_ctx) . save( & ip, port, & request. pool( ) ) } ;
281- unsafe { ( * new_ctx) . bind_port( v) } ;
282- request. set_module_ctx( new_ctx as * mut c_void, Module :: module( ) ) ;
240+ None => {
241+ ngx_log_debug_http!( request, "httporigdst: context not found, getting address" ) ;
242+ unsafe { ngx_get_origdst( request) } . map_or_else(
243+ |e| e,
244+ |( ip, port) | {
245+ rctx. set( NgxHttpOrigDstCtx :: default ( ) )
246+ . map_or( Status :: NGX_ERROR , |ctx| {
247+ unsafe { ctx. bind_port( v) } ;
248+ ngx_log_debug_http!(
249+ request,
250+ "httporigdst: saving ip - {ip}, port - {port}" ,
251+ ) ;
252+ ctx. save( & ip, port, & request. pool( ) )
253+ } )
254+ } ,
255+ )
283256 }
284257 }
285- Status :: NGX_OK
286258 }
287259) ;
288260
0 commit comments