Ruby: added the Rack environment parameter "SCRIPT_NAME".

This commit is contained in:
Zhidao HONG
2022-03-09 13:29:43 +08:00
parent 03a7701133
commit 0032543fa6
3 changed files with 48 additions and 4 deletions

View File

@@ -31,6 +31,12 @@ NGINX Unit updated to 1.27.0.
date="" time="" date="" time=""
packager="Andrei Belov <defan@nginx.com>"> packager="Andrei Belov <defan@nginx.com>">
<change type="change">
<para>
Ruby Rack environment parameter "SCRIPT_NAME" support.
</para>
</change>
<change type="feature"> <change type="feature">
<para> <para>
variables support in the "location" option of the "return" action. variables support in the "location" option of the "return" action.

View File

@@ -29,6 +29,7 @@ typedef struct {
static nxt_int_t nxt_ruby_start(nxt_task_t *task, static nxt_int_t nxt_ruby_start(nxt_task_t *task,
nxt_process_data_t *data); nxt_process_data_t *data);
static VALUE nxt_ruby_init_basic(VALUE arg); static VALUE nxt_ruby_init_basic(VALUE arg);
static VALUE nxt_ruby_script_basename(nxt_str_t *script);
static VALUE nxt_ruby_hook_procs_load(VALUE path); static VALUE nxt_ruby_hook_procs_load(VALUE path);
static VALUE nxt_ruby_hook_register(VALUE arg); static VALUE nxt_ruby_hook_register(VALUE arg);
@@ -49,7 +50,7 @@ static void *nxt_ruby_thread_create_gvl(void *rctx);
static VALUE nxt_ruby_thread_func(VALUE arg); static VALUE nxt_ruby_thread_func(VALUE arg);
static void *nxt_ruby_unit_run(void *ctx); static void *nxt_ruby_unit_run(void *ctx);
static void nxt_ruby_ubf(void *ctx); static void nxt_ruby_ubf(void *ctx);
static int nxt_ruby_init_threads(nxt_ruby_app_conf_t *c); static int nxt_ruby_init_threads(VALUE script, nxt_ruby_app_conf_t *c);
static void nxt_ruby_join_threads(nxt_unit_ctx_t *ctx, static void nxt_ruby_join_threads(nxt_unit_ctx_t *ctx,
nxt_ruby_app_conf_t *c); nxt_ruby_app_conf_t *c);
@@ -260,7 +261,7 @@ static nxt_int_t
nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data) nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data)
{ {
int state, rc; int state, rc;
VALUE res, path; VALUE res, path, script;
nxt_ruby_ctx_t ruby_ctx; nxt_ruby_ctx_t ruby_ctx;
nxt_unit_ctx_t *unit_ctx; nxt_unit_ctx_t *unit_ctx;
nxt_unit_init_t ruby_unit_init; nxt_unit_init_t ruby_unit_init;
@@ -282,7 +283,10 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data)
ruby_options(2, argv); ruby_options(2, argv);
ruby_script("NGINX_Unit"); ruby_script("NGINX_Unit");
script = nxt_ruby_script_basename(&c->script);
ruby_ctx.env = Qnil; ruby_ctx.env = Qnil;
ruby_ctx.script = script;
ruby_ctx.io_input = Qnil; ruby_ctx.io_input = Qnil;
ruby_ctx.io_error = Qnil; ruby_ctx.io_error = Qnil;
ruby_ctx.thread = Qnil; ruby_ctx.thread = Qnil;
@@ -352,7 +356,7 @@ nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data)
goto fail; goto fail;
} }
rc = nxt_ruby_init_threads(c); rc = nxt_ruby_init_threads(script, c);
if (nxt_slow_path(rc == NXT_UNIT_ERROR)) { if (nxt_slow_path(rc == NXT_UNIT_ERROR)) {
goto fail; goto fail;
} }
@@ -420,6 +424,37 @@ fail:
} }
static VALUE
nxt_ruby_script_basename(nxt_str_t *script)
{
size_t len;
u_char *p, *last;
last = NULL;
p = script->start + script->length;
while (p > script->start) {
if (p[-1] == '/') {
last = p;
break;
}
p--;
}
if (last != NULL) {
len = script->length - (last - script->start);
} else {
last = script->start;
len = script->length;
}
return rb_str_new((const char *) last, len);
}
static VALUE static VALUE
nxt_ruby_init_basic(VALUE arg) nxt_ruby_init_basic(VALUE arg)
{ {
@@ -563,6 +598,7 @@ nxt_ruby_rack_env_create(VALUE arg)
rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MAJOR)); rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MAJOR));
rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MINOR)); rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MINOR));
rb_hash_aset(hash_env, rb_str_new2("SCRIPT_NAME"), rctx->script);
rb_hash_aset(hash_env, rb_str_new2("rack.version"), version); rb_hash_aset(hash_env, rb_str_new2("rack.version"), version);
rb_hash_aset(hash_env, rb_str_new2("rack.input"), rctx->io_input); rb_hash_aset(hash_env, rb_str_new2("rack.input"), rctx->io_input);
rb_hash_aset(hash_env, rb_str_new2("rack.errors"), rctx->io_error); rb_hash_aset(hash_env, rb_str_new2("rack.errors"), rctx->io_error);
@@ -1357,7 +1393,7 @@ nxt_ruby_ubf(void *ctx)
static int static int
nxt_ruby_init_threads(nxt_ruby_app_conf_t *c) nxt_ruby_init_threads(VALUE script, nxt_ruby_app_conf_t *c)
{ {
int state; int state;
uint32_t i; uint32_t i;
@@ -1379,6 +1415,7 @@ nxt_ruby_init_threads(nxt_ruby_app_conf_t *c)
rctx = &nxt_ruby_ctxs[i]; rctx = &nxt_ruby_ctxs[i];
rctx->env = Qnil; rctx->env = Qnil;
rctx->script = script;
rctx->io_input = Qnil; rctx->io_input = Qnil;
rctx->io_error = Qnil; rctx->io_error = Qnil;
rctx->thread = Qnil; rctx->thread = Qnil;

View File

@@ -22,6 +22,7 @@
typedef struct { typedef struct {
VALUE env; VALUE env;
VALUE script;
VALUE io_input; VALUE io_input;
VALUE io_error; VALUE io_error;
VALUE thread; VALUE thread;