xref: /unit/src/nxt_controller.c (revision 312)
120Sigor@sysoev.ru 
220Sigor@sysoev.ru /*
320Sigor@sysoev.ru  * Copyright (C) Igor Sysoev
420Sigor@sysoev.ru  * Copyright (C) Valentin V. Bartenev
520Sigor@sysoev.ru  * Copyright (C) NGINX, Inc.
620Sigor@sysoev.ru  */
720Sigor@sysoev.ru 
820Sigor@sysoev.ru #include <nxt_main.h>
920Sigor@sysoev.ru #include <nxt_runtime.h>
10240Sigor@sysoev.ru #include <nxt_main_process.h>
1129Svbart@nginx.com #include <nxt_conf.h>
1220Sigor@sysoev.ru 
1320Sigor@sysoev.ru 
1427Svbart@nginx.com typedef struct {
15106Svbart@nginx.com     nxt_conf_value_t  *root;
16106Svbart@nginx.com     nxt_mp_t          *pool;
1744Svbart@nginx.com } nxt_controller_conf_t;
1844Svbart@nginx.com 
1944Svbart@nginx.com 
2044Svbart@nginx.com typedef struct {
2127Svbart@nginx.com     nxt_http_request_parse_t  parser;
2227Svbart@nginx.com     size_t                    length;
2344Svbart@nginx.com     nxt_controller_conf_t     conf;
24140Svbart@nginx.com     nxt_conn_t                *conn;
25140Svbart@nginx.com     nxt_queue_link_t          link;
2627Svbart@nginx.com } nxt_controller_request_t;
2727Svbart@nginx.com 
2827Svbart@nginx.com 
2944Svbart@nginx.com typedef struct {
30208Svbart@nginx.com     nxt_uint_t        status;
31106Svbart@nginx.com     nxt_conf_value_t  *conf;
32208Svbart@nginx.com 
33208Svbart@nginx.com     u_char            *title;
34208Svbart@nginx.com     u_char            *detail;
35208Svbart@nginx.com     ssize_t           offset;
36208Svbart@nginx.com     nxt_uint_t        line;
37208Svbart@nginx.com     nxt_uint_t        column;
3844Svbart@nginx.com } nxt_controller_response_t;
3944Svbart@nginx.com 
4044Svbart@nginx.com 
41248Svbart@nginx.com static void nxt_controller_process_new_port_handler(nxt_task_t *task,
42248Svbart@nginx.com     nxt_port_recv_msg_t *msg);
43249Svbart@nginx.com static nxt_int_t nxt_controller_conf_default(void);
44249Svbart@nginx.com static void nxt_controller_conf_init_handler(nxt_task_t *task,
45249Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
46249Svbart@nginx.com static nxt_int_t nxt_controller_conf_send(nxt_task_t *task,
47249Svbart@nginx.com     nxt_conf_value_t *conf, nxt_port_rpc_handler_t handler, void *data);
48248Svbart@nginx.com 
4920Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
5020Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
5162Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
5220Sigor@sysoev.ru     uintptr_t data);
5320Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
5420Sigor@sysoev.ru     void *data);
5520Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
5620Sigor@sysoev.ru     void *data);
5727Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
5827Svbart@nginx.com     void *data);
5927Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
6027Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
6127Svbart@nginx.com     void *data);
6227Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
6327Svbart@nginx.com     void *data);
6420Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
6520Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
6620Sigor@sysoev.ru 
6727Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
6867Svbart@nginx.com     nxt_http_field_t *field, nxt_log_t *log);
6927Svbart@nginx.com 
7027Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
71140Svbart@nginx.com     nxt_controller_request_t *req);
72238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task,
73238Svbart@nginx.com     nxt_port_recv_msg_t *msg, void *data);
74140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
75140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
76208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
77208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
7827Svbart@nginx.com 
7927Svbart@nginx.com 
8060Svbart@nginx.com static nxt_http_fields_hash_entry_t  nxt_controller_request_fields[] = {
8127Svbart@nginx.com     { nxt_string("Content-Length"),
8227Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
8327Svbart@nginx.com 
8427Svbart@nginx.com     { nxt_null_string, NULL, 0 }
8527Svbart@nginx.com };
8627Svbart@nginx.com 
8760Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_fields_hash;
8827Svbart@nginx.com 
89238Svbart@nginx.com static nxt_controller_conf_t   nxt_controller_conf;
90238Svbart@nginx.com static nxt_queue_t             nxt_controller_waiting_requests;
9127Svbart@nginx.com 
9220Sigor@sysoev.ru 
9320Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
9427Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
9527Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
9620Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
9720Sigor@sysoev.ru 
9820Sigor@sysoev.ru 
99248Svbart@nginx.com nxt_port_handler_t  nxt_controller_process_port_handlers[] = {
100248Svbart@nginx.com     nxt_worker_process_quit_handler,
101248Svbart@nginx.com     nxt_controller_process_new_port_handler,
102248Svbart@nginx.com     nxt_port_change_log_file_handler,
103248Svbart@nginx.com     nxt_port_mmap_handler,
104248Svbart@nginx.com     nxt_port_data_handler,
105248Svbart@nginx.com     nxt_port_remove_pid_handler,
106248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_READY        */
107248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_START_WORKER */
108248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_SOCKET       */
109248Svbart@nginx.com     NULL, /* NXT_PORT_MSG_MODULES      */
110248Svbart@nginx.com     nxt_port_rpc_handler,
111248Svbart@nginx.com     nxt_port_rpc_handler,
112248Svbart@nginx.com };
113248Svbart@nginx.com 
114248Svbart@nginx.com 
11520Sigor@sysoev.ru nxt_int_t
116141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
11720Sigor@sysoev.ru {
118141Smax.romanov@nginx.com     nxt_runtime_t           *rt;
11927Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
12027Svbart@nginx.com 
121141Smax.romanov@nginx.com     rt = task->thread->runtime;
122141Smax.romanov@nginx.com 
12360Svbart@nginx.com     hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
12460Svbart@nginx.com                                        rt->mem_pool);
12527Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
12627Svbart@nginx.com         return NXT_ERROR;
12727Svbart@nginx.com     }
12827Svbart@nginx.com 
12960Svbart@nginx.com     nxt_controller_fields_hash = hash;
130248Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
13127Svbart@nginx.com 
132248Svbart@nginx.com     return NXT_OK;
133248Svbart@nginx.com }
134248Svbart@nginx.com 
135248Svbart@nginx.com 
136248Svbart@nginx.com static void
137248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task,
138248Svbart@nginx.com     nxt_port_recv_msg_t *msg)
139248Svbart@nginx.com {
140249Svbart@nginx.com     nxt_int_t         rc;
141248Svbart@nginx.com     nxt_runtime_t     *rt;
142248Svbart@nginx.com     nxt_conf_value_t  *conf;
143248Svbart@nginx.com 
144248Svbart@nginx.com     nxt_port_new_port_handler(task, msg);
145248Svbart@nginx.com 
146249Svbart@nginx.com     if (msg->new_port->type != NXT_PROCESS_ROUTER) {
147248Svbart@nginx.com         return;
14820Sigor@sysoev.ru     }
14920Sigor@sysoev.ru 
150249Svbart@nginx.com     conf = nxt_controller_conf.root;
151249Svbart@nginx.com 
152249Svbart@nginx.com     if (conf != NULL) {
153249Svbart@nginx.com         rc = nxt_controller_conf_send(task, conf,
154249Svbart@nginx.com                                       nxt_controller_conf_init_handler, NULL);
15544Svbart@nginx.com 
156249Svbart@nginx.com         if (nxt_fast_path(rc == NXT_OK)) {
157249Svbart@nginx.com             return;
158249Svbart@nginx.com         }
159249Svbart@nginx.com 
160249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
161249Svbart@nginx.com 
162249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
163249Svbart@nginx.com             nxt_abort();
164249Svbart@nginx.com         }
16544Svbart@nginx.com     }
16644Svbart@nginx.com 
167249Svbart@nginx.com     if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
168248Svbart@nginx.com         nxt_abort();
16944Svbart@nginx.com     }
17044Svbart@nginx.com 
171248Svbart@nginx.com     rt = task->thread->runtime;
172140Svbart@nginx.com 
173248Svbart@nginx.com     if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) {
174248Svbart@nginx.com         nxt_abort();
175248Svbart@nginx.com     }
17620Sigor@sysoev.ru }
17720Sigor@sysoev.ru 
17820Sigor@sysoev.ru 
179249Svbart@nginx.com static nxt_int_t
180249Svbart@nginx.com nxt_controller_conf_default(void)
181249Svbart@nginx.com {
182249Svbart@nginx.com     nxt_mp_t          *mp;
183249Svbart@nginx.com     nxt_conf_value_t  *conf;
184249Svbart@nginx.com 
185249Svbart@nginx.com     static const nxt_str_t json
186249Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
187249Svbart@nginx.com 
188249Svbart@nginx.com     mp = nxt_mp_create(1024, 128, 256, 32);
189249Svbart@nginx.com 
190249Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
191249Svbart@nginx.com         return NXT_ERROR;
192249Svbart@nginx.com     }
193249Svbart@nginx.com 
194249Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
195249Svbart@nginx.com 
196249Svbart@nginx.com     if (nxt_slow_path(conf == NULL)) {
197249Svbart@nginx.com         return NXT_ERROR;
198249Svbart@nginx.com     }
199249Svbart@nginx.com 
200249Svbart@nginx.com     nxt_controller_conf.root = conf;
201249Svbart@nginx.com     nxt_controller_conf.pool = mp;
202249Svbart@nginx.com 
203249Svbart@nginx.com     return NXT_OK;
204249Svbart@nginx.com }
205249Svbart@nginx.com 
206249Svbart@nginx.com 
207249Svbart@nginx.com static void
208249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
209249Svbart@nginx.com     void *data)
210249Svbart@nginx.com {
211249Svbart@nginx.com     if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) {
212249Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
213249Svbart@nginx.com 
214249Svbart@nginx.com         if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) {
215249Svbart@nginx.com             nxt_abort();
216249Svbart@nginx.com         }
217249Svbart@nginx.com     }
218249Svbart@nginx.com }
219249Svbart@nginx.com 
220249Svbart@nginx.com 
221249Svbart@nginx.com static nxt_int_t
222249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf,
223249Svbart@nginx.com     nxt_port_rpc_handler_t handler, void *data)
224249Svbart@nginx.com {
225249Svbart@nginx.com     size_t         size;
226249Svbart@nginx.com     uint32_t       stream;
227249Svbart@nginx.com     nxt_int_t      rc;
228249Svbart@nginx.com     nxt_buf_t      *b;
229249Svbart@nginx.com     nxt_port_t     *router_port, *controller_port;
230249Svbart@nginx.com     nxt_runtime_t  *rt;
231249Svbart@nginx.com 
232249Svbart@nginx.com     rt = task->thread->runtime;
233249Svbart@nginx.com 
234249Svbart@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
235249Svbart@nginx.com 
236249Svbart@nginx.com     if (nxt_slow_path(router_port == NULL)) {
237249Svbart@nginx.com         return NXT_DECLINED;
238249Svbart@nginx.com     }
239249Svbart@nginx.com 
240249Svbart@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
241249Svbart@nginx.com 
242249Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
243249Svbart@nginx.com 
244249Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
245249Svbart@nginx.com 
246249Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
247249Svbart@nginx.com 
248249Svbart@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
249249Svbart@nginx.com                                            handler, handler,
250249Svbart@nginx.com                                            router_port->pid, data);
251249Svbart@nginx.com 
252249Svbart@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
253249Svbart@nginx.com                                stream, controller_port->id, b);
254249Svbart@nginx.com 
255249Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
256249Svbart@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
257249Svbart@nginx.com         return NXT_ERROR;
258249Svbart@nginx.com     }
259249Svbart@nginx.com 
260249Svbart@nginx.com     return NXT_OK;
261249Svbart@nginx.com }
262249Svbart@nginx.com 
263249Svbart@nginx.com 
26420Sigor@sysoev.ru nxt_int_t
26520Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
26620Sigor@sysoev.ru {
26720Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
26820Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
26920Sigor@sysoev.ru 
27020Sigor@sysoev.ru     sa = rt->controller_listen;
27120Sigor@sysoev.ru 
27265Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
27320Sigor@sysoev.ru     if (ls == NULL) {
27420Sigor@sysoev.ru         return NXT_ERROR;
27520Sigor@sysoev.ru     }
27620Sigor@sysoev.ru 
27720Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
27820Sigor@sysoev.ru                                        sa->socklen, sa->length);
27920Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
28020Sigor@sysoev.ru         return NXT_ERROR;
28120Sigor@sysoev.ru     }
28220Sigor@sysoev.ru 
28320Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
284*312Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
28520Sigor@sysoev.ru 
286*312Sigor@sysoev.ru     nxt_listen_socket_remote_size(ls, sa);
28720Sigor@sysoev.ru 
28820Sigor@sysoev.ru     ls->socket = -1;
28920Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
29020Sigor@sysoev.ru     ls->read_after_accept = 1;
29120Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
29220Sigor@sysoev.ru 
29320Sigor@sysoev.ru #if 0
29420Sigor@sysoev.ru     /* STUB */
29565Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
29620Sigor@sysoev.ru     if (wq == NULL) {
29720Sigor@sysoev.ru         return NXT_ERROR;
29820Sigor@sysoev.ru     }
29920Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
30020Sigor@sysoev.ru     /**/
30120Sigor@sysoev.ru 
30220Sigor@sysoev.ru     ls->work_queue = wq;
30320Sigor@sysoev.ru #endif
30420Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
30520Sigor@sysoev.ru 
30620Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
30720Sigor@sysoev.ru         return NXT_ERROR;
30820Sigor@sysoev.ru     }
30920Sigor@sysoev.ru 
31020Sigor@sysoev.ru     rt->controller_socket = ls;
31120Sigor@sysoev.ru 
31220Sigor@sysoev.ru     return NXT_OK;
31320Sigor@sysoev.ru }
31420Sigor@sysoev.ru 
31520Sigor@sysoev.ru 
31620Sigor@sysoev.ru static void
31720Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
31820Sigor@sysoev.ru {
31927Svbart@nginx.com     nxt_buf_t                 *b;
32062Sigor@sysoev.ru     nxt_conn_t                *c;
32127Svbart@nginx.com     nxt_event_engine_t        *engine;
32227Svbart@nginx.com     nxt_controller_request_t  *r;
32320Sigor@sysoev.ru 
32420Sigor@sysoev.ru     c = obj;
32520Sigor@sysoev.ru 
32620Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
32720Sigor@sysoev.ru 
32865Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
32927Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
33027Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
33127Svbart@nginx.com         return;
33227Svbart@nginx.com     }
33327Svbart@nginx.com 
334140Svbart@nginx.com     r->conn = c;
335140Svbart@nginx.com 
33660Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
33760Svbart@nginx.com                       != NXT_OK))
33860Svbart@nginx.com     {
33960Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
34060Svbart@nginx.com         return;
34160Svbart@nginx.com     }
34227Svbart@nginx.com 
34367Svbart@nginx.com     r->parser.fields_hash = nxt_controller_fields_hash;
34467Svbart@nginx.com 
34520Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
34620Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
34720Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
34820Sigor@sysoev.ru         return;
34920Sigor@sysoev.ru     }
35020Sigor@sysoev.ru 
35120Sigor@sysoev.ru     c->read = b;
35227Svbart@nginx.com     c->socket.data = r;
35320Sigor@sysoev.ru     c->socket.read_ready = 1;
35420Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
35520Sigor@sysoev.ru 
35620Sigor@sysoev.ru     engine = task->thread->engine;
35720Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
35827Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
35920Sigor@sysoev.ru 
36062Sigor@sysoev.ru     nxt_conn_read(engine, c);
36120Sigor@sysoev.ru }
36220Sigor@sysoev.ru 
36320Sigor@sysoev.ru 
36420Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
36520Sigor@sysoev.ru     nxt_aligned(64) =
36620Sigor@sysoev.ru {
36756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
36856Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
36956Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
37020Sigor@sysoev.ru 
37156Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
37256Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
37356Sigor@sysoev.ru     .timer_data = 60 * 1000,
37420Sigor@sysoev.ru };
37520Sigor@sysoev.ru 
37620Sigor@sysoev.ru 
37720Sigor@sysoev.ru static void
37820Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
37920Sigor@sysoev.ru {
38027Svbart@nginx.com     size_t                    preread;
38127Svbart@nginx.com     nxt_buf_t                 *b;
38227Svbart@nginx.com     nxt_int_t                 rc;
38362Sigor@sysoev.ru     nxt_conn_t                *c;
38427Svbart@nginx.com     nxt_controller_request_t  *r;
38520Sigor@sysoev.ru 
38620Sigor@sysoev.ru     c = obj;
38727Svbart@nginx.com     r = data;
38820Sigor@sysoev.ru 
38920Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
39020Sigor@sysoev.ru 
39127Svbart@nginx.com     nxt_queue_remove(&c->link);
39227Svbart@nginx.com     nxt_queue_self(&c->link);
39327Svbart@nginx.com 
39427Svbart@nginx.com     b = c->read;
39527Svbart@nginx.com 
39627Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
39727Svbart@nginx.com 
39827Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
39927Svbart@nginx.com 
40027Svbart@nginx.com         if (rc == NXT_AGAIN) {
40127Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
40227Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
40327Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
40427Svbart@nginx.com                 return;
40527Svbart@nginx.com             }
40627Svbart@nginx.com 
40762Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
40827Svbart@nginx.com             return;
40927Svbart@nginx.com         }
41027Svbart@nginx.com 
41127Svbart@nginx.com         /* rc == NXT_ERROR */
41227Svbart@nginx.com 
41327Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
41427Svbart@nginx.com 
41527Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
41627Svbart@nginx.com         return;
41727Svbart@nginx.com     }
41827Svbart@nginx.com 
41967Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, r, task->log);
42060Svbart@nginx.com 
42160Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
42260Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
42360Svbart@nginx.com         return;
42460Svbart@nginx.com     }
42560Svbart@nginx.com 
42627Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
42727Svbart@nginx.com 
42827Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
429107Svbart@nginx.com                     "body length: %uz, preread: %uz",
43027Svbart@nginx.com                     r->length, preread);
43127Svbart@nginx.com 
43227Svbart@nginx.com     if (preread >= r->length) {
433140Svbart@nginx.com         nxt_controller_process_request(task, r);
43427Svbart@nginx.com         return;
43527Svbart@nginx.com     }
43627Svbart@nginx.com 
43727Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
43827Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
43927Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
44027Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
44127Svbart@nginx.com             return;
44227Svbart@nginx.com         }
44327Svbart@nginx.com 
44427Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
44527Svbart@nginx.com 
44627Svbart@nginx.com         c->read = b;
44727Svbart@nginx.com     }
44827Svbart@nginx.com 
44927Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
45027Svbart@nginx.com 
45162Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
45220Sigor@sysoev.ru }
45320Sigor@sysoev.ru 
45420Sigor@sysoev.ru 
45520Sigor@sysoev.ru static nxt_msec_t
45662Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
45720Sigor@sysoev.ru {
45820Sigor@sysoev.ru     return (nxt_msec_t) data;
45920Sigor@sysoev.ru }
46020Sigor@sysoev.ru 
46120Sigor@sysoev.ru 
46220Sigor@sysoev.ru static void
46320Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
46420Sigor@sysoev.ru {
46562Sigor@sysoev.ru     nxt_conn_t  *c;
46620Sigor@sysoev.ru 
46720Sigor@sysoev.ru     c = obj;
46820Sigor@sysoev.ru 
46920Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
47020Sigor@sysoev.ru 
47127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
47220Sigor@sysoev.ru }
47320Sigor@sysoev.ru 
47420Sigor@sysoev.ru 
47520Sigor@sysoev.ru static void
47620Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
47720Sigor@sysoev.ru {
47862Sigor@sysoev.ru     nxt_timer_t  *timer;
47962Sigor@sysoev.ru     nxt_conn_t   *c;
48020Sigor@sysoev.ru 
48162Sigor@sysoev.ru     timer = obj;
48220Sigor@sysoev.ru 
48362Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
48420Sigor@sysoev.ru     c->socket.timedout = 1;
48520Sigor@sysoev.ru     c->socket.closed = 1;
48620Sigor@sysoev.ru 
48720Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
48820Sigor@sysoev.ru 
48927Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
49027Svbart@nginx.com }
49127Svbart@nginx.com 
49227Svbart@nginx.com 
49327Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
49427Svbart@nginx.com     nxt_aligned(64) =
49527Svbart@nginx.com {
49656Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
49756Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
49856Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
49927Svbart@nginx.com 
50056Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
50156Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
50256Sigor@sysoev.ru     .timer_data = 60 * 1000,
50356Sigor@sysoev.ru     .timer_autoreset = 1,
50427Svbart@nginx.com };
50527Svbart@nginx.com 
50627Svbart@nginx.com 
50727Svbart@nginx.com static void
50827Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
50927Svbart@nginx.com {
510107Svbart@nginx.com     size_t                    read;
511107Svbart@nginx.com     nxt_buf_t                 *b;
512107Svbart@nginx.com     nxt_conn_t                *c;
513107Svbart@nginx.com     nxt_controller_request_t  *r;
51427Svbart@nginx.com 
51527Svbart@nginx.com     c = obj;
516107Svbart@nginx.com     r = data;
51727Svbart@nginx.com     b = c->read;
51827Svbart@nginx.com 
519107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
52027Svbart@nginx.com 
521107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
522107Svbart@nginx.com               read, r->length);
52327Svbart@nginx.com 
524107Svbart@nginx.com     if (read >= r->length) {
525140Svbart@nginx.com         nxt_controller_process_request(task, r);
52627Svbart@nginx.com         return;
52727Svbart@nginx.com     }
52827Svbart@nginx.com 
52962Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
53027Svbart@nginx.com }
53127Svbart@nginx.com 
53227Svbart@nginx.com 
53327Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
53427Svbart@nginx.com     nxt_aligned(64) =
53527Svbart@nginx.com {
53656Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
53756Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
53827Svbart@nginx.com 
53956Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
54056Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
54156Sigor@sysoev.ru     .timer_data = 60 * 1000,
54256Sigor@sysoev.ru     .timer_autoreset = 1,
54327Svbart@nginx.com };
54427Svbart@nginx.com 
54527Svbart@nginx.com 
54627Svbart@nginx.com static void
54727Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
54827Svbart@nginx.com {
54962Sigor@sysoev.ru     nxt_buf_t   *b;
55062Sigor@sysoev.ru     nxt_conn_t  *c;
55127Svbart@nginx.com 
55227Svbart@nginx.com     c = obj;
55327Svbart@nginx.com 
55427Svbart@nginx.com     nxt_debug(task, "controller conn write");
55527Svbart@nginx.com 
55627Svbart@nginx.com     b = c->write;
55727Svbart@nginx.com 
55827Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
55962Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
56027Svbart@nginx.com         return;
56127Svbart@nginx.com     }
56227Svbart@nginx.com 
56327Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
56427Svbart@nginx.com 
56527Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
56627Svbart@nginx.com }
56727Svbart@nginx.com 
56827Svbart@nginx.com 
56927Svbart@nginx.com static void
57027Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
57127Svbart@nginx.com {
57262Sigor@sysoev.ru     nxt_conn_t  *c;
57327Svbart@nginx.com 
57427Svbart@nginx.com     c = obj;
57527Svbart@nginx.com 
57627Svbart@nginx.com     nxt_debug(task, "controller conn write error");
57727Svbart@nginx.com 
57827Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
57927Svbart@nginx.com }
58027Svbart@nginx.com 
58127Svbart@nginx.com 
58227Svbart@nginx.com static void
58327Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
58427Svbart@nginx.com {
58562Sigor@sysoev.ru     nxt_conn_t   *c;
58662Sigor@sysoev.ru     nxt_timer_t  *timer;
58727Svbart@nginx.com 
58862Sigor@sysoev.ru     timer = obj;
58927Svbart@nginx.com 
59062Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
59127Svbart@nginx.com     c->socket.timedout = 1;
59227Svbart@nginx.com     c->socket.closed = 1;
59327Svbart@nginx.com 
59427Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
59527Svbart@nginx.com 
59627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
59720Sigor@sysoev.ru }
59820Sigor@sysoev.ru 
59920Sigor@sysoev.ru 
60020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
60120Sigor@sysoev.ru     nxt_aligned(64) =
60220Sigor@sysoev.ru {
60356Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
60420Sigor@sysoev.ru };
60520Sigor@sysoev.ru 
60620Sigor@sysoev.ru 
60720Sigor@sysoev.ru static void
60820Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
60920Sigor@sysoev.ru {
61062Sigor@sysoev.ru     nxt_conn_t  *c;
61120Sigor@sysoev.ru 
61220Sigor@sysoev.ru     c = obj;
61320Sigor@sysoev.ru 
61420Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
61520Sigor@sysoev.ru 
61627Svbart@nginx.com     nxt_queue_remove(&c->link);
61727Svbart@nginx.com 
61820Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
61920Sigor@sysoev.ru 
62062Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
62120Sigor@sysoev.ru }
62220Sigor@sysoev.ru 
62320Sigor@sysoev.ru 
62420Sigor@sysoev.ru static void
62520Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
62620Sigor@sysoev.ru {
62762Sigor@sysoev.ru     nxt_conn_t  *c;
62820Sigor@sysoev.ru 
62920Sigor@sysoev.ru     c = obj;
63020Sigor@sysoev.ru 
63120Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
63220Sigor@sysoev.ru 
63365Sigor@sysoev.ru     nxt_mp_destroy(c->mem_pool);
63420Sigor@sysoev.ru 
63520Sigor@sysoev.ru     //nxt_free(c);
63620Sigor@sysoev.ru }
63727Svbart@nginx.com 
63827Svbart@nginx.com 
63927Svbart@nginx.com static nxt_int_t
64060Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
64167Svbart@nginx.com     nxt_log_t *log)
64227Svbart@nginx.com {
64327Svbart@nginx.com     off_t                     length;
64427Svbart@nginx.com     nxt_controller_request_t  *r;
64527Svbart@nginx.com 
64627Svbart@nginx.com     r = ctx;
64727Svbart@nginx.com 
64860Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
64927Svbart@nginx.com 
65027Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
651107Svbart@nginx.com 
652107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
653107Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
654107Svbart@nginx.com             return NXT_ERROR;
655107Svbart@nginx.com         }
65627Svbart@nginx.com 
65727Svbart@nginx.com         r->length = length;
65827Svbart@nginx.com         return NXT_OK;
65927Svbart@nginx.com     }
66027Svbart@nginx.com 
66160Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
66227Svbart@nginx.com 
66327Svbart@nginx.com     return NXT_ERROR;
66427Svbart@nginx.com }
66527Svbart@nginx.com 
66627Svbart@nginx.com 
66727Svbart@nginx.com static void
668140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
66927Svbart@nginx.com {
67065Sigor@sysoev.ru     nxt_mp_t                   *mp;
67151Svbart@nginx.com     nxt_int_t                  rc;
67246Svbart@nginx.com     nxt_str_t                  path;
673140Svbart@nginx.com     nxt_conn_t                 *c;
67451Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
675106Svbart@nginx.com     nxt_conf_op_t              *ops;
676106Svbart@nginx.com     nxt_conf_value_t           *value;
677208Svbart@nginx.com     nxt_conf_json_error_t      error;
67844Svbart@nginx.com     nxt_controller_response_t  resp;
67944Svbart@nginx.com 
68051Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
68151Svbart@nginx.com 
682140Svbart@nginx.com     c = req->conn;
683112Smax.romanov@nginx.com     path = req->parser.path;
68451Svbart@nginx.com 
68551Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
68651Svbart@nginx.com         path.length--;
68751Svbart@nginx.com     }
68851Svbart@nginx.com 
68944Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
69044Svbart@nginx.com 
69144Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
69246Svbart@nginx.com 
693106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
69451Svbart@nginx.com 
69551Svbart@nginx.com         if (value == NULL) {
696208Svbart@nginx.com             goto not_found;
69751Svbart@nginx.com         }
69851Svbart@nginx.com 
699208Svbart@nginx.com         resp.status = 200;
700106Svbart@nginx.com         resp.conf = value;
70146Svbart@nginx.com 
702208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
703208Svbart@nginx.com         return;
70451Svbart@nginx.com     }
70551Svbart@nginx.com 
70651Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
70746Svbart@nginx.com 
708238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
709238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
710238Svbart@nginx.com             return;
711238Svbart@nginx.com         }
712238Svbart@nginx.com 
71365Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
71451Svbart@nginx.com 
71551Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
716208Svbart@nginx.com             goto alloc_fail;
71746Svbart@nginx.com         }
71846Svbart@nginx.com 
71951Svbart@nginx.com         mbuf = &c->read->mem;
72051Svbart@nginx.com 
721208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
722208Svbart@nginx.com 
723208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
72451Svbart@nginx.com 
72551Svbart@nginx.com         if (value == NULL) {
72665Sigor@sysoev.ru             nxt_mp_destroy(mp);
727208Svbart@nginx.com 
728208Svbart@nginx.com             if (error.pos == NULL) {
729208Svbart@nginx.com                 goto alloc_fail;
730208Svbart@nginx.com             }
731208Svbart@nginx.com 
732208Svbart@nginx.com             resp.status = 400;
733208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
734208Svbart@nginx.com             resp.detail = error.detail;
735208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
736208Svbart@nginx.com 
737208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
738208Svbart@nginx.com                                    &resp.line, &resp.column);
739208Svbart@nginx.com 
740208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
741208Svbart@nginx.com             return;
74251Svbart@nginx.com         }
74351Svbart@nginx.com 
74451Svbart@nginx.com         if (path.length != 1) {
745106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
746106Svbart@nginx.com                                      nxt_controller_conf.root,
747106Svbart@nginx.com                                      &path, value);
74846Svbart@nginx.com 
74951Svbart@nginx.com             if (rc != NXT_OK) {
75051Svbart@nginx.com                 if (rc == NXT_DECLINED) {
751208Svbart@nginx.com                     goto not_found;
75251Svbart@nginx.com                 }
75346Svbart@nginx.com 
754208Svbart@nginx.com                 goto alloc_fail;
75551Svbart@nginx.com             }
75651Svbart@nginx.com 
757106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
75851Svbart@nginx.com 
75951Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
76065Sigor@sysoev.ru                 nxt_mp_destroy(mp);
761208Svbart@nginx.com                 goto alloc_fail;
76251Svbart@nginx.com             }
76346Svbart@nginx.com         }
76444Svbart@nginx.com 
765116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
766121Svbart@nginx.com             nxt_mp_destroy(mp);
767208Svbart@nginx.com             goto invalid_conf;
768116Svbart@nginx.com         }
769116Svbart@nginx.com 
770249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
771249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
772247Svbart@nginx.com 
773247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
774121Svbart@nginx.com             nxt_mp_destroy(mp);
775247Svbart@nginx.com 
776247Svbart@nginx.com             if (rc == NXT_DECLINED) {
777247Svbart@nginx.com                 goto no_router;
778247Svbart@nginx.com             }
779247Svbart@nginx.com 
780247Svbart@nginx.com             /* rc == NXT_ERROR */
781208Svbart@nginx.com             goto alloc_fail;
782121Svbart@nginx.com         }
783121Svbart@nginx.com 
784249Svbart@nginx.com         req->conf.root = value;
785249Svbart@nginx.com         req->conf.pool = mp;
786249Svbart@nginx.com 
787249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
788249Svbart@nginx.com 
789140Svbart@nginx.com         return;
79051Svbart@nginx.com     }
79127Svbart@nginx.com 
79251Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
79351Svbart@nginx.com 
794238Svbart@nginx.com         if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) {
795238Svbart@nginx.com             nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
796238Svbart@nginx.com             return;
797238Svbart@nginx.com         }
798238Svbart@nginx.com 
79951Svbart@nginx.com         if (path.length == 1) {
80065Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
80144Svbart@nginx.com 
80251Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
803208Svbart@nginx.com                 goto alloc_fail;
80451Svbart@nginx.com             }
80551Svbart@nginx.com 
806106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
80727Svbart@nginx.com 
80844Svbart@nginx.com         } else {
809106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
810106Svbart@nginx.com                                      nxt_controller_conf.root,
811106Svbart@nginx.com                                      &path, NULL);
81251Svbart@nginx.com 
81351Svbart@nginx.com             if (rc != NXT_OK) {
81451Svbart@nginx.com                 if (rc == NXT_DECLINED) {
815208Svbart@nginx.com                     goto not_found;
81651Svbart@nginx.com                 }
81751Svbart@nginx.com 
818208Svbart@nginx.com                 goto alloc_fail;
81951Svbart@nginx.com             }
82051Svbart@nginx.com 
82165Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
82251Svbart@nginx.com 
82351Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
824208Svbart@nginx.com                 goto alloc_fail;
82551Svbart@nginx.com             }
82651Svbart@nginx.com 
827106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
82851Svbart@nginx.com         }
82951Svbart@nginx.com 
83051Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
83165Sigor@sysoev.ru             nxt_mp_destroy(mp);
832208Svbart@nginx.com             goto alloc_fail;
83344Svbart@nginx.com         }
83444Svbart@nginx.com 
835116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
836121Svbart@nginx.com             nxt_mp_destroy(mp);
837208Svbart@nginx.com             goto invalid_conf;
838116Svbart@nginx.com         }
839116Svbart@nginx.com 
840249Svbart@nginx.com         rc = nxt_controller_conf_send(task, value,
841249Svbart@nginx.com                                       nxt_controller_conf_handler, req);
842247Svbart@nginx.com 
843247Svbart@nginx.com         if (nxt_slow_path(rc != NXT_OK)) {
844121Svbart@nginx.com             nxt_mp_destroy(mp);
845247Svbart@nginx.com 
846247Svbart@nginx.com             if (rc == NXT_DECLINED) {
847247Svbart@nginx.com                 goto no_router;
848247Svbart@nginx.com             }
849247Svbart@nginx.com 
850247Svbart@nginx.com             /* rc == NXT_ERROR */
851208Svbart@nginx.com             goto alloc_fail;
852121Svbart@nginx.com         }
853121Svbart@nginx.com 
854249Svbart@nginx.com         req->conf.root = value;
855249Svbart@nginx.com         req->conf.pool = mp;
856249Svbart@nginx.com 
857249Svbart@nginx.com         nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link);
858249Svbart@nginx.com 
859140Svbart@nginx.com         return;
86051Svbart@nginx.com     }
86151Svbart@nginx.com 
862208Svbart@nginx.com     resp.status = 405;
863208Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
864208Svbart@nginx.com     resp.offset = -1;
86551Svbart@nginx.com 
866208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
867208Svbart@nginx.com     return;
86851Svbart@nginx.com 
869208Svbart@nginx.com not_found:
870208Svbart@nginx.com 
871208Svbart@nginx.com     resp.status = 404;
872208Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
873208Svbart@nginx.com     resp.offset = -1;
874208Svbart@nginx.com 
875208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
876208Svbart@nginx.com     return;
877208Svbart@nginx.com 
878208Svbart@nginx.com invalid_conf:
879208Svbart@nginx.com 
880208Svbart@nginx.com     resp.status = 400;
881208Svbart@nginx.com     resp.title = (u_char *) "Invalid configuration.";
882208Svbart@nginx.com     resp.offset = -1;
883208Svbart@nginx.com 
884208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
885208Svbart@nginx.com     return;
886247Svbart@nginx.com 
887247Svbart@nginx.com alloc_fail:
888247Svbart@nginx.com 
889247Svbart@nginx.com     resp.status = 500;
890247Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
891247Svbart@nginx.com     resp.offset = -1;
892247Svbart@nginx.com 
893247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
894247Svbart@nginx.com     return;
895247Svbart@nginx.com 
896247Svbart@nginx.com no_router:
897247Svbart@nginx.com 
898247Svbart@nginx.com     resp.status = 500;
899247Svbart@nginx.com     resp.title = (u_char *) "Router process isn't available.";
900247Svbart@nginx.com     resp.offset = -1;
901247Svbart@nginx.com 
902247Svbart@nginx.com     nxt_controller_response(task, req, &resp);
903247Svbart@nginx.com     return;
90427Svbart@nginx.com }
90527Svbart@nginx.com 
90627Svbart@nginx.com 
907193Smax.romanov@nginx.com static void
908193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
909193Smax.romanov@nginx.com     void *data)
910140Svbart@nginx.com {
911238Svbart@nginx.com     nxt_queue_t                queue;
912140Svbart@nginx.com     nxt_controller_request_t   *req;
913140Svbart@nginx.com     nxt_controller_response_t  resp;
914140Svbart@nginx.com 
915238Svbart@nginx.com     req = data;
916238Svbart@nginx.com 
917201Svbart@nginx.com     nxt_debug(task, "controller conf ready: %*s",
918201Svbart@nginx.com               nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos);
919140Svbart@nginx.com 
920238Svbart@nginx.com     nxt_queue_remove(&req->link);
921140Svbart@nginx.com 
922238Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
923140Svbart@nginx.com 
924193Smax.romanov@nginx.com     if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) {
925140Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
926140Svbart@nginx.com 
927140Svbart@nginx.com         nxt_controller_conf = req->conf;
928140Svbart@nginx.com 
929208Svbart@nginx.com         resp.status = 200;
930208Svbart@nginx.com         resp.title = (u_char *) "Reconfiguration done.";
931140Svbart@nginx.com 
932140Svbart@nginx.com     } else {
933140Svbart@nginx.com         nxt_mp_destroy(req->conf.pool);
934140Svbart@nginx.com 
935208Svbart@nginx.com         resp.status = 500;
936208Svbart@nginx.com         resp.title = (u_char *) "Failed to apply new configuration.";
937208Svbart@nginx.com         resp.offset = -1;
938140Svbart@nginx.com     }
939140Svbart@nginx.com 
940140Svbart@nginx.com     nxt_controller_response(task, req, &resp);
941140Svbart@nginx.com 
942238Svbart@nginx.com     nxt_queue_init(&queue);
943238Svbart@nginx.com     nxt_queue_add(&queue, &nxt_controller_waiting_requests);
944140Svbart@nginx.com 
945238Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
946121Svbart@nginx.com 
947238Svbart@nginx.com     nxt_queue_each(req, &queue, nxt_controller_request_t, link) {
948238Svbart@nginx.com         nxt_controller_process_request(task, req);
949238Svbart@nginx.com     } nxt_queue_loop;
950121Svbart@nginx.com }
951121Svbart@nginx.com 
952121Svbart@nginx.com 
953140Svbart@nginx.com static void
954140Svbart@nginx.com nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req,
95544Svbart@nginx.com     nxt_controller_response_t *resp)
95633Svbart@nginx.com {
957208Svbart@nginx.com     size_t                  size;
958208Svbart@nginx.com     nxt_str_t               status_line, str;
959208Svbart@nginx.com     nxt_buf_t               *b, *body;
960208Svbart@nginx.com     nxt_conn_t              *c;
961208Svbart@nginx.com     nxt_uint_t              n;
962208Svbart@nginx.com     nxt_conf_value_t        *value, *location;
963208Svbart@nginx.com     nxt_conf_json_pretty_t  pretty;
964208Svbart@nginx.com 
965208Svbart@nginx.com     static nxt_str_t  success_str = nxt_string("success");
966208Svbart@nginx.com     static nxt_str_t  error_str = nxt_string("error");
967208Svbart@nginx.com     static nxt_str_t  detail_str = nxt_string("detail");
968208Svbart@nginx.com     static nxt_str_t  location_str = nxt_string("location");
969208Svbart@nginx.com     static nxt_str_t  offset_str = nxt_string("offset");
970208Svbart@nginx.com     static nxt_str_t  line_str = nxt_string("line");
971208Svbart@nginx.com     static nxt_str_t  column_str = nxt_string("column");
972208Svbart@nginx.com 
973208Svbart@nginx.com     static nxt_time_string_t  date_cache = {
974208Svbart@nginx.com         (nxt_atomic_uint_t) -1,
975208Svbart@nginx.com         nxt_controller_date,
976208Svbart@nginx.com         "%s, %02d %s %4d %02d:%02d:%02d GMT",
977208Svbart@nginx.com         sizeof("Wed, 31 Dec 1986 16:40:00 GMT") - 1,
978208Svbart@nginx.com         NXT_THREAD_TIME_GMT,
979208Svbart@nginx.com         NXT_THREAD_TIME_SEC,
980208Svbart@nginx.com     };
981208Svbart@nginx.com 
982208Svbart@nginx.com     switch (resp->status) {
983208Svbart@nginx.com 
984208Svbart@nginx.com     case 200:
985208Svbart@nginx.com         nxt_str_set(&status_line, "200 OK");
986208Svbart@nginx.com         break;
987208Svbart@nginx.com 
988208Svbart@nginx.com     case 400:
989208Svbart@nginx.com         nxt_str_set(&status_line, "400 Bad Request");
990208Svbart@nginx.com         break;
991208Svbart@nginx.com 
992208Svbart@nginx.com     case 404:
993208Svbart@nginx.com         nxt_str_set(&status_line, "404 Not Found");
994208Svbart@nginx.com         break;
995208Svbart@nginx.com 
996208Svbart@nginx.com     case 405:
997208Svbart@nginx.com         nxt_str_set(&status_line, "405 Method Not Allowed");
998208Svbart@nginx.com         break;
999208Svbart@nginx.com 
1000209Svbart@nginx.com     default:
1001208Svbart@nginx.com         nxt_str_set(&status_line, "500 Internal Server Error");
1002208Svbart@nginx.com         break;
1003208Svbart@nginx.com     }
1004140Svbart@nginx.com 
1005140Svbart@nginx.com     c = req->conn;
1006208Svbart@nginx.com     value = resp->conf;
100733Svbart@nginx.com 
1008208Svbart@nginx.com     if (value == NULL) {
1009208Svbart@nginx.com         n = 1
1010208Svbart@nginx.com             + (resp->detail != NULL)
1011208Svbart@nginx.com             + (resp->status >= 400 && resp->offset != -1);
1012208Svbart@nginx.com 
1013208Svbart@nginx.com         value = nxt_conf_create_object(c->mem_pool, n);
1014208Svbart@nginx.com 
1015208Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
1016208Svbart@nginx.com             nxt_controller_conn_close(task, c, req);
1017208Svbart@nginx.com             return;
1018208Svbart@nginx.com         }
1019208Svbart@nginx.com 
1020208Svbart@nginx.com         str.length = nxt_strlen(resp->title);
1021208Svbart@nginx.com         str.start = resp->title;
1022208Svbart@nginx.com 
1023208Svbart@nginx.com         if (resp->status < 400) {
1024208Svbart@nginx.com             nxt_conf_set_member_string(value, &success_str, &str, 0);
1025208Svbart@nginx.com 
1026208Svbart@nginx.com         } else {
1027208Svbart@nginx.com             nxt_conf_set_member_string(value, &error_str, &str, 0);
1028208Svbart@nginx.com         }
1029208Svbart@nginx.com 
1030208Svbart@nginx.com         n = 0;
1031208Svbart@nginx.com 
1032208Svbart@nginx.com         if (resp->detail != NULL) {
1033208Svbart@nginx.com             str.length = nxt_strlen(resp->