Skip to content

Commit 8e0e8b2

Browse files
committed
Merge branch 'feature_server_rewrite' of github.com:xiaobiaozhao/lua-nginx-module into feature_server_rewrite
2 parents 3f050db + d7ac596 commit 8e0e8b2

12 files changed

+167
-148
lines changed

README.markdown

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -973,7 +973,7 @@ TODO
973973
* cosocket: review and merge aviramc's [patch](https://github.com/openresty/lua-nginx-module/pull/290) for adding the `bsdrecv` method.
974974
* cosocket: add configure options for different strategies of handling the cosocket connection exceeding in the pools.
975975
* review and apply vadim-pavlov's patch for [ngx.location.capture](#ngxlocationcapture)'s `extra_headers` option
976-
* use `ngx_hash_t` to optimize the built-in header look-up process for [ngx.req.set_header](#ngxreqset_header), [ngx.header.HEADER](#ngxheaderheader), and etc.
976+
* use `ngx_hash_t` to optimize the built-in header look-up process for [ngx.req.set_header](#ngxreqset_header), and etc.
977977
* add `ignore_resp_headers`, `ignore_resp_body`, and `ignore_resp` options to [ngx.location.capture](#ngxlocationcapture) and [ngx.location.capture_multi](#ngxlocationcapture_multi) methods, to allow micro performance tuning on the user side.
978978
* add automatic Lua code time slicing support by yielding and resuming the Lua VM actively via Lua's debug hooks.
979979
* add `stat` mode similar to [mod_lua](https://httpd.apache.org/docs/trunk/mod/mod_lua.html).
@@ -2741,14 +2741,7 @@ packages](https://openresty.org/en/linux-packages.html) for
27412741
[OpenResty](https://openresty.org/) 1.21.4.1 or later, then everything should
27422742
work out of the box.
27432743

2744-
If you are not using one of the [OpenSSL
2745-
packages](https://openresty.org/en/linux-packages.html) provided by
2746-
[OpenResty](https://openresty.org), you will need to apply patches to OpenSSL
2747-
in order to use this directive:
2748-
2749-
<https://openresty.org/en/openssl-patches.html>
2750-
2751-
Similarly, if you are not using the Nginx core shipped with
2744+
If you are not using the Nginx core shipped with
27522745
[OpenResty](https://openresty.org) 1.21.4.1 or later, you will need to apply
27532746
patches to the standard Nginx core:
27542747

@@ -2858,14 +2851,7 @@ packages](https://openresty.org/en/linux-packages.html) for
28582851
[OpenResty](https://openresty.org/) 1.9.7.2 or later, then everything should
28592852
work out of the box.
28602853

2861-
If you are not using one of the [OpenSSL
2862-
packages](https://openresty.org/en/linux-packages.html) provided by
2863-
[OpenResty](https://openresty.org), you will need to apply patches to OpenSSL
2864-
in order to use this directive:
2865-
2866-
<https://openresty.org/en/openssl-patches.html>
2867-
2868-
Similarly, if you are not using the Nginx core shipped with
2854+
If you are not using the Nginx core shipped with
28692855
[OpenResty](https://openresty.org) 1.9.7.2 or later, you will need to apply
28702856
patches to the standard Nginx core:
28712857

doc/HttpLuaModule.wiki

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,7 @@ phases.
808808
* cosocket: review and merge aviramc's [https://github.com/openresty/lua-nginx-module/pull/290 patch] for adding the <code>bsdrecv</code> method.
809809
* cosocket: add configure options for different strategies of handling the cosocket connection exceeding in the pools.
810810
* review and apply vadim-pavlov's patch for [[#ngx.location.capture|ngx.location.capture]]'s <code>extra_headers</code> option
811-
* use <code>ngx_hash_t</code> to optimize the built-in header look-up process for [[#ngx.req.set_header|ngx.req.set_header]], [[#ngx.header.HEADER|ngx.header.HEADER]], and etc.
811+
* use <code>ngx_hash_t</code> to optimize the built-in header look-up process for [[#ngx.req.set_header|ngx.req.set_header]], and etc.
812812
* add <code>ignore_resp_headers</code>, <code>ignore_resp_body</code>, and <code>ignore_resp</code> options to [[#ngx.location.capture|ngx.location.capture]] and [[#ngx.location.capture_multi|ngx.location.capture_multi]] methods, to allow micro performance tuning on the user side.
813813
* add automatic Lua code time slicing support by yielding and resuming the Lua VM actively via Lua's debug hooks.
814814
* add <code>stat</code> mode similar to [https://httpd.apache.org/docs/trunk/mod/mod_lua.html mod_lua].
@@ -2404,14 +2404,7 @@ packages](https://openresty.org/en/linux-packages.html) for
24042404
[OpenResty](https://openresty.org/) 1.21.4.1 or later, then everything should
24052405
work out of the box.
24062406
2407-
If you are not using one of the [OpenSSL
2408-
packages](https://openresty.org/en/linux-packages.html) provided by
2409-
[OpenResty](https://openresty.org), you will need to apply patches to OpenSSL
2410-
in order to use this directive:
2411-
2412-
https://openresty.org/en/openssl-patches.html
2413-
2414-
Similarly, if you are not using the Nginx core shipped with
2407+
If you are not using the Nginx core shipped with
24152408
[OpenResty](https://openresty.org) 1.21.4.1 or later, you will need to apply
24162409
patches to the standard Nginx core:
24172410
@@ -2514,14 +2507,7 @@ packages](https://openresty.org/en/linux-packages.html) for
25142507
[OpenResty](https://openresty.org/) 1.9.7.2 or later, then everything should
25152508
work out of the box.
25162509
2517-
If you are not using one of the [OpenSSL
2518-
packages](https://openresty.org/en/linux-packages.html) provided by
2519-
[OpenResty](https://openresty.org), you will need to apply patches to OpenSSL
2520-
in order to use this directive:
2521-
2522-
https://openresty.org/en/openssl-patches.html
2523-
2524-
Similarly, if you are not using the Nginx core shipped with
2510+
If you are not using the Nginx core shipped with
25252511
[OpenResty](https://openresty.org) 1.9.7.2 or later, you will need to apply
25262512
patches to the standard Nginx core:
25272513

src/ngx_http_lua_bodyfilterby.c

Lines changed: 47 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -396,60 +396,58 @@ ngx_http_lua_body_filter_init(void)
396396

397397

398398
int
399-
ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r)
399+
ngx_http_lua_ffi_get_body_filter_param_eof(ngx_http_request_t *r)
400400
{
401-
u_char *data, *p;
402-
size_t size;
403401
ngx_chain_t *cl;
404-
ngx_buf_t *b;
405-
int idx;
406402
ngx_chain_t *in;
407403

408404
ngx_http_lua_main_conf_t *lmcf;
409405

410-
idx = luaL_checkint(L, 2);
406+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
407+
in = lmcf->body_filter_chain;
411408

412-
dd("index: %d", idx);
409+
/* asking for the eof argument */
413410

414-
if (idx != 1 && idx != 2) {
415-
lua_pushnil(L);
416-
return 1;
411+
for (cl = in; cl; cl = cl->next) {
412+
if (cl->buf->last_buf || cl->buf->last_in_chain) {
413+
return 1;
414+
}
417415
}
418416

419-
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
420-
in = lmcf->body_filter_chain;
417+
return 0;
418+
}
421419

422-
if (idx == 2) {
423-
/* asking for the eof argument */
424420

425-
for (cl = in; cl; cl = cl->next) {
426-
if (cl->buf->last_buf || cl->buf->last_in_chain) {
427-
lua_pushboolean(L, 1);
428-
return 1;
429-
}
430-
}
421+
int
422+
ngx_http_lua_ffi_get_body_filter_param_body(ngx_http_request_t *r,
423+
u_char **data_p, size_t *len_p)
424+
{
425+
size_t size;
426+
ngx_chain_t *cl;
427+
ngx_buf_t *b;
428+
ngx_chain_t *in;
431429

432-
lua_pushboolean(L, 0);
433-
return 1;
434-
}
430+
ngx_http_lua_main_conf_t *lmcf;
435431

436-
/* idx == 1 */
432+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
433+
in = lmcf->body_filter_chain;
437434

438435
size = 0;
439436

440437
if (in == NULL) {
441438
/* being a cleared chain on the Lua land */
442-
lua_pushliteral(L, "");
443-
return 1;
439+
*len_p = 0;
440+
return NGX_OK;
444441
}
445442

446443
if (in->next == NULL) {
447444

448445
dd("seen only single buffer");
449446

450447
b = in->buf;
451-
lua_pushlstring(L, (char *) b->pos, b->last - b->pos);
452-
return 1;
448+
*data_p = b->pos;
449+
*len_p = b->last - b->pos;
450+
return NGX_OK;
453451
}
454452

455453
dd("seen multiple buffers");
@@ -464,7 +462,26 @@ ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r)
464462
}
465463
}
466464

467-
data = (u_char *) lua_newuserdata(L, size);
465+
/* the buf is need and is not allocated from Lua land yet, return with
466+
* the actual size */
467+
*len_p = size;
468+
return NGX_AGAIN;
469+
}
470+
471+
472+
int
473+
ngx_http_lua_ffi_copy_body_filter_param_body(ngx_http_request_t *r,
474+
u_char *data)
475+
{
476+
u_char *p;
477+
ngx_chain_t *cl;
478+
ngx_buf_t *b;
479+
ngx_chain_t *in;
480+
481+
ngx_http_lua_main_conf_t *lmcf;
482+
483+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
484+
in = lmcf->body_filter_chain;
468485

469486
for (p = data, cl = in; cl; cl = cl->next) {
470487
b = cl->buf;
@@ -475,8 +492,7 @@ ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r)
475492
}
476493
}
477494

478-
lua_pushlstring(L, (char *) data, size);
479-
return 1;
495+
return NGX_OK;
480496
}
481497

482498

src/ngx_http_lua_bodyfilterby.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ ngx_int_t ngx_http_lua_body_filter_inline(ngx_http_request_t *r,
2121
ngx_chain_t *in);
2222
ngx_int_t ngx_http_lua_body_filter_file(ngx_http_request_t *r,
2323
ngx_chain_t *in);
24-
int ngx_http_lua_body_filter_param_get(lua_State *L, ngx_http_request_t *r);
2524
int ngx_http_lua_body_filter_param_set(lua_State *L, ngx_http_request_t *r,
2625
ngx_http_lua_ctx_t *ctx);
2726

src/ngx_http_lua_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@ struct ngx_http_lua_main_conf_s {
215215

216216
ngx_int_t lua_thread_cache_max_entries;
217217

218+
ngx_hash_t builtin_headers_out;
219+
218220
#if (NGX_PCRE)
219221
ngx_int_t regex_cache_entries;
220222
ngx_int_t regex_cache_max_entries;

src/ngx_http_lua_headers_out.c

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -486,8 +486,9 @@ ngx_http_lua_set_output_header(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx,
486486
ngx_str_t key, ngx_str_t value, unsigned override)
487487
{
488488
ngx_http_lua_header_val_t hv;
489-
ngx_http_lua_set_header_t *handlers = ngx_http_lua_set_handlers;
490-
ngx_uint_t i;
489+
ngx_http_lua_main_conf_t *lmcf;
490+
ngx_http_lua_set_header_t *lsh;
491+
ngx_hash_t *hash;
491492

492493
dd("set header value: %.*s", (int) value.len, value.data);
493494

@@ -499,53 +500,25 @@ ngx_http_lua_set_output_header(ngx_http_request_t *r, ngx_http_lua_ctx_t *ctx,
499500
return NGX_ERROR;
500501
}
501502

502-
if (value.len > 0) {
503-
hv.hash = ngx_hash_key_lc(key.data, key.len);
504-
505-
} else {
506-
hv.hash = 0;
507-
}
508-
503+
hv.hash = ngx_hash_key_lc(key.data, key.len);
509504
hv.key = key;
510505

511506
hv.offset = 0;
512507
hv.no_override = !override;
513-
hv.handler = NULL;
514-
515-
for (i = 0; handlers[i].name.len; i++) {
516-
if (hv.key.len != handlers[i].name.len
517-
|| ngx_strncasecmp(hv.key.data, handlers[i].name.data,
518-
handlers[i].name.len) != 0)
519-
{
520-
dd("hv key comparison: %s <> %s", handlers[i].name.data,
521-
hv.key.data);
522-
523-
continue;
524-
}
525-
526-
dd("Matched handler: %s %s", handlers[i].name.data, hv.key.data);
527-
528-
hv.offset = handlers[i].offset;
529-
hv.handler = handlers[i].handler;
530-
508+
hv.handler = ngx_http_set_header;
509+
510+
lmcf = ngx_http_get_module_main_conf(r, ngx_http_lua_module);
511+
hash = &lmcf->builtin_headers_out;
512+
lsh = ngx_http_lua_hash_find_lc(hash, hv.hash, hv.key.data, hv.key.len);
513+
if (lsh) {
514+
dd("Matched handler: %s %s", lsh->name.data, hv.key.data);
515+
hv.offset = lsh->offset;
516+
hv.handler = lsh->handler;
531517
if (hv.handler == ngx_http_set_content_type_header) {
532518
ctx->mime_set = 1;
533519
}
534-
535-
break;
536-
}
537-
538-
if (handlers[i].name.len == 0 && handlers[i].handler) {
539-
hv.offset = handlers[i].offset;
540-
hv.handler = handlers[i].handler;
541520
}
542521

543-
#if 1
544-
if (hv.handler == NULL) {
545-
return NGX_ERROR;
546-
}
547-
#endif
548-
549522
return hv.handler(r, &hv, &value);
550523
}
551524

@@ -658,4 +631,48 @@ ngx_http_lua_get_output_header(lua_State *L, ngx_http_request_t *r,
658631
return 1;
659632
}
660633

634+
635+
ngx_int_t
636+
ngx_http_lua_init_builtin_headers_out(ngx_conf_t *cf,
637+
ngx_http_lua_main_conf_t *lmcf)
638+
{
639+
ngx_array_t headers;
640+
ngx_hash_key_t *hk;
641+
ngx_hash_init_t hash;
642+
ngx_http_lua_set_header_t *handlers = ngx_http_lua_set_handlers;
643+
ngx_uint_t count;
644+
645+
count = sizeof(ngx_http_lua_set_handlers)
646+
/ sizeof(ngx_http_lua_set_header_t);
647+
648+
if (ngx_array_init(&headers, cf->temp_pool, count, sizeof(ngx_hash_key_t))
649+
!= NGX_OK)
650+
{
651+
return NGX_ERROR;
652+
}
653+
654+
while (handlers->name.data) {
655+
hk = ngx_array_push(&headers);
656+
if (hk == NULL) {
657+
return NGX_ERROR;
658+
}
659+
660+
hk->key = handlers->name;
661+
hk->key_hash = ngx_hash_key_lc(handlers->name.data, handlers->name.len);
662+
hk->value = (void *) handlers;
663+
664+
handlers++;
665+
}
666+
667+
hash.hash = &lmcf->builtin_headers_out;
668+
hash.key = ngx_hash_key_lc;
669+
hash.max_size = 512;
670+
hash.bucket_size = ngx_align(64, ngx_cacheline_size);
671+
hash.name = "builtin_headers_out_hash";
672+
hash.pool = cf->pool;
673+
hash.temp_pool = NULL;
674+
675+
return ngx_hash_init(&hash, headers.elts, headers.nelts);
676+
}
677+
661678
/* vi:set ft=c ts=4 sw=4 et fdm=marker: */

src/ngx_http_lua_headers_out.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ ngx_int_t ngx_http_lua_set_output_header(ngx_http_request_t *r,
1616
ngx_http_lua_ctx_t *ctx, ngx_str_t key, ngx_str_t value, unsigned override);
1717
int ngx_http_lua_get_output_header(lua_State *L, ngx_http_request_t *r,
1818
ngx_http_lua_ctx_t *ctx, ngx_str_t *key);
19+
ngx_int_t ngx_http_lua_init_builtin_headers_out(ngx_conf_t *cf,
20+
ngx_http_lua_main_conf_t *lmcf);
1921

2022

2123
#endif /* _NGX_HTTP_LUA_HEADERS_OUT_H_INCLUDED_ */

src/ngx_http_lua_module.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "ngx_http_lua_ssl_session_storeby.h"
3333
#include "ngx_http_lua_ssl_session_fetchby.h"
3434
#include "ngx_http_lua_headers.h"
35+
#include "ngx_http_lua_headers_out.h"
3536
#include "ngx_http_lua_pipe.h"
3637

3738

@@ -1113,6 +1114,15 @@ ngx_http_lua_init_main_conf(ngx_conf_t *cf, void *conf)
11131114
lmcf->worker_thread_vm_pool_size = 100;
11141115
}
11151116

1117+
if (ngx_http_lua_init_builtin_headers_out(cf, lmcf) != NGX_OK) {
1118+
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "init header out error");
1119+
1120+
return NGX_CONF_ERROR;
1121+
}
1122+
1123+
dd("init built in headers out hash size: %ld",
1124+
lmcf->builtin_headers_out.size);
1125+
11161126
return NGX_CONF_OK;
11171127
}
11181128

0 commit comments

Comments
 (0)