3232#include "ngx_http_lua_log.h"
3333
3434
35+ #define LJ_CHUNKNAME_MAX_LEN 42
36+
37+
3538typedef struct ngx_http_lua_block_parser_ctx_s
3639 ngx_http_lua_block_parser_ctx_t ;
3740
@@ -43,8 +46,6 @@ typedef struct ngx_http_lua_block_parser_ctx_s
4346static ngx_int_t ngx_http_lua_set_by_lua_init (ngx_http_request_t * r );
4447#endif
4548
46- static u_char * ngx_http_lua_gen_chunk_name (ngx_conf_t * cf , const char * tag ,
47- size_t tag_len , size_t * chunkname_len );
4849static ngx_int_t ngx_http_lua_conf_read_lua_token (ngx_conf_t * cf ,
4950 ngx_http_lua_block_parser_ctx_t * ctx );
5051static u_char * ngx_http_lua_strlstrn (u_char * s1 , u_char * last , u_char * s2 ,
@@ -280,6 +281,8 @@ ngx_http_lua_set_by_lua_block(ngx_conf_t *cf, ngx_command_t *cmd,
280281char *
281282ngx_http_lua_set_by_lua (ngx_conf_t * cf , ngx_command_t * cmd , void * conf )
282283{
284+ size_t chunkname_len ;
285+ u_char * chunkname ;
283286 u_char * cache_key ;
284287 ngx_str_t * value ;
285288 ngx_str_t target ;
@@ -312,7 +315,15 @@ ngx_http_lua_set_by_lua(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
312315 return NGX_CONF_ERROR ;
313316 }
314317
318+ chunkname = ngx_http_lua_gen_chunk_name (cf , "set_by_lua" ,
319+ sizeof ("set_by_lua" ) - 1 ,
320+ & chunkname_len );
321+ if (chunkname == NULL ) {
322+ return NGX_CONF_ERROR ;
323+ }
324+
315325 filter_data -> key = cache_key ;
326+ filter_data -> chunkname = chunkname ;
316327 filter_data -> ref = LUA_REFNIL ;
317328 filter_data -> script = value [2 ];
318329 filter_data -> size = filter .size ;
@@ -375,6 +386,7 @@ ngx_http_lua_set_by_lua_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
375386 filter_data -> key = cache_key ;
376387 filter_data -> ref = LUA_REFNIL ;
377388 filter_data -> size = filter .size ;
389+ filter_data -> chunkname = NULL ;
378390
379391 ngx_str_null (& filter_data -> script );
380392
@@ -404,7 +416,8 @@ ngx_http_lua_filter_set_by_lua_inline(ngx_http_request_t *r, ngx_str_t *val,
404416 filter_data -> script .data ,
405417 filter_data -> script .len ,
406418 & filter_data -> ref ,
407- filter_data -> key , "=set_by_lua" );
419+ filter_data -> key ,
420+ (const char * ) filter_data -> chunkname );
408421 if (rc != NGX_OK ) {
409422 return NGX_ERROR ;
410423 }
@@ -912,7 +925,8 @@ char *
912925ngx_http_lua_header_filter_by_lua (ngx_conf_t * cf , ngx_command_t * cmd ,
913926 void * conf )
914927{
915- u_char * cache_key = NULL ;
928+ size_t chunkname_len ;
929+ u_char * cache_key = NULL , * chunkname ;
916930 ngx_str_t * value ;
917931 ngx_http_lua_main_conf_t * lmcf ;
918932 ngx_http_lua_loc_conf_t * llcf = conf ;
@@ -947,8 +961,15 @@ ngx_http_lua_header_filter_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
947961 return NGX_CONF_ERROR ;
948962 }
949963
964+ chunkname = ngx_http_lua_gen_chunk_name (cf , "header_filter_by_lua" ,
965+ sizeof ("header_filter_by_lua" ) - 1 , & chunkname_len );
966+ if (chunkname == NULL ) {
967+ return NGX_CONF_ERROR ;
968+ }
969+
950970 /* Don't eval nginx variables for inline lua code */
951971 llcf -> header_filter_src .value = value [1 ];
972+ llcf -> header_filter_chunkname = chunkname ;
952973
953974 } else {
954975 ngx_memzero (& ccv , sizeof (ngx_http_compile_complex_value_t ));
@@ -1004,7 +1025,8 @@ char *
10041025ngx_http_lua_body_filter_by_lua (ngx_conf_t * cf , ngx_command_t * cmd ,
10051026 void * conf )
10061027{
1007- u_char * cache_key = NULL ;
1028+ size_t chunkname_len ;
1029+ u_char * cache_key = NULL , * chunkname ;
10081030 ngx_str_t * value ;
10091031 ngx_http_lua_main_conf_t * lmcf ;
10101032 ngx_http_lua_loc_conf_t * llcf = conf ;
@@ -1039,8 +1061,16 @@ ngx_http_lua_body_filter_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
10391061 return NGX_CONF_ERROR ;
10401062 }
10411063
1064+ chunkname = ngx_http_lua_gen_chunk_name (cf , "body_filter_by_lua" ,
1065+ sizeof ("body_filter_by_lua" ) - 1 , & chunkname_len );
1066+ if (chunkname == NULL ) {
1067+ return NGX_CONF_ERROR ;
1068+ }
1069+
1070+
10421071 /* Don't eval nginx variables for inline lua code */
10431072 llcf -> body_filter_src .value = value [1 ];
1073+ llcf -> body_filter_chunkname = chunkname ;
10441074
10451075 } else {
10461076 ngx_memzero (& ccv , sizeof (ngx_http_compile_complex_value_t ));
@@ -1100,6 +1130,8 @@ ngx_http_lua_init_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
11001130 u_char * name ;
11011131 ngx_str_t * value ;
11021132 ngx_http_lua_main_conf_t * lmcf = conf ;
1133+ size_t chunkname_len ;
1134+ u_char * chunkname ;
11031135
11041136 dd ("enter" );
11051137
@@ -1135,6 +1167,15 @@ ngx_http_lua_init_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
11351167
11361168 } else {
11371169 lmcf -> init_src = value [1 ];
1170+
1171+ chunkname = ngx_http_lua_gen_chunk_name (cf , "init_by_lua" ,
1172+ sizeof ("init_by_lua" ) - 1 ,
1173+ & chunkname_len );
1174+ if (chunkname == NULL ) {
1175+ return NGX_CONF_ERROR ;
1176+ }
1177+
1178+ lmcf -> init_chunkname = chunkname ;
11381179 }
11391180
11401181 return NGX_CONF_OK ;
@@ -1167,6 +1208,8 @@ ngx_http_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
11671208 u_char * name ;
11681209 ngx_str_t * value ;
11691210 ngx_http_lua_main_conf_t * lmcf = conf ;
1211+ size_t chunkname_len ;
1212+ u_char * chunkname ;
11701213
11711214 dd ("enter" );
11721215
@@ -1195,6 +1238,14 @@ ngx_http_lua_init_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
11951238
11961239 } else {
11971240 lmcf -> init_worker_src = value [1 ];
1241+
1242+ chunkname = ngx_http_lua_gen_chunk_name (cf , "init_worker_by_lua" ,
1243+ sizeof ("init_worker_by_lua" ) - 1 , & chunkname_len );
1244+ if (chunkname == NULL ) {
1245+ return NGX_CONF_ERROR ;
1246+ }
1247+
1248+ lmcf -> init_worker_chunkname = chunkname ;
11981249 }
11991250
12001251 return NGX_CONF_OK ;
@@ -1227,6 +1278,8 @@ ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
12271278 u_char * name ;
12281279 ngx_str_t * value ;
12291280 ngx_http_lua_main_conf_t * lmcf = conf ;
1281+ size_t chunkname_len ;
1282+ u_char * chunkname ;
12301283
12311284 /* must specify a content handler */
12321285 if (cmd -> post == NULL ) {
@@ -1253,6 +1306,15 @@ ngx_http_lua_exit_worker_by_lua(ngx_conf_t *cf, ngx_command_t *cmd,
12531306
12541307 } else {
12551308 lmcf -> exit_worker_src = value [1 ];
1309+
1310+ chunkname = ngx_http_lua_gen_chunk_name (cf , "exit_worker_by_lua" ,
1311+ sizeof ("exit_worker_by_lua" )- 1 ,
1312+ & chunkname_len );
1313+ if (chunkname == NULL ) {
1314+ return NGX_CONF_ERROR ;
1315+ }
1316+
1317+ lmcf -> exit_worker_chunkname = chunkname ;
12561318 }
12571319
12581320 return NGX_CONF_OK ;
@@ -1296,13 +1358,18 @@ ngx_http_lua_set_by_lua_init(ngx_http_request_t *r)
12961358#endif
12971359
12981360
1299- static u_char *
1361+ u_char *
13001362ngx_http_lua_gen_chunk_name (ngx_conf_t * cf , const char * tag , size_t tag_len ,
13011363 size_t * chunkname_len )
13021364{
13031365 u_char * p , * out ;
13041366 size_t len ;
13051367 ngx_uint_t start_line ;
1368+ ngx_str_t * conf_prefix ;
1369+ ngx_str_t * filename ;
1370+ u_char * filename_end ;
1371+ const char * pre_str = "" ;
1372+ ngx_uint_t start_line_len ;
13061373
13071374 ngx_http_lua_main_conf_t * lmcf ;
13081375
@@ -1314,30 +1381,55 @@ ngx_http_lua_gen_chunk_name(ngx_conf_t *cf, const char *tag, size_t tag_len,
13141381 return NULL ;
13151382 }
13161383
1317- if (cf -> conf_file -> file .name .len ) {
1318- p = cf -> conf_file -> file .name .data + cf -> conf_file -> file .name .len ;
1319- while (-- p >= cf -> conf_file -> file .name .data ) {
1320- if (* p == '/' || * p == '\\' ) {
1321- p ++ ;
1384+ lmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_lua_module );
1385+ start_line = lmcf -> directive_line > 0
1386+ ? lmcf -> directive_line : cf -> conf_file -> line ;
1387+ p = ngx_snprintf (out , len , "%d" , start_line );
1388+ start_line_len = p - out ;
1389+
1390+ filename = & cf -> conf_file -> file .name ;
1391+ filename_end = filename -> data + filename -> len ;
1392+ if (filename -> len > 0 ) {
1393+ if (filename -> len >= 11 ) {
1394+ p = filename_end - 11 ;
1395+ if ((* p == '/' || * p == '\\' )
1396+ && ngx_memcmp (p , "/nginx.conf" , 11 ) == 0 )
1397+ {
1398+ p ++ ; /* now p is nginx.conf */
13221399 goto found ;
13231400 }
13241401 }
13251402
1326- p ++ ;
1403+ conf_prefix = & cf -> cycle -> conf_prefix ;
1404+ p = filename -> data + conf_prefix -> len ;
1405+ if ((conf_prefix -> len < filename -> len )
1406+ && ngx_memcmp (conf_prefix -> data ,
1407+ filename -> data , conf_prefix -> len ) == 0 )
1408+ {
1409+ /* files in conf_prefix directory, use the relative path */
1410+ if (filename_end - p + start_line_len > LJ_CHUNKNAME_MAX_LEN ) {
1411+ p = filename_end - LJ_CHUNKNAME_MAX_LEN + start_line_len + 3 ;
1412+ pre_str = "..." ;
1413+ }
13271414
1328- } else {
1329- p = cf -> conf_file -> file . name . data ;
1415+ goto found ;
1416+ }
13301417 }
13311418
1419+ p = filename -> data ;
1420+
1421+ if (filename -> len + start_line_len <= LJ_CHUNKNAME_MAX_LEN ) {
1422+ goto found ;
1423+ }
1424+
1425+ p = filename_end - LJ_CHUNKNAME_MAX_LEN + start_line_len + 3 ;
1426+ pre_str = "..." ;
1427+
13321428found :
13331429
1334- lmcf = ngx_http_conf_get_module_main_conf (cf , ngx_http_lua_module );
1335- start_line = lmcf -> directive_line > 0
1336- ? lmcf -> directive_line : cf -> conf_file -> line ;
13371430
1338- p = ngx_snprintf (out , len , "=%*s(%*s:%d)%Z" ,
1339- tag_len , tag , cf -> conf_file -> file .name .data
1340- + cf -> conf_file -> file .name .len - p ,
1431+ p = ngx_snprintf (out , len , "=%*s(%s%*s:%d)%Z" ,
1432+ tag_len , tag , pre_str , filename_end - p ,
13411433 p , start_line );
13421434
13431435 * chunkname_len = p - out - 1 ; /* exclude the trailing '\0' byte */
0 commit comments