3232#include "ngx_http_lua_ssl_session_fetchby.h"
3333#include "ngx_http_lua_headers.h"
3434#include "ngx_http_lua_pipe.h"
35+ #include "ngx_http_lua_event.h"
36+
37+ int ngx_http_lua_event_inited = 0 ;
38+
39+ ngx_http_lua_event_actions_t ngx_http_lua_event_actions ;
3540
3641
3742static void * ngx_http_lua_create_main_conf (ngx_conf_t * cf );
@@ -715,6 +720,24 @@ ngx_http_lua_init(ngx_conf_t *cf)
715720 ngx_http_lua_main_conf_t * lmcf ;
716721 ngx_pool_cleanup_t * cln ;
717722 ngx_str_t name = ngx_string ("host" );
723+ ngx_cycle_t * fake_cycle ;
724+ ngx_connection_t * c = NULL ;
725+ ngx_http_request_t * r = NULL ;
726+ ngx_uint_t i ;
727+ void * next ;
728+ ngx_event_t * rev , * wev ;
729+ ngx_http_lua_ctx_t * ctx ;
730+ ngx_module_t * * modules ;
731+ ngx_http_module_t * module ;
732+ // ngx_core_module_t *c_module;
733+ ngx_http_conf_ctx_t * conf_ctx , http_ctx ;
734+ void * cur , * prev ;
735+ ngx_conf_t conf ;
736+ ngx_http_lua_loc_conf_t * top_llcf ;
737+ ngx_http_core_loc_conf_t * top_clcf ;
738+ char * rv ;
739+ ngx_conf_file_t cf_file ;
740+ ngx_event_conf_t * ecf ;
718741
719742 if (ngx_process == NGX_PROCESS_SIGNALLER || ngx_test_config ) {
720743 return NGX_OK ;
@@ -889,6 +912,246 @@ ngx_http_lua_init(ngx_conf_t *cf)
889912
890913 ngx_http_lua_assert (lmcf -> lua != NULL );
891914
915+ conf_ctx = cf -> ctx ;
916+ http_ctx .main_conf = conf_ctx -> main_conf ;
917+
918+ top_clcf = conf_ctx -> loc_conf [ngx_http_core_module .ctx_index ];
919+ top_llcf = conf_ctx -> loc_conf [ngx_http_lua_module .ctx_index ];
920+
921+ ngx_memzero (& conf , sizeof (ngx_conf_t ));
922+
923+ conf .temp_pool = ngx_create_pool (NGX_CYCLE_POOL_SIZE , cf -> log );
924+ if (conf .temp_pool == NULL ) {
925+ return NGX_ERROR ;
926+ }
927+
928+ conf .temp_pool -> log = cf -> log ;
929+
930+ /* we fake a temporary ngx_cycle_t here because some
931+ * modules' merge conf handler may produce side effects in
932+ * cf->cycle (like ngx_proxy vs cf->cycle->paths).
933+ * also, we cannot allocate our temp cycle on the stack
934+ * because some modules like ngx_http_core_module reference
935+ * addresses within cf->cycle (i.e., via "&cf->cycle->new_log")
936+ */
937+ fake_cycle = ngx_palloc (cf -> pool , sizeof (ngx_cycle_t ));
938+ if (fake_cycle == NULL ) {
939+ return NGX_ERROR ;
940+ }
941+
942+ ngx_memcpy (fake_cycle , cf -> cycle , sizeof (ngx_cycle_t ));
943+ fake_cycle -> pool = cf -> pool ;
944+
945+ conf .ctx = & http_ctx ;
946+ conf .cycle = fake_cycle ;
947+ conf .pool = fake_cycle -> pool ;
948+ conf .log = fake_cycle -> log ;
949+
950+ ngx_memzero (& cf_file , sizeof (cf_file ));
951+ cf_file .file .name = ngx_cycle -> conf_file ;
952+ conf .conf_file = & cf_file ;
953+
954+ http_ctx .loc_conf = ngx_pcalloc (conf .pool ,
955+ sizeof (void * ) * ngx_http_max_module );
956+ if (http_ctx .loc_conf == NULL ) {
957+ return NGX_ERROR ;
958+ }
959+
960+ http_ctx .srv_conf = ngx_pcalloc (conf .pool ,
961+ sizeof (void * ) * ngx_http_max_module );
962+ if (http_ctx .srv_conf == NULL ) {
963+ return NGX_ERROR ;
964+ }
965+
966+ fake_cycle -> log = cf -> log ;
967+ fake_cycle -> connection_n = 128 ;
968+
969+ ngx_queue_init (& fake_cycle -> reusable_connections_queue );
970+
971+ #if (nginx_version >= 1009011 )
972+ modules = cf -> cycle -> modules ;
973+ #else
974+ modules = ngx_modules ;
975+ #endif
976+
977+ #if 0
978+ // init modules
979+ for (i = 0 ; modules [i ]; i ++ ) {
980+ if (modules [i ]-> init_module && modules [i ]-> type != NGX_CORE_MODULE ) {
981+ if (modules [i ]-> init_module (fake_cycle ) != NGX_OK ) {
982+ return NGX_ERROR ;
983+ }
984+ }
985+ }
986+ #endif
987+
988+ ecf = ngx_palloc (fake_cycle -> pool , sizeof (ngx_event_conf_t ));
989+ #if 0
990+ // init ngx event
991+ for (i = 0 ; modules [i ]; i ++ ) {
992+ if (modules [i ]-> type != NGX_EVENT_MODULE ) {
993+ continue ;
994+ }
995+
996+ c_module = modules [i ]-> ctx ;
997+ ecf -> use = modules [i ]-> ctx_index ;
998+ ecf -> name = c_module -> name .data ;
999+ }
1000+ #endif
1001+
1002+ // TODO: init fake_cycle->conf_ctx before assignment
1003+ fake_cycle -> conf_ctx [ngx_events_module .index ] = cf -> ctx ;
1004+ (fake_cycle -> conf_ctx [ngx_events_module .index ])[ngx_event_core_module .ctx_index ] = (void * * ) ecf ;
1005+
1006+ fake_cycle -> connections = ngx_alloc (
1007+ sizeof (ngx_connection_t ) * fake_cycle -> connection_n ,
1008+ fake_cycle -> log );
1009+ if (fake_cycle -> connections == NULL ) {
1010+ return NGX_ERROR ;
1011+ }
1012+
1013+ fake_cycle -> read_events = ngx_alloc (
1014+ sizeof (ngx_event_t ) * fake_cycle -> connection_n , fake_cycle -> log );
1015+ if (fake_cycle -> read_events == NULL ) {
1016+ return NGX_ERROR ;
1017+ }
1018+
1019+ rev = fake_cycle -> read_events ;
1020+ for (i = 0 ; i < fake_cycle -> connection_n ; i ++ ) {
1021+ rev [i ].closed = 1 ;
1022+ rev [i ].instance = 1 ;
1023+ }
1024+
1025+ fake_cycle -> write_events = ngx_alloc (
1026+ sizeof (ngx_event_t ) * fake_cycle -> connection_n , fake_cycle -> log );
1027+ if (fake_cycle -> write_events == NULL ) {
1028+ return NGX_ERROR ;
1029+ }
1030+
1031+ wev = fake_cycle -> write_events ;
1032+ for (i = 0 ; i < fake_cycle -> connection_n ; i ++ ) {
1033+ wev [i ].closed = 1 ;
1034+ }
1035+
1036+ c = fake_cycle -> connections ;
1037+ i = fake_cycle -> connection_n ;
1038+ next = NULL ;
1039+
1040+ do {
1041+ i -- ;
1042+
1043+ c [i ].data = next ;
1044+ c [i ].read = & fake_cycle -> read_events [i ];
1045+ c [i ].write = & fake_cycle -> write_events [i ];
1046+ c [i ].fd = (ngx_socket_t ) - 1 ;
1047+
1048+ next = & c [i ];
1049+ } while (i );
1050+
1051+ fake_cycle -> free_connections = next ;
1052+ fake_cycle -> free_connection_n = fake_cycle -> connection_n ;
1053+
1054+ saved_cycle = ngx_cycle ;
1055+ ngx_cycle = fake_cycle ;
1056+
1057+ c = ngx_http_lua_create_fake_connection (cf -> pool );
1058+ if (c == NULL ) {
1059+ return NGX_ERROR ;
1060+ }
1061+
1062+ r = ngx_http_lua_create_fake_request (c );
1063+ if (r == NULL ) {
1064+ ngx_http_lua_close_fake_connection (c );
1065+ return NGX_ERROR ;
1066+ }
1067+
1068+ ngx_cycle = saved_cycle ;
1069+
1070+ for (i = 0 ; modules [i ]; i ++ ) {
1071+ if (modules [i ]-> type != NGX_HTTP_MODULE ) {
1072+ continue ;
1073+ }
1074+
1075+ module = modules [i ]-> ctx ;
1076+
1077+ if (module -> create_srv_conf ) {
1078+ cur = module -> create_srv_conf (& conf );
1079+ if (cur == NULL ) {
1080+ return NGX_ERROR ;
1081+ }
1082+
1083+ http_ctx .srv_conf [modules [i ]-> ctx_index ] = cur ;
1084+
1085+ if (module -> merge_srv_conf ) {
1086+ prev = module -> create_srv_conf (& conf );
1087+ if (prev == NULL ) {
1088+ return NGX_ERROR ;
1089+ }
1090+
1091+ rv = module -> merge_srv_conf (& conf , prev , cur );
1092+ if (rv != NGX_CONF_OK ) {
1093+ // goto failed;
1094+ return NGX_ERROR ;
1095+ }
1096+ }
1097+ }
1098+
1099+ if (module -> create_loc_conf ) {
1100+ cur = module -> create_loc_conf (& conf );
1101+ if (cur == NULL ) {
1102+ return NGX_ERROR ;
1103+ }
1104+
1105+ http_ctx .loc_conf [modules [i ]-> ctx_index ] = cur ;
1106+
1107+ if (module -> merge_loc_conf ) {
1108+ if (modules [i ] == & ngx_http_lua_module ) {
1109+ prev = top_llcf ;
1110+
1111+ } else if (modules [i ] == & ngx_http_core_module ) {
1112+ prev = top_clcf ;
1113+
1114+ } else {
1115+ prev = module -> create_loc_conf (& conf );
1116+ if (prev == NULL ) {
1117+ return NGX_ERROR ;
1118+ }
1119+ }
1120+
1121+ rv = module -> merge_loc_conf (& conf , prev , cur );
1122+ if (rv != NGX_CONF_OK ) {
1123+ // goto failed;
1124+ return NGX_ERROR ;
1125+ }
1126+ }
1127+ }
1128+ }
1129+
1130+ ngx_destroy_pool (conf .temp_pool );
1131+ conf .temp_pool = NULL ;
1132+
1133+ r -> main_conf = http_ctx .main_conf ;
1134+ r -> srv_conf = http_ctx .srv_conf ;
1135+ r -> loc_conf = http_ctx .loc_conf ;
1136+
1137+ ctx = ngx_http_lua_create_ctx (r );
1138+ if (ctx == NULL ) {
1139+ ngx_http_lua_close_fake_connection (c );
1140+ return NGX_ERROR ;
1141+ }
1142+
1143+ ctx -> context = NGX_HTTP_LUA_CONTEXT_INIT ;
1144+ ctx -> cur_co_ctx = NULL ;
1145+ r -> read_event_handler = ngx_http_block_reading ;
1146+
1147+ ngx_http_lua_set_req (lmcf -> lua , r );
1148+
1149+ #if 0
1150+ if (ngx_http_lua_init_event (cf -> cycle ) == NGX_OK ) {
1151+ ngx_http_lua_event_inited = 1 ;
1152+ }
1153+ #endif
1154+
8921155 if (!lmcf -> requires_shm && lmcf -> init_handler ) {
8931156 saved_cycle = ngx_cycle ;
8941157 ngx_cycle = cf -> cycle ;
@@ -898,6 +1161,8 @@ ngx_http_lua_init(ngx_conf_t *cf)
8981161 ngx_cycle = saved_cycle ;
8991162
9001163 if (rc != NGX_OK ) {
1164+ // ngx_http_lua_close_fake_connection(c);
1165+
9011166 /* an error happened */
9021167 return NGX_ERROR ;
9031168 }
0 commit comments