xref: /unit/src/nxt_controller.c (revision 1013)
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);
50249Svbart@nginx.com static nxt_int_t nxt_controller_conf_send(nxt_task_t *task,
51249Svbart@nginx.com     nxt_conf_value_t *conf, nxt_port_rpc_handler_t handler, void *data);
52248Svbart@nginx.com 
5320Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
5420Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
5562Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
5620Sigor@sysoev.ru     uintptr_t data);
5720Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
5820Sigor@sysoev.ru     void *data);
5920Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
6020Sigor@sysoev.ru     void *data);
6127Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
6227Svbart@nginx.com     void *data);
6327Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
6427Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
6527Svbart@nginx.com     void *data);
6627Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
6727Svbart@nginx.com     void *data);
6820Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
6920Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
7020Sigor@sysoev.ru 
7127Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
72417Svbart@nginx.com     nxt_http_field_t *field, uintptr_t data);
7327Svbart@nginx.com 
7427Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
75140Svbart@nginx.com     nxt_controller_request_t *req);
76774Svbart@nginx.com static void nxt_controller_process_config(nxt_task_t *task,
77774Svbart@nginx.com     nxt_controller_request_t *req, nxt_str_t *path);
78774Svbart@nginx.com #if (NXT_TLS)
79774Svbart@nginx.com static void nxt_controller_process_cert(nxt_task_t *task,
80774Svbart@nginx.com     nxt_controller_request_t *req, nxt_str_t *path);
81774Svbart@nginx.com static void nxt_controller_process_cert_save(nxt_task_t *task,
82774Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
83774Svbart@nginx.com static nxt_bool_t nxt_controller_cert_in_use(nxt_str_t *name);
84774Svbart@nginx.com #endif
85238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task,
86238Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
87314Svbart@nginx.com static void nxt_controller_conf_store(nxt_task_t *task,
88314Svbart@nginx.com     nxt_conf_value_t *conf);
89140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
90140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
91208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
92208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
9327Svbart@nginx.com 
9427Svbart@nginx.com 
95417Svbart@nginx.com static nxt_http_field_proc_t  nxt_controller_request_fields[] = {
9627Svbart@nginx.com     { nxt_string("Content-Length"),
9727Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
9827Svbart@nginx.com };
9927Svbart@nginx.com 
100417Svbart@nginx.com static nxt_lvlhsh_t            nxt_controller_fields_hash;
10127Svbart@nginx.com 
102314Svbart@nginx.com static nxt_uint_t              nxt_controller_listening;
103662Smax.romanov@nginx.com static nxt_uint_t              nxt_controller_router_ready;
104238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
105238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
10627Svbart@nginx.com 
10720Sigor@sysoev.ru 
10820Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
10927Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
11027Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
11120Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
11220Sigor@sysoev.ru 
11320Sigor@sysoev.ru 
114320Smax.romanov@nginx.com nxt_port_handlers_t  nxt_controller_process_port_handlers = {
115662Smax.romanov@nginx.com     .quit           = nxt_worker_process_quit_handler,
116662Smax.romanov@nginx.com     .new_port       = nxt_controller_process_new_port_handler,
117662Smax.romanov@nginx.com     .change_file    = nxt_port_change_log_file_handler,
118662Smax.romanov@nginx.com     .mmap           = nxt_port_mmap_handler,
119662Smax.romanov@nginx.com     .process_ready  = nxt_controller_router_ready_handler,
120662Smax.romanov@nginx.com     .data           = nxt_port_data_handler,
121662Smax.romanov@nginx.com     .remove_pid     = nxt_port_remove_pid_handler,
122662Smax.romanov@nginx.com     .rpc_ready      = nxt_port_rpc_handler,
123662Smax.romanov@nginx.com     .rpc_error      = nxt_port_rpc_handler,
124248Svbart@nginx.com };
125248Svbart@nginx.com 
126248Svbart@nginx.com 
12720Sigor@sysoev.ru nxt_int_t
128141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
12920Sigor@sysoev.ru {
130417Svbart@nginx.com     nxt_mp_t               *mp;
131417Svbart@nginx.com     nxt_int_t              ret;
132417Svbart@nginx.com     nxt_str_t              *json;
133417Svbart@nginx.com     nxt_runtime_t          *rt;
134417Svbart@nginx.com     nxt_conf_value_t       *conf;
135417Svbart@nginx.com     nxt_conf_validation_t  vldt;
136774Svbart@nginx.com     nxt_controller_init_t  *init;
13727Svbart@nginx.com 
138141Smax.romanov@nginx.com     rt = task->thread->runtime;
139141Smax.romanov@nginx.com 
140417Svbart@nginx.com     ret = nxt_http_fields_hash(&nxt_controller_fields_hash, rt->mem_pool,
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)) {
187*1013Smax.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)) {
251249Svbart@nginx.com             return;
252249Svbart@nginx.com         }
253249Svbart@nginx.com 
254249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
255249Svbart@nginx.com 
256249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
257249Svbart@nginx.com             nxt_abort();
258249Svbart@nginx.com         }
25944Svbart@nginx.com     }
26044Svbart@nginx.com 
261249Svbart@nginx.com     if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
262248Svbart@nginx.com         nxt_abort();
26344Svbart@nginx.com     }
26444Svbart@nginx.com 
265248Svbart@nginx.com     rt = task->thread->runtime;
266140Svbart@nginx.com 
267248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
268248Svbart@nginx.com         nxt_abort();
269248Svbart@nginx.com     }
270314Svbart@nginx.com 
271314Svbart@nginx.com     nxt_controller_listening = 1;
27220Sigor@sysoev.ru }
27320Sigor@sysoev.ru 
27420Sigor@sysoev.ru 
275662Smax.romanov@nginx.com static void
276662Smax.romanov@nginx.com nxt_controller_router_ready_handler(nxt_task_t *task,
277662Smax.romanov@nginx.com     nxt_port_recv_msg_t *msg)
278662Smax.romanov@nginx.com {
279662Smax.romanov@nginx.com     nxt_port_t     *router_port;
280662Smax.romanov@nginx.com     nxt_runtime_t  *rt;
281662Smax.romanov@nginx.com 
282662Smax.romanov@nginx.com     rt = task->thread->runtime;
283662Smax.romanov@nginx.com 
284662Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
285662Smax.romanov@nginx.com 
286662Smax.romanov@nginx.com     nxt_controller_router_ready = 1;
287662Smax.romanov@nginx.com 
288662Smax.romanov@nginx.com     if (router_port != NULL) {
289662Smax.romanov@nginx.com         nxt_controller_send_current_conf(task);
290662Smax.romanov@nginx.com     }
291662Smax.romanov@nginx.com }
292662Smax.romanov@nginx.com 
293662Smax.romanov@nginx.com 
294249Svbart@nginx.com static nxt_int_t
295249Svbart@nginx.com nxt_controller_conf_default(void)
296249Svbart@nginx.com {
297249Svbart@nginx.com     nxt_mp_t          *mp;
298249Svbart@nginx.com     nxt_conf_value_t  *conf;
299249Svbart@nginx.com 
300249Svbart@nginx.com     static const nxt_str_t json
301249Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
302249Svbart@nginx.com 
303249Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
304249Svbart@nginx.com 
305249Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
306249Svbart@nginx.com         return NXT_ERROR;
307249Svbart@nginx.com     }
308249Svbart@nginx.com 
309249Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
310249Svbart@nginx.com 
311249Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
312249Svbart@nginx.com         return NXT_ERROR;
313249Svbart@nginx.com     }
314249Svbart@nginx.com 
315249Svbart@nginx.com     nxt_controller_conf.root = conf;
316249Svbart@nginx.com     nxt_controller_conf.pool = mp;
317249Svbart@nginx.com 
318249Svbart@nginx.com     return NXT_OK;
319249Svbart@nginx.com }
320249Svbart@nginx.com 
321249Svbart@nginx.com 
322249Svbart@nginx.com static void
323249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
324249Svbart@nginx.com     void *data)
325249Svbart@nginx.com {
326314Svbart@nginx.com     nxt_runtime_t  *rt;
327314Svbart@nginx.com 
328249Svbart@nginx.com     if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) {
329564Svbart@nginx.com         nxt_alert(task, "failed to apply previous configuration");
330314Svbart@nginx.com 
331249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
332249Svbart@nginx.com 
333249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
334249Svbart@nginx.com             nxt_abort();
335249Svbart@nginx.com         }
336249Svbart@nginx.com     }
337314Svbart@nginx.com 
338314Svbart@nginx.com     if (nxt_controller_listening == 0) {
339314Svbart@nginx.com         rt = task->thread->runtime;
340314Svbart@nginx.com 
341314Svbart@nginx.com         if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket)
342314Svbart@nginx.com                           == NULL))
343314Svbart@nginx.com         {
344314Svbart@nginx.com             nxt_abort();
345314Svbart@nginx.com         }
346314Svbart@nginx.com 
347314Svbart@nginx.com         nxt_controller_listening = 1;
348314Svbart@nginx.com     }
349249Svbart@nginx.com }
350249Svbart@nginx.com 
351249Svbart@nginx.com 
352249Svbart@nginx.com static nxt_int_t
353249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf,
354249Svbart@nginx.com     nxt_port_rpc_handler_t handler, void *data)
355249Svbart@nginx.com {
356249Svbart@nginx.com     size_t         size;
357249Svbart@nginx.com     uint32_t       stream;
358249Svbart@nginx.com     nxt_int_t      rc;
359249Svbart@nginx.com     nxt_buf_t      *b;
360249Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
361249Svbart@nginx.com     nxt_runtime_t  *rt;
362249Svbart@nginx.com 
363249Svbart@nginx.com     rt = task->thread->runtime;
364249Svbart@nginx.com 
365249Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
366249Svbart@nginx.com 
367662Smax.romanov@nginx.com     if (nxt_slow_path(router_port == NULL || !nxt_controller_router_ready)) {
368249Svbart@nginx.com         return NXT_DECLINED;
369249Svbart@nginx.com     }
370249Svbart@nginx.com 
371249Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
372249Svbart@nginx.com 
373249Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
374249Svbart@nginx.com 
375249Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
376379Smax.romanov@nginx.com     if (nxt_slow_path(b == NULL)) {
377379Smax.romanov@nginx.com         return NXT_ERROR;
378379Smax.romanov@nginx.com     }
379249Svbart@nginx.com 
380249Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
381249Svbart@nginx.com 
382249Svbart@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
383249Svbart@nginx.com                                            handler, handler,
384249Svbart@nginx.com                                            router_port->pid, data);
385249Svbart@nginx.com 
386645Svbart@nginx.com     if (nxt_slow_path(stream == 0)) {
387645Svbart@nginx.com         return NXT_ERROR;
388645Svbart@nginx.com     }
389645Svbart@nginx.com 
390249Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
391249Svbart@nginx.com                                stream, controller_port->id, b);
392249Svbart@nginx.com 
393249Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
394249Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
395249Svbart@nginx.com         return NXT_ERROR;
396249Svbart@nginx.com     }
397249Svbart@nginx.com 
398249Svbart@nginx.com     return NXT_OK;
399249Svbart@nginx.com }
400249Svbart@nginx.com 
401249Svbart@nginx.com 
40220Sigor@sysoev.ru nxt_int_t
40320Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
40420Sigor@sysoev.ru {
40520Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
40620Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
40720Sigor@sysoev.ru 
40820Sigor@sysoev.ru     sa = rt->controller_listen;
40920Sigor@sysoev.ru 
41065Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
41120Sigor@sysoev.ru     if (ls == NULL) {
41220Sigor@sysoev.ru         return NXT_ERROR;
41320Sigor@sysoev.ru     }
41420Sigor@sysoev.ru 
41520Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
41620Sigor@sysoev.ru                                        sa->socklen, sa->length);
41720Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
41820Sigor@sysoev.ru         return NXT_ERROR;
41920Sigor@sysoev.ru     }
42020Sigor@sysoev.ru 
42120Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
422312Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
42320Sigor@sysoev.ru 
424359Sigor@sysoev.ru     nxt_listen_socket_remote_size(ls);
42520Sigor@sysoev.ru 
42620Sigor@sysoev.ru     ls->socket = -1;
42720Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
42820Sigor@sysoev.ru     ls->read_after_accept = 1;
42920Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
43020Sigor@sysoev.ru 
43120Sigor@sysoev.ru #if 0
43220Sigor@sysoev.ru     /* STUB */
43365Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
43420Sigor@sysoev.ru     if (wq == NULL) {
43520Sigor@sysoev.ru         return NXT_ERROR;
43620Sigor@sysoev.ru     }
43720Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
43820Sigor@sysoev.ru     /**/
43920Sigor@sysoev.ru 
44020Sigor@sysoev.ru     ls->work_queue = wq;
44120Sigor@sysoev.ru #endif
44220Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
44320Sigor@sysoev.ru 
44420Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
44520Sigor@sysoev.ru         return NXT_ERROR;
44620Sigor@sysoev.ru     }
44720Sigor@sysoev.ru 
44820Sigor@sysoev.ru     rt->controller_socket = ls;
44920Sigor@sysoev.ru 
45020Sigor@sysoev.ru     return NXT_OK;
45120Sigor@sysoev.ru }
45220Sigor@sysoev.ru 
45320Sigor@sysoev.ru 
45420Sigor@sysoev.ru static void
45520Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
45620Sigor@sysoev.ru {
45727Svbart@nginx.com     nxt_buf_t                 *b;
45862Sigor@sysoev.ru     nxt_conn_t                *c;
45927Svbart@nginx.com     nxt_event_engine_t        *engine;
46027Svbart@nginx.com     nxt_controller_request_t  *r;
46120Sigor@sysoev.ru 
46220Sigor@sysoev.ru     c = obj;
46320Sigor@sysoev.ru 
46420Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
46520Sigor@sysoev.ru 
46665Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
46727Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
46827Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
46927Svbart@nginx.com         return;
47027Svbart@nginx.com     }
47127Svbart@nginx.com 
472140Svbart@nginx.com     r->conn = c;
473140Svbart@nginx.com 
47460Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
47560Svbart@nginx.com                       != NXT_OK))
47660Svbart@nginx.com     {
47760Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
47860Svbart@nginx.com         return;
47960Svbart@nginx.com     }
48027Svbart@nginx.com 
48120Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
48220Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
48320Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
48420Sigor@sysoev.ru         return;
48520Sigor@sysoev.ru     }
48620Sigor@sysoev.ru 
48720Sigor@sysoev.ru     c->read = b;
48827Svbart@nginx.com     c->socket.data = r;
48920Sigor@sysoev.ru     c->socket.read_ready = 1;
49020Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
49120Sigor@sysoev.ru 
49220Sigor@sysoev.ru     engine = task->thread->engine;
49320Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
49427Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
49520Sigor@sysoev.ru 
49662Sigor@sysoev.ru     nxt_conn_read(engine, c);
49720Sigor@sysoev.ru }
49820Sigor@sysoev.ru 
49920Sigor@sysoev.ru 
50020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
50120Sigor@sysoev.ru     nxt_aligned(64) =
50220Sigor@sysoev.ru {
50356Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
50456Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
50556Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
50620Sigor@sysoev.ru 
50756Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
50856Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
50956Sigor@sysoev.ru     .timer_data = 60 * 1000,
51020Sigor@sysoev.ru };
51120Sigor@sysoev.ru 
51220Sigor@sysoev.ru 
51320Sigor@sysoev.ru static void
51420Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
51520Sigor@sysoev.ru {
51627Svbart@nginx.com     size_t                    preread;
51727Svbart@nginx.com     nxt_buf_t                 *b;
51827Svbart@nginx.com     nxt_int_t                 rc;
51962Sigor@sysoev.ru     nxt_conn_t                *c;
52027Svbart@nginx.com     nxt_controller_request_t  *r;
52120Sigor@sysoev.ru 
52220Sigor@sysoev.ru     c = obj;
52327Svbart@nginx.com     r = data;
52420Sigor@sysoev.ru 
52520Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
52620Sigor@sysoev.ru 
52727Svbart@nginx.com     nxt_queue_remove(&c->link);
52827Svbart@nginx.com     nxt_queue_self(&c->link);
52927Svbart@nginx.com 
53027Svbart@nginx.com     b = c->read;
53127Svbart@nginx.com 
53227Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
53327Svbart@nginx.com 
53427Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
53527Svbart@nginx.com 
53627Svbart@nginx.com         if (rc == NXT_AGAIN) {
53727Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
53827Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
53927Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
54027Svbart@nginx.com                 return;
54127Svbart@nginx.com             }
54227Svbart@nginx.com 
54362Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
54427Svbart@nginx.com             return;
54527Svbart@nginx.com         }
54627Svbart@nginx.com 
54727Svbart@nginx.com         /* rc == NXT_ERROR */
54827Svbart@nginx.com 
54927Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
55027Svbart@nginx.com 
55127Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
55227Svbart@nginx.com         return;
55327Svbart@nginx.com     }
55427Svbart@nginx.com 
555417Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash,
556417Svbart@nginx.com                                  r);
55760Svbart@nginx.com 
55860Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
55960Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
56060Svbart@nginx.com         return;
56160Svbart@nginx.com     }
56260Svbart@nginx.com 
56327Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
56427Svbart@nginx.com 
56527Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
566107Svbart@nginx.com                     "body length: %uz, preread: %uz",
56727Svbart@nginx.com                     r->length, preread);
56827Svbart@nginx.com 
56927Svbart@nginx.com     if (preread >= r->length) {
570140Svbart@nginx.com         nxt_controller_process_request(task, r);
57127Svbart@nginx.com         return;
57227Svbart@nginx.com     }
57327Svbart@nginx.com 
57427Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
57527Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
57627Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
57727Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
57827Svbart@nginx.com             return;
57927Svbart@nginx.com         }
58027Svbart@nginx.com 
58127Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
58227Svbart@nginx.com 
58327Svbart@nginx.com         c->read = b;
58427Svbart@nginx.com     }
58527Svbart@nginx.com 
58627Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
58727Svbart@nginx.com 
58862Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
58920Sigor@sysoev.ru }
59020Sigor@sysoev.ru 
59120Sigor@sysoev.ru 
59220Sigor@sysoev.ru static nxt_msec_t
59362Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
59420Sigor@sysoev.ru {
59520Sigor@sysoev.ru     return (nxt_msec_t) data;
59620Sigor@sysoev.ru }
59720Sigor@sysoev.ru 
59820Sigor@sysoev.ru 
59920Sigor@sysoev.ru static void
60020Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
60120Sigor@sysoev.ru {
60262Sigor@sysoev.ru     nxt_conn_t  *c;
60320Sigor@sysoev.ru 
60420Sigor@sysoev.ru     c = obj;
60520Sigor@sysoev.ru 
60620Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
60720Sigor@sysoev.ru 
60827Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
60920Sigor@sysoev.ru }
61020Sigor@sysoev.ru 
61120Sigor@sysoev.ru 
61220Sigor@sysoev.ru static void
61320Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
61420Sigor@sysoev.ru {
61562Sigor@sysoev.ru     nxt_timer_t  *timer;
61662Sigor@sysoev.ru     nxt_conn_t   *c;
61720Sigor@sysoev.ru 
61862Sigor@sysoev.ru     timer = obj;
61920Sigor@sysoev.ru 
62062Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
62120Sigor@sysoev.ru     c->socket.timedout = 1;
62220Sigor@sysoev.ru     c->socket.closed = 1;
62320Sigor@sysoev.ru 
62420Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
62520Sigor@sysoev.ru 
62627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
62727Svbart@nginx.com }
62827Svbart@nginx.com 
62927Svbart@nginx.com 
63027Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
63127Svbart@nginx.com     nxt_aligned(64) =
63227Svbart@nginx.com {
63356Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
63456Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
63556Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
63627Svbart@nginx.com 
63756Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
63856Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
63956Sigor@sysoev.ru     .timer_data = 60 * 1000,
64056Sigor@sysoev.ru     .timer_autoreset = 1,
64127Svbart@nginx.com };
64227Svbart@nginx.com 
64327Svbart@nginx.com 
64427Svbart@nginx.com static void
64527Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
64627Svbart@nginx.com {
647107Svbart@nginx.com     size_t                    read;
648107Svbart@nginx.com     nxt_buf_t                 *b;
649107Svbart@nginx.com     nxt_conn_t                *c;
650107Svbart@nginx.com     nxt_controller_request_t  *r;
65127Svbart@nginx.com 
65227Svbart@nginx.com     c = obj;
653107Svbart@nginx.com     r = data;
65427Svbart@nginx.com     b = c->read;
65527Svbart@nginx.com 
656107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
65727Svbart@nginx.com 
658107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
659107Svbart@nginx.com               read, r->length);
66027Svbart@nginx.com 
661107Svbart@nginx.com     if (read >= r->length) {
662140Svbart@nginx.com         nxt_controller_process_request(task, r);
66327Svbart@nginx.com         return;
66427Svbart@nginx.com     }
66527Svbart@nginx.com 
66662Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
66727Svbart@nginx.com }
66827Svbart@nginx.com 
66927Svbart@nginx.com 
67027Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
67127Svbart@nginx.com     nxt_aligned(64) =
67227Svbart@nginx.com {
67356Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
67456Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
67527Svbart@nginx.com 
67656Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
67756Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
67856Sigor@sysoev.ru     .timer_data = 60 * 1000,
67956Sigor@sysoev.ru     .timer_autoreset = 1,
68027Svbart@nginx.com };
68127Svbart@nginx.com 
68227Svbart@nginx.com 
68327Svbart@nginx.com static void
68427Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
68527Svbart@nginx.com {
68662Sigor@sysoev.ru     nxt_buf_t   *b;
68762Sigor@sysoev.ru     nxt_conn_t  *c;
68827Svbart@nginx.com 
68927Svbart@nginx.com     c = obj;
69027Svbart@nginx.com 
69127Svbart@nginx.com     nxt_debug(task, "controller conn write");
69227Svbart@nginx.com 
69327Svbart@nginx.com     b = c->write;
69427Svbart@nginx.com 
69527Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
69662Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
69727Svbart@nginx.com         return;
69827Svbart@nginx.com     }
69927Svbart@nginx.com 
70027Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
70127Svbart@nginx.com 
70227Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
70327Svbart@nginx.com }
70427Svbart@nginx.com 
70527Svbart@nginx.com 
70627Svbart@nginx.com static void
70727Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
70827Svbart@nginx.com {
70962Sigor@sysoev.ru     nxt_conn_t  *c;
71027Svbart@nginx.com 
71127Svbart@nginx.com     c = obj;
71227Svbart@nginx.com 
71327Svbart@nginx.com     nxt_debug(task, "controller conn write error");
71427Svbart@nginx.com 
71527Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
71627Svbart@nginx.com }
71727Svbart@nginx.com 
71827Svbart@nginx.com 
71927Svbart@nginx.com static void
72027Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
72127Svbart@nginx.com {
72262Sigor@sysoev.ru     nxt_conn_t   *c;
72362Sigor@sysoev.ru     nxt_timer_t  *timer;
72427Svbart@nginx.com 
72562Sigor@sysoev.ru     timer = obj;
72627Svbart@nginx.com 
72762Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
72827Svbart@nginx.com     c->socket.timedout = 1;
72927Svbart@nginx.com     c->socket.closed = 1;
73027Svbart@nginx.com 
73127Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
73227Svbart@nginx.com 
73327Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
73420Sigor@sysoev.ru }
73520Sigor@sysoev.ru 
73620Sigor@sysoev.ru 
73720Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
73820Sigor@sysoev.ru     nxt_aligned(64) =
73920Sigor@sysoev.ru {
74056Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
74120Sigor@sysoev.ru };
74220Sigor@sysoev.ru 
74320Sigor@sysoev.ru 
74420Sigor@sysoev.ru static void
74520Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
74620Sigor@sysoev.ru {
74762Sigor@sysoev.ru     nxt_conn_t  *c;
74820Sigor@sysoev.ru 
74920Sigor@sysoev.ru     c = obj;
75020Sigor@sysoev.ru 
75120Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
75220Sigor@sysoev.ru 
75327Svbart@nginx.com     nxt_queue_remove(&c->link);
75427Svbart@nginx.com 
75520Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
75620Sigor@sysoev.ru 
75762Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
75820Sigor@sysoev.ru }
75920Sigor@sysoev.ru 
76020Sigor@sysoev.ru 
76120Sigor@sysoev.ru static void
76220Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
76320Sigor@sysoev.ru {
76462Sigor@sysoev.ru     nxt_conn_t  *c;
76520Sigor@sysoev.ru 
76620Sigor@sysoev.ru     c = obj;
76720Sigor@sysoev.ru 
76820Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
76920Sigor@sysoev.ru 
770337Sigor@sysoev.ru     nxt_sockaddr_cache_free(task->thread->engine, c);
771337Sigor@sysoev.ru 
772386Sigor@sysoev.ru     nxt_conn_free(task, c);
77320Sigor@sysoev.ru }
77427Svbart@nginx.com 
77527Svbart@nginx.com 
77627Svbart@nginx.com static nxt_int_t
77760Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
778417Svbart@nginx.com     uintptr_t data)
77927Svbart@nginx.com {
78027Svbart@nginx.com     off_t                     length;
78127Svbart@nginx.com     nxt_controller_request_t  *r;
78227Svbart@nginx.com 
78327Svbart@nginx.com     r = ctx;
78427Svbart@nginx.com 
785417Svbart@nginx.com     length = nxt_off_t_parse(field->value, field->value_length);
78627Svbart@nginx.com 
787710Svbart@nginx.com     if (nxt_fast_path(length >= 0)) {
788107Svbart@nginx.com 
789107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
790417Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, &r->conn->log,
791417Svbart@nginx.com                           "Content-Length is too big");
792107Svbart@nginx.com             return NXT_ERROR;
793107Svbart@nginx.com         }
79427Svbart@nginx.com 
79527Svbart@nginx.com         r->length = length;
79627Svbart@nginx.com         return NXT_OK;
79727Svbart@nginx.com     }
79827Svbart@nginx.com 
799417Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, &r->conn->log, "Content-Length is invalid");
80027Svbart@nginx.com 
80127Svbart@nginx.com     return NXT_ERROR;
80227Svbart@nginx.com }
80327Svbart@nginx.com 
80427Svbart@nginx.com 
80527Svbart@nginx.com static void
806140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
80727Svbart@nginx.com {
808774Svbart@nginx.com     uint32_t                   i, count;
809774Svbart@nginx.com     nxt_str_t                  path;
810774Svbart@nginx.com     nxt_conn_t                 *c;
811774Svbart@nginx.com     nxt_conf_value_t           *value;
812774Svbart@nginx.com     nxt_controller_response_t  resp;
813774Svbart@nginx.com #if (NXT_TLS)
814774Svbart@nginx.com     nxt_conf_value_t           *certs;
815774Svbart@nginx.com 
816774Svbart@nginx.com     static nxt_str_t certificates = nxt_string("certificates");
817774Svbart@nginx.com #endif
818774Svbart@nginx.com     static nxt_str_t config = nxt_string("config");
819774Svbart@nginx.com 
820774Svbart@nginx.com     c = req->conn;
821774Svbart@nginx.com     path = req->parser.path;
822774Svbart@nginx.com 
823774Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
824774Svbart@nginx.com         path.length--;
825774Svbart@nginx.com     }
826774Svbart@nginx.com 
827774Svbart@nginx.com     if (nxt_str_start(&path, "/config", 7)
828774Svbart@nginx.com         && (path.length == 7 || path.start[7] == '/'))
829774Svbart@nginx.com     {
830774Svbart@nginx.com         if (path.length == 7) {
831774Svbart@nginx.com             path.length = 1;
832774Svbart@nginx.com 
833774Svbart@nginx.com         } else {
834774Svbart@nginx.com             path.length -= 7;
835774Svbart@nginx.com             path.start += 7;
836774Svbart@nginx.com         }
837774Svbart@nginx.com 
838774Svbart@nginx.com         nxt_controller_process_config(task, req, &path);
839774Svbart@nginx.com         return;
840774Svbart@nginx.com     }
841774Svbart@nginx.com 
842774Svbart@nginx.com #if (NXT_TLS)
843774Svbart@nginx.com 
844774Svbart@nginx.com     if (nxt_str_start(&path, "/certificates", 13)
845774Svbart@nginx.com         && (path.length == 13 || path.start[13] == '/'))
846774Svbart@nginx.com     {
847774Svbart@nginx.com         if (path.length == 13) {
848774Svbart@nginx.com             path.length = 1;
849774Svbart@nginx.com 
850774Svbart@nginx.com         } else {
851774Svbart@nginx.com             path.length -= 13;
852774Svbart@nginx.com             path.start += 13;
853774Svbart@nginx.com         }
854774Svbart@nginx.com 
855774Svbart@nginx.com         nxt_controller_process_cert(task, req, &path);
856774Svbart@nginx.com         return;
857774Svbart@nginx.com     }
858774Svbart@nginx.com 
859774Svbart@nginx.com #endif
860774Svbart@nginx.com 
861774Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
862774Svbart@nginx.com 
863774Svbart@nginx.com     if (path.length == 1 && path.start[0] == '/') {
864774Svbart@nginx.com 
865774Svbart@nginx.com         if (!nxt_str_eq(&req->parser.method, "GET", 3)) {
866774Svbart@nginx.com             goto invalid_method;
867774Svbart@nginx.com         }
868774Svbart@nginx.com 
869774Svbart@nginx.com         count = 1;
870774Svbart@nginx.com #if (NXT_TLS)
871774Svbart@nginx.com         count++;
872774Svbart@nginx.com #endif
873774Svbart@nginx.com 
874774Svbart@nginx.com         value = nxt_conf_create_object(c->mem_pool, count);
875774Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
876774Svbart@nginx.com             goto alloc_fail;
877774Svbart@nginx.com         }
878774Svbart@nginx.com 
879774Svbart@nginx.com         i = 0;
880774Svbart@nginx.com 
881774Svbart@nginx.com #if (NXT_TLS)
882774Svbart@nginx.com         certs = nxt_cert_info_get_all(c->mem_pool);
883774Svbart@nginx.com         if (nxt_slow_path(certs == NULL)) {
884774Svbart@nginx.com             goto alloc_fail;
885774Svbart@nginx.com         }
886774Svbart@nginx.com 
887774Svbart@nginx.com         nxt_conf_set_member(value, &certificates, certs, i++);
888774Svbart@nginx.com #endif
889774Svbart@nginx.com 
890774Svbart@nginx.com         nxt_conf_set_member(value, &config, nxt_controller_conf.root, i);
891774Svbart@nginx.com 
892774Svbart@nginx.com         resp.status = 200;
893774Svbart@nginx.com         resp.conf = value;
894774Svbart@nginx.com 
895774Svbart@nginx.com         nxt_controller_response(task, req, &resp);
896774Svbart@nginx.com         return;
897774Svbart@nginx.com     }
898774Svbart@nginx.com 
899774Svbart@nginx.com     resp.status = 404;
900774Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
901774Svbart@nginx.com     resp.offset = -1;
902774Svbart@nginx.com 
903774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
904774Svbart@nginx.com     return;
905774Svbart@nginx.com 
906774Svbart@nginx.com invalid_method:
907774Svbart@nginx.com 
908774Svbart@nginx.com     resp.status = 405;
909774Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
910774Svbart@nginx.com     resp.offset = -1;
911774Svbart@nginx.com 
912774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
913774Svbart@nginx.com     return;
914774Svbart@nginx.com 
915774Svbart@nginx.com alloc_fail:
916774Svbart@nginx.com 
917774Svbart@nginx.com     resp.status = 500;
918774Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
919774Svbart@nginx.com     resp.offset = -1;
920774Svbart@nginx.com 
921774Svbart@nginx.com     nxt_controller_response(task, req, &resp);
922774Svbart@nginx.com     return;
923774Svbart@nginx.com }
924774Svbart@nginx.com 
925774Svbart@nginx.com 
926774Svbart@nginx.com static void
927774Svbart@nginx.com nxt_controller_process_config(nxt_task_t *task, nxt_controller_request_t *req,
928774Svbart@nginx.com     nxt_str_t *path)
929774Svbart@nginx.com {
93065Sigor@sysoev.ru     nxt_mp_t                   *mp;
93151Svbart@nginx.com     nxt_int_t                  rc;
932140Svbart@nginx.com     nxt_conn_t                 *c;
93351Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
934106Svbart@nginx.com     nxt_conf_op_t              *ops;
935106Svbart@nginx.com     nxt_conf_value_t           *value;
936357Svbart@nginx.com     nxt_conf_validation_t      vldt;
937208Svbart@nginx.com     nxt_conf_json_error_t      error;
93844Svbart@nginx.com     nxt_controller_response_t  resp;
93944Svbart@nginx.com 
94051Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
94151Svbart@nginx.com 
942774Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
943632Svbart@nginx.com 
944774Svbart@nginx.com     c = req->conn;
94544Svbart@nginx.com 
94644Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
94746Svbart@nginx.com 
948774Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, path);
94951Svbart@nginx.com 
95051Svbart@nginx.com         if (value == NULL) {
951208Svbart@nginx.com             goto not_found;
95251Svbart@nginx.com         }
95351Svbart@nginx.com 
954208Svbart@nginx.com         resp.status = 200;
955106Svbart@nginx.com         resp.conf = value;
95646Svbart@nginx.com 
957208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
958208Svbart@nginx.com         return;
95951Svbart@nginx.com     }
96051Svbart@nginx.com 
96151Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
96246Svbart@nginx.com 
963238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
964238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
965238Svbart@nginx.com             return;
966238Svbart@nginx.com         }
967238Svbart@nginx.com 
96865Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
96951Svbart@nginx.com 
97051Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
971208Svbart@nginx.com             goto alloc_fail;
97246Svbart@nginx.com         }
97346Svbart@nginx.com 
97451Svbart@nginx.com         mbuf = &c->read->mem;
97551Svbart@nginx.com 
976208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
977208Svbart@nginx.com 
978208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
97951Svbart@nginx.com 
98051Svbart@nginx.com         if (value == NULL) {
98165Sigor@sysoev.ru             nxt_mp_destroy(mp);
982208Svbart@nginx.com 
983208Svbart@nginx.com             if (error.pos == NULL) {
984208Svbart@nginx.com                 goto alloc_fail;
985208Svbart@nginx.com             }
986208Svbart@nginx.com 
987208Svbart@nginx.com             resp.status = 400;
988208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
989357Svbart@nginx.com             resp.detail.length = nxt_strlen(error.detail);
990357Svbart@nginx.com             resp.detail.start = error.detail;
991208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
992208Svbart@nginx.com 
993208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
994208Svbart@nginx.com                                    &resp.line, &resp.column);
995208Svbart@nginx.com 
996208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
997208Svbart@nginx.com             return;
99851Svbart@nginx.com         }
99951Svbart@nginx.com 
1000774Svbart@nginx.com         if (path->length != 1) {
1001106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
1002106Svbart@nginx.com                                      nxt_controller_conf.root,
1003774Svbart@nginx.com                                      path, value);
100446Svbart@nginx.com 
100551Svbart@nginx.com             if (rc != NXT_OK) {
1006619Svbart@nginx.com                 nxt_mp_destroy(mp);
1007619Svbart@nginx.com 
100851Svbart@nginx.com                 if (rc == NXT_DECLINED) {
1009208Svbart@nginx.com                     goto not_found;
101051Svbart@nginx.com                 }
101146Svbart@nginx.com 
1012208Svbart@nginx.com                 goto alloc_fail;
101351Svbart@nginx.com             }
101451Svbart@nginx.com 
1015106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
101651Svbart@nginx.com 
101751Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
101865Sigor@sysoev.ru                 nxt_mp_destroy(mp);
1019208Svbart@nginx.com                 goto alloc_fail;
102051Svbart@nginx.com             }
102146Svbart@nginx.com         }
102244Svbart@nginx.com 
1023357Svbart@nginx.com         nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
1024357Svbart@nginx.com 
1025357Svbart@nginx.com         vldt.conf = value;
1026357Svbart@nginx.com         vldt.pool = c->mem_pool;
1027357Svbart@nginx.com 
1028357Svbart@nginx.com         rc = nxt_conf_validate(&vldt);
1029357Svbart@nginx.com 
1030357Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
1031121Svbart@nginx.com             nxt_mp_destroy(mp);
1032357Svbart@nginx.com 
1033357Svbart@nginx.com             if (rc == NXT_DECLINED) {
1034357Svbart@nginx.com                 resp.detail = vldt.error;
1035357Svbart@nginx.com                 goto invalid_conf;
1036357Svbart@nginx.com             }
1037357Svbart@nginx.com 
1038357Svbart@nginx.com             /* rc == NXT_ERROR */
1039357Svbart@nginx.com             goto alloc_fail;
1040116Svbart@nginx.com         }
1041116Svbart@nginx.com 
1042249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
1043249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
1044247Svbart@nginx.com 
1045247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
1046121Svbart@nginx.com             nxt_mp_destroy(mp);
1047247Svbart@nginx.com 
1048247Svbart@nginx.com             if (rc == NXT_DECLINED) {
1049247Svbart@nginx.com                 goto no_router;
1050247Svbart@nginx.com             }
1051247Svbart@nginx.com 
1052247Svbart@nginx.com             /* rc == NXT_ERROR */
1053