xref: /unit/src/nxt_controller.c (revision 1470)
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);
501469Smax.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);
79*1470Smax.romanov@nginx.com static nxt_bool_t nxt_controller_check_postpone_request(nxt_task_t *task);
80774Svbart@nginx.com #if (NXT_TLS)
81774Svbart@nginx.com static void nxt_controller_process_cert(nxt_task_t *task,
82774Svbart@nginx.com     nxt_controller_request_t *req, nxt_str_t *path);
83774Svbart@nginx.com static void nxt_controller_process_cert_save(nxt_task_t *task,
84774Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
85774Svbart@nginx.com static nxt_bool_t nxt_controller_cert_in_use(nxt_str_t *name);
86774Svbart@nginx.com #endif
87238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task,
88238Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
89314Svbart@nginx.com static void nxt_controller_conf_store(nxt_task_t *task,
90314Svbart@nginx.com     nxt_conf_value_t *conf);
91140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
92140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
93208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
94208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
9527Svbart@nginx.com 
9627Svbart@nginx.com 
97417Svbart@nginx.com static nxt_http_field_proc_t  nxt_controller_request_fields[] = {
9827Svbart@nginx.com     { nxt_string("Content-Length"),
9927Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
10027Svbart@nginx.com };
10127Svbart@nginx.com 
102417Svbart@nginx.com static nxt_lvlhsh_t            nxt_controller_fields_hash;
10327Svbart@nginx.com 
104314Svbart@nginx.com static nxt_uint_t              nxt_controller_listening;
105662Smax.romanov@nginx.com static nxt_uint_t              nxt_controller_router_ready;
106238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
107238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
1081469Smax.romanov@nginx.com static nxt_bool_t              nxt_controller_waiting_init_conf;
10927Svbart@nginx.com 
11020Sigor@sysoev.ru 
11120Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
11227Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
11327Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
11420Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
11520Sigor@sysoev.ru 
11620Sigor@sysoev.ru 
117320Smax.romanov@nginx.com nxt_port_handlers_t  nxt_controller_process_port_handlers = {
118662Smax.romanov@nginx.com     .quit           = nxt_worker_process_quit_handler,
119662Smax.romanov@nginx.com     .new_port       = nxt_controller_process_new_port_handler,
120662Smax.romanov@nginx.com     .change_file    = nxt_port_change_log_file_handler,
121662Smax.romanov@nginx.com     .mmap           = nxt_port_mmap_handler,
122662Smax.romanov@nginx.com     .process_ready  = nxt_controller_router_ready_handler,
123662Smax.romanov@nginx.com     .data           = nxt_port_data_handler,
124662Smax.romanov@nginx.com     .remove_pid     = nxt_port_remove_pid_handler,
125662Smax.romanov@nginx.com     .rpc_ready      = nxt_port_rpc_handler,
126662Smax.romanov@nginx.com     .rpc_error      = nxt_port_rpc_handler,
127248Svbart@nginx.com };
128248Svbart@nginx.com 
129248Svbart@nginx.com 
13020Sigor@sysoev.ru nxt_int_t
131141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
13220Sigor@sysoev.ru {
133417Svbart@nginx.com     nxt_mp_t               *mp;
134417Svbart@nginx.com     nxt_int_t              ret;
135417Svbart@nginx.com     nxt_str_t              *json;
136417Svbart@nginx.com     nxt_conf_value_t       *conf;
137417Svbart@nginx.com     nxt_conf_validation_t  vldt;
138774Svbart@nginx.com     nxt_controller_init_t  *init;
13927Svbart@nginx.com 
1401459Smax.romanov@nginx.com     ret = nxt_http_fields_hash(&nxt_controller_fields_hash,
141417Svbart@nginx.com                                nxt_controller_request_fields,
142417Svbart@nginx.com                                nxt_nitems(nxt_controller_request_fields));
143417Svbart@nginx.com 
144417Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
14527Svbart@nginx.com         return NXT_ERROR;
14627Svbart@nginx.com     }
14727Svbart@nginx.com 
148248Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
14927Svbart@nginx.com 
150774Svbart@nginx.com     init = data;
151774Svbart@nginx.com 
152774Svbart@nginx.com #if (NXT_TLS)
153314Svbart@nginx.com 
154774Svbart@nginx.com     if (init->certs != NULL) {
155774Svbart@nginx.com         nxt_cert_info_init(task, init->certs);
156774Svbart@nginx.com         nxt_cert_store_release(init->certs);
157774Svbart@nginx.com     }
158774Svbart@nginx.com 
159774Svbart@nginx.com #endif
160774Svbart@nginx.com 
161774Svbart@nginx.com     json = &init->conf;
162774Svbart@nginx.com 
163774Svbart@nginx.com     if (json->start == NULL) {
164314Svbart@nginx.com         return NXT_OK;
165314Svbart@nginx.com     }
166314Svbart@nginx.com 
167314Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
168314Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
169314Svbart@nginx.com         return NXT_ERROR;
170314Svbart@nginx.com     }
171314Svbart@nginx.com 
172314Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, json);
173314Svbart@nginx.com     nxt_free(json->start);
174314Svbart@nginx.com 
175314Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
176564Svbart@nginx.com         nxt_alert(task, "failed to restore previous configuration: "
177564Svbart@nginx.com                   "file is corrupted or not enough memory");
178314Svbart@nginx.com 
179314Svbart@nginx.com         nxt_mp_destroy(mp);
180314Svbart@nginx.com         return NXT_OK;
181314Svbart@nginx.com     }
182314Svbart@nginx.com 
183357Svbart@nginx.com     nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
184314Svbart@nginx.com 
185357Svbart@nginx.com     vldt.pool = nxt_mp_create(1024, 128, 256, 32);
186357Svbart@nginx.com     if (nxt_slow_path(vldt.pool == NULL)) {
1871013Smax.romanov@nginx.com         nxt_mp_destroy(mp);
188357Svbart@nginx.com         return NXT_ERROR;
189314Svbart@nginx.com     }
190314Svbart@nginx.com 
191357Svbart@nginx.com     vldt.conf = conf;
192357Svbart@nginx.com 
193357Svbart@nginx.com     ret = nxt_conf_validate(&vldt);
194357Svbart@nginx.com 
195357Svbart@nginx.com     if (nxt_slow_path(ret != NXT_OK)) {
196357Svbart@nginx.com 
197357Svbart@nginx.com         if (ret == NXT_DECLINED) {
198564Svbart@nginx.com             nxt_alert(task, "the previous configuration is invalid: %V",
199564Svbart@nginx.com                       &vldt.error);
200357Svbart@nginx.com 
201357Svbart@nginx.com             nxt_mp_destroy(vldt.pool);
202357Svbart@nginx.com             nxt_mp_destroy(mp);
203357Svbart@nginx.com 
204357Svbart@nginx.com             return NXT_OK;
205357Svbart@nginx.com         }
206357Svbart@nginx.com 
207357Svbart@nginx.com         /* ret == NXT_ERROR */
208357Svbart@nginx.com 
209357Svbart@nginx.com         return NXT_ERROR;
210357Svbart@nginx.com     }
211357Svbart@nginx.com 
212357Svbart@nginx.com     nxt_mp_destroy(vldt.pool);
213314Svbart@nginx.com 
214314Svbart@nginx.com     nxt_controller_conf.root = conf;
215314Svbart@nginx.com     nxt_controller_conf.pool = mp;
216314Svbart@nginx.com 
217248Svbart@nginx.com     return NXT_OK;
218248Svbart@nginx.com }
219248Svbart@nginx.com 
220248Svbart@nginx.com 
221248Svbart@nginx.com static void
222248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task,
223248Svbart@nginx.com     nxt_port_recv_msg_t *msg)
224248Svbart@nginx.com {
225662Smax.romanov@nginx.com     nxt_port_new_port_handler(task, msg);
226662Smax.romanov@nginx.com 
227662Smax.romanov@nginx.com     if (msg->u.new_port->type != NXT_PROCESS_ROUTER
228662Smax.romanov@nginx.com         || !nxt_controller_router_ready)
229662Smax.romanov@nginx.com     {
230662Smax.romanov@nginx.com         return;
231662Smax.romanov@nginx.com     }
232662Smax.romanov@nginx.com 
233662Smax.romanov@nginx.com     nxt_controller_send_current_conf(task);
234662Smax.romanov@nginx.com }
235662Smax.romanov@nginx.com 
236662Smax.romanov@nginx.com 
237662Smax.romanov@nginx.com static void
238662Smax.romanov@nginx.com nxt_controller_send_current_conf(nxt_task_t *task)
239662Smax.romanov@nginx.com {
240249Svbart@nginx.com     nxt_int_t         rc;
241248Svbart@nginx.com     nxt_runtime_t     *rt;
242248Svbart@nginx.com     nxt_conf_value_t  *conf;
243248Svbart@nginx.com 
244249Svbart@nginx.com     conf = nxt_controller_conf.root;
245249Svbart@nginx.com 
246249Svbart@nginx.com     if (conf != NULL) {
247249Svbart@nginx.com         rc = nxt_controller_conf_send(task, conf,
248249Svbart@nginx.com                                       nxt_controller_conf_init_handler, NULL);
24944Svbart@nginx.com 
250249Svbart@nginx.com         if (nxt_fast_path(rc == NXT_OK)) {
2511469Smax.romanov@nginx.com             nxt_controller_waiting_init_conf = 1;
2521469Smax.romanov@nginx.com 
253249Svbart@nginx.com             return;
254249Svbart@nginx.com         }
255249Svbart@nginx.com 
256249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
257249Svbart@nginx.com 
258249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
259249Svbart@nginx.com             nxt_abort();
260249Svbart@nginx.com         }
26144Svbart@nginx.com     }
26244Svbart@nginx.com 
263249Svbart@nginx.com     if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
264248Svbart@nginx.com         nxt_abort();
26544Svbart@nginx.com     }
26644Svbart@nginx.com 
267248Svbart@nginx.com     rt = task->thread->runtime;
268140Svbart@nginx.com 
269248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
270248Svbart@nginx.com         nxt_abort();
271248Svbart@nginx.com     }
272314Svbart@nginx.com 
273314Svbart@nginx.com     nxt_controller_listening = 1;
274*1470Smax.romanov@nginx.com 
275*1470Smax.romanov@nginx.com     nxt_controller_flush_requests(task);
27620Sigor@sysoev.ru }
27720Sigor@sysoev.ru 
27820Sigor@sysoev.ru 
279662Smax.romanov@nginx.com static void
280662Smax.romanov@nginx.com nxt_controller_router_ready_handler(nxt_task_t *task,
281662Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg)
282662Smax.romanov@nginx.com {
283662Smax.romanov@nginx.com     nxt_port_t     *router_port;
284662Smax.romanov@nginx.com     nxt_runtime_t  *rt;
285662Smax.romanov@nginx.com 
286662Smax.romanov@nginx.com     rt = task->thread->runtime;
287662Smax.romanov@nginx.com 
288662Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
289662Smax.romanov@nginx.com 
290662Smax.romanov@nginx.com     nxt_controller_router_ready = 1;
291662Smax.romanov@nginx.com 
292662Smax.romanov@nginx.com     if (router_port != NULL) {
293662Smax.romanov@nginx.com         nxt_controller_send_current_conf(task);
294662Smax.romanov@nginx.com     }
295662Smax.romanov@nginx.com }
296662Smax.romanov@nginx.com 
297662Smax.romanov@nginx.com 
298249Svbart@nginx.com static nxt_int_t
299249Svbart@nginx.com nxt_controller_conf_default(void)
300249Svbart@nginx.com {
301249Svbart@nginx.com     nxt_mp_t          *mp;
302249Svbart@nginx.com     nxt_conf_value_t  *conf;
303249Svbart@nginx.com 
304249Svbart@nginx.com     static const nxt_str_t json
305249Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
306249Svbart@nginx.com 
307249Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
308249Svbart@nginx.com 
309249Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
310249Svbart@nginx.com         return NXT_ERROR;
311249Svbart@nginx.com     }
312249Svbart@nginx.com 
313249Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
314249Svbart@nginx.com 
315249Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
316249Svbart@nginx.com         return NXT_ERROR;
317249Svbart@nginx.com     }
318249Svbart@nginx.com 
319249Svbart@nginx.com     nxt_controller_conf.root = conf;
320249Svbart@nginx.com     nxt_controller_conf.pool = mp;
321249Svbart@nginx.com 
322249Svbart@nginx.com     return NXT_OK;
323249Svbart@nginx.com }
324249Svbart@nginx.com 
325249Svbart@nginx.com 
326249Svbart@nginx.com static void
327249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
328249Svbart@nginx.com     void *data)
329249Svbart@nginx.com {
330314Svbart@nginx.com     nxt_runtime_t  *rt;
331314Svbart@nginx.com 
3321469Smax.romanov@nginx.com     nxt_controller_waiting_init_conf = 0;
3331469Smax.romanov@nginx.com 
334249Svbart@nginx.com     if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) {
335564Svbart@nginx.com         nxt_alert(task, "failed to apply previous configuration");
336314Svbart@nginx.com 
337249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
338249Svbart@nginx.com 
339249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
340249Svbart@nginx.com             nxt_abort();
341249Svbart@nginx.com         }
342249Svbart@nginx.com     }
343314Svbart@nginx.com 
344314Svbart@nginx.com     if (nxt_controller_listening == 0) {
345314Svbart@nginx.com         rt = task->thread->runtime;
346314Svbart@nginx.com 
347314Svbart@nginx.com         if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket)
348314Svbart@nginx.com                           == NULL))
349314Svbart@nginx.com         {
350314Svbart@nginx.com             nxt_abort();
351314Svbart@nginx.com         }
352314Svbart@nginx.com 
353314Svbart@nginx.com         nxt_controller_listening = 1;
354314Svbart@nginx.com     }
3551469Smax.romanov@nginx.com 
3561469Smax.romanov@nginx.com     nxt_controller_flush_requests(task);
3571469Smax.romanov@nginx.com }
3581469Smax.romanov@nginx.com 
3591469Smax.romanov@nginx.com 
3601469Smax.romanov@nginx.com static void
3611469Smax.romanov@nginx.com nxt_controller_flush_requests(nxt_task_t *task)
3621469Smax.romanov@nginx.com {
3631469Smax.romanov@nginx.com     nxt_queue_t               queue;
3641469Smax.romanov@nginx.com     nxt_controller_request_t  *req;
3651469Smax.romanov@nginx.com 
3661469Smax.romanov@nginx.com     nxt_queue_init(&queue);
3671469Smax.romanov@nginx.com     nxt_queue_add(&queue, &nxt_controller_waiting_requests);
3681469Smax.romanov@nginx.com 
3691469Smax.romanov@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
3701469Smax.romanov@nginx.com 
3711469Smax.romanov@nginx.com     nxt_queue_each(req, &queue, nxt_controller_request_t, link) {
3721469Smax.romanov@nginx.com         nxt_controller_process_request(task, req);
3731469Smax.romanov@nginx.com     } nxt_queue_loop;
374249Svbart@nginx.com }
375249Svbart@nginx.com 
376249Svbart@nginx.com 
377249Svbart@nginx.com static nxt_int_t
378249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf,
379249Svbart@nginx.com     nxt_port_rpc_handler_t handler, void *data)
380249Svbart@nginx.com {
381249Svbart@nginx.com     size_t         size;
382249Svbart@nginx.com     uint32_t       stream;
383249Svbart@nginx.com     nxt_int_t      rc;
384249Svbart@nginx.com     nxt_buf_t      *b;
385249Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
386249Svbart@nginx.com     nxt_runtime_t  *rt;
387249Svbart@nginx.com 
388249Svbart@nginx.com     rt = task->thread->runtime;
389249Svbart@nginx.com 
390249Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
391249Svbart@nginx.com 
392*1470Smax.romanov@nginx.com     nxt_assert(router_port != NULL);
393*1470Smax.romanov@nginx.com     nxt_assert(nxt_controller_router_ready);
394249Svbart@nginx.com 
395249Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
396249Svbart@nginx.com 
397249Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
398249Svbart@nginx.com 
399249Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
400379Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
401379Smax.romanov@nginx.com         return NXT_ERROR;
402379Smax.romanov@nginx.com     }
403249Svbart@nginx.com 
404249Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
405249Svbart@nginx.com 
406249Svbart@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
407249Svbart@nginx.com                                            handler, handler,
408249Svbart@nginx.com                                            router_port->pid, data);
409249Svbart@nginx.com 
410645Svbart@nginx.com     if (nxt_slow_path(stream == 0)) {
411645Svbart@nginx.com         return NXT_ERROR;
412645Svbart@nginx.com     }
413645Svbart@nginx.com 
414249Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
415249Svbart@nginx.com                                stream, controller_port->id, b);
416249Svbart@nginx.com 
417249Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
418249Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
419249Svbart@nginx.com         return NXT_ERROR;
420249Svbart@nginx.com     }
421249Svbart@nginx.com 
422249Svbart@nginx.com     return NXT_OK;
423249Svbart@nginx.com }
424249Svbart@nginx.com 
425249Svbart@nginx.com 
42620Sigor@sysoev.ru nxt_int_t
42720Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
42820Sigor@sysoev.ru {
42920Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
43020Sigor@sysoev.ru 
43165Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
43220Sigor@sysoev.ru     if (ls == NULL) {
43320Sigor@sysoev.ru         return NXT_ERROR;
43420Sigor@sysoev.ru     }
43520Sigor@sysoev.ru 
4361448Svbart@nginx.com     ls->sockaddr = rt->controller_listen;
43720Sigor@sysoev.ru 
438359Sigor@sysoev.ru     nxt_listen_socket_remote_size(ls);
43920Sigor@sysoev.ru 
44020Sigor@sysoev.ru     ls->socket = -1;
44120Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
44220Sigor@sysoev.ru     ls->read_after_accept = 1;
44320Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
44420Sigor@sysoev.ru 
44520Sigor@sysoev.ru #if 0
44620Sigor@sysoev.ru     /* STUB */
44765Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
44820Sigor@sysoev.ru     if (wq == NULL) {
44920Sigor@sysoev.ru         return NXT_ERROR;
45020Sigor@sysoev.ru     }
45120Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
45220Sigor@sysoev.ru     /**/
45320Sigor@sysoev.ru 
45420Sigor@sysoev.ru     ls->work_queue = wq;
45520Sigor@sysoev.ru #endif
45620Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
45720Sigor@sysoev.ru 
4581451Svbart@nginx.com     if (nxt_listen_socket_create(task, rt->mem_pool, ls) != NXT_OK) {
45920Sigor@sysoev.ru         return NXT_ERROR;
46020Sigor@sysoev.ru     }
46120Sigor@sysoev.ru 
46220Sigor@sysoev.ru     rt->controller_socket = ls;
46320Sigor@sysoev.ru 
46420Sigor@sysoev.ru     return NXT_OK;
46520Sigor@sysoev.ru }
46620Sigor@sysoev.ru 
46720Sigor@sysoev.ru 
46820Sigor@sysoev.ru static void
46920Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
47020Sigor@sysoev.ru {
47127Svbart@nginx.com     nxt_buf_t                 *b;
47262Sigor@sysoev.ru     nxt_conn_t                *c;
47327Svbart@nginx.com     nxt_event_engine_t        *engine;
47427Svbart@nginx.com     nxt_controller_request_t  *r;
47520Sigor@sysoev.ru 
47620Sigor@sysoev.ru     c = obj;
47720Sigor@sysoev.ru 
47820Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
47920Sigor@sysoev.ru 
48065Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
48127Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
48227Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
48327Svbart@nginx.com         return;
48427Svbart@nginx.com     }
48527Svbart@nginx.com 
486140Svbart@nginx.com     r->conn = c;
487140Svbart@nginx.com 
48860Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
48960Svbart@nginx.com                       != NXT_OK))
49060Svbart@nginx.com     {
49160Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
49260Svbart@nginx.com         return;
49360Svbart@nginx.com     }
49427Svbart@nginx.com 
4951167Svbart@nginx.com     r->parser.encoded_slashes = 1;
4961167Svbart@nginx.com 
49720Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
49820Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
49920Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
50020Sigor@sysoev.ru         return;
50120Sigor@sysoev.ru     }
50220Sigor@sysoev.ru 
50320Sigor@sysoev.ru     c->read = b;
50427Svbart@nginx.com     c->socket.data = r;
50520Sigor@sysoev.ru     c->socket.read_ready = 1;
50620Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
50720Sigor@sysoev.ru 
50820Sigor@sysoev.ru     engine = task->thread->engine;
50920Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
51027Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
51120Sigor@sysoev.ru 
51262Sigor@sysoev.ru     nxt_conn_read(engine, c);
51320Sigor@sysoev.ru }
51420Sigor@sysoev.ru 
51520Sigor@sysoev.ru 
51620Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
51720Sigor@sysoev.ru     nxt_aligned(64) =
51820Sigor@sysoev.ru {
51956Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
52056Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
52156Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
52220Sigor@sysoev.ru 
52356Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
52456Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
52556Sigor@sysoev.ru     .timer_data = 60 * 1000,
52620Sigor@sysoev.ru };
52720Sigor@sysoev.ru 
52820Sigor@sysoev.ru 
52920Sigor@sysoev.ru static void
53020Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
53120Sigor@sysoev.ru {
53227Svbart@nginx.com     size_t                    preread;
53327Svbart@nginx.com     nxt_buf_t                 *b;
53427Svbart@nginx.com     nxt_int_t                 rc;
53562Sigor@sysoev.ru     nxt_conn_t                *c;
53627Svbart@nginx.com     nxt_controller_request_t  *r;
53720Sigor@sysoev.ru 
53820Sigor@sysoev.ru     c = obj;
53927Svbart@nginx.com     r = data;
54020Sigor@sysoev.ru 
54120Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
54220Sigor@sysoev.ru 
54327Svbart@nginx.com     nxt_queue_remove(&c->link);
54427Svbart@nginx.com     nxt_queue_self(&c->link);
54527Svbart@nginx.com 
54627Svbart@nginx.com     b = c->read;
54727Svbart@nginx.com 
54827Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
54927Svbart@nginx.com 
55027Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
55127Svbart@nginx.com 
55227Svbart@nginx.com         if (rc == NXT_AGAIN) {
55327Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
55427Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
55527Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
55627Svbart@nginx.com                 return;
55727Svbart@nginx.com             }
55827Svbart@nginx.com 
55962Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
56027Svbart@nginx.com             return;
56127Svbart@nginx.com         }
56227Svbart@nginx.com 
56327Svbart@nginx.com         /* rc == NXT_ERROR */
56427Svbart@nginx.com 
56527Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
56627Svbart@nginx.com 
56727Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
56827Svbart@nginx.com         return;
56927Svbart@nginx.com     }
57027Svbart@nginx.com 
571417Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash,
572417Svbart@nginx.com                                  r);
57360Svbart@nginx.com 
57460Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
57560Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
57660Svbart@nginx.com         return;
57760Svbart@nginx.com     }
57860Svbart@nginx.com 
57927Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
58027Svbart@nginx.com 
58127Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
582107Svbart@nginx.com                     "body length: %uz, preread: %uz",
58327Svbart@nginx.com                     r->length, preread);
58427Svbart@nginx.com 
58527Svbart@nginx.com     if (preread >= r->length) {
586140Svbart@nginx.com         nxt_controller_process_request(task, r);
58727Svbart@nginx.com         return;
58827Svbart@nginx.com     }
58927Svbart@nginx.com 
59027Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
59127Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
59227Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
59327Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
59427Svbart@nginx.com             return;
59527Svbart@nginx.com         }
59627Svbart@nginx.com 
59727Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
59827Svbart@nginx.com 
59927Svbart@nginx.com         c->read = b;
60027Svbart@nginx.com     }
60127Svbart@nginx.com 
60227Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
60327Svbart@nginx.com 
60462Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
60520Sigor@sysoev.ru }
60620Sigor@sysoev.ru 
60720Sigor@sysoev.ru 
60820Sigor@sysoev.ru static nxt_msec_t
60962Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
61020Sigor@sysoev.ru {
61120Sigor@sysoev.ru     return (nxt_msec_t) data;
61220Sigor@sysoev.ru }
61320Sigor@sysoev.ru 
61420Sigor@sysoev.ru 
61520Sigor@sysoev.ru static void
61620Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
61720Sigor@sysoev.ru {
61862Sigor@sysoev.ru     nxt_conn_t  *c;
61920Sigor@sysoev.ru 
62020Sigor@sysoev.ru     c = obj;
62120Sigor@sysoev.ru 
62220Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
62320Sigor@sysoev.ru 
62427Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
62520Sigor@sysoev.ru }
62620Sigor@sysoev.ru 
62720Sigor@sysoev.ru 
62820Sigor@sysoev.ru static void
62920Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
63020Sigor@sysoev.ru {
63162Sigor@sysoev.ru     nxt_timer_t  *timer;
63262Sigor@sysoev.ru     nxt_conn_t   *c;
63320Sigor@sysoev.ru 
63462Sigor@sysoev.ru     timer = obj;
63520Sigor@sysoev.ru 
63662Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
63720Sigor@sysoev.ru     c->socket.timedout = 1;
63820Sigor@sysoev.ru     c->socket.closed = 1;
63920Sigor@sysoev.ru 
64020Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
64120Sigor@sysoev.ru 
64227Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
64327Svbart@nginx.com }
64427Svbart@nginx.com 
64527Svbart@nginx.com 
64627Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
64727Svbart@nginx.com     nxt_aligned(64) =
64827Svbart@nginx.com {
64956Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
65056Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
65156Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
65227Svbart@nginx.com 
65356Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
65456Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
65556Sigor@sysoev.ru     .timer_data = 60 * 1000,
65656Sigor@sysoev.ru     .timer_autoreset = 1,
65727Svbart@nginx.com };
65827Svbart@nginx.com 
65927Svbart@nginx.com 
66027Svbart@nginx.com static void
66127Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
66227Svbart@nginx.com {
663107Svbart@nginx.com     size_t                    read;
664107Svbart@nginx.com     nxt_buf_t                 *b;
665107Svbart@nginx.com     nxt_conn_t                *c;
666107Svbart@nginx.com     nxt_controller_request_t  *r;
66727Svbart@nginx.com 
66827Svbart@nginx.com     c = obj;
669107Svbart@nginx.com     r = data;
67027Svbart@nginx.com     b = c->read;
67127Svbart@nginx.com 
672107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
67327Svbart@nginx.com 
674107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
675107Svbart@nginx.com               read, r->length);
67627Svbart@nginx.com 
677107Svbart@nginx.com     if (read >= r->length) {
678140Svbart@nginx.com         nxt_controller_process_request(task, r);
67927Svbart@nginx.com         return;
68027Svbart@nginx.com     }
68127Svbart@nginx.com 
68262Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
68327Svbart@nginx.com }
68427Svbart@nginx.com 
68527Svbart@nginx.com 
68627Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
68727Svbart@nginx.com     nxt_aligned(64) =
68827Svbart@nginx.com {
68956Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
69056Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
69127Svbart@nginx.com 
69256Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
69356Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
69456Sigor@sysoev.ru     .timer_data = 60 * 1000,
69556Sigor@sysoev.ru     .timer_autoreset = 1,
69627Svbart@nginx.com };
69727Svbart@nginx.com 
69827Svbart@nginx.com 
69927Svbart@nginx.com static void
70027Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
70127Svbart@nginx.com {
70262Sigor@sysoev.ru     nxt_buf_t   *b;
70362Sigor@sysoev.ru     nxt_conn_t  *c;
70427Svbart@nginx.com 
70527Svbart@nginx.com     c = obj;
70627Svbart@nginx.com 
70727Svbart@nginx.com     nxt_debug(task, "controller conn write");
70827Svbart@nginx.com 
70927Svbart@nginx.com     b = c->write;
71027Svbart@nginx.com 
71127Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
71262Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
71327Svbart@nginx.com         return;
71427Svbart@nginx.com     }
71527Svbart@nginx.com 
71627Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
71727Svbart@nginx.com 
71827Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
71927Svbart@nginx.com }
72027Svbart@nginx.com 
72127Svbart@nginx.com 
72227Svbart@nginx.com static void
72327Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
72427Svbart@nginx.com {
72562Sigor@sysoev.ru     nxt_conn_t  *c;
72627Svbart@nginx.com 
72727Svbart@nginx.com     c = obj;
72827Svbart@nginx.com 
72927Svbart@nginx.com     nxt_debug(task, "controller conn write error");
73027Svbart@nginx.com 
73127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
73227Svbart@nginx.com }
73327Svbart@nginx.com 
73427Svbart@nginx.com 
73527Svbart@nginx.com static void
73627Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
73727Svbart@nginx.com {
73862Sigor@sysoev.ru     nxt_conn_t   *c;
73962Sigor@sysoev.ru     nxt_timer_t  *timer;
74027Svbart@nginx.com 
74162Sigor@sysoev.ru     timer = obj;
74227Svbart@nginx.com 
74362Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
74427Svbart@nginx.com     c->socket.timedout = 1;
74527Svbart@nginx.com     c->socket.closed = 1;
74627Svbart@nginx.com 
74727Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
74827Svbart@nginx.com 
74927Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
75020Sigor@sysoev.ru }
75120Sigor@sysoev.ru 
75220Sigor@sysoev.ru 
75320Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
75420Sigor@sysoev.ru     nxt_aligned(64) =
75520Sigor@sysoev.ru {
75656Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
75720Sigor@sysoev.ru };
75820Sigor@sysoev.ru 
75920Sigor@sysoev.ru 
76020Sigor@sysoev.ru static void
76120Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
76220Sigor@sysoev.ru {
76362Sigor@sysoev.ru     nxt_conn_t  *c;
76420Sigor@sysoev.ru 
76520Sigor@sysoev.ru     c = obj;
76620Sigor@sysoev.ru 
76720Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
76820Sigor@sysoev.ru 
76927Svbart@nginx.com     nxt_queue_remove(&c->link);
77027Svbart@nginx.com 
77120Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
77220Sigor@sysoev.ru 
77362Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
77420Sigor@sysoev.ru }
77520Sigor@sysoev.ru 
77620Sigor@sysoev.ru 
77720Sigor@sysoev.ru static void
77820Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
77920Sigor@sysoev.ru {
78062Sigor@sysoev.ru     nxt_conn_t  *c;
78120Sigor@sysoev.ru 
78220Sigor@sysoev.ru     c = obj;
78320Sigor@sysoev.ru 
78420Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
78520Sigor@sysoev.ru 
786337Sigor@sysoev.ru     nxt_sockaddr_cache_free(task->thread->engine, c);
787337Sigor@sysoev.ru 
788386Sigor@sysoev.ru     nxt_conn_free(task, c);
78920Sigor@sysoev.ru }
79027Svbart@nginx.com 
79127Svbart@nginx.com 
79227Svbart@nginx.com static nxt_int_t
79360Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
794417Svbart@nginx.com     uintptr_t data)
79527Svbart@nginx.com {
79627Svbart@nginx.com     off_t                     length;
79727Svbart@nginx.com     nxt_controller_request_t  *r;
79827Svbart@nginx.com 
79927Svbart@nginx.com     r = ctx;
80027Svbart@nginx.com 
801417Svbart@nginx.com     length = nxt_off_t_parse(field->value, field->value_length);
80227Svbart@nginx.com 
803710Svbart@nginx.com     if (nxt_fast_path(length >= 0)) {
804107Svbart@nginx.com 
805107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
806417Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, &r->conn->log,
807417Svbart@nginx.com                           "Content-Length is too big");
808107Svbart@nginx.com             return NXT_ERROR;
809107Svbart@nginx.com         }
81027Svbart@nginx.com 
81127Svbart@nginx.com         r->length = length;
81227Svbart@nginx.com         return NXT_OK;
81327Svbart@nginx.com     }
81427Svbart@nginx.com 
815417Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, &r->conn->log, "Content-Length is invalid");
81627Svbart@nginx.com 
81727Svbart@nginx.com     return NXT_ERROR;
81827Svbart@nginx.com }
81927Svbart@nginx.com 
82027Svbart@nginx.com 
82127Svbart@nginx.com static void
822140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
82327Svbart@nginx.com {
824774Svbart@nginx.com     uint32_t                   i, count;
825774Svbart@nginx.com     nxt_str_t                  path;
826774Svbart@nginx.com     nxt_conn_t                 *c;
827774Svbart@nginx.com     nxt_conf_value_t           *value;
828774Svbart@nginx.com     nxt_controller_response_t  resp;
829774Svbart@nginx.com #if (NXT_TLS)
830774Svbart@nginx.com     nxt_conf_value_t           *certs;
831774Svbart@nginx.com 
832774Svbart@nginx.com     static nxt_str_t certificates = nxt_string("certificates");
833774Svbart@nginx.com #endif
834774Svbart@nginx.com     static nxt_str_t config = nxt_string("config");
835774Svbart@nginx.com 
836774Svbart@nginx.com     c = req->conn;
837774Svbart@nginx.com     path = req->parser.path;
838774Svbart@nginx.com 
839774Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
840774Svbart@nginx.com         path.length--;
841774Svbart@nginx.com     }
842774Svbart@nginx.com 
843774Svbart@nginx.com     if (nxt_str_start(&path, "/config", 7)
844774Svbart@nginx.com         && (path.length == 7 || path.start[7] == '/'))
845774Svbart@nginx.com     {
846774Svbart@nginx.com         if (path.length == 7) {
847774Svbart@nginx.com             path.length = 1;
848774Svbart@nginx.com 
849774Svbart@nginx.com         } else {
850774Svbart@nginx.com             path.length -= 7;
851774Svbart@nginx.com             path.start += 7;
852774Svbart@nginx.com         }
853774Svbart@nginx.com 
854774Svbart@nginx.com         nxt_controller_process_config(task, req, &path);
855774Svbart@nginx.com         return;
856774Svbart@nginx.com     }
857774Svbart@nginx.com 
858774Svbart@nginx.com #if (NXT_TLS)
859774Svbart@nginx.com 
860774Svbart@nginx.com     if (nxt_str_start(&path, "/certificates", 13)
861774Svbart@nginx.com         && (path.length == 13 || path.start[13] == '/'))
862774Svbart@nginx.com     {
863774Svbart@nginx.com         if (path.length == 13) {
864774Svbart@nginx.com             path.length = 1;
865774Svbart@nginx.com 
866774Svbart@nginx.com         } else {
867774Svbart@nginx.com             path.length -= 13;
868774Svbart@nginx.com             path.start += 13;
869774Svbart@nginx.com         }
870774Svbart@nginx.com 
871774Svbart@nginx.com         nxt_controller_process_cert(task, req, &path);
872774Svbart@nginx.com         return;
873774Svbart@nginx.com     }
874774Svbart@nginx.com 
875774Svbart@nginx.com #endif
876774Svbart@nginx.com 
877774Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
878774Svbart@nginx.com 
879774Svbart@nginx.com     if (path.length == 1 && path.start[0] == '/') {
880774Svbart@nginx.com 
881774Svbart@nginx.com         if (!nxt_str_eq(&req->parser.method, "GET", 3)) {
882774Svbart@nginx.com             goto invalid_method;
883774Svbart@nginx.com         }
884774Svbart@nginx.com 
885774Svbart@nginx.com         count = 1;
886774Svbart@nginx.com #if (NXT_TLS)
887774Svbart@nginx.com         count++;
888774Svbart@nginx.com #endif
889774Svbart@nginx.com 
890774Svbart@nginx.com         value = nxt_conf_create_object(c->mem_pool, count);
891774Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
892774Svbart@nginx.com             goto alloc_fail;
893774Svbart@nginx.com         }
894774Svbart@nginx.com 
895774Svbart@nginx.com         i = 0;
896774Svbart@nginx.com 
897774Svbart@nginx.com #if (NXT_TLS)
898774Svbart@nginx.com         certs = nxt_cert_info_get_all(c->mem_pool);
899774Svbart@nginx.com         if (nxt_slow_path(certs == NULL)) {
900774Svbart@nginx.com             goto alloc_fail;
901774Svbart@nginx.com         }
902774Svbart@nginx.com 
903774Svbart@nginx.com         nxt_conf_set_member(value, &certificates, certs, i++);
904774Svbart@nginx.com #endif
905774Svbart@nginx.com 
906774Svbart@nginx.com         nxt_conf_set_member(value, &config, nxt_controller_conf.root, i);
907774Svbart@nginx.com 
908774Svbart@nginx.com         resp.status = 200;
909774Svbart@nginx.com         resp.conf = value;
910774Svbart@nginx.com 
911774Svbart@nginx.com         nxt_controller_response(task, req, &resp);
912774Svbart@nginx.com         return;
913774Svbart@nginx.com     }
914774Svbart@nginx.com 
915774Svbart@nginx.com     resp.status = 404;
916774Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
917774Svbart@nginx.com     resp.offset = -1;
918774Svbart@nginx.com 
919774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
920774Svbart@nginx.com     return;
921774Svbart@nginx.com 
922774Svbart@nginx.com invalid_method:
923774Svbart@nginx.com 
924774Svbart@nginx.com     resp.status = 405;
925774Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
926774Svbart@nginx.com     resp.offset = -1;
927774Svbart@nginx.com 
928774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
929774Svbart@nginx.com     return;
930774Svbart@nginx.com 
931774Svbart@nginx.com alloc_fail:
932774Svbart@nginx.com 
933774Svbart@nginx.com     resp.status = 500;
934774Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
935774Svbart@nginx.com     resp.offset = -1;
936774Svbart@nginx.com 
937774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
938774Svbart@nginx.com     return;
939774Svbart@nginx.com }
940774Svbart@nginx.com 
941774Svbart@nginx.com 
942774Svbart@nginx.com static void
943774Svbart@nginx.com nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req,
944774Svbart@nginx.com     nxt_str_t *path)
945774Svbart@nginx.com {
94665Sigor@sysoev.ru     nxt_mp_t                   *mp;
94751Svbart@nginx.com     nxt_int_t                  rc;
948140Svbart@nginx.com     nxt_conn_t                 *c;
9491049Svbart@nginx.com     nxt_bool_t                 post;
95051Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
951106Svbart@nginx.com     nxt_conf_op_t              *ops;
952106Svbart@nginx.com     nxt_conf_value_t           *value;
953357Svbart@nginx.com     nxt_conf_validation_t      vldt;
954208Svbart@nginx.com     nxt_conf_json_error_t      error;
95544Svbart@nginx.com     nxt_controller_response_t  resp;
95644Svbart@nginx.com 
95751Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
95851Svbart@nginx.com 
959774Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
960632Svbart@nginx.com 
961774Svbart@nginx.com     c = req->conn;
96244Svbart@nginx.com 
96344Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
96446Svbart@nginx.com 
965774Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, path);
96651Svbart@nginx.com 
96751Svbart@nginx.com         if (value == NULL) {
968208Svbart@nginx.com             goto not_found;
96951Svbart@nginx.com         }
97051Svbart@nginx.com 
971208Svbart@nginx.com         resp.status = 200;
972106Svbart@nginx.com         resp.conf = value;
97346Svbart@nginx.com 
974208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
975208Svbart@nginx.com         return;
97651Svbart@nginx.com     }
97751Svbart@nginx.com 
9781049Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "POST", 4)) {
9791049Svbart@nginx.com         if (path->length == 1) {
9801049Svbart@nginx.com             goto not_allowed;
9811049Svbart@nginx.com         }
9821049Svbart@nginx.com 
9831049Svbart@nginx.com         post = 1;
9841049Svbart@nginx.com 
9851049Svbart@nginx.com     } else {
9861049Svbart@nginx.com         post = 0;
9871049Svbart@nginx.com     }
9881049Svbart@nginx.com 
9891049Svbart@nginx.com     if (post || nxt_str_eq(&req->parser.method, "PUT", 3)) {
99046Svbart@nginx.com 
991*1470Smax.romanov@nginx.com         if (nxt_controller_check_postpone_request(task)) {
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 not_allowed;
104951