unit.cpp (1731:43759e8fd1f6) unit.cpp (1766:9ec17030b67e)
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6#include "unit.h"
7
8#include <unistd.h>

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

22 nxt_unit_ctx_t *ctx;
23 nxt_unit_port_t *port;
24 uv_poll_t poll;
25};
26
27
28struct req_data_t {
29 napi_ref sock_ref;
1
2/*
3 * Copyright (C) NGINX, Inc.
4 */
5
6#include "unit.h"
7
8#include <unistd.h>

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

22 nxt_unit_ctx_t *ctx;
23 nxt_unit_port_t *port;
24 uv_poll_t poll;
25};
26
27
28struct req_data_t {
29 napi_ref sock_ref;
30 napi_ref req_ref;
30 napi_ref resp_ref;
31 napi_ref conn_ref;
32};
33
34
35Unit::Unit(napi_env env, napi_value jsthis):
36 nxt_napi(env),
37 wrapper_(wrap(jsthis, this, destroy)),

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

60 { "listen", 0, listen, 0, 0, 0, napi_default, 0 },
61 };
62
63 try {
64 ctor = napi.define_class("Unit", create, 2, unit_props);
65 constructor_ = napi.create_reference(ctor);
66
67 napi.set_named_property(exports, "Unit", ctor);
31 napi_ref resp_ref;
32 napi_ref conn_ref;
33};
34
35
36Unit::Unit(napi_env env, napi_value jsthis):
37 nxt_napi(env),
38 wrapper_(wrap(jsthis, this, destroy)),

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

