xref: /unit/src/nxt_controller.c (revision 140)
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>
1020Sigor@sysoev.ru #include <nxt_master_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;
24*140Svbart@nginx.com     nxt_conn_t                *conn;
25*140Svbart@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 {
30106Svbart@nginx.com     nxt_str_t         status_line;
31106Svbart@nginx.com     nxt_conf_value_t  *conf;
32106Svbart@nginx.com     nxt_str_t         json;
3344Svbart@nginx.com } nxt_controller_response_t;
3444Svbart@nginx.com 
3544Svbart@nginx.com 
3620Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
3720Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
3862Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
3920Sigor@sysoev.ru     uintptr_t data);
4020Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
4120Sigor@sysoev.ru     void *data);
4220Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
4320Sigor@sysoev.ru     void *data);
4427Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
4527Svbart@nginx.com     void *data);
4627Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
4727Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
4827Svbart@nginx.com     void *data);
4927Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
5027Svbart@nginx.com     void *data);
5120Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
5220Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
5320Sigor@sysoev.ru 
5427Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
5567Svbart@nginx.com     nxt_http_field_t *field, nxt_log_t *log);
5627Svbart@nginx.com 
5727Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
58*140Svbart@nginx.com     nxt_controller_request_t *req);
59121Svbart@nginx.com static nxt_int_t nxt_controller_conf_apply(nxt_task_t *task,
60*140Svbart@nginx.com     nxt_controller_request_t *req);
61*140Svbart@nginx.com static void nxt_controller_process_waiting(nxt_task_t *task);
62*140Svbart@nginx.com static nxt_int_t nxt_controller_conf_pass(nxt_task_t *task,
63121Svbart@nginx.com     nxt_conf_value_t *conf);
64*140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
65*140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
6645Svbart@nginx.com static nxt_buf_t *nxt_controller_response_body(nxt_controller_response_t *resp,
6765Sigor@sysoev.ru     nxt_mp_t *pool);
6827Svbart@nginx.com 
6927Svbart@nginx.com 
7060Svbart@nginx.com static nxt_http_fields_hash_entry_t  nxt_controller_request_fields[] = {
7127Svbart@nginx.com     { nxt_string("Content-Length"),
7227Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
7327Svbart@nginx.com 
7427Svbart@nginx.com     { nxt_null_string, NULL, 0 }
7527Svbart@nginx.com };
7627Svbart@nginx.com 
7760Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_fields_hash;
7827Svbart@nginx.com 
7944Svbart@nginx.com 
80*140Svbart@nginx.com static nxt_controller_conf_t     nxt_controller_conf;
81*140Svbart@nginx.com static nxt_queue_t               nxt_controller_waiting_requests;
82*140Svbart@nginx.com static nxt_controller_request_t  *nxt_controller_current_request;
8327Svbart@nginx.com 
8420Sigor@sysoev.ru 
8520Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
8627Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
8727Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
8820Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
8920Sigor@sysoev.ru 
9020Sigor@sysoev.ru 
9120Sigor@sysoev.ru nxt_int_t
9220Sigor@sysoev.ru nxt_controller_start(nxt_task_t *task, nxt_runtime_t *rt)
9320Sigor@sysoev.ru {
9465Sigor@sysoev.ru     nxt_mp_t                *mp;
95106Svbart@nginx.com     nxt_conf_value_t        *conf;
9627Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
9727Svbart@nginx.com 
9844Svbart@nginx.com     static const nxt_str_t json
99116Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
10044Svbart@nginx.com 
10160Svbart@nginx.com     hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
10260Svbart@nginx.com                                        rt->mem_pool);
10327Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
10427Svbart@nginx.com         return NXT_ERROR;
10527Svbart@nginx.com     }
10627Svbart@nginx.com 
10760Svbart@nginx.com     nxt_controller_fields_hash = hash;
10827Svbart@nginx.com 
10954Sigor@sysoev.ru     if (nxt_listen_event(task, rt->controller_socket) == NULL) {
11020Sigor@sysoev.ru         return NXT_ERROR;
11120Sigor@sysoev.ru     }
11220Sigor@sysoev.ru 
11365Sigor@sysoev.ru     mp = nxt_mp_create(1024, 128, 256, 32);
11444Svbart@nginx.com 
11544Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
11644Svbart@nginx.com         return NXT_ERROR;
11744Svbart@nginx.com     }
11844Svbart@nginx.com 
119106Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
12044Svbart@nginx.com 
12144Svbart@nginx.com     if (conf == NULL) {
12244Svbart@nginx.com         return NXT_ERROR;
12344Svbart@nginx.com     }
12444Svbart@nginx.com 
12544Svbart@nginx.com     nxt_controller_conf.root = conf;
12644Svbart@nginx.com     nxt_controller_conf.pool = mp;
12744Svbart@nginx.com 
128*140Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
129*140Svbart@nginx.com 
13020Sigor@sysoev.ru     return NXT_OK;
13120Sigor@sysoev.ru }
13220Sigor@sysoev.ru 
13320Sigor@sysoev.ru 
13420Sigor@sysoev.ru nxt_int_t
13520Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
13620Sigor@sysoev.ru {
13720Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
13820Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
13920Sigor@sysoev.ru 
14020Sigor@sysoev.ru     sa = rt->controller_listen;
14120Sigor@sysoev.ru 
14220Sigor@sysoev.ru     if (rt->controller_listen == NULL) {
14320Sigor@sysoev.ru         sa = nxt_sockaddr_alloc(rt->mem_pool, sizeof(struct sockaddr_in),
14420Sigor@sysoev.ru                                 NXT_INET_ADDR_STR_LEN);
14520Sigor@sysoev.ru         if (sa == NULL) {
14620Sigor@sysoev.ru             return NXT_ERROR;
14720Sigor@sysoev.ru         }
14820Sigor@sysoev.ru 
14920Sigor@sysoev.ru         sa->type = SOCK_STREAM;
15020Sigor@sysoev.ru         sa->u.sockaddr_in.sin_family = AF_INET;
15120Sigor@sysoev.ru         sa->u.sockaddr_in.sin_port = htons(8443);
15220Sigor@sysoev.ru 
15320Sigor@sysoev.ru         nxt_sockaddr_text(sa);
15420Sigor@sysoev.ru 
15520Sigor@sysoev.ru         rt->controller_listen = sa;
15620Sigor@sysoev.ru     }
15720Sigor@sysoev.ru 
15865Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
15920Sigor@sysoev.ru     if (ls == NULL) {
16020Sigor@sysoev.ru         return NXT_ERROR;
16120Sigor@sysoev.ru     }
16220Sigor@sysoev.ru 
16320Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
16420Sigor@sysoev.ru                                        sa->socklen, sa->length);
16520Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
16620Sigor@sysoev.ru         return NXT_ERROR;
16720Sigor@sysoev.ru     }
16820Sigor@sysoev.ru 
16920Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
170103Sigor@sysoev.ru     ls->socklen = sa->socklen;
171103Sigor@sysoev.ru     ls->address_length = sa->length;
17220Sigor@sysoev.ru 
17320Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
17420Sigor@sysoev.ru 
17520Sigor@sysoev.ru     ls->socket = -1;
17620Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
17720Sigor@sysoev.ru     ls->read_after_accept = 1;
17820Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
17920Sigor@sysoev.ru 
18020Sigor@sysoev.ru #if 0
18120Sigor@sysoev.ru     /* STUB */
18265Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
18320Sigor@sysoev.ru     if (wq == NULL) {
18420Sigor@sysoev.ru         return NXT_ERROR;
18520Sigor@sysoev.ru     }
18620Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
18720Sigor@sysoev.ru     /**/
18820Sigor@sysoev.ru 
18920Sigor@sysoev.ru     ls->work_queue = wq;
19020Sigor@sysoev.ru #endif
19120Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
19220Sigor@sysoev.ru 
19320Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
19420Sigor@sysoev.ru         return NXT_ERROR;
19520Sigor@sysoev.ru     }
19620Sigor@sysoev.ru 
19720Sigor@sysoev.ru     rt->controller_socket = ls;
19820Sigor@sysoev.ru 
19920Sigor@sysoev.ru     return NXT_OK;
20020Sigor@sysoev.ru }
20120Sigor@sysoev.ru 
20220Sigor@sysoev.ru 
20320Sigor@sysoev.ru static void
20420Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
20520Sigor@sysoev.ru {
20627Svbart@nginx.com     nxt_buf_t                 *b;
20762Sigor@sysoev.ru     nxt_conn_t                *c;
20827Svbart@nginx.com     nxt_event_engine_t        *engine;
20927Svbart@nginx.com     nxt_controller_request_t  *r;
21020Sigor@sysoev.ru 
21120Sigor@sysoev.ru     c = obj;
21220Sigor@sysoev.ru 
21320Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
21420Sigor@sysoev.ru 
21565Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
21627Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
21727Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
21827Svbart@nginx.com         return;
21927Svbart@nginx.com     }
22027Svbart@nginx.com 
221*140Svbart@nginx.com     r->conn = c;
222*140Svbart@nginx.com 
22360Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
22460Svbart@nginx.com                       != NXT_OK))
22560Svbart@nginx.com     {
22660Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
22760Svbart@nginx.com         return;
22860Svbart@nginx.com     }
22927Svbart@nginx.com 
23067Svbart@nginx.com     r->parser.fields_hash = nxt_controller_fields_hash;
23167Svbart@nginx.com 
23220Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
23320Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
23420Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
23520Sigor@sysoev.ru         return;
23620Sigor@sysoev.ru     }
23720Sigor@sysoev.ru 
23820Sigor@sysoev.ru     c->read = b;
23927Svbart@nginx.com     c->socket.data = r;
24020Sigor@sysoev.ru     c->socket.read_ready = 1;
24120Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
24220Sigor@sysoev.ru 
24320Sigor@sysoev.ru     engine = task->thread->engine;
24420Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
24527Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
24620Sigor@sysoev.ru 
24762Sigor@sysoev.ru     nxt_conn_read(engine, c);
24820Sigor@sysoev.ru }
24920Sigor@sysoev.ru 
25020Sigor@sysoev.ru 
25120Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
25220Sigor@sysoev.ru     nxt_aligned(64) =
25320Sigor@sysoev.ru {
25456Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
25556Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
25656Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
25720Sigor@sysoev.ru 
25856Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
25956Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
26056Sigor@sysoev.ru     .timer_data = 60 * 1000,
26120Sigor@sysoev.ru };
26220Sigor@sysoev.ru 
26320Sigor@sysoev.ru 
26420Sigor@sysoev.ru static void
26520Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
26620Sigor@sysoev.ru {
26727Svbart@nginx.com     size_t                    preread;
26827Svbart@nginx.com     nxt_buf_t                 *b;
26927Svbart@nginx.com     nxt_int_t                 rc;
27062Sigor@sysoev.ru     nxt_conn_t                *c;
27127Svbart@nginx.com     nxt_controller_request_t  *r;
27220Sigor@sysoev.ru 
27320Sigor@sysoev.ru     c = obj;
27427Svbart@nginx.com     r = data;
27520Sigor@sysoev.ru 
27620Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
27720Sigor@sysoev.ru 
27827Svbart@nginx.com     nxt_queue_remove(&c->link);
27927Svbart@nginx.com     nxt_queue_self(&c->link);
28027Svbart@nginx.com 
28127Svbart@nginx.com     b = c->read;
28227Svbart@nginx.com 
28327Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
28427Svbart@nginx.com 
28527Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
28627Svbart@nginx.com 
28727Svbart@nginx.com         if (rc == NXT_AGAIN) {
28827Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
28927Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
29027Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
29127Svbart@nginx.com                 return;
29227Svbart@nginx.com             }
29327Svbart@nginx.com 
29462Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
29527Svbart@nginx.com             return;
29627Svbart@nginx.com         }
29727Svbart@nginx.com 
29827Svbart@nginx.com         /* rc == NXT_ERROR */
29927Svbart@nginx.com 
30027Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
30127Svbart@nginx.com 
30227Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
30327Svbart@nginx.com         return;
30427Svbart@nginx.com     }
30527Svbart@nginx.com 
30667Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, r, task->log);
30760Svbart@nginx.com 
30860Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
30960Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
31060Svbart@nginx.com         return;
31160Svbart@nginx.com     }
31260Svbart@nginx.com 
31327Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
31427Svbart@nginx.com 
31527Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
316107Svbart@nginx.com                     "body length: %uz, preread: %uz",
31727Svbart@nginx.com                     r->length, preread);
31827Svbart@nginx.com 
31927Svbart@nginx.com     if (preread >= r->length) {
320*140Svbart@nginx.com         nxt_controller_process_request(task, r);
32127Svbart@nginx.com         return;
32227Svbart@nginx.com     }
32327Svbart@nginx.com 
32427Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
32527Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
32627Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
32727Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
32827Svbart@nginx.com             return;
32927Svbart@nginx.com         }
33027Svbart@nginx.com 
33127Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
33227Svbart@nginx.com 
33327Svbart@nginx.com         c->read = b;
33427Svbart@nginx.com     }
33527Svbart@nginx.com 
33627Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
33727Svbart@nginx.com 
33862Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
33920Sigor@sysoev.ru }
34020Sigor@sysoev.ru 
34120Sigor@sysoev.ru 
34220Sigor@sysoev.ru static nxt_msec_t
34362Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
34420Sigor@sysoev.ru {
34520Sigor@sysoev.ru     return (nxt_msec_t) data;
34620Sigor@sysoev.ru }
34720Sigor@sysoev.ru 
34820Sigor@sysoev.ru 
34920Sigor@sysoev.ru static void
35020Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
35120Sigor@sysoev.ru {
35262Sigor@sysoev.ru     nxt_conn_t  *c;
35320Sigor@sysoev.ru 
35420Sigor@sysoev.ru     c = obj;
35520Sigor@sysoev.ru 
35620Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
35720Sigor@sysoev.ru 
35827Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
35920Sigor@sysoev.ru }
36020Sigor@sysoev.ru 
36120Sigor@sysoev.ru 
36220Sigor@sysoev.ru static void
36320Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
36420Sigor@sysoev.ru {
36562Sigor@sysoev.ru     nxt_timer_t  *timer;
36662Sigor@sysoev.ru     nxt_conn_t   *c;
36720Sigor@sysoev.ru 
36862Sigor@sysoev.ru     timer = obj;
36920Sigor@sysoev.ru 
37062Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
37120Sigor@sysoev.ru     c->socket.timedout = 1;
37220Sigor@sysoev.ru     c->socket.closed = 1;
37320Sigor@sysoev.ru 
37420Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
37520Sigor@sysoev.ru 
37627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
37727Svbart@nginx.com }
37827Svbart@nginx.com 
37927Svbart@nginx.com 
38027Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
38127Svbart@nginx.com     nxt_aligned(64) =
38227Svbart@nginx.com {
38356Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
38456Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
38556Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
38627Svbart@nginx.com 
38756Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
38856Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
38956Sigor@sysoev.ru     .timer_data = 60 * 1000,
39056Sigor@sysoev.ru     .timer_autoreset = 1,
39127Svbart@nginx.com };
39227Svbart@nginx.com 
39327Svbart@nginx.com 
39427Svbart@nginx.com static void
39527Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
39627Svbart@nginx.com {
397107Svbart@nginx.com     size_t                    read;
398107Svbart@nginx.com     nxt_buf_t                 *b;
399107Svbart@nginx.com     nxt_conn_t                *c;
400107Svbart@nginx.com     nxt_controller_request_t  *r;
40127Svbart@nginx.com 
40227Svbart@nginx.com     c = obj;
403107Svbart@nginx.com     r = data;
40427Svbart@nginx.com     b = c->read;
40527Svbart@nginx.com 
406107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
40727Svbart@nginx.com 
408107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
409107Svbart@nginx.com               read, r->length);
41027Svbart@nginx.com 
411107Svbart@nginx.com     if (read >= r->length) {
412*140Svbart@nginx.com         nxt_controller_process_request(task, r);
41327Svbart@nginx.com         return;
41427Svbart@nginx.com     }
41527Svbart@nginx.com 
41662Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
41727Svbart@nginx.com }
41827Svbart@nginx.com 
41927Svbart@nginx.com 
42027Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
42127Svbart@nginx.com     nxt_aligned(64) =
42227Svbart@nginx.com {
42356Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
42456Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
42527Svbart@nginx.com 
42656Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
42756Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
42856Sigor@sysoev.ru     .timer_data = 60 * 1000,
42956Sigor@sysoev.ru     .timer_autoreset = 1,
43027Svbart@nginx.com };
43127Svbart@nginx.com 
43227Svbart@nginx.com 
43327Svbart@nginx.com static void
43427Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
43527Svbart@nginx.com {
43662Sigor@sysoev.ru     nxt_buf_t   *b;
43762Sigor@sysoev.ru     nxt_conn_t  *c;
43827Svbart@nginx.com 
43927Svbart@nginx.com     c = obj;
44027Svbart@nginx.com 
44127Svbart@nginx.com     nxt_debug(task, "controller conn write");
44227Svbart@nginx.com 
44327Svbart@nginx.com     b = c->write;
44427Svbart@nginx.com 
44527Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
44662Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
44727Svbart@nginx.com         return;
44827Svbart@nginx.com     }
44927Svbart@nginx.com 
45027Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
45127Svbart@nginx.com 
45227Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
45327Svbart@nginx.com }
45427Svbart@nginx.com 
45527Svbart@nginx.com 
45627Svbart@nginx.com static void
45727Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
45827Svbart@nginx.com {
45962Sigor@sysoev.ru     nxt_conn_t  *c;
46027Svbart@nginx.com 
46127Svbart@nginx.com     c = obj;
46227Svbart@nginx.com 
46327Svbart@nginx.com     nxt_debug(task, "controller conn write error");
46427Svbart@nginx.com 
46527Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
46627Svbart@nginx.com }
46727Svbart@nginx.com 
46827Svbart@nginx.com 
46927Svbart@nginx.com static void
47027Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
47127Svbart@nginx.com {
47262Sigor@sysoev.ru     nxt_conn_t   *c;
47362Sigor@sysoev.ru     nxt_timer_t  *timer;
47427Svbart@nginx.com 
47562Sigor@sysoev.ru     timer = obj;
47627Svbart@nginx.com 
47762Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
47827Svbart@nginx.com     c->socket.timedout = 1;
47927Svbart@nginx.com     c->socket.closed = 1;
48027Svbart@nginx.com 
48127Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
48227Svbart@nginx.com 
48327Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
48420Sigor@sysoev.ru }
48520Sigor@sysoev.ru 
48620Sigor@sysoev.ru 
48720Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
48820Sigor@sysoev.ru     nxt_aligned(64) =
48920Sigor@sysoev.ru {
49056Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
49120Sigor@sysoev.ru };
49220Sigor@sysoev.ru 
49320Sigor@sysoev.ru 
49420Sigor@sysoev.ru static void
49520Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
49620Sigor@sysoev.ru {
49762Sigor@sysoev.ru     nxt_conn_t  *c;
49820Sigor@sysoev.ru 
49920Sigor@sysoev.ru     c = obj;
50020Sigor@sysoev.ru 
50120Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
50220Sigor@sysoev.ru 
50327Svbart@nginx.com     nxt_queue_remove(&c->link);
50427Svbart@nginx.com 
50520Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
50620Sigor@sysoev.ru 
50762Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
50820Sigor@sysoev.ru }
50920Sigor@sysoev.ru 
51020Sigor@sysoev.ru 
51120Sigor@sysoev.ru static void
51220Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
51320Sigor@sysoev.ru {
51462Sigor@sysoev.ru     nxt_conn_t  *c;
51520Sigor@sysoev.ru 
51620Sigor@sysoev.ru     c = obj;
51720Sigor@sysoev.ru 
51820Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
51920Sigor@sysoev.ru 
52065Sigor@sysoev.ru     nxt_mp_destroy(c->mem_pool);
52120Sigor@sysoev.ru 
52220Sigor@sysoev.ru     //nxt_free(c);
52320Sigor@sysoev.ru }
52427Svbart@nginx.com 
52527Svbart@nginx.com 
52627Svbart@nginx.com static nxt_int_t
52760Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
52867Svbart@nginx.com     nxt_log_t *log)
52927Svbart@nginx.com {
53027Svbart@nginx.com     off_t                     length;
53127Svbart@nginx.com     nxt_controller_request_t  *r;
53227Svbart@nginx.com 
53327Svbart@nginx.com     r = ctx;
53427Svbart@nginx.com 
53560Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
53627Svbart@nginx.com 
53727Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
538107Svbart@nginx.com 
539107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
540107Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
541107Svbart@nginx.com             return NXT_ERROR;
542107Svbart@nginx.com         }
54327Svbart@nginx.com 
54427Svbart@nginx.com         r->length = length;
54527Svbart@nginx.com         return NXT_OK;
54627Svbart@nginx.com     }
54727Svbart@nginx.com 
54860Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
54927Svbart@nginx.com 
55027Svbart@nginx.com     return NXT_ERROR;
55127Svbart@nginx.com }
55227Svbart@nginx.com 
55327Svbart@nginx.com 
55427Svbart@nginx.com static void
555*140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
55627Svbart@nginx.com {
55765Sigor@sysoev.ru     nxt_mp_t                   *mp;
55851Svbart@nginx.com     nxt_int_t                  rc;
55946Svbart@nginx.com     nxt_str_t                  path;
560*140Svbart@nginx.com     nxt_conn_t                 *c;
56151Svbart@nginx.com     nxt_uint_t                 status;
56251Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
563106Svbart@nginx.com     nxt_conf_op_t              *ops;
564106Svbart@nginx.com     nxt_conf_value_t           *value;
56544Svbart@nginx.com     nxt_controller_response_t  resp;
56644Svbart@nginx.com 
56751Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
56851Svbart@nginx.com 
569*140Svbart@nginx.com     c = req->conn;
570112Smax.romanov@nginx.com     path = req->parser.path;
57151Svbart@nginx.com 
57251Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
57351Svbart@nginx.com         path.length--;
57451Svbart@nginx.com     }
57551Svbart@nginx.com 
57644Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
57744Svbart@nginx.com 
57844Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
57946Svbart@nginx.com 
580106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
58151Svbart@nginx.com 
58251Svbart@nginx.com         if (value == NULL) {
58351Svbart@nginx.com             status = 404;
58451Svbart@nginx.com             goto done;
58551Svbart@nginx.com         }
58651Svbart@nginx.com 
587106Svbart@nginx.com         resp.conf = value;
58846Svbart@nginx.com 
58951Svbart@nginx.com         status = 200;
59051Svbart@nginx.com         goto done;
59151Svbart@nginx.com     }
59251Svbart@nginx.com 
59351Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
59446Svbart@nginx.com 
59565Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
59651Svbart@nginx.com 
59751Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
59851Svbart@nginx.com             status = 500;
59951Svbart@nginx.com             goto done;
60046Svbart@nginx.com         }
60146Svbart@nginx.com 
60251Svbart@nginx.com         mbuf = &c->read->mem;
60351Svbart@nginx.com 
60491Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free);
60551Svbart@nginx.com 
60651Svbart@nginx.com         if (value == NULL) {
60765Sigor@sysoev.ru             nxt_mp_destroy(mp);
60851Svbart@nginx.com             status = 400;
609116Svbart@nginx.com             nxt_str_set(&resp.json, "{ \"error\": \"Invalid JSON.\" }");
61051Svbart@nginx.com             goto done;
61151Svbart@nginx.com         }
61251Svbart@nginx.com 
61351Svbart@nginx.com         if (path.length != 1) {
614106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
615106Svbart@nginx.com                                      nxt_controller_conf.root,
616106Svbart@nginx.com                                      &path, value);
61746Svbart@nginx.com 
61851Svbart@nginx.com             if (rc != NXT_OK) {
61951Svbart@nginx.com                 if (rc == NXT_DECLINED) {
62051Svbart@nginx.com                     status = 404;
62151Svbart@nginx.com                     goto done;
62251Svbart@nginx.com                 }
62346Svbart@nginx.com 
62451Svbart@nginx.com                 status = 500;
62551Svbart@nginx.com                 goto done;
62651Svbart@nginx.com             }
62751Svbart@nginx.com 
628106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
62951Svbart@nginx.com 
63051Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
63165Sigor@sysoev.ru                 nxt_mp_destroy(mp);
63251Svbart@nginx.com                 status = 500;
63351Svbart@nginx.com                 goto done;
63451Svbart@nginx.com             }
63546Svbart@nginx.com         }
63644Svbart@nginx.com 
637116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
638121Svbart@nginx.com             nxt_mp_destroy(mp);
639116Svbart@nginx.com             status = 400;
640116Svbart@nginx.com             nxt_str_set(&resp.json,
641116Svbart@nginx.com                         "{ \"error\": \"Invalid configuration.\" }");
642116Svbart@nginx.com             goto done;
643116Svbart@nginx.com         }
644116Svbart@nginx.com 
645*140Svbart@nginx.com         req->conf.root = value;
646*140Svbart@nginx.com         req->conf.pool = mp;
647*140Svbart@nginx.com 
648*140Svbart@nginx.com         if (nxt_controller_conf_apply(task, req) != NXT_OK) {
649121Svbart@nginx.com             nxt_mp_destroy(mp);
650121Svbart@nginx.com             status = 500;
651121Svbart@nginx.com             goto done;
652121Svbart@nginx.com         }
653121Svbart@nginx.com 
654*140Svbart@nginx.com         return;
65551Svbart@nginx.com     }
65627Svbart@nginx.com 
65751Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
65851Svbart@nginx.com 
65951Svbart@nginx.com         if (path.length == 1) {
66065Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
66144Svbart@nginx.com 
66251Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
66351Svbart@nginx.com                 status = 500;
66451Svbart@nginx.com                 goto done;
66551Svbart@nginx.com             }
66651Svbart@nginx.com 
667106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
66827Svbart@nginx.com 
66944Svbart@nginx.com         } else {
670106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
671106Svbart@nginx.com                                      nxt_controller_conf.root,
672106Svbart@nginx.com                                      &path, NULL);
67351Svbart@nginx.com 
67451Svbart@nginx.com             if (rc != NXT_OK) {
67551Svbart@nginx.com                 if (rc == NXT_DECLINED) {
67651Svbart@nginx.com                     status = 404;
67751Svbart@nginx.com                     goto done;
67851Svbart@nginx.com                 }
67951Svbart@nginx.com 
68051Svbart@nginx.com                 status = 500;
68151Svbart@nginx.com                 goto done;
68251Svbart@nginx.com             }
68351Svbart@nginx.com 
68465Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
68551Svbart@nginx.com 
68651Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
68751Svbart@nginx.com                 status = 500;
68851Svbart@nginx.com                 goto done;
68951Svbart@nginx.com             }
69051Svbart@nginx.com 
691106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
69251Svbart@nginx.com         }
69351Svbart@nginx.com 
69451Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
69565Sigor@sysoev.ru             nxt_mp_destroy(mp);
69651Svbart@nginx.com             status = 500;
69751Svbart@nginx.com             goto done;
69844Svbart@nginx.com         }
69944Svbart@nginx.com 
700116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
701121Svbart@nginx.com             nxt_mp_destroy(mp);
702116Svbart@nginx.com             status = 400;
703116Svbart@nginx.com             nxt_str_set(&resp.json,
704116Svbart@nginx.com                         "{ \"error\": \"Invalid configuration.\" }");
705116Svbart@nginx.com             goto done;
706116Svbart@nginx.com         }
707116Svbart@nginx.com 
708*140Svbart@nginx.com         req->conf.root = value;
709*140Svbart@nginx.com         req->conf.pool = mp;
710*140Svbart@nginx.com 
711*140Svbart@nginx.com         if (nxt_controller_conf_apply(task, req) != NXT_OK) {
712121Svbart@nginx.com             nxt_mp_destroy(mp);
713121Svbart@nginx.com             status = 500;
714121Svbart@nginx.com             goto done;
715121Svbart@nginx.com         }
716121Svbart@nginx.com 
717*140Svbart@nginx.com         return;
71851Svbart@nginx.com     }
71951Svbart@nginx.com 
72051Svbart@nginx.com     status = 405;
72151Svbart@nginx.com 
72251Svbart@nginx.com done:
72351Svbart@nginx.com 
72451Svbart@nginx.com     switch (status) {
72551Svbart@nginx.com 
72651Svbart@nginx.com     case 200:
72751Svbart@nginx.com         nxt_str_set(&resp.status_line, "200 OK");
72851Svbart@nginx.com         break;
72951Svbart@nginx.com 
73051Svbart@nginx.com     case 400:
73151Svbart@nginx.com         nxt_str_set(&resp.status_line, "400 Bad Request");
73251Svbart@nginx.com         break;
73351Svbart@nginx.com 
73451Svbart@nginx.com     case 404:
73551Svbart@nginx.com         nxt_str_set(&resp.status_line, "404 Not Found");
736106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"error\": \"Value doesn't exist.\" }");
73751Svbart@nginx.com         break;
73851Svbart@nginx.com 
73951Svbart@nginx.com     case 405:
74044Svbart@nginx.com         nxt_str_set(&resp.status_line, "405 Method Not Allowed");
741106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"error\": \"Invalid method.\" }");
74251Svbart@nginx.com         break;
74351Svbart@nginx.com 
74451Svbart@nginx.com     case 500:
74551Svbart@nginx.com         nxt_str_set(&resp.status_line, "500 Internal Server Error");
746106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"error\": \"Memory allocation failed.\" }");
74751Svbart@nginx.com         break;
74829Svbart@nginx.com     }
74927Svbart@nginx.com 
750*140Svbart@nginx.com     nxt_controller_response(task, req, &resp);
75127Svbart@nginx.com }
75227Svbart@nginx.com 
75327Svbart@nginx.com 
75427Svbart@nginx.com static nxt_int_t
755*140Svbart@nginx.com nxt_controller_conf_apply(nxt_task_t *task, nxt_controller_request_t *req)
756*140Svbart@nginx.com {
757*140Svbart@nginx.com     nxt_int_t  rc;
758*140Svbart@nginx.com 
759*140Svbart@nginx.com     if (nxt_controller_current_request != NULL) {
760*140Svbart@nginx.com         nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
761*140Svbart@nginx.com         return NXT_OK;
762*140Svbart@nginx.com     }
763*140Svbart@nginx.com 
764*140Svbart@nginx.com     rc = nxt_controller_conf_pass(task, req->conf.root);
765*140Svbart@nginx.com 
766*140Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
767*140Svbart@nginx.com         return NXT_ERROR;
768*140Svbart@nginx.com     }
769*140Svbart@nginx.com 
770*140Svbart@nginx.com     nxt_controller_current_request = req;
771*140Svbart@nginx.com 
772*140Svbart@nginx.com     return NXT_OK;
773*140Svbart@nginx.com }
774*140Svbart@nginx.com 
775*140Svbart@nginx.com 
776*140Svbart@nginx.com void
777*140Svbart@nginx.com nxt_port_controller_data_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg)
778*140Svbart@nginx.com {
779*140Svbart@nginx.com     size_t                     size, dump_size;
780*140Svbart@nginx.com     nxt_buf_t                  *b;
781*140Svbart@nginx.com     nxt_controller_request_t   *req;
782*140Svbart@nginx.com     nxt_controller_response_t  resp;
783*140Svbart@nginx.com 
784*140Svbart@nginx.com     b = msg->buf;
785*140Svbart@nginx.com     size = b->mem.free - b->mem.pos;
786*140Svbart@nginx.com 
787*140Svbart@nginx.com     dump_size = size > 300 ? 300 : size;
788*140Svbart@nginx.com 
789*140Svbart@nginx.com     nxt_debug(task, "contoller data: %*s ...", dump_size, b->mem.pos);
790*140Svbart@nginx.com 
791*140Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
792*140Svbart@nginx.com 
793*140Svbart@nginx.com     req = nxt_controller_current_request;
794*140Svbart@nginx.com     nxt_controller_current_request = NULL;
795*140Svbart@nginx.com 
796*140Svbart@nginx.com     if (size == 2 && nxt_memcmp(b->mem.pos, "OK", 2) == 0) {
797*140Svbart@nginx.com 
798*140Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
799*140Svbart@nginx.com 
800*140Svbart@nginx.com         nxt_controller_conf = req->conf;
801*140Svbart@nginx.com 
802*140Svbart@nginx.com         nxt_str_set(&resp.status_line, "200 OK");
803*140Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"success\": \"Reconfiguration done.\" }");
804*140Svbart@nginx.com 
805*140Svbart@nginx.com     } else {
806*140Svbart@nginx.com         nxt_mp_destroy(req->conf.pool);
807*140Svbart@nginx.com 
808*140Svbart@nginx.com         nxt_str_set(&resp.status_line, "500 Internal Server Error");
809*140Svbart@nginx.com         nxt_str_set(&resp.json,
810*140Svbart@nginx.com                     "{ \"error\": \"Failed to apply new configuration.\" }");
811*140Svbart@nginx.com     }
812*140Svbart@nginx.com 
813*140Svbart@nginx.com     nxt_controller_response(task, req, &resp);
814*140Svbart@nginx.com 
815*140Svbart@nginx.com     nxt_controller_process_waiting(task);
816*140Svbart@nginx.com }
817*140Svbart@nginx.com 
818*140Svbart@nginx.com 
819*140Svbart@nginx.com static void
820*140Svbart@nginx.com nxt_controller_process_waiting(nxt_task_t *task)
821*140Svbart@nginx.com {
822*140Svbart@nginx.com     nxt_controller_request_t   *req;
823*140Svbart@nginx.com     nxt_controller_response_t  resp;
824*140Svbart@nginx.com 
825*140Svbart@nginx.com     nxt_queue_each(req, &nxt_controller_waiting_requests,
826*140Svbart@nginx.com                    nxt_controller_request_t, link)
827*140Svbart@nginx.com     {
828*140Svbart@nginx.com         nxt_queue_remove(&req->link);
829*140Svbart@nginx.com 
830*140Svbart@nginx.com         if (nxt_fast_path(nxt_controller_conf_apply(task, req) == NXT_OK)) {
831*140Svbart@nginx.com             return;
832*140Svbart@nginx.com         }
833*140Svbart@nginx.com 
834*140Svbart@nginx.com         nxt_mp_destroy(req->conf.pool);
835*140Svbart@nginx.com 
836*140Svbart@nginx.com         nxt_str_set(&resp.status_line, "500 Internal Server Error");
837*140Svbart@nginx.com         nxt_str_set(&resp.json,
838*140Svbart@nginx.com                     "{ \"error\": \"Memory allocation failed.\" }");
839*140Svbart@nginx.com 
840*140Svbart@nginx.com         nxt_controller_response(task, req, &resp);
841*140Svbart@nginx.com 
842*140Svbart@nginx.com     } nxt_queue_loop;
843*140Svbart@nginx.com }
844*140Svbart@nginx.com 
845*140Svbart@nginx.com 
846*140Svbart@nginx.com static nxt_int_t
847*140Svbart@nginx.com nxt_controller_conf_pass(nxt_task_t *task, nxt_conf_value_t *conf)
848121Svbart@nginx.com {
849135Svbart@nginx.com     size_t         size;
850135Svbart@nginx.com     nxt_buf_t      *b;
851135Svbart@nginx.com     nxt_port_t     *port;
852135Svbart@nginx.com     nxt_runtime_t  *rt;
853121Svbart@nginx.com 
854121Svbart@nginx.com     rt = task->thread->runtime;
855121Svbart@nginx.com 
856121Svbart@nginx.com     nxt_runtime_port_each(rt, port) {
857121Svbart@nginx.com 
858121Svbart@nginx.com         if (port->type == NXT_PROCESS_ROUTER) {
859121Svbart@nginx.com             break;
860121Svbart@nginx.com         }
861121Svbart@nginx.com 
862121Svbart@nginx.com     } nxt_runtime_port_loop;
863121Svbart@nginx.com 
864135Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
865121Svbart@nginx.com 
866121Svbart@nginx.com     b = nxt_port_mmap_get_buf(task, port, size);
867121Svbart@nginx.com 
868135Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
869121Svbart@nginx.com 
870*140Svbart@nginx.com     return nxt_port_socket_write(task, port, NXT_PORT_MSG_DATA, -1, 0, 0, b);
871121Svbart@nginx.com }
872121Svbart@nginx.com 
873121Svbart@nginx.com 
874*140Svbart@nginx.com 
875*140Svbart@nginx.com static void
876*140Svbart@nginx.com nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req,
87744Svbart@nginx.com     nxt_controller_response_t *resp)
87833Svbart@nginx.com {
879*140Svbart@nginx.com     size_t      size;
880*140Svbart@nginx.com     nxt_buf_t   *b;
881*140Svbart@nginx.com     nxt_conn_t  *c;
882*140Svbart@nginx.com 
883*140Svbart@nginx.com     c = req->conn;
88433Svbart@nginx.com 
88545Svbart@nginx.com     size = sizeof("HTTP/1.0 " "\r\n\r\n") - 1 + resp->status_line.length;
88633Svbart@nginx.com 
88744Svbart@nginx.com     b = nxt_buf_mem_alloc(c->mem_pool, size, 0);
88833Svbart@nginx.com     if (nxt_slow_path(b == NULL)) {
889*140Svbart@nginx.com         nxt_controller_conn_close(task, c, req);
890*140Svbart@nginx.com         return;
89133Svbart@nginx.com     }
89233Svbart@nginx.com 
89344Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, "HTTP/1.0 ", sizeof("HTTP/1.0 ") - 1);
89444Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, resp->status_line.start,
89544Svbart@nginx.com                              resp->status_line.length);
89644Svbart@nginx.com 
89744Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, "\r\n\r\n", sizeof("\r\n\r\n") - 1);
89844Svbart@nginx.com 
89945Svbart@nginx.com     b->next = nxt_controller_response_body(resp, c->mem_pool);
90045Svbart@nginx.com 
90145Svbart@nginx.com     if (nxt_slow_path(b->next == NULL)) {
902*140Svbart@nginx.com         nxt_controller_conn_close(task, c, req);
903*140Svbart@nginx.com         return;
90444Svbart@nginx.com     }
90533Svbart@nginx.com 
90633Svbart@nginx.com     c->write = b;
90744Svbart@nginx.com     c->write_state = &nxt_controller_conn_write_state;
90833Svbart@nginx.com 
90962Sigor@sysoev.ru     nxt_conn_write(task->thread->engine, c);
91033Svbart@nginx.com }
91145Svbart@nginx.com 
91245Svbart@nginx.com 
91345Svbart@nginx.com static nxt_buf_t *
91465Sigor@sysoev.ru nxt_controller_response_body(nxt_controller_response_t *resp, nxt_mp_t *pool)
91545Svbart@nginx.com {
91645Svbart@nginx.com     size_t                  size;
91745Svbart@nginx.com     nxt_buf_t               *b;
918106Svbart@nginx.com     nxt_conf_value_t        *value;
91945Svbart@nginx.com     nxt_conf_json_pretty_t  pretty;
92045Svbart@nginx.com 
921106Svbart@nginx.com     if (resp->conf) {
922106Svbart@nginx.com         value = resp->conf;
92345Svbart@nginx.com 
92445Svbart@nginx.com     } else {
925106Svbart@nginx.com         value = nxt_conf_json_parse_str(pool, &resp->json);
92645Svbart@nginx.com 
92745Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
92845Svbart@nginx.com             return NULL;
92945Svbart@nginx.com         }
93045Svbart@nginx.com     }
93145Svbart@nginx.com 
93245Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
93345Svbart@nginx.com 
934106Svbart@nginx.com     size = nxt_conf_json_length(value, &pretty) + 2;
93545Svbart@nginx.com 
93645Svbart@nginx.com     b = nxt_buf_mem_alloc(pool, size, 0);
93745Svbart@nginx.com     if (nxt_slow_path(b == NULL)) {
93845Svbart@nginx.com         return NULL;
93945Svbart@nginx.com     }
94045Svbart@nginx.com 
94145Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
94245Svbart@nginx.com 
943106Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, value, &pretty);
94445Svbart@nginx.com 
94545Svbart@nginx.com     *b->mem.free++ = '\r';
94645Svbart@nginx.com     *b->mem.free++ = '\n';
94745Svbart@nginx.com 
94845Svbart@nginx.com     return b;
94945Svbart@nginx.com }
950