Skip to content

Commit 767ad84

Browse files
committed
now we back-ported the subrequest mechanism of ngx_lua to ngx_echo. this also helps some crazy test cases of mixing echo_location and echo_location_async pass now :)
1 parent 25fc46f commit 767ad84

File tree

7 files changed

+46
-23
lines changed

7 files changed

+46
-23
lines changed

src/ngx_http_echo_echo.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ static ngx_buf_t ngx_http_echo_space_buf;
1111

1212
static ngx_buf_t ngx_http_echo_newline_buf;
1313

14+
1415
ngx_int_t
1516
ngx_http_echo_echo_init(ngx_conf_t *cf)
1617
{
@@ -20,19 +21,25 @@ ngx_http_echo_echo_init(ngx_conf_t *cf)
2021
dd("global init...");
2122

2223
ngx_memzero(&ngx_http_echo_space_buf, sizeof(ngx_buf_t));
24+
2325
ngx_http_echo_space_buf.memory = 1;
26+
2427
ngx_http_echo_space_buf.start =
2528
ngx_http_echo_space_buf.pos =
2629
space_str;
30+
2731
ngx_http_echo_space_buf.end =
2832
ngx_http_echo_space_buf.last =
2933
space_str + sizeof(space_str) - 1;
3034

3135
ngx_memzero(&ngx_http_echo_newline_buf, sizeof(ngx_buf_t));
36+
3237
ngx_http_echo_newline_buf.memory = 1;
38+
3339
ngx_http_echo_newline_buf.start =
3440
ngx_http_echo_newline_buf.pos =
3541
newline_str;
42+
3643
ngx_http_echo_newline_buf.end =
3744
ngx_http_echo_newline_buf.last =
3845
newline_str + sizeof(newline_str) - 1;
@@ -119,6 +126,7 @@ ngx_http_echo_exec_echo(ngx_http_request_t *r,
119126
cl->buf = buf;
120127
cl->next = NULL;
121128
ll = &cl->next;
129+
122130
} else {
123131
/* append a space first */
124132
*ll = ngx_alloc_chain_link(r->pool);

src/ngx_http_echo_handler.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ ngx_http_echo_wev_handler(ngx_http_request_t *r)
5050
return;
5151
}
5252

53+
dd("waiting: %d, done: %d", (int) ctx->waiting, (int) ctx->done);
54+
5355
if (ctx->waiting && ! ctx->done) {
5456
if (r == r->connection->data && r->postponed) {
5557
if (r->postponed->request) {
@@ -337,13 +339,17 @@ ngx_http_echo_post_subrequest(ngx_http_request_t *r,
337339
ngx_http_request_t *pr;
338340
ngx_http_echo_ctx_t *pr_ctx;
339341

340-
dd_enter();
342+
dd("echo post_subrequest: %.*s", (int) r->uri.len, r->uri.data);
341343

342-
#if 0
343344
if (ctx->run_post_subrequest) {
345+
dd("already run post_subrequest: %p: %.*s", ctx,
346+
(int) r->uri.len, r->uri.data);
347+
344348
return rc;
345349
}
346-
#endif
350+
351+
dd("setting run_post_subrequest to 1 for %p for %.*s", ctx,
352+
(int) r->uri.len, r->uri.data);
347353

348354
ctx->run_post_subrequest = 1;
349355

@@ -361,16 +367,17 @@ ngx_http_echo_post_subrequest(ngx_http_request_t *r,
361367

362368
pr->write_event_handler = ngx_http_echo_wev_handler;
363369

364-
/* ensure that the parent request is (or will be)
365-
* posted out the head of the r->posted_requests chain */
370+
/* work-around issues in nginx's event module */
366371

367-
if (r->main->posted_requests
368-
&& r->main->posted_requests->request != pr)
372+
if (r != r->connection->data && r->postponed &&
373+
(r->main->posted_requests == NULL ||
374+
r->main->posted_requests->request != pr))
369375
{
370-
rc = ngx_http_echo_post_request_at_head(pr, NULL);
371-
if (rc != NGX_OK) {
372-
return NGX_ERROR;
373-
}
376+
#if defined(nginx_version) && nginx_version >= 8012
377+
ngx_http_post_request(pr, NULL);
378+
#else
379+
ngx_http_post_request(pr);
380+
#endif
374381
}
375382

376383
return rc;

src/ngx_http_echo_location.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,7 @@ ngx_http_echo_exec_echo_location(ngx_http_request_t *r,
8888
ngx_http_post_subrequest_t *psr;
8989
ngx_str_t args;
9090
ngx_uint_t flags = 0;
91-
92-
93-
dd_enter();
91+
ngx_http_echo_ctx_t *sr_ctx;
9492

9593
computed_arg_elts = computed_args->elts;
9694

@@ -124,14 +122,16 @@ ngx_http_echo_exec_echo_location(ngx_http_request_t *r,
124122
return rc;
125123
}
126124

125+
sr_ctx = ngx_http_echo_create_ctx(r);
126+
127127
psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
128128

129129
if (psr == NULL) {
130130
return NGX_HTTP_INTERNAL_SERVER_ERROR;
131131
}
132132

133133
psr->handler = ngx_http_echo_post_subrequest;
134-
psr->data = ctx;
134+
psr->data = sr_ctx;
135135

136136
rc = ngx_http_subrequest(r, &location, url_args, &sr, psr, 0);
137137

src/ngx_http_echo_subrequest.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ ngx_http_echo_exec_echo_subrequest_async(ngx_http_request_t *r,
5757
ngx_str_t args;
5858
ngx_uint_t flags = 0;
5959

60-
6160
dd_enter();
6261

6362
rc = ngx_http_echo_parse_subrequest_spec(r, computed_args, &parsed_sr);
@@ -152,6 +151,9 @@ ngx_http_echo_exec_echo_subrequest(ngx_http_request_t *r,
152151
* sr_ctx->run_post_subrequest = 0
153152
*/
154153

154+
dd("creating sr ctx for %.*s: %p", (int) parsed_sr->location->len,
155+
parsed_sr->location->data, sr_ctx);
156+
155157
psr = ngx_palloc(r->pool, sizeof(ngx_http_post_subrequest_t));
156158

157159
if (psr == NULL) {

t/echo-sleep.t

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,12 @@ done
161161
location /quit {
162162
echo before;
163163
echo_flush;
164-
echo_sleep 5;
164+
echo_sleep 1;
165165
echo after;
166166
}
167167
--- request
168168
GET /quit
169169
--- response_body
170170
before
171-
--- timeout: 1
172-
--- SKIP
171+
after
173172

t/location-async.t

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ post main
304304

305305

306306

307-
=== TEST 17: access/deny
307+
=== TEST 17: access/deny (access phase handlers skipped in subrequests)
308308
--- config
309309
location /main {
310310
echo_location_async /denied;
@@ -315,9 +315,9 @@ post main
315315
}
316316
--- request
317317
GET /main
318-
--- error_code: 403
318+
--- error_code: 200
319319
--- response_body
320-
--- SKIP
320+
No no no
321321

322322

323323

t/location.t

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,6 @@ GET /main
437437
6
438438
7
439439
--- timeout: 2
440-
--- SKIP
441440

442441

443442

@@ -522,6 +521,14 @@ hi(world);
522521
echo_location /foo;
523522
echo_location /bar;
524523
}
524+
location /main2 {
525+
content_by_lua '
526+
local res = ngx.location.capture("/foo")
527+
local res2 = ngx.location.capture("/bar")
528+
ngx.say(res.body)
529+
ngx.say(res2.body)
530+
';
531+
}
525532
location /foo {
526533
echo -n world;
527534
}

0 commit comments

Comments
 (0)