61 { "listen", 0, listen, 0, 0, 0, napi_default, 0 },
62 };
63
64 try {
65 ctor = napi.define_class("Unit", create, 2, unit_props);
66 constructor_ = napi.create_reference(ctor);
67
68 napi.set_named_property(exports, "Unit", ctor);
69 napi.set_named_property(exports, "request_read", request_read);
68 napi.set_named_property(exports, "response_send_headers",
69 response_send_headers);
70 napi.set_named_property(exports, "response_write", response_write);
71 napi.set_named_property(exports, "response_end", response_end);
72 napi.set_named_property(exports, "websocket_send_frame",
73 websocket_send_frame);
74 napi.set_named_property(exports, "websocket_set_sock",
75 websocket_set_sock);

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

201 memset(req->data, 0, sizeof(req_data_t));
202
203 try {
204 nxt_handle_scope scope(env());
205
206 server_obj = get_server_object();
207
208 socket = create_socket(server_obj, req);
70 napi.set_named_property(exports, "response_send_headers",
71 response_send_headers);
72 napi.set_named_property(exports, "response_write", response_write);
73 napi.set_named_property(exports, "response_end", response_end);
74 napi.set_named_property(exports, "websocket_send_frame",
75 websocket_send_frame);
76 napi.set_named_property(exports, "websocket_set_sock",
77 websocket_set_sock);

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

203 memset(req->data, 0, sizeof(req_data_t));
204
205 try {
206 nxt_handle_scope scope(env());
207
208 server_obj = get_server_object();
209
210 socket = create_socket(server_obj, req);
209 request = create_request(server_obj, socket);
211 request = create_request(server_obj, socket, req);
210 response = create_response(server_obj, request, req);
211
212 create_headers(req, request);
213
214 emit_request = get_named_property(server_obj, "emit_request");
215
216 nxt_async_context async_context(env(), "request_handler");
217 nxt_callback_scope async_scope(async_context);

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

296
297 nxt_async_context async_context(env(), "close_handler");
298 nxt_callback_scope async_scope(async_context);
299
300 make_callback(async_context, conn, conn_handle_close,
301 nxt_napi::create(0));
302
303 remove_wrap(req_data->sock_ref);
212 response = create_response(server_obj, request, req);
213
214 create_headers(req, request);
215
216 emit_request = get_named_property(server_obj, "emit_request");
217
218 nxt_async_context async_context(env(), "request_handler");
219 nxt_callback_scope async_scope(async_context);

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

298
299 nxt_async_context async_context(env(), "close_handler");
300 nxt_callback_scope async_scope(async_context);
301
302 make_callback(async_context, conn, conn_handle_close,
303 nxt_napi::create(0));
304
305 remove_wrap(req_data->sock_ref);
306 remove_wrap(req_data->req_ref);
304 remove_wrap(req_data->resp_ref);
305 remove_wrap(req_data->conn_ref);
306
307 } catch (exception &e) {
308 nxt_unit_req_warn(req, "close_handler: %s", e.str);
309
310 nxt_unit_request_done(req, NXT_UNIT_ERROR);
311

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

483
484 return get_named_property(unit_obj, "server");
485}
486
487
488void
489Unit::create_headers(nxt_unit_request_info_t *req, napi_value request)
490{
307 remove_wrap(req_data->resp_ref);
308 remove_wrap(req_data->conn_ref);
309
310 } catch (exception &e) {
311 nxt_unit_req_warn(req, "close_handler: %s", e.str);
312
313 nxt_unit_request_done(req, NXT_UNIT_ERROR);
314

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

486
487 return get_named_property(unit_obj, "server");
488}
489
490
491void
492Unit::create_headers(nxt_unit_request_info_t *req, napi_value request)
493{
491 void *data;
492 uint32_t i;
494 uint32_t i;
493 napi_value headers, raw_headers, buffer;
495 napi_value headers, raw_headers;
494 napi_status status;
495 nxt_unit_request_t *r;
496
497 r = req->request;
498
499 headers = create_object();
500
501 status = napi_create_array_with_length(env(), r->fields_count * 2,

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

510
511 set_named_property(request, "headers", headers);
512 set_named_property(request, "rawHeaders", raw_headers);
513 set_named_property(request, "httpVersion", r->version, r->version_length);
514 set_named_property(request, "method", r->method, r->method_length);
515 set_named_property(request, "url", r->target, r->target_length);
516
517 set_named_property(request, "_websocket_handshake", r->websocket_handshake);
496 napi_status status;
497 nxt_unit_request_t *r;
498
499 r = req->request;
500
501 headers = create_object();
502
503 status = napi_create_array_with_length(env(), r->fields_count * 2,

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

512
513 set_named_property(request, "headers", headers);
514 set_named_property(request, "rawHeaders", raw_headers);
515 set_named_property(request, "httpVersion", r->version, r->version_length);
516 set_named_property(request, "method", r->method, r->method_length);
517 set_named_property(request, "url", r->target, r->target_length);
518
519 set_named_property(request, "_websocket_handshake", r->websocket_handshake);
518
519 buffer = create_buffer((size_t) req->content_length, &data);
520 nxt_unit_request_read(req, data, req->content_length);
521
522 set_named_property(request, "_data", buffer);
523}
524
525
526inline char
527lowcase(char c)
528{
529 return (c >= 'A' && c <= 'Z') ? (c | 0x20) : c;
530}

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

572 set_named_property(res, "remoteAddress", r->remote, r->remote_length);
573 set_named_property(res, "localAddress", r->local, r->local_length);
574
575 return res;
576}
577
578
579napi_value
520}
521
522
523inline char
524lowcase(char c)
525{
526 return (c >= 'A' && c <= 'Z') ? (c | 0x20) : c;
527}

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

569 set_named_property(res, "remoteAddress", r->remote, r->remote_length);
570 set_named_property(res, "localAddress", r->local, r->local_length);
571
572 return res;
573}
574
575
576napi_value
580Unit::create_request(napi_value server_obj, napi_value socket)
577Unit::create_request(napi_value server_obj, napi_value socket,
578 nxt_unit_request_info_t *req)
581{
579{
582 napi_value constructor;
580 napi_value constructor, res;
581 req_data_t *req_data;
583
584 constructor = get_named_property(server_obj, "ServerRequest");
585
582
583 constructor = get_named_property(server_obj, "ServerRequest");
584
586 return new_instance(constructor, server_obj, socket);
585 res = new_instance(constructor, server_obj, socket);
586
587 req_data = (req_data_t *) req->data;
588 req_data->req_ref = wrap(res, req, req_destroy);
589
590 return res;
587}
588
589
590napi_value
591Unit::create_response(napi_value server_obj, napi_value request,
592 nxt_unit_request_info_t *req)
593{
594 napi_value constructor, res;

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

638
639 set_named_property(res, "binaryPayload", buffer);
640
641 return res;
642}
643
644
645napi_value
591}
592
593
594napi_value
595Unit::create_response(napi_value server_obj, napi_value request,
596 nxt_unit_request_info_t *req)
597{
598 napi_value constructor, res;

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

642
643 set_named_property(res, "binaryPayload", buffer);
644
645 return res;
646}
647
648
649napi_value
650Unit::request_read(napi_env env, napi_callback_info info)
651{
652 void *data;
653 uint32_t wm;
654 nxt_napi napi(env);
655 napi_value this_arg, argv, buffer;
656 nxt_unit_request_info_t *req;
657
658 try {
659 this_arg = napi.get_cb_info(info, argv);
660
661 try {
662 req = napi.get_request_info(this_arg);
663
664 } catch (exception &e) {
665 return nullptr;
666 }
667
668 if (req->content_length == 0) {
669 return nullptr;
670 }
671
672 wm = napi.get_value_uint32(argv);
673
674 if (wm > req->content_length) {
675 wm = req->content_length;
676 }
677
678 buffer = napi.create_buffer((size_t) wm, &data);
679 nxt_unit_request_read(req, data, wm);
680
681 } catch (exception &e) {
682 napi.throw_error(e);
683 return nullptr;
684 }
685
686 return buffer;
687}
688
689
690napi_value
646Unit::response_send_headers(napi_env env, napi_callback_info info)
647{
648 int ret;
649 char *ptr, *name_ptr;
650 bool is_array;
651 size_t argc, name_len, value_len;
652 uint32_t status_code, header_len, keys_len, array_len;
653 uint32_t keys_count, i, j;

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

879 try {
880 this_arg = napi.get_cb_info(info);
881
882 req = napi.get_request_info(this_arg);
883
884 req_data = (req_data_t *) req->data;
885
886 napi.remove_wrap(req_data->sock_ref);
691Unit::response_send_headers(napi_env env, napi_callback_info info)
692{
693 int ret;
694 char *ptr, *name_ptr;
695 bool is_array;
696 size_t argc, name_len, value_len;
697 uint32_t status_code, header_len, keys_len, array_len;
698 uint32_t keys_count, i, j;

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

924 try {
925 this_arg = napi.get_cb_info(info);
926
927 req = napi.get_request_info(this_arg);
928
929 req_data = (req_data_t *) req->data;
930
931 napi.remove_wrap(req_data->sock_ref);
932 napi.remove_wrap(req_data->req_ref);
887 napi.remove_wrap(req_data->resp_ref);
888 napi.remove_wrap(req_data->conn_ref);
889
890 } catch (exception &e) {
891 napi.throw_error(e);
892 return nullptr;
893 }
894

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

1007void
1008Unit::sock_destroy(napi_env env, void *r, void *finalize_hint)
1009{
1010 nxt_unit_req_debug(NULL, "sock_destroy: %p", r);
1011}
1012
1013
1014void
933 napi.remove_wrap(req_data->resp_ref);
934 napi.remove_wrap(req_data->conn_ref);
935
936 } catch (exception &e) {
937 napi.throw_error(e);
938 return nullptr;
939 }
940

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

1053void
1054Unit::sock_destroy(napi_env env, void *r, void *finalize_hint)
1055{
1056 nxt_unit_req_debug(NULL, "sock_destroy: %p", r);
1057}
1058
1059
1060void
1061Unit::req_destroy(napi_env env, void *r, void *finalize_hint)
1062{
1063 nxt_unit_req_debug(NULL, "req_destroy: %p", r);
1064}
1065
1066
1067void
1015Unit::resp_destroy(napi_env env, void *r, void *finalize_hint)
1016{
1017 nxt_unit_req_debug(NULL, "resp_destroy: %p", r);
1018}
1068Unit::resp_destroy(napi_env env, void *r, void *finalize_hint)
1069{
1070 nxt_unit_req_debug(NULL, "resp_destroy: %p", r);
1071}