xref: /unit/src/nxt_controller.c (revision 116)
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;
2333Svbart@nginx.com 
2444Svbart@nginx.com     nxt_controller_conf_t     conf;
2527Svbart@nginx.com } nxt_controller_request_t;
2627Svbart@nginx.com 
2727Svbart@nginx.com 
2844Svbart@nginx.com typedef struct {
29106Svbart@nginx.com     nxt_str_t         status_line;
30106Svbart@nginx.com     nxt_conf_value_t  *conf;
31106Svbart@nginx.com     nxt_str_t         json;
3244Svbart@nginx.com } nxt_controller_response_t;
3344Svbart@nginx.com 
3444Svbart@nginx.com 
3520Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data);
3620Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data);
3762Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c,
3820Sigor@sysoev.ru     uintptr_t data);
3920Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj,
4020Sigor@sysoev.ru     void *data);
4120Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj,
4220Sigor@sysoev.ru     void *data);
4327Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj,
4427Svbart@nginx.com     void *data);
4527Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data);
4627Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
4727Svbart@nginx.com     void *data);
4827Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
4927Svbart@nginx.com     void *data);
5020Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
5120Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
5220Sigor@sysoev.ru 
5327Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx,
5467Svbart@nginx.com     nxt_http_field_t *field, nxt_log_t *log);
5527Svbart@nginx.com 
5627Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
5762Sigor@sysoev.ru     nxt_conn_t *c, nxt_controller_request_t *r);
5862Sigor@sysoev.ru static nxt_int_t nxt_controller_response(nxt_task_t *task, nxt_conn_t *c,
5944Svbart@nginx.com     nxt_controller_response_t *resp);
6045Svbart@nginx.com static nxt_buf_t *nxt_controller_response_body(nxt_controller_response_t *resp,
6165Sigor@sysoev.ru     nxt_mp_t *pool);
6227Svbart@nginx.com 
6327Svbart@nginx.com 
6460Svbart@nginx.com static nxt_http_fields_hash_entry_t  nxt_controller_request_fields[] = {
6527Svbart@nginx.com     { nxt_string("Content-Length"),
6627Svbart@nginx.com       &nxt_controller_request_content_length, 0 },
6727Svbart@nginx.com 
6827Svbart@nginx.com     { nxt_null_string, NULL, 0 }
6927Svbart@nginx.com };
7027Svbart@nginx.com 
7160Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_fields_hash;
7227Svbart@nginx.com 
7344Svbart@nginx.com 
7444Svbart@nginx.com static nxt_controller_conf_t  nxt_controller_conf;
7527Svbart@nginx.com 
7620Sigor@sysoev.ru 
7720Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state;
7827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state;
7927Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state;
8020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state;
8120Sigor@sysoev.ru 
8220Sigor@sysoev.ru 
8320Sigor@sysoev.ru nxt_int_t
8420Sigor@sysoev.ru nxt_controller_start(nxt_task_t *task, nxt_runtime_t *rt)
8520Sigor@sysoev.ru {
8665Sigor@sysoev.ru     nxt_mp_t                *mp;
87106Svbart@nginx.com     nxt_conf_value_t        *conf;
8827Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
8927Svbart@nginx.com 
9044Svbart@nginx.com     static const nxt_str_t json
91*116Svbart@nginx.com         = nxt_string("{ \"listeners\": {}, \"applications\": {} }");
9244Svbart@nginx.com 
9360Svbart@nginx.com     hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
9460Svbart@nginx.com                                        rt->mem_pool);
9527Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
9627Svbart@nginx.com         return NXT_ERROR;
9727Svbart@nginx.com     }
9827Svbart@nginx.com 
9960Svbart@nginx.com     nxt_controller_fields_hash = hash;
10027Svbart@nginx.com 
10154Sigor@sysoev.ru     if (nxt_listen_event(task, rt->controller_socket) == NULL) {
10220Sigor@sysoev.ru         return NXT_ERROR;
10320Sigor@sysoev.ru     }
10420Sigor@sysoev.ru 
10565Sigor@sysoev.ru     mp = nxt_mp_create(1024, 128, 256, 32);
10644Svbart@nginx.com 
10744Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
10844Svbart@nginx.com         return NXT_ERROR;
10944Svbart@nginx.com     }
11044Svbart@nginx.com 
111106Svbart@nginx.com     conf = nxt_conf_json_parse_str(mp, &json);
11244Svbart@nginx.com 
11344Svbart@nginx.com     if (conf == NULL) {
11444Svbart@nginx.com         return NXT_ERROR;
11544Svbart@nginx.com     }
11644Svbart@nginx.com 
11744Svbart@nginx.com     nxt_controller_conf.root = conf;
11844Svbart@nginx.com     nxt_controller_conf.pool = mp;
11944Svbart@nginx.com 
12020Sigor@sysoev.ru     return NXT_OK;
12120Sigor@sysoev.ru }
12220Sigor@sysoev.ru 
12320Sigor@sysoev.ru 
12420Sigor@sysoev.ru nxt_int_t
12520Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt)
12620Sigor@sysoev.ru {
12720Sigor@sysoev.ru     nxt_sockaddr_t       *sa;
12820Sigor@sysoev.ru     nxt_listen_socket_t  *ls;
12920Sigor@sysoev.ru 
13020Sigor@sysoev.ru     sa = rt->controller_listen;
13120Sigor@sysoev.ru 
13220Sigor@sysoev.ru     if (rt->controller_listen == NULL) {
13320Sigor@sysoev.ru         sa = nxt_sockaddr_alloc(rt->mem_pool, sizeof(struct sockaddr_in),
13420Sigor@sysoev.ru                                 NXT_INET_ADDR_STR_LEN);
13520Sigor@sysoev.ru         if (sa == NULL) {
13620Sigor@sysoev.ru             return NXT_ERROR;
13720Sigor@sysoev.ru         }
13820Sigor@sysoev.ru 
13920Sigor@sysoev.ru         sa->type = SOCK_STREAM;
14020Sigor@sysoev.ru         sa->u.sockaddr_in.sin_family = AF_INET;
14120Sigor@sysoev.ru         sa->u.sockaddr_in.sin_port = htons(8443);
14220Sigor@sysoev.ru 
14320Sigor@sysoev.ru         nxt_sockaddr_text(sa);
14420Sigor@sysoev.ru 
14520Sigor@sysoev.ru         rt->controller_listen = sa;
14620Sigor@sysoev.ru     }
14720Sigor@sysoev.ru 
14865Sigor@sysoev.ru     ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t));
14920Sigor@sysoev.ru     if (ls == NULL) {
15020Sigor@sysoev.ru         return NXT_ERROR;
15120Sigor@sysoev.ru     }
15220Sigor@sysoev.ru 
15320Sigor@sysoev.ru     ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr,
15420Sigor@sysoev.ru                                        sa->socklen, sa->length);
15520Sigor@sysoev.ru     if (ls->sockaddr == NULL) {
15620Sigor@sysoev.ru         return NXT_ERROR;
15720Sigor@sysoev.ru     }
15820Sigor@sysoev.ru 
15920Sigor@sysoev.ru     ls->sockaddr->type = sa->type;
160103Sigor@sysoev.ru     ls->socklen = sa->socklen;
161103Sigor@sysoev.ru     ls->address_length = sa->length;
16220Sigor@sysoev.ru 
16320Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
16420Sigor@sysoev.ru 
16520Sigor@sysoev.ru     ls->socket = -1;
16620Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
16720Sigor@sysoev.ru     ls->read_after_accept = 1;
16820Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
16920Sigor@sysoev.ru 
17020Sigor@sysoev.ru #if 0
17120Sigor@sysoev.ru     /* STUB */
17265Sigor@sysoev.ru     wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t));
17320Sigor@sysoev.ru     if (wq == NULL) {
17420Sigor@sysoev.ru         return NXT_ERROR;
17520Sigor@sysoev.ru     }
17620Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
17720Sigor@sysoev.ru     /**/
17820Sigor@sysoev.ru 
17920Sigor@sysoev.ru     ls->work_queue = wq;
18020Sigor@sysoev.ru #endif
18120Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
18220Sigor@sysoev.ru 
18320Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
18420Sigor@sysoev.ru         return NXT_ERROR;
18520Sigor@sysoev.ru     }
18620Sigor@sysoev.ru 
18720Sigor@sysoev.ru     rt->controller_socket = ls;
18820Sigor@sysoev.ru 
18920Sigor@sysoev.ru     return NXT_OK;
19020Sigor@sysoev.ru }
19120Sigor@sysoev.ru 
19220Sigor@sysoev.ru 
19320Sigor@sysoev.ru static void
19420Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
19520Sigor@sysoev.ru {
19627Svbart@nginx.com     nxt_buf_t                 *b;
19762Sigor@sysoev.ru     nxt_conn_t                *c;
19827Svbart@nginx.com     nxt_event_engine_t        *engine;
19927Svbart@nginx.com     nxt_controller_request_t  *r;
20020Sigor@sysoev.ru 
20120Sigor@sysoev.ru     c = obj;
20220Sigor@sysoev.ru 
20320Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
20420Sigor@sysoev.ru 
20565Sigor@sysoev.ru     r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t));
20627Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
20727Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
20827Svbart@nginx.com         return;
20927Svbart@nginx.com     }
21027Svbart@nginx.com 
21160Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
21260Svbart@nginx.com                       != NXT_OK))
21360Svbart@nginx.com     {
21460Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
21560Svbart@nginx.com         return;
21660Svbart@nginx.com     }
21727Svbart@nginx.com 
21867Svbart@nginx.com     r->parser.fields_hash = nxt_controller_fields_hash;
21967Svbart@nginx.com 
22020Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
22120Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
22220Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
22320Sigor@sysoev.ru         return;
22420Sigor@sysoev.ru     }
22520Sigor@sysoev.ru 
22620Sigor@sysoev.ru     c->read = b;
22727Svbart@nginx.com     c->socket.data = r;
22820Sigor@sysoev.ru     c->socket.read_ready = 1;
22920Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
23020Sigor@sysoev.ru 
23120Sigor@sysoev.ru     engine = task->thread->engine;
23220Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
23327Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
23420Sigor@sysoev.ru 
23562Sigor@sysoev.ru     nxt_conn_read(engine, c);
23620Sigor@sysoev.ru }
23720Sigor@sysoev.ru 
23820Sigor@sysoev.ru 
23920Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
24020Sigor@sysoev.ru     nxt_aligned(64) =
24120Sigor@sysoev.ru {
24256Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
24356Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
24456Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
24520Sigor@sysoev.ru 
24656Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
24756Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
24856Sigor@sysoev.ru     .timer_data = 60 * 1000,
24920Sigor@sysoev.ru };
25020Sigor@sysoev.ru 
25120Sigor@sysoev.ru 
25220Sigor@sysoev.ru static void
25320Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
25420Sigor@sysoev.ru {
25527Svbart@nginx.com     size_t                    preread;
25627Svbart@nginx.com     nxt_buf_t                 *b;
25727Svbart@nginx.com     nxt_int_t                 rc;
25862Sigor@sysoev.ru     nxt_conn_t                *c;
25927Svbart@nginx.com     nxt_controller_request_t  *r;
26020Sigor@sysoev.ru 
26120Sigor@sysoev.ru     c = obj;
26227Svbart@nginx.com     r = data;
26320Sigor@sysoev.ru 
26420Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
26520Sigor@sysoev.ru 
26627Svbart@nginx.com     nxt_queue_remove(&c->link);
26727Svbart@nginx.com     nxt_queue_self(&c->link);
26827Svbart@nginx.com 
26927Svbart@nginx.com     b = c->read;
27027Svbart@nginx.com 
27127Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
27227Svbart@nginx.com 
27327Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
27427Svbart@nginx.com 
27527Svbart@nginx.com         if (rc == NXT_AGAIN) {
27627Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
27727Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
27827Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
27927Svbart@nginx.com                 return;
28027Svbart@nginx.com             }
28127Svbart@nginx.com 
28262Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
28327Svbart@nginx.com             return;
28427Svbart@nginx.com         }
28527Svbart@nginx.com 
28627Svbart@nginx.com         /* rc == NXT_ERROR */
28727Svbart@nginx.com 
28827Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
28927Svbart@nginx.com 
29027Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
29127Svbart@nginx.com         return;
29227Svbart@nginx.com     }
29327Svbart@nginx.com 
29467Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, r, task->log);
29560Svbart@nginx.com 
29660Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
29760Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
29860Svbart@nginx.com         return;
29960Svbart@nginx.com     }
30060Svbart@nginx.com 
30127Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
30227Svbart@nginx.com 
30327Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
304107Svbart@nginx.com                     "body length: %uz, preread: %uz",
30527Svbart@nginx.com                     r->length, preread);
30627Svbart@nginx.com 
30727Svbart@nginx.com     if (preread >= r->length) {
30827Svbart@nginx.com         nxt_controller_process_request(task, c, r);
30927Svbart@nginx.com         return;
31027Svbart@nginx.com     }
31127Svbart@nginx.com 
31227Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
31327Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
31427Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
31527Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
31627Svbart@nginx.com             return;
31727Svbart@nginx.com         }
31827Svbart@nginx.com 
31927Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
32027Svbart@nginx.com 
32127Svbart@nginx.com         c->read = b;
32227Svbart@nginx.com     }
32327Svbart@nginx.com 
32427Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
32527Svbart@nginx.com 
32662Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
32720Sigor@sysoev.ru }
32820Sigor@sysoev.ru 
32920Sigor@sysoev.ru 
33020Sigor@sysoev.ru static nxt_msec_t
33162Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
33220Sigor@sysoev.ru {
33320Sigor@sysoev.ru     return (nxt_msec_t) data;
33420Sigor@sysoev.ru }
33520Sigor@sysoev.ru 
33620Sigor@sysoev.ru 
33720Sigor@sysoev.ru static void
33820Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
33920Sigor@sysoev.ru {
34062Sigor@sysoev.ru     nxt_conn_t  *c;
34120Sigor@sysoev.ru 
34220Sigor@sysoev.ru     c = obj;
34320Sigor@sysoev.ru 
34420Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
34520Sigor@sysoev.ru 
34627Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
34720Sigor@sysoev.ru }
34820Sigor@sysoev.ru 
34920Sigor@sysoev.ru 
35020Sigor@sysoev.ru static void
35120Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
35220Sigor@sysoev.ru {
35362Sigor@sysoev.ru     nxt_timer_t  *timer;
35462Sigor@sysoev.ru     nxt_conn_t   *c;
35520Sigor@sysoev.ru 
35662Sigor@sysoev.ru     timer = obj;
35720Sigor@sysoev.ru 
35862Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
35920Sigor@sysoev.ru     c->socket.timedout = 1;
36020Sigor@sysoev.ru     c->socket.closed = 1;
36120Sigor@sysoev.ru 
36220Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
36320Sigor@sysoev.ru 
36427Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
36527Svbart@nginx.com }
36627Svbart@nginx.com 
36727Svbart@nginx.com 
36827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
36927Svbart@nginx.com     nxt_aligned(64) =
37027Svbart@nginx.com {
37156Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
37256Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
37356Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
37427Svbart@nginx.com 
37556Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
37656Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
37756Sigor@sysoev.ru     .timer_data = 60 * 1000,
37856Sigor@sysoev.ru     .timer_autoreset = 1,
37927Svbart@nginx.com };
38027Svbart@nginx.com 
38127Svbart@nginx.com 
38227Svbart@nginx.com static void
38327Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
38427Svbart@nginx.com {
385107Svbart@nginx.com     size_t                    read;
386107Svbart@nginx.com     nxt_buf_t                 *b;
387107Svbart@nginx.com     nxt_conn_t                *c;
388107Svbart@nginx.com     nxt_controller_request_t  *r;
38927Svbart@nginx.com 
39027Svbart@nginx.com     c = obj;
391107Svbart@nginx.com     r = data;
39227Svbart@nginx.com     b = c->read;
39327Svbart@nginx.com 
394107Svbart@nginx.com     read = nxt_buf_mem_used_size(&b->mem);
39527Svbart@nginx.com 
396107Svbart@nginx.com     nxt_debug(task, "controller conn body read: %uz of %uz",
397107Svbart@nginx.com               read, r->length);
39827Svbart@nginx.com 
399107Svbart@nginx.com     if (read >= r->length) {
40027Svbart@nginx.com         nxt_controller_process_request(task, c, data);
40127Svbart@nginx.com         return;
40227Svbart@nginx.com     }
40327Svbart@nginx.com 
40462Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
40527Svbart@nginx.com }
40627Svbart@nginx.com 
40727Svbart@nginx.com 
40827Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
40927Svbart@nginx.com     nxt_aligned(64) =
41027Svbart@nginx.com {
41156Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
41256Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
41327Svbart@nginx.com 
41456Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
41556Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
41656Sigor@sysoev.ru     .timer_data = 60 * 1000,
41756Sigor@sysoev.ru     .timer_autoreset = 1,
41827Svbart@nginx.com };
41927Svbart@nginx.com 
42027Svbart@nginx.com 
42127Svbart@nginx.com static void
42227Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
42327Svbart@nginx.com {
42462Sigor@sysoev.ru     nxt_buf_t   *b;
42562Sigor@sysoev.ru     nxt_conn_t  *c;
42627Svbart@nginx.com 
42727Svbart@nginx.com     c = obj;
42827Svbart@nginx.com 
42927Svbart@nginx.com     nxt_debug(task, "controller conn write");
43027Svbart@nginx.com 
43127Svbart@nginx.com     b = c->write;
43227Svbart@nginx.com 
43327Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
43462Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
43527Svbart@nginx.com         return;
43627Svbart@nginx.com     }
43727Svbart@nginx.com 
43827Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
43927Svbart@nginx.com 
44027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
44127Svbart@nginx.com }
44227Svbart@nginx.com 
44327Svbart@nginx.com 
44427Svbart@nginx.com static void
44527Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
44627Svbart@nginx.com {
44762Sigor@sysoev.ru     nxt_conn_t  *c;
44827Svbart@nginx.com 
44927Svbart@nginx.com     c = obj;
45027Svbart@nginx.com 
45127Svbart@nginx.com     nxt_debug(task, "controller conn write error");
45227Svbart@nginx.com 
45327Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
45427Svbart@nginx.com }
45527Svbart@nginx.com 
45627Svbart@nginx.com 
45727Svbart@nginx.com static void
45827Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
45927Svbart@nginx.com {
46062Sigor@sysoev.ru     nxt_conn_t   *c;
46162Sigor@sysoev.ru     nxt_timer_t  *timer;
46227Svbart@nginx.com 
46362Sigor@sysoev.ru     timer = obj;
46427Svbart@nginx.com 
46562Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
46627Svbart@nginx.com     c->socket.timedout = 1;
46727Svbart@nginx.com     c->socket.closed = 1;
46827Svbart@nginx.com 
46927Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
47027Svbart@nginx.com 
47127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
47220Sigor@sysoev.ru }
47320Sigor@sysoev.ru 
47420Sigor@sysoev.ru 
47520Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
47620Sigor@sysoev.ru     nxt_aligned(64) =
47720Sigor@sysoev.ru {
47856Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
47920Sigor@sysoev.ru };
48020Sigor@sysoev.ru 
48120Sigor@sysoev.ru 
48220Sigor@sysoev.ru static void
48320Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
48420Sigor@sysoev.ru {
48562Sigor@sysoev.ru     nxt_conn_t  *c;
48620Sigor@sysoev.ru 
48720Sigor@sysoev.ru     c = obj;
48820Sigor@sysoev.ru 
48920Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
49020Sigor@sysoev.ru 
49127Svbart@nginx.com     nxt_queue_remove(&c->link);
49227Svbart@nginx.com 
49320Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
49420Sigor@sysoev.ru 
49562Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
49620Sigor@sysoev.ru }
49720Sigor@sysoev.ru 
49820Sigor@sysoev.ru 
49920Sigor@sysoev.ru static void
50020Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
50120Sigor@sysoev.ru {
50262Sigor@sysoev.ru     nxt_conn_t  *c;
50320Sigor@sysoev.ru 
50420Sigor@sysoev.ru     c = obj;
50520Sigor@sysoev.ru 
50620Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
50720Sigor@sysoev.ru 
50865Sigor@sysoev.ru     nxt_mp_destroy(c->mem_pool);
50920Sigor@sysoev.ru 
51020Sigor@sysoev.ru     //nxt_free(c);
51120Sigor@sysoev.ru }
51227Svbart@nginx.com 
51327Svbart@nginx.com 
51427Svbart@nginx.com static nxt_int_t
51560Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
51667Svbart@nginx.com     nxt_log_t *log)
51727Svbart@nginx.com {
51827Svbart@nginx.com     off_t                     length;
51927Svbart@nginx.com     nxt_controller_request_t  *r;
52027Svbart@nginx.com 
52127Svbart@nginx.com     r = ctx;
52227Svbart@nginx.com 
52360Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
52427Svbart@nginx.com 
52527Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
526107Svbart@nginx.com 
527107Svbart@nginx.com         if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
528107Svbart@nginx.com             nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
529107Svbart@nginx.com             return NXT_ERROR;
530107Svbart@nginx.com         }
53127Svbart@nginx.com 
53227Svbart@nginx.com         r->length = length;
53327Svbart@nginx.com         return NXT_OK;
53427Svbart@nginx.com     }
53527Svbart@nginx.com 
53660Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
53727Svbart@nginx.com 
53827Svbart@nginx.com     return NXT_ERROR;
53927Svbart@nginx.com }
54027Svbart@nginx.com 
54127Svbart@nginx.com 
54227Svbart@nginx.com static void
54362Sigor@sysoev.ru nxt_controller_process_request(nxt_task_t *task, nxt_conn_t *c,
54444Svbart@nginx.com     nxt_controller_request_t *req)
54527Svbart@nginx.com {
54665Sigor@sysoev.ru     nxt_mp_t                   *mp;
54751Svbart@nginx.com     nxt_int_t                  rc;
54846Svbart@nginx.com     nxt_str_t                  path;
54951Svbart@nginx.com     nxt_uint_t                 status;
55051Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
551106Svbart@nginx.com     nxt_conf_op_t              *ops;
552106Svbart@nginx.com     nxt_conf_value_t           *value;
55344Svbart@nginx.com     nxt_controller_response_t  resp;
55444Svbart@nginx.com 
55551Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
55651Svbart@nginx.com 
557112Smax.romanov@nginx.com     path = req->parser.path;
55851Svbart@nginx.com 
55951Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
56051Svbart@nginx.com         path.length--;
56151Svbart@nginx.com     }
56251Svbart@nginx.com 
56344Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
56444Svbart@nginx.com 
56544Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
56646Svbart@nginx.com 
567106Svbart@nginx.com         value = nxt_conf_get_path(nxt_controller_conf.root, &path);
56851Svbart@nginx.com 
56951Svbart@nginx.com         if (value == NULL) {
57051Svbart@nginx.com             status = 404;
57151Svbart@nginx.com             goto done;
57251Svbart@nginx.com         }
57351Svbart@nginx.com 
574106Svbart@nginx.com         resp.conf = value;
57546Svbart@nginx.com 
57651Svbart@nginx.com         status = 200;
57751Svbart@nginx.com         goto done;
57851Svbart@nginx.com     }
57951Svbart@nginx.com 
58051Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
58146Svbart@nginx.com 
58265Sigor@sysoev.ru         mp = nxt_mp_create(1024, 128, 256, 32);
58351Svbart@nginx.com 
58451Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
58551Svbart@nginx.com             status = 500;
58651Svbart@nginx.com             goto done;
58746Svbart@nginx.com         }
58846Svbart@nginx.com 
58951Svbart@nginx.com         mbuf = &c->read->mem;
59051Svbart@nginx.com 
59191Svbart@nginx.com         value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free);
59251Svbart@nginx.com 
59351Svbart@nginx.com         if (value == NULL) {
59465Sigor@sysoev.ru             nxt_mp_destroy(mp);
59551Svbart@nginx.com             status = 400;
596*116Svbart@nginx.com             nxt_str_set(&resp.json, "{ \"error\": \"Invalid JSON.\" }");
59751Svbart@nginx.com             goto done;
59851Svbart@nginx.com         }
59951Svbart@nginx.com 
60051Svbart@nginx.com         if (path.length != 1) {
601106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
602106Svbart@nginx.com                                      nxt_controller_conf.root,
603106Svbart@nginx.com                                      &path, value);
60446Svbart@nginx.com 
60551Svbart@nginx.com             if (rc != NXT_OK) {
60651Svbart@nginx.com                 if (rc == NXT_DECLINED) {
60751Svbart@nginx.com                     status = 404;
60851Svbart@nginx.com                     goto done;
60951Svbart@nginx.com                 }
61046Svbart@nginx.com 
61151Svbart@nginx.com                 status = 500;
61251Svbart@nginx.com                 goto done;
61351Svbart@nginx.com             }
61451Svbart@nginx.com 
615106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
61651Svbart@nginx.com 
61751Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
61865Sigor@sysoev.ru                 nxt_mp_destroy(mp);
61951Svbart@nginx.com                 status = 500;
62051Svbart@nginx.com                 goto done;
62151Svbart@nginx.com             }
62246Svbart@nginx.com         }
62344Svbart@nginx.com 
624*116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
625*116Svbart@nginx.com             status = 400;
626*116Svbart@nginx.com             nxt_str_set(&resp.json,
627*116Svbart@nginx.com                         "{ \"error\": \"Invalid configuration.\" }");
628*116Svbart@nginx.com             goto done;
629*116Svbart@nginx.com         }
630*116Svbart@nginx.com 
63165Sigor@sysoev.ru         nxt_mp_destroy(nxt_controller_conf.pool);
63251Svbart@nginx.com 
63351Svbart@nginx.com         nxt_controller_conf.root = value;
63451Svbart@nginx.com         nxt_controller_conf.pool = mp;
63544Svbart@nginx.com 
636106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"success\": \"Updated.\" }");
63751Svbart@nginx.com 
63851Svbart@nginx.com         status = 200;
63951Svbart@nginx.com         goto done;
64051Svbart@nginx.com     }
64127Svbart@nginx.com 
64251Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
64351Svbart@nginx.com 
64451Svbart@nginx.com         if (path.length == 1) {
64565Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
64644Svbart@nginx.com 
64751Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
64851Svbart@nginx.com                 status = 500;
64951Svbart@nginx.com                 goto done;
65051Svbart@nginx.com             }
65151Svbart@nginx.com 
652106Svbart@nginx.com             value = nxt_conf_json_parse_str(mp, &empty_obj);
65327Svbart@nginx.com 
65444Svbart@nginx.com         } else {
655106Svbart@nginx.com             rc = nxt_conf_op_compile(c->mem_pool, &ops,
656106Svbart@nginx.com                                      nxt_controller_conf.root,
657106Svbart@nginx.com                                      &path, NULL);
65851Svbart@nginx.com 
65951Svbart@nginx.com             if (rc != NXT_OK) {
66051Svbart@nginx.com                 if (rc == NXT_DECLINED) {
66151Svbart@nginx.com                     status = 404;
66251Svbart@nginx.com                     goto done;
66351Svbart@nginx.com                 }
66451Svbart@nginx.com 
66551Svbart@nginx.com                 status = 500;
66651Svbart@nginx.com                 goto done;
66751Svbart@nginx.com             }
66851Svbart@nginx.com 
66965Sigor@sysoev.ru             mp = nxt_mp_create(1024, 128, 256, 32);
67051Svbart@nginx.com 
67151Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
67251Svbart@nginx.com                 status = 500;
67351Svbart@nginx.com                 goto done;
67451Svbart@nginx.com             }
67551Svbart@nginx.com 
676106Svbart@nginx.com             value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
67751Svbart@nginx.com         }
67851Svbart@nginx.com 
67951Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
68065Sigor@sysoev.ru             nxt_mp_destroy(mp);
68151Svbart@nginx.com             status = 500;
68251Svbart@nginx.com             goto done;
68344Svbart@nginx.com         }
68444Svbart@nginx.com 
685*116Svbart@nginx.com         if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
686*116Svbart@nginx.com             status = 400;
687*116Svbart@nginx.com             nxt_str_set(&resp.json,
688*116Svbart@nginx.com                         "{ \"error\": \"Invalid configuration.\" }");
689*116Svbart@nginx.com             goto done;
690*116Svbart@nginx.com         }
691*116Svbart@nginx.com 
69265Sigor@sysoev.ru         nxt_mp_destroy(nxt_controller_conf.pool);
69351Svbart@nginx.com 
69451Svbart@nginx.com         nxt_controller_conf.root = value;
69551Svbart@nginx.com         nxt_controller_conf.pool = mp;
69651Svbart@nginx.com 
697106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"success\": \"Deleted.\" }");
69851Svbart@nginx.com 
69951Svbart@nginx.com         status = 200;
70051Svbart@nginx.com         goto done;
70151Svbart@nginx.com     }
70251Svbart@nginx.com 
70351Svbart@nginx.com     status = 405;
70451Svbart@nginx.com 
70551Svbart@nginx.com done:
70651Svbart@nginx.com 
70751Svbart@nginx.com     switch (status) {
70851Svbart@nginx.com 
70951Svbart@nginx.com     case 200:
71051Svbart@nginx.com         nxt_str_set(&resp.status_line, "200 OK");
71151Svbart@nginx.com         break;
71251Svbart@nginx.com 
71351Svbart@nginx.com     case 400:
71451Svbart@nginx.com         nxt_str_set(&resp.status_line, "400 Bad Request");
71551Svbart@nginx.com         break;
71651Svbart@nginx.com 
71751Svbart@nginx.com     case 404:
71851Svbart@nginx.com         nxt_str_set(&resp.status_line, "404 Not Found");
719106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"error\": \"Value doesn't exist.\" }");
72051Svbart@nginx.com         break;
72151Svbart@nginx.com 
72251Svbart@nginx.com     case 405:
72344Svbart@nginx.com         nxt_str_set(&resp.status_line, "405 Method Not Allowed");
724106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"error\": \"Invalid method.\" }");
72551Svbart@nginx.com         break;
72651Svbart@nginx.com 
72751Svbart@nginx.com     case 500:
72851Svbart@nginx.com         nxt_str_set(&resp.status_line, "500 Internal Server Error");
729106Svbart@nginx.com         nxt_str_set(&resp.json, "{ \"error\": \"Memory allocation failed.\" }");
73051Svbart@nginx.com         break;
73129Svbart@nginx.com     }
73227Svbart@nginx.com 
73344Svbart@nginx.com     if (nxt_controller_response(task, c, &resp) != NXT_OK) {
73444Svbart@nginx.com         nxt_controller_conn_close(task, c, req);
73527Svbart@nginx.com     }
73627Svbart@nginx.com }
73727Svbart@nginx.com 
73827Svbart@nginx.com 
73927Svbart@nginx.com static nxt_int_t
74062Sigor@sysoev.ru nxt_controller_response(nxt_task_t *task, nxt_conn_t *c,
74144Svbart@nginx.com     nxt_controller_response_t *resp)
74233Svbart@nginx.com {
74344Svbart@nginx.com     size_t     size;
74433Svbart@nginx.com     nxt_buf_t  *b;
74533Svbart@nginx.com 
74645Svbart@nginx.com     size = sizeof("HTTP/1.0 " "\r\n\r\n") - 1 + resp->status_line.length;
74733Svbart@nginx.com 
74844Svbart@nginx.com     b = nxt_buf_mem_alloc(c->mem_pool, size, 0);
74933Svbart@nginx.com     if (nxt_slow_path(b == NULL)) {
75044Svbart@nginx.com         return NXT_ERROR;
75133Svbart@nginx.com     }
75233Svbart@nginx.com 
75344Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, "HTTP/1.0 ", sizeof("HTTP/1.0 ") - 1);
75444Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, resp->status_line.start,
75544Svbart@nginx.com                              resp->status_line.length);
75644Svbart@nginx.com 
75744Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, "\r\n\r\n", sizeof("\r\n\r\n") - 1);
75844Svbart@nginx.com 
75945Svbart@nginx.com     b->next = nxt_controller_response_body(resp, c->mem_pool);
76045Svbart@nginx.com 
76145Svbart@nginx.com     if (nxt_slow_path(b->next == NULL)) {
76245Svbart@nginx.com         return NXT_ERROR;
76344Svbart@nginx.com     }
76433Svbart@nginx.com 
76533Svbart@nginx.com     c->write = b;
76644Svbart@nginx.com     c->write_state = &nxt_controller_conn_write_state;
76733Svbart@nginx.com 
76862Sigor@sysoev.ru     nxt_conn_write(task->thread->engine, c);
76933Svbart@nginx.com 
77044Svbart@nginx.com     return NXT_OK;
77133Svbart@nginx.com }
77245Svbart@nginx.com 
77345Svbart@nginx.com 
77445Svbart@nginx.com static nxt_buf_t *
77565Sigor@sysoev.ru nxt_controller_response_body(nxt_controller_response_t *resp, nxt_mp_t *pool)
77645Svbart@nginx.com {
77745Svbart@nginx.com     size_t                  size;
77845Svbart@nginx.com     nxt_buf_t               *b;
779106Svbart@nginx.com     nxt_conf_value_t        *value;
78045Svbart@nginx.com     nxt_conf_json_pretty_t  pretty;
78145Svbart@nginx.com 
782106Svbart@nginx.com     if (resp->conf) {
783106Svbart@nginx.com         value = resp->conf;
78445Svbart@nginx.com 
78545Svbart@nginx.com     } else {
786106Svbart@nginx.com         value = nxt_conf_json_parse_str(pool, &resp->json);
78745Svbart@nginx.com 
78845Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
78945Svbart@nginx.com             return NULL;
79045Svbart@nginx.com         }
79145Svbart@nginx.com     }
79245Svbart@nginx.com 
79345Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
79445Svbart@nginx.com 
795106Svbart@nginx.com     size = nxt_conf_json_length(value, &pretty) + 2;
79645Svbart@nginx.com 
79745Svbart@nginx.com     b = nxt_buf_mem_alloc(pool, size, 0);
79845Svbart@nginx.com     if (nxt_slow_path(b == NULL)) {
79945Svbart@nginx.com         return NULL;
80045Svbart@nginx.com     }
80145Svbart@nginx.com 
80245Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
80345Svbart@nginx.com 
804106Svbart@nginx.com     b->mem.free = nxt_conf_json_print(b->mem.free, value, &pretty);
80545Svbart@nginx.com 
80645Svbart@nginx.com     *b->mem.free++ = '\r';
80745Svbart@nginx.com     *b->mem.free++ = '\n';
80845Svbart@nginx.com 
80945Svbart@nginx.com     return b;
81045Svbart@nginx.com }
811