Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When subrequest is used, set value is not used. #506

Merged
merged 2 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/http/ngx_http_mruby_async.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ typedef struct {
mrb_value *fiber;
ngx_http_request_t *r;
ngx_uint_t fiber_prev_status;
ngx_http_request_t *sr;
} ngx_mrb_reentrant_t;

typedef struct {
Expand Down Expand Up @@ -116,7 +117,7 @@ static ngx_int_t ngx_mrb_post_fiber(ngx_mrb_reentrant_t *re, ngx_http_mruby_ctx_
if (re->mrb->exc) {
ngx_mrb_raise_error(re->mrb, mrb_obj_value(re->mrb->exc), re->r);
rc = NGX_HTTP_INTERNAL_SERVER_ERROR;
} else if (ctx->set_var_target.len > 1) {
} else if (re->sr == NULL && ctx->set_var_target.len > 1) {
if (ctx->set_var_target.data[0] != '$') {
ngx_log_error(NGX_LOG_NOTICE, re->r->connection->log, 0,
"%s NOTICE %s:%d: invalid variable name error name: %s", MODULE_NAME, __func__, __LINE__,
Expand Down Expand Up @@ -200,6 +201,7 @@ static mrb_value ngx_mrb_async_sleep(mrb_state *mrb, mrb_value self)
re->mrb = mrb;
re->fiber_prev_status = r->headers_out.status;
re->r = r;
re->sr = NULL;

ctx = ngx_mrb_http_get_module_ctx(mrb, r);
re->fiber = ctx->fiber_proc;
Expand Down Expand Up @@ -267,7 +269,9 @@ static ngx_int_t ngx_mrb_async_http_sub_request_done(ngx_http_request_t *sr, voi
ngx_mrb_reentrant_t *re = actx->re;
ngx_http_mruby_ctx_t *ctx;

re->sr = sr;
re->r = sr->parent;

// read mruby context of parent request_rec
ctx = ngx_mrb_http_get_module_ctx(NULL, sr->parent);
if (ctx == NULL) {
Expand Down Expand Up @@ -326,6 +330,7 @@ static mrb_value ngx_mrb_async_http_sub_request(mrb_state *mrb, mrb_value self)
re = (ngx_mrb_reentrant_t *)ngx_palloc(r->pool, sizeof(ngx_mrb_reentrant_t));
re->mrb = mrb;
re->fiber_prev_status = r->headers_out.status;
re->sr = NULL;

ctx = ngx_mrb_http_get_module_ctx(mrb, r);
re->fiber = ctx->fiber_proc;
Expand Down
11 changes: 11 additions & 0 deletions test/conf/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ http {
server_name localhost;
root __NGXDOCROOT__;

mruby_set_code $subrequest_after_backend '"127.0.0.1:58081"';

# test for hello world and cache option
location /mruby {
mruby_content_handler build/nginx/html/unified_hello.rb cache;
Expand Down Expand Up @@ -852,6 +854,15 @@ http {
';
}

location /async_http_sub_request_with_mruby_set {
mruby_rewrite_handler_code '
Nginx::Async::HTTP.sub_request "/sub_req_proxy_pass"
Nginx::Async::HTTP.last_response
';
proxy_pass http://$subrequest_after_backend;

}

location /sub_req_dst {
mruby_content_handler_code '
Nginx.rputs Nginx::Request.new.uri_args.inspect
Expand Down
8 changes: 8 additions & 0 deletions test/t/ngx_mruby.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable all
# ngx_mruby test
#

Expand Down Expand Up @@ -745,11 +746,18 @@ def _run
t.assert_equal 200, res["code"]
t.assert_equal 'proxy test ok', res["body"]
end

t.assert('ngx_mruby - Nginx::Async::HTTP.new sub request with proxy', 'location /async_http_sub_request_with_serverfalt') do
res = HttpRequest.new.get base + '/async_http_sub_request_with_serverfault'
t.assert_equal 503, res['code']
end

t.assert('ngx_mruby - Nginx.Async.sub request with proxy(set_code)', 'location /async_http_sub_request_with_mruby_set') do
res = HttpRequest.new.get base + '/async_http_sub_request_with_mruby_set'
t.assert_equal 'proxy test ok', res['body']
t.assert_equal 200, res.code
end

t.assert('ngx_mruby - Nginx::Async::HTTP.new "/dst"', 'location /async_http_sub_request') do
res = HttpRequest.new.get base + '/async_http_sub_request_with_hash'
t.assert_equal 200, res["code"]
Expand Down