Skip to content

Commit

Permalink
Merge pull request #513 from matsumotory/subrequest-response
Browse files Browse the repository at this point in the history
  • Loading branch information
matsumotory authored Oct 17, 2023
2 parents 39666b8 + 282c2a8 commit ef553d1
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 30 deletions.
21 changes: 20 additions & 1 deletion src/http/ngx_http_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
22 changes: 0 additions & 22 deletions src/http/ngx_http_mruby_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
7 changes: 0 additions & 7 deletions test/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ef553d1

Please sign in to comment.