Deleted
Added
nxt_controller.c (62:5e1efcc7b740) | nxt_controller.c (65:10688b89aa16) |
---|---|
1 2/* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) Valentin V. Bartenev 5 * Copyright (C) NGINX, Inc. 6 */ 7 8#include <nxt_main.h> 9#include <nxt_runtime.h> 10#include <nxt_master_process.h> 11#include <nxt_conf.h> 12 13 14typedef struct { 15 nxt_conf_json_value_t *root; | 1 2/* 3 * Copyright (C) Igor Sysoev 4 * Copyright (C) Valentin V. Bartenev 5 * Copyright (C) NGINX, Inc. 6 */ 7 8#include <nxt_main.h> 9#include <nxt_runtime.h> 10#include <nxt_master_process.h> 11#include <nxt_conf.h> 12 13 14typedef struct { 15 nxt_conf_json_value_t *root; |
16 nxt_mem_pool_t *pool; | 16 nxt_mp_t *pool; |
17} nxt_controller_conf_t; 18 19 20typedef struct { 21 nxt_http_request_parse_t parser; 22 size_t length; 23 24 nxt_controller_conf_t conf; --- 28 unchanged lines hidden (view full) --- 53static nxt_int_t nxt_controller_request_content_length(void *ctx, 54 nxt_http_field_t *field, uintptr_t data, nxt_log_t *log); 55 56static void nxt_controller_process_request(nxt_task_t *task, 57 nxt_conn_t *c, nxt_controller_request_t *r); 58static nxt_int_t nxt_controller_response(nxt_task_t *task, nxt_conn_t *c, 59 nxt_controller_response_t *resp); 60static nxt_buf_t *nxt_controller_response_body(nxt_controller_response_t *resp, | 17} nxt_controller_conf_t; 18 19 20typedef struct { 21 nxt_http_request_parse_t parser; 22 size_t length; 23 24 nxt_controller_conf_t conf; --- 28 unchanged lines hidden (view full) --- 53static nxt_int_t nxt_controller_request_content_length(void *ctx, 54 nxt_http_field_t *field, uintptr_t data, nxt_log_t *log); 55 56static void nxt_controller_process_request(nxt_task_t *task, 57 nxt_conn_t *c, nxt_controller_request_t *r); 58static nxt_int_t nxt_controller_response(nxt_task_t *task, nxt_conn_t *c, 59 nxt_controller_response_t *resp); 60static nxt_buf_t *nxt_controller_response_body(nxt_controller_response_t *resp, |
61 nxt_mem_pool_t *pool); | 61 nxt_mp_t *pool); |
62 63 64static nxt_http_fields_hash_entry_t nxt_controller_request_fields[] = { 65 { nxt_string("Content-Length"), 66 &nxt_controller_request_content_length, 0 }, 67 68 { nxt_null_string, NULL, 0 } 69}; --- 8 unchanged lines hidden (view full) --- 78static const nxt_event_conn_state_t nxt_controller_conn_body_read_state; 79static const nxt_event_conn_state_t nxt_controller_conn_write_state; 80static const nxt_event_conn_state_t nxt_controller_conn_close_state; 81 82 83nxt_int_t 84nxt_controller_start(nxt_task_t *task, nxt_runtime_t *rt) 85{ | 62 63 64static nxt_http_fields_hash_entry_t nxt_controller_request_fields[] = { 65 { nxt_string("Content-Length"), 66 &nxt_controller_request_content_length, 0 }, 67 68 { nxt_null_string, NULL, 0 } 69}; --- 8 unchanged lines hidden (view full) --- 78static const nxt_event_conn_state_t nxt_controller_conn_body_read_state; 79static const nxt_event_conn_state_t nxt_controller_conn_write_state; 80static const nxt_event_conn_state_t nxt_controller_conn_close_state; 81 82 83nxt_int_t 84nxt_controller_start(nxt_task_t *task, nxt_runtime_t *rt) 85{ |
86 nxt_mem_pool_t *mp; | 86 nxt_mp_t *mp; |
87 nxt_conf_json_value_t *conf; 88 nxt_http_fields_hash_t *hash; 89 90 static const nxt_str_t json 91 = nxt_string("{ \"sockets\": {}, \"applications\": {} }"); 92 93 hash = nxt_http_fields_hash_create(nxt_controller_request_fields, 94 rt->mem_pool); 95 if (nxt_slow_path(hash == NULL)) { 96 return NXT_ERROR; 97 } 98 99 nxt_controller_fields_hash = hash; 100 101 if (nxt_listen_event(task, rt->controller_socket) == NULL) { 102 return NXT_ERROR; 103 } 104 | 87 nxt_conf_json_value_t *conf; 88 nxt_http_fields_hash_t *hash; 89 90 static const nxt_str_t json 91 = nxt_string("{ \"sockets\": {}, \"applications\": {} }"); 92 93 hash = nxt_http_fields_hash_create(nxt_controller_request_fields, 94 rt->mem_pool); 95 if (nxt_slow_path(hash == NULL)) { 96 return NXT_ERROR; 97 } 98 99 nxt_controller_fields_hash = hash; 100 101 if (nxt_listen_event(task, rt->controller_socket) == NULL) { 102 return NXT_ERROR; 103 } 104 |
105 mp = nxt_mem_pool_create(256); | 105 mp = nxt_mp_create(1024, 128, 256, 32); |
106 107 if (nxt_slow_path(mp == NULL)) { 108 return NXT_ERROR; 109 } 110 111 conf = nxt_conf_json_parse(json.start, json.length, mp); 112 113 if (conf == NULL) { --- 26 unchanged lines hidden (view full) --- 140 sa->u.sockaddr_in.sin_family = AF_INET; 141 sa->u.sockaddr_in.sin_port = htons(8443); 142 143 nxt_sockaddr_text(sa); 144 145 rt->controller_listen = sa; 146 } 147 | 106 107 if (nxt_slow_path(mp == NULL)) { 108 return NXT_ERROR; 109 } 110 111 conf = nxt_conf_json_parse(json.start, json.length, mp); 112 113 if (conf == NULL) { --- 26 unchanged lines hidden (view full) --- 140 sa->u.sockaddr_in.sin_family = AF_INET; 141 sa->u.sockaddr_in.sin_port = htons(8443); 142 143 nxt_sockaddr_text(sa); 144 145 rt->controller_listen = sa; 146 } 147 |
148 ls = nxt_mem_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t)); | 148 ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t)); |
149 if (ls == NULL) { 150 return NXT_ERROR; 151 } 152 153 ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr, 154 sa->socklen, sa->length); 155 if (ls->sockaddr == NULL) { 156 return NXT_ERROR; --- 5 unchanged lines hidden (view full) --- 162 163 ls->socket = -1; 164 ls->backlog = NXT_LISTEN_BACKLOG; 165 ls->read_after_accept = 1; 166 ls->flags = NXT_NONBLOCK; 167 168#if 0 169 /* STUB */ | 149 if (ls == NULL) { 150 return NXT_ERROR; 151 } 152 153 ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr, 154 sa->socklen, sa->length); 155 if (ls->sockaddr == NULL) { 156 return NXT_ERROR; --- 5 unchanged lines hidden (view full) --- 162 163 ls->socket = -1; 164 ls->backlog = NXT_LISTEN_BACKLOG; 165 ls->read_after_accept = 1; 166 ls->flags = NXT_NONBLOCK; 167 168#if 0 169 /* STUB */ |
170 wq = nxt_mem_zalloc(cf->mem_pool, sizeof(nxt_work_queue_t)); | 170 wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t)); |
171 if (wq == NULL) { 172 return NXT_ERROR; 173 } 174 nxt_work_queue_name(wq, "listen"); 175 /**/ 176 177 ls->work_queue = wq; 178#endif 179 ls->handler = nxt_controller_conn_init; 180 | 171 if (wq == NULL) { 172 return NXT_ERROR; 173 } 174 nxt_work_queue_name(wq, "listen"); 175 /**/ 176 177 ls->work_queue = wq; 178#endif 179 ls->handler = nxt_controller_conn_init; 180 |
181 /* 182 * Connection memory pool chunk size is tunned to 183 * allocate the most data in one mem_pool chunk. 184 */ 185 ls->mem_pool_size = nxt_listen_socket_pool_min_size(ls) 186 + sizeof(nxt_event_conn_proxy_t) 187 + sizeof(nxt_conn_t) 188 + 4 * sizeof(nxt_buf_t); 189 | |
190 if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) { 191 return NXT_ERROR; 192 } 193 194 rt->controller_socket = ls; 195 196 return NXT_OK; 197} --- 6 unchanged lines hidden (view full) --- 204 nxt_conn_t *c; 205 nxt_event_engine_t *engine; 206 nxt_controller_request_t *r; 207 208 c = obj; 209 210 nxt_debug(task, "controller conn init fd:%d", c->socket.fd); 211 | 181 if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) { 182 return NXT_ERROR; 183 } 184 185 rt->controller_socket = ls; 186 187 return NXT_OK; 188} --- 6 unchanged lines hidden (view full) --- 195 nxt_conn_t *c; 196 nxt_event_engine_t *engine; 197 nxt_controller_request_t *r; 198 199 c = obj; 200 201 nxt_debug(task, "controller conn init fd:%d", c->socket.fd); 202 |
212 r = nxt_mem_zalloc(c->mem_pool, sizeof(nxt_controller_request_t)); | 203 r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t)); |
213 if (nxt_slow_path(r == NULL)) { 214 nxt_controller_conn_free(task, c, NULL); 215 return; 216 } 217 218 if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool) 219 != NXT_OK)) 220 { --- 287 unchanged lines hidden (view full) --- 508nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data) 509{ 510 nxt_conn_t *c; 511 512 c = obj; 513 514 nxt_debug(task, "controller conn free"); 515 | 204 if (nxt_slow_path(r == NULL)) { 205 nxt_controller_conn_free(task, c, NULL); 206 return; 207 } 208 209 if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool) 210 != NXT_OK)) 211 { --- 287 unchanged lines hidden (view full) --- 499nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data) 500{ 501 nxt_conn_t *c; 502 503 c = obj; 504 505 nxt_debug(task, "controller conn free"); 506 |
516 nxt_mem_pool_destroy(c->mem_pool); | 507 nxt_mp_destroy(c->mem_pool); |
517 518 //nxt_free(c); 519} 520 521 522static nxt_int_t 523nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field, 524 uintptr_t data, nxt_log_t *log) --- 17 unchanged lines hidden (view full) --- 542 return NXT_ERROR; 543} 544 545 546static void 547nxt_controller_process_request(nxt_task_t *task, nxt_conn_t *c, 548 nxt_controller_request_t *req) 549{ | 508 509 //nxt_free(c); 510} 511 512 513static nxt_int_t 514nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field, 515 uintptr_t data, nxt_log_t *log) --- 17 unchanged lines hidden (view full) --- 533 return NXT_ERROR; 534} 535 536 537static void 538nxt_controller_process_request(nxt_task_t *task, nxt_conn_t *c, 539 nxt_controller_request_t *req) 540{ |
541 nxt_mp_t *mp; |
|
550 nxt_int_t rc; 551 nxt_str_t path; 552 nxt_uint_t status; 553 nxt_buf_mem_t *mbuf; | 542 nxt_int_t rc; 543 nxt_str_t path; 544 nxt_uint_t status; 545 nxt_buf_mem_t *mbuf; |
554 nxt_mem_pool_t *mp; | |
555 nxt_conf_json_op_t *ops; 556 nxt_conf_json_value_t *value; 557 nxt_controller_response_t resp; 558 559 static const nxt_str_t empty_obj = nxt_string("{}"); 560 561 path.start = req->parser.target_start; 562 --- 22 unchanged lines hidden (view full) --- 585 resp.json_value = value; 586 587 status = 200; 588 goto done; 589 } 590 591 if (nxt_str_eq(&req->parser.method, "PUT", 3)) { 592 | 546 nxt_conf_json_op_t *ops; 547 nxt_conf_json_value_t *value; 548 nxt_controller_response_t resp; 549 550 static const nxt_str_t empty_obj = nxt_string("{}"); 551 552 path.start = req->parser.target_start; 553 --- 22 unchanged lines hidden (view full) --- 576 resp.json_value = value; 577 578 status = 200; 579 goto done; 580 } 581 582 if (nxt_str_eq(&req->parser.method, "PUT", 3)) { 583 |
593 mp = nxt_mem_pool_create(512); | 584 mp = nxt_mp_create(1024, 128, 256, 32); |
594 595 if (nxt_slow_path(mp == NULL)) { 596 status = 500; 597 goto done; 598 } 599 600 mbuf = &c->read->mem; 601 602 value = nxt_conf_json_parse(mbuf->pos, mbuf->free - mbuf->pos, mp); 603 604 if (value == NULL) { | 585 586 if (nxt_slow_path(mp == NULL)) { 587 status = 500; 588 goto done; 589 } 590 591 mbuf = &c->read->mem; 592 593 value = nxt_conf_json_parse(mbuf->pos, mbuf->free - mbuf->pos, mp); 594 595 if (value == NULL) { |
605 nxt_mem_pool_destroy(mp); | 596 nxt_mp_destroy(mp); |
606 status = 400; 607 goto done; 608 } 609 610 if (path.length != 1) { 611 rc = nxt_conf_json_op_compile(nxt_controller_conf.root, value, 612 &ops, &path, c->mem_pool); 613 --- 6 unchanged lines hidden (view full) --- 620 status = 500; 621 goto done; 622 } 623 624 value = nxt_conf_json_clone_value(nxt_controller_conf.root, 625 ops, mp); 626 627 if (nxt_slow_path(value == NULL)) { | 597 status = 400; 598 goto done; 599 } 600 601 if (path.length != 1) { 602 rc = nxt_conf_json_op_compile(nxt_controller_conf.root, value, 603 &ops, &path, c->mem_pool); 604 --- 6 unchanged lines hidden (view full) --- 611 status = 500; 612 goto done; 613 } 614 615 value = nxt_conf_json_clone_value(nxt_controller_conf.root, 616 ops, mp); 617 618 if (nxt_slow_path(value == NULL)) { |
628 nxt_mem_pool_destroy(mp); | 619 nxt_mp_destroy(mp); |
629 status = 500; 630 goto done; 631 } 632 } 633 | 620 status = 500; 621 goto done; 622 } 623 } 624 |
634 nxt_mem_pool_destroy(nxt_controller_conf.pool); | 625 nxt_mp_destroy(nxt_controller_conf.pool); |
635 636 nxt_controller_conf.root = value; 637 nxt_controller_conf.pool = mp; 638 639 nxt_str_set(&resp.json_string, "{ \"success\": \"Updated.\" }"); 640 641 status = 200; 642 goto done; 643 } 644 645 if (nxt_str_eq(&req->parser.method, "DELETE", 6)) { 646 647 if (path.length == 1) { | 626 627 nxt_controller_conf.root = value; 628 nxt_controller_conf.pool = mp; 629 630 nxt_str_set(&resp.json_string, "{ \"success\": \"Updated.\" }"); 631 632 status = 200; 633 goto done; 634 } 635 636 if (nxt_str_eq(&req->parser.method, "DELETE", 6)) { 637 638 if (path.length == 1) { |
648 mp = nxt_mem_pool_create(128); | 639 mp = nxt_mp_create(1024, 128, 256, 32); |
649 650 if (nxt_slow_path(mp == NULL)) { 651 status = 500; 652 goto done; 653 } 654 655 value = nxt_conf_json_parse(empty_obj.start, empty_obj.length, mp); 656 --- 6 unchanged lines hidden (view full) --- 663 status = 404; 664 goto done; 665 } 666 667 status = 500; 668 goto done; 669 } 670 | 640 641 if (nxt_slow_path(mp == NULL)) { 642 status = 500; 643 goto done; 644 } 645 646 value = nxt_conf_json_parse(empty_obj.start, empty_obj.length, mp); 647 --- 6 unchanged lines hidden (view full) --- 654 status = 404; 655 goto done; 656 } 657 658 status = 500; 659 goto done; 660 } 661 |
671 mp = nxt_mem_pool_create(512); | 662 mp = nxt_mp_create(1024, 128, 256, 32); |
672 673 if (nxt_slow_path(mp == NULL)) { 674 status = 500; 675 goto done; 676 } 677 678 value = nxt_conf_json_clone_value(nxt_controller_conf.root, 679 ops, mp); 680 } 681 682 if (nxt_slow_path(value == NULL)) { | 663 664 if (nxt_slow_path(mp == NULL)) { 665 status = 500; 666 goto done; 667 } 668 669 value = nxt_conf_json_clone_value(nxt_controller_conf.root, 670 ops, mp); 671 } 672 673 if (nxt_slow_path(value == NULL)) { |
683 nxt_mem_pool_destroy(mp); | 674 nxt_mp_destroy(mp); |
684 status = 500; 685 goto done; 686 } 687 | 675 status = 500; 676 goto done; 677 } 678 |
688 nxt_mem_pool_destroy(nxt_controller_conf.pool); | 679 nxt_mp_destroy(nxt_controller_conf.pool); |
689 690 nxt_controller_conf.root = value; 691 nxt_controller_conf.pool = mp; 692 693 nxt_str_set(&resp.json_string, "{ \"success\": \"Deleted.\" }"); 694 695 status = 200; 696 goto done; --- 70 unchanged lines hidden (view full) --- 767 768 nxt_conn_write(task->thread->engine, c); 769 770 return NXT_OK; 771} 772 773 774static nxt_buf_t * | 680 681 nxt_controller_conf.root = value; 682 nxt_controller_conf.pool = mp; 683 684 nxt_str_set(&resp.json_string, "{ \"success\": \"Deleted.\" }"); 685 686 status = 200; 687 goto done; --- 70 unchanged lines hidden (view full) --- 758 759 nxt_conn_write(task->thread->engine, c); 760 761 return NXT_OK; 762} 763 764 765static nxt_buf_t * |
775nxt_controller_response_body(nxt_controller_response_t *resp, 776 nxt_mem_pool_t *pool) | 766nxt_controller_response_body(nxt_controller_response_t *resp, nxt_mp_t *pool) |
777{ 778 size_t size; 779 nxt_buf_t *b; 780 nxt_conf_json_value_t *value; 781 nxt_conf_json_pretty_t pretty; 782 783 if (resp->json_value) { 784 value = resp->json_value; --- 29 unchanged lines hidden --- | 767{ 768 size_t size; 769 nxt_buf_t *b; 770 nxt_conf_json_value_t *value; 771 nxt_conf_json_pretty_t pretty; 772 773 if (resp->json_value) { 774 value = resp->json_value; --- 29 unchanged lines hidden --- |