nxt_ruby.c (2087:ce43da300a31) nxt_ruby.c (2163:67d2d679509e)
1/*
2 * Copyright (C) Alexander Borisov
3 * Copyright (C) NGINX, Inc.
4 */
5
6#include <ruby/nxt_ruby.h>
7
8#include <nxt_unit.h>

--- 15 unchanged lines hidden (view full) ---

24 nxt_str_t *script;
25 nxt_ruby_ctx_t *rctx;
26} nxt_ruby_rack_init_t;
27
28
29static nxt_int_t nxt_ruby_start(nxt_task_t *task,
30 nxt_process_data_t *data);
31static VALUE nxt_ruby_init_basic(VALUE arg);
1/*
2 * Copyright (C) Alexander Borisov
3 * Copyright (C) NGINX, Inc.
4 */
5
6#include <ruby/nxt_ruby.h>
7
8#include <nxt_unit.h>

--- 15 unchanged lines hidden (view full) ---

24 nxt_str_t *script;
25 nxt_ruby_ctx_t *rctx;
26} nxt_ruby_rack_init_t;
27
28
29static nxt_int_t nxt_ruby_start(nxt_task_t *task,
30 nxt_process_data_t *data);
31static VALUE nxt_ruby_init_basic(VALUE arg);
32static VALUE nxt_ruby_script_basename(nxt_str_t *script);
33
34static VALUE nxt_ruby_hook_procs_load(VALUE path);
35static VALUE nxt_ruby_hook_register(VALUE arg);
36static VALUE nxt_ruby_hook_call(VALUE name);
37
38static VALUE nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init);
39
40static VALUE nxt_ruby_require_rubygems(VALUE arg);

--- 4 unchanged lines hidden (view full) ---

45static int nxt_ruby_init_io(nxt_ruby_ctx_t *rctx);
46static void nxt_ruby_request_handler(nxt_unit_request_info_t *req);
47static void *nxt_ruby_request_handler_gvl(void *req);
48static int nxt_ruby_ready_handler(nxt_unit_ctx_t *ctx);
49static void *nxt_ruby_thread_create_gvl(void *rctx);
50static VALUE nxt_ruby_thread_func(VALUE arg);
51static void *nxt_ruby_unit_run(void *ctx);
52static void nxt_ruby_ubf(void *ctx);
32
33static VALUE nxt_ruby_hook_procs_load(VALUE path);
34static VALUE nxt_ruby_hook_register(VALUE arg);
35static VALUE nxt_ruby_hook_call(VALUE name);
36
37static VALUE nxt_ruby_rack_init(nxt_ruby_rack_init_t *rack_init);
38
39static VALUE nxt_ruby_require_rubygems(VALUE arg);

--- 4 unchanged lines hidden (view full) ---

