xref: /unit/src/nxt_controller.c (revision 357)
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;
34*357Svbart@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,
6867Svbart@nginx.com     nxt_http_field_t *field, nxt_log_t *log);
6927Svbart@nginx.com 
7027Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
71140Svbart@nginx.com     nxt_controller_request_t *req);
72238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task,
73238Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
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 
8260Svbart@nginx.com static nxt_http_fields_hash_entry_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     { nxt_null_string, NULL, 0 }
8727Svbart@nginx.com };
8827Svbart@nginx.com 
8960Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_fields_hash;
9027Svbart@nginx.com 
91314Svbart@nginx.com static nxt_uint_t              nxt_controller_listening;
92238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
93238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
9427Svbart@nginx.com 
9520Sigor@sysoev.ru 
9620Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
9727Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
9827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
9920Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
10020Sigor@sysoev.ru 
10120Sigor@sysoev.ru 
102320Smax.romanov@nginx.com nxt_port_handlers_t  nxt_controller_process_port_handlers = {
103320Smax.romanov@nginx.com     .quit         = nxt_worker_process_quit_handler,
104320Smax.romanov@nginx.com     .new_port     = nxt_controller_process_new_port_handler,
105320Smax.romanov@nginx.com     .change_file  = nxt_port_change_log_file_handler,
106320Smax.romanov@nginx.com     .mmap         = nxt_port_mmap_handler,
107320Smax.romanov@nginx.com     .data         = nxt_port_data_handler,
108320Smax.romanov@nginx.com     .remove_pid   = nxt_port_remove_pid_handler,
109320Smax.romanov@nginx.com     .rpc_ready    = nxt_port_rpc_handler,
110320Smax.romanov@nginx.com     .rpc_error    = nxt_port_rpc_handler,
111248Svbart@nginx.com };
112248Svbart@nginx.com 
113248Svbart@nginx.com 
11420Sigor@sysoev.ru nxt_int_t
115141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
11620Sigor@sysoev.ru {
117314Svbart@nginx.com     nxt_mp_t                *mp;
118*357Svbart@nginx.com     nxt_int_t               ret;
119314Svbart@nginx.com     nxt_str_t               *json;
120141Smax.romanov@nginx.com     nxt_runtime_t           *rt;
121314Svbart@nginx.com     nxt_conf_value_t        *conf;
122337Sigor@sysoev.ru     nxt_event_engine_t      *engine;
123*357Svbart@nginx.com     nxt_conf_validation_t   vldt;
12427Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
12527Svbart@nginx.com 
126141Smax.romanov@nginx.com     rt = task->thread->runtime;
127141Smax.romanov@nginx.com 
128337Sigor@sysoev.ru     engine = task->thread->engine;
129337Sigor@sysoev.ru 
130337Sigor@sysoev.ru     engine->mem_pool = nxt_mp_create(4096, 128, 1024, 64);
131337Sigor@sysoev.ru     if (nxt_slow_path(engine->mem_pool == NULL)) {
132337Sigor@sysoev.ru         return NXT_ERROR;
133337Sigor@sysoev.ru     }
134337Sigor@sysoev.ru 
13560Svbart@nginx.com     hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
13660Svbart@nginx.com                                        rt->mem_pool);
13727Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
13827Svbart@nginx.com         return NXT_ERROR;
13927Svbart@nginx.com     }
14027Svbart@nginx.com 
14160Svbart@nginx.com     nxt_controller_fields_hash = hash;
142248Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
14327Svbart@nginx.com 
144314Svbart@nginx.com     json = data;
145314Svbart@nginx.com 
146314Svbart@nginx.com     if (json->length == 0) {
147314Svbart@nginx.com         return NXT_OK;
148314Svbart@nginx.com     }
149314Svbart@nginx.com 
150314Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
151314Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
152314Svbart@nginx.com         return NXT_ERROR;
153314Svbart@nginx.com     }
154314Svbart@nginx.com 
155314Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, json);
156314Svbart@nginx.com     nxt_free(json->start);
157314Svbart@nginx.com 
158314Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
159314Svbart@nginx.com         nxt_log(task, NXT_LOG_ALERT,
160314Svbart@nginx.com                 "failed to restore previous configuration: "
161314Svbart@nginx.com                 "file is corrupted or not enough memory");
162314Svbart@nginx.com 
163314Svbart@nginx.com         nxt_mp_destroy(mp);
164314Svbart@nginx.com         return NXT_OK;
165314Svbart@nginx.com     }
166314Svbart@nginx.com 
167*357Svbart@nginx.com     nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
168314Svbart@nginx.com 
169*357Svbart@nginx.com     vldt.pool = nxt_mp_create(1024, 128, 256, 32);
170*357Svbart@nginx.com     if (nxt_slow_path(vldt.pool == NULL)) {
171*357Svbart@nginx.com         return NXT_ERROR;
172314Svbart@nginx.com     }
173314Svbart@nginx.com 
174*357Svbart@nginx.com     vldt.conf = conf;
175*357Svbart@nginx.com 
176*357Svbart@nginx.com     ret = nxt_conf_validate(&vldt);
177*357Svbart@nginx.com 
178*357Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
179*357Svbart@nginx.com 
180*357Svbart@nginx.com         if (ret == NXT_DECLINED) {
181*357Svbart@nginx.com             nxt_log(task, NXT_LOG_ALERT,
182*357Svbart@nginx.com                     "the previous configuration is invalid: %V", &vldt.error);
183*357Svbart@nginx.com 
184*357Svbart@nginx.com             nxt_mp_destroy(vldt.pool);
185*357Svbart@nginx.com             nxt_mp_destroy(mp);
186*357Svbart@nginx.com 
187*357Svbart@nginx.com             return NXT_OK;
188*357Svbart@nginx.com         }
189*357Svbart@nginx.com 
190*357Svbart@nginx.com         /* ret == NXT_ERROR */
191*357Svbart@nginx.com 
192*357Svbart@nginx.com         return NXT_ERROR;
193*357Svbart@nginx.com     }
194*357Svbart@nginx.com 
195*357Svbart@nginx.com     nxt_mp_destroy(vldt.pool);
196314Svbart@nginx.com 
197314Svbart@nginx.com     nxt_controller_conf.root = conf;
198314Svbart@nginx.com     nxt_controller_conf.pool = mp;
199314Svbart@nginx.com 
200248Svbart@nginx.com     return NXT_OK;
201248Svbart@nginx.com }
202248Svbart@nginx.com 
203248Svbart@nginx.com 
204248Svbart@nginx.com static void
205248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task,
206248Svbart@nginx.com     nxt_port_recv_msg_t *msg)
207248Svbart@nginx.com {
208249Svbart@nginx.com     nxt_int_t         rc;
209248Svbart@nginx.com     nxt_runtime_t     *rt;
210248Svbart@nginx.com     nxt_conf_value_t  *conf;
211248Svbart@nginx.com 
212248Svbart@nginx.com     nxt_port_new_port_handler(task, msg);
213248Svbart@nginx.com 
214347Smax.romanov@nginx.com     if (msg->u.new_port->type != NXT_PROCESS_ROUTER) {
215248Svbart@nginx.com         return;
21620Sigor@sysoev.ru     }
21720Sigor@sysoev.ru 
218249Svbart@nginx.com     conf = nxt_controller_conf.root;
219249Svbart@nginx.com 
220249Svbart@nginx.com     if (conf != NULL) {
221249Svbart@nginx.com         rc = nxt_controller_conf_send(task, conf,
222249Svbart@nginx.com                                       nxt_controller_conf_init_handler, NULL);
22344Svbart@nginx.com 
224249Svbart@nginx.com         if (nxt_fast_path(rc == NXT_OK)) {
225249Svbart@nginx.com             return;
226249Svbart@nginx.com         }
227249Svbart@nginx.com 
228249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
229249Svbart@nginx.com 
230249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
231249Svbart@nginx.com             nxt_abort();
232249Svbart@nginx.com         }
23344Svbart@nginx.com     }
23444Svbart@nginx.com 
235249Svbart@nginx.com     if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
236248Svbart@nginx.com         nxt_abort();
23744Svbart@nginx.com     }
23844Svbart@nginx.com 
239248Svbart@nginx.com     rt = task->thread->runtime;
240140Svbart@nginx.com 
241248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
242248Svbart@nginx.com         nxt_abort();
243248Svbart@nginx.com     }
244314Svbart@nginx.com 
245314Svbart@nginx.com     nxt_controller_listening = 1;
24620Sigor@sysoev.ru }
24720Sigor@sysoev.ru 
24820Sigor@sysoev.ru 
249249Svbart@nginx.com static nxt_int_t
250249Svbart@nginx.com nxt_controller_conf_default(void)
251249Svbart@nginx.com {
252249Svbart@nginx.com     nxt_mp_t          *mp;
253249Svbart@nginx.com     nxt_conf_value_t  *conf;
254249Svbart@nginx.com 
255249Svbart@nginx.com     static const nxt_str_t json
256249Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
257249Svbart@nginx.com 
258249Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
259249Svbart@nginx.com 
260249Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
261249Svbart@nginx.com         return NXT_ERROR;
262249Svbart@nginx.com     }
263249Svbart@nginx.com 
264249Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
265249Svbart@nginx.com 
266249Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
267249Svbart@nginx.com         return NXT_ERROR;
268249Svbart@nginx.com     }
269249Svbart@nginx.com 
270249Svbart@nginx.com     nxt_controller_conf.root = conf;
271249Svbart@nginx.com     nxt_controller_conf.pool = mp;
272249Svbart@nginx.com 
273249Svbart@nginx.com     return NXT_OK;
274249Svbart@nginx.com }
275249Svbart@nginx.com 
276249Svbart@nginx.com 
277249Svbart@nginx.com static void
278249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
279249Svbart@nginx.com     void *data)
280249Svbart@nginx.com {
281314Svbart@nginx.com     nxt_runtime_t  *rt;
282314Svbart@nginx.com 
283249Svbart@nginx.com     if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) {
284314Svbart@nginx.com         nxt_log(task, NXT_LOG_ALERT, "failed to apply previous configuration");
285314Svbart@nginx.com 
286249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
287249Svbart@nginx.com 
288249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
289249Svbart@nginx.com             nxt_abort();
290249Svbart@nginx.com         }
291249Svbart@nginx.com     }
292314Svbart@nginx.com 
293314Svbart@nginx.com     if (nxt_controller_listening == 0) {
294314Svbart@nginx.com         rt = task->thread->runtime;
295314Svbart@nginx.com 
296314Svbart@nginx.com         if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket)
297314Svbart@nginx.com                           == NULL))
298314Svbart@nginx.com         {
299314Svbart@nginx.com             nxt_abort();
300314Svbart@nginx.com         }
301314Svbart@nginx.com 
302314Svbart@nginx.com         nxt_controller_listening = 1;
303314Svbart@nginx.com     }
304249Svbart@nginx.com }
305249Svbart@nginx.com 
306249Svbart@nginx.com 
307249Svbart@nginx.com static nxt_int_t
308249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf,
309249Svbart@nginx.com     nxt_port_rpc_handler_t handler, void *data)
310249Svbart@nginx.com {
311249Svbart@nginx.com     size_t         size;
312249Svbart@nginx.com     uint32_t       stream;
313249Svbart@nginx.com     nxt_int_t      rc;
314249Svbart@nginx.com     nxt_buf_t      *b;
315249Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
316249Svbart@nginx.com     nxt_runtime_t  *rt;
317249Svbart@nginx.com 
318249Svbart@nginx.com     rt = task->thread->runtime;
319249Svbart@nginx.com 
320249Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
321249Svbart@nginx.com 
322249Svbart@nginx.com     if (nxt_slow_path(router_port == NULL)) {
323249Svbart@nginx.com         return NXT_DECLINED;
324249Svbart@nginx.com     }
325249Svbart@nginx.com 
326249Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
327249Svbart@nginx.com 
328249Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
329249Svbart@nginx.com 
330249Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
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 
372312Sigor@sysoev.ru     nxt_listen_socket_remote_size(ls, sa);
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 
42967Svbart@nginx.com     r->parser.fields_hash = nxt_controller_fields_hash;
43067Svbart@nginx.com 
43120Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
43220Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
43320Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
43420Sigor@sysoev.ru         return;
43520Sigor@sysoev.ru     }
43620Sigor@sysoev.ru 
43720Sigor@sysoev.ru     c->read = b;
43827Svbart@nginx.com     c->socket.data = r;
43920Sigor@sysoev.ru     c->socket.read_ready = 1;
44020Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
44120Sigor@sysoev.ru 
44220Sigor@sysoev.ru     engine = task->thread->engine;
44320Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
44427Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
44520Sigor@sysoev.ru 
44662Sigor@sysoev.ru     nxt_conn_read(engine, c);
44720Sigor@sysoev.ru }
44820Sigor@sysoev.ru 
44920Sigor@sysoev.ru 
45020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
45120Sigor@sysoev.ru     nxt_aligned(64) =
45220Sigor@sysoev.ru {
45356Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
45456Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
45556Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
45620Sigor@sysoev.ru 
45756Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
45856Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
45956Sigor@sysoev.ru     .timer_data = 60 * 1000,
46020Sigor@sysoev.ru };
46120Sigor@sysoev.ru 
46220Sigor@sysoev.ru 
46320Sigor@sysoev.ru static void
46420Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
46520Sigor@sysoev.ru {
46627Svbart@nginx.com     size_t                    preread;
46727Svbart@nginx.com     nxt_buf_t                 *b;
46827Svbart@nginx.com     nxt_int_t                 rc;
46962Sigor@sysoev.ru     nxt_conn_t                *c;
47027Svbart@nginx.com     nxt_controller_request_t  *r;
47120Sigor@sysoev.ru 
47220Sigor@sysoev.ru     c = obj;
47327Svbart@nginx.com     r = data;
47420Sigor@sysoev.ru 
47520Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
47620Sigor@sysoev.ru 
47727Svbart@nginx.com     nxt_queue_remove(&c->link);
47827Svbart@nginx.com     nxt_queue_self(&c->link);
47927Svbart@nginx.com 
48027Svbart@nginx.com     b = c->read;
48127Svbart@nginx.com 
48227Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
48327Svbart@nginx.com 
48427Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
48527Svbart@nginx.com 
48627Svbart@nginx.com         if (rc == NXT_AGAIN) {
48727Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
48827Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
48927Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
49027Svbart@nginx.com                 return;
49127Svbart@nginx.com             }
49227Svbart@nginx.com 
49362Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
49427Svbart@nginx.com             return;
49527Svbart@nginx.com         }
49627Svbart@nginx.com 
49727Svbart@nginx.com         /* rc == NXT_ERROR */
49827Svbart@nginx.com 
49927Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
50027Svbart@nginx.com 
50127Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
50227Svbart@nginx.com         return;
50327Svbart@nginx.com     }
50427Svbart@nginx.com 
50567Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, r, task->log);
50660Svbart@nginx.com 
50760Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
50860Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
50960Svbart@nginx.com         return;
51060Svbart@nginx.com     }
51160Svbart@nginx.com 
51227Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
51327Svbart@nginx.com 
51427Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
515107Svbart@nginx.com                     "body length: %uz, preread: %uz",
51627Svbart@nginx.com                     r->length, preread);
51727Svbart@nginx.com 
51827Svbart@nginx.com     if (preread >= r->length) {
519140Svbart@nginx.com         nxt_controller_process_request(task, r);
52027Svbart@nginx.com         return;
52127Svbart@nginx.com     }
52227Svbart@nginx.com 
52327Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
52427Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
52527Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
52627Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
52727Svbart@nginx.com             return;
52827Svbart@nginx.com         }
52927Svbart@nginx.com 
53027Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
53127Svbart@nginx.com 
53227Svbart@nginx.com         c->read = b;
53327Svbart@nginx.com     }
53427Svbart@nginx.com 
53527Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
53627Svbart@nginx.com 
53762Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
53820Sigor@sysoev.ru }
53920Sigor@sysoev.ru 
54020Sigor@sysoev.ru 
54120Sigor@sysoev.ru static nxt_msec_t
54262Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
54320Sigor@sysoev.ru {
54420Sigor@sysoev.ru     return (nxt_msec_t) data;
54520Sigor@sysoev.ru }
54620Sigor@sysoev.ru 
54720Sigor@sysoev.ru 
54820Sigor@sysoev.ru static void
54920Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
55020Sigor@sysoev.ru {
55162Sigor@sysoev.ru     nxt_conn_t  *c;
55220Sigor@sysoev.ru 
55320Sigor@sysoev.ru     c = obj;
55420Sigor@sysoev.ru 
55520Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
55620Sigor@sysoev.ru 
55727Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
55820Sigor@sysoev.ru }
55920Sigor@sysoev.ru 
56020Sigor@sysoev.ru 
56120Sigor@sysoev.ru static void
56220Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
56320Sigor@sysoev.ru {
56462Sigor@sysoev.ru     nxt_timer_t  *timer;
56562Sigor@sysoev.ru     nxt_conn_t   *c;
56620Sigor@sysoev.ru 
56762Sigor@sysoev.ru     timer = obj;
56820Sigor@sysoev.ru 
56962Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
57020Sigor@sysoev.ru     c->socket.timedout = 1;
57120Sigor@sysoev.ru     c->socket.closed = 1;
57220Sigor@sysoev.ru 
57320Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
57420Sigor@sysoev.ru 
57527Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
57627Svbart@nginx.com }
57727Svbart@nginx.com 
57827Svbart@nginx.com 
57927Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
58027Svbart@nginx.com     nxt_aligned(64) =
58127Svbart@nginx.com {
58256Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
58356Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
58456Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
58527Svbart@nginx.com 
58656Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
58756Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
58856Sigor@sysoev.ru     .timer_data = 60 * 1000,
58956Sigor@sysoev.ru     .timer_autoreset = 1,
59027Svbart@nginx.com };
59127Svbart@nginx.com 
59227Svbart@nginx.com 
59327Svbart@nginx.com static void
59427Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
59527Svbart@nginx.com {
596107Svbart@nginx.com     size_t                    read;
597107Svbart@nginx.com     nxt_buf_t                 *b;
598107Svbart@nginx.com     nxt_conn_t                *c;
599107Svbart@nginx.com     nxt_controller_request_t  *r;
60027Svbart@nginx.com 
60127Svbart@nginx.com     c = obj;
602107Svbart@nginx.com     r = data;
60327Svbart@nginx.com     b = c->read;
60427Svbart@nginx.com 
605107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
60627Svbart@nginx.com 
607107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
608107Svbart@nginx.com               read, r->length);
60927Svbart@nginx.com 
610107Svbart@nginx.com     if (read >= r->length) {
611140Svbart@nginx.com         nxt_controller_process_request(task, r);
61227Svbart@nginx.com         return;
61327Svbart@nginx.com     }
61427Svbart@nginx.com 
61562Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
61627Svbart@nginx.com }
61727Svbart@nginx.com 
61827Svbart@nginx.com 
61927Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
62027Svbart@nginx.com     nxt_aligned(64) =
62127Svbart@nginx.com {
62256Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
62356Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
62427Svbart@nginx.com 
62556Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
62656Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
62756Sigor@sysoev.ru     .timer_data = 60 * 1000,
62856Sigor@sysoev.ru     .timer_autoreset = 1,
62927Svbart@nginx.com };
63027Svbart@nginx.com 
63127Svbart@nginx.com 
63227Svbart@nginx.com static void
63327Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
63427Svbart@nginx.com {
63562Sigor@sysoev.ru     nxt_buf_t   *b;
63662Sigor@sysoev.ru     nxt_conn_t  *c;
63727Svbart@nginx.com 
63827Svbart@nginx.com     c = obj;
63927Svbart@nginx.com 
64027Svbart@nginx.com     nxt_debug(task, "controller conn write");
64127Svbart@nginx.com 
64227Svbart@nginx.com     b = c->write;
64327Svbart@nginx.com 
64427Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
64562Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
64627Svbart@nginx.com         return;
64727Svbart@nginx.com     }
64827Svbart@nginx.com 
64927Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
65027Svbart@nginx.com 
65127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
65227Svbart@nginx.com }
65327Svbart@nginx.com 
65427Svbart@nginx.com 
65527Svbart@nginx.com static void
65627Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
65727Svbart@nginx.com {
65862Sigor@sysoev.ru     nxt_conn_t  *c;
65927Svbart@nginx.com 
66027Svbart@nginx.com     c = obj;
66127Svbart@nginx.com 
66227Svbart@nginx.com     nxt_debug(task, "controller conn write error");
66327Svbart@nginx.com 
66427Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
66527Svbart@nginx.com }
66627Svbart@nginx.com 
66727Svbart@nginx.com 
66827Svbart@nginx.com static void
66927Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
67027Svbart@nginx.com {
67162Sigor@sysoev.ru     nxt_conn_t   *c;
67262Sigor@sysoev.ru     nxt_timer_t  *timer;
67327Svbart@nginx.com 
67462Sigor@sysoev.ru     timer = obj;
67527Svbart@nginx.com 
67662Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
67727Svbart@nginx.com     c->socket.timedout = 1;
67827Svbart@nginx.com     c->socket.closed = 1;
67927Svbart@nginx.com 
68027Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
68127Svbart@nginx.com 
68227Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
68320Sigor@sysoev.ru }
68420Sigor@sysoev.ru 
68520Sigor@sysoev.ru 
68620Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
68720Sigor@sysoev.ru     nxt_aligned(64) =
68820Sigor@sysoev.ru {
68956Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
69020Sigor@sysoev.ru };
69120Sigor@sysoev.ru 
69220Sigor@sysoev.ru 
69320Sigor@sysoev.ru static void
69420Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
69520Sigor@sysoev.ru {
69662Sigor@sysoev.ru     nxt_conn_t  *c;
69720Sigor@sysoev.ru 
69820Sigor@sysoev.ru     c = obj;
69920Sigor@sysoev.ru 
70020Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
70120Sigor@sysoev.ru 
70227Svbart@nginx.com     nxt_queue_remove(&c->link);
70327Svbart@nginx.com 
70420Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
70520Sigor@sysoev.ru 
70662Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
70720Sigor@sysoev.ru }
70820Sigor@sysoev.ru 
70920Sigor@sysoev.ru 
71020Sigor@sysoev.ru static void
71120Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
71220Sigor@sysoev.ru {
71362Sigor@sysoev.ru     nxt_conn_t  *c;
71420Sigor@sysoev.ru 
71520Sigor@sysoev.ru     c = obj;
71620Sigor@sysoev.ru 
71720Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
71820Sigor@sysoev.ru 
719337Sigor@sysoev.ru     nxt_sockaddr_cache_free(task->thread->engine, c);
720337Sigor@sysoev.ru 
72165Sigor@sysoev.ru     nxt_mp_destroy(c->mem_pool);
72220Sigor@sysoev.ru 
72320Sigor@sysoev.ru     //nxt_free(c);
72420Sigor@sysoev.ru }
72527Svbart@nginx.com 
72627Svbart@nginx.com 
72727Svbart@nginx.com static nxt_int_t
72860Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
72967Svbart@nginx.com     nxt_log_t *log)
73027Svbart@nginx.com {
73127Svbart@nginx.com     off_t                     length;
73227Svbart@nginx.com     nxt_controller_request_t  *r;
73327Svbart@nginx.com 
73427Svbart@nginx.com     r = ctx;
73527Svbart@nginx.com 
73660Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
73727Svbart@nginx.com 
73827Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
739107Svbart@nginx.com 
740107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
741107Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
742107Svbart@nginx.com             return NXT_ERROR;
743107Svbart@nginx.com         }
74427Svbart@nginx.com 
74527Svbart@nginx.com         r->length = length;
74627Svbart@nginx.com         return NXT_OK;
74727Svbart@nginx.com     }
74827Svbart@nginx.com 
74960Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
75027Svbart@nginx.com 
75127Svbart@nginx.com     return NXT_ERROR;
75227Svbart@nginx.com }
75327Svbart@nginx.com 
75427Svbart@nginx.com 
75527Svbart@nginx.com static void
756140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
75727Svbart@nginx.com {
75865Sigor@sysoev.ru     nxt_mp_t                   *mp;
75951Svbart@nginx.com     nxt_int_t                  rc;
76046Svbart@nginx.com     nxt_str_t                  path;
761140Svbart@nginx.com     nxt_conn_t                 *c;
76251Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
763106Svbart@nginx.com     nxt_conf_op_t              *ops;
764106Svbart@nginx.com     nxt_conf_value_t           *value;
765*357Svbart@nginx.com     nxt_conf_validation_t      vldt;
766208Svbart@nginx.com     nxt_conf_json_error_t      error;
76744Svbart@nginx.com     nxt_controller_response_t  resp;
76844Svbart@nginx.com 
76951Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
77051Svbart@nginx.com 
771140Svbart@nginx.com     c = req->conn;
772112Smax.romanov@nginx.com     path = req->parser.path;
77351Svbart@nginx.com 
77451Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
77551Svbart@nginx.com         path.length--;
77651Svbart@nginx.com     }
77751Svbart@nginx.com 
77844Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
77944Svbart@nginx.com 
78044Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
78146Svbart@nginx.com 
782106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
78351Svbart@nginx.com 
78451Svbart@nginx.com         if (value == NULL) {
785208Svbart@nginx.com             goto not_found;
78651Svbart@nginx.com         }
78751Svbart@nginx.com 
788208Svbart@nginx.com         resp.status = 200;
789106Svbart@nginx.com         resp.conf = value;
79046Svbart@nginx.com 
791208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
792208Svbart@nginx.com         return;
79351Svbart@nginx.com     }
79451Svbart@nginx.com 
79551Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
79646Svbart@nginx.com 
797238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
798238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
799238Svbart@nginx.com             return;
800238Svbart@nginx.com         }
801238Svbart@nginx.com 
80265Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
80351Svbart@nginx.com 
80451Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
805208Svbart@nginx.com             goto alloc_fail;
80646Svbart@nginx.com         }
80746Svbart@nginx.com 
80851Svbart@nginx.com         mbuf = &c->read->mem;
80951Svbart@nginx.com 
810208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
811208Svbart@nginx.com 
812208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
81351Svbart@nginx.com 
81451Svbart@nginx.com         if (value == NULL) {
81565Sigor@sysoev.ru             nxt_mp_destroy(mp);
816208Svbart@nginx.com 
817208Svbart@nginx.com             if (error.pos == NULL) {
818208Svbart@nginx.com                 goto alloc_fail;
819208Svbart@nginx.com             }
820208Svbart@nginx.com 
821208Svbart@nginx.com             resp.status = 400;
822208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
823*357Svbart@nginx.com             resp.detail.length = nxt_strlen(error.detail);
824*357Svbart@nginx.com             resp.detail.start = error.detail;
825208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
826208Svbart@nginx.com 
827208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
828208Svbart@nginx.com                                    &resp.line, &resp.column);
829208Svbart@nginx.com 
830208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
831208Svbart@nginx.com             return;
83251Svbart@nginx.com         }
83351Svbart@nginx.com 
83451Svbart@nginx.com         if (path.length != 1) {
835106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
836106Svbart@nginx.com                                      nxt_controller_conf.root,
837106Svbart@nginx.com                                      &path, value);
83846Svbart@nginx.com 
83951Svbart@nginx.com             if (rc != NXT_OK) {
84051Svbart@nginx.com                 if (rc == NXT_DECLINED) {
841208Svbart@nginx.com                     goto not_found;
84251Svbart@nginx.com                 }
84346Svbart@nginx.com 
844208Svbart@nginx.com                 goto alloc_fail;
84551Svbart@nginx.com             }
84651Svbart@nginx.com 
847106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
84851Svbart@nginx.com 
84951Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
85065Sigor@sysoev.ru                 nxt_mp_destroy(mp);
851208Svbart@nginx.com                 goto alloc_fail;
85251Svbart@nginx.com             }
85346Svbart@nginx.com         }
85444Svbart@nginx.com 
855*357Svbart@nginx.com         nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
856*357Svbart@nginx.com 
857*357Svbart@nginx.com         vldt.conf = value;
858*357Svbart@nginx.com         vldt.pool = c->mem_pool;
859*357Svbart@nginx.com 
860*357Svbart@nginx.com         rc = nxt_conf_validate(&vldt);
861*357Svbart@nginx.com 
862*357Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
863121Svbart@nginx.com             nxt_mp_destroy(mp);
864*357Svbart@nginx.com 
865*357Svbart@nginx.com             if (rc == NXT_DECLINED) {
866*357Svbart@nginx.com                 resp.detail = vldt.error;
867*357Svbart@nginx.com                 goto invalid_conf;
868*357Svbart@nginx.com             }
869*357Svbart@nginx.com 
870*357Svbart@nginx.com             /* rc == NXT_ERROR */
871*357Svbart@nginx.com             goto alloc_fail;
872116Svbart@nginx.com         }
873116Svbart@nginx.com 
874249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
875249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
876247Svbart@nginx.com 
877247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
878121Svbart@nginx.com             nxt_mp_destroy(mp);
879247Svbart@nginx.com 
880247Svbart@nginx.com             if (rc == NXT_DECLINED) {
881247Svbart@nginx.com                 goto no_router;
882247Svbart@nginx.com             }
883247Svbart@nginx.com 
884247Svbart@nginx.com             /* rc == NXT_ERROR */
885208Svbart@nginx.com             goto alloc_fail;
886121Svbart@nginx.com         }
887121Svbart@nginx.com 
888249Svbart@nginx.com         req->conf.root = value;
889249Svbart@nginx.com         req->conf.pool = mp;
890249Svbart@nginx.com 
891249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
892249Svbart@nginx.com 
893140Svbart@nginx.com         return;
89451Svbart@nginx.com     }
89527Svbart@nginx.com 
89651Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
89751Svbart@nginx.com 
898238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
899238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
900238Svbart@nginx.com             return;
901238Svbart@nginx.com         }
902238Svbart@nginx.com 
90351Svbart@nginx.com         if (path.length == 1) {
90465Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
90544Svbart@nginx.com 
90651Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
907208Svbart@nginx.com                 goto alloc_fail;
90851Svbart@nginx.com             }
90951Svbart@nginx.com 
910106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
91127Svbart@nginx.com 
91244Svbart@nginx.com         } else {
913106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
914106Svbart@nginx.com                                      nxt_controller_conf.root,
915106Svbart@nginx.com                                      &path, NULL);
91651Svbart@nginx.com 
91751Svbart@nginx.com             if (rc != NXT_OK) {
91851Svbart@nginx.com                 if (rc == NXT_DECLINED) {
919208Svbart@nginx.com                     goto not_found;
92051Svbart@nginx.com                 }
92151Svbart@nginx.com 
922208Svbart@nginx.com                 goto alloc_fail;
92351Svbart@nginx.com             }
92451Svbart@nginx.com 
92565Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
92651Svbart@nginx.com 
92751Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
928208Svbart@nginx.com                 goto alloc_fail;
92951Svbart@nginx.com             }
93051Svbart@nginx.com 
931106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
93251Svbart@nginx.com         }
93351Svbart@nginx.com 
93451Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
93565Sigor@sysoev.ru             nxt_mp_destroy(mp);
936208Svbart@nginx.com             goto alloc_fail;
93744Svbart@nginx.com         }
93844Svbart@nginx.com 
939*357Svbart@nginx.com         nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
940*357Svbart@nginx.com 
941*357Svbart@nginx.com         vldt.conf = value;
942*357Svbart@nginx.com         vldt.pool = c->mem_pool;
943*357Svbart@nginx.com 
944*357Svbart@nginx.com         rc = nxt_conf_validate(&vldt);
945*357Svbart@nginx.com 
946*357Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
947121Svbart@nginx.com             nxt_mp_destroy(mp);
948*357Svbart@nginx.com 
949*357Svbart@nginx.com             if (rc == NXT_DECLINED) {
950*357Svbart@nginx.com                 resp.detail = vldt.error;
951*357Svbart@nginx.com                 goto invalid_conf;
952*357Svbart@nginx.com             }
953*357Svbart@nginx.com 
954*357Svbart@nginx.com             /* rc == NXT_ERROR */
955*357Svbart@nginx.com             goto alloc_fail;
956116Svbart@nginx.com         }
957116Svbart@nginx.com 
958249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
959249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
960247Svbart@nginx.com 
961247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
962121Svbart@nginx.com             nxt_mp_destroy(mp);
963247Svbart@nginx.com 
964247Svbart@nginx.com             if (rc == NXT_DECLINED) {
965247Svbart@nginx.com                 goto no_router;
966247Svbart@nginx.com             }
967247Svbart@nginx.com 
968247Svbart@nginx.com             /* rc == NXT_ERROR */
969208Svbart@nginx.com             goto alloc_fail;
970121Svbart@nginx.com         }
971121Svbart@nginx.com 
972249Svbart@nginx.com         req->conf.root = value;
973249Svbart@nginx.com         req->conf.pool = mp;
974249Svbart@nginx.com 
975249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
976249Svbart@nginx.com 
977140Svbart@nginx.com         return;
97851Svbart@nginx.com     }
97951Svbart@nginx.com 
980208Svbart@nginx.com     resp.status = 405;
981208Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
982208Svbart@nginx.com     resp.offset = -1;
98351Svbart@nginx.com 
984208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
985208Svbart@nginx.com     return;
98651Svbart@nginx.com 
987208Svbart@nginx.com not_found:
988208Svbart@nginx.com 
989208Svbart@nginx.com     resp.status = 404;
990208Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
991208Svbart@nginx.com     resp.offset = -1;
992208Svbart@nginx.com 
993208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
994208Svbart@nginx.com     return;
995208Svbart@nginx.com 
996208Svbart@nginx.com invalid_conf:
997208Svbart@nginx.com 
998208Svbart@nginx.com     resp.status = 400;
999208Svbart@nginx.com     resp.title = (u_char *) "Invalid configuration.";
1000208Svbart@nginx.com     resp.offset = -1;
1001208Svbart@nginx.com 
1002208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1003208Svbart@nginx.com     return;
1004247Svbart@nginx.com 
1005247Svbart@nginx.com alloc_fail:
1006247Svbart@nginx.com 
1007247Svbart@nginx.com     resp.status = 500;
1008247Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
1009247Svbart@nginx.com     resp.offset = -1;
1010247Svbart@nginx.com 
1011247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1012247Svbart@nginx.com     return;
1013247Svbart@nginx.com 
1014247Svbart@nginx.com no_router:
1015247Svbart@nginx.com 
1016247Svbart@nginx.com     resp.status = 500;
1017247Svbart@nginx.com     resp.title = (u_char *) "Router process isn't available.";
1018247Svbart@nginx.com     resp.offset = -1;
1019247Svbart@nginx.com 
1020247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
1021247Svbart@nginx.com     return;
102227Svbart@nginx.com }
102327Svbart@nginx.com 
102427Svbart@nginx.com 
1025193Smax.romanov@nginx.com static void
1026193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
1027193Smax.romanov@nginx.com     void *data)
1028140Svbart@nginx.com {
1029238Svbart@nginx.com     nxt_queue_t                queue;
1030140Svbart@nginx.com     nxt_controller_request_t   *req;
1031140Svbart@nginx.com     nxt_controller_response_t  resp;
1032140Svbart@nginx.com 
1033238Svbart@nginx.com     req = data;
1034238Svbart@nginx.com 
1035201Svbart@nginx.com     nxt_debug(task, "controller conf ready: %*s",
1036201Svbart@nginx.com               nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos);
1037140Svbart@nginx.com 
1038238Svbart@nginx.com     nxt_queue_remove(&req->link);
1039140Svbart@nginx.com 
1040238Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
1041