xref: /unit/src/nxt_controller.c (revision 208)
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;
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 {
30*208Svbart@nginx.com     nxt_uint_t        status;
31106Svbart@nginx.com     nxt_conf_value_t  *conf;
32*208Svbart@nginx.com 
33*208Svbart@nginx.com     u_char            *title;
34*208Svbart@nginx.com     u_char            *detail;
35*208Svbart@nginx.com     ssize_t           offset;
36*208Svbart@nginx.com     nxt_uint_t        line;
37*208Svbart@nginx.com     nxt_uint_t        column;
3844Svbart@nginx.com } nxt_controller_response_t;
3944Svbart@nginx.com 
4044Svbart@nginx.com 
4120Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
4220Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
4362Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
4420Sigor@sysoev.ru     uintptr_t data);
4520Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
4620Sigor@sysoev.ru     void *data);
4720Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
4820Sigor@sysoev.ru     void *data);
4927Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
5027Svbart@nginx.com     void *data);
5127Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
5227Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
5327Svbart@nginx.com     void *data);
5427Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
5527Svbart@nginx.com     void *data);
5620Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
5720Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
5820Sigor@sysoev.ru 
5927Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
6067Svbart@nginx.com     nxt_http_field_t *field, nxt_log_t *log);
6127Svbart@nginx.com 
6227Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
63140Svbart@nginx.com     nxt_controller_request_t *req);
64121Svbart@nginx.com static nxt_int_t nxt_controller_conf_apply(nxt_task_t *task,
65140Svbart@nginx.com     nxt_controller_request_t *req);
66140Svbart@nginx.com static void nxt_controller_process_waiting(nxt_task_t *task);
67140Svbart@nginx.com static nxt_int_t nxt_controller_conf_pass(nxt_task_t *task,
68121Svbart@nginx.com     nxt_conf_value_t *conf);
69140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task,
70140Svbart@nginx.com     nxt_controller_request_t *req, nxt_controller_response_t *resp);
71*208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
72*208Svbart@nginx.com     struct tm *tm, size_t size, const char *format);
7327Svbart@nginx.com 
7427Svbart@nginx.com 
7560Svbart@nginx.com static nxt_http_fields_hash_entry_t  nxt_controller_request_fields[] = {
7627Svbart@nginx.com     { nxt_string("Content-Length"),
7727Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
7827Svbart@nginx.com 
7927Svbart@nginx.com     { nxt_null_string, NULL, 0 }
8027Svbart@nginx.com };
8127Svbart@nginx.com 
8260Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_fields_hash;
8327Svbart@nginx.com 
8444Svbart@nginx.com 
85140Svbart@nginx.com static nxt_controller_conf_t     nxt_controller_conf;
86140Svbart@nginx.com static nxt_queue_t               nxt_controller_waiting_requests;
87140Svbart@nginx.com static nxt_controller_request_t  *nxt_controller_current_request;
8827Svbart@nginx.com 
8920Sigor@sysoev.ru 
9020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
9127Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
9227Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
9320Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
9420Sigor@sysoev.ru 
9520Sigor@sysoev.ru 
9620Sigor@sysoev.ru nxt_int_t
97141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data)
9820Sigor@sysoev.ru {
9965Sigor@sysoev.ru     nxt_mp_t                *mp;
100141Smax.romanov@nginx.com     nxt_runtime_t           *rt;
101106Svbart@nginx.com     nxt_conf_value_t        *conf;
10227Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
10327Svbart@nginx.com 
10444Svbart@nginx.com     static const nxt_str_t json
105116Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
10644Svbart@nginx.com 
107141Smax.romanov@nginx.com     rt = task->thread->runtime;
108141Smax.romanov@nginx.com 
10960Svbart@nginx.com     hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
11060Svbart@nginx.com                                        rt->mem_pool);
11127Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
11227Svbart@nginx.com         return NXT_ERROR;
11327Svbart@nginx.com     }
11427Svbart@nginx.com 
11560Svbart@nginx.com     nxt_controller_fields_hash = hash;
11627Svbart@nginx.com 
11754Sigor@sysoev.ru     if (nxt_listen_event(task, rt->controller_socket) == NULL) {
11820Sigor@sysoev.ru         return NXT_ERROR;
11920Sigor@sysoev.ru     }
12020Sigor@sysoev.ru 
12165Sigor@sysoev.ru     mp = nxt_mp_create(1024, 128, 256, 32);
12244Svbart@nginx.com 
12344Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
12444Svbart@nginx.com         return NXT_ERROR;
12544Svbart@nginx.com     }
12644Svbart@nginx.com 
127106Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
12844Svbart@nginx.com 
12944Svbart@nginx.com     if (conf == NULL) {
13044Svbart@nginx.com         return NXT_ERROR;
13144Svbart@nginx.com     }
13244Svbart@nginx.com 
13344Svbart@nginx.com     nxt_controller_conf.root = conf;
13444Svbart@nginx.com     nxt_controller_conf.pool = mp;
13544Svbart@nginx.com 
136140Svbart@nginx.com     nxt_queue_init(&nxt_controller_waiting_requests);
137140Svbart@nginx.com 
13820Sigor@sysoev.ru     return NXT_OK;
13920Sigor@sysoev.ru }
14020Sigor@sysoev.ru 
14120Sigor@sysoev.ru 
14220Sigor@sysoev.ru nxt_int_t
14320Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
14420Sigor@sysoev.ru {
14520Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
14620Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
14720Sigor@sysoev.ru 
14820Sigor@sysoev.ru     sa = rt->controller_listen;
14920Sigor@sysoev.ru 
15020Sigor@sysoev.ru     if (rt->controller_listen == NULL) {
15120Sigor@sysoev.ru         sa = nxt_sockaddr_alloc(rt->mem_pool, sizeof(struct sockaddr_in),
15220Sigor@sysoev.ru                                 NXT_INET_ADDR_STR_LEN);
15320Sigor@sysoev.ru         if (sa == NULL) {
15420Sigor@sysoev.ru             return NXT_ERROR;
15520Sigor@sysoev.ru         }
15620Sigor@sysoev.ru 
15720Sigor@sysoev.ru         sa->type = SOCK_STREAM;
15820Sigor@sysoev.ru         sa->u.sockaddr_in.sin_family = AF_INET;
15920Sigor@sysoev.ru         sa->u.sockaddr_in.sin_port = htons(8443);
16020Sigor@sysoev.ru 
16120Sigor@sysoev.ru         nxt_sockaddr_text(sa);
16220Sigor@sysoev.ru 
16320Sigor@sysoev.ru         rt->controller_listen = sa;
16420Sigor@sysoev.ru     }
16520Sigor@sysoev.ru 
16665Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
16720Sigor@sysoev.ru     if (ls == NULL) {
16820Sigor@sysoev.ru         return NXT_ERROR;
16920Sigor@sysoev.ru     }
17020Sigor@sysoev.ru 
17120Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
17220Sigor@sysoev.ru                                        sa->socklen, sa->length);
17320Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
17420Sigor@sysoev.ru         return NXT_ERROR;
17520Sigor@sysoev.ru     }
17620Sigor@sysoev.ru 
17720Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
178103Sigor@sysoev.ru     ls->socklen = sa->socklen;
179103Sigor@sysoev.ru     ls->address_length = sa->length;
18020Sigor@sysoev.ru 
18120Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
18220Sigor@sysoev.ru 
18320Sigor@sysoev.ru     ls->socket = -1;
18420Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
18520Sigor@sysoev.ru     ls->read_after_accept = 1;
18620Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
18720Sigor@sysoev.ru 
18820Sigor@sysoev.ru #if 0
18920Sigor@sysoev.ru     /* STUB */
19065Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
19120Sigor@sysoev.ru     if (wq == NULL) {
19220Sigor@sysoev.ru         return NXT_ERROR;
19320Sigor@sysoev.ru     }
19420Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
19520Sigor@sysoev.ru     /**/
19620Sigor@sysoev.ru 
19720Sigor@sysoev.ru     ls->work_queue = wq;
19820Sigor@sysoev.ru #endif
19920Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
20020Sigor@sysoev.ru 
20120Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
20220Sigor@sysoev.ru         return NXT_ERROR;
20320Sigor@sysoev.ru     }
20420Sigor@sysoev.ru 
20520Sigor@sysoev.ru     rt->controller_socket = ls;
20620Sigor@sysoev.ru 
20720Sigor@sysoev.ru     return NXT_OK;
20820Sigor@sysoev.ru }
20920Sigor@sysoev.ru 
21020Sigor@sysoev.ru 
21120Sigor@sysoev.ru static void
21220Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
21320Sigor@sysoev.ru {
21427Svbart@nginx.com     nxt_buf_t                 *b;
21562Sigor@sysoev.ru     nxt_conn_t                *c;
21627Svbart@nginx.com     nxt_event_engine_t        *engine;
21727Svbart@nginx.com     nxt_controller_request_t  *r;
21820Sigor@sysoev.ru 
21920Sigor@sysoev.ru     c = obj;
22020Sigor@sysoev.ru 
22120Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
22220Sigor@sysoev.ru 
22365Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
22427Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
22527Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
22627Svbart@nginx.com         return;
22727Svbart@nginx.com     }
22827Svbart@nginx.com 
229140Svbart@nginx.com     r->conn = c;
230140Svbart@nginx.com 
23160Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
23260Svbart@nginx.com                       != NXT_OK))
23360Svbart@nginx.com     {
23460Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
23560Svbart@nginx.com         return;
23660Svbart@nginx.com     }
23727Svbart@nginx.com 
23867Svbart@nginx.com     r->parser.fields_hash = nxt_controller_fields_hash;
23967Svbart@nginx.com 
24020Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
24120Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
24220Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
24320Sigor@sysoev.ru         return;
24420Sigor@sysoev.ru     }
24520Sigor@sysoev.ru 
24620Sigor@sysoev.ru     c->read = b;
24727Svbart@nginx.com     c->socket.data = r;
24820Sigor@sysoev.ru     c->socket.read_ready = 1;
24920Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
25020Sigor@sysoev.ru 
25120Sigor@sysoev.ru     engine = task->thread->engine;
25220Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
25327Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
25420Sigor@sysoev.ru 
25562Sigor@sysoev.ru     nxt_conn_read(engine, c);
25620Sigor@sysoev.ru }
25720Sigor@sysoev.ru 
25820Sigor@sysoev.ru 
25920Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
26020Sigor@sysoev.ru     nxt_aligned(64) =
26120Sigor@sysoev.ru {
26256Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
26356Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
26456Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
26520Sigor@sysoev.ru 
26656Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
26756Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
26856Sigor@sysoev.ru     .timer_data = 60 * 1000,
26920Sigor@sysoev.ru };
27020Sigor@sysoev.ru 
27120Sigor@sysoev.ru 
27220Sigor@sysoev.ru static void
27320Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
27420Sigor@sysoev.ru {
27527Svbart@nginx.com     size_t                    preread;
27627Svbart@nginx.com     nxt_buf_t                 *b;
27727Svbart@nginx.com     nxt_int_t                 rc;
27862Sigor@sysoev.ru     nxt_conn_t                *c;
27927Svbart@nginx.com     nxt_controller_request_t  *r;
28020Sigor@sysoev.ru 
28120Sigor@sysoev.ru     c = obj;
28227Svbart@nginx.com     r = data;
28320Sigor@sysoev.ru 
28420Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
28520Sigor@sysoev.ru 
28627Svbart@nginx.com     nxt_queue_remove(&c->link);
28727Svbart@nginx.com     nxt_queue_self(&c->link);
28827Svbart@nginx.com 
28927Svbart@nginx.com     b = c->read;
29027Svbart@nginx.com 
29127Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
29227Svbart@nginx.com 
29327Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
29427Svbart@nginx.com 
29527Svbart@nginx.com         if (rc == NXT_AGAIN) {
29627Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
29727Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
29827Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
29927Svbart@nginx.com                 return;
30027Svbart@nginx.com             }
30127Svbart@nginx.com 
30262Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
30327Svbart@nginx.com             return;
30427Svbart@nginx.com         }
30527Svbart@nginx.com 
30627Svbart@nginx.com         /* rc == NXT_ERROR */
30727Svbart@nginx.com 
30827Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
30927Svbart@nginx.com 
31027Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
31127Svbart@nginx.com         return;
31227Svbart@nginx.com     }
31327Svbart@nginx.com 
31467Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, r, task->log);
31560Svbart@nginx.com 
31660Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
31760Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
31860Svbart@nginx.com         return;
31960Svbart@nginx.com     }
32060Svbart@nginx.com 
32127Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
32227Svbart@nginx.com 
32327Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
324107Svbart@nginx.com                     "body length: %uz, preread: %uz",
32527Svbart@nginx.com                     r->length, preread);
32627Svbart@nginx.com 
32727Svbart@nginx.com     if (preread >= r->length) {
328140Svbart@nginx.com         nxt_controller_process_request(task, r);
32927Svbart@nginx.com         return;
33027Svbart@nginx.com     }
33127Svbart@nginx.com 
33227Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
33327Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
33427Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
33527Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
33627Svbart@nginx.com             return;
33727Svbart@nginx.com         }
33827Svbart@nginx.com 
33927Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
34027Svbart@nginx.com 
34127Svbart@nginx.com         c->read = b;
34227Svbart@nginx.com     }
34327Svbart@nginx.com 
34427Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
34527Svbart@nginx.com 
34662Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
34720Sigor@sysoev.ru }
34820Sigor@sysoev.ru 
34920Sigor@sysoev.ru 
35020Sigor@sysoev.ru static nxt_msec_t
35162Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
35220Sigor@sysoev.ru {
35320Sigor@sysoev.ru     return (nxt_msec_t) data;
35420Sigor@sysoev.ru }
35520Sigor@sysoev.ru 
35620Sigor@sysoev.ru 
35720Sigor@sysoev.ru static void
35820Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
35920Sigor@sysoev.ru {
36062Sigor@sysoev.ru     nxt_conn_t  *c;
36120Sigor@sysoev.ru 
36220Sigor@sysoev.ru     c = obj;
36320Sigor@sysoev.ru 
36420Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
36520Sigor@sysoev.ru 
36627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
36720Sigor@sysoev.ru }
36820Sigor@sysoev.ru 
36920Sigor@sysoev.ru 
37020Sigor@sysoev.ru static void
37120Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
37220Sigor@sysoev.ru {
37362Sigor@sysoev.ru     nxt_timer_t  *timer;
37462Sigor@sysoev.ru     nxt_conn_t   *c;
37520Sigor@sysoev.ru 
37662Sigor@sysoev.ru     timer = obj;
37720Sigor@sysoev.ru 
37862Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
37920Sigor@sysoev.ru     c->socket.timedout = 1;
38020Sigor@sysoev.ru     c->socket.closed = 1;
38120Sigor@sysoev.ru 
38220Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
38320Sigor@sysoev.ru 
38427Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
38527Svbart@nginx.com }
38627Svbart@nginx.com 
38727Svbart@nginx.com 
38827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
38927Svbart@nginx.com     nxt_aligned(64) =
39027Svbart@nginx.com {
39156Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
39256Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
39356Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
39427Svbart@nginx.com 
39556Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
39656Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
39756Sigor@sysoev.ru     .timer_data = 60 * 1000,
39856Sigor@sysoev.ru     .timer_autoreset = 1,
39927Svbart@nginx.com };
40027Svbart@nginx.com 
40127Svbart@nginx.com 
40227Svbart@nginx.com static void
40327Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
40427Svbart@nginx.com {
405107Svbart@nginx.com     size_t                    read;
406107Svbart@nginx.com     nxt_buf_t                 *b;
407107Svbart@nginx.com     nxt_conn_t                *c;
408107Svbart@nginx.com     nxt_controller_request_t  *r;
40927Svbart@nginx.com 
41027Svbart@nginx.com     c = obj;
411107Svbart@nginx.com     r = data;
41227Svbart@nginx.com     b = c->read;
41327Svbart@nginx.com 
414107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
41527Svbart@nginx.com 
416107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
417107Svbart@nginx.com               read, r->length);
41827Svbart@nginx.com 
419107Svbart@nginx.com     if (read >= r->length) {
420140Svbart@nginx.com         nxt_controller_process_request(task, r);
42127Svbart@nginx.com         return;
42227Svbart@nginx.com     }
42327Svbart@nginx.com 
42462Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
42527Svbart@nginx.com }
42627Svbart@nginx.com 
42727Svbart@nginx.com 
42827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
42927Svbart@nginx.com     nxt_aligned(64) =
43027Svbart@nginx.com {
43156Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
43256Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
43327Svbart@nginx.com 
43456Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
43556Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
43656Sigor@sysoev.ru     .timer_data = 60 * 1000,
43756Sigor@sysoev.ru     .timer_autoreset = 1,
43827Svbart@nginx.com };
43927Svbart@nginx.com 
44027Svbart@nginx.com 
44127Svbart@nginx.com static void
44227Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
44327Svbart@nginx.com {
44462Sigor@sysoev.ru     nxt_buf_t   *b;
44562Sigor@sysoev.ru     nxt_conn_t  *c;
44627Svbart@nginx.com 
44727Svbart@nginx.com     c = obj;
44827Svbart@nginx.com 
44927Svbart@nginx.com     nxt_debug(task, "controller conn write");
45027Svbart@nginx.com 
45127Svbart@nginx.com     b = c->write;
45227Svbart@nginx.com 
45327Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
45462Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
45527Svbart@nginx.com         return;
45627Svbart@nginx.com     }
45727Svbart@nginx.com 
45827Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
45927Svbart@nginx.com 
46027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
46127Svbart@nginx.com }
46227Svbart@nginx.com 
46327Svbart@nginx.com 
46427Svbart@nginx.com static void
46527Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
46627Svbart@nginx.com {
46762Sigor@sysoev.ru     nxt_conn_t  *c;
46827Svbart@nginx.com 
46927Svbart@nginx.com     c = obj;
47027Svbart@nginx.com 
47127Svbart@nginx.com     nxt_debug(task, "controller conn write error");
47227Svbart@nginx.com 
47327Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
47427Svbart@nginx.com }
47527Svbart@nginx.com 
47627Svbart@nginx.com 
47727Svbart@nginx.com static void
47827Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
47927Svbart@nginx.com {
48062Sigor@sysoev.ru     nxt_conn_t   *c;
48162Sigor@sysoev.ru     nxt_timer_t  *timer;
48227Svbart@nginx.com 
48362Sigor@sysoev.ru     timer = obj;
48427Svbart@nginx.com 
48562Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
48627Svbart@nginx.com     c->socket.timedout = 1;
48727Svbart@nginx.com     c->socket.closed = 1;
48827Svbart@nginx.com 
48927Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
49027Svbart@nginx.com 
49127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
49220Sigor@sysoev.ru }
49320Sigor@sysoev.ru 
49420Sigor@sysoev.ru 
49520Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
49620Sigor@sysoev.ru     nxt_aligned(64) =
49720Sigor@sysoev.ru {
49856Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
49920Sigor@sysoev.ru };
50020Sigor@sysoev.ru 
50120Sigor@sysoev.ru 
50220Sigor@sysoev.ru static void
50320Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
50420Sigor@sysoev.ru {
50562Sigor@sysoev.ru     nxt_conn_t  *c;
50620Sigor@sysoev.ru 
50720Sigor@sysoev.ru     c = obj;
50820Sigor@sysoev.ru 
50920Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
51020Sigor@sysoev.ru 
51127Svbart@nginx.com     nxt_queue_remove(&c->link);
51227Svbart@nginx.com 
51320Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
51420Sigor@sysoev.ru 
51562Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
51620Sigor@sysoev.ru }
51720Sigor@sysoev.ru 
51820Sigor@sysoev.ru 
51920Sigor@sysoev.ru static void
52020Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
52120Sigor@sysoev.ru {
52262Sigor@sysoev.ru     nxt_conn_t  *c;
52320Sigor@sysoev.ru 
52420Sigor@sysoev.ru     c = obj;
52520Sigor@sysoev.ru 
52620Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
52720Sigor@sysoev.ru 
52865Sigor@sysoev.ru     nxt_mp_destroy(c->mem_pool);
52920Sigor@sysoev.ru 
53020Sigor@sysoev.ru     //nxt_free(c);
53120Sigor@sysoev.ru }
53227Svbart@nginx.com 
53327Svbart@nginx.com 
53427Svbart@nginx.com static nxt_int_t
53560Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
53667Svbart@nginx.com     nxt_log_t *log)
53727Svbart@nginx.com {
53827Svbart@nginx.com     off_t                     length;
53927Svbart@nginx.com     nxt_controller_request_t  *r;
54027Svbart@nginx.com 
54127Svbart@nginx.com     r = ctx;
54227Svbart@nginx.com 
54360Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
54427Svbart@nginx.com 
54527Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
546107Svbart@nginx.com 
547107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
548107Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
549107Svbart@nginx.com             return NXT_ERROR;
550107Svbart@nginx.com         }
55127Svbart@nginx.com 
55227Svbart@nginx.com         r->length = length;
55327Svbart@nginx.com         return NXT_OK;
55427Svbart@nginx.com     }
55527Svbart@nginx.com 
55660Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
55727Svbart@nginx.com 
55827Svbart@nginx.com     return NXT_ERROR;
55927Svbart@nginx.com }
56027Svbart@nginx.com 
56127Svbart@nginx.com 
56227Svbart@nginx.com static void
563140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
56427Svbart@nginx.com {
56565Sigor@sysoev.ru     nxt_mp_t                   *mp;
56651Svbart@nginx.com     nxt_int_t                  rc;
56746Svbart@nginx.com     nxt_str_t                  path;
568140Svbart@nginx.com     nxt_conn_t                 *c;
56951Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
570106Svbart@nginx.com     nxt_conf_op_t              *ops;
571106Svbart@nginx.com     nxt_conf_value_t           *value;
572*208Svbart@nginx.com     nxt_conf_json_error_t      error;
57344Svbart@nginx.com     nxt_controller_response_t  resp;
57444Svbart@nginx.com 
57551Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
57651Svbart@nginx.com 
577140Svbart@nginx.com     c = req->conn;
578112Smax.romanov@nginx.com     path = req->parser.path;
57951Svbart@nginx.com 
58051Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
58151Svbart@nginx.com         path.length--;
58251Svbart@nginx.com     }
58351Svbart@nginx.com 
58444Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
58544Svbart@nginx.com 
58644Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
58746Svbart@nginx.com 
588106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
58951Svbart@nginx.com 
59051Svbart@nginx.com         if (value == NULL) {
591*208Svbart@nginx.com             goto not_found;
59251Svbart@nginx.com         }
59351Svbart@nginx.com 
594*208Svbart@nginx.com         resp.status = 200;
595106Svbart@nginx.com         resp.conf = value;
59646Svbart@nginx.com 
597*208Svbart@nginx.com         nxt_controller_response(task, req, &resp);
598*208Svbart@nginx.com         return;
59951Svbart@nginx.com     }
60051Svbart@nginx.com 
60151Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
60246Svbart@nginx.com 
60365Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
60451Svbart@nginx.com 
60551Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
606*208Svbart@nginx.com             goto alloc_fail;
60746Svbart@nginx.com         }
60846Svbart@nginx.com 
60951Svbart@nginx.com         mbuf = &c->read->mem;
61051Svbart@nginx.com 
611*208Svbart@nginx.com         nxt_memzero(&error, sizeof(nxt_conf_json_error_t));
612*208Svbart@nginx.com 
613*208Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error);
61451Svbart@nginx.com 
61551Svbart@nginx.com         if (value == NULL) {
61665Sigor@sysoev.ru             nxt_mp_destroy(mp);
617*208Svbart@nginx.com 
618*208Svbart@nginx.com             if (error.pos == NULL) {
619*208Svbart@nginx.com                 goto alloc_fail;
620*208Svbart@nginx.com             }
621*208Svbart@nginx.com 
622*208Svbart@nginx.com             resp.status = 400;
623*208Svbart@nginx.com             resp.title = (u_char *) "Invalid JSON.";
624*208Svbart@nginx.com             resp.detail = error.detail;
625*208Svbart@nginx.com             resp.offset = error.pos - mbuf->pos;
626*208Svbart@nginx.com 
627*208Svbart@nginx.com             nxt_conf_json_position(mbuf->pos, error.pos,
628*208Svbart@nginx.com                                    &resp.line, &resp.column);
629*208Svbart@nginx.com 
630*208Svbart@nginx.com             nxt_controller_response(task, req, &resp);
631*208Svbart@nginx.com             return;
63251Svbart@nginx.com         }
63351Svbart@nginx.com 
63451Svbart@nginx.com         if (path.length != 1) {
635106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
636106Svbart@nginx.com                                      nxt_controller_conf.root,
637106Svbart@nginx.com                                      &path, value);
63846Svbart@nginx.com 
63951Svbart@nginx.com             if (rc != NXT_OK) {
64051Svbart@nginx.com                 if (rc == NXT_DECLINED) {
641*208Svbart@nginx.com                     goto not_found;
64251Svbart@nginx.com                 }
64346Svbart@nginx.com 
644*208Svbart@nginx.com                 goto alloc_fail;
64551Svbart@nginx.com             }
64651Svbart@nginx.com 
647106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
64851Svbart@nginx.com 
64951Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
65065Sigor@sysoev.ru                 nxt_mp_destroy(mp);
651*208Svbart@nginx.com                 goto alloc_fail;
65251Svbart@nginx.com             }
65346Svbart@nginx.com         }
65444Svbart@nginx.com 
655116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
656121Svbart@nginx.com             nxt_mp_destroy(mp);
657*208Svbart@nginx.com             goto invalid_conf;
658116Svbart@nginx.com         }
659116Svbart@nginx.com 
660140Svbart@nginx.com         req->conf.root = value;
661140Svbart@nginx.com         req->conf.pool = mp;
662140Svbart@nginx.com 
663140Svbart@nginx.com         if (nxt_controller_conf_apply(task, req) != NXT_OK) {
664121Svbart@nginx.com             nxt_mp_destroy(mp);
665*208Svbart@nginx.com             goto alloc_fail;
666121Svbart@nginx.com         }
667121Svbart@nginx.com 
668140Svbart@nginx.com         return;
66951Svbart@nginx.com     }
67027Svbart@nginx.com 
67151Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
67251Svbart@nginx.com 
67351Svbart@nginx.com         if (path.length == 1) {
67465Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
67544Svbart@nginx.com 
67651Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
677*208Svbart@nginx.com                 goto alloc_fail;
67851Svbart@nginx.com             }
67951Svbart@nginx.com 
680106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
68127Svbart@nginx.com 
68244Svbart@nginx.com         } else {
683106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
684106Svbart@nginx.com                                      nxt_controller_conf.root,
685106Svbart@nginx.com                                      &path, NULL);
68651Svbart@nginx.com 
68751Svbart@nginx.com             if (rc != NXT_OK) {
68851Svbart@nginx.com                 if (rc == NXT_DECLINED) {
689*208Svbart@nginx.com                     goto not_found;
69051Svbart@nginx.com                 }
69151Svbart@nginx.com 
692*208Svbart@nginx.com                 goto alloc_fail;
69351Svbart@nginx.com             }
69451Svbart@nginx.com 
69565Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
69651Svbart@nginx.com 
69751Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
698*208Svbart@nginx.com                 goto alloc_fail;
69951Svbart@nginx.com             }
70051Svbart@nginx.com 
701106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
70251Svbart@nginx.com         }
70351Svbart@nginx.com 
70451Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
70565Sigor@sysoev.ru             nxt_mp_destroy(mp);
706*208Svbart@nginx.com             goto alloc_fail;
70744Svbart@nginx.com         }
70844Svbart@nginx.com 
709116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
710121Svbart@nginx.com             nxt_mp_destroy(mp);
711*208Svbart@nginx.com             goto invalid_conf;
712116Svbart@nginx.com         }
713116Svbart@nginx.com 
714140Svbart@nginx.com         req->conf.root = value;
715140Svbart@nginx.com         req->conf.pool = mp;
716140Svbart@nginx.com 
717140Svbart@nginx.com         if (nxt_controller_conf_apply(task, req) != NXT_OK) {
718121Svbart@nginx.com             nxt_mp_destroy(mp);
719*208Svbart@nginx.com             goto alloc_fail;
720121Svbart@nginx.com         }
721121Svbart@nginx.com 
722140Svbart@nginx.com         return;
72351Svbart@nginx.com     }
72451Svbart@nginx.com 
725*208Svbart@nginx.com     resp.status = 405;
726*208Svbart@nginx.com     resp.title = (u_char *) "Invalid method.";
727*208Svbart@nginx.com     resp.offset = -1;
72851Svbart@nginx.com 
729*208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
730*208Svbart@nginx.com     return;
73151Svbart@nginx.com 
732*208Svbart@nginx.com alloc_fail:
73351Svbart@nginx.com 
734*208Svbart@nginx.com     resp.status = 500;
735*208Svbart@nginx.com     resp.title = (u_char *) "Memory allocation failed.";
736*208Svbart@nginx.com     resp.offset = -1;
73727Svbart@nginx.com 
738140Svbart@nginx.com     nxt_controller_response(task, req, &resp);
739*208Svbart@nginx.com     return;
740*208Svbart@nginx.com 
741*208Svbart@nginx.com not_found:
742*208Svbart@nginx.com 
743*208Svbart@nginx.com     resp.status = 404;
744*208Svbart@nginx.com     resp.title = (u_char *) "Value doesn't exist.";
745*208Svbart@nginx.com     resp.offset = -1;
746*208Svbart@nginx.com 
747*208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
748*208Svbart@nginx.com     return;
749*208Svbart@nginx.com 
750*208Svbart@nginx.com invalid_conf:
751*208Svbart@nginx.com 
752*208Svbart@nginx.com     resp.status = 400;
753*208Svbart@nginx.com     resp.title = (u_char *) "Invalid configuration.";
754*208Svbart@nginx.com     resp.offset = -1;
755*208Svbart@nginx.com 
756*208Svbart@nginx.com     nxt_controller_response(task, req, &resp);
757*208Svbart@nginx.com     return;
75827Svbart@nginx.com }
75927Svbart@nginx.com 
76027Svbart@nginx.com 
76127Svbart@nginx.com static nxt_int_t
762140Svbart@nginx.com nxt_controller_conf_apply(nxt_task_t *task, nxt_controller_request_t *req)
763140Svbart@nginx.com {
764140Svbart@nginx.com     nxt_int_t  rc;
765140Svbart@nginx.com 
766140Svbart@nginx.com     if (nxt_controller_current_request != NULL) {
767140Svbart@nginx.com         nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link);
768140Svbart@nginx.com         return NXT_OK;
769140Svbart@nginx.com     }
770140Svbart@nginx.com 
771140Svbart@nginx.com     rc = nxt_controller_conf_pass(task, req->conf.root);
772140Svbart@nginx.com 
773140Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
774140Svbart@nginx.com         return NXT_ERROR;
775140Svbart@nginx.com     }
776140Svbart@nginx.com 
777140Svbart@nginx.com     nxt_controller_current_request = req;
778140Svbart@nginx.com 
779140Svbart@nginx.com     return NXT_OK;
780140Svbart@nginx.com }
781140Svbart@nginx.com 
782140Svbart@nginx.com 
783193Smax.romanov@nginx.com static void
784193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg,
785193Smax.romanov@nginx.com     void *data)
786140Svbart@nginx.com {
787140Svbart@nginx.com     nxt_controller_request_t   *req;
788140Svbart@nginx.com     nxt_controller_response_t  resp;
789140Svbart@nginx.com 
790201Svbart@nginx.com     nxt_debug(task, "controller conf ready: %*s",
791201Svbart@nginx.com               nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos);
792140Svbart@nginx.com 
793140Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
794140Svbart@nginx.com 
795140Svbart@nginx.com     req = nxt_controller_current_request;
796140Svbart@nginx.com     nxt_controller_current_request = NULL;
797140Svbart@nginx.com 
798193Smax.romanov@nginx.com     if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) {
799140Svbart@nginx.com         nxt_mp_destroy(nxt_controller_conf.pool);
800140Svbart@nginx.com 
801140Svbart@nginx.com         nxt_controller_conf = req->conf;
802140Svbart@nginx.com 
803*208Svbart@nginx.com         resp.status = 200;
804*208Svbart@nginx.com         resp.title = (u_char *) "Reconfiguration done.";
805140Svbart@nginx.com 
806140Svbart@nginx.com     } else {
807140Svbart@nginx.com         nxt_mp_destroy(req->conf.pool);
808140Svbart@nginx.com 
809*208Svbart@nginx.com         resp.status = 500;
810*208Svbart@nginx.com         resp.title = (u_char *) "Failed to apply new configuration.";
811*208Svbart@nginx.com         resp.offset = -1;
812140Svbart@nginx.com     }
813140Svbart@nginx.com 
814140Svbart@nginx.com     nxt_controller_response(task, req, &resp);
815140Svbart@nginx.com 
816140Svbart@nginx.com     nxt_controller_process_waiting(task);
817140Svbart@nginx.com }
818140Svbart@nginx.com 
819140Svbart@nginx.com 
820140Svbart@nginx.com static void
821140Svbart@nginx.com nxt_controller_process_waiting(nxt_task_t *task)
822140Svbart@nginx.com {
823140Svbart@nginx.com     nxt_controller_request_t   *req;
824140Svbart@nginx.com     nxt_controller_response_t  resp;
825140Svbart@nginx.com 
826140Svbart@nginx.com     nxt_queue_each(req, &nxt_controller_waiting_requests,
827140Svbart@nginx.com                    nxt_controller_request_t, link)
828140Svbart@nginx.com     {
829140Svbart@nginx.com         nxt_queue_remove(&req->link);
830140Svbart@nginx.com 
831140Svbart@nginx.com         if (nxt_fast_path(nxt_controller_conf_apply(task, req) == NXT_OK)) {
832140Svbart@nginx.com             return;
833140Svbart@nginx.com         }
834140Svbart@nginx.com 
835140Svbart@nginx.com         nxt_mp_destroy(req->conf.pool);
836140Svbart@nginx.com 
837*208Svbart@nginx.com         nxt_memzero(&resp, sizeof(nxt_controller_response_t));
838*208Svbart@nginx.com 
839*208Svbart@nginx.com         resp.status = 500;
840*208Svbart@nginx.com         resp.title = (u_char *) "Memory allocation failed.";
841*208Svbart@nginx.com         resp.offset = -1;
842140Svbart@nginx.com 
843140Svbart@nginx.com         nxt_controller_response(task, req, &resp);
844140Svbart@nginx.com 
845140Svbart@nginx.com     } nxt_queue_loop;
846140Svbart@nginx.com }
847140Svbart@nginx.com 
848140Svbart@nginx.com 
849140Svbart@nginx.com static nxt_int_t
850140Svbart@nginx.com nxt_controller_conf_pass(nxt_task_t *task, nxt_conf_value_t *conf)
851121Svbart@nginx.com {
852135Svbart@nginx.com     size_t         size;
853193Smax.romanov@nginx.com     uint32_t       stream;
854193Smax.romanov@nginx.com     nxt_int_t      rc;
855135Svbart@nginx.com     nxt_buf_t      *b;
856193Smax.romanov@nginx.com     nxt_port_t     *router_port, *controller_port;
857135Svbart@nginx.com     nxt_runtime_t  *rt;
858121Svbart@nginx.com 
859121Svbart@nginx.com     rt = task->thread->runtime;
860121Svbart@nginx.com 
861193Smax.romanov@nginx.com     router_port = rt->port_by_type[NXT_PROCESS_ROUTER];
862193Smax.romanov@nginx.com     controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER];
863121Svbart@nginx.com 
864135Svbart@nginx.com     size = nxt_conf_json_length(conf, NULL);
865121Svbart@nginx.com 
866193Smax.romanov@nginx.com     b = nxt_port_mmap_get_buf(task, router_port, size);
867121Svbart@nginx.com 
868135Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL);
869121Svbart@nginx.com 
870193Smax.romanov@nginx.com     stream = nxt_port_rpc_register_handler(task, controller_port,
871193Smax.romanov@nginx.com                                            nxt_controller_conf_handler,
872193Smax.romanov@nginx.com                                            nxt_controller_conf_handler,
873193Smax.romanov@nginx.com                                            router_port->pid, NULL);
874193Smax.romanov@nginx.com 
875193Smax.romanov@nginx.com     rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1,
876193Smax.romanov@nginx.com                                stream, controller_port->id, b);
877193Smax.romanov@nginx.com 
878193Smax.romanov@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
879193Smax.romanov@nginx.com         nxt_port_rpc_cancel(task, controller_port, stream);
880193Smax.romanov@nginx.com     }
881193Smax.romanov@nginx.com 
882193Smax.romanov@nginx.com     return rc;
883121Svbart@nginx.com }
884121Svbart@nginx.com 
885121Svbart@nginx.com 
886140Svbart@nginx.com static void
887140Svbart@nginx.com nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req,
88844Svbart@nginx.com     nxt_controller_response_t *resp)
88933Svbart@nginx.com {
890*208Svbart@nginx.com     size_t                  size;
891*208Svbart@nginx.com     nxt_str_t               status_line, str;
892*208Svbart@nginx.com     nxt_buf_t               *b, *body;
893*208Svbart@nginx.com     nxt_conn_t              *c;
894*208Svbart@nginx.com     nxt_uint_t              n;
895*208Svbart@nginx.com     nxt_conf_value_t        *value, *location;
896*208Svbart@nginx.com     nxt_conf_json_pretty_t  pretty;
897*208Svbart@nginx.com 
898*208Svbart@nginx.com     static nxt_str_t  success_str = nxt_string("success");
899*208Svbart@nginx.com     static nxt_str_t  error_str = nxt_string("error");
900*208Svbart@nginx.com     static nxt_str_t  detail_str = nxt_string("detail");
901*208Svbart@nginx.com     static nxt_str_t  location_str = nxt_string("location");
902*208Svbart@nginx.com     static nxt_str_t  offset_str = nxt_string("offset");
903*208Svbart@nginx.com     static nxt_str_t  line_str = nxt_string("line");
904*208Svbart@nginx.com     static nxt_str_t  column_str = nxt_string("column");
905*208Svbart@nginx.com 
906*208Svbart@nginx.com     static nxt_time_string_t  date_cache = {
907*208Svbart@nginx.com         (nxt_atomic_uint_t) -1,
908*208Svbart@nginx.com         nxt_controller_date,
909*208Svbart@nginx.com         "%s, %02d %s %4d %02d:%02d:%02d GMT",
910*208Svbart@nginx.com         sizeof("Wed, 31 Dec 1986 16:40:00 GMT") - 1,
911*208Svbart@nginx.com         NXT_THREAD_TIME_GMT,
912*208Svbart@nginx.com         NXT_THREAD_TIME_SEC,
913*208Svbart@nginx.com     };
914*208Svbart@nginx.com 
915*208Svbart@nginx.com     switch (resp->status) {
916*208Svbart@nginx.com 
917*208Svbart@nginx.com     case 200:
918*208Svbart@nginx.com         nxt_str_set(&status_line, "200 OK");
919*208Svbart@nginx.com         break;
920*208Svbart@nginx.com 
921*208Svbart@nginx.com     case 400:
922*208Svbart@nginx.com         nxt_str_set(&status_line, "400 Bad Request");
923*208Svbart@nginx.com         break;
924*208Svbart@nginx.com 
925*208Svbart@nginx.com     case 404:
926*208Svbart@nginx.com         nxt_str_set(&status_line, "404 Not Found");
927*208Svbart@nginx.com         break;
928*208Svbart@nginx.com 
929*208Svbart@nginx.com     case 405:
930*208Svbart@nginx.com         nxt_str_set(&status_line, "405 Method Not Allowed");
931*208Svbart@nginx.com         break;
932*208Svbart@nginx.com 
933*208Svbart@nginx.com     case 500:
934*208Svbart@nginx.com         nxt_str_set(&status_line, "500 Internal Server Error");
935*208Svbart@nginx.com         break;
936*208Svbart@nginx.com     }
937140Svbart@nginx.com 
938140Svbart@nginx.com     c = req->conn;
939*208Svbart@nginx.com     value = resp->conf;
94033Svbart@nginx.com 
941*208Svbart@nginx.com     if (value == NULL) {
942*208Svbart@nginx.com         n = 1
943*208Svbart@nginx.com             + (resp->detail != NULL)
944*208Svbart@nginx.com             + (resp->status >= 400 && resp->offset != -1);
945*208Svbart@nginx.com 
946*208Svbart@nginx.com         value = nxt_conf_create_object(c->mem_pool, n);
947*208Svbart@nginx.com 
948*208Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
949*208Svbart@nginx.com             nxt_controller_conn_close(task, c, req);
950*208Svbart@nginx.com             return;
951*208Svbart@nginx.com         }
952*208Svbart@nginx.com 
953*208Svbart@nginx.com         str.length = nxt_strlen(resp->title);
954*208Svbart@nginx.com         str.start = resp->title;
955*208Svbart@nginx.com 
956*208Svbart@nginx.com         if (resp->status < 400) {
957*208Svbart@nginx.com             nxt_conf_set_member_string(value, &success_str, &str, 0);
958*208Svbart@nginx.com 
959*208Svbart@nginx.com         } else {
960*208Svbart@nginx.com             nxt_conf_set_member_string(value, &error_str, &str, 0);
961*208Svbart@nginx.com         }
962*208Svbart@nginx.com 
963*208Svbart@nginx.com         n = 0;
964*208Svbart@nginx.com 
965*208Svbart@nginx.com         if (resp->detail != NULL) {
966*208Svbart@nginx.com             str.length = nxt_strlen(resp->detail);
967*208Svbart@nginx.com             str.start = resp->detail;
968*208Svbart@nginx.com 
969*208Svbart@nginx.com             n++;
970*208Svbart@nginx.com 
971*208Svbart@nginx.com             nxt_conf_set_member_string(value, &detail_str, &str, n);
972*208Svbart@nginx.com         }
973*208Svbart@nginx.com 
974*208Svbart@nginx.com         if (resp->status >= 400 && resp->offset != -1) {
975*208Svbart@nginx.com             n++;
976*208Svbart@nginx.com 
977*208Svbart@nginx.com             location = nxt_conf_create_object(c->mem_pool,
978*208Svbart@nginx.com                                               resp->line != 0 ? 3 : 1);
979*208Svbart@nginx.com 
980*208Svbart@nginx.com             nxt_conf_set_member(value, &location_str, location, n);
981*208Svbart@nginx.com 
982*208Svbart@nginx.com             nxt_conf_set_member_integer(location, &offset_str, resp->offset, 0);
983*208Svbart@nginx.com 
984*208Svbart@nginx.com             if (resp->line != 0) {
985*208Svbart@nginx.com                 nxt_conf_set_member_integer(location, &line_str,
986*208Svbart@nginx.com                                             resp->line, 1);
987*208Svbart@nginx.com 
988*208Svbart@nginx.com                 nxt_conf_set_member_integer(location, &column_str,
989*208Svbart@nginx.com                                             resp->column, 2);
990*208Svbart@nginx.com             }
991*208Svbart@nginx.com         }
992*208Svbart@nginx.com     }
993*208Svbart@nginx.com 
994*208Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
995*208Svbart@nginx.com 
996*208Svbart@nginx.com     size = nxt_conf_json_length(value, &pretty) + 2;
997*208Svbart@nginx.com 
998*208Svbart@nginx.com     body = nxt_buf_mem_alloc(c->mem_pool, size, 0);
999*208Svbart@nginx.com     if (nxt_slow_path(body == NULL)) {
1000*208Svbart@nginx.com         nxt_controller_conn_close(task, c, req);
1001*208Svbart@nginx.com         return;
1002*208Svbart@nginx.com     }
1003*208Svbart@nginx.com 
1004*208Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
1005*208Svbart@nginx.com 
1006*208Svbart@nginx.com     body->mem.free = nxt_conf_json_print(body->mem.free, value, &pretty);
1007*208Svbart@nginx.com 
1008*208Svbart@nginx.com     body->mem.free = nxt_cpymem(body->mem.free, "\r\n", 2);
1009*208Svbart@nginx.com 
1010*208Svbart@nginx.com     size = sizeof("HTTP/1.1 " "\r\n") - 1 + status_line.length
1011*208Svbart@nginx.com            + sizeof("Server: nginext/0.1\r\n") - 1
1012*208Svbart@nginx.com            + sizeof("Date: Wed, 31 Dec 1986 16:40:00 GMT\r\n") - 1
1013*208Svbart@nginx.com            + sizeof("Content-Type: application/json\r\n") - 1
1014*208Svbart@nginx.com            + sizeof("Content-Length: " "\r\n") - 1 + NXT_SIZE_T_LEN
1015*208Svbart@nginx.com            + sizeof("Connection: close\r\n") - 1
1016*208Svbart@nginx.com            + sizeof("\r\n") - 1;
101733Svbart@nginx.com 
101844Svbart@nginx.com     b = nxt_buf_mem_alloc(c->mem_pool, size, 0);
101933Svbart@nginx.com     if (nxt_slow_path(b == NULL)) {
1020140Svbart@nginx.com         nxt_controller_conn_close(task, c, req);
1021140Svbart@nginx.com         return;
102233Svbart@nginx.com     }
102333Svbart@nginx.com 
1024*208Svbart@nginx.com     b->next = body;
1025*208Svbart@nginx.com 
1026*208Svbart@nginx.com     nxt_str_set(&str, "HTTP/1.1 ");
102744Svbart@nginx.com 
1028*208Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length);
1029*208Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, status_line.start,
1030*208Svbart@nginx.com                              status_line.length);
1031*208Svbart@nginx.com 
1032*208Svbart@nginx.com     nxt_str_set(&str, "\r\n"
1033*208Svbart@nginx.com                       "Server: nginext/0.1\r\n"
1034*208Svbart@nginx.com                       "Date: ");
1035*208Svbart@nginx.com 
1036*208Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length);
1037