xref: /unit/src/nxt_controller.c (revision 1469)
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>
12774Svbart@nginx.com #include <nxt_cert.h>
1320Sigor@sysoev.ru 
1420Sigor@sysoev.ru 
1527Svbart@nginx.com typedef struct {
16106Svbart@nginx.com     nxt_conf_value_t  *root;
17106Svbart@nginx.com     nxt_mp_t          *pool;
1844Svbart@nginx.com } nxt_controller_conf_t;
1944Svbart@nginx.com 
2044Svbart@nginx.com 
2144Svbart@nginx.com typedef struct {
2227Svbart@nginx.com     nxt_http_request_parse_t  parser;
2327Svbart@nginx.com     size_t                    length;
2444Svbart@nginx.com     nxt_controller_conf_t     conf;
25140Svbart@nginx.com     nxt_conn_t                *conn;
26140Svbart@nginx.com     nxt_queue_link_t          link;
2727Svbart@nginx.com } nxt_controller_request_t;
2827Svbart@nginx.com 
2927Svbart@nginx.com 
3044Svbart@nginx.com typedef struct {
31208Svbart@nginx.com     nxt_uint_t        status;
32106Svbart@nginx.com     nxt_conf_value_t  *conf;
33208Svbart@nginx.com 
34208Svbart@nginx.com     u_char            *title;
35357Svbart@nginx.com     nxt_str_t         detail;
36208Svbart@nginx.com     ssize_t           offset;
37208Svbart@nginx.com     nxt_uint_t        line;
38208Svbart@nginx.com     nxt_uint_t        column;
3944Svbart@nginx.com } nxt_controller_response_t;
4044Svbart@nginx.com 
4144Svbart@nginx.com 
42248Svbart@nginx.com static void nxt_controller_process_new_port_handler(nxt_task_t *task,
43248Svbart@nginx.com     nxt_port_recv_msg_t *msg);
44662Smax.romanov@nginx.com static void nxt_controller_send_current_conf(nxt_task_t *task);
45662Smax.romanov@nginx.com static void nxt_controller_router_ready_handler(nxt_task_t *task,
46662Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg);
47249Svbart@nginx.com static nxt_int_t nxt_controller_conf_default(void);
48249Svbart@nginx.com static void nxt_controller_conf_init_handler(nxt_task_t *task,
49249Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
50*1469Smax.romanov@nginx.com static void nxt_controller_flush_requests(nxt_task_t *task);
51249Svbart@nginx.com static nxt_int_t nxt_controller_conf_send(nxt_task_t *task,
52249Svbart@nginx.com     nxt_conf_value_t *conf, nxt_port_rpc_handler_t handler, void *data);
53248Svbart@nginx.com 
5420Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
5520Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
5662Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
5720Sigor@sysoev.ru     uintptr_t data);
5820Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
5920Sigor@sysoev.ru     void *data);
6020Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
6120Sigor@sysoev.ru     void *data);
6227Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
6327Svbart@nginx.com     void *data);
6427Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
6527Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
6627Svbart@nginx.com     void *data);
6727Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
6827Svbart@nginx.com     void *data);
6920Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
7020Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
7120Sigor@sysoev.ru 
7227Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
73417Svbart@nginx.com     nxt_http_field_t *field, uintptr_t data);
7427Svbart@nginx.com 
7527Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
76140Svbart@nginx.com     nxt_controller_request_t *req);
77774Svbart@nginx.com static void nxt_controller_process_config(nxt_task_t *task,
78774Svbart@nginx.com     nxt_controller_request_t *req, nxt_str_t *path);
79774Svbart@nginx.com #if (NXT_TLS)
80774Svbart@nginx.com static void nxt_controller_process_cert(nxt_task_t *task,
81774Svbart@nginx.com     nxt_controller_request_t *req, nxt_str_t *path);
82774Svbart@nginx.com static void nxt_controller_process_cert_save(nxt_task_t *task,
83774Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
84774Svbart@nginx.com static nxt_bool_t nxt_controller_cert_in_use(nxt_str_t *name);
85774Svbart@nginx.com #endif
86238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task,
87238Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
88314Svbart@nginx.com static void nxt_controller_conf_store(nxt_task_t *task,
89314Svbart@nginx.com     nxt_conf_value_t *conf);
90140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
91140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
92208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
93208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
9427Svbart@nginx.com 
9527Svbart@nginx.com 
96417Svbart@nginx.com static nxt_http_field_proc_t  nxt_controller_request_fields[] = {
9727Svbart@nginx.com     { nxt_string("Content-Length"),
9827Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
9927Svbart@nginx.com };
10027Svbart@nginx.com 
101417Svbart@nginx.com static nxt_lvlhsh_t            nxt_controller_fields_hash;
10227Svbart@nginx.com 
103314Svbart@nginx.com static nxt_uint_t              nxt_controller_listening;
104662Smax.romanov@nginx.com static nxt_uint_t              nxt_controller_router_ready;
105238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
106238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
107*1469Smax.romanov@nginx.com static nxt_bool_t              nxt_controller_waiting_init_conf;
10827Svbart@nginx.com 
10920Sigor@sysoev.ru 
11020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
11127Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
11227Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
11320Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
11420Sigor@sysoev.ru 
11520Sigor@sysoev.ru 
116320Smax.romanov@nginx.com nxt_port_handlers_t  nxt_controller_process_port_handlers = {
117662Smax.romanov@nginx.com     .quit           = nxt_worker_process_quit_handler,
118662Smax.romanov@nginx.com     .new_port       = nxt_controller_process_new_port_handler,
119662Smax.romanov@nginx.com     .change_file    = nxt_port_change_log_file_handler,
120662Smax.romanov@nginx.com     .mmap           = nxt_port_mmap_handler,
121662Smax.romanov@nginx.com     .process_ready  = nxt_controller_router_ready_handler,
122662Smax.romanov@nginx.com     .data           = nxt_port_data_handler,
123662Smax.romanov@nginx.com     .remove_pid     = nxt_port_remove_pid_handler,
124662Smax.romanov@nginx.com     .rpc_ready      = nxt_port_rpc_handler,
125662Smax.romanov@nginx.com     .rpc_error      = nxt_port_rpc_handler,
126248Svbart@nginx.com };
127248Svbart@nginx.com 
128248Svbart@nginx.com 
12920Sigor@sysoev.ru nxt_int_t
130141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
13120Sigor@sysoev.ru {
132417Svbart@nginx.com     nxt_mp_t               *mp;
133417Svbart@nginx.com     nxt_int_t              ret;
134417Svbart@nginx.com     nxt_str_t              *json;
135417Svbart@nginx.com     nxt_conf_value_t       *conf;
136417Svbart@nginx.com     nxt_conf_validation_t  vldt;
137774Svbart@nginx.com     nxt_controller_init_t  *init;
13827Svbart@nginx.com 
1391459Smax.romanov@nginx.com     ret = nxt_http_fields_hash(&nxt_controller_fields_hash,
140417Svbart@nginx.com                                nxt_controller_request_fields,
141417Svbart@nginx.com                                nxt_nitems(nxt_controller_request_fields));
142417Svbart@nginx.com 
143417Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
14427Svbart@nginx.com         return NXT_ERROR;
14527Svbart@nginx.com     }
14627Svbart@nginx.com 
147248Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
14827Svbart@nginx.com 
149774Svbart@nginx.com     init = data;
150774Svbart@nginx.com 
151774Svbart@nginx.com #if (NXT_TLS)
152314Svbart@nginx.com 
153774Svbart@nginx.com     if (init->certs != NULL) {
154774Svbart@nginx.com         nxt_cert_info_init(task, init->certs);
155774Svbart@nginx.com         nxt_cert_store_release(init->certs);
156774Svbart@nginx.com     }
157774Svbart@nginx.com 
158774Svbart@nginx.com #endif
159774Svbart@nginx.com 
160774Svbart@nginx.com     json = &init->conf;
161774Svbart@nginx.com 
162774Svbart@nginx.com     if (json->start == NULL) {
163314Svbart@nginx.com         return NXT_OK;
164314Svbart@nginx.com     }
165314Svbart@nginx.com 
166314Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
167314Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
168314Svbart@nginx.com         return NXT_ERROR;
169314Svbart@nginx.com     }
170314Svbart@nginx.com 
171314Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, json);
172314Svbart@nginx.com     nxt_free(json->start);
173314Svbart@nginx.com 
174314Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
175564Svbart@nginx.com         nxt_alert(task, "failed to restore previous configuration: "
176564Svbart@nginx.com                   "file is corrupted or not enough memory");
177314Svbart@nginx.com 
178314Svbart@nginx.com         nxt_mp_destroy(mp);
179314Svbart@nginx.com         return NXT_OK;
180314Svbart@nginx.com     }
181314Svbart@nginx.com 
182357Svbart@nginx.com     nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
183314Svbart@nginx.com 
184357Svbart@nginx.com     vldt.pool = nxt_mp_create(1024, 128, 256, 32);
185357Svbart@nginx.com     if (nxt_slow_path(vldt.pool == NULL)) {
1861013Smax.romanov@nginx.com         nxt_mp_destroy(mp);
187357Svbart@nginx.com         return NXT_ERROR;
188314Svbart@nginx.com     }
189314Svbart@nginx.com 
190357Svbart@nginx.com     vldt.conf = conf;
191357Svbart@nginx.com 
192357Svbart@nginx.com     ret = nxt_conf_validate(&vldt);
193357Svbart@nginx.com 
194357Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
195357Svbart@nginx.com 
196357Svbart@nginx.com         if (ret == NXT_DECLINED) {
197564Svbart@nginx.com             nxt_alert(task, "the previous configuration is invalid: %V",
198564Svbart@nginx.com                       &vldt.error);
199357Svbart@nginx.com 
200357Svbart@nginx.com             nxt_mp_destroy(vldt.pool);
201357Svbart@nginx.com             nxt_mp_destroy(mp);
202357Svbart@nginx.com 
203357Svbart@nginx.com             return NXT_OK;
204357Svbart@nginx.com         }
205357Svbart@nginx.com 
206357Svbart@nginx.com         /* ret == NXT_ERROR */
207357Svbart@nginx.com 
208357Svbart@nginx.com         return NXT_ERROR;
209357Svbart@nginx.com     }
210357Svbart@nginx.com 
211357Svbart@nginx.com     nxt_mp_destroy(vldt.pool);
212314Svbart@nginx.com 
213314Svbart@nginx.com     nxt_controller_conf.root = conf;
214314Svbart@nginx.com     nxt_controller_conf.pool = mp;
215314Svbart@nginx.com 
216248Svbart@nginx.com     return NXT_OK;
217248Svbart@nginx.com }
218248Svbart@nginx.com 
219248Svbart@nginx.com 
220248Svbart@nginx.com static void
221248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task,
222248Svbart@nginx.com     nxt_port_recv_msg_t *msg)
223248Svbart@nginx.com {
224662Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
225662Smax.romanov@nginx.com 
226662Smax.romanov@nginx.com     if (msg->u.new_port->type != NXT_PROCESS_ROUTER
227662Smax.romanov@nginx.com         || !nxt_controller_router_ready)
228662Smax.romanov@nginx.com     {
229662Smax.romanov@nginx.com         return;
230662Smax.romanov@nginx.com     }
231662Smax.romanov@nginx.com 
232662Smax.romanov@nginx.com     nxt_controller_send_current_conf(task);
233662Smax.romanov@nginx.com }
234662Smax.romanov@nginx.com 
235662Smax.romanov@nginx.com 
236662Smax.romanov@nginx.com static void
237662Smax.romanov@nginx.com nxt_controller_send_current_conf(nxt_task_t *task)
238662Smax.romanov@nginx.com {
239249Svbart@nginx.com     nxt_int_t         rc;
240248Svbart@nginx.com     nxt_runtime_t     *rt;
241248Svbart@nginx.com     nxt_conf_value_t  *conf;
242248Svbart@nginx.com 
243249Svbart@nginx.com     conf = nxt_controller_conf.root;
244249Svbart@nginx.com 
245249Svbart@nginx.com     if (conf != NULL) {
246249Svbart@nginx.com         rc = nxt_controller_conf_send(task, conf,
247249Svbart@nginx.com                                       nxt_controller_conf_init_handler, NULL);
24844Svbart@nginx.com 
249249Svbart@nginx.com         if (nxt_fast_path(rc == NXT_OK)) {
250*1469Smax.romanov@nginx.com             nxt_controller_waiting_init_conf = 1;
251*1469Smax.romanov@nginx.com 
252249Svbart@nginx.com             return;
253249Svbart@nginx.com         }
254249Svbart@nginx.com 
255249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
256249Svbart@nginx.com 
257249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
258249Svbart@nginx.com             nxt_abort();
259249Svbart@nginx.com         }
26044Svbart@nginx.com     }
26144Svbart@nginx.com 
262249Svbart@nginx.com     if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
263248Svbart@nginx.com         nxt_abort();
26444Svbart@nginx.com     }
26544Svbart@nginx.com 
266248Svbart@nginx.com     rt = task->thread->runtime;
267140Svbart@nginx.com 
268248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
269248Svbart@nginx.com         nxt_abort();
270248Svbart@nginx.com     }
271314Svbart@nginx.com 
272314Svbart@nginx.com     nxt_controller_listening = 1;
27320Sigor@sysoev.ru }
27420Sigor@sysoev.ru 
27520Sigor@sysoev.ru 
276662Smax.romanov@nginx.com static void
277662Smax.romanov@nginx.com nxt_controller_router_ready_handler(nxt_task_t *task,
278662Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg)
279662Smax.romanov@nginx.com {
280662Smax.romanov@nginx.com     nxt_port_t     *router_port;
281662Smax.romanov@nginx.com     nxt_runtime_t  *rt;
282662Smax.romanov@nginx.com 
283662Smax.romanov@nginx.com     rt = task->thread->runtime;
284662Smax.romanov@nginx.com 
285662Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
286662Smax.romanov@nginx.com 
287662Smax.romanov@nginx.com     nxt_controller_router_ready = 1;
288662Smax.romanov@nginx.com 
289662Smax.romanov@nginx.com     if (router_port != NULL) {
290662Smax.romanov@nginx.com         nxt_controller_send_current_conf(task);
291662Smax.romanov@nginx.com     }
292662Smax.romanov@nginx.com }
293662Smax.romanov@nginx.com 
294662Smax.romanov@nginx.com 
295249Svbart@nginx.com static nxt_int_t
296249Svbart@nginx.com nxt_controller_conf_default(void)
297249Svbart@nginx.com {
298249Svbart@nginx.com     nxt_mp_t          *mp;
299249Svbart@nginx.com     nxt_conf_value_t  *conf;
300249Svbart@nginx.com 
301249Svbart@nginx.com     static const nxt_str_t json
302249Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
303249Svbart@nginx.com 
304249Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
305249Svbart@nginx.com 
306249Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
307249Svbart@nginx.com         return NXT_ERROR;
308249Svbart@nginx.com     }
309249Svbart@nginx.com 
310249Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
311249Svbart@nginx.com 
312249Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
313249Svbart@nginx.com         return NXT_ERROR;
314249Svbart@nginx.com     }
315249Svbart@nginx.com 
316249Svbart@nginx.com     nxt_controller_conf.root = conf;
317249Svbart@nginx.com     nxt_controller_conf.pool = mp;
318249Svbart@nginx.com 
319249Svbart@nginx.com     return NXT_OK;
320249Svbart@nginx.com }
321249Svbart@nginx.com 
322249Svbart@nginx.com 
323249Svbart@nginx.com static void
324249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
325249Svbart@nginx.com     void *data)
326249Svbart@nginx.com {
327314Svbart@nginx.com     nxt_runtime_t  *rt;
328314Svbart@nginx.com 
329*1469Smax.romanov@nginx.com     nxt_controller_waiting_init_conf = 0;
330*1469Smax.romanov@nginx.com 
331249Svbart@nginx.com     if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) {
332564Svbart@nginx.com         nxt_alert(task, "failed to apply previous configuration");
333314Svbart@nginx.com 
334249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
335249Svbart@nginx.com 
336249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
337249Svbart@nginx.com             nxt_abort();
338249Svbart@nginx.com         }
339249Svbart@nginx.com     }
340314Svbart@nginx.com 
341314Svbart@nginx.com     if (nxt_controller_listening == 0) {
342314Svbart@nginx.com         rt = task->thread->runtime;
343314Svbart@nginx.com 
344314Svbart@nginx.com         if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket)
345314Svbart@nginx.com                           == NULL))
346314Svbart@nginx.com         {
347314Svbart@nginx.com             nxt_abort();
348314Svbart@nginx.com         }
349314Svbart@nginx.com 
350314Svbart@nginx.com         nxt_controller_listening = 1;
351314Svbart@nginx.com     }
352*1469Smax.romanov@nginx.com 
353*1469Smax.romanov@nginx.com     nxt_controller_flush_requests(task);
354*1469Smax.romanov@nginx.com }
355*1469Smax.romanov@nginx.com 
356*1469Smax.romanov@nginx.com 
357*1469Smax.romanov@nginx.com static void
358*1469Smax.romanov@nginx.com nxt_controller_flush_requests(nxt_task_t *task)
359*1469Smax.romanov@nginx.com {
360*1469Smax.romanov@nginx.com     nxt_queue_t               queue;
361*1469Smax.romanov@nginx.com     nxt_controller_request_t  *req;
362*1469Smax.romanov@nginx.com 
363*1469Smax.romanov@nginx.com     nxt_queue_init(&queue);
364*1469Smax.romanov@nginx.com     nxt_queue_add(&queue, &nxt_controller_waiting_requests);
365*1469Smax.romanov@nginx.com 
366*1469Smax.romanov@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
367*1469Smax.romanov@nginx.com 
368*1469Smax.romanov@nginx.com     nxt_queue_each(req, &queue, nxt_controller_request_t, link) {
369*1469Smax.romanov@nginx.com         nxt_controller_process_request(task, req);
370*1469Smax.romanov@nginx.com     } nxt_queue_loop;
371249Svbart@nginx.com }
372249Svbart@nginx.com 
373249Svbart@nginx.com 
374249Svbart@nginx.com static nxt_int_t
375249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf,
376249Svbart@nginx.com     nxt_port_rpc_handler_t handler, void *data)
377249Svbart@nginx.com {
378249Svbart@nginx.com     size_t         size;
379249Svbart@nginx.com     uint32_t       stream;
380249Svbart@nginx.com     nxt_int_t      rc;
381249Svbart@nginx.com     nxt_buf_t      *b;
382249Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
383249Svbart@nginx.com     nxt_runtime_t  *rt;
384249Svbart@nginx.com 
385249Svbart@nginx.com     rt = task->thread->runtime;
386249Svbart@nginx.com 
387249Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
388249Svbart@nginx.com 
389662Smax.romanov@nginx.com     if (nxt_slow_path(router_port == NULL || !nxt_controller_router_ready)) {
390249Svbart@nginx.com         return NXT_DECLINED;
391249Svbart@nginx.com     }
392249Svbart@nginx.com 
393249Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
394249Svbart@nginx.com 
395249Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
396249Svbart@nginx.com 
397249Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
398379Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
399379Smax.romanov@nginx.com         return NXT_ERROR;
400379Smax.romanov@nginx.com     }
401249Svbart@nginx.com 
402249Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
403249Svbart@nginx.com 
404249Svbart@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
405249Svbart@nginx.com                                            handler, handler,
406249Svbart@nginx.com                                            router_port->pid, data);
407249Svbart@nginx.com 
408645Svbart@nginx.com     if (nxt_slow_path(stream == 0)) {
409645Svbart@nginx.com         return NXT_ERROR;
410645Svbart@nginx.com     }
411645Svbart@nginx.com 
412249Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
413249Svbart@nginx.com                                stream, controller_port->id, b);
414249Svbart@nginx.com 
415249Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
416249Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
417249Svbart@nginx.com         return NXT_ERROR;
418249Svbart@nginx.com     }
419249Svbart@nginx.com 
420249Svbart@nginx.com     return NXT_OK;
421249Svbart@nginx.com }
422249Svbart@nginx.com 
423249Svbart@nginx.com 
42420Sigor@sysoev.ru nxt_int_t
42520Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
42620Sigor@sysoev.ru {
42720Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
42820Sigor@sysoev.ru 
42965Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
43020Sigor@sysoev.ru     if (ls == NULL) {
43120Sigor@sysoev.ru         return NXT_ERROR;
43220Sigor@sysoev.ru     }
43320Sigor@sysoev.ru 
4341448Svbart@nginx.com     ls->sockaddr = rt->controller_listen;
43520Sigor@sysoev.ru 
436359Sigor@sysoev.ru     nxt_listen_socket_remote_size(ls);
43720Sigor@sysoev.ru 
43820Sigor@sysoev.ru     ls->socket = -1;
43920Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
44020Sigor@sysoev.ru     ls->read_after_accept = 1;
44120Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
44220Sigor@sysoev.ru 
44320Sigor@sysoev.ru #if 0
44420Sigor@sysoev.ru     /* STUB */
44565Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
44620Sigor@sysoev.ru     if (wq == NULL) {
44720Sigor@sysoev.ru         return NXT_ERROR;
44820Sigor@sysoev.ru     }
44920Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
45020Sigor@sysoev.ru     /**/
45120Sigor@sysoev.ru 
45220Sigor@sysoev.ru     ls->work_queue = wq;
45320Sigor@sysoev.ru #endif
45420Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
45520Sigor@sysoev.ru 
4561451Svbart@nginx.com     if (nxt_listen_socket_create(task, rt->mem_pool, ls) != NXT_OK) {
45720Sigor@sysoev.ru         return NXT_ERROR;
45820Sigor@sysoev.ru     }
45920Sigor@sysoev.ru 
46020Sigor@sysoev.ru     rt->controller_socket = ls;
46120Sigor@sysoev.ru 
46220Sigor@sysoev.ru     return NXT_OK;
46320Sigor@sysoev.ru }
46420Sigor@sysoev.ru 
46520Sigor@sysoev.ru 
46620Sigor@sysoev.ru static void
46720Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
46820Sigor@sysoev.ru {
46927Svbart@nginx.com     nxt_buf_t                 *b;
47062Sigor@sysoev.ru     nxt_conn_t                *c;
47127Svbart@nginx.com     nxt_event_engine_t        *engine;
47227Svbart@nginx.com     nxt_controller_request_t  *r;
47320Sigor@sysoev.ru 
47420Sigor@sysoev.ru     c = obj;
47520Sigor@sysoev.ru 
47620Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
47720Sigor@sysoev.ru 
47865Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
47927Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
48027Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
48127Svbart@nginx.com         return;
48227Svbart@nginx.com     }
48327Svbart@nginx.com 
484140Svbart@nginx.com     r->conn = c;
485140Svbart@nginx.com 
48660Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
48760Svbart@nginx.com                       != NXT_OK))
48860Svbart@nginx.com     {
48960Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
49060Svbart@nginx.com         return;
49160Svbart@nginx.com     }
49227Svbart@nginx.com 
4931167Svbart@nginx.com     r->parser.encoded_slashes = 1;
4941167Svbart@nginx.com 
49520Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
49620Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
49720Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
49820Sigor@sysoev.ru         return;
49920Sigor@sysoev.ru     }
50020Sigor@sysoev.ru 
50120Sigor@sysoev.ru     c->read = b;
50227Svbart@nginx.com     c->socket.data = r;
50320Sigor@sysoev.ru     c->socket.read_ready = 1;
50420Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
50520Sigor@sysoev.ru 
50620Sigor@sysoev.ru     engine = task->thread->engine;
50720Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
50827Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
50920Sigor@sysoev.ru 
51062Sigor@sysoev.ru     nxt_conn_read(engine, c);
51120Sigor@sysoev.ru }
51220Sigor@sysoev.ru 
51320Sigor@sysoev.ru 
51420Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
51520Sigor@sysoev.ru     nxt_aligned(64) =
51620Sigor@sysoev.ru {
51756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
51856Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
51956Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
52020Sigor@sysoev.ru 
52156Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
52256Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
52356Sigor@sysoev.ru     .timer_data = 60 * 1000,
52420Sigor@sysoev.ru };
52520Sigor@sysoev.ru 
52620Sigor@sysoev.ru 
52720Sigor@sysoev.ru static void
52820Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
52920Sigor@sysoev.ru {
53027Svbart@nginx.com     size_t                    preread;
53127Svbart@nginx.com     nxt_buf_t                 *b;
53227Svbart@nginx.com     nxt_int_t                 rc;
53362Sigor@sysoev.ru     nxt_conn_t                *c;
53427Svbart@nginx.com     nxt_controller_request_t  *r;
53520Sigor@sysoev.ru 
53620Sigor@sysoev.ru     c = obj;
53727Svbart@nginx.com     r = data;
53820Sigor@sysoev.ru 
53920Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
54020Sigor@sysoev.ru 
54127Svbart@nginx.com     nxt_queue_remove(&c->link);
54227Svbart@nginx.com     nxt_queue_self(&c->link);
54327Svbart@nginx.com 
54427Svbart@nginx.com     b = c->read;
54527Svbart@nginx.com 
54627Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
54727Svbart@nginx.com 
54827Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
54927Svbart@nginx.com 
55027Svbart@nginx.com         if (rc == NXT_AGAIN) {
55127Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
55227Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
55327Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
55427Svbart@nginx.com                 return;
55527Svbart@nginx.com             }
55627Svbart@nginx.com 
55762Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
55827Svbart@nginx.com             return;
55927Svbart@nginx.com         }
56027Svbart@nginx.com 
56127Svbart@nginx.com         /* rc == NXT_ERROR */
56227Svbart@nginx.com 
56327Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
56427Svbart@nginx.com 
56527Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
56627Svbart@nginx.com         return;
56727Svbart@nginx.com     }
56827Svbart@nginx.com 
569417Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash,
570417Svbart@nginx.com                                  r);
57160Svbart@nginx.com 
57260Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
57360Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
57460Svbart@nginx.com         return;
57560Svbart@nginx.com     }
57660Svbart@nginx.com 
57727Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
57827Svbart@nginx.com 
57927Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
580107Svbart@nginx.com                     "body length: %uz, preread: %uz",
58127Svbart@nginx.com                     r->length, preread);
58227Svbart@nginx.com 
58327Svbart@nginx.com     if (preread >= r->length) {
584140Svbart@nginx.com         nxt_controller_process_request(task, r);
58527Svbart@nginx.com         return;
58627Svbart@nginx.com     }
58727Svbart@nginx.com 
58827Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
58927Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
59027Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
59127Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
59227Svbart@nginx.com             return;
59327Svbart@nginx.com         }
59427Svbart@nginx.com 
59527Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
59627Svbart@nginx.com 
59727Svbart@nginx.com         c->read = b;
59827Svbart@nginx.com     }
59927Svbart@nginx.com 
60027Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
60127Svbart@nginx.com 
60262Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
60320Sigor@sysoev.ru }
60420Sigor@sysoev.ru 
60520Sigor@sysoev.ru 
60620Sigor@sysoev.ru static nxt_msec_t
60762Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
60820Sigor@sysoev.ru {
60920Sigor@sysoev.ru     return (nxt_msec_t) data;
61020Sigor@sysoev.ru }
61120Sigor@sysoev.ru 
61220Sigor@sysoev.ru 
61320Sigor@sysoev.ru static void
61420Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
61520Sigor@sysoev.ru {
61662Sigor@sysoev.ru     nxt_conn_t  *c;
61720Sigor@sysoev.ru 
61820Sigor@sysoev.ru     c = obj;
61920Sigor@sysoev.ru 
62020Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
62120Sigor@sysoev.ru 
62227Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
62320Sigor@sysoev.ru }
62420Sigor@sysoev.ru 
62520Sigor@sysoev.ru 
62620Sigor@sysoev.ru static void
62720Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
62820Sigor@sysoev.ru {
62962Sigor@sysoev.ru     nxt_timer_t  *timer;
63062Sigor@sysoev.ru     nxt_conn_t   *c;
63120Sigor@sysoev.ru 
63262Sigor@sysoev.ru     timer = obj;
63320Sigor@sysoev.ru 
63462Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
63520Sigor@sysoev.ru     c->socket.timedout = 1;
63620Sigor@sysoev.ru     c->socket.closed = 1;
63720Sigor@sysoev.ru 
63820Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
63920Sigor@sysoev.ru 
64027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
64127Svbart@nginx.com }
64227Svbart@nginx.com 
64327Svbart@nginx.com 
64427Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
64527Svbart@nginx.com     nxt_aligned(64) =
64627Svbart@nginx.com {
64756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
64856Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
64956Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
65027Svbart@nginx.com 
65156Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
65256Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
65356Sigor@sysoev.ru     .timer_data = 60 * 1000,
65456Sigor@sysoev.ru     .timer_autoreset = 1,
65527Svbart@nginx.com };
65627Svbart@nginx.com 
65727Svbart@nginx.com 
65827Svbart@nginx.com static void
65927Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
66027Svbart@nginx.com {
661107Svbart@nginx.com     size_t                    read;
662107Svbart@nginx.com     nxt_buf_t                 *b;
663107Svbart@nginx.com     nxt_conn_t                *c;
664107Svbart@nginx.com     nxt_controller_request_t  *r;
66527Svbart@nginx.com 
66627Svbart@nginx.com     c = obj;
667107Svbart@nginx.com     r = data;
66827Svbart@nginx.com     b = c->read;
66927Svbart@nginx.com 
670107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
67127Svbart@nginx.com 
672107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
673107Svbart@nginx.com               read, r->length);
67427Svbart@nginx.com 
675107Svbart@nginx.com     if (read >= r->length) {
676140Svbart@nginx.com         nxt_controller_process_request(task, r);
67727Svbart@nginx.com         return;
67827Svbart@nginx.com     }
67927Svbart@nginx.com 
68062Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
68127Svbart@nginx.com }
68227Svbart@nginx.com 
68327Svbart@nginx.com 
68427Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
68527Svbart@nginx.com     nxt_aligned(64) =
68627Svbart@nginx.com {
68756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
68856Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
68927Svbart@nginx.com 
69056Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
69156Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
69256Sigor@sysoev.ru     .timer_data = 60 * 1000,
69356Sigor@sysoev.ru     .timer_autoreset = 1,
69427Svbart@nginx.com };
69527Svbart@nginx.com 
69627Svbart@nginx.com 
69727Svbart@nginx.com static void
69827Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
69927Svbart@nginx.com {
70062Sigor@sysoev.ru     nxt_buf_t   *b;
70162Sigor@sysoev.ru     nxt_conn_t  *c;
70227Svbart@nginx.com 
70327Svbart@nginx.com     c = obj;
70427Svbart@nginx.com 
70527Svbart@nginx.com     nxt_debug(task, "controller conn write");
70627Svbart@nginx.com 
70727Svbart@nginx.com     b = c->write;
70827Svbart@nginx.com 
70927Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
71062Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
71127Svbart@nginx.com         return;
71227Svbart@nginx.com     }
71327Svbart@nginx.com 
71427Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
71527Svbart@nginx.com 
71627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
71727Svbart@nginx.com }
71827Svbart@nginx.com 
71927Svbart@nginx.com 
72027Svbart@nginx.com static void
72127Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
72227Svbart@nginx.com {
72362Sigor@sysoev.ru     nxt_conn_t  *c;
72427Svbart@nginx.com 
72527Svbart@nginx.com     c = obj;
72627Svbart@nginx.com 
72727Svbart@nginx.com     nxt_debug(task, "controller conn write error");
72827Svbart@nginx.com 
72927Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
73027Svbart@nginx.com }
73127Svbart@nginx.com 
73227Svbart@nginx.com 
73327Svbart@nginx.com static void
73427Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
73527Svbart@nginx.com {
73662Sigor@sysoev.ru     nxt_conn_t   *c;
73762Sigor@sysoev.ru     nxt_timer_t  *timer;
73827Svbart@nginx.com 
73962Sigor@sysoev.ru     timer = obj;
74027Svbart@nginx.com 
74162Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
74227Svbart@nginx.com     c->socket.timedout = 1;
74327Svbart@nginx.com     c->socket.closed = 1;
74427Svbart@nginx.com 
74527Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
74627Svbart@nginx.com 
74727Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
74820Sigor@sysoev.ru }
74920Sigor@sysoev.ru 
75020Sigor@sysoev.ru 
75120Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
75220Sigor@sysoev.ru     nxt_aligned(64) =
75320Sigor@sysoev.ru {
75456Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
75520Sigor@sysoev.ru };
75620Sigor@sysoev.ru 
75720Sigor@sysoev.ru 
75820Sigor@sysoev.ru static void
75920Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
76020Sigor@sysoev.ru {
76162Sigor@sysoev.ru     nxt_conn_t  *c;
76220Sigor@sysoev.ru 
76320Sigor@sysoev.ru     c = obj;
76420Sigor@sysoev.ru 
76520Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
76620Sigor@sysoev.ru 
76727Svbart@nginx.com     nxt_queue_remove(&c->link);
76827Svbart@nginx.com 
76920Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
77020Sigor@sysoev.ru 
77162Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
77220Sigor@sysoev.ru }
77320Sigor@sysoev.ru 
77420Sigor@sysoev.ru 
77520Sigor@sysoev.ru static void
77620Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
77720Sigor@sysoev.ru {
77862Sigor@sysoev.ru     nxt_conn_t  *c;
77920Sigor@sysoev.ru 
78020Sigor@sysoev.ru     c = obj;
78120Sigor@sysoev.ru 
78220Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
78320Sigor@sysoev.ru 
784337Sigor@sysoev.ru     nxt_sockaddr_cache_free(task->thread->engine, c);
785337Sigor@sysoev.ru 
786386Sigor@sysoev.ru     nxt_conn_free(task, c);
78720Sigor@sysoev.ru }
78827Svbart@nginx.com 
78927Svbart@nginx.com 
79027Svbart@nginx.com static nxt_int_t
79160Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
792417Svbart@nginx.com     uintptr_t data)
79327Svbart@nginx.com {
79427Svbart@nginx.com     off_t                     length;
79527Svbart@nginx.com     nxt_controller_request_t  *r;
79627Svbart@nginx.com 
79727Svbart@nginx.com     r = ctx;
79827Svbart@nginx.com 
799417Svbart@nginx.com     length = nxt_off_t_parse(field->value, field->value_length);
80027Svbart@nginx.com 
801710Svbart@nginx.com     if (nxt_fast_path(length >= 0)) {
802107Svbart@nginx.com 
803107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
804417Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, &r->conn->log,
805417Svbart@nginx.com                           "Content-Length is too big");
806107Svbart@nginx.com             return NXT_ERROR;
807107Svbart@nginx.com         }
80827Svbart@nginx.com 
80927Svbart@nginx.com         r->length = length;
81027Svbart@nginx.com         return NXT_OK;
81127Svbart@nginx.com     }
81227Svbart@nginx.com 
813417Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, &r->conn->log, "Content-Length is invalid");
81427Svbart@nginx.com 
81527Svbart@nginx.com     return NXT_ERROR;
81627Svbart@nginx.com }
81727Svbart@nginx.com 
81827Svbart@nginx.com 
81927Svbart@nginx.com static void
820140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
82127Svbart@nginx.com {
822774Svbart@nginx.com     uint32_t                   i, count;
823774Svbart@nginx.com     nxt_str_t                  path;
824774Svbart@nginx.com     nxt_conn_t                 *c;
825774Svbart@nginx.com     nxt_conf_value_t           *value;
826774Svbart@nginx.com     nxt_controller_response_t  resp;
827774Svbart@nginx.com #if (NXT_TLS)
828774Svbart@nginx.com     nxt_conf_value_t           *certs;
829774Svbart@nginx.com 
830774Svbart@nginx.com     static nxt_str_t certificates = nxt_string("certificates");
831774Svbart@nginx.com #endif
832774Svbart@nginx.com     static nxt_str_t config = nxt_string("config");
833774Svbart@nginx.com 
834774Svbart@nginx.com     c = req->conn;
835774Svbart@nginx.com     path = req->parser.path;
836774Svbart@nginx.com 
837774Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
838774Svbart@nginx.com         path.length--;
839774Svbart@nginx.com     }
840774Svbart@nginx.com 
841774Svbart@nginx.com     if (nxt_str_start(&path, "/config", 7)
842774Svbart@nginx.com         && (path.length == 7 || path.start[7] == '/'))
843774Svbart@nginx.com     {
844774Svbart@nginx.com         if (path.length == 7) {
845774Svbart@nginx.com             path.length = 1;
846774Svbart@nginx.com 
847774Svbart@nginx.com         } else {
848774Svbart@nginx.com             path.length -= 7;
849774Svbart@nginx.com             path.start += 7;
850774Svbart@nginx.com         }
851774Svbart@nginx.com 
852774Svbart@nginx.com         nxt_controller_process_config(task, req, &path);
853774Svbart@nginx.com         return;
854774Svbart@nginx.com     }
855774Svbart@nginx.com 
856774Svbart@nginx.com #if (NXT_TLS)
857774Svbart@nginx.com 
858774Svbart@nginx.com     if (nxt_str_start(&path, "/certificates", 13)
859774Svbart@nginx.com         && (path.length == 13 || path.start[13] == '/'))
860774Svbart@nginx.com     {
861774Svbart@nginx.com         if (path.length == 13) {
862774Svbart@nginx.com             path.length = 1;
863774Svbart@nginx.com 
864774Svbart@nginx.com         } else {
865774Svbart@nginx.com             path.length -= 13;
866774Svbart@nginx.com             path.start += 13;
867774Svbart@nginx.com         }
868774Svbart@nginx.com 
869774Svbart@nginx.com         nxt_controller_process_cert(task, req, &path);
870774Svbart@nginx.com         return;
871774Svbart@nginx.com     }
872774Svbart@nginx.com 
873774Svbart@nginx.com #endif
874774Svbart@nginx.com 
875774Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
876774Svbart@nginx.com 
877774Svbart@nginx.com     if (path.length == 1 && path.start[0] == '/') {
878774Svbart@nginx.com 
879774Svbart@nginx.com         if (!nxt_str_eq(&req->parser.method, "GET", 3)) {
880774Svbart@nginx.com             goto invalid_method;
881774Svbart@nginx.com         }
882774Svbart@nginx.com 
883774Svbart@nginx.com         count = 1;
884774Svbart@nginx.com #if (NXT_TLS)
885774Svbart@nginx.com         count++;
886774Svbart@nginx.com #endif
887774Svbart@nginx.com 
888774Svbart@nginx.com         value = nxt_conf_create_object(c->mem_pool, count);
889774Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
890774Svbart@nginx.com             goto alloc_fail;
891774Svbart@nginx.com         }
892774Svbart@nginx.com 
893774Svbart@nginx.com         i = 0;
894774Svbart@nginx.com 
895774Svbart@nginx.com #if (NXT_TLS)
896774Svbart@nginx.com         certs = nxt_cert_info_get_all(c->mem_pool);
897774Svbart@nginx.com         if (nxt_slow_path(certs == NULL)) {
898774Svbart@nginx.com             goto alloc_fail;
899774Svbart@nginx.com         }
900774Svbart@nginx.com 
901774Svbart@nginx.com         nxt_conf_set_member(value, &certificates, certs, i++);
902774Svbart@nginx.com #endif
903774Svbart@nginx.com 
904774Svbart@nginx.com         nxt_conf_set_member(value, &config, nxt_controller_conf.root, i);
905774Svbart@nginx.com 
906774Svbart@nginx.com         resp.status = 200;
907774Svbart@nginx.com         resp.conf = value;
908774Svbart@nginx.com 
909774Svbart@nginx.com         nxt_controller_response(task, req, &resp);
910774Svbart@nginx.com         return;
911774Svbart@nginx.com     }
912774Svbart@nginx.com 
913774Svbart@nginx.com     resp.status = 404;
914774Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
915774Svbart@nginx.com     resp.offset = -1;
916774Svbart@nginx.com 
917774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
918774Svbart@nginx.com     return;
919774Svbart@nginx.com 
920774Svbart@nginx.com invalid_method:
921774Svbart@nginx.com 
922774Svbart@nginx.com     resp.status = 405;
923774Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
924774Svbart@nginx.com     resp.offset = -1;
925774Svbart@nginx.com 
926774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
927774Svbart@nginx.com     return;
928774Svbart@nginx.com 
929774Svbart@nginx.com alloc_fail:
930774Svbart@nginx.com 
931774Svbart@nginx.com     resp.status = 500;
932774Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
933774Svbart@nginx.com     resp.offset = -1;
934774Svbart@nginx.com 
935774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
936774Svbart@nginx.com     return;
937774Svbart@nginx.com }
938774Svbart@nginx.com 
939774Svbart@nginx.com 
940774Svbart@nginx.com static void
941774Svbart@nginx.com nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req,
942774Svbart@nginx.com     nxt_str_t *path)
943774Svbart@nginx.com {
94465Sigor@sysoev.ru     nxt_mp_t                   *mp;
94551Svbart@nginx.com     nxt_int_t                  rc;
946140Svbart@nginx.com     nxt_conn_t                 *c;
9471049Svbart@nginx.com     nxt_bool_t                 post;
94851Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
949106Svbart@nginx.com     nxt_conf_op_t              *ops;
950106Svbart@nginx.com     nxt_conf_value_t           *value;
951357Svbart@nginx.com     nxt_conf_validation_t      vldt;
952208Svbart@nginx.com     nxt_conf_json_error_t      error;
95344Svbart@nginx.com     nxt_controller_response_t  resp;
95444Svbart@nginx.com 
95551Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
95651Svbart@nginx.com 
957774Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
958632Svbart@nginx.com 
959774Svbart@nginx.com     c = req->conn;
96044Svbart@nginx.com 
96144Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
96246Svbart@nginx.com 
963774Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, path);
96451Svbart@nginx.com 
96551Svbart@nginx.com         if (value == NULL) {
966208Svbart@nginx.com             goto not_found;
96751Svbart@nginx.com         }
96851Svbart@nginx.com 
969208Svbart@nginx.com         resp.status = 200;
970106Svbart@nginx.com         resp.conf = value;
97146Svbart@nginx.com 
972208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
973208Svbart@nginx.com         return;
97451Svbart@nginx.com     }
97551Svbart@nginx.com 
9761049Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "POST", 4)) {
9771049Svbart@nginx.com         if (path->length == 1) {
9781049Svbart@nginx.com             goto not_allowed;
9791049Svbart@nginx.com         }
9801049Svbart@nginx.com 
9811049Svbart@nginx.com         post = 1;
9821049Svbart@nginx.com 
9831049Svbart@nginx.com     } else {
9841049Svbart@nginx.com         post = 0;
9851049Svbart@nginx.com     }
9861049Svbart@nginx.com 
9871049Svbart@nginx.com     if (post || nxt_str_eq(&req->parser.method, "PUT", 3)) {
98846Svbart@nginx.com 
989*1469Smax.romanov@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)
990*1469Smax.romanov@nginx.com             || nxt_controller_waiting_init_conf)
991*1469Smax.romanov@nginx.com         {
992238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
993238Svbart@nginx.com             return;
994238Svbart@nginx.com         }
995238Svbart@nginx.com 
99665Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
99751Svbart@nginx.com 
99851Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
999208Svbart@nginx.com             goto alloc_fail;
100046Svbart@nginx.com         }
100146Svbart@nginx.com 
100251Svbart@nginx.com         mbuf = &c->read->mem;
100351Svbart@nginx.com 
1004208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
1005208Svbart@nginx.com 
10061364Svbart@nginx.com         /* Skip UTF-8 BOM. */
10071364Svbart@nginx.com         if (nxt_buf_mem_used_size(mbuf) >= 3
10081364Svbart@nginx.com             && nxt_memcmp(mbuf->pos, "\xEF\xBB\xBF", 3) == 0)
10091364Svbart@nginx.com         {
10101364Svbart@nginx.com             mbuf->pos += 3;
10111364Svbart@nginx.com         }
10121364Svbart@nginx.com 
1013208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
101451Svbart@nginx.com 
101551Svbart@nginx.com         if (value == NULL) {
101665Sigor@sysoev.ru             nxt_mp_destroy(mp);
1017208Svbart@nginx.com 
1018208Svbart@nginx.com             if (error.pos == NULL) {
1019208Svbart@nginx.com                 goto alloc_fail;
1020208Svbart@nginx.com             }
1021208Svbart@nginx.com 
1022208Svbart@nginx.com             resp.status = 400;
1023208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
1024357Svbart@nginx.com             resp.detail.length = nxt_strlen(error.detail);
1025357Svbart@nginx.com             resp.detail.start = error.detail;
1026208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
1027208Svbart@nginx.com 
1028208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
1029208Svbart@nginx.com                                    &resp.line, &resp.column);
1030208Svbart@nginx.com 
1031208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
1032208Svbart@nginx.com             return;
103351Svbart@nginx.com         }
103451Svbart@nginx.com 
1035774Svbart@nginx.com         if (path->length != 1) {
1036106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
1037106Svbart@nginx.com                                      nxt_controller_conf.root,
10381049Svbart@nginx.com                                      path, value, post);
103946Svbart@nginx.com 
10401049Svbart@nginx.com             if (rc != NXT_CONF_OP_OK) {
1041619Svbart@nginx.com                 nxt_mp_destroy(mp);
1042619Svbart@nginx.com 
10431049Svbart@nginx.com                 switch (rc) {
10441049Svbart@nginx.com                 case NXT_CONF_OP_NOT_FOUND:
1045208Svbart@nginx.com                     goto not_found;
10461049Svbart@nginx.com 
10471049Svbart@nginx.com                 case NXT_CONF_OP_NOT_ALLOWED:
10481049Svbart@nginx.com                     goto