@@ -35,6 +35,9 @@ static ngx_int_t ngx_http_python_variable(ngx_http_request_t *r,
3535static PyObject * ngx_http_python_eval (ngx_http_request_t * r , PyCodeObject * code ,
3636 ngx_event_t * wake );
3737
38+ static void * ngx_http_python_create_loc_conf (ngx_conf_t * cf );
39+ static char * ngx_http_python_merge_loc_conf (ngx_conf_t * cf , void * parent ,
40+ void * child );
3841static char * ngx_http_python_set (ngx_conf_t * cf , ngx_command_t * cmd ,
3942 void * conf );
4043static char * ngx_http_python_access (ngx_conf_t * cf , ngx_command_t * cmd ,
@@ -43,30 +46,29 @@ static char *ngx_http_python_log(ngx_conf_t *cf, ngx_command_t *cmd,
4346 void * conf );
4447static char * ngx_http_python_content (ngx_conf_t * cf , ngx_command_t * cmd ,
4548 void * conf );
46- static void * ngx_http_python_create_loc_conf (ngx_conf_t * cf );
47- static char * ngx_http_python_merge_loc_conf (ngx_conf_t * cf , void * parent ,
48- void * child );
4949static ngx_int_t ngx_http_python_init (ngx_conf_t * cf );
50- static ngx_int_t ngx_http_python_init_namespace (ngx_conf_t * cf );
5150
5251
5352static ngx_command_t ngx_http_python_commands [] = {
5453
5554 { ngx_string ("python" ),
56- NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
55+ NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF
56+ |NGX_HTTP_UPS_CONF |NGX_HTTP_SIF_CONF |NGX_HTTP_LIF_CONF
57+ |NGX_CONF_TAKE1 ,
5758 ngx_python_set_slot ,
5859 0 ,
5960 0 ,
6061 NULL },
6162
6263 { ngx_string ("python_include" ),
63- NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF |NGX_CONF_TAKE1 ,
64+ NGX_HTTP_MAIN_CONF |NGX_HTTP_SRV_CONF |NGX_HTTP_LOC_CONF
65+ |NGX_HTTP_UPS_CONF |NGX_HTTP_SIF_CONF |NGX_HTTP_LIF_CONF
66+ |NGX_CONF_TAKE1 ,
6467 ngx_python_include_set_slot ,
6568 0 ,
6669 0 ,
6770 NULL },
6871
69-
7072 { ngx_string ("python_set" ),
7173 NGX_HTTP_MAIN_CONF |NGX_CONF_TAKE2 ,
7274 ngx_http_python_set ,
@@ -330,7 +332,7 @@ static PyObject *
330332ngx_http_python_eval (ngx_http_request_t * r , PyCodeObject * code ,
331333 ngx_event_t * wake )
332334{
333- PyObject * result , * pr ;
335+ PyObject * result , * old ;
334336 ngx_http_python_ctx_t * ctx ;
335337 ngx_http_core_loc_conf_t * clcf ;
336338
@@ -347,18 +349,16 @@ ngx_http_python_eval(ngx_http_request_t *r, PyCodeObject *code,
347349 ngx_http_set_ctx (r , ctx , ngx_http_python_module );
348350 }
349351
350- if (ctx -> request == NULL ) {
351- ctx -> request = ngx_http_python_request_create (r );
352- if (ctx -> request == NULL ) {
353- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
354- "python error: %s" , ngx_python_get_error (r -> pool ));
352+ if (ctx -> python == NULL ) {
353+ ctx -> python = ngx_python_create_ctx (r -> pool , r -> connection -> log );
354+ if (ctx -> python == NULL ) {
355355 return NULL ;
356356 }
357357 }
358358
359- if (ctx -> python == NULL ) {
360- ctx -> python = ngx_python_create_ctx ( r -> pool , r -> connection -> log );
361- if (ctx -> python == NULL ) {
359+ if (ctx -> request == NULL ) {
360+ ctx -> request = ngx_http_python_request_create ( r );
361+ if (ctx -> request == NULL ) {
362362 return NULL ;
363363 }
364364 }
@@ -368,23 +368,11 @@ ngx_http_python_eval(ngx_http_request_t *r, PyCodeObject *code,
368368 ngx_python_set_resolver (ctx -> python , clcf -> resolver ,
369369 clcf -> resolver_timeout );
370370
371- pr = PyDict_GetItemString (ctx -> python -> ns , "r" );
372-
373- if (pr == NULL ) {
374- if (PyDict_SetItemString (ctx -> python -> ns , "r" , ctx -> request ) < 0 ) {
375- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
376- "python error: %s" , ngx_python_get_error (r -> pool ));
377- }
378- }
371+ old = ngx_python_set_value (ctx -> python , "r" , ctx -> request );
379372
380373 result = ngx_python_eval (ctx -> python , code , wake );
381374
382- if (pr == NULL ) {
383- if (PyDict_DelItemString (ctx -> python -> ns , "r" ) < 0 ) {
384- ngx_log_error (NGX_LOG_ERR , r -> connection -> log , 0 ,
385- "python error: %s" , ngx_python_get_error (r -> pool ));
386- }
387- }
375+ ngx_python_reset_value (ctx -> python , "r" , old );
388376
389377 ngx_log_debug3 (NGX_LOG_DEBUG_HTTP , r -> connection -> log , 0 ,
390378 "http python eval end code:%p, wake:%p, result:%p" ,
@@ -430,32 +418,6 @@ ngx_http_python_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
430418}
431419
432420
433- static ngx_int_t
434- ngx_http_python_init (ngx_conf_t * cf )
435- {
436- ngx_http_handler_pt * h ;
437- ngx_http_core_main_conf_t * cmcf ;
438-
439- cmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_core_module );
440-
441- h = ngx_array_push (& cmcf -> phases [NGX_HTTP_ACCESS_PHASE ].handlers );
442- if (h == NULL ) {
443- return NGX_ERROR ;
444- }
445-
446- * h = ngx_http_python_access_handler ;
447-
448- h = ngx_array_push (& cmcf -> phases [NGX_HTTP_LOG_PHASE ].handlers );
449- if (h == NULL ) {
450- return NGX_ERROR ;
451- }
452-
453- * h = ngx_http_python_log_handler ;
454-
455- return NGX_OK ;
456- }
457-
458-
459421static char *
460422ngx_http_python_set (ngx_conf_t * cf , ngx_command_t * cmd , void * conf )
461423{
@@ -479,10 +441,6 @@ ngx_http_python_set(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
479441 return NGX_CONF_ERROR ;
480442 }
481443
482- if (ngx_http_python_init_namespace (cf ) != NGX_OK ) {
483- return NGX_CONF_ERROR ;
484- }
485-
486444 code = ngx_python_compile (cf , value [2 ].data );
487445 if (code == NULL ) {
488446 return NGX_CONF_ERROR ;
@@ -517,10 +475,6 @@ ngx_http_python_access(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
517475 return NGX_CONF_ERROR ;
518476 }
519477
520- if (ngx_http_python_init_namespace (cf ) != NGX_OK ) {
521- return NGX_CONF_ERROR ;
522- }
523-
524478 * pcode = ngx_python_compile (cf , value [1 ].data );
525479 if (* pcode == NULL ) {
526480 return NGX_CONF_ERROR ;
@@ -552,10 +506,6 @@ ngx_http_python_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
552506 return NGX_CONF_ERROR ;
553507 }
554508
555- if (ngx_http_python_init_namespace (cf ) != NGX_OK ) {
556- return NGX_CONF_ERROR ;
557- }
558-
559509 * pcode = ngx_python_compile (cf , value [1 ].data );
560510 if (* pcode == NULL ) {
561511 return NGX_CONF_ERROR ;
@@ -579,10 +529,6 @@ ngx_http_python_content(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
579529 return "is duplicate" ;
580530 }
581531
582- if (ngx_http_python_init_namespace (cf ) != NGX_OK ) {
583- return NGX_CONF_ERROR ;
584- }
585-
586532 plcf -> content = ngx_python_compile (cf , value [1 ].data );
587533 if (plcf -> content == NULL ) {
588534 return NGX_CONF_ERROR ;
@@ -597,15 +543,34 @@ ngx_http_python_content(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
597543
598544
599545static ngx_int_t
600- ngx_http_python_init_namespace (ngx_conf_t * cf )
546+ ngx_http_python_init (ngx_conf_t * cf )
601547{
602- if (ngx_python_get_namespace (cf ) == NULL ) {
603- return NGX_ERROR ;
548+ ngx_http_handler_pt * h ;
549+ ngx_http_core_main_conf_t * cmcf ;
550+
551+ if (ngx_python_active (cf ) != NGX_OK ) {
552+ return NGX_OK ;
604553 }
605554
606555 if (ngx_http_python_request_init (cf ) != NGX_OK ) {
607556 return NGX_ERROR ;
608557 }
609558
559+ cmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_core_module );
560+
561+ h = ngx_array_push (& cmcf -> phases [NGX_HTTP_ACCESS_PHASE ].handlers );
562+ if (h == NULL ) {
563+ return NGX_ERROR ;
564+ }
565+
566+ * h = ngx_http_python_access_handler ;
567+
568+ h = ngx_array_push (& cmcf -> phases [NGX_HTTP_LOG_PHASE ].handlers );
569+ if (h == NULL ) {
570+ return NGX_ERROR ;
571+ }
572+
573+ * h = ngx_http_python_log_handler ;
574+
610575 return NGX_OK ;
611576}
0 commit comments