Skip to content

Commit e8f65dc

Browse files
bugfix: didn't flush send buffer after lua phase(access/rewrite/server_rewrite) done.
1 parent 004922e commit e8f65dc

File tree

4 files changed

+192
-7
lines changed

4 files changed

+192
-7
lines changed

src/ngx_http_lua_accessby.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ ngx_http_lua_access_handler(ngx_http_request_t *r)
9595

9696
dd("entered? %d", (int) ctx->entered_access_phase);
9797

98+
9899
if (ctx->entered_access_phase) {
99100
dd("calling wev handler");
100101
rc = ctx->resume_handler(r);
@@ -105,7 +106,7 @@ ngx_http_lua_access_handler(ngx_http_request_t *r)
105106
}
106107

107108
if (rc == NGX_OK) {
108-
if (r->header_sent) {
109+
if (r->header_sent || r->headers_out.status!= 0) {
109110
dd("header already sent");
110111

111112
/* response header was already generated in access_by_lua*,
@@ -369,7 +370,7 @@ ngx_http_lua_access_by_chunk(lua_State *L, ngx_http_request_t *r)
369370

370371
#if 1
371372
if (rc == NGX_OK) {
372-
if (r->header_sent) {
373+
if (r->header_sent || r->headers_out.status != 0) {
373374
dd("header already sent");
374375

375376
/* response header was already generated in access_by_lua*,

src/ngx_http_lua_rewriteby.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ ngx_http_lua_rewrite_handler(ngx_http_request_t *r)
107107
}
108108

109109
if (rc == NGX_DECLINED) {
110-
if (r->header_sent) {
110+
if (r->header_sent || r->headers_out.status != 0) {
111111
dd("header already sent");
112112

113113
/* response header was already generated in rewrite_by_lua*,
@@ -349,7 +349,7 @@ ngx_http_lua_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
349349
}
350350

351351
if (rc == NGX_OK || rc == NGX_DECLINED) {
352-
if (r->header_sent) {
352+
if (r->header_sent || r->headers_out.status != 0) {
353353
dd("header already sent");
354354

355355
/* response header was already generated in rewrite_by_lua*,

src/ngx_http_lua_server_rewriteby.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ ngx_http_lua_server_rewrite_handler(ngx_http_request_t *r)
6969
}
7070

7171
if (rc == NGX_DECLINED) {
72-
if (r->header_sent) {
72+
if (r->header_sent || r->headers_out.status != 0) {
7373
dd("header already sent");
7474

7575
/* response header was already generated in rewrite_by_lua*,
@@ -307,7 +307,7 @@ ngx_http_lua_server_rewrite_by_chunk(lua_State *L, ngx_http_request_t *r)
307307
}
308308

309309
if (rc == NGX_OK || rc == NGX_DECLINED) {
310-
if (r->header_sent) {
310+
if (r->header_sent || r->headers_out.status != 0) {
311311
dd("header already sent");
312312

313313
/* response header was already generated in rewrite_by_lua*,

t/015-status.t

Lines changed: 185 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ log_level('warn');
99
#repeat_each(120);
1010
repeat_each(2);
1111

12-
plan tests => repeat_each() * (blocks() * 2 + 10);
12+
plan tests => repeat_each() * (blocks() * 3 + 2);
1313

1414
#no_diff();
1515
#no_long_string();
@@ -29,6 +29,8 @@ __DATA__
2929
GET /nil
3030
--- response_body
3131
nil
32+
--- no_error_log
33+
[error]
3234
3335
3436
@@ -43,6 +45,8 @@ nil
4345
GET /nil
4446
--- response_body
4547
not nil
48+
--- no_error_log
49+
[error]
4650
4751
4852
@@ -57,6 +61,8 @@ not nil
5761
GET /nil
5862
--- response_body
5963
0
64+
--- no_error_log
65+
[error]
6066
6167
6268
@@ -73,6 +79,8 @@ GET /nil
7379
--- response_body
7480
blah
7581
200
82+
--- no_error_log
83+
[error]
7684
7785
7886
@@ -89,6 +97,8 @@ GET /201
8997
--- response_body
9098
created
9199
--- error_code: 201
100+
--- no_error_log
101+
[error]
92102
93103
94104
@@ -105,6 +115,8 @@ GET /201
105115
--- response_body
106116
created
107117
--- error_code: 201
118+
--- no_error_log
119+
[error]
108120
109121
110122
@@ -121,6 +133,8 @@ GET /201
121133
--- response_body
122134
created
123135
--- error_code: 201
136+
--- no_error_log
137+
[error]
124138
125139
126140
@@ -136,6 +150,8 @@ created
136150
GET /201
137151
--- response_body_like: 500 Internal Server Error
138152
--- error_code: 500
153+
--- no_error_log
154+
[crit]
139155
140156
141157
@@ -248,6 +264,8 @@ GET /t
248264
--- http09
249265
--- response_body
250266
status = 9
267+
--- no_error_log
268+
[error]
251269
252270
253271
@@ -345,3 +363,169 @@ GET /t
345363
match
346364
--- no_error_log
347365
[error]
366+
367+
368+
369+
=== TEST 18: set ngx.status in server_rewrite_by_lua_block
370+
don't proxy_pass to upstream
371+
--- config
372+
server_rewrite_by_lua_block {
373+
if ngx.var.uri == "/t" then
374+
ngx.status = 403
375+
ngx.say("Hello World")
376+
end
377+
}
378+
379+
location /t {
380+
proxy_pass http://127.0.0.1:$TEST_NGINX_SERVER_PORT/u;
381+
}
382+
383+
location /u {
384+
content_by_lua_block {
385+
ngx.say("From upstream")
386+
}
387+
}
388+
--- request
389+
GET /t HTTP/1.0
390+
--- response_body
391+
Hello World
392+
--- error_code: 403
393+
--- no_error_log
394+
[error]
395+
396+
397+
398+
=== TEST 19: set ngx.status in rewrite_by_lua_block
399+
don't proxy_pass to upstream
400+
--- config
401+
location /t {
402+
rewrite_by_lua_block {
403+
ngx.status = 403
404+
ngx.say("Hello World")
405+
}
406+
proxy_pass http://127.0.0.1:$TEST_NGINX_SERVER_PORT/u;
407+
}
408+
409+
location /u {
410+
content_by_lua_block {
411+
ngx.say("From upstream")
412+
}
413+
}
414+
--- request
415+
GET /t HTTP/1.0
416+
--- response_body
417+
Hello World
418+
--- error_code: 403
419+
--- no_error_log
420+
[error]
421+
422+
423+
424+
=== TEST 20: set ngx.status in access_by_lua_block
425+
don't proxy_pass to upstream
426+
--- config
427+
location /t {
428+
access_by_lua_block {
429+
ngx.status = 403
430+
ngx.say("Hello World")
431+
}
432+
proxy_pass http://127.0.0.1:$TEST_NGINX_SERVER_PORT/u;
433+
}
434+
435+
location /u {
436+
content_by_lua_block {
437+
ngx.say("From upstream")
438+
}
439+
}
440+
--- request
441+
GET /t HTTP/1.0
442+
--- response_body
443+
Hello World
444+
--- error_code: 403
445+
--- no_error_log
446+
[error]
447+
448+
449+
450+
=== TEST 21: set ngx.status in server_rewrite_by_lua_block with sleep
451+
don't proxy_pass to upstream
452+
--- config
453+
server_rewrite_by_lua_block {
454+
if ngx.var.uri == "/t" then
455+
ngx.sleep(0.001)
456+
ngx.status = 403
457+
ngx.say("Hello World")
458+
end
459+
}
460+
461+
location /t {
462+
proxy_pass http://127.0.0.1:$TEST_NGINX_SERVER_PORT/u;
463+
}
464+
465+
location /u {
466+
content_by_lua_block {
467+
ngx.say("From upstream")
468+
}
469+
}
470+
--- request
471+
GET /t HTTP/1.0
472+
--- response_body
473+
Hello World
474+
--- error_code: 403
475+
--- no_error_log
476+
[error]
477+
478+
479+
480+
=== TEST 22: set ngx.status in rewrite_by_lua_block with sleep
481+
don't proxy_pass to upstream
482+
--- config
483+
location /t {
484+
rewrite_by_lua_block {
485+
ngx.sleep(0.001)
486+
ngx.status = 403
487+
ngx.say("Hello World")
488+
}
489+
490+
proxy_pass http://127.0.0.1:$TEST_NGINX_SERVER_PORT/u;
491+
}
492+
493+
location /u {
494+
content_by_lua_block {
495+
ngx.say("From upstream")
496+
}
497+
}
498+
--- request
499+
GET /t HTTP/1.0
500+
--- response_body
501+
Hello World
502+
--- error_code: 403
503+
--- no_error_log
504+
[error]
505+
506+
507+
508+
=== TEST 23: set ngx.status in access_by_lua_block
509+
don't proxy_pass to upstream
510+
--- config
511+
location /t {
512+
access_by_lua_block {
513+
ngx.sleep(0.001)
514+
ngx.status = 403
515+
ngx.say("Hello World")
516+
}
517+
proxy_pass http://127.0.0.1:$TEST_NGINX_SERVER_PORT/u;
518+
}
519+
520+
location /u {
521+
content_by_lua_block {
522+
ngx.say("From upstream")
523+
}
524+
}
525+
--- request
526+
GET /t HTTP/1.0
527+
--- response_body
528+
Hello World
529+
--- error_code: 403
530+
--- no_error_log
531+
[error]

0 commit comments

Comments
 (0)