xref: /unit/src/nxt_controller.c (revision 645)
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;
34357Svbart@nginx.com     nxt_str_t         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);
43249Svbart@nginx.com static nxt_int_t nxt_controller_conf_default(void);
44249Svbart@nginx.com static void nxt_controller_conf_init_handler(nxt_task_t *task,
45249Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
46249Svbart@nginx.com static nxt_int_t nxt_controller_conf_send(nxt_task_t *task,
47249Svbart@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,
68417Svbart@nginx.com     nxt_http_field_t *field, uintptr_t data);
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);
74314Svbart@nginx.com static void nxt_controller_conf_store(nxt_task_t *task,
75314Svbart@nginx.com     nxt_conf_value_t *conf);
76140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
77140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
78208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
79208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
8027Svbart@nginx.com 
8127Svbart@nginx.com 
82417Svbart@nginx.com static nxt_http_field_proc_t  nxt_controller_request_fields[] = {
8327Svbart@nginx.com     { nxt_string("Content-Length"),
8427Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
8527Svbart@nginx.com };
8627Svbart@nginx.com 
87417Svbart@nginx.com static nxt_lvlhsh_t            nxt_controller_fields_hash;
8827Svbart@nginx.com 
89314Svbart@nginx.com static nxt_uint_t              nxt_controller_listening;
90238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
91238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
9227Svbart@nginx.com 
9320Sigor@sysoev.ru 
9420Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
9527Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
9627Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
9720Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
9820Sigor@sysoev.ru 
9920Sigor@sysoev.ru 
100320Smax.romanov@nginx.com nxt_port_handlers_t  nxt_controller_process_port_handlers = {
101320Smax.romanov@nginx.com     .quit         = nxt_worker_process_quit_handler,
102320Smax.romanov@nginx.com     .new_port     = nxt_controller_process_new_port_handler,
103320Smax.romanov@nginx.com     .change_file  = nxt_port_change_log_file_handler,
104320Smax.romanov@nginx.com     .mmap         = nxt_port_mmap_handler,
105320Smax.romanov@nginx.com     .data         = nxt_port_data_handler,
106320Smax.romanov@nginx.com     .remove_pid   = nxt_port_remove_pid_handler,
107320Smax.romanov@nginx.com     .rpc_ready    = nxt_port_rpc_handler,
108320Smax.romanov@nginx.com     .rpc_error    = nxt_port_rpc_handler,
109248Svbart@nginx.com };
110248Svbart@nginx.com 
111248Svbart@nginx.com 
11220Sigor@sysoev.ru nxt_int_t
113141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
11420Sigor@sysoev.ru {
115417Svbart@nginx.com     nxt_mp_t               *mp;
116417Svbart@nginx.com     nxt_int_t              ret;
117417Svbart@nginx.com     nxt_str_t              *json;
118417Svbart@nginx.com     nxt_runtime_t          *rt;
119417Svbart@nginx.com     nxt_conf_value_t       *conf;
120417Svbart@nginx.com     nxt_event_engine_t     *engine;
121417Svbart@nginx.com     nxt_conf_validation_t  vldt;
12227Svbart@nginx.com 
123141Smax.romanov@nginx.com     rt = task->thread->runtime;
124141Smax.romanov@nginx.com 
125337Sigor@sysoev.ru     engine = task->thread->engine;
126337Sigor@sysoev.ru 
127337Sigor@sysoev.ru     engine->mem_pool = nxt_mp_create(4096, 128, 1024, 64);
128337Sigor@sysoev.ru     if (nxt_slow_path(engine->mem_pool == NULL)) {
129337Sigor@sysoev.ru         return NXT_ERROR;
130337Sigor@sysoev.ru     }
131337Sigor@sysoev.ru 
132417Svbart@nginx.com     ret = nxt_http_fields_hash(&nxt_controller_fields_hash, rt->mem_pool,
133417Svbart@nginx.com                                nxt_controller_request_fields,
134417Svbart@nginx.com                                nxt_nitems(nxt_controller_request_fields));
135417Svbart@nginx.com 
136417Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
13727Svbart@nginx.com         return NXT_ERROR;
13827Svbart@nginx.com     }
13927Svbart@nginx.com 
140248Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
14127Svbart@nginx.com 
142314Svbart@nginx.com     json = data;
143314Svbart@nginx.com 
144314Svbart@nginx.com     if (json->length == 0) {
145314Svbart@nginx.com         return NXT_OK;
146314Svbart@nginx.com     }
147314Svbart@nginx.com 
148314Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
149314Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
150314Svbart@nginx.com         return NXT_ERROR;
151314Svbart@nginx.com     }
152314Svbart@nginx.com 
153314Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, json);
154314Svbart@nginx.com     nxt_free(json->start);
155314Svbart@nginx.com 
156314Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
157564Svbart@nginx.com         nxt_alert(task, "failed to restore previous configuration: "
158564Svbart@nginx.com                   "file is corrupted or not enough memory");
159314Svbart@nginx.com 
160314Svbart@nginx.com         nxt_mp_destroy(mp);
161314Svbart@nginx.com         return NXT_OK;
162314Svbart@nginx.com     }
163314Svbart@nginx.com 
164357Svbart@nginx.com     nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
165314Svbart@nginx.com 
166357Svbart@nginx.com     vldt.pool = nxt_mp_create(1024, 128, 256, 32);
167357Svbart@nginx.com     if (nxt_slow_path(vldt.pool == NULL)) {
168357Svbart@nginx.com         return NXT_ERROR;
169314Svbart@nginx.com     }
170314Svbart@nginx.com 
171357Svbart@nginx.com     vldt.conf = conf;
172357Svbart@nginx.com 
173357Svbart@nginx.com     ret = nxt_conf_validate(&vldt);
174357Svbart@nginx.com 
175357Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
176357Svbart@nginx.com 
177357Svbart@nginx.com         if (ret == NXT_DECLINED) {
178564Svbart@nginx.com             nxt_alert(task, "the previous configuration is invalid: %V",
179564Svbart@nginx.com                       &vldt.error);
180357Svbart@nginx.com 
181357Svbart@nginx.com             nxt_mp_destroy(vldt.pool);
182357Svbart@nginx.com             nxt_mp_destroy(mp);
183357Svbart@nginx.com 
184357Svbart@nginx.com             return NXT_OK;
185357Svbart@nginx.com         }
186357Svbart@nginx.com 
187357Svbart@nginx.com         /* ret == NXT_ERROR */
188357Svbart@nginx.com 
189357Svbart@nginx.com         return NXT_ERROR;
190357Svbart@nginx.com     }
191357Svbart@nginx.com 
192357Svbart@nginx.com     nxt_mp_destroy(vldt.pool);
193314Svbart@nginx.com 
194314Svbart@nginx.com     nxt_controller_conf.root = conf;
195314Svbart@nginx.com     nxt_controller_conf.pool = mp;
196314Svbart@nginx.com 
197248Svbart@nginx.com     return NXT_OK;
198248Svbart@nginx.com }
199248Svbart@nginx.com 
200248Svbart@nginx.com 
201248Svbart@nginx.com static void
202248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task,
203248Svbart@nginx.com     nxt_port_recv_msg_t *msg)
204248Svbart@nginx.com {
205249Svbart@nginx.com     nxt_int_t         rc;
206248Svbart@nginx.com     nxt_runtime_t     *rt;
207248Svbart@nginx.com     nxt_conf_value_t  *conf;
208248Svbart@nginx.com 
209248Svbart@nginx.com     nxt_port_new_port_handler(task, msg);
210248Svbart@nginx.com 
211347Smax.romanov@nginx.com     if (msg->u.new_port->type != NXT_PROCESS_ROUTER) {
212248Svbart@nginx.com         return;
21320Sigor@sysoev.ru     }
21420Sigor@sysoev.ru 
215249Svbart@nginx.com     conf = nxt_controller_conf.root;
216249Svbart@nginx.com 
217249Svbart@nginx.com     if (conf != NULL) {
218249Svbart@nginx.com         rc = nxt_controller_conf_send(task, conf,
219249Svbart@nginx.com                                       nxt_controller_conf_init_handler, NULL);
22044Svbart@nginx.com 
221249Svbart@nginx.com         if (nxt_fast_path(rc == NXT_OK)) {
222249Svbart@nginx.com             return;
223249Svbart@nginx.com         }
224249Svbart@nginx.com 
225249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
226249Svbart@nginx.com 
227249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
228249Svbart@nginx.com             nxt_abort();
229249Svbart@nginx.com         }
23044Svbart@nginx.com     }
23144Svbart@nginx.com 
232249Svbart@nginx.com     if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
233248Svbart@nginx.com         nxt_abort();
23444Svbart@nginx.com     }
23544Svbart@nginx.com 
236248Svbart@nginx.com     rt = task->thread->runtime;
237140Svbart@nginx.com 
238248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
239248Svbart@nginx.com         nxt_abort();
240248Svbart@nginx.com     }
241314Svbart@nginx.com 
242314Svbart@nginx.com     nxt_controller_listening = 1;
24320Sigor@sysoev.ru }
24420Sigor@sysoev.ru 
24520Sigor@sysoev.ru 
246249Svbart@nginx.com static nxt_int_t
247249Svbart@nginx.com nxt_controller_conf_default(void)
248249Svbart@nginx.com {
249249Svbart@nginx.com     nxt_mp_t          *mp;
250249Svbart@nginx.com     nxt_conf_value_t  *conf;
251249Svbart@nginx.com 
252249Svbart@nginx.com     static const nxt_str_t json
253249Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
254249Svbart@nginx.com 
255249Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
256249Svbart@nginx.com 
257249Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
258249Svbart@nginx.com         return NXT_ERROR;
259249Svbart@nginx.com     }
260249Svbart@nginx.com 
261249Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
262249Svbart@nginx.com 
263249Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
264249Svbart@nginx.com         return NXT_ERROR;
265249Svbart@nginx.com     }
266249Svbart@nginx.com 
267249Svbart@nginx.com     nxt_controller_conf.root = conf;
268249Svbart@nginx.com     nxt_controller_conf.pool = mp;
269249Svbart@nginx.com 
270249Svbart@nginx.com     return NXT_OK;
271249Svbart@nginx.com }
272249Svbart@nginx.com 
273249Svbart@nginx.com 
274249Svbart@nginx.com static void
275249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
276249Svbart@nginx.com     void *data)
277249Svbart@nginx.com {
278314Svbart@nginx.com     nxt_runtime_t  *rt;
279314Svbart@nginx.com 
280249Svbart@nginx.com     if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) {
281564Svbart@nginx.com         nxt_alert(task, "failed to apply previous configuration");
282314Svbart@nginx.com 
283249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
284249Svbart@nginx.com 
285249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
286249Svbart@nginx.com             nxt_abort();
287249Svbart@nginx.com         }
288249Svbart@nginx.com     }
289314Svbart@nginx.com 
290314Svbart@nginx.com     if (nxt_controller_listening == 0) {
291314Svbart@nginx.com         rt = task->thread->runtime;
292314Svbart@nginx.com 
293314Svbart@nginx.com         if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket)
294314Svbart@nginx.com                           == NULL))
295314Svbart@nginx.com         {
296314Svbart@nginx.com             nxt_abort();
297314Svbart@nginx.com         }
298314Svbart@nginx.com 
299314Svbart@nginx.com         nxt_controller_listening = 1;
300314Svbart@nginx.com     }
301249Svbart@nginx.com }
302249Svbart@nginx.com 
303249Svbart@nginx.com 
304249Svbart@nginx.com static nxt_int_t
305249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf,
306249Svbart@nginx.com     nxt_port_rpc_handler_t handler, void *data)
307249Svbart@nginx.com {
308249Svbart@nginx.com     size_t         size;
309249Svbart@nginx.com     uint32_t       stream;
310249Svbart@nginx.com     nxt_int_t      rc;
311249Svbart@nginx.com     nxt_buf_t      *b;
312249Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
313249Svbart@nginx.com     nxt_runtime_t  *rt;
314249Svbart@nginx.com 
315249Svbart@nginx.com     rt = task->thread->runtime;
316249Svbart@nginx.com 
317249Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
318249Svbart@nginx.com 
319249Svbart@nginx.com     if (nxt_slow_path(router_port == NULL)) {
320249Svbart@nginx.com         return NXT_DECLINED;
321249Svbart@nginx.com     }
322249Svbart@nginx.com 
323249Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
324249Svbart@nginx.com 
325249Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
326249Svbart@nginx.com 
327249Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
328379Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
329379Smax.romanov@nginx.com         return NXT_ERROR;
330379Smax.romanov@nginx.com     }
331249Svbart@nginx.com 
332249Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
333249Svbart@nginx.com 
334249Svbart@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
335249Svbart@nginx.com                                            handler, handler,
336249Svbart@nginx.com                                            router_port->pid, data);
337249Svbart@nginx.com 
338*645Svbart@nginx.com     if (nxt_slow_path(stream == 0)) {
339*645Svbart@nginx.com         return NXT_ERROR;
340*645Svbart@nginx.com     }
341*645Svbart@nginx.com 
342249Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
343249Svbart@nginx.com                                stream, controller_port->id, b);
344249Svbart@nginx.com 
345249Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
346249Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
347249Svbart@nginx.com         return NXT_ERROR;
348249Svbart@nginx.com     }
349249Svbart@nginx.com 
350249Svbart@nginx.com     return NXT_OK;
351249Svbart@nginx.com }
352249Svbart@nginx.com 
353249Svbart@nginx.com 
35420Sigor@sysoev.ru nxt_int_t
35520Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
35620Sigor@sysoev.ru {
35720Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
35820Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
35920Sigor@sysoev.ru 
36020Sigor@sysoev.ru     sa = rt->controller_listen;
36120Sigor@sysoev.ru 
36265Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
36320Sigor@sysoev.ru     if (ls == NULL) {
36420Sigor@sysoev.ru         return NXT_ERROR;
36520Sigor@sysoev.ru     }
36620Sigor@sysoev.ru 
36720Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
36820Sigor@sysoev.ru                                        sa->socklen, sa->length);
36920Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
37020Sigor@sysoev.ru         return NXT_ERROR;
37120Sigor@sysoev.ru     }
37220Sigor@sysoev.ru 
37320Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
374312Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
37520Sigor@sysoev.ru 
376359Sigor@sysoev.ru     nxt_listen_socket_remote_size(ls);
37720Sigor@sysoev.ru 
37820Sigor@sysoev.ru     ls->socket = -1;
37920Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
38020Sigor@sysoev.ru     ls->read_after_accept = 1;
38120Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
38220Sigor@sysoev.ru 
38320Sigor@sysoev.ru #if 0
38420Sigor@sysoev.ru     /* STUB */
38565Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
38620Sigor@sysoev.ru     if (wq == NULL) {
38720Sigor@sysoev.ru         return NXT_ERROR;
38820Sigor@sysoev.ru     }
38920Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
39020Sigor@sysoev.ru     /**/
39120Sigor@sysoev.ru 
39220Sigor@sysoev.ru     ls->work_queue = wq;
39320Sigor@sysoev.ru #endif
39420Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
39520Sigor@sysoev.ru 
39620Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
39720Sigor@sysoev.ru         return NXT_ERROR;
39820Sigor@sysoev.ru     }
39920Sigor@sysoev.ru 
40020Sigor@sysoev.ru     rt->controller_socket = ls;
40120Sigor@sysoev.ru 
40220Sigor@sysoev.ru     return NXT_OK;
40320Sigor@sysoev.ru }
40420Sigor@sysoev.ru 
40520Sigor@sysoev.ru 
40620Sigor@sysoev.ru static void
40720Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
40820Sigor@sysoev.ru {
40927Svbart@nginx.com     nxt_buf_t                 *b;
41062Sigor@sysoev.ru     nxt_conn_t                *c;
41127Svbart@nginx.com     nxt_event_engine_t        *engine;
41227Svbart@nginx.com     nxt_controller_request_t  *r;
41320Sigor@sysoev.ru 
41420Sigor@sysoev.ru     c = obj;
41520Sigor@sysoev.ru 
41620Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
41720Sigor@sysoev.ru 
41865Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
41927Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
42027Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
42127Svbart@nginx.com         return;
42227Svbart@nginx.com     }
42327Svbart@nginx.com 
424140Svbart@nginx.com     r->conn = c;
425140Svbart@nginx.com 
42660Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
42760Svbart@nginx.com                       != NXT_OK))
42860Svbart@nginx.com     {
42960Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
43060Svbart@nginx.com         return;
43160Svbart@nginx.com     }
43227Svbart@nginx.com 
43320Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
43420Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
43520Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
43620Sigor@sysoev.ru         return;
43720Sigor@sysoev.ru     }
43820Sigor@sysoev.ru 
43920Sigor@sysoev.ru     c->read = b;
44027Svbart@nginx.com     c->socket.data = r;
44120Sigor@sysoev.ru     c->socket.read_ready = 1;
44220Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
44320Sigor@sysoev.ru 
44420Sigor@sysoev.ru     engine = task->thread->engine;
44520Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
44627Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
44720Sigor@sysoev.ru 
44862Sigor@sysoev.ru     nxt_conn_read(engine, c);
44920Sigor@sysoev.ru }
45020Sigor@sysoev.ru 
45120Sigor@sysoev.ru 
45220Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
45320Sigor@sysoev.ru     nxt_aligned(64) =
45420Sigor@sysoev.ru {
45556Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
45656Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
45756Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
45820Sigor@sysoev.ru 
45956Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
46056Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
46156Sigor@sysoev.ru     .timer_data = 60 * 1000,
46220Sigor@sysoev.ru };
46320Sigor@sysoev.ru 
46420Sigor@sysoev.ru 
46520Sigor@sysoev.ru static void
46620Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
46720Sigor@sysoev.ru {
46827Svbart@nginx.com     size_t                    preread;
46927Svbart@nginx.com     nxt_buf_t                 *b;
47027Svbart@nginx.com     nxt_int_t                 rc;
47162Sigor@sysoev.ru     nxt_conn_t                *c;
47227Svbart@nginx.com     nxt_controller_request_t  *r;
47320Sigor@sysoev.ru 
47420Sigor@sysoev.ru     c = obj;
47527Svbart@nginx.com     r = data;
47620Sigor@sysoev.ru 
47720Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
47820Sigor@sysoev.ru 
47927Svbart@nginx.com     nxt_queue_remove(&c->link);
48027Svbart@nginx.com     nxt_queue_self(&c->link);
48127Svbart@nginx.com 
48227Svbart@nginx.com     b = c->read;
48327Svbart@nginx.com 
48427Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
48527Svbart@nginx.com 
48627Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
48727Svbart@nginx.com 
48827Svbart@nginx.com         if (rc == NXT_AGAIN) {
48927Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
49027Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
49127Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
49227Svbart@nginx.com                 return;
49327Svbart@nginx.com             }
49427Svbart@nginx.com 
49562Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
49627Svbart@nginx.com             return;
49727Svbart@nginx.com         }
49827Svbart@nginx.com 
49927Svbart@nginx.com         /* rc == NXT_ERROR */
50027Svbart@nginx.com 
50127Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
50227Svbart@nginx.com 
50327Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
50427Svbart@nginx.com         return;
50527Svbart@nginx.com     }
50627Svbart@nginx.com 
507417Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash,
508417Svbart@nginx.com                                  r);
50960Svbart@nginx.com 
51060Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
51160Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
51260Svbart@nginx.com         return;
51360Svbart@nginx.com     }
51460Svbart@nginx.com 
51527Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
51627Svbart@nginx.com 
51727Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
518107Svbart@nginx.com                     "body length: %uz, preread: %uz",
51927Svbart@nginx.com                     r->length, preread);
52027Svbart@nginx.com 
52127Svbart@nginx.com     if (preread >= r->length) {
522140Svbart@nginx.com         nxt_controller_process_request(task, r);
52327Svbart@nginx.com         return;
52427Svbart@nginx.com     }
52527Svbart@nginx.com 
52627Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
52727Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
52827Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
52927Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
53027Svbart@nginx.com             return;
53127Svbart@nginx.com         }
53227Svbart@nginx.com 
53327Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
53427Svbart@nginx.com 
53527Svbart@nginx.com         c->read = b;
53627Svbart@nginx.com     }
53727Svbart@nginx.com 
53827Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
53927Svbart@nginx.com 
54062Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
54120Sigor@sysoev.ru }
54220Sigor@sysoev.ru 
54320Sigor@sysoev.ru 
54420Sigor@sysoev.ru static nxt_msec_t
54562Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
54620Sigor@sysoev.ru {
54720Sigor@sysoev.ru     return (nxt_msec_t) data;
54820Sigor@sysoev.ru }
54920Sigor@sysoev.ru 
55020Sigor@sysoev.ru 
55120Sigor@sysoev.ru static void
55220Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
55320Sigor@sysoev.ru {
55462Sigor@sysoev.ru     nxt_conn_t  *c;
55520Sigor@sysoev.ru 
55620Sigor@sysoev.ru     c = obj;
55720Sigor@sysoev.ru 
55820Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
55920Sigor@sysoev.ru 
56027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
56120Sigor@sysoev.ru }
56220Sigor@sysoev.ru 
56320Sigor@sysoev.ru 
56420Sigor@sysoev.ru static void
56520Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
56620Sigor@sysoev.ru {
56762Sigor@sysoev.ru     nxt_timer_t  *timer;
56862Sigor@sysoev.ru     nxt_conn_t   *c;
56920Sigor@sysoev.ru 
57062Sigor@sysoev.ru     timer = obj;
57120Sigor@sysoev.ru 
57262Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
57320Sigor@sysoev.ru     c->socket.timedout = 1;
57420Sigor@sysoev.ru     c->socket.closed = 1;
57520Sigor@sysoev.ru 
57620Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
57720Sigor@sysoev.ru 
57827Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
57927Svbart@nginx.com }
58027Svbart@nginx.com 
58127Svbart@nginx.com 
58227Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
58327Svbart@nginx.com     nxt_aligned(64) =
58427Svbart@nginx.com {
58556Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
58656Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
58756Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
58827Svbart@nginx.com 
58956Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
59056Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
59156Sigor@sysoev.ru     .timer_data = 60 * 1000,
59256Sigor@sysoev.ru     .timer_autoreset = 1,
59327Svbart@nginx.com };
59427Svbart@nginx.com 
59527Svbart@nginx.com 
59627Svbart@nginx.com static void
59727Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
59827Svbart@nginx.com {
599107Svbart@nginx.com     size_t                    read;
600107Svbart@nginx.com     nxt_buf_t                 *b;
601107Svbart@nginx.com     nxt_conn_t                *c;
602107Svbart@nginx.com     nxt_controller_request_t  *r;
60327Svbart@nginx.com 
60427Svbart@nginx.com     c = obj;
605107Svbart@nginx.com     r = data;
60627Svbart@nginx.com     b = c->read;
60727Svbart@nginx.com 
608107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
60927Svbart@nginx.com 
610107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
611107Svbart@nginx.com               read, r->length);
61227Svbart@nginx.com 
613107Svbart@nginx.com     if (read >= r->length) {
614140Svbart@nginx.com         nxt_controller_process_request(task, r);
61527Svbart@nginx.com         return;
61627Svbart@nginx.com     }
61727Svbart@nginx.com 
61862Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
61927Svbart@nginx.com }
62027Svbart@nginx.com 
62127Svbart@nginx.com 
62227Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
62327Svbart@nginx.com     nxt_aligned(64) =
62427Svbart@nginx.com {
62556Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
62656Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
62727Svbart@nginx.com 
62856Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
62956Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
63056Sigor@sysoev.ru     .timer_data = 60 * 1000,
63156Sigor@sysoev.ru     .timer_autoreset = 1,
63227Svbart@nginx.com };
63327Svbart@nginx.com 
63427Svbart@nginx.com 
63527Svbart@nginx.com static void
63627Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
63727Svbart@nginx.com {
63862Sigor@sysoev.ru     nxt_buf_t   *b;
63962Sigor@sysoev.ru     nxt_conn_t  *c;
64027Svbart@nginx.com 
64127Svbart@nginx.com     c = obj;
64227Svbart@nginx.com 
64327Svbart@nginx.com     nxt_debug(task, "controller conn write");
64427Svbart@nginx.com 
64527Svbart@nginx.com     b = c->write;
64627Svbart@nginx.com 
64727Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
64862Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
64927Svbart@nginx.com         return;
65027Svbart@nginx.com     }
65127Svbart@nginx.com 
65227Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
65327Svbart@nginx.com 
65427Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
65527Svbart@nginx.com }
65627Svbart@nginx.com 
65727Svbart@nginx.com 
65827Svbart@nginx.com static void
65927Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
66027Svbart@nginx.com {
66162Sigor@sysoev.ru     nxt_conn_t  *c;
66227Svbart@nginx.com 
66327Svbart@nginx.com     c = obj;
66427Svbart@nginx.com 
66527Svbart@nginx.com     nxt_debug(task, "controller conn write error");
66627Svbart@nginx.com 
66727Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
66827Svbart@nginx.com }
66927Svbart@nginx.com 
67027Svbart@nginx.com 
67127Svbart@nginx.com static void
67227Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
67327Svbart@nginx.com {
67462Sigor@sysoev.ru     nxt_conn_t   *c;
67562Sigor@sysoev.ru     nxt_timer_t  *timer;
67627Svbart@nginx.com 
67762Sigor@sysoev.ru     timer = obj;
67827Svbart@nginx.com 
67962Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
68027Svbart@nginx.com     c->socket.timedout = 1;
68127Svbart@nginx.com     c->socket.closed = 1;
68227Svbart@nginx.com 
68327Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
68427Svbart@nginx.com 
68527Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
68620Sigor@sysoev.ru }
68720Sigor@sysoev.ru 
68820Sigor@sysoev.ru 
68920Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
69020Sigor@sysoev.ru     nxt_aligned(64) =
69120Sigor@sysoev.ru {
69256Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
69320Sigor@sysoev.ru };
69420Sigor@sysoev.ru 
69520Sigor@sysoev.ru 
69620Sigor@sysoev.ru static void
69720Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
69820Sigor@sysoev.ru {
69962Sigor@sysoev.ru     nxt_conn_t  *c;
70020Sigor@sysoev.ru 
70120Sigor@sysoev.ru     c = obj;
70220Sigor@sysoev.ru 
70320Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
70420Sigor@sysoev.ru 
70527Svbart@nginx.com     nxt_queue_remove(&c->link);
70627Svbart@nginx.com 
70720Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
70820Sigor@sysoev.ru 
70962Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
71020Sigor@sysoev.ru }
71120Sigor@sysoev.ru 
71220Sigor@sysoev.ru 
71320Sigor@sysoev.ru static void
71420Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
71520Sigor@sysoev.ru {
71662Sigor@sysoev.ru     nxt_conn_t  *c;
71720Sigor@sysoev.ru 
71820Sigor@sysoev.ru     c = obj;
71920Sigor@sysoev.ru 
72020Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
72120Sigor@sysoev.ru 
722337Sigor@sysoev.ru     nxt_sockaddr_cache_free(task->thread->engine, c);
723337Sigor@sysoev.ru 
724386Sigor@sysoev.ru     nxt_conn_free(task, c);
72520Sigor@sysoev.ru }
72627Svbart@nginx.com 
72727Svbart@nginx.com 
72827Svbart@nginx.com static nxt_int_t
72960Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
730417Svbart@nginx.com     uintptr_t data)
73127Svbart@nginx.com {
73227Svbart@nginx.com     off_t                     length;
73327Svbart@nginx.com     nxt_controller_request_t  *r;
73427Svbart@nginx.com 
73527Svbart@nginx.com     r = ctx;
73627Svbart@nginx.com 
737417Svbart@nginx.com     length = nxt_off_t_parse(field->value, field->value_length);
73827Svbart@nginx.com 
73927Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
740107Svbart@nginx.com 
741107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
742417Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, &r->conn->log,
743417Svbart@nginx.com                           "Content-Length is too big");
744107Svbart@nginx.com             return NXT_ERROR;
745107Svbart@nginx.com         }
74627Svbart@nginx.com 
74727Svbart@nginx.com         r->length = length;
74827Svbart@nginx.com         return NXT_OK;
74927Svbart@nginx.com     }
75027Svbart@nginx.com 
751417Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, &r->conn->log, "Content-Length is invalid");
75227Svbart@nginx.com 
75327Svbart@nginx.com     return NXT_ERROR;
75427Svbart@nginx.com }
75527Svbart@nginx.com 
75627Svbart@nginx.com 
75727Svbart@nginx.com static void
758140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
75927Svbart@nginx.com {
76065Sigor@sysoev.ru     nxt_mp_t                   *mp;
76151Svbart@nginx.com     nxt_int_t                  rc;
76246Svbart@nginx.com     nxt_str_t                  path;
763140Svbart@nginx.com     nxt_conn_t                 *c;
76451Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
765106Svbart@nginx.com     nxt_conf_op_t              *ops;
766106Svbart@nginx.com     nxt_conf_value_t           *value;
767357Svbart@nginx.com     nxt_conf_validation_t      vldt;
768208Svbart@nginx.com     nxt_conf_json_error_t      error;
76944Svbart@nginx.com     nxt_controller_response_t  resp;
77044Svbart@nginx.com 
77151Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
77251Svbart@nginx.com 
773140Svbart@nginx.com     c = req->conn;
774112Smax.romanov@nginx.com     path = req->parser.path;
77551Svbart@nginx.com 
776632Svbart@nginx.com     if (nxt_str_start(&path, "/config", 7)) {
777632Svbart@nginx.com 
778632Svbart@nginx.com         if (path.length == 7) {
779632Svbart@nginx.com             path.length = 1;
780632Svbart@nginx.com 
781632Svbart@nginx.com         } else if (path.start[7] == '/') {
782632Svbart@nginx.com             path.length -= 7;
783632Svbart@nginx.com             path.start += 7;
784632Svbart@nginx.com         }
785632Svbart@nginx.com     }
786632Svbart@nginx.com 
78751Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
78851Svbart@nginx.com         path.length--;
78951Svbart@nginx.com     }
79051Svbart@nginx.com 
79144Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
79244Svbart@nginx.com 
79344Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
79446Svbart@nginx.com 
795106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
79651Svbart@nginx.com 
79751Svbart@nginx.com         if (value == NULL) {
798208Svbart@nginx.com             goto not_found;
79951Svbart@nginx.com         }
80051Svbart@nginx.com 
801208Svbart@nginx.com         resp.status = 200;
802106Svbart@nginx.com         resp.conf = value;
80346Svbart@nginx.com 
804208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
805208Svbart@nginx.com         return;
80651Svbart@nginx.com     }
80751Svbart@nginx.com 
80851Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
80946Svbart@nginx.com 
810238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
811238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
812238Svbart@nginx.com             return;
813238Svbart@nginx.com         }
814238Svbart@nginx.com 
81565Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
81651Svbart@nginx.com 
81751Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
818208Svbart@nginx.com             goto alloc_fail;
81946Svbart@nginx.com         }
82046Svbart@nginx.com 
82151Svbart@nginx.com         mbuf = &c->read->mem;
82251Svbart@nginx.com 
823208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
824208Svbart@nginx.com 
825208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
82651Svbart@nginx.com 
82751Svbart@nginx.com         if (value == NULL) {
82865Sigor@sysoev.ru             nxt_mp_destroy(mp);
829208Svbart@nginx.com 
830208Svbart@nginx.com             if (error.pos == NULL) {
831208Svbart@nginx.com                 goto alloc_fail;
832208Svbart@nginx.com             }
833208Svbart@nginx.com 
834208Svbart@nginx.com             resp.status = 400;
835208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
836357Svbart@nginx.com             resp.detail.length = nxt_strlen(error.detail);
837357Svbart@nginx.com             resp.detail.start = error.detail;
838208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
839208Svbart@nginx.com 
840208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
841208Svbart@nginx.com                                    &resp.line, &resp.column);
842208Svbart@nginx.com 
843208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
844208Svbart@nginx.com             return;
84551Svbart@nginx.com         }
84651Svbart@nginx.com 
84751Svbart@nginx.com         if (path.length != 1) {
848106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
849106Svbart@nginx.com                                      nxt_controller_conf.root,
850106Svbart@nginx.com                                      &path, value);
85146Svbart@nginx.com 
85251Svbart@nginx.com             if (rc != NXT_OK) {
853619Svbart@nginx.com                 nxt_mp_destroy(mp);
854619Svbart@nginx.com 
85551Svbart@nginx.com                 if (rc == NXT_DECLINED) {
856208Svbart@nginx.com                     goto not_found;
85751Svbart@nginx.com                 }
85846Svbart@nginx.com 
859208Svbart@nginx.com                 goto alloc_fail;
86051Svbart@nginx.com             }
86151Svbart@nginx.com 
862106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
86351Svbart@nginx.com 
86451Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
86565Sigor@sysoev.ru                 nxt_mp_destroy(mp);
866208Svbart@nginx.com                 goto alloc_fail;
86751Svbart@nginx.com             }
86846Svbart@nginx.com         }
86944Svbart@nginx.com 
870357Svbart@nginx.com         nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
871357Svbart@nginx.com 
872357Svbart@nginx.com         vldt.conf = value;
873357Svbart@nginx.com         vldt.pool = c->mem_pool;
874357Svbart@nginx.com 
875357Svbart@nginx.com         rc = nxt_conf_validate(&vldt);
876357Svbart@nginx.com 
877357Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
878121Svbart@nginx.com             nxt_mp_destroy(mp);
879357Svbart@nginx.com 
880357Svbart@nginx.com             if (rc == NXT_DECLINED) {
881357Svbart@nginx.com                 resp.detail = vldt.error;
882357Svbart@nginx.com                 goto invalid_conf;
883357Svbart@nginx.com             }
884357Svbart@nginx.com 
885357Svbart@nginx.com             /* rc == NXT_ERROR */
886357Svbart@nginx.com             goto alloc_fail;
887116Svbart@nginx.com         }
888116Svbart@nginx.com 
889249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
890249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
891247Svbart@nginx.com 
892247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
893121Svbart@nginx.com             nxt_mp_destroy(mp);
894247Svbart@nginx.com 
895247Svbart@nginx.com             if (rc == NXT_DECLINED) {
896247Svbart@nginx.com                 goto no_router;
897247Svbart@nginx.com             }
898247Svbart@nginx.com 
899247Svbart@nginx.com             /* rc == NXT_ERROR */
900208Svbart@nginx.com             goto alloc_fail;
901121Svbart@nginx.com         }
902121Svbart@nginx.com 
903249Svbart@nginx.com         req->conf.root = value;
904249Svbart@nginx.com         req->conf.pool = mp;
905249Svbart@nginx.com 
906249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
907249Svbart@nginx.com 
908140Svbart@nginx.com         return;
90951Svbart@nginx.com     }
91027Svbart@nginx.com 
91151Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
91251Svbart@nginx.com 
913238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
914238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
915238Svbart@nginx.com             return;
916238Svbart@nginx.com         }
917238Svbart@nginx.com 
91851Svbart@nginx.com         if (path.length == 1) {
91965Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
92044Svbart@nginx.com 
92151Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
922208Svbart@nginx.com                 goto alloc_fail;
92351Svbart@nginx.com             }
92451Svbart@nginx.com 
925106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
92627Svbart@nginx.com 
92744Svbart@nginx.com         } else {
928106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
929106Svbart@nginx.com                                      nxt_controller_conf.root,
930106Svbart@nginx.com                                      &path, NULL);
93151Svbart@nginx.com 
93251Svbart@nginx.com             if (rc != NXT_OK) {
93351Svbart@nginx.com                 if (rc == NXT_DECLINED) {
934208Svbart@nginx.com                     goto not_found;
93551Svbart@nginx.com                 }
93651Svbart@nginx.com 
937208Svbart@nginx.com                 goto alloc_fail;
93851Svbart@nginx.com             }
93951Svbart@nginx.com 
94065Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
94151Svbart@nginx.com 
94251Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
943208Svbart@nginx.com                 goto alloc_fail;
94451Svbart@nginx.com             }
94551Svbart@nginx.com 
946106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
94751Svbart@nginx.com         }
94851Svbart@nginx.com 
94951Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
95065Sigor@sysoev.ru             nxt_mp_destroy(mp);
951208Svbart@nginx.com             goto alloc_fail;
95244Svbart@nginx.com         }
95344Svbart@nginx.com 
954357Svbart@nginx.com         nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
955357Svbart@nginx.com 
956357Svbart@nginx.com         vldt.conf = value;
957357Svbart@nginx.com         vldt.pool = c->mem_pool;
958357Svbart@nginx.com 
959357Svbart@nginx.com         rc = nxt_conf_validate(&vldt);
960357Svbart@nginx.com 
961357Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
962121Svbart@nginx.com             nxt_mp_destroy(mp);
963357Svbart@nginx.com 
964357Svbart@nginx.com             if (rc == NXT_DECLINED) {
965357Svbart@nginx.com                 resp.detail = vldt.error;
966357Svbart@nginx.com                 goto invalid_conf;
967357Svbart@nginx.com             }
968357Svbart@nginx.com 
969357Svbart@nginx.com             /* rc == NXT_ERROR */
970357Svbart@nginx.com             goto alloc_fail;
971116Svbart@nginx.com         }
972116Svbart@nginx.com 
973249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
974249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
975247Svbart@nginx.com 
976247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
977121Svbart@nginx.com             nxt_mp_destroy(mp);
978247Svbart@nginx.com 
979247Svbart@nginx.com             if (rc == NXT_DECLINED) {
980247Svbart@nginx.com                 goto no_router;
981247Svbart@nginx.com             }
982247Svbart@nginx.com 
983247Svbart@nginx.com             /* rc == NXT_ERROR */
984208Svbart@nginx.com             goto alloc_fail;
985121Svbart@nginx.com         }
986121Svbart@nginx.com 
987249Svbart@nginx.com         req->conf.root = value;
988249Svbart@nginx.com         req->conf.pool = mp;
989249Svbart@nginx.com 
990249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
991249Svbart@nginx.com 
992140Svbart@nginx.com         return;
99351Svbart@nginx.com     }
99451Svbart@nginx.com 
995208Svbart@nginx.com     resp.status = 405;
996208Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
997208Svbart@nginx.com     resp.offset = -1;
99851Svbart@nginx.com 
999208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1000208Svbart@nginx.com     return;
100151Svbart@nginx.com 
1002208Svbart@nginx.com not_found:
1003208Svbart@nginx.com 
1004208Svbart@nginx.com     resp.status = 404;
1005208Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
1006208Svbart@nginx.com     resp.offset = -1;
1007208Svbart@nginx.com 
1008208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1009208Svbart@nginx.com     return;
1010208Svbart@nginx.com 
1011208Svbart@nginx.com invalid_conf:
1012208Svbart@nginx.com 
1013208Svbart@nginx.com     resp.status = 400;
1014208Svbart@nginx.com     resp.title = (u_char *) "Invalid configuration.";
1015208Svbart@nginx.com     resp.offset = -1;
1016208Svbart@nginx.com 
1017208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1018208Svbart@nginx.com     return;
1019247Svbart@nginx.com 
1020247Svbart@nginx.com alloc_fail:
1021247Svbart@nginx.com 
1022247Svbart@nginx.com     resp.status = 500;
1023247Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
1024247Svbart@nginx.com     resp.offset = -1;
1025247Svbart@nginx.com 
1026247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1027247Svbart@nginx.com     return;
1028247Svbart@nginx.com 
1029247Svbart@nginx.com no_router:
1030247Svbart@nginx.com 
1031247Svbart@nginx.com     resp.status = 500;
1032247Svbart@nginx.com     resp.title = (u_char *) "Router process isn't available.";
1033247Svbart@nginx.com     resp.offset = -1;
1034247Svbart@nginx.com 
1035247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1036247Svbart@nginx.com     return;
103727Svbart@nginx.com }
103827Svbart@nginx.com 
103927Svbart@nginx.com 
1040193Smax.romanov@nginx.com static void
1041193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
1042193Smax.romanov@nginx.com     void *data)
1043140Svbart@nginx.com {
1044238Svbart@nginx.com     nxt_queue_t                queue;
1045140Svbart@nginx.com     nxt_controller_request_t   *req;
1046140Svbart@nginx.com     nxt_controller_response_t  resp;
1047