44static int nxt_ruby_init_io(nxt_ruby_ctx_t *rctx);
45static void nxt_ruby_request_handler(nxt_unit_request_info_t *req);
46static void *nxt_ruby_request_handler_gvl(void *req);
47static int nxt_ruby_ready_handler(nxt_unit_ctx_t *ctx);
48static void *nxt_ruby_thread_create_gvl(void *rctx);
49static VALUE nxt_ruby_thread_func(VALUE arg);
50static void *nxt_ruby_unit_run(void *ctx);
51static void nxt_ruby_ubf(void *ctx);
53static int nxt_ruby_init_threads(VALUE script, nxt_ruby_app_conf_t *c);
52static int nxt_ruby_init_threads(nxt_ruby_app_conf_t *c);
54static void nxt_ruby_join_threads(nxt_unit_ctx_t *ctx,
55 nxt_ruby_app_conf_t *c);
56
57static VALUE nxt_ruby_rack_app_run(VALUE arg);
58static int nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env);
59nxt_inline void nxt_ruby_add_sptr(VALUE hash_env, VALUE name,
60 nxt_unit_sptr_t *sptr, uint32_t len);
61static nxt_int_t nxt_ruby_rack_result_status(nxt_unit_request_info_t *req,

--- 194 unchanged lines hidden (view full) ---

256 return rb_funcall(proc, rb_intern("call"), 0);
257}
258
259
260static nxt_int_t
261nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data)
262{
263 int state, rc;
53static void nxt_ruby_join_threads(nxt_unit_ctx_t *ctx,
54 nxt_ruby_app_conf_t *c);
55
56static VALUE nxt_ruby_rack_app_run(VALUE arg);
57static int nxt_ruby_read_request(nxt_unit_request_info_t *req, VALUE hash_env);
58nxt_inline void nxt_ruby_add_sptr(VALUE hash_env, VALUE name,
59 nxt_unit_sptr_t *sptr, uint32_t len);
60static nxt_int_t nxt_ruby_rack_result_status(nxt_unit_request_info_t *req,

--- 194 unchanged lines hidden (view full) ---

255 return rb_funcall(proc, rb_intern("call"), 0);
256}
257
258
259static nxt_int_t
260nxt_ruby_start(nxt_task_t *task, nxt_process_data_t *data)
261{
262 int state, rc;
264 VALUE res, path, script;
263 VALUE res, path;
265 nxt_ruby_ctx_t ruby_ctx;
266 nxt_unit_ctx_t *unit_ctx;
267 nxt_unit_init_t ruby_unit_init;
268 nxt_ruby_app_conf_t *c;
269 nxt_ruby_rack_init_t rack_init;
270 nxt_common_app_conf_t *conf;
271
272 static char *argv[2] = { (char *) "NGINX_Unit", (char *) "-e0" };

--- 5 unchanged lines hidden (view full) ---

278
279 setlocale(LC_CTYPE, "");
280
281 RUBY_INIT_STACK
282 ruby_init();
283 ruby_options(2, argv);
284 ruby_script("NGINX_Unit");
285
264 nxt_ruby_ctx_t ruby_ctx;
265 nxt_unit_ctx_t *unit_ctx;
266 nxt_unit_init_t ruby_unit_init;
267 nxt_ruby_app_conf_t *c;
268 nxt_ruby_rack_init_t rack_init;
269 nxt_common_app_conf_t *conf;
270
271 static char *argv[2] = { (char *) "NGINX_Unit", (char *) "-e0" };

--- 5 unchanged lines hidden (view full) ---

277
278 setlocale(LC_CTYPE, "");
279
280 RUBY_INIT_STACK
281 ruby_init();
282 ruby_options(2, argv);
283 ruby_script("NGINX_Unit");
284
286 script = nxt_ruby_script_basename(&c->script);
287
288 ruby_ctx.env = Qnil;
285 ruby_ctx.env = Qnil;
289 ruby_ctx.script = script;
290 ruby_ctx.io_input = Qnil;
291 ruby_ctx.io_error = Qnil;
292 ruby_ctx.thread = Qnil;
293 ruby_ctx.ctx = NULL;
294 ruby_ctx.req = NULL;
295
296 rack_init.task = task;
297 rack_init.script = &c->script;

--- 53 unchanged lines hidden (view full) ---

351 ruby_ctx.env = rb_protect(nxt_ruby_rack_env_create,
352 (VALUE) (uintptr_t) &ruby_ctx, &state);
353 if (nxt_slow_path(ruby_ctx.env == Qnil || state != 0)) {
354 nxt_ruby_exception_log(NULL, NXT_LOG_ALERT,
355 "Failed to create 'environ' variable");
356 goto fail;
357 }
358
286 ruby_ctx.io_input = Qnil;
287 ruby_ctx.io_error = Qnil;
288 ruby_ctx.thread = Qnil;
289 ruby_ctx.ctx = NULL;
290 ruby_ctx.req = NULL;
291
292 rack_init.task = task;
293 rack_init.script = &c->script;

--- 53 unchanged lines hidden (view full) ---

347 ruby_ctx.env = rb_protect(nxt_ruby_rack_env_create,
348 (VALUE) (uintptr_t) &ruby_ctx, &state);
349 if (nxt_slow_path(ruby_ctx.env == Qnil || state != 0)) {
350 nxt_ruby_exception_log(NULL, NXT_LOG_ALERT,
351 "Failed to create 'environ' variable");
352 goto fail;
353 }
354
359 rc = nxt_ruby_init_threads(script, c);
355 rc = nxt_ruby_init_threads(c);
360 if (nxt_slow_path(rc == NXT_UNIT_ERROR)) {
361 goto fail;
362 }
363
364 nxt_unit_default_init(task, &ruby_unit_init, conf);
365
366 ruby_unit_init.callbacks.request_handler = nxt_ruby_request_handler;
367 ruby_unit_init.callbacks.ready_handler = nxt_ruby_ready_handler;

--- 52 unchanged lines hidden (view full) ---

420
421 nxt_ruby_atexit();
422
423 return NXT_ERROR;
424}
425
426
427static VALUE
356 if (nxt_slow_path(rc == NXT_UNIT_ERROR)) {
357 goto fail;
358 }
359
360 nxt_unit_default_init(task, &ruby_unit_init, conf);
361
362 ruby_unit_init.callbacks.request_handler = nxt_ruby_request_handler;
363 ruby_unit_init.callbacks.ready_handler = nxt_ruby_ready_handler;

--- 52 unchanged lines hidden (view full) ---

416
417 nxt_ruby_atexit();
418
419 return NXT_ERROR;
420}
421
422
423static VALUE
428nxt_ruby_script_basename(nxt_str_t *script)
429{
430 size_t len;
431 u_char *p, *last;
432
433 last = NULL;
434 p = script->start + script->length;
435
436 while (p > script->start) {
437
438 if (p[-1] == '/') {
439 last = p;
440 break;
441 }
442
443 p--;
444 }
445
446 if (last != NULL) {
447 len = script->length - (last - script->start);
448
449 } else {
450 last = script->start;
451 len = script->length;
452 }
453
454 return rb_str_new((const char *) last, len);
455}
456
457
458static VALUE
459nxt_ruby_init_basic(VALUE arg)
460{
461 int state;
462 nxt_ruby_rack_init_t *rack_init;
463
464 rack_init = (nxt_ruby_rack_init_t *) (uintptr_t) arg;
465
466 state = rb_enc_find_index("encdb");

--- 126 unchanged lines hidden (view full) ---

593 rb_str_new((const char *) nxt_server.start,
594 (long) nxt_server.length));
595
596 version = rb_ary_new();
597
598 rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MAJOR));
599 rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MINOR));
600
424nxt_ruby_init_basic(VALUE arg)
425{
426 int state;
427 nxt_ruby_rack_init_t *rack_init;
428
429 rack_init = (nxt_ruby_rack_init_t *) (uintptr_t) arg;
430
431 state = rb_enc_find_index("encdb");

--- 126 unchanged lines hidden (view full) ---

558 rb_str_new((const char *) nxt_server.start,
559 (long) nxt_server.length));
560
561 version = rb_ary_new();
562
563 rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MAJOR));
564 rb_ary_push(version, UINT2NUM(NXT_RUBY_RACK_API_VERSION_MINOR));
565
601 rb_hash_aset(hash_env, rb_str_new2("SCRIPT_NAME"), rctx->script);
566 rb_hash_aset(hash_env, rb_str_new2("SCRIPT_NAME"), rb_str_new("", 0));
602 rb_hash_aset(hash_env, rb_str_new2("rack.version"), version);
603 rb_hash_aset(hash_env, rb_str_new2("rack.input"), rctx->io_input);
604 rb_hash_aset(hash_env, rb_str_new2("rack.errors"), rctx->io_error);
605 rb_hash_aset(hash_env, rb_str_new2("rack.multithread"),
606 nxt_ruby_threads > 1 ? Qtrue : Qfalse);
607 rb_hash_aset(hash_env, rb_str_new2("rack.multiprocess"), Qtrue);
608 rb_hash_aset(hash_env, rb_str_new2("rack.run_once"), Qfalse);
609 rb_hash_aset(hash_env, rb_str_new2("rack.hijack?"), Qfalse);

