xref: /unit/src/nxt_controller.c (revision 249)
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 
41248Svbart@nginx.com static void nxt_controller_process_new_port_handler(nxt_task_t *task,
42248Svbart@nginx.com     nxt_port_recv_msg_t *msg);
43*249Svbart@nginx.com static nxt_int_t nxt_controller_conf_default(void);
44*249Svbart@nginx.com static void nxt_controller_conf_init_handler(nxt_task_t *task,
45*249Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
46*249Svbart@nginx.com static nxt_int_t nxt_controller_conf_send(nxt_task_t *task,
47*249Svbart@nginx.com     nxt_conf_value_t *conf, nxt_port_rpc_handler_t handler, void *data);
48248Svbart@nginx.com 
4920Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
5020Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
5162Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
5220Sigor@sysoev.ru     uintptr_t data);
5320Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
5420Sigor@sysoev.ru     void *data);
5520Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
5620Sigor@sysoev.ru     void *data);
5727Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
5827Svbart@nginx.com     void *data);
5927Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
6027Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
6127Svbart@nginx.com     void *data);
6227Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
6327Svbart@nginx.com     void *data);
6420Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
6520Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
6620Sigor@sysoev.ru 
6727Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
6867Svbart@nginx.com     nxt_http_field_t *field, nxt_log_t *log);
6927Svbart@nginx.com 
7027Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
71140Svbart@nginx.com     nxt_controller_request_t *req);
72238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task,
73238Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
74140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
75140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
76208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
77208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
7827Svbart@nginx.com 
7927Svbart@nginx.com 
8060Svbart@nginx.com static nxt_http_fields_hash_entry_t  nxt_controller_request_fields[] = {
8127Svbart@nginx.com     { nxt_string("Content-Length"),
8227Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
8327Svbart@nginx.com 
8427Svbart@nginx.com     { nxt_null_string, NULL, 0 }
8527Svbart@nginx.com };
8627Svbart@nginx.com 
8760Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_fields_hash;
8827Svbart@nginx.com 
89238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
90238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
9127Svbart@nginx.com 
9220Sigor@sysoev.ru 
9320Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
9427Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
9527Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
9620Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
9720Sigor@sysoev.ru 
9820Sigor@sysoev.ru 
99248Svbart@nginx.com nxt_port_handler_t  nxt_controller_process_port_handlers[] = {
100248Svbart@nginx.com     nxt_worker_process_quit_handler,
101248Svbart@nginx.com     nxt_controller_process_new_port_handler,
102248Svbart@nginx.com     nxt_port_change_log_file_handler,
103248Svbart@nginx.com     nxt_port_mmap_handler,
104248Svbart@nginx.com     nxt_port_data_handler,
105248Svbart@nginx.com     nxt_port_remove_pid_handler,
106248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_READY        */
107248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_START_WORKER */
108248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_SOCKET       */
109248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_MODULES      */
110248Svbart@nginx.com     nxt_port_rpc_handler,
111248Svbart@nginx.com     nxt_port_rpc_handler,
112248Svbart@nginx.com };
113248Svbart@nginx.com 
114248Svbart@nginx.com 
11520Sigor@sysoev.ru nxt_int_t
116141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
11720Sigor@sysoev.ru {
118141Smax.romanov@nginx.com     nxt_runtime_t           *rt;
11927Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
12027Svbart@nginx.com 
121141Smax.romanov@nginx.com     rt = task->thread->runtime;
122141Smax.romanov@nginx.com 
12360Svbart@nginx.com     hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
12460Svbart@nginx.com                                        rt->mem_pool);
12527Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
12627Svbart@nginx.com         return NXT_ERROR;
12727Svbart@nginx.com     }
12827Svbart@nginx.com 
12960Svbart@nginx.com     nxt_controller_fields_hash = hash;
130248Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
13127Svbart@nginx.com 
132248Svbart@nginx.com     return NXT_OK;
133248Svbart@nginx.com }
134248Svbart@nginx.com 
135248Svbart@nginx.com 
136248Svbart@nginx.com static void
137248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task,
138248Svbart@nginx.com     nxt_port_recv_msg_t *msg)
139248Svbart@nginx.com {
140*249Svbart@nginx.com     nxt_int_t         rc;
141248Svbart@nginx.com     nxt_runtime_t     *rt;
142248Svbart@nginx.com     nxt_conf_value_t  *conf;
143248Svbart@nginx.com 
144248Svbart@nginx.com     nxt_port_new_port_handler(task, msg);
145248Svbart@nginx.com 
146*249Svbart@nginx.com     if (msg->new_port->type != NXT_PROCESS_ROUTER) {
147248Svbart@nginx.com         return;
14820Sigor@sysoev.ru     }
14920Sigor@sysoev.ru 
150*249Svbart@nginx.com     conf = nxt_controller_conf.root;
151*249Svbart@nginx.com 
152*249Svbart@nginx.com     if (conf != NULL) {
153*249Svbart@nginx.com         rc = nxt_controller_conf_send(task, conf,
154*249Svbart@nginx.com                                       nxt_controller_conf_init_handler, NULL);
15544Svbart@nginx.com 
156*249Svbart@nginx.com         if (nxt_fast_path(rc == NXT_OK)) {
157*249Svbart@nginx.com             return;
158*249Svbart@nginx.com         }
159*249Svbart@nginx.com 
160*249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
161*249Svbart@nginx.com 
162*249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
163*249Svbart@nginx.com             nxt_abort();
164*249Svbart@nginx.com         }
16544Svbart@nginx.com     }
16644Svbart@nginx.com 
167*249Svbart@nginx.com     if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
168248Svbart@nginx.com         nxt_abort();
16944Svbart@nginx.com     }
17044Svbart@nginx.com 
171248Svbart@nginx.com     rt = task->thread->runtime;
172140Svbart@nginx.com 
173248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
174248Svbart@nginx.com         nxt_abort();
175248Svbart@nginx.com     }
17620Sigor@sysoev.ru }
17720Sigor@sysoev.ru 
17820Sigor@sysoev.ru 
179*249Svbart@nginx.com static nxt_int_t
180*249Svbart@nginx.com nxt_controller_conf_default(void)
181*249Svbart@nginx.com {
182*249Svbart@nginx.com     nxt_mp_t          *mp;
183*249Svbart@nginx.com     nxt_conf_value_t  *conf;
184*249Svbart@nginx.com 
185*249Svbart@nginx.com     static const nxt_str_t json
186*249Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
187*249Svbart@nginx.com 
188*249Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
189*249Svbart@nginx.com 
190*249Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
191*249Svbart@nginx.com         return NXT_ERROR;
192*249Svbart@nginx.com     }
193*249Svbart@nginx.com 
194*249Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
195*249Svbart@nginx.com 
196*249Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
197*249Svbart@nginx.com         return NXT_ERROR;
198*249Svbart@nginx.com     }
199*249Svbart@nginx.com 
200*249Svbart@nginx.com     nxt_controller_conf.root = conf;
201*249Svbart@nginx.com     nxt_controller_conf.pool = mp;
202*249Svbart@nginx.com 
203*249Svbart@nginx.com     return NXT_OK;
204*249Svbart@nginx.com }
205*249Svbart@nginx.com 
206*249Svbart@nginx.com 
207*249Svbart@nginx.com static void
208*249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
209*249Svbart@nginx.com     void *data)
210*249Svbart@nginx.com {
211*249Svbart@nginx.com     if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) {
212*249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
213*249Svbart@nginx.com 
214*249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
215*249Svbart@nginx.com             nxt_abort();
216*249Svbart@nginx.com         }
217*249Svbart@nginx.com     }
218*249Svbart@nginx.com }
219*249Svbart@nginx.com 
220*249Svbart@nginx.com 
221*249Svbart@nginx.com static nxt_int_t
222*249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf,
223*249Svbart@nginx.com     nxt_port_rpc_handler_t handler, void *data)
224*249Svbart@nginx.com {
225*249Svbart@nginx.com     size_t         size;
226*249Svbart@nginx.com     uint32_t       stream;
227*249Svbart@nginx.com     nxt_int_t      rc;
228*249Svbart@nginx.com     nxt_buf_t      *b;
229*249Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
230*249Svbart@nginx.com     nxt_runtime_t  *rt;
231*249Svbart@nginx.com 
232*249Svbart@nginx.com     rt = task->thread->runtime;
233*249Svbart@nginx.com 
234*249Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
235*249Svbart@nginx.com 
236*249Svbart@nginx.com     if (nxt_slow_path(router_port == NULL)) {
237*249Svbart@nginx.com         return NXT_DECLINED;
238*249Svbart@nginx.com     }
239*249Svbart@nginx.com 
240*249Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
241*249Svbart@nginx.com 
242*249Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
243*249Svbart@nginx.com 
244*249Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
245*249Svbart@nginx.com 
246*249Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
247*249Svbart@nginx.com 
248*249Svbart@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
249*249Svbart@nginx.com                                            handler, handler,
250*249Svbart@nginx.com                                            router_port->pid, data);
251*249Svbart@nginx.com 
252*249Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
253*249Svbart@nginx.com                                stream, controller_port->id, b);
254*249Svbart@nginx.com 
255*249Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
256*249Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
257*249Svbart@nginx.com         return NXT_ERROR;
258*249Svbart@nginx.com     }
259*249Svbart@nginx.com 
260*249Svbart@nginx.com     return NXT_OK;
261*249Svbart@nginx.com }
262*249Svbart@nginx.com 
263*249Svbart@nginx.com 
26420Sigor@sysoev.ru nxt_int_t
26520Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
26620Sigor@sysoev.ru {
26720Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
26820Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
26920Sigor@sysoev.ru 
27020Sigor@sysoev.ru     sa = rt->controller_listen;
27120Sigor@sysoev.ru 
27265Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
27320Sigor@sysoev.ru     if (ls == NULL) {
27420Sigor@sysoev.ru         return NXT_ERROR;
27520Sigor@sysoev.ru     }
27620Sigor@sysoev.ru 
27720Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
27820Sigor@sysoev.ru                                        sa->socklen, sa->length);
27920Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
28020Sigor@sysoev.ru         return NXT_ERROR;
28120Sigor@sysoev.ru     }
28220Sigor@sysoev.ru 
28320Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
284103Sigor@sysoev.ru     ls->socklen = sa->socklen;
285103Sigor@sysoev.ru     ls->address_length = sa->length;
28620Sigor@sysoev.ru 
28720Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
28820Sigor@sysoev.ru 
28920Sigor@sysoev.ru     ls->socket = -1;
29020Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
29120Sigor@sysoev.ru     ls->read_after_accept = 1;
29220Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
29320Sigor@sysoev.ru 
29420Sigor@sysoev.ru #if 0
29520Sigor@sysoev.ru     /* STUB */
29665Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
29720Sigor@sysoev.ru     if (wq == NULL) {
29820Sigor@sysoev.ru         return NXT_ERROR;
29920Sigor@sysoev.ru     }
30020Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
30120Sigor@sysoev.ru     /**/
30220Sigor@sysoev.ru 
30320Sigor@sysoev.ru     ls->work_queue = wq;
30420Sigor@sysoev.ru #endif
30520Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
30620Sigor@sysoev.ru 
30720Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
30820Sigor@sysoev.ru         return NXT_ERROR;
30920Sigor@sysoev.ru     }
31020Sigor@sysoev.ru 
31120Sigor@sysoev.ru     rt->controller_socket = ls;
31220Sigor@sysoev.ru 
31320Sigor@sysoev.ru     return NXT_OK;
31420Sigor@sysoev.ru }
31520Sigor@sysoev.ru 
31620Sigor@sysoev.ru 
31720Sigor@sysoev.ru static void
31820Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
31920Sigor@sysoev.ru {
32027Svbart@nginx.com     nxt_buf_t                 *b;
32162Sigor@sysoev.ru     nxt_conn_t                *c;
32227Svbart@nginx.com     nxt_event_engine_t        *engine;
32327Svbart@nginx.com     nxt_controller_request_t  *r;
32420Sigor@sysoev.ru 
32520Sigor@sysoev.ru     c = obj;
32620Sigor@sysoev.ru 
32720Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
32820Sigor@sysoev.ru 
32965Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
33027Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
33127Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
33227Svbart@nginx.com         return;
33327Svbart@nginx.com     }
33427Svbart@nginx.com 
335140Svbart@nginx.com     r->conn = c;
336140Svbart@nginx.com 
33760Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
33860Svbart@nginx.com                       != NXT_OK))
33960Svbart@nginx.com     {
34060Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
34160Svbart@nginx.com         return;
34260Svbart@nginx.com     }
34327Svbart@nginx.com 
34467Svbart@nginx.com     r->parser.fields_hash = nxt_controller_fields_hash;
34567Svbart@nginx.com 
34620Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
34720Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
34820Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
34920Sigor@sysoev.ru         return;
35020Sigor@sysoev.ru     }
35120Sigor@sysoev.ru 
35220Sigor@sysoev.ru     c->read = b;
35327Svbart@nginx.com     c->socket.data = r;
35420Sigor@sysoev.ru     c->socket.read_ready = 1;
35520Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
35620Sigor@sysoev.ru 
35720Sigor@sysoev.ru     engine = task->thread->engine;
35820Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
35927Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
36020Sigor@sysoev.ru 
36162Sigor@sysoev.ru     nxt_conn_read(engine, c);
36220Sigor@sysoev.ru }
36320Sigor@sysoev.ru 
36420Sigor@sysoev.ru 
36520Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
36620Sigor@sysoev.ru     nxt_aligned(64) =
36720Sigor@sysoev.ru {
36856Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
36956Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
37056Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
37120Sigor@sysoev.ru 
37256Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
37356Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
37456Sigor@sysoev.ru     .timer_data = 60 * 1000,
37520Sigor@sysoev.ru };
37620Sigor@sysoev.ru 
37720Sigor@sysoev.ru 
37820Sigor@sysoev.ru static void
37920Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
38020Sigor@sysoev.ru {
38127Svbart@nginx.com     size_t                    preread;
38227Svbart@nginx.com     nxt_buf_t                 *b;
38327Svbart@nginx.com     nxt_int_t                 rc;
38462Sigor@sysoev.ru     nxt_conn_t                *c;
38527Svbart@nginx.com     nxt_controller_request_t  *r;
38620Sigor@sysoev.ru 
38720Sigor@sysoev.ru     c = obj;
38827Svbart@nginx.com     r = data;
38920Sigor@sysoev.ru 
39020Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
39120Sigor@sysoev.ru 
39227Svbart@nginx.com     nxt_queue_remove(&c->link);
39327Svbart@nginx.com     nxt_queue_self(&c->link);
39427Svbart@nginx.com 
39527Svbart@nginx.com     b = c->read;
39627Svbart@nginx.com 
39727Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
39827Svbart@nginx.com 
39927Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
40027Svbart@nginx.com 
40127Svbart@nginx.com         if (rc == NXT_AGAIN) {
40227Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
40327Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
40427Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
40527Svbart@nginx.com                 return;
40627Svbart@nginx.com             }
40727Svbart@nginx.com 
40862Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
40927Svbart@nginx.com             return;
41027Svbart@nginx.com         }
41127Svbart@nginx.com 
41227Svbart@nginx.com         /* rc == NXT_ERROR */
41327Svbart@nginx.com 
41427Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
41527Svbart@nginx.com 
41627Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
41727Svbart@nginx.com         return;
41827Svbart@nginx.com     }
41927Svbart@nginx.com 
42067Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, r, task->log);
42160Svbart@nginx.com 
42260Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
42360Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
42460Svbart@nginx.com         return;
42560Svbart@nginx.com     }
42660Svbart@nginx.com 
42727Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
42827Svbart@nginx.com 
42927Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
430107Svbart@nginx.com                     "body length: %uz, preread: %uz",
43127Svbart@nginx.com                     r->length, preread);
43227Svbart@nginx.com 
43327Svbart@nginx.com     if (preread >= r->length) {
434140Svbart@nginx.com         nxt_controller_process_request(task, r);
43527Svbart@nginx.com         return;
43627Svbart@nginx.com     }
43727Svbart@nginx.com 
43827Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
43927Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
44027Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
44127Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
44227Svbart@nginx.com             return;
44327Svbart@nginx.com         }
44427Svbart@nginx.com 
44527Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
44627Svbart@nginx.com 
44727Svbart@nginx.com         c->read = b;
44827Svbart@nginx.com     }
44927Svbart@nginx.com 
45027Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
45127Svbart@nginx.com 
45262Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
45320Sigor@sysoev.ru }
45420Sigor@sysoev.ru 
45520Sigor@sysoev.ru 
45620Sigor@sysoev.ru static nxt_msec_t
45762Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
45820Sigor@sysoev.ru {
45920Sigor@sysoev.ru     return (nxt_msec_t) data;
46020Sigor@sysoev.ru }
46120Sigor@sysoev.ru 
46220Sigor@sysoev.ru 
46320Sigor@sysoev.ru static void
46420Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
46520Sigor@sysoev.ru {
46662Sigor@sysoev.ru     nxt_conn_t  *c;
46720Sigor@sysoev.ru 
46820Sigor@sysoev.ru     c = obj;
46920Sigor@sysoev.ru 
47020Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
47120Sigor@sysoev.ru 
47227Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
47320Sigor@sysoev.ru }
47420Sigor@sysoev.ru 
47520Sigor@sysoev.ru 
47620Sigor@sysoev.ru static void
47720Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
47820Sigor@sysoev.ru {
47962Sigor@sysoev.ru     nxt_timer_t  *timer;
48062Sigor@sysoev.ru     nxt_conn_t   *c;
48120Sigor@sysoev.ru 
48262Sigor@sysoev.ru     timer = obj;
48320Sigor@sysoev.ru 
48462Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
48520Sigor@sysoev.ru     c->socket.timedout = 1;
48620Sigor@sysoev.ru     c->socket.closed = 1;
48720Sigor@sysoev.ru 
48820Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
48920Sigor@sysoev.ru 
49027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
49127Svbart@nginx.com }
49227Svbart@nginx.com 
49327Svbart@nginx.com 
49427Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
49527Svbart@nginx.com     nxt_aligned(64) =
49627Svbart@nginx.com {
49756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
49856Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
49956Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
50027Svbart@nginx.com 
50156Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
50256Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
50356Sigor@sysoev.ru     .timer_data = 60 * 1000,
50456Sigor@sysoev.ru     .timer_autoreset = 1,
50527Svbart@nginx.com };
50627Svbart@nginx.com 
50727Svbart@nginx.com 
50827Svbart@nginx.com static void
50927Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
51027Svbart@nginx.com {
511107Svbart@nginx.com     size_t                    read;
512107Svbart@nginx.com     nxt_buf_t                 *b;
513107Svbart@nginx.com     nxt_conn_t                *c;
514107Svbart@nginx.com     nxt_controller_request_t  *r;
51527Svbart@nginx.com 
51627Svbart@nginx.com     c = obj;
517107Svbart@nginx.com     r = data;
51827Svbart@nginx.com     b = c->read;
51927Svbart@nginx.com 
520107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
52127Svbart@nginx.com 
522107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
523107Svbart@nginx.com               read, r->length);
52427Svbart@nginx.com 
525107Svbart@nginx.com     if (read >= r->length) {
526140Svbart@nginx.com         nxt_controller_process_request(task, r);
52727Svbart@nginx.com         return;
52827Svbart@nginx.com     }
52927Svbart@nginx.com 
53062Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
53127Svbart@nginx.com }
53227Svbart@nginx.com 
53327Svbart@nginx.com 
53427Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
53527Svbart@nginx.com     nxt_aligned(64) =
53627Svbart@nginx.com {
53756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
53856Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
53927Svbart@nginx.com 
54056Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
54156Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
54256Sigor@sysoev.ru     .timer_data = 60 * 1000,
54356Sigor@sysoev.ru     .timer_autoreset = 1,
54427Svbart@nginx.com };
54527Svbart@nginx.com 
54627Svbart@nginx.com 
54727Svbart@nginx.com static void
54827Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
54927Svbart@nginx.com {
55062Sigor@sysoev.ru     nxt_buf_t   *b;
55162Sigor@sysoev.ru     nxt_conn_t  *c;
55227Svbart@nginx.com 
55327Svbart@nginx.com     c = obj;
55427Svbart@nginx.com 
55527Svbart@nginx.com     nxt_debug(task, "controller conn write");
55627Svbart@nginx.com 
55727Svbart@nginx.com     b = c->write;
55827Svbart@nginx.com 
55927Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
56062Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
56127Svbart@nginx.com         return;
56227Svbart@nginx.com     }
56327Svbart@nginx.com 
56427Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
56527Svbart@nginx.com 
56627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
56727Svbart@nginx.com }
56827Svbart@nginx.com 
56927Svbart@nginx.com 
57027Svbart@nginx.com static void
57127Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
57227Svbart@nginx.com {
57362Sigor@sysoev.ru     nxt_conn_t  *c;
57427Svbart@nginx.com 
57527Svbart@nginx.com     c = obj;
57627Svbart@nginx.com 
57727Svbart@nginx.com     nxt_debug(task, "controller conn write error");
57827Svbart@nginx.com 
57927Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
58027Svbart@nginx.com }
58127Svbart@nginx.com 
58227Svbart@nginx.com 
58327Svbart@nginx.com static void
58427Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
58527Svbart@nginx.com {
58662Sigor@sysoev.ru     nxt_conn_t   *c;
58762Sigor@sysoev.ru     nxt_timer_t  *timer;
58827Svbart@nginx.com 
58962Sigor@sysoev.ru     timer = obj;
59027Svbart@nginx.com 
59162Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
59227Svbart@nginx.com     c->socket.timedout = 1;
59327Svbart@nginx.com     c->socket.closed = 1;
59427Svbart@nginx.com 
59527Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
59627Svbart@nginx.com 
59727Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
59820Sigor@sysoev.ru }
59920Sigor@sysoev.ru 
60020Sigor@sysoev.ru 
60120Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
60220Sigor@sysoev.ru     nxt_aligned(64) =
60320Sigor@sysoev.ru {
60456Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
60520Sigor@sysoev.ru };
60620Sigor@sysoev.ru 
60720Sigor@sysoev.ru 
60820Sigor@sysoev.ru static void
60920Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
61020Sigor@sysoev.ru {
61162Sigor@sysoev.ru     nxt_conn_t  *c;
61220Sigor@sysoev.ru 
61320Sigor@sysoev.ru     c = obj;
61420Sigor@sysoev.ru 
61520Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
61620Sigor@sysoev.ru 
61727Svbart@nginx.com     nxt_queue_remove(&c->link);
61827Svbart@nginx.com 
61920Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
62020Sigor@sysoev.ru 
62162Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
62220Sigor@sysoev.ru }
62320Sigor@sysoev.ru 
62420Sigor@sysoev.ru 
62520Sigor@sysoev.ru static void
62620Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
62720Sigor@sysoev.ru {
62862Sigor@sysoev.ru     nxt_conn_t  *c;
62920Sigor@sysoev.ru 
63020Sigor@sysoev.ru     c = obj;
63120Sigor@sysoev.ru 
63220Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
63320Sigor@sysoev.ru 
63465Sigor@sysoev.ru     nxt_mp_destroy(c->mem_pool);
63520Sigor@sysoev.ru 
63620Sigor@sysoev.ru     //nxt_free(c);
63720Sigor@sysoev.ru }
63827Svbart@nginx.com 
63927Svbart@nginx.com 
64027Svbart@nginx.com static nxt_int_t
64160Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
64267Svbart@nginx.com     nxt_log_t *log)
64327Svbart@nginx.com {
64427Svbart@nginx.com     off_t                     length;
64527Svbart@nginx.com     nxt_controller_request_t  *r;
64627Svbart@nginx.com 
64727Svbart@nginx.com     r = ctx;
64827Svbart@nginx.com 
64960Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
65027Svbart@nginx.com 
65127Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
652107Svbart@nginx.com 
653107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
654107Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
655107Svbart@nginx.com             return NXT_ERROR;
656107Svbart@nginx.com         }
65727Svbart@nginx.com 
65827Svbart@nginx.com         r->length = length;
65927Svbart@nginx.com         return NXT_OK;
66027Svbart@nginx.com     }
66127Svbart@nginx.com 
66260Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
66327Svbart@nginx.com 
66427Svbart@nginx.com     return NXT_ERROR;
66527Svbart@nginx.com }
66627Svbart@nginx.com 
66727Svbart@nginx.com 
66827Svbart@nginx.com static void
669140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
67027Svbart@nginx.com {
67165Sigor@sysoev.ru     nxt_mp_t                   *mp;
67251Svbart@nginx.com     nxt_int_t                  rc;
67346Svbart@nginx.com     nxt_str_t                  path;
674140Svbart@nginx.com     nxt_conn_t                 *c;
67551Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
676106Svbart@nginx.com     nxt_conf_op_t              *ops;
677106Svbart@nginx.com     nxt_conf_value_t           *value;
678208Svbart@nginx.com     nxt_conf_json_error_t      error;
67944Svbart@nginx.com     nxt_controller_response_t  resp;
68044Svbart@nginx.com 
68151Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
68251Svbart@nginx.com 
683140Svbart@nginx.com     c = req->conn;
684112Smax.romanov@nginx.com     path = req->parser.path;
68551Svbart@nginx.com 
68651Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
68751Svbart@nginx.com         path.length--;
68851Svbart@nginx.com     }
68951Svbart@nginx.com 
69044Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
69144Svbart@nginx.com 
69244Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
69346Svbart@nginx.com 
694106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
69551Svbart@nginx.com 
69651Svbart@nginx.com         if (value == NULL) {
697208Svbart@nginx.com             goto not_found;
69851Svbart@nginx.com         }
69951Svbart@nginx.com 
700208Svbart@nginx.com         resp.status = 200;
701106Svbart@nginx.com         resp.conf = value;
70246Svbart@nginx.com 
703208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
704208Svbart@nginx.com         return;
70551Svbart@nginx.com     }
70651Svbart@nginx.com 
70751Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
70846Svbart@nginx.com 
709238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
710238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
711238Svbart@nginx.com             return;
712238Svbart@nginx.com         }
713238Svbart@nginx.com 
71465Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
71551Svbart@nginx.com 
71651Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
717208Svbart@nginx.com             goto alloc_fail;
71846Svbart@nginx.com         }
71946Svbart@nginx.com 
72051Svbart@nginx.com         mbuf = &c->read->mem;
72151Svbart@nginx.com 
722208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
723208Svbart@nginx.com 
724208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
72551Svbart@nginx.com 
72651Svbart@nginx.com         if (value == NULL) {
72765Sigor@sysoev.ru             nxt_mp_destroy(mp);
728208Svbart@nginx.com 
729208Svbart@nginx.com             if (error.pos == NULL) {
730208Svbart@nginx.com                 goto alloc_fail;
731208Svbart@nginx.com             }
732208Svbart@nginx.com 
733208Svbart@nginx.com             resp.status = 400;
734208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
735208Svbart@nginx.com             resp.detail = error.detail;
736208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
737208Svbart@nginx.com 
738208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
739208Svbart@nginx.com                                    &resp.line, &resp.column);
740208Svbart@nginx.com 
741208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
742208Svbart@nginx.com             return;
74351Svbart@nginx.com         }
74451Svbart@nginx.com 
74551Svbart@nginx.com         if (path.length != 1) {
746106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
747106Svbart@nginx.com                                      nxt_controller_conf.root,
748106Svbart@nginx.com                                      &path, value);
74946Svbart@nginx.com 
75051Svbart@nginx.com             if (rc != NXT_OK) {
75151Svbart@nginx.com                 if (rc == NXT_DECLINED) {
752208Svbart@nginx.com                     goto not_found;
75351Svbart@nginx.com                 }
75446Svbart@nginx.com 
755208Svbart@nginx.com                 goto alloc_fail;
75651Svbart@nginx.com             }
75751Svbart@nginx.com 
758106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
75951Svbart@nginx.com 
76051Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
76165Sigor@sysoev.ru                 nxt_mp_destroy(mp);
762208Svbart@nginx.com                 goto alloc_fail;
76351Svbart@nginx.com             }
76446Svbart@nginx.com         }
76544Svbart@nginx.com 
766116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
767121Svbart@nginx.com             nxt_mp_destroy(mp);
768208Svbart@nginx.com             goto invalid_conf;
769116Svbart@nginx.com         }
770116Svbart@nginx.com 
771*249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
772*249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
773247Svbart@nginx.com 
774247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
775121Svbart@nginx.com             nxt_mp_destroy(mp);
776247Svbart@nginx.com 
777247Svbart@nginx.com             if (rc == NXT_DECLINED) {
778247Svbart@nginx.com                 goto no_router;
779247Svbart@nginx.com             }
780247Svbart@nginx.com 
781247Svbart@nginx.com             /* rc == NXT_ERROR */
782208Svbart@nginx.com             goto alloc_fail;
783121Svbart@nginx.com         }
784121Svbart@nginx.com 
785*249Svbart@nginx.com         req->conf.root = value;
786*249Svbart@nginx.com         req->conf.pool = mp;
787*249Svbart@nginx.com 
788*249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
789*249Svbart@nginx.com 
790140Svbart@nginx.com         return;
79151Svbart@nginx.com     }
79227Svbart@nginx.com 
79351Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
79451Svbart@nginx.com 
795238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
796238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
797238Svbart@nginx.com             return;
798238Svbart@nginx.com         }
799238Svbart@nginx.com 
80051Svbart@nginx.com         if (path.length == 1) {
80165Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
80244Svbart@nginx.com 
80351Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
804208Svbart@nginx.com                 goto alloc_fail;
80551Svbart@nginx.com             }
80651Svbart@nginx.com 
807106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
80827Svbart@nginx.com 
80944Svbart@nginx.com         } else {
810106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
811106Svbart@nginx.com                                      nxt_controller_conf.root,
812106Svbart@nginx.com                                      &path, NULL);
81351Svbart@nginx.com 
81451Svbart@nginx.com             if (rc != NXT_OK) {
81551Svbart@nginx.com                 if (rc == NXT_DECLINED) {
816208Svbart@nginx.com                     goto not_found;
81751Svbart@nginx.com                 }
81851Svbart@nginx.com 
819208Svbart@nginx.com                 goto alloc_fail;
82051Svbart@nginx.com             }
82151Svbart@nginx.com 
82265Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
82351Svbart@nginx.com 
82451Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
825208Svbart@nginx.com                 goto alloc_fail;
82651Svbart@nginx.com             }
82751Svbart@nginx.com 
828106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
82951Svbart@nginx.com         }
83051Svbart@nginx.com 
83151Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
83265Sigor@sysoev.ru             nxt_mp_destroy(mp);
833208Svbart@nginx.com             goto alloc_fail;
83444Svbart@nginx.com         }
83544Svbart@nginx.com 
836116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
837121Svbart@nginx.com             nxt_mp_destroy(mp);
838208Svbart@nginx.com             goto invalid_conf;
839116Svbart@nginx.com         }
840116Svbart@nginx.com 
841*249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
842*249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
843247Svbart@nginx.com 
844247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
845121Svbart@nginx.com             nxt_mp_destroy(mp);
846247Svbart@nginx.com 
847247Svbart@nginx.com             if (rc == NXT_DECLINED) {
848247Svbart@nginx.com                 goto no_router;
849247Svbart@nginx.com             }
850247Svbart@nginx.com 
851247Svbart@nginx.com             /* rc == NXT_ERROR */
852208Svbart@nginx.com             goto alloc_fail;
853121Svbart@nginx.com         }
854121Svbart@nginx.com 
855*249Svbart@nginx.com         req->conf.root = value;
856*249Svbart@nginx.com         req->conf.pool = mp;
857*249Svbart@nginx.com 
858*249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
859*249Svbart@nginx.com 
860140Svbart@nginx.com         return;
86151Svbart@nginx.com     }
86251Svbart@nginx.com 
863208Svbart@nginx.com     resp.status = 405;
864208Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
865208Svbart@nginx.com     resp.offset = -1;
86651Svbart@nginx.com 
867208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
868208Svbart@nginx.com     return;
86951Svbart@nginx.com 
870208Svbart@nginx.com not_found:
871208Svbart@nginx.com 
872208Svbart@nginx.com     resp.status = 404;
873208Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
874208Svbart@nginx.com     resp.offset = -1;
875208Svbart@nginx.com 
876208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
877208Svbart@nginx.com     return;
878208Svbart@nginx.com 
879208Svbart@nginx.com invalid_conf:
880208Svbart@nginx.com 
881208Svbart@nginx.com     resp.status = 400;
882208Svbart@nginx.com     resp.title = (u_char *) "Invalid configuration.";
883208Svbart@nginx.com     resp.offset = -1;
884208Svbart@nginx.com 
885208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
886208Svbart@nginx.com     return;
887247Svbart@nginx.com 
888247Svbart@nginx.com alloc_fail:
889247Svbart@nginx.com 
890247Svbart@nginx.com     resp.status = 500;
891247Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
892247Svbart@nginx.com     resp.offset = -1;
893247Svbart@nginx.com 
894247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
895247Svbart@nginx.com     return;
896247Svbart@nginx.com 
897247Svbart@nginx.com no_router:
898247Svbart@nginx.com 
899247Svbart@nginx.com     resp.status = 500;
900247Svbart@nginx.com     resp.title = (u_char *) "Router process isn't available.";
901247Svbart@nginx.com     resp.offset = -1;
902247Svbart@nginx.com 
903247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
904247Svbart@nginx.com     return;
90527Svbart@nginx.com }
90627Svbart@nginx.com 
90727Svbart@nginx.com 
908193Smax.romanov@nginx.com static void
909193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
910193Smax.romanov@nginx.com     void *data)
911140Svbart@nginx.com {
912238Svbart@nginx.com     nxt_queue_t                queue;
913140Svbart@nginx.com     nxt_controller_request_t   *req;
914140Svbart@nginx.com     nxt_controller_response_t  resp;
915140Svbart@nginx.com 
916238Svbart@nginx.com     req = data;
917238Svbart@nginx.com 
918201Svbart@nginx.com     nxt_debug(task, "controller conf ready: %*s",
919201Svbart@nginx.com               nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos);
920140Svbart@nginx.com 
921238Svbart@nginx.com     nxt_queue_remove(&req->link);
922140Svbart@nginx.com 
923238Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
924140Svbart@nginx.com 
925193Smax.romanov@nginx.com     if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) {
926140Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
927140Svbart@nginx.com 
928140Svbart@nginx.com         nxt_controller_conf = req->conf;
929140Svbart@nginx.com 
930208Svbart@nginx.com         resp.status = 200;
931208Svbart@nginx.com         resp.title = (u_char *) "Reconfiguration done.";
932140Svbart@nginx.com 
933140Svbart@nginx.com     } else {
934140Svbart@nginx.com         nxt_mp_destroy(req->conf.pool);
935140Svbart@nginx.com 
936208Svbart@nginx.com         resp.status = 500;
937208Svbart@nginx.com         resp.title = (u_char *) "Failed to apply new configuration.";
938208Svbart@nginx.com         resp.offset = -1;
939140Svbart@nginx.com     }
940140Svbart@nginx.com 
941140Svbart@nginx.com     nxt_controller_response(task, req, &resp);
942140Svbart@nginx.com 
943238Svbart@nginx.com     nxt_queue_init(&queue);
944238Svbart@nginx.com     nxt_queue_add(&queue, &nxt_controller_waiting_requests);
945140Svbart@nginx.com 
946238Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
947121Svbart@nginx.com 
948238Svbart@nginx.com     nxt_queue_each(req, &queue, nxt_controller_request_t, link) {
949238Svbart@nginx.com         nxt_controller_process_request(task, req);
950238Svbart@nginx.com     } nxt_queue_loop;
951121Svbart@nginx.com }
952121Svbart@nginx.com 
953121Svbart@nginx.com 
954140Svbart@nginx.com static void
955140Svbart@nginx.com nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req,
95644Svbart@nginx.com     nxt_controller_response_t *resp)
95733Svbart@nginx.com {
958208Svbart@nginx.com     size_t                  size;
959208Svbart@nginx.com     nxt_str_t               status_line, str;
960208Svbart@nginx.com     nxt_buf_t               *b, *body;
961208Svbart@nginx.com     nxt_conn_t              *c;
962208Svbart@nginx.com     nxt_uint_t              n;
963208Svbart@nginx.com     nxt_conf_value_t        *value, *location;
964208Svbart@nginx.com     nxt_conf_json_pretty_t  pretty;
965208Svbart@nginx.com 
966208Svbart@nginx.com     static nxt_str_t  success_str = nxt_string("success");
967208Svbart@nginx.com     static nxt_str_t  error_str = nxt_string("error");
968208Svbart@nginx.com     static nxt_str_t  detail_str = nxt_string("detail");
969208Svbart@nginx.com     static nxt_str_t  location_str = nxt_string("location");
970208Svbart@nginx.com     static nxt_str_t  offset_str = nxt_string("offset");
971208Svbart@nginx.com     static nxt_str_t  line_str = nxt_string("line");
972208Svbart@nginx.com     static nxt_str_t  column_str = nxt_string("column");
973208Svbart@nginx.com 
974208Svbart@nginx.com     static nxt_time_string_t  date_cache = {
975208Svbart@nginx.com         (nxt_atomic_uint_t) -1,
976208Svbart@nginx.com         nxt_controller_date,
977208Svbart@nginx.com         "%s, %02d %s %4d %02d:%02d:%02d GMT",
978208Svbart@nginx.com         sizeof("Wed, 31 Dec 1986 16:40:00 GMT") - 1,
979208Svbart@nginx.com         NXT_THREAD_TIME_GMT,
980208Svbart@nginx.com         NXT_THREAD_TIME_SEC,
981208Svbart@nginx.com     };
982208Svbart@nginx.com 
983208Svbart@nginx.com     switch (resp->status) {
984208Svbart@nginx.com 
985208Svbart@nginx.com     case 200:
986208Svbart@nginx.com         nxt_str_set(&status_line, "200 OK");
987208Svbart@nginx.com         break;
988208Svbart@nginx.com 
989208Svbart@nginx.com     case 400:
990208Svbart@nginx.com         nxt_str_set(&status_line, "400 Bad Request");
991208Svbart@nginx.com         break;
992208Svbart@nginx.com 
993208Svbart@nginx.com     case 404:
994208Svbart@nginx.com         nxt_str_set(&status_line, "404 Not Found");
995208Svbart@nginx.com         break;
996208Svbart@nginx.com 
997208Svbart@nginx.com     case 405:
998208Svbart@nginx.com         nxt_str_set(&status_line, "405 Method Not Allowed");
999208Svbart@nginx.com         break;
1000208Svbart@nginx.com 
1001209Svbart@nginx.com     default:
1002208Svbart@nginx.com         nxt_str_set(&status_line, "500 Internal Server Error");
1003208Svbart@nginx.com         break;
1004208Svbart@nginx.com     }
1005140Svbart@nginx.com 
1006140Svbart@nginx.com     c = req->conn;
1007208Svbart@nginx.com     value = resp->conf;
100833Svbart@nginx.com 
1009208Svbart@nginx.com     if (value == NULL) {
1010208Svbart@nginx.com         n = 1
1011208Svbart@nginx.com             + (resp->detail != NULL)
1012208Svbart@nginx.com             + (resp->status >= 400 && resp->offset != -1);
1013208Svbart@nginx.com 
1014208Svbart@nginx.com         value = nxt_conf_create_object(c->mem_pool, n);
1015208Svbart@nginx.com 
1016208Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
1017208Svbart@nginx.com             nxt_controller_conn_close(task, c, req);
1018208Svbart@nginx.com             return;
1019208Svbart@nginx.com         }
1020208Svbart@nginx.com 
1021208Svbart@nginx.com         str.length = nxt_strlen(resp->title);
1022208Svbart@nginx.com         str.start = resp->title;
1023208Svbart@nginx.com 
1024208Svbart@nginx.com         if (resp->status < 400) {
1025208Svbart@nginx.com             nxt_conf_set_member_string(value, &success_str, &str, 0);
1026208Svbart@nginx.com 
1027208Svbart@nginx.com         } else {
1028208