@@ -2,34 +2,21 @@ use std::ffi::{c_char, c_void};
22
33use ngx:: core;
44use ngx:: ffi:: {
5- ngx_array_push, ngx_command_t, ngx_conf_t, ngx_http_handler_pt, ngx_http_module_t,
6- ngx_http_phases_NGX_HTTP_ACCESS_PHASE, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
5+ ngx_command_t, ngx_conf_t, ngx_http_module_t, ngx_int_t, ngx_module_t, ngx_str_t, ngx_uint_t,
76 NGX_CONF_TAKE1 , NGX_HTTP_LOC_CONF , NGX_HTTP_LOC_CONF_OFFSET , NGX_HTTP_MODULE , NGX_LOG_EMERG ,
87} ;
9- use ngx:: http:: { self , HttpModule , MergeConfigError } ;
10- use ngx:: http:: { HttpModuleLocationConf , HttpModuleMainConf , NgxHttpCoreModule } ;
11- use ngx:: { http_request_handler, ngx_conf_log_error, ngx_log_debug_http, ngx_string} ;
8+ use ngx:: http:: { self , HttpModule , HttpModuleLocationConf , HttpRequestHandler , MergeConfigError } ;
9+ use ngx:: { ngx_conf_log_error, ngx_log_debug_http, ngx_string} ;
1210
1311struct Module ;
1412
1513impl http:: HttpModule for Module {
1614 fn module ( ) -> & ' static ngx_module_t {
1715 unsafe { & * :: core:: ptr:: addr_of!( ngx_http_curl_module) }
1816 }
19-
2017 unsafe extern "C" fn postconfiguration ( cf : * mut ngx_conf_t ) -> ngx_int_t {
21- // SAFETY: this function is called with non-NULL cf always
22- let cf = & mut * cf;
23- let cmcf = NgxHttpCoreModule :: main_conf_mut ( cf) . expect ( "http core main conf" ) ;
24-
25- let h = ngx_array_push (
26- & mut cmcf. phases [ ngx_http_phases_NGX_HTTP_ACCESS_PHASE as usize ] . handlers ,
27- ) as * mut ngx_http_handler_pt ;
28- if h. is_null ( ) {
29- return core:: Status :: NGX_ERROR . into ( ) ;
30- }
31- // set an Access phase handler
32- * h = Some ( curl_access_handler) ;
18+ let cf = unsafe { & mut * cf } ;
19+ CurlRequestHandler :: register ( cf) ;
3320 core:: Status :: NGX_OK . into ( )
3421 }
3522}
@@ -90,25 +77,32 @@ impl http::Merge for ModuleConfig {
9077 }
9178}
9279
93- http_request_handler ! ( curl_access_handler, |request: & mut http:: Request | {
94- let co = Module :: location_conf( request) . expect( "module config is none" ) ;
80+ struct CurlRequestHandler ;
81+
82+ impl HttpRequestHandler < Option < ngx_int_t > > for CurlRequestHandler {
83+ const PHASE : nginx_sys:: NgxHttpPhases = nginx_sys:: NgxHttpPhases :: Access ;
84+ type Module = Module ;
9585
96- ngx_log_debug_http!( request, "curl module enabled: {}" , co. enable) ;
86+ fn handler ( request : & mut http:: Request ) -> Option < ngx_int_t > {
87+ let co = Module :: location_conf ( request) . expect ( "module config is none" ) ;
9788
98- match co. enable {
99- true => {
100- if request
101- . user_agent( )
102- . is_some_and( |ua| ua. as_bytes( ) . starts_with( b"curl" ) )
103- {
104- http:: HTTPStatus :: FORBIDDEN . into( )
105- } else {
106- core:: NGX_O_DECLINED
89+ ngx_log_debug_http ! ( request, "curl module enabled: {}" , co. enable) ;
90+
91+ match co. enable {
92+ true => {
93+ if request
94+ . user_agent ( )
95+ . is_some_and ( |ua| ua. as_bytes ( ) . starts_with ( b"curl" ) )
96+ {
97+ http:: HTTPStatus :: FORBIDDEN . into ( )
98+ } else {
99+ core:: NGX_O_DECLINED
100+ }
107101 }
102+ false => core:: NGX_O_DECLINED ,
108103 }
109- false => core:: NGX_O_DECLINED ,
110104 }
111- } ) ;
105+ }
112106
113107extern "C" fn ngx_http_curl_commands_set_enable (
114108 cf : * mut ngx_conf_t ,
0 commit comments