From 282c2a80b8adf98f91634bf4f42132a69574f7fd Mon Sep 17 00:00:00 2001 From: pyama Date: Tue, 17 Oct 2023 15:22:35 +0900 Subject: [PATCH] The return value of a subrequest can be retrieved immediately. --- src/http/ngx_http_mruby_async.c | 21 ++++++++++++++++++++- src/http/ngx_http_mruby_core.c | 22 ---------------------- test/conf/nginx.conf | 7 ------- 3 files changed, 20 insertions(+), 30 deletions(-) diff --git a/src/http/ngx_http_mruby_async.c b/src/http/ngx_http_mruby_async.c index 4dc2ccb6..175481b3 100644 --- a/src/http/ngx_http_mruby_async.c +++ b/src/http/ngx_http_mruby_async.c @@ -267,6 +267,8 @@ static ngx_int_t ngx_mrb_async_http_sub_request_done(ngx_http_request_t *sr, voi ngx_mrb_async_http_ctx_t *actx = data; ngx_mrb_reentrant_t *re = actx->re; ngx_http_mruby_ctx_t *ctx; + ngx_buf_t *buffer = NULL; + ngx_int_t body_len = 0; re->sr = sr; re->r = sr->parent; @@ -279,10 +281,27 @@ static ngx_int_t ngx_mrb_async_http_sub_request_done(ngx_http_request_t *sr, voi ngx_log_debug1(NGX_LOG_DEBUG_HTTP, sr->parent->connection->log, 0, "http_sub_request done s:%ui", sr->parent->headers_out.status); + ctx->sub_response_more = 0; - rc = ngx_mrb_post_fiber(re, ctx); + if (sr->upstream) { + buffer = &sr->upstream->buffer; + } else if(sr->out){ + buffer = sr->out->buf; + } + if(buffer != NULL) { + if (buffer->pos != buffer->last) { + body_len = buffer->last - buffer->pos; + } + ctx->sub_response_body = ngx_palloc(re->r->pool, body_len); + ngx_memcpy(ctx->sub_response_body, buffer->pos, body_len); + ctx->sub_response_body_length = body_len; + ctx->sub_response_status = sr->headers_out.status; + ctx->sub_response_headers = sr->headers_out; + } + + rc = ngx_mrb_post_fiber(re, ctx); if (rc != NGX_DECLINED && rc != NGX_OK) { ngx_http_finalize_request(re->r, rc); return NGX_DONE; diff --git a/src/http/ngx_http_mruby_core.c b/src/http/ngx_http_mruby_core.c index 0c007078..eafb9e78 100644 --- a/src/http/ngx_http_mruby_core.c +++ b/src/http/ngx_http_mruby_core.c @@ -115,23 +115,6 @@ ngx_int_t ngx_mrb_finalize_rputs(ngx_http_request_t *r, ngx_http_mruby_ctx_t *ct return rc; } -static void ngx_http_mrb_read_subrequest_responce(ngx_http_request_t *r, ngx_http_mruby_ctx_t *ctx) -{ - ngx_http_mruby_ctx_t *main_ctx; - main_ctx = ngx_mrb_http_get_module_ctx(NULL, r->main); - - ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "%s DEBUG %s:%d: r->main parse subrequest response", MODULE_NAME, - __func__, __LINE__); - - if (main_ctx != NULL && ctx->body_length > 0) { - main_ctx->sub_response_body = ngx_palloc(r->pool, ctx->body_length); - ngx_memcpy(main_ctx->sub_response_body, ctx->body, ctx->body_length); - main_ctx->sub_response_body_length = ctx->body_length; - main_ctx->sub_response_status = r->headers_out.status; - main_ctx->sub_response_headers = r->headers_out; - } -} - ngx_int_t ngx_mrb_finalize_body_filter(ngx_http_request_t *r, ngx_http_mruby_ctx_t *ctx) { ngx_buf_t *b; @@ -165,11 +148,6 @@ ngx_int_t ngx_mrb_finalize_body_filter(ngx_http_request_t *r, ngx_http_mruby_ctx ngx_log_error(NGX_LOG_DEBUG, r->connection->log, 0, "%s DEBUG %s:%d: data after body length: %uz", MODULE_NAME, __func__, __LINE__, ctx->body_length); - if (r->parent != NULL && r != r->parent) { - ngx_http_mrb_read_subrequest_responce(r, ctx); - return NGX_OK; - } - rc = ngx_http_next_header_filter(r); if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { diff --git a/test/conf/nginx.conf b/test/conf/nginx.conf index 001d7663..925a2c7a 100644 --- a/test/conf/nginx.conf +++ b/test/conf/nginx.conf @@ -831,9 +831,6 @@ http { # if subreqeust_in_memory limit reached(default 4k/8k), # increased the memory size by subrequest_output_buffer_size proxy_pass http://127.0.0.1:58081/; - mruby_output_body_filter_code ' - Nginx.log Nginx::LOG_INFO, "read subrequest proxy pass" - '; } location /async_http_sub_request_with_proxy_pass { @@ -888,10 +885,6 @@ http { mruby_content_handler_code ' Nginx.rputs Nginx::Request.new.uri_args.inspect '; - mruby_output_body_filter_code ' - Nginx.log Nginx::LOG_INFO, "read subrequest body" - '; - } location /async_http_sub_request {