--- 778 unchanged lines hidden (view full) ---

1388static void
1389nxt_ruby_ubf(void *ctx)
1390{
1391 nxt_unit_warn(ctx, "Ruby: UBF");
1392}
1393
1394
1395static int
567 rb_hash_aset(hash_env, rb_str_new2("rack.version"), version);
568 rb_hash_aset(hash_env, rb_str_new2("rack.input"), rctx->io_input);
569 rb_hash_aset(hash_env, rb_str_new2("rack.errors"), rctx->io_error);
570 rb_hash_aset(hash_env, rb_str_new2("rack.multithread"),
571 nxt_ruby_threads > 1 ? Qtrue : Qfalse);
572 rb_hash_aset(hash_env, rb_str_new2("rack.multiprocess"), Qtrue);
573 rb_hash_aset(hash_env, rb_str_new2("rack.run_once"), Qfalse);
574 rb_hash_aset(hash_env, rb_str_new2("rack.hijack?"), Qfalse);

--- 778 unchanged lines hidden (view full) ---

1353static void
1354nxt_ruby_ubf(void *ctx)
1355{
1356 nxt_unit_warn(ctx, "Ruby: UBF");
1357}
1358
1359
1360static int
1396nxt_ruby_init_threads(VALUE script, nxt_ruby_app_conf_t *c)
1361nxt_ruby_init_threads(nxt_ruby_app_conf_t *c)
1397{
1398 int state;
1399 uint32_t i;
1400 nxt_ruby_ctx_t *rctx;
1401
1402 if (c->threads <= 1) {
1403 return NXT_UNIT_OK;
1404 }

--- 5 unchanged lines hidden (view full) ---

1410
1411 return NXT_UNIT_ERROR;
1412 }
1413
1414 for (i = 0; i < c->threads - 1; i++) {
1415 rctx = &nxt_ruby_ctxs[i];
1416
1417 rctx->env = Qnil;
1362{
1363 int state;
1364 uint32_t i;
1365 nxt_ruby_ctx_t *rctx;
1366
1367 if (c->threads <= 1) {
1368 return NXT_UNIT_OK;
1369 }

--- 5 unchanged lines hidden (view full) ---

1375
1376 return NXT_UNIT_ERROR;
1377 }
1378
1379 for (i = 0; i < c->threads - 1; i++) {
1380 rctx = &nxt_ruby_ctxs[i];
1381
1382 rctx->env = Qnil;
1418 rctx->script = script;
1419 rctx->io_input = Qnil;
1420 rctx->io_error = Qnil;
1421 rctx->thread = Qnil;
1422 }
1423
1424 for (i = 0; i < c->threads - 1; i++) {
1425 rctx = &nxt_ruby_ctxs[i];
1426

--- 42 unchanged lines hidden ---
1383 rctx->io_input = Qnil;
1384 rctx->io_error = Qnil;
1385 rctx->thread = Qnil;
1386 }
1387
1388 for (i = 0; i < c->threads - 1; i++) {
1389 rctx = &nxt_ruby_ctxs[i];
1390

--- 42 unchanged lines hidden ---