xref: /unit/src/nxt_controller.c (revision 248)
120Sigor@sysoev.ru 
220Sigor@sysoev.ru /*
320Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
420Sigor@sysoev.ru  * Copyright (C) Valentin V. Bartenev
520Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
620Sigor@sysoev.ru  */
720Sigor@sysoev.ru 
820Sigor@sysoev.ru #include <nxt_main.h>
920Sigor@sysoev.ru #include <nxt_runtime.h>
10240Sigor@sysoev.ru #include <nxt_main_process.h>
1129Svbart@nginx.com #include <nxt_conf.h>
1220Sigor@sysoev.ru 
1320Sigor@sysoev.ru 
1427Svbart@nginx.com typedef struct {
15106Svbart@nginx.com     nxt_conf_value_t  *root;
16106Svbart@nginx.com     nxt_mp_t          *pool;
1744Svbart@nginx.com } nxt_controller_conf_t;
1844Svbart@nginx.com 
1944Svbart@nginx.com 
2044Svbart@nginx.com typedef struct {
2127Svbart@nginx.com     nxt_http_request_parse_t  parser;
2227Svbart@nginx.com     size_t                    length;
2344Svbart@nginx.com     nxt_controller_conf_t     conf;
24140Svbart@nginx.com     nxt_conn_t                *conn;
25140Svbart@nginx.com     nxt_queue_link_t          link;
2627Svbart@nginx.com } nxt_controller_request_t;
2727Svbart@nginx.com 
2827Svbart@nginx.com 
2944Svbart@nginx.com typedef struct {
30208Svbart@nginx.com     nxt_uint_t        status;
31106Svbart@nginx.com     nxt_conf_value_t  *conf;
32208Svbart@nginx.com 
33208Svbart@nginx.com     u_char            *title;
34208Svbart@nginx.com     u_char            *detail;
35208Svbart@nginx.com     ssize_t           offset;
36208Svbart@nginx.com     nxt_uint_t        line;
37208Svbart@nginx.com     nxt_uint_t        column;
3844Svbart@nginx.com } nxt_controller_response_t;
3944Svbart@nginx.com 
4044Svbart@nginx.com 
41*248Svbart@nginx.com static void nxt_controller_process_new_port_handler(nxt_task_t *task,
42*248Svbart@nginx.com     nxt_port_recv_msg_t *msg);
43*248Svbart@nginx.com 
4420Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
4520Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
4662Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
4720Sigor@sysoev.ru     uintptr_t data);
4820Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
4920Sigor@sysoev.ru     void *data);
5020Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
5120Sigor@sysoev.ru     void *data);
5227Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
5327Svbart@nginx.com     void *data);
5427Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
5527Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
5627Svbart@nginx.com     void *data);
5727Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
5827Svbart@nginx.com     void *data);
5920Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
6020Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
6120Sigor@sysoev.ru 
6227Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
6367Svbart@nginx.com     nxt_http_field_t *field, nxt_log_t *log);
6427Svbart@nginx.com 
6527Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
66140Svbart@nginx.com     nxt_controller_request_t *req);
67121Svbart@nginx.com static nxt_int_t nxt_controller_conf_apply(nxt_task_t *task,
68140Svbart@nginx.com     nxt_controller_request_t *req);
69238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task,
70238Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
71140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
72140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
73208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
74208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
7527Svbart@nginx.com 
7627Svbart@nginx.com 
7760Svbart@nginx.com static nxt_http_fields_hash_entry_t  nxt_controller_request_fields[] = {
7827Svbart@nginx.com     { nxt_string("Content-Length"),
7927Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
8027Svbart@nginx.com 
8127Svbart@nginx.com     { nxt_null_string, NULL, 0 }
8227Svbart@nginx.com };
8327Svbart@nginx.com 
8460Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_fields_hash;
8527Svbart@nginx.com 
86238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
87238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
8827Svbart@nginx.com 
8920Sigor@sysoev.ru 
9020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
9127Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
9227Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
9320Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
9420Sigor@sysoev.ru 
9520Sigor@sysoev.ru 
96*248Svbart@nginx.com nxt_port_handler_t  nxt_controller_process_port_handlers[] = {
97*248Svbart@nginx.com     nxt_worker_process_quit_handler,
98*248Svbart@nginx.com     nxt_controller_process_new_port_handler,
99*248Svbart@nginx.com     nxt_port_change_log_file_handler,
100*248Svbart@nginx.com     nxt_port_mmap_handler,
101*248Svbart@nginx.com     nxt_port_data_handler,
102*248Svbart@nginx.com     nxt_port_remove_pid_handler,
103*248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_READY        */
104*248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_START_WORKER */
105*248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_SOCKET       */
106*248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_MODULES      */
107*248Svbart@nginx.com     nxt_port_rpc_handler,
108*248Svbart@nginx.com     nxt_port_rpc_handler,
109*248Svbart@nginx.com };
110*248Svbart@nginx.com 
111*248Svbart@nginx.com 
11220Sigor@sysoev.ru nxt_int_t
113141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
11420Sigor@sysoev.ru {
115141Smax.romanov@nginx.com     nxt_runtime_t           *rt;
11627Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
11727Svbart@nginx.com 
118141Smax.romanov@nginx.com     rt = task->thread->runtime;
119141Smax.romanov@nginx.com 
12060Svbart@nginx.com     hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
12160Svbart@nginx.com                                        rt->mem_pool);
12227Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
12327Svbart@nginx.com         return NXT_ERROR;
12427Svbart@nginx.com     }
12527Svbart@nginx.com 
12660Svbart@nginx.com     nxt_controller_fields_hash = hash;
127*248Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
12827Svbart@nginx.com 
129*248Svbart@nginx.com     return NXT_OK;
130*248Svbart@nginx.com }
131*248Svbart@nginx.com 
132*248Svbart@nginx.com 
133*248Svbart@nginx.com static void
134*248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task,
135*248Svbart@nginx.com     nxt_port_recv_msg_t *msg)
136*248Svbart@nginx.com {
137*248Svbart@nginx.com     nxt_mp_t          *mp;
138*248Svbart@nginx.com     nxt_runtime_t     *rt;
139*248Svbart@nginx.com     nxt_conf_value_t  *conf;
140*248Svbart@nginx.com 
141*248Svbart@nginx.com     static const nxt_str_t json
142*248Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
143*248Svbart@nginx.com 
144*248Svbart@nginx.com     nxt_port_new_port_handler(task, msg);
145*248Svbart@nginx.com 
146*248Svbart@nginx.com     if (nxt_controller_conf.root != NULL
147*248Svbart@nginx.com         || msg->new_port->type != NXT_PROCESS_ROUTER)
148*248Svbart@nginx.com     {
149*248Svbart@nginx.com         return;
15020Sigor@sysoev.ru     }
15120Sigor@sysoev.ru 
15265Sigor@sysoev.ru     mp = nxt_mp_create(1024, 128, 256, 32);
15344Svbart@nginx.com 
15444Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
155*248Svbart@nginx.com         nxt_abort();
15644Svbart@nginx.com     }
15744Svbart@nginx.com 
158106Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
15944Svbart@nginx.com 
160*248Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
161*248Svbart@nginx.com         nxt_abort();
16244Svbart@nginx.com     }
16344Svbart@nginx.com 
16444Svbart@nginx.com     nxt_controller_conf.root = conf;
16544Svbart@nginx.com     nxt_controller_conf.pool = mp;
16644Svbart@nginx.com 
167*248Svbart@nginx.com     rt = task->thread->runtime;
168140Svbart@nginx.com 
169*248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
170*248Svbart@nginx.com         nxt_abort();
171*248Svbart@nginx.com     }
17220Sigor@sysoev.ru }
17320Sigor@sysoev.ru 
17420Sigor@sysoev.ru 
17520Sigor@sysoev.ru nxt_int_t
17620Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
17720Sigor@sysoev.ru {
17820Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
17920Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
18020Sigor@sysoev.ru 
18120Sigor@sysoev.ru     sa = rt->controller_listen;
18220Sigor@sysoev.ru 
18365Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
18420Sigor@sysoev.ru     if (ls == NULL) {
18520Sigor@sysoev.ru         return NXT_ERROR;
18620Sigor@sysoev.ru     }
18720Sigor@sysoev.ru 
18820Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
18920Sigor@sysoev.ru                                        sa->socklen, sa->length);
19020Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
19120Sigor@sysoev.ru         return NXT_ERROR;
19220Sigor@sysoev.ru     }
19320Sigor@sysoev.ru 
19420Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
195103Sigor@sysoev.ru     ls->socklen = sa->socklen;
196103Sigor@sysoev.ru     ls->address_length = sa->length;
19720Sigor@sysoev.ru 
19820Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
19920Sigor@sysoev.ru 
20020Sigor@sysoev.ru     ls->socket = -1;
20120Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
20220Sigor@sysoev.ru     ls->read_after_accept = 1;
20320Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
20420Sigor@sysoev.ru 
20520Sigor@sysoev.ru #if 0
20620Sigor@sysoev.ru     /* STUB */
20765Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
20820Sigor@sysoev.ru     if (wq == NULL) {
20920Sigor@sysoev.ru         return NXT_ERROR;
21020Sigor@sysoev.ru     }
21120Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
21220Sigor@sysoev.ru     /**/
21320Sigor@sysoev.ru 
21420Sigor@sysoev.ru     ls->work_queue = wq;
21520Sigor@sysoev.ru #endif
21620Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
21720Sigor@sysoev.ru 
21820Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
21920Sigor@sysoev.ru         return NXT_ERROR;
22020Sigor@sysoev.ru     }
22120Sigor@sysoev.ru 
22220Sigor@sysoev.ru     rt->controller_socket = ls;
22320Sigor@sysoev.ru 
22420Sigor@sysoev.ru     return NXT_OK;
22520Sigor@sysoev.ru }
22620Sigor@sysoev.ru 
22720Sigor@sysoev.ru 
22820Sigor@sysoev.ru static void
22920Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
23020Sigor@sysoev.ru {
23127Svbart@nginx.com     nxt_buf_t                 *b;
23262Sigor@sysoev.ru     nxt_conn_t                *c;
23327Svbart@nginx.com     nxt_event_engine_t        *engine;
23427Svbart@nginx.com     nxt_controller_request_t  *r;
23520Sigor@sysoev.ru 
23620Sigor@sysoev.ru     c = obj;
23720Sigor@sysoev.ru 
23820Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
23920Sigor@sysoev.ru 
24065Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
24127Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
24227Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
24327Svbart@nginx.com         return;
24427Svbart@nginx.com     }
24527Svbart@nginx.com 
246140Svbart@nginx.com     r->conn = c;
247140Svbart@nginx.com 
24860Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
24960Svbart@nginx.com                       != NXT_OK))
25060Svbart@nginx.com     {
25160Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
25260Svbart@nginx.com         return;
25360Svbart@nginx.com     }
25427Svbart@nginx.com 
25567Svbart@nginx.com     r->parser.fields_hash = nxt_controller_fields_hash;
25667Svbart@nginx.com 
25720Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
25820Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
25920Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
26020Sigor@sysoev.ru         return;
26120Sigor@sysoev.ru     }
26220Sigor@sysoev.ru 
26320Sigor@sysoev.ru     c->read = b;
26427Svbart@nginx.com     c->socket.data = r;
26520Sigor@sysoev.ru     c->socket.read_ready = 1;
26620Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
26720Sigor@sysoev.ru 
26820Sigor@sysoev.ru     engine = task->thread->engine;
26920Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
27027Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
27120Sigor@sysoev.ru 
27262Sigor@sysoev.ru     nxt_conn_read(engine, c);
27320Sigor@sysoev.ru }
27420Sigor@sysoev.ru 
27520Sigor@sysoev.ru 
27620Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
27720Sigor@sysoev.ru     nxt_aligned(64) =
27820Sigor@sysoev.ru {
27956Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
28056Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
28156Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
28220Sigor@sysoev.ru 
28356Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
28456Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
28556Sigor@sysoev.ru     .timer_data = 60 * 1000,
28620Sigor@sysoev.ru };
28720Sigor@sysoev.ru 
28820Sigor@sysoev.ru 
28920Sigor@sysoev.ru static void
29020Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
29120Sigor@sysoev.ru {
29227Svbart@nginx.com     size_t                    preread;
29327Svbart@nginx.com     nxt_buf_t                 *b;
29427Svbart@nginx.com     nxt_int_t                 rc;
29562Sigor@sysoev.ru     nxt_conn_t                *c;
29627Svbart@nginx.com     nxt_controller_request_t  *r;
29720Sigor@sysoev.ru 
29820Sigor@sysoev.ru     c = obj;
29927Svbart@nginx.com     r = data;
30020Sigor@sysoev.ru 
30120Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
30220Sigor@sysoev.ru 
30327Svbart@nginx.com     nxt_queue_remove(&c->link);
30427Svbart@nginx.com     nxt_queue_self(&c->link);
30527Svbart@nginx.com 
30627Svbart@nginx.com     b = c->read;
30727Svbart@nginx.com 
30827Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
30927Svbart@nginx.com 
31027Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
31127Svbart@nginx.com 
31227Svbart@nginx.com         if (rc == NXT_AGAIN) {
31327Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
31427Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
31527Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
31627Svbart@nginx.com                 return;
31727Svbart@nginx.com             }
31827Svbart@nginx.com 
31962Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
32027Svbart@nginx.com             return;
32127Svbart@nginx.com         }
32227Svbart@nginx.com 
32327Svbart@nginx.com         /* rc == NXT_ERROR */
32427Svbart@nginx.com 
32527Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
32627Svbart@nginx.com 
32727Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
32827Svbart@nginx.com         return;
32927Svbart@nginx.com     }
33027Svbart@nginx.com 
33167Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, r, task->log);
33260Svbart@nginx.com 
33360Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
33460Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
33560Svbart@nginx.com         return;
33660Svbart@nginx.com     }
33760Svbart@nginx.com 
33827Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
33927Svbart@nginx.com 
34027Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
341107Svbart@nginx.com                     "body length: %uz, preread: %uz",
34227Svbart@nginx.com                     r->length, preread);
34327Svbart@nginx.com 
34427Svbart@nginx.com     if (preread >= r->length) {
345140Svbart@nginx.com         nxt_controller_process_request(task, r);
34627Svbart@nginx.com         return;
34727Svbart@nginx.com     }
34827Svbart@nginx.com 
34927Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
35027Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
35127Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
35227Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
35327Svbart@nginx.com             return;
35427Svbart@nginx.com         }
35527Svbart@nginx.com 
35627Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
35727Svbart@nginx.com 
35827Svbart@nginx.com         c->read = b;
35927Svbart@nginx.com     }
36027Svbart@nginx.com 
36127Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
36227Svbart@nginx.com 
36362Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
36420Sigor@sysoev.ru }
36520Sigor@sysoev.ru 
36620Sigor@sysoev.ru 
36720Sigor@sysoev.ru static nxt_msec_t
36862Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
36920Sigor@sysoev.ru {
37020Sigor@sysoev.ru     return (nxt_msec_t) data;
37120Sigor@sysoev.ru }
37220Sigor@sysoev.ru 
37320Sigor@sysoev.ru 
37420Sigor@sysoev.ru static void
37520Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
37620Sigor@sysoev.ru {
37762Sigor@sysoev.ru     nxt_conn_t  *c;
37820Sigor@sysoev.ru 
37920Sigor@sysoev.ru     c = obj;
38020Sigor@sysoev.ru 
38120Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
38220Sigor@sysoev.ru 
38327Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
38420Sigor@sysoev.ru }
38520Sigor@sysoev.ru 
38620Sigor@sysoev.ru 
38720Sigor@sysoev.ru static void
38820Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
38920Sigor@sysoev.ru {
39062Sigor@sysoev.ru     nxt_timer_t  *timer;
39162Sigor@sysoev.ru     nxt_conn_t   *c;
39220Sigor@sysoev.ru 
39362Sigor@sysoev.ru     timer = obj;
39420Sigor@sysoev.ru 
39562Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
39620Sigor@sysoev.ru     c->socket.timedout = 1;
39720Sigor@sysoev.ru     c->socket.closed = 1;
39820Sigor@sysoev.ru 
39920Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
40020Sigor@sysoev.ru 
40127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
40227Svbart@nginx.com }
40327Svbart@nginx.com 
40427Svbart@nginx.com 
40527Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
40627Svbart@nginx.com     nxt_aligned(64) =
40727Svbart@nginx.com {
40856Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
40956Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
41056Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
41127Svbart@nginx.com 
41256Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
41356Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
41456Sigor@sysoev.ru     .timer_data = 60 * 1000,
41556Sigor@sysoev.ru     .timer_autoreset = 1,
41627Svbart@nginx.com };
41727Svbart@nginx.com 
41827Svbart@nginx.com 
41927Svbart@nginx.com static void
42027Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
42127Svbart@nginx.com {
422107Svbart@nginx.com     size_t                    read;
423107Svbart@nginx.com     nxt_buf_t                 *b;
424107Svbart@nginx.com     nxt_conn_t                *c;
425107Svbart@nginx.com     nxt_controller_request_t  *r;
42627Svbart@nginx.com 
42727Svbart@nginx.com     c = obj;
428107Svbart@nginx.com     r = data;
42927Svbart@nginx.com     b = c->read;
43027Svbart@nginx.com 
431107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
43227Svbart@nginx.com 
433107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
434107Svbart@nginx.com               read, r->length);
43527Svbart@nginx.com 
436107Svbart@nginx.com     if (read >= r->length) {
437140Svbart@nginx.com         nxt_controller_process_request(task, r);
43827Svbart@nginx.com         return;
43927Svbart@nginx.com     }
44027Svbart@nginx.com 
44162Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
44227Svbart@nginx.com }
44327Svbart@nginx.com 
44427Svbart@nginx.com 
44527Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
44627Svbart@nginx.com     nxt_aligned(64) =
44727Svbart@nginx.com {
44856Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
44956Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
45027Svbart@nginx.com 
45156Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
45256Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
45356Sigor@sysoev.ru     .timer_data = 60 * 1000,
45456Sigor@sysoev.ru     .timer_autoreset = 1,
45527Svbart@nginx.com };
45627Svbart@nginx.com 
45727Svbart@nginx.com 
45827Svbart@nginx.com static void
45927Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
46027Svbart@nginx.com {
46162Sigor@sysoev.ru     nxt_buf_t   *b;
46262Sigor@sysoev.ru     nxt_conn_t  *c;
46327Svbart@nginx.com 
46427Svbart@nginx.com     c = obj;
46527Svbart@nginx.com 
46627Svbart@nginx.com     nxt_debug(task, "controller conn write");
46727Svbart@nginx.com 
46827Svbart@nginx.com     b = c->write;
46927Svbart@nginx.com 
47027Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
47162Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
47227Svbart@nginx.com         return;
47327Svbart@nginx.com     }
47427Svbart@nginx.com 
47527Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
47627Svbart@nginx.com 
47727Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
47827Svbart@nginx.com }
47927Svbart@nginx.com 
48027Svbart@nginx.com 
48127Svbart@nginx.com static void
48227Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
48327Svbart@nginx.com {
48462Sigor@sysoev.ru     nxt_conn_t  *c;
48527Svbart@nginx.com 
48627Svbart@nginx.com     c = obj;
48727Svbart@nginx.com 
48827Svbart@nginx.com     nxt_debug(task, "controller conn write error");
48927Svbart@nginx.com 
49027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
49127Svbart@nginx.com }
49227Svbart@nginx.com 
49327Svbart@nginx.com 
49427Svbart@nginx.com static void
49527Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
49627Svbart@nginx.com {
49762Sigor@sysoev.ru     nxt_conn_t   *c;
49862Sigor@sysoev.ru     nxt_timer_t  *timer;
49927Svbart@nginx.com 
50062Sigor@sysoev.ru     timer = obj;
50127Svbart@nginx.com 
50262Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
50327Svbart@nginx.com     c->socket.timedout = 1;
50427Svbart@nginx.com     c->socket.closed = 1;
50527Svbart@nginx.com 
50627Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
50727Svbart@nginx.com 
50827Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
50920Sigor@sysoev.ru }
51020Sigor@sysoev.ru 
51120Sigor@sysoev.ru 
51220Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
51320Sigor@sysoev.ru     nxt_aligned(64) =
51420Sigor@sysoev.ru {
51556Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
51620Sigor@sysoev.ru };
51720Sigor@sysoev.ru 
51820Sigor@sysoev.ru 
51920Sigor@sysoev.ru static void
52020Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
52120Sigor@sysoev.ru {
52262Sigor@sysoev.ru     nxt_conn_t  *c;
52320Sigor@sysoev.ru 
52420Sigor@sysoev.ru     c = obj;
52520Sigor@sysoev.ru 
52620Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
52720Sigor@sysoev.ru 
52827Svbart@nginx.com     nxt_queue_remove(&c->link);
52927Svbart@nginx.com 
53020Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
53120Sigor@sysoev.ru 
53262Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
53320Sigor@sysoev.ru }
53420Sigor@sysoev.ru 
53520Sigor@sysoev.ru 
53620Sigor@sysoev.ru static void
53720Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
53820Sigor@sysoev.ru {
53962Sigor@sysoev.ru     nxt_conn_t  *c;
54020Sigor@sysoev.ru 
54120Sigor@sysoev.ru     c = obj;
54220Sigor@sysoev.ru 
54320Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
54420Sigor@sysoev.ru 
54565Sigor@sysoev.ru     nxt_mp_destroy(c->mem_pool);
54620Sigor@sysoev.ru 
54720Sigor@sysoev.ru     //nxt_free(c);
54820Sigor@sysoev.ru }
54927Svbart@nginx.com 
55027Svbart@nginx.com 
55127Svbart@nginx.com static nxt_int_t
55260Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
55367Svbart@nginx.com     nxt_log_t *log)
55427Svbart@nginx.com {
55527Svbart@nginx.com     off_t                     length;
55627Svbart@nginx.com     nxt_controller_request_t  *r;
55727Svbart@nginx.com 
55827Svbart@nginx.com     r = ctx;
55927Svbart@nginx.com 
56060Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
56127Svbart@nginx.com 
56227Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
563107Svbart@nginx.com 
564107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
565107Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
566107Svbart@nginx.com             return NXT_ERROR;
567107Svbart@nginx.com         }
56827Svbart@nginx.com 
56927Svbart@nginx.com         r->length = length;
57027Svbart@nginx.com         return NXT_OK;
57127Svbart@nginx.com     }
57227Svbart@nginx.com 
57360Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
57427Svbart@nginx.com 
57527Svbart@nginx.com     return NXT_ERROR;
57627Svbart@nginx.com }
57727Svbart@nginx.com 
57827Svbart@nginx.com 
57927Svbart@nginx.com static void
580140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
58127Svbart@nginx.com {
58265Sigor@sysoev.ru     nxt_mp_t                   *mp;
58351Svbart@nginx.com     nxt_int_t                  rc;
58446Svbart@nginx.com     nxt_str_t                  path;
585140Svbart@nginx.com     nxt_conn_t                 *c;
58651Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
587106Svbart@nginx.com     nxt_conf_op_t              *ops;
588106Svbart@nginx.com     nxt_conf_value_t           *value;
589208Svbart@nginx.com     nxt_conf_json_error_t      error;
59044Svbart@nginx.com     nxt_controller_response_t  resp;
59144Svbart@nginx.com 
59251Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
59351Svbart@nginx.com 
594140Svbart@nginx.com     c = req->conn;
595112Smax.romanov@nginx.com     path = req->parser.path;
59651Svbart@nginx.com 
59751Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
59851Svbart@nginx.com         path.length--;
59951Svbart@nginx.com     }
60051Svbart@nginx.com 
60144Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
60244Svbart@nginx.com 
60344Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
60446Svbart@nginx.com 
605106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
60651Svbart@nginx.com 
60751Svbart@nginx.com         if (value == NULL) {
608208Svbart@nginx.com             goto not_found;
60951Svbart@nginx.com         }
61051Svbart@nginx.com 
611208Svbart@nginx.com         resp.status = 200;
612106Svbart@nginx.com         resp.conf = value;
61346Svbart@nginx.com 
614208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
615208Svbart@nginx.com         return;
61651Svbart@nginx.com     }
61751Svbart@nginx.com 
61851Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
61946Svbart@nginx.com 
620238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
621238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
622238Svbart@nginx.com             return;
623238Svbart@nginx.com         }
624238Svbart@nginx.com 
62565Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
62651Svbart@nginx.com 
62751Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
628208Svbart@nginx.com             goto alloc_fail;
62946Svbart@nginx.com         }
63046Svbart@nginx.com 
63151Svbart@nginx.com         mbuf = &c->read->mem;
63251Svbart@nginx.com 
633208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
634208Svbart@nginx.com 
635208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
63651Svbart@nginx.com 
63751Svbart@nginx.com         if (value == NULL) {
63865Sigor@sysoev.ru             nxt_mp_destroy(mp);
639208Svbart@nginx.com 
640208Svbart@nginx.com             if (error.pos == NULL) {
641208Svbart@nginx.com                 goto alloc_fail;
642208Svbart@nginx.com             }
643208Svbart@nginx.com 
644208Svbart@nginx.com             resp.status = 400;
645208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
646208Svbart@nginx.com             resp.detail = error.detail;
647208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
648208Svbart@nginx.com 
649208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
650208Svbart@nginx.com                                    &resp.line, &resp.column);
651208Svbart@nginx.com 
652208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
653208Svbart@nginx.com             return;
65451Svbart@nginx.com         }
65551Svbart@nginx.com 
65651Svbart@nginx.com         if (path.length != 1) {
657106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
658106Svbart@nginx.com                                      nxt_controller_conf.root,
659106Svbart@nginx.com                                      &path, value);
66046Svbart@nginx.com 
66151Svbart@nginx.com             if (rc != NXT_OK) {
66251Svbart@nginx.com                 if (rc == NXT_DECLINED) {
663208Svbart@nginx.com                     goto not_found;
66451Svbart@nginx.com                 }
66546Svbart@nginx.com 
666208Svbart@nginx.com                 goto alloc_fail;
66751Svbart@nginx.com             }
66851Svbart@nginx.com 
669106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
67051Svbart@nginx.com 
67151Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
67265Sigor@sysoev.ru                 nxt_mp_destroy(mp);
673208Svbart@nginx.com                 goto alloc_fail;
67451Svbart@nginx.com             }
67546Svbart@nginx.com         }
67644Svbart@nginx.com 
677116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
678121Svbart@nginx.com             nxt_mp_destroy(mp);
679208Svbart@nginx.com             goto invalid_conf;
680116Svbart@nginx.com         }
681116Svbart@nginx.com 
682140Svbart@nginx.com         req->conf.root = value;
683140Svbart@nginx.com         req->conf.pool = mp;
684140Svbart@nginx.com 
685247Svbart@nginx.com         rc = nxt_controller_conf_apply(task, req);
686247Svbart@nginx.com 
687247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
688121Svbart@nginx.com             nxt_mp_destroy(mp);
689247Svbart@nginx.com 
690247Svbart@nginx.com             if (rc == NXT_DECLINED) {
691247Svbart@nginx.com                 goto no_router;
692247Svbart@nginx.com             }
693247Svbart@nginx.com 
694247Svbart@nginx.com             /* rc == NXT_ERROR */
695208Svbart@nginx.com             goto alloc_fail;
696121Svbart@nginx.com         }
697121Svbart@nginx.com 
698140Svbart@nginx.com         return;
69951Svbart@nginx.com     }
70027Svbart@nginx.com 
70151Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
70251Svbart@nginx.com 
703238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
704238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
705238Svbart@nginx.com             return;
706238Svbart@nginx.com         }
707238Svbart@nginx.com 
70851Svbart@nginx.com         if (path.length == 1) {
70965Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
71044Svbart@nginx.com 
71151Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
712208Svbart@nginx.com                 goto alloc_fail;
71351Svbart@nginx.com             }
71451Svbart@nginx.com 
715106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
71627Svbart@nginx.com 
71744Svbart@nginx.com         } else {
718106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
719106Svbart@nginx.com                                      nxt_controller_conf.root,
720106Svbart@nginx.com                                      &path, NULL);
72151Svbart@nginx.com 
72251Svbart@nginx.com             if (rc != NXT_OK) {
72351Svbart@nginx.com                 if (rc == NXT_DECLINED) {
724208Svbart@nginx.com                     goto not_found;
72551Svbart@nginx.com                 }
72651Svbart@nginx.com 
727208Svbart@nginx.com                 goto alloc_fail;
72851Svbart@nginx.com             }
72951Svbart@nginx.com 
73065Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
73151Svbart@nginx.com 
73251Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
733208Svbart@nginx.com                 goto alloc_fail;
73451Svbart@nginx.com             }
73551Svbart@nginx.com 
736106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
73751Svbart@nginx.com         }
73851Svbart@nginx.com 
73951Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
74065Sigor@sysoev.ru             nxt_mp_destroy(mp);
741208Svbart@nginx.com             goto alloc_fail;
74244Svbart@nginx.com         }
74344Svbart@nginx.com 
744116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
745121Svbart@nginx.com             nxt_mp_destroy(mp);
746208Svbart@nginx.com             goto invalid_conf;
747116Svbart@nginx.com         }
748116Svbart@nginx.com 
749140Svbart@nginx.com         req->conf.root = value;
750140Svbart@nginx.com         req->conf.pool = mp;
751140Svbart@nginx.com 
752247Svbart@nginx.com         rc = nxt_controller_conf_apply(task, req);
753247Svbart@nginx.com 
754247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
755121Svbart@nginx.com             nxt_mp_destroy(mp);
756247Svbart@nginx.com 
757247Svbart@nginx.com             if (rc == NXT_DECLINED) {
758247Svbart@nginx.com                 goto no_router;
759247Svbart@nginx.com             }
760247Svbart@nginx.com 
761247Svbart@nginx.com             /* rc == NXT_ERROR */
762208Svbart@nginx.com             goto alloc_fail;
763121Svbart@nginx.com         }
764121Svbart@nginx.com 
765140Svbart@nginx.com         return;
76651Svbart@nginx.com     }
76751Svbart@nginx.com 
768208Svbart@nginx.com     resp.status = 405;
769208Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
770208Svbart@nginx.com     resp.offset = -1;
77151Svbart@nginx.com 
772208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
773208Svbart@nginx.com     return;
77451Svbart@nginx.com 
775208Svbart@nginx.com not_found:
776208Svbart@nginx.com 
777208Svbart@nginx.com     resp.status = 404;
778208Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
779208Svbart@nginx.com     resp.offset = -1;
780208Svbart@nginx.com 
781208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
782208Svbart@nginx.com     return;
783208Svbart@nginx.com 
784208Svbart@nginx.com invalid_conf:
785208Svbart@nginx.com 
786208Svbart@nginx.com     resp.status = 400;
787208Svbart@nginx.com     resp.title = (u_char *) "Invalid configuration.";
788208Svbart@nginx.com     resp.offset = -1;
789208Svbart@nginx.com 
790208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
791208Svbart@nginx.com     return;
792247Svbart@nginx.com 
793247Svbart@nginx.com alloc_fail:
794247Svbart@nginx.com 
795247Svbart@nginx.com     resp.status = 500;
796247Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
797247Svbart@nginx.com     resp.offset = -1;
798247Svbart@nginx.com 
799247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
800247Svbart@nginx.com     return;
801247Svbart@nginx.com 
802247Svbart@nginx.com no_router:
803247Svbart@nginx.com 
804247Svbart@nginx.com     resp.status = 500;
805247Svbart@nginx.com     resp.title = (u_char *) "Router process isn't available.";
806247Svbart@nginx.com     resp.offset = -1;
807247Svbart@nginx.com 
808247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
809247Svbart@nginx.com     return;
81027Svbart@nginx.com }
81127Svbart@nginx.com 
81227Svbart@nginx.com 
81327Svbart@nginx.com static nxt_int_t
814140Svbart@nginx.com nxt_controller_conf_apply(nxt_task_t *task, nxt_controller_request_t *req)
815140Svbart@nginx.com {
816238Svbart@nginx.com     size_t         size;
817238Svbart@nginx.com     uint32_t       stream;
818238Svbart@nginx.com     nxt_int_t      rc;
819238Svbart@nginx.com     nxt_buf_t      *b;
820238Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
821238Svbart@nginx.com     nxt_runtime_t  *rt;
822238Svbart@nginx.com 
823238Svbart@nginx.com     rt = task->thread->runtime;
824238Svbart@nginx.com 
825238Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
826247Svbart@nginx.com 
827247Svbart@nginx.com     if (nxt_slow_path(router_port == NULL)) {
828247Svbart@nginx.com         return NXT_DECLINED;
829247Svbart@nginx.com     }
830247Svbart@nginx.com 
831238Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
832140Svbart@nginx.com 
833238Svbart@nginx.com     size = nxt_conf_json_length(req->conf.root, NULL);
834238Svbart@nginx.com 
835238Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
836238Svbart@nginx.com 
837238Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, req->conf.root, NULL);
838140Svbart@nginx.com 
839238Svbart@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
840238Svbart@nginx.com                                            nxt_controller_conf_handler,
841238Svbart@nginx.com                                            nxt_controller_conf_handler,
842238Svbart@nginx.com                                            router_port->pid, req);
843238Svbart@nginx.com 
844238Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
845238Svbart@nginx.com                                stream, controller_port->id, b);
846140Svbart@nginx.com 
847140Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
848238Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
849140Svbart@nginx.com         return NXT_ERROR;
850140Svbart@nginx.com     }
851140Svbart@nginx.com 
852238Svbart@nginx.com     nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
853140Svbart@nginx.com 
854140Svbart@nginx.com     return NXT_OK;
855140Svbart@nginx.com }
856140Svbart@nginx.com 
857140Svbart@nginx.com 
858193Smax.romanov@nginx.com static void
859193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
860193Smax.romanov@nginx.com     void *data)
861140Svbart@nginx.com {
862238Svbart@nginx.com     nxt_queue_t                queue;
863140Svbart@nginx.com     nxt_controller_request_t   *req;
864140Svbart@nginx.com     nxt_controller_response_t  resp;
865140Svbart@nginx.com 
866238Svbart@nginx.com     req = data;
867238Svbart@nginx.com 
868201Svbart@nginx.com     nxt_debug(task, "controller conf ready: %*s",
869201Svbart@nginx.com               nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos);
870140Svbart@nginx.com 
871238Svbart@nginx.com     nxt_queue_remove(&req->link);
872140Svbart@nginx.com 
873238Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
874140Svbart@nginx.com 
875193Smax.romanov@nginx.com     if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) {
876140Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
877140Svbart@nginx.com 
878140Svbart@nginx.com         nxt_controller_conf = req->conf;
879140Svbart@nginx.com 
880208Svbart@nginx.com         resp.status = 200;
881208Svbart@nginx.com         resp.title = (u_char *) "Reconfiguration done.";
882140Svbart@nginx.com 
883140Svbart@nginx.com     } else {
884140Svbart@nginx.com         nxt_mp_destroy(req->conf.pool);
885140Svbart@nginx.com 
886208Svbart@nginx.com         resp.status = 500;
887208Svbart@nginx.com         resp.title = (u_char *) "Failed to apply new configuration.";
888208Svbart@nginx.com         resp.offset = -1;
889140Svbart@nginx.com     }
890140Svbart@nginx.com 
891140Svbart@nginx.com     nxt_controller_response(task, req, &resp);
892140Svbart@nginx.com 
893238Svbart@nginx.com     nxt_queue_init(&queue);
894238Svbart@nginx.com     nxt_queue_add(&queue, &nxt_controller_waiting_requests);
895140Svbart@nginx.com 
896238Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
897121Svbart@nginx.com 
898238Svbart@nginx.com     nxt_queue_each(req, &queue, nxt_controller_request_t, link) {
899238Svbart@nginx.com         nxt_controller_process_request(task, req);
900238Svbart@nginx.com     } nxt_queue_loop;
901121Svbart@nginx.com }
902121Svbart@nginx.com 
903121Svbart@nginx.com 
904140Svbart@nginx.com static void
905140Svbart@nginx.com nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req,
90644Svbart@nginx.com     nxt_controller_response_t *resp)
90733Svbart@nginx.com {
908208Svbart@nginx.com     size_t                  size;
909208Svbart@nginx.com     nxt_str_t               status_line, str;
910208Svbart@nginx.com     nxt_buf_t               *b, *body;
911208Svbart@nginx.com     nxt_conn_t              *c;
912208Svbart@nginx.com     nxt_uint_t              n;
913208Svbart@nginx.com     nxt_conf_value_t        *value, *location;
914208Svbart@nginx.com     nxt_conf_json_pretty_t  pretty;
915208Svbart@nginx.com 
916208Svbart@nginx.com     static nxt_str_t  success_str = nxt_string("success");
917208Svbart@nginx.com     static nxt_str_t  error_str = nxt_string("error");
918208Svbart@nginx.com     static nxt_str_t  detail_str = nxt_string("detail");
919208Svbart@nginx.com     static nxt_str_t  location_str = nxt_string("location");
920208Svbart@nginx.com     static nxt_str_t  offset_str = nxt_string("offset");
921208Svbart@nginx.com     static nxt_str_t  line_str = nxt_string("line");
922208Svbart@nginx.com     static nxt_str_t  column_str = nxt_string("column");
923208Svbart@nginx.com 
924208Svbart@nginx.com     static nxt_time_string_t  date_cache = {
925208Svbart@nginx.com         (nxt_atomic_uint_t) -1,
926208Svbart@nginx.com         nxt_controller_date,
927208Svbart@nginx.com         "%s, %02d %s %4d %02d:%02d:%02d GMT",
928208Svbart@nginx.com         sizeof("Wed, 31 Dec 1986 16:40:00 GMT") - 1,
929208Svbart@nginx.com         NXT_THREAD_TIME_GMT,
930208Svbart@nginx.com         NXT_THREAD_TIME_SEC,
931208Svbart@nginx.com     };
932208Svbart@nginx.com 
933208Svbart@nginx.com     switch (resp->status) {
934208Svbart@nginx.com 
935208Svbart@nginx.com     case 200:
936208Svbart@nginx.com         nxt_str_set(&status_line, "200 OK");
937208Svbart@nginx.com         break;
938208Svbart@nginx.com 
939208Svbart@nginx.com     case 400:
940208Svbart@nginx.com         nxt_str_set(&status_line, "400 Bad Request");
941208Svbart@nginx.com         break;
942208Svbart@nginx.com 
943208Svbart@nginx.com     case 404:
944208Svbart@nginx.com         nxt_str_set(&status_line, "404 Not Found");
945208Svbart@nginx.com         break;
946208Svbart@nginx.com 
947208Svbart@nginx.com     case 405:
948208Svbart@nginx.com         nxt_str_set(&status_line, "405 Method Not Allowed");
949208Svbart@nginx.com         break;
950208Svbart@nginx.com 
951209Svbart@nginx.com     default:
952208Svbart@nginx.com         nxt_str_set(&status_line, "500 Internal Server Error");
953208Svbart@nginx.com         break;
954208Svbart@nginx.com     }
955140Svbart@nginx.com 
956140Svbart@nginx.com     c = req->conn;
957208Svbart@nginx.com     value = resp->conf;
95833Svbart@nginx.com 
959208Svbart@nginx.com     if (value == NULL) {
960208Svbart@nginx.com         n = 1
961208Svbart@nginx.com             + (resp->detail != NULL)
962208Svbart@nginx.com             + (resp->status >= 400 && resp->offset != -1);
963208Svbart@nginx.com 
964208Svbart@nginx.com         value = nxt_conf_create_object(c->mem_pool, n);
965208Svbart@nginx.com 
966208Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
967208Svbart@nginx.com             nxt_controller_conn_close(task, c, req);
968208Svbart@nginx.com             return;
969208Svbart@nginx.com         }
970208Svbart@nginx.com 
971208Svbart@nginx.com         str.length = nxt_strlen(resp->title);
972208Svbart@nginx.com         str.start = resp->title;
973208Svbart@nginx.com 
974208Svbart@nginx.com         if (resp->status < 400) {
975208Svbart@nginx.com             nxt_conf_set_member_string(value, &success_str, &str, 0);
976208Svbart@nginx.com 
977208Svbart@nginx.com         } else {
978208Svbart@nginx.com             nxt_conf_set_member_string(value, &error_str, &str, 0);
979208Svbart@nginx.com         }
980208Svbart@nginx.com 
981208Svbart@nginx.com         n = 0;
982208Svbart@nginx.com 
983208Svbart@nginx.com         if (resp->detail != NULL) {
984208Svbart@nginx.com             str.length = nxt_strlen(resp->detail);
985208Svbart@nginx.com             str.start = resp->detail;
986208Svbart@nginx.com 
987208Svbart@nginx.com             n++;
988208Svbart@nginx.com 
989208Svbart@nginx.com             nxt_conf_set_member_string(value, &detail_str, &str, n);
990208Svbart@nginx.com         }
991208Svbart@nginx.com 
992208Svbart@nginx.com         if (resp->status >= 400 && resp->offset != -1) {
993208Svbart@nginx.com             n++;
994208Svbart@nginx.com 
995208Svbart@nginx.com             location = nxt_conf_create_object(c->mem_pool,
996208Svbart@nginx.com                                               resp->line != 0 ? 3 : 1);
997208Svbart@nginx.com 
998208Svbart@nginx.com             nxt_conf_set_member(value, &location_str, location, n);
999208Svbart@nginx.com 
1000208Svbart@nginx.com             nxt_conf_set_member_integer(location, &offset_str, resp->offset, 0);
1001208Svbart@nginx.com 
1002208Svbart@nginx.com             if (resp->line != 0) {
1003208Svbart@nginx.com                 nxt_conf_set_member_integer(location, &line_str,
1004208Svbart@nginx.com                                             resp->line, 1);
1005208Svbart@nginx.com 
1006208Svbart@nginx.com                 nxt_conf_set_member_integer(location, &column_str,
1007208Svbart@nginx.com                                             resp->column, 2);
1008208Svbart@nginx.com             }
1009208Svbart@nginx.com         }
1010208Svbart@nginx.com     }
1011208Svbart@nginx.com 
1012208Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
1013208Svbart@nginx.com 
1014208Svbart@nginx.com     size = nxt_conf_json_length(value, &pretty) + 2;
1015208Svbart@nginx.com 
1016208Svbart@nginx.com     body = nxt_buf_mem_alloc(c->mem_pool, size, 0);
1017208Svbart@nginx.com     if (nxt_slow_path(body == NULL)) {
1018208Svbart@nginx.com         nxt_controller_conn_close(task, c, req);
1019208Svbart@nginx.com         return;
1020208Svbart@nginx.com     }
1021208Svbart@nginx.com 
1022208Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
1023208Svbart@nginx.com 
1024208Svbart@nginx.com     body->mem.free = nxt_conf_json_print(body->mem.free, value, &pretty);
1025208Svbart@nginx.com