Deleted Added
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;
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);
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;
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);
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));
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));
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
212 r = nxt_mem_zalloc(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
516 nxt_mem_pool_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{
550 nxt_int_t rc;
551 nxt_str_t path;
552 nxt_uint_t status;
553 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
593 mp = nxt_mem_pool_create(512);
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) {
605 nxt_mem_pool_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)) {
628 nxt_mem_pool_destroy(mp);
629 status = 500;
630 goto done;
631 }
632 }
633
634 nxt_mem_pool_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) {
648 mp = nxt_mem_pool_create(128);
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
671 mp = nxt_mem_pool_create(512);
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)) {
683 nxt_mem_pool_destroy(mp);
684 status = 500;
685 goto done;
686 }
687
688 nxt_mem_pool_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 *
775nxt_controller_response_body(nxt_controller_response_t *resp,
776 nxt_mem_pool_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 ---