@@ -84,6 +84,7 @@ typedef struct {
8484} ngx_http_link_func_main_conf_t ;
8585
8686typedef void (* ngx_http_link_func_app_handler )(ngx_link_func_ctx_t * );
87+ typedef void (* ngx_http_link_func_app_cycle_handler )(ngx_link_func_cycle_t * );
8788
8889typedef struct {
8990 void * _app ;
@@ -203,6 +204,12 @@ static ngx_http_link_func_http_header_body* ngx_http_link_func_https_request( ng
203204/*** End Download Feature Support ***/
204205
205206/**Extern interface**/
207+ void ngx_link_func_cyc_log_debug (ngx_link_func_cycle_t * cyc , const char * msg );
208+ void ngx_link_func_cyc_log_info (ngx_link_func_cycle_t * cyc , const char * msg );
209+ void ngx_link_func_cyc_log_warn (ngx_link_func_cycle_t * cyc , const char * msg );
210+ void ngx_link_func_cyc_log_err (ngx_link_func_cycle_t * cyc , const char * msg );
211+ u_char * ngx_link_func_cyc_get_prop (ngx_link_func_cycle_t * cyc , const char * key , size_t keylen );
212+
206213void ngx_link_func_log_debug (ngx_link_func_ctx_t * ctx , const char * msg );
207214void ngx_link_func_log_info (ngx_link_func_ctx_t * ctx , const char * msg );
208215void ngx_link_func_log_warn (ngx_link_func_ctx_t * ctx , const char * msg );
@@ -634,18 +641,25 @@ static ngx_int_t
634641ngx_http_link_func_proceed_init_calls (ngx_cycle_t * cycle , ngx_http_link_func_srv_conf_t * scf , ngx_http_link_func_main_conf_t * mcf ) {
635642 /**** Init the client apps ngx_http_link_func_init ***/
636643 char * error ;
637- ngx_http_link_func_app_handler func ;
638- * (void * * )(& func ) = dlsym (scf -> _app , (const char * )"ngx_link_func_init " );
644+ ngx_http_link_func_app_cycle_handler func ;
645+ * (void * * )(& func ) = dlsym (scf -> _app , (const char * )"ngx_link_func_init_cycle " );
639646 if ((error = dlerror ()) != NULL ) {
640647 ngx_log_error (NGX_LOG_WARN , cycle -> log , 0 , "Unable to init call %s , skipped init called" , error );
641648 } else {
642649 ngx_log_error (NGX_LOG_INFO , cycle -> log , 0 , "application initializing" );
643650 /*** Init the apps ***/
644- ngx_link_func_ctx_t new_ctx ; //config request
645- new_ctx .__pl__ = cycle -> pool ;
646- new_ctx .__log__ = cycle -> log ;
647- new_ctx .shared_mem = (void * )mcf -> shm_ctx -> shared_mem ;
648- func (& new_ctx );
651+ ngx_link_func_cycle_t appcyc ;
652+ appcyc .has_error = 0 ;
653+ appcyc .__cycle__ = cycle ;
654+ appcyc .__srv_cf__ = scf ;
655+ appcyc .__pl__ = cycle -> pool ;
656+ appcyc .__log__ = cycle -> log ;
657+ appcyc .shared_mem = (void * )mcf -> shm_ctx -> shared_mem ;
658+ func (& appcyc );
659+ if (appcyc .has_error ) {
660+ ngx_log_error (NGX_LOG_EMERG , cycle -> log , 0 , "%s" , "link function worker Initialize unsuccessfully" );
661+ return NGX_ERROR ;
662+ }
649663 }
650664
651665 ngx_log_error (NGX_LOG_INFO , cycle -> log , 0 , "%s" , "Done proceed init calls" );
@@ -729,7 +743,7 @@ ngx_http_link_func_pre_configuration(ngx_conf_t *cf) {
729743 return NGX_ERROR ;
730744#endif
731745
732- #ifndef ngx_link_func_module_version_32
746+ #ifndef ngx_link_func_module_version_33
733747 ngx_conf_log_error (NGX_LOG_EMERG , cf , 0 , "%s" , "the ngx_http_link_func_module.h might not be latest or not found in the c header path, \
734748 please copy latest ngx_http_link_func_module.h to your /usr/include or /usr/local/include or relavent header search path \
735749 with read and write permission." );
@@ -775,6 +789,24 @@ ngx_http_link_func_application_compatibility_check(ngx_conf_t *cf, ngx_http_core
775789 }
776790
777791 char * error ;
792+
793+ /* * check init function block, this version has to be at least init with empty function * */
794+ ngx_http_link_func_app_cycle_handler func ;
795+ * (void * * )(& func ) = dlsym (scf -> _app , (const char * )"ngx_link_func_init_cycle" );
796+ if ((error = dlerror ()) != NULL ) {
797+ ngx_conf_log_error (NGX_LOG_ERR , cf , 0 ,
798+ "function ngx_link_func_init_cycle(ngx_link_func_cycle_t *cycle) not found in \"%V\", at least create an empty init function block \n %s" ,
799+ & scf -> _libname , error );
800+ return NGX_ERROR ;
801+ }
802+ * (void * * )(& func ) = dlsym (scf -> _app , (const char * )"ngx_link_func_exit_cycle" );
803+ if ((error = dlerror ()) != NULL ) {
804+ ngx_conf_log_error (NGX_LOG_ERR , cf , 0 ,
805+ "function ngx_link_func_exit_cycle(ngx_link_func_cycle_t *cycle) not found in \"%V\", at least create an empty exit function block \n %s" ,
806+ & scf -> _libname , error );
807+ }
808+
809+
778810 /*** loop and without remove queue***/
779811 ngx_queue_t * q ;
780812 for (q = ngx_queue_head (scf -> _link_func_locs_queue );
@@ -942,16 +974,22 @@ ngx_http_link_func_process_exit(ngx_cycle_t *cycle) {
942974 scf = cscf -> ctx -> srv_conf [ngx_http_link_func_module .ctx_index ];
943975 if (scf && scf -> _app ) {
944976 /*** Exiting the client apps ***/
945- ngx_http_link_func_app_handler func ;
946- * (void * * )(& func ) = dlsym (scf -> _app , (const char * )"ngx_link_func_exit " );
977+ ngx_http_link_func_app_cycle_handler func ;
978+ * (void * * )(& func ) = dlsym (scf -> _app , (const char * )"ngx_link_func_exit_cycle " );
947979 if ((error = dlerror ()) != NULL ) {
948980 ngx_log_error (NGX_LOG_WARN , cycle -> log , 0 , "Unable to exit call %s , skipped exit called" , error );
949981 } else {
950- ngx_link_func_ctx_t new_ctx ; //config request
951- new_ctx .__pl__ = cycle -> pool ;
952- new_ctx .__log__ = cycle -> log ;
953- new_ctx .shared_mem = (void * )mcf -> shm_ctx -> shared_mem ;
954- func (& new_ctx );
982+ ngx_link_func_cycle_t appcyc ;
983+ appcyc .has_error = 0 ;
984+ appcyc .__cycle__ = cycle ;
985+ appcyc .__srv_cf__ = scf ;
986+ appcyc .__pl__ = cycle -> pool ;
987+ appcyc .__log__ = cycle -> log ;
988+ appcyc .shared_mem = (void * )mcf -> shm_ctx -> shared_mem ;
989+ func (& appcyc );
990+ if (appcyc .has_error ) {
991+ ngx_log_error (NGX_LOG_ERR , cycle -> log , 0 , "%s" , "link function worker exit error" );
992+ }
955993 }
956994 // Unload app, unload old app if nginx reload
957995 if (dlclose (scf -> _app ) != 0 ) {
@@ -1700,6 +1738,23 @@ ngx_http_link_func_client_body_handler(ngx_http_request_t *r) {
17001738}
17011739
17021740/****** extern interface ********/
1741+ void
1742+ ngx_link_func_cyc_log_debug (ngx_link_func_cycle_t * cyc , const char * msg ) {
1743+ ngx_log_error (NGX_LOG_DEBUG , (ngx_log_t * )cyc -> __log__ , 0 , "%s" , msg );
1744+ }
1745+ void
1746+ ngx_link_func_cyc_log_info (ngx_link_func_cycle_t * cyc , const char * msg ) {
1747+ ngx_log_error (NGX_LOG_INFO , (ngx_log_t * )cyc -> __log__ , 0 , "%s" , msg );
1748+ }
1749+ void
1750+ ngx_link_func_cyc_log_warn (ngx_link_func_cycle_t * cyc , const char * msg ) {
1751+ ngx_log_error (NGX_LOG_WARN , (ngx_log_t * )cyc -> __log__ , 0 , "%s" , msg );
1752+ }
1753+ void
1754+ ngx_link_func_cyc_log_err (ngx_link_func_cycle_t * cyc , const char * msg ) {
1755+ ngx_log_error (NGX_LOG_ERR , (ngx_log_t * )cyc -> __log__ , 0 , "%s" , msg );
1756+ }
1757+
17031758void
17041759ngx_link_func_log_debug (ngx_link_func_ctx_t * ctx , const char * msg ) {
17051760 ngx_log_error (NGX_LOG_DEBUG , (ngx_log_t * )ctx -> __log__ , 0 , "%s" , msg );
@@ -1802,6 +1857,42 @@ ngx_link_func_get_prop(ngx_link_func_ctx_t *ctx, const char *key, size_t keylen)
18021857 return NULL ;
18031858}
18041859
1860+ u_char *
1861+ ngx_link_func_cyc_get_prop (ngx_link_func_cycle_t * cyc , const char * key , size_t keylen ) {
1862+ ngx_http_link_func_srv_conf_t * scf ;
1863+ ngx_uint_t nelts , i ;
1864+ ngx_keyval_t * keyval ;
1865+ ngx_log_t * log ;
1866+
1867+ if (cyc == NULL ) {
1868+ return NULL ;
1869+ }
1870+
1871+ log = (ngx_log_t * ) cyc -> __log__ ;
1872+ scf = (ngx_http_link_func_srv_conf_t * ) cyc -> __srv_cf__ ;
1873+
1874+ if ( scf == NULL || log == NULL ) {
1875+ ngx_log_error (NGX_LOG_EMERG , log , 0 , "Invalid link function server config" );
1876+ return NULL ;
1877+ }
1878+
1879+ if (scf -> _props == NULL ) {
1880+ return NULL ;
1881+ }
1882+
1883+ nelts = scf -> _props -> nelts ;
1884+ keyval = scf -> _props -> elts ;
1885+
1886+ for (i = 0 ; i < nelts ; i ++ ) {
1887+ if ( keyval -> key .len == keylen && ngx_strncasecmp (keyval -> key .data , (u_char * ) key , keylen ) == 0 ) {
1888+ /** it is config memory pool, should not reallocate or overwrite **/
1889+ return keyval -> value .data ;
1890+ }
1891+ keyval ++ ;
1892+ }
1893+ return NULL ;
1894+ }
1895+
18051896
18061897static int
18071898strpos (const char * haystack , const char * needle ) {
0 commit comments