xref: /unit/src/nxt_controller.c (revision 564)
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)) {
157*564Svbart@nginx.com         nxt_alert(task, "failed to restore previous configuration: "
158*564Svbart@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) {
178*564Svbart@nginx.com             nxt_alert(task, "the previous configuration is invalid: %V",
179*564Svbart@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) {
281*564Svbart@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 
338249Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
339249Svbart@nginx.com                                stream, controller_port->id, b);
340249Svbart@nginx.com 
341249Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
342249Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
343249Svbart@nginx.com         return NXT_ERROR;
344249Svbart@nginx.com     }
345249Svbart@nginx.com 
346249Svbart@nginx.com     return NXT_OK;
347249Svbart@nginx.com }
348249Svbart@nginx.com 
349249Svbart@nginx.com 
35020Sigor@sysoev.ru nxt_int_t
35120Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
35220Sigor@sysoev.ru {
35320Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
35420Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
35520Sigor@sysoev.ru 
35620Sigor@sysoev.ru     sa = rt->controller_listen;
35720Sigor@sysoev.ru 
35865Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
35920Sigor@sysoev.ru     if (ls == NULL) {
36020Sigor@sysoev.ru         return NXT_ERROR;
36120Sigor@sysoev.ru     }
36220Sigor@sysoev.ru 
36320Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
36420Sigor@sysoev.ru                                        sa->socklen, sa->length);
36520Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
36620Sigor@sysoev.ru         return NXT_ERROR;
36720Sigor@sysoev.ru     }
36820Sigor@sysoev.ru 
36920Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
370312Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
37120Sigor@sysoev.ru 
372359Sigor@sysoev.ru     nxt_listen_socket_remote_size(ls);
37320Sigor@sysoev.ru 
37420Sigor@sysoev.ru     ls->socket = -1;
37520Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
37620Sigor@sysoev.ru     ls->read_after_accept = 1;
37720Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
37820Sigor@sysoev.ru 
37920Sigor@sysoev.ru #if 0
38020Sigor@sysoev.ru     /* STUB */
38165Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
38220Sigor@sysoev.ru     if (wq == NULL) {
38320Sigor@sysoev.ru         return NXT_ERROR;
38420Sigor@sysoev.ru     }
38520Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
38620Sigor@sysoev.ru     /**/
38720Sigor@sysoev.ru 
38820Sigor@sysoev.ru     ls->work_queue = wq;
38920Sigor@sysoev.ru #endif
39020Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
39120Sigor@sysoev.ru 
39220Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
39320Sigor@sysoev.ru         return NXT_ERROR;
39420Sigor@sysoev.ru     }
39520Sigor@sysoev.ru 
39620Sigor@sysoev.ru     rt->controller_socket = ls;
39720Sigor@sysoev.ru 
39820Sigor@sysoev.ru     return NXT_OK;
39920Sigor@sysoev.ru }
40020Sigor@sysoev.ru 
40120Sigor@sysoev.ru 
40220Sigor@sysoev.ru static void
40320Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
40420Sigor@sysoev.ru {
40527Svbart@nginx.com     nxt_buf_t                 *b;
40662Sigor@sysoev.ru     nxt_conn_t                *c;
40727Svbart@nginx.com     nxt_event_engine_t        *engine;
40827Svbart@nginx.com     nxt_controller_request_t  *r;
40920Sigor@sysoev.ru 
41020Sigor@sysoev.ru     c = obj;
41120Sigor@sysoev.ru 
41220Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
41320Sigor@sysoev.ru 
41465Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
41527Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
41627Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
41727Svbart@nginx.com         return;
41827Svbart@nginx.com     }
41927Svbart@nginx.com 
420140Svbart@nginx.com     r->conn = c;
421140Svbart@nginx.com 
42260Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
42360Svbart@nginx.com                       != NXT_OK))
42460Svbart@nginx.com     {
42560Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
42660Svbart@nginx.com         return;
42760Svbart@nginx.com     }
42827Svbart@nginx.com 
42920Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
43020Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
43120Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
43220Sigor@sysoev.ru         return;
43320Sigor@sysoev.ru     }
43420Sigor@sysoev.ru 
43520Sigor@sysoev.ru     c->read = b;
43627Svbart@nginx.com     c->socket.data = r;
43720Sigor@sysoev.ru     c->socket.read_ready = 1;
43820Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
43920Sigor@sysoev.ru 
44020Sigor@sysoev.ru     engine = task->thread->engine;
44120Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
44227Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
44320Sigor@sysoev.ru 
44462Sigor@sysoev.ru     nxt_conn_read(engine, c);
44520Sigor@sysoev.ru }
44620Sigor@sysoev.ru 
44720Sigor@sysoev.ru 
44820Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
44920Sigor@sysoev.ru     nxt_aligned(64) =
45020Sigor@sysoev.ru {
45156Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
45256Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
45356Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
45420Sigor@sysoev.ru 
45556Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
45656Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
45756Sigor@sysoev.ru     .timer_data = 60 * 1000,
45820Sigor@sysoev.ru };
45920Sigor@sysoev.ru 
46020Sigor@sysoev.ru 
46120Sigor@sysoev.ru static void
46220Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
46320Sigor@sysoev.ru {
46427Svbart@nginx.com     size_t                    preread;
46527Svbart@nginx.com     nxt_buf_t                 *b;
46627Svbart@nginx.com     nxt_int_t                 rc;
46762Sigor@sysoev.ru     nxt_conn_t                *c;
46827Svbart@nginx.com     nxt_controller_request_t  *r;
46920Sigor@sysoev.ru 
47020Sigor@sysoev.ru     c = obj;
47127Svbart@nginx.com     r = data;
47220Sigor@sysoev.ru 
47320Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
47420Sigor@sysoev.ru 
47527Svbart@nginx.com     nxt_queue_remove(&c->link);
47627Svbart@nginx.com     nxt_queue_self(&c->link);
47727Svbart@nginx.com 
47827Svbart@nginx.com     b = c->read;
47927Svbart@nginx.com 
48027Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
48127Svbart@nginx.com 
48227Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
48327Svbart@nginx.com 
48427Svbart@nginx.com         if (rc == NXT_AGAIN) {
48527Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
48627Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
48727Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
48827Svbart@nginx.com                 return;
48927Svbart@nginx.com             }
49027Svbart@nginx.com 
49162Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
49227Svbart@nginx.com             return;
49327Svbart@nginx.com         }
49427Svbart@nginx.com 
49527Svbart@nginx.com         /* rc == NXT_ERROR */
49627Svbart@nginx.com 
49727Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
49827Svbart@nginx.com 
49927Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
50027Svbart@nginx.com         return;
50127Svbart@nginx.com     }
50227Svbart@nginx.com 
503417Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash,
504417Svbart@nginx.com                                  r);
50560Svbart@nginx.com 
50660Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
50760Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
50860Svbart@nginx.com         return;
50960Svbart@nginx.com     }
51060Svbart@nginx.com 
51127Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
51227Svbart@nginx.com 
51327Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
514107Svbart@nginx.com                     "body length: %uz, preread: %uz",
51527Svbart@nginx.com                     r->length, preread);
51627Svbart@nginx.com 
51727Svbart@nginx.com     if (preread >= r->length) {
518140Svbart@nginx.com         nxt_controller_process_request(task, r);
51927Svbart@nginx.com         return;
52027Svbart@nginx.com     }
52127Svbart@nginx.com 
52227Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
52327Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
52427Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
52527Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
52627Svbart@nginx.com             return;
52727Svbart@nginx.com         }
52827Svbart@nginx.com 
52927Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
53027Svbart@nginx.com 
53127Svbart@nginx.com         c->read = b;
53227Svbart@nginx.com     }
53327Svbart@nginx.com 
53427Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
53527Svbart@nginx.com 
53662Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
53720Sigor@sysoev.ru }
53820Sigor@sysoev.ru 
53920Sigor@sysoev.ru 
54020Sigor@sysoev.ru static nxt_msec_t
54162Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
54220Sigor@sysoev.ru {
54320Sigor@sysoev.ru     return (nxt_msec_t) data;
54420Sigor@sysoev.ru }
54520Sigor@sysoev.ru 
54620Sigor@sysoev.ru 
54720Sigor@sysoev.ru static void
54820Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
54920Sigor@sysoev.ru {
55062Sigor@sysoev.ru     nxt_conn_t  *c;
55120Sigor@sysoev.ru 
55220Sigor@sysoev.ru     c = obj;
55320Sigor@sysoev.ru 
55420Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
55520Sigor@sysoev.ru 
55627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
55720Sigor@sysoev.ru }
55820Sigor@sysoev.ru 
55920Sigor@sysoev.ru 
56020Sigor@sysoev.ru static void
56120Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
56220Sigor@sysoev.ru {
56362Sigor@sysoev.ru     nxt_timer_t  *timer;
56462Sigor@sysoev.ru     nxt_conn_t   *c;
56520Sigor@sysoev.ru 
56662Sigor@sysoev.ru     timer = obj;
56720Sigor@sysoev.ru 
56862Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
56920Sigor@sysoev.ru     c->socket.timedout = 1;
57020Sigor@sysoev.ru     c->socket.closed = 1;
57120Sigor@sysoev.ru 
57220Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
57320Sigor@sysoev.ru 
57427Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
57527Svbart@nginx.com }
57627Svbart@nginx.com 
57727Svbart@nginx.com 
57827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
57927Svbart@nginx.com     nxt_aligned(64) =
58027Svbart@nginx.com {
58156Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
58256Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
58356Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
58427Svbart@nginx.com 
58556Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
58656Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
58756Sigor@sysoev.ru     .timer_data = 60 * 1000,
58856Sigor@sysoev.ru     .timer_autoreset = 1,
58927Svbart@nginx.com };
59027Svbart@nginx.com 
59127Svbart@nginx.com 
59227Svbart@nginx.com static void
59327Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
59427Svbart@nginx.com {
595107Svbart@nginx.com     size_t                    read;
596107Svbart@nginx.com     nxt_buf_t                 *b;
597107Svbart@nginx.com     nxt_conn_t                *c;
598107Svbart@nginx.com     nxt_controller_request_t  *r;
59927Svbart@nginx.com 
60027Svbart@nginx.com     c = obj;
601107Svbart@nginx.com     r = data;
60227Svbart@nginx.com     b = c->read;
60327Svbart@nginx.com 
604107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
60527Svbart@nginx.com 
606107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
607107Svbart@nginx.com               read, r->length);
60827Svbart@nginx.com 
609107Svbart@nginx.com     if (read >= r->length) {
610140Svbart@nginx.com         nxt_controller_process_request(task, r);
61127Svbart@nginx.com         return;
61227Svbart@nginx.com     }
61327Svbart@nginx.com 
61462Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
61527Svbart@nginx.com }
61627Svbart@nginx.com 
61727Svbart@nginx.com 
61827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
61927Svbart@nginx.com     nxt_aligned(64) =
62027Svbart@nginx.com {
62156Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
62256Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
62327Svbart@nginx.com 
62456Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
62556Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
62656Sigor@sysoev.ru     .timer_data = 60 * 1000,
62756Sigor@sysoev.ru     .timer_autoreset = 1,
62827Svbart@nginx.com };
62927Svbart@nginx.com 
63027Svbart@nginx.com 
63127Svbart@nginx.com static void
63227Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
63327Svbart@nginx.com {
63462Sigor@sysoev.ru     nxt_buf_t   *b;
63562Sigor@sysoev.ru     nxt_conn_t  *c;
63627Svbart@nginx.com 
63727Svbart@nginx.com     c = obj;
63827Svbart@nginx.com 
63927Svbart@nginx.com     nxt_debug(task, "controller conn write");
64027Svbart@nginx.com 
64127Svbart@nginx.com     b = c->write;
64227Svbart@nginx.com 
64327Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
64462Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
64527Svbart@nginx.com         return;
64627Svbart@nginx.com     }
64727Svbart@nginx.com 
64827Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
64927Svbart@nginx.com 
65027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
65127Svbart@nginx.com }
65227Svbart@nginx.com 
65327Svbart@nginx.com 
65427Svbart@nginx.com static void
65527Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
65627Svbart@nginx.com {
65762Sigor@sysoev.ru     nxt_conn_t  *c;
65827Svbart@nginx.com 
65927Svbart@nginx.com     c = obj;
66027Svbart@nginx.com 
66127Svbart@nginx.com     nxt_debug(task, "controller conn write error");
66227Svbart@nginx.com 
66327Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
66427Svbart@nginx.com }
66527Svbart@nginx.com 
66627Svbart@nginx.com 
66727Svbart@nginx.com static void
66827Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
66927Svbart@nginx.com {
67062Sigor@sysoev.ru     nxt_conn_t   *c;
67162Sigor@sysoev.ru     nxt_timer_t  *timer;
67227Svbart@nginx.com 
67362Sigor@sysoev.ru     timer = obj;
67427Svbart@nginx.com 
67562Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
67627Svbart@nginx.com     c->socket.timedout = 1;
67727Svbart@nginx.com     c->socket.closed = 1;
67827Svbart@nginx.com 
67927Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
68027Svbart@nginx.com 
68127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
68220Sigor@sysoev.ru }
68320Sigor@sysoev.ru 
68420Sigor@sysoev.ru 
68520Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
68620Sigor@sysoev.ru     nxt_aligned(64) =
68720Sigor@sysoev.ru {
68856Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
68920Sigor@sysoev.ru };
69020Sigor@sysoev.ru 
69120Sigor@sysoev.ru 
69220Sigor@sysoev.ru static void
69320Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
69420Sigor@sysoev.ru {
69562Sigor@sysoev.ru     nxt_conn_t  *c;
69620Sigor@sysoev.ru 
69720Sigor@sysoev.ru     c = obj;
69820Sigor@sysoev.ru 
69920Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
70020Sigor@sysoev.ru 
70127Svbart@nginx.com     nxt_queue_remove(&c->link);
70227Svbart@nginx.com 
70320Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
70420Sigor@sysoev.ru 
70562Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
70620Sigor@sysoev.ru }
70720Sigor@sysoev.ru 
70820Sigor@sysoev.ru 
70920Sigor@sysoev.ru static void
71020Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
71120Sigor@sysoev.ru {
71262Sigor@sysoev.ru     nxt_conn_t  *c;
71320Sigor@sysoev.ru 
71420Sigor@sysoev.ru     c = obj;
71520Sigor@sysoev.ru 
71620Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
71720Sigor@sysoev.ru 
718337Sigor@sysoev.ru     nxt_sockaddr_cache_free(task->thread->engine, c);
719337Sigor@sysoev.ru 
720386Sigor@sysoev.ru     nxt_conn_free(task, c);
72120Sigor@sysoev.ru }
72227Svbart@nginx.com 
72327Svbart@nginx.com 
72427Svbart@nginx.com static nxt_int_t
72560Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
726417Svbart@nginx.com     uintptr_t data)
72727Svbart@nginx.com {
72827Svbart@nginx.com     off_t                     length;
72927Svbart@nginx.com     nxt_controller_request_t  *r;
73027Svbart@nginx.com 
73127Svbart@nginx.com     r = ctx;
73227Svbart@nginx.com 
733417Svbart@nginx.com     length = nxt_off_t_parse(field->value, field->value_length);
73427Svbart@nginx.com 
73527Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
736107Svbart@nginx.com 
737107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
738417Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, &r->conn->log,
739417Svbart@nginx.com                           "Content-Length is too big");
740107Svbart@nginx.com             return NXT_ERROR;
741107Svbart@nginx.com         }
74227Svbart@nginx.com 
74327Svbart@nginx.com         r->length = length;
74427Svbart@nginx.com         return NXT_OK;
74527Svbart@nginx.com     }
74627Svbart@nginx.com 
747417Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, &r->conn->log, "Content-Length is invalid");
74827Svbart@nginx.com 
74927Svbart@nginx.com     return NXT_ERROR;
75027Svbart@nginx.com }
75127Svbart@nginx.com 
75227Svbart@nginx.com 
75327Svbart@nginx.com static void
754140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
75527Svbart@nginx.com {
75665Sigor@sysoev.ru     nxt_mp_t                   *mp;
75751Svbart@nginx.com     nxt_int_t                  rc;
75846Svbart@nginx.com     nxt_str_t                  path;
759140Svbart@nginx.com     nxt_conn_t                 *c;
76051Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
761106Svbart@nginx.com     nxt_conf_op_t              *ops;
762106Svbart@nginx.com     nxt_conf_value_t           *value;
763357Svbart@nginx.com     nxt_conf_validation_t      vldt;
764208Svbart@nginx.com     nxt_conf_json_error_t      error;
76544Svbart@nginx.com     nxt_controller_response_t  resp;
76644Svbart@nginx.com 
76751Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
76851Svbart@nginx.com 
769140Svbart@nginx.com     c = req->conn;
770112Smax.romanov@nginx.com     path = req->parser.path;
77151Svbart@nginx.com 
77251Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
77351Svbart@nginx.com         path.length--;
77451Svbart@nginx.com     }
77551Svbart@nginx.com 
77644Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
77744Svbart@nginx.com 
77844Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
77946Svbart@nginx.com 
780106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
78151Svbart@nginx.com 
78251Svbart@nginx.com         if (value == NULL) {
783208Svbart@nginx.com             goto not_found;
78451Svbart@nginx.com         }
78551Svbart@nginx.com 
786208Svbart@nginx.com         resp.status = 200;
787106Svbart@nginx.com         resp.conf = value;
78846Svbart@nginx.com 
789208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
790208Svbart@nginx.com         return;
79151Svbart@nginx.com     }
79251Svbart@nginx.com 
79351Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
79446Svbart@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 
80065Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
80151Svbart@nginx.com 
80251Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
803208Svbart@nginx.com             goto alloc_fail;
80446Svbart@nginx.com         }
80546Svbart@nginx.com 
80651Svbart@nginx.com         mbuf = &c->read->mem;
80751Svbart@nginx.com 
808208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
809208Svbart@nginx.com 
810208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
81151Svbart@nginx.com 
81251Svbart@nginx.com         if (value == NULL) {
81365Sigor@sysoev.ru             nxt_mp_destroy(mp);
814208Svbart@nginx.com 
815208Svbart@nginx.com             if (error.pos == NULL) {
816208Svbart@nginx.com                 goto alloc_fail;
817208Svbart@nginx.com             }
818208Svbart@nginx.com 
819208Svbart@nginx.com             resp.status = 400;
820208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
821357Svbart@nginx.com             resp.detail.length = nxt_strlen(error.detail);
822357Svbart@nginx.com             resp.detail.start = error.detail;
823208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
824208Svbart@nginx.com 
825208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
826208Svbart@nginx.com                                    &resp.line, &resp.column);
827208Svbart@nginx.com 
828208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
829208Svbart@nginx.com             return;
83051Svbart@nginx.com         }
83151Svbart@nginx.com 
83251Svbart@nginx.com         if (path.length != 1) {
833106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
834106Svbart@nginx.com                                      nxt_controller_conf.root,
835106Svbart@nginx.com                                      &path, value);
83646Svbart@nginx.com 
83751Svbart@nginx.com             if (rc != NXT_OK) {
83851Svbart@nginx.com                 if (rc == NXT_DECLINED) {
839208Svbart@nginx.com                     goto not_found;
84051Svbart@nginx.com                 }
84146Svbart@nginx.com 
842208Svbart@nginx.com                 goto alloc_fail;
84351Svbart@nginx.com             }
84451Svbart@nginx.com 
845106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
84651Svbart@nginx.com 
84751Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
84865Sigor@sysoev.ru                 nxt_mp_destroy(mp);
849208Svbart@nginx.com                 goto alloc_fail;
85051Svbart@nginx.com             }
85146Svbart@nginx.com         }
85244Svbart@nginx.com 
853357Svbart@nginx.com         nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
854357Svbart@nginx.com 
855357Svbart@nginx.com         vldt.conf = value;
856357Svbart@nginx.com         vldt.pool = c->mem_pool;
857357Svbart@nginx.com 
858357Svbart@nginx.com         rc = nxt_conf_validate(&vldt);
859357Svbart@nginx.com 
860357Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
861121Svbart@nginx.com             nxt_mp_destroy(mp);
862357Svbart@nginx.com 
863357Svbart@nginx.com             if (rc == NXT_DECLINED) {
864357Svbart@nginx.com                 resp.detail = vldt.error;
865357Svbart@nginx.com                 goto invalid_conf;
866357Svbart@nginx.com             }
867357Svbart@nginx.com 
868357Svbart@nginx.com             /* rc == NXT_ERROR */
869357Svbart@nginx.com             goto alloc_fail;
870116Svbart@nginx.com         }
871116Svbart@nginx.com 
872249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
873249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
874247Svbart@nginx.com 
875247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
876121Svbart@nginx.com             nxt_mp_destroy(mp);
877247Svbart@nginx.com 
878247Svbart@nginx.com             if (rc == NXT_DECLINED) {
879247Svbart@nginx.com                 goto no_router;
880247Svbart@nginx.com             }
881247Svbart@nginx.com 
882247Svbart@nginx.com             /* rc == NXT_ERROR */
883208Svbart@nginx.com             goto alloc_fail;
884121Svbart@nginx.com         }
885121Svbart@nginx.com 
886249Svbart@nginx.com         req->conf.root = value;
887249Svbart@nginx.com         req->conf.pool = mp;
888249Svbart@nginx.com 
889249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
890249Svbart@nginx.com 
891140Svbart@nginx.com         return;
89251Svbart@nginx.com     }
89327Svbart@nginx.com 
89451Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
89551Svbart@nginx.com 
896238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
897238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
898238Svbart@nginx.com             return;
899238Svbart@nginx.com         }
900238Svbart@nginx.com 
90151Svbart@nginx.com         if (path.length == 1) {
90265Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
90344Svbart@nginx.com 
90451Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
905208Svbart@nginx.com                 goto alloc_fail;
90651Svbart@nginx.com             }
90751Svbart@nginx.com 
908106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
90927Svbart@nginx.com 
91044Svbart@nginx.com         } else {
911106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
912106Svbart@nginx.com                                      nxt_controller_conf.root,
913106Svbart@nginx.com                                      &path, NULL);
91451Svbart@nginx.com 
91551Svbart@nginx.com             if (rc != NXT_OK) {
91651Svbart@nginx.com                 if (rc == NXT_DECLINED) {
917208Svbart@nginx.com                     goto not_found;
91851Svbart@nginx.com                 }
91951Svbart@nginx.com 
920208Svbart@nginx.com                 goto alloc_fail;
92151Svbart@nginx.com             }
92251Svbart@nginx.com 
92365Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
92451Svbart@nginx.com 
92551Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
926208Svbart@nginx.com                 goto alloc_fail;
92751Svbart@nginx.com             }
92851Svbart@nginx.com 
929106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
93051Svbart@nginx.com         }
93151Svbart@nginx.com 
93251Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
93365Sigor@sysoev.ru             nxt_mp_destroy(mp);
934208Svbart@nginx.com             goto alloc_fail;
93544Svbart@nginx.com         }
93644Svbart@nginx.com 
937357Svbart@nginx.com         nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
938357Svbart@nginx.com 
939357Svbart@nginx.com         vldt.conf = value;
940357Svbart@nginx.com         vldt.pool = c->mem_pool;
941357Svbart@nginx.com 
942357Svbart@nginx.com         rc = nxt_conf_validate(&vldt);
943357Svbart@nginx.com 
944357Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
945121Svbart@nginx.com             nxt_mp_destroy(mp);
946357Svbart@nginx.com 
947357Svbart@nginx.com             if (rc == NXT_DECLINED) {
948357Svbart@nginx.com                 resp.detail = vldt.error;
949357Svbart@nginx.com                 goto invalid_conf;
950357Svbart@nginx.com             }
951357Svbart@nginx.com 
952357Svbart@nginx.com             /* rc == NXT_ERROR */
953357Svbart@nginx.com             goto alloc_fail;
954116Svbart@nginx.com         }
955116Svbart@nginx.com 
956249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
957249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
958247Svbart@nginx.com 
959247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
960121Svbart@nginx.com             nxt_mp_destroy(mp);
961247Svbart@nginx.com 
962247Svbart@nginx.com             if (rc == NXT_DECLINED) {
963247Svbart@nginx.com                 goto no_router;
964247Svbart@nginx.com             }
965247Svbart@nginx.com 
966247Svbart@nginx.com             /* rc == NXT_ERROR */
967208Svbart@nginx.com             goto alloc_fail;
968121Svbart@nginx.com         }
969121Svbart@nginx.com 
970249Svbart@nginx.com         req->conf.root = value;
971249Svbart@nginx.com         req->conf.pool = mp;
972249Svbart@nginx.com 
973249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
974249Svbart@nginx.com 
975140Svbart@nginx.com         return;
97651Svbart@nginx.com     }
97751Svbart@nginx.com 
978208Svbart@nginx.com     resp.status = 405;
979208Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
980208Svbart@nginx.com     resp.offset = -1;
98151Svbart@nginx.com 
982208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
983208Svbart@nginx.com     return;
98451Svbart@nginx.com 
985208Svbart@nginx.com not_found:
986208Svbart@nginx.com 
987208Svbart@nginx.com     resp.status = 404;
988208Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
989208Svbart@nginx.com     resp.offset = -1;
990208Svbart@nginx.com 
991208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
992208Svbart@nginx.com     return;
993208Svbart@nginx.com 
994208Svbart@nginx.com invalid_conf:
995208Svbart@nginx.com 
996208Svbart@nginx.com     resp.status = 400;
997208Svbart@nginx.com     resp.title = (u_char *) "Invalid configuration.";
998208Svbart@nginx.com     resp.offset = -1;
999208Svbart@nginx.com 
1000208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1001208Svbart@nginx.com     return;
1002247Svbart@nginx.com 
1003247Svbart@nginx.com alloc_fail:
1004247Svbart@nginx.com 
1005247Svbart@nginx.com     resp.status = 500;
1006247Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
1007247Svbart@nginx.com     resp.offset = -1;
1008247Svbart@nginx.com 
1009247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1010247Svbart@nginx.com     return;
1011247Svbart@nginx.com 
1012247Svbart@nginx.com no_router:
1013247Svbart@nginx.com 
1014247Svbart@nginx.com     resp.status = 500;
1015247Svbart@nginx.com     resp.title = (u_char *) "Router process isn't available.";
1016247Svbart@nginx.com     resp.offset = -1;
1017247Svbart@nginx.com 
1018247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1019247Svbart@nginx.com     return;
102027Svbart@nginx.com }
102127Svbart@nginx.com 
102227Svbart@nginx.com 
1023193Smax.romanov@nginx.com static void
1024193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
1025193Smax.romanov@nginx.com     void *data)
1026140Svbart@nginx.com {
1027238Svbart@nginx.com     nxt_queue_t                queue;
1028140Svbart@nginx.com     nxt_controller_request_t   *req;
1029140Svbart@nginx.com     nxt_controller_response_t  resp;
1030140Svbart@nginx.com 
1031238Svbart@nginx.com     req = data;
1032238Svbart@nginx.com 
1033201Svbart@nginx.com     nxt_debug(task, "controller conf ready: %*s",
1034201Svbart@nginx.com               nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos);
1035140Svbart@nginx.com 
1036238Svbart@nginx.com     nxt_queue_remove(&req->link);
1037140Svbart@nginx.com 
1038238Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
1039140Svbart@nginx.com 
1040193Smax.romanov@nginx.com     if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) {
1041140Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
1042140Svbart@nginx.com 
1043140Svbart@nginx.com         nxt_controller_conf = req->