xref: /unit/src/nxt_controller.c (revision 62)
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 {
1544Svbart@nginx.com     nxt_conf_json_value_t  *root;
1644Svbart@nginx.com     nxt_mem_pool_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 {
2944Svbart@nginx.com     nxt_str_t              status_line;
3044Svbart@nginx.com     nxt_conf_json_value_t  *json_value;
3144Svbart@nginx.com     nxt_str_t              json_string;
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);
37*62Sigor@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,
5460Svbart@nginx.com     nxt_http_field_t *field, uintptr_t data, nxt_log_t *log);
5527Svbart@nginx.com 
5627Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
57*62Sigor@sysoev.ru     nxt_conn_t *c, nxt_controller_request_t *r);
58*62Sigor@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,
6145Svbart@nginx.com     nxt_mem_pool_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 {
8644Svbart@nginx.com     nxt_mem_pool_t          *mp;
8744Svbart@nginx.com     nxt_conf_json_value_t   *conf;
8827Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
8927Svbart@nginx.com 
9044Svbart@nginx.com     static const nxt_str_t json
9144Svbart@nginx.com         = nxt_string("{ \"sockets\": {}, \"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 
10544Svbart@nginx.com     mp = nxt_mem_pool_create(256);
10644Svbart@nginx.com 
10744Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
10844Svbart@nginx.com         return NXT_ERROR;
10944Svbart@nginx.com     }
11044Svbart@nginx.com 
11144Svbart@nginx.com     conf = nxt_conf_json_parse(json.start, json.length, mp);
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 
14820Sigor@sysoev.ru     ls = nxt_mem_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;
16020Sigor@sysoev.ru 
16120Sigor@sysoev.ru     nxt_sockaddr_text(ls->sockaddr);
16220Sigor@sysoev.ru 
16320Sigor@sysoev.ru     ls->socket = -1;
16420Sigor@sysoev.ru     ls->backlog = NXT_LISTEN_BACKLOG;
16520Sigor@sysoev.ru     ls->read_after_accept = 1;
16620Sigor@sysoev.ru     ls->flags = NXT_NONBLOCK;
16720Sigor@sysoev.ru 
16820Sigor@sysoev.ru #if 0
16920Sigor@sysoev.ru     /* STUB */
17020Sigor@sysoev.ru     wq = nxt_mem_zalloc(cf->mem_pool, sizeof(nxt_work_queue_t));
17120Sigor@sysoev.ru     if (wq == NULL) {
17220Sigor@sysoev.ru         return NXT_ERROR;
17320Sigor@sysoev.ru     }
17420Sigor@sysoev.ru     nxt_work_queue_name(wq, "listen");
17520Sigor@sysoev.ru     /**/
17620Sigor@sysoev.ru 
17720Sigor@sysoev.ru     ls->work_queue = wq;
17820Sigor@sysoev.ru #endif
17920Sigor@sysoev.ru     ls->handler = nxt_controller_conn_init;
18020Sigor@sysoev.ru 
18120Sigor@sysoev.ru     /*
18220Sigor@sysoev.ru      * Connection memory pool chunk size is tunned to
18320Sigor@sysoev.ru      * allocate the most data in one mem_pool chunk.
18420Sigor@sysoev.ru      */
18520Sigor@sysoev.ru     ls->mem_pool_size = nxt_listen_socket_pool_min_size(ls)
18620Sigor@sysoev.ru                         + sizeof(nxt_event_conn_proxy_t)
187*62Sigor@sysoev.ru                         + sizeof(nxt_conn_t)
18820Sigor@sysoev.ru                         + 4 * sizeof(nxt_buf_t);
18920Sigor@sysoev.ru 
19020Sigor@sysoev.ru     if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) {
19120Sigor@sysoev.ru         return NXT_ERROR;
19220Sigor@sysoev.ru     }
19320Sigor@sysoev.ru 
19420Sigor@sysoev.ru     rt->controller_socket = ls;
19520Sigor@sysoev.ru 
19620Sigor@sysoev.ru     return NXT_OK;
19720Sigor@sysoev.ru }
19820Sigor@sysoev.ru 
19920Sigor@sysoev.ru 
20020Sigor@sysoev.ru static void
20120Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data)
20220Sigor@sysoev.ru {
20327Svbart@nginx.com     nxt_buf_t                 *b;
204*62Sigor@sysoev.ru     nxt_conn_t                *c;
20527Svbart@nginx.com     nxt_event_engine_t        *engine;
20627Svbart@nginx.com     nxt_controller_request_t  *r;
20720Sigor@sysoev.ru 
20820Sigor@sysoev.ru     c = obj;
20920Sigor@sysoev.ru 
21020Sigor@sysoev.ru     nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
21120Sigor@sysoev.ru 
21227Svbart@nginx.com     r = nxt_mem_zalloc(c->mem_pool, sizeof(nxt_controller_request_t));
21327Svbart@nginx.com     if (nxt_slow_path(r == NULL)) {
21427Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
21527Svbart@nginx.com         return;
21627Svbart@nginx.com     }
21727Svbart@nginx.com 
21860Svbart@nginx.com     if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
21960Svbart@nginx.com                       != NXT_OK))
22060Svbart@nginx.com     {
22160Svbart@nginx.com         nxt_controller_conn_free(task, c, NULL);
22260Svbart@nginx.com         return;
22360Svbart@nginx.com     }
22427Svbart@nginx.com 
22520Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
22620Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
22720Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
22820Sigor@sysoev.ru         return;
22920Sigor@sysoev.ru     }
23020Sigor@sysoev.ru 
23120Sigor@sysoev.ru     c->read = b;
23227Svbart@nginx.com     c->socket.data = r;
23320Sigor@sysoev.ru     c->socket.read_ready = 1;
23420Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
23520Sigor@sysoev.ru 
23620Sigor@sysoev.ru     engine = task->thread->engine;
23720Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
23827Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
23920Sigor@sysoev.ru 
240*62Sigor@sysoev.ru     nxt_conn_read(engine, c);
24120Sigor@sysoev.ru }
24220Sigor@sysoev.ru 
24320Sigor@sysoev.ru 
24420Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
24520Sigor@sysoev.ru     nxt_aligned(64) =
24620Sigor@sysoev.ru {
24756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_read,
24856Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
24956Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
25020Sigor@sysoev.ru 
25156Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
25256Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
25356Sigor@sysoev.ru     .timer_data = 60 * 1000,
25420Sigor@sysoev.ru };
25520Sigor@sysoev.ru 
25620Sigor@sysoev.ru 
25720Sigor@sysoev.ru static void
25820Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
25920Sigor@sysoev.ru {
26027Svbart@nginx.com     size_t                    preread;
26127Svbart@nginx.com     nxt_buf_t                 *b;
26227Svbart@nginx.com     nxt_int_t                 rc;
263*62Sigor@sysoev.ru     nxt_conn_t                *c;
26427Svbart@nginx.com     nxt_controller_request_t  *r;
26520Sigor@sysoev.ru 
26620Sigor@sysoev.ru     c = obj;
26727Svbart@nginx.com     r = data;
26820Sigor@sysoev.ru 
26920Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
27020Sigor@sysoev.ru 
27127Svbart@nginx.com     nxt_queue_remove(&c->link);
27227Svbart@nginx.com     nxt_queue_self(&c->link);
27327Svbart@nginx.com 
27427Svbart@nginx.com     b = c->read;
27527Svbart@nginx.com 
27627Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
27727Svbart@nginx.com 
27827Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
27927Svbart@nginx.com 
28027Svbart@nginx.com         if (rc == NXT_AGAIN) {
28127Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
28227Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
28327Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
28427Svbart@nginx.com                 return;
28527Svbart@nginx.com             }
28627Svbart@nginx.com 
287*62Sigor@sysoev.ru             nxt_conn_read(task->thread->engine, c);
28827Svbart@nginx.com             return;
28927Svbart@nginx.com         }
29027Svbart@nginx.com 
29127Svbart@nginx.com         /* rc == NXT_ERROR */
29227Svbart@nginx.com 
29327Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
29427Svbart@nginx.com 
29527Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
29627Svbart@nginx.com         return;
29727Svbart@nginx.com     }
29827Svbart@nginx.com 
29960Svbart@nginx.com     rc = nxt_http_fields_process(r->parser.fields, nxt_controller_fields_hash,
30060Svbart@nginx.com                                  r, task->log);
30160Svbart@nginx.com 
30260Svbart@nginx.com     if (nxt_slow_path(rc != NXT_OK)) {
30360Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
30460Svbart@nginx.com         return;
30560Svbart@nginx.com     }
30660Svbart@nginx.com 
30727Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
30827Svbart@nginx.com 
30927Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
31027Svbart@nginx.com                     "body length: %O, preread: %uz",
31127Svbart@nginx.com                     r->length, preread);
31227Svbart@nginx.com 
31327Svbart@nginx.com     if (preread >= r->length) {
31427Svbart@nginx.com         nxt_controller_process_request(task, c, r);
31527Svbart@nginx.com         return;
31627Svbart@nginx.com     }
31727Svbart@nginx.com 
31827Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
31927Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
32027Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
32127Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
32227Svbart@nginx.com             return;
32327Svbart@nginx.com         }
32427Svbart@nginx.com 
32527Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
32627Svbart@nginx.com 
32727Svbart@nginx.com         c->read = b;
32827Svbart@nginx.com     }
32927Svbart@nginx.com 
33027Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
33127Svbart@nginx.com 
332*62Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
33320Sigor@sysoev.ru }
33420Sigor@sysoev.ru 
33520Sigor@sysoev.ru 
33620Sigor@sysoev.ru static nxt_msec_t
337*62Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data)
33820Sigor@sysoev.ru {
33920Sigor@sysoev.ru     return (nxt_msec_t) data;
34020Sigor@sysoev.ru }
34120Sigor@sysoev.ru 
34220Sigor@sysoev.ru 
34320Sigor@sysoev.ru static void
34420Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
34520Sigor@sysoev.ru {
346*62Sigor@sysoev.ru     nxt_conn_t  *c;
34720Sigor@sysoev.ru 
34820Sigor@sysoev.ru     c = obj;
34920Sigor@sysoev.ru 
35020Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
35120Sigor@sysoev.ru 
35227Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
35320Sigor@sysoev.ru }
35420Sigor@sysoev.ru 
35520Sigor@sysoev.ru 
35620Sigor@sysoev.ru static void
35720Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
35820Sigor@sysoev.ru {
359*62Sigor@sysoev.ru     nxt_timer_t  *timer;
360*62Sigor@sysoev.ru     nxt_conn_t   *c;
36120Sigor@sysoev.ru 
362*62Sigor@sysoev.ru     timer = obj;
36320Sigor@sysoev.ru 
364*62Sigor@sysoev.ru     c = nxt_read_timer_conn(timer);
36520Sigor@sysoev.ru     c->socket.timedout = 1;
36620Sigor@sysoev.ru     c->socket.closed = 1;
36720Sigor@sysoev.ru 
36820Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
36920Sigor@sysoev.ru 
37027Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
37127Svbart@nginx.com }
37227Svbart@nginx.com 
37327Svbart@nginx.com 
37427Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
37527Svbart@nginx.com     nxt_aligned(64) =
37627Svbart@nginx.com {
37756Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_body_read,
37856Sigor@sysoev.ru     .close_handler = nxt_controller_conn_close,
37956Sigor@sysoev.ru     .error_handler = nxt_controller_conn_read_error,
38027Svbart@nginx.com 
38156Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_read_timeout,
38256Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
38356Sigor@sysoev.ru     .timer_data = 60 * 1000,
38456Sigor@sysoev.ru     .timer_autoreset = 1,
38527Svbart@nginx.com };
38627Svbart@nginx.com 
38727Svbart@nginx.com 
38827Svbart@nginx.com static void
38927Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
39027Svbart@nginx.com {
391*62Sigor@sysoev.ru     size_t      rest;
392*62Sigor@sysoev.ru     nxt_buf_t   *b;
393*62Sigor@sysoev.ru     nxt_conn_t  *c;
39427Svbart@nginx.com 
39527Svbart@nginx.com     c = obj;
39627Svbart@nginx.com 
39727Svbart@nginx.com     nxt_debug(task, "controller conn body read");
39827Svbart@nginx.com 
39927Svbart@nginx.com     b = c->read;
40027Svbart@nginx.com 
40127Svbart@nginx.com     rest = nxt_buf_mem_free_size(&b->mem);
40227Svbart@nginx.com 
40327Svbart@nginx.com     if (rest == 0) {
40427Svbart@nginx.com         nxt_debug(task, "controller conn body read complete");
40527Svbart@nginx.com 
40627Svbart@nginx.com         nxt_controller_process_request(task, c, data);
40727Svbart@nginx.com         return;
40827Svbart@nginx.com     }
40927Svbart@nginx.com 
41027Svbart@nginx.com     nxt_debug(task, "controller conn body read again, rest: %uz", rest);
41127Svbart@nginx.com 
412*62Sigor@sysoev.ru     nxt_conn_read(task->thread->engine, c);
41327Svbart@nginx.com }
41427Svbart@nginx.com 
41527Svbart@nginx.com 
41627Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
41727Svbart@nginx.com     nxt_aligned(64) =
41827Svbart@nginx.com {
41956Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_write,
42056Sigor@sysoev.ru     .error_handler = nxt_controller_conn_write_error,
42127Svbart@nginx.com 
42256Sigor@sysoev.ru     .timer_handler = nxt_controller_conn_write_timeout,
42356Sigor@sysoev.ru     .timer_value = nxt_controller_conn_timeout_value,
42456Sigor@sysoev.ru     .timer_data = 60 * 1000,
42556Sigor@sysoev.ru     .timer_autoreset = 1,
42627Svbart@nginx.com };
42727Svbart@nginx.com 
42827Svbart@nginx.com 
42927Svbart@nginx.com static void
43027Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
43127Svbart@nginx.com {
432*62Sigor@sysoev.ru     nxt_buf_t   *b;
433*62Sigor@sysoev.ru     nxt_conn_t  *c;
43427Svbart@nginx.com 
43527Svbart@nginx.com     c = obj;
43627Svbart@nginx.com 
43727Svbart@nginx.com     nxt_debug(task, "controller conn write");
43827Svbart@nginx.com 
43927Svbart@nginx.com     b = c->write;
44027Svbart@nginx.com 
44127Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
442*62Sigor@sysoev.ru         nxt_conn_write(task->thread->engine, c);
44327Svbart@nginx.com         return;
44427Svbart@nginx.com     }
44527Svbart@nginx.com 
44627Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
44727Svbart@nginx.com 
44827Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
44927Svbart@nginx.com }
45027Svbart@nginx.com 
45127Svbart@nginx.com 
45227Svbart@nginx.com static void
45327Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
45427Svbart@nginx.com {
455*62Sigor@sysoev.ru     nxt_conn_t  *c;
45627Svbart@nginx.com 
45727Svbart@nginx.com     c = obj;
45827Svbart@nginx.com 
45927Svbart@nginx.com     nxt_debug(task, "controller conn write error");
46027Svbart@nginx.com 
46127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
46227Svbart@nginx.com }
46327Svbart@nginx.com 
46427Svbart@nginx.com 
46527Svbart@nginx.com static void
46627Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
46727Svbart@nginx.com {
468*62Sigor@sysoev.ru     nxt_conn_t   *c;
469*62Sigor@sysoev.ru     nxt_timer_t  *timer;
47027Svbart@nginx.com 
471*62Sigor@sysoev.ru     timer = obj;
47227Svbart@nginx.com 
473*62Sigor@sysoev.ru     c = nxt_write_timer_conn(timer);
47427Svbart@nginx.com     c->socket.timedout = 1;
47527Svbart@nginx.com     c->socket.closed = 1;
47627Svbart@nginx.com 
47727Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
47827Svbart@nginx.com 
47927Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
48020Sigor@sysoev.ru }
48120Sigor@sysoev.ru 
48220Sigor@sysoev.ru 
48320Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
48420Sigor@sysoev.ru     nxt_aligned(64) =
48520Sigor@sysoev.ru {
48656Sigor@sysoev.ru     .ready_handler = nxt_controller_conn_free,
48720Sigor@sysoev.ru };
48820Sigor@sysoev.ru 
48920Sigor@sysoev.ru 
49020Sigor@sysoev.ru static void
49120Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
49220Sigor@sysoev.ru {
493*62Sigor@sysoev.ru     nxt_conn_t  *c;
49420Sigor@sysoev.ru 
49520Sigor@sysoev.ru     c = obj;
49620Sigor@sysoev.ru 
49720Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
49820Sigor@sysoev.ru 
49927Svbart@nginx.com     nxt_queue_remove(&c->link);
50027Svbart@nginx.com 
50120Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
50220Sigor@sysoev.ru 
503*62Sigor@sysoev.ru     nxt_conn_close(task->thread->engine, c);
50420Sigor@sysoev.ru }
50520Sigor@sysoev.ru 
50620Sigor@sysoev.ru 
50720Sigor@sysoev.ru static void
50820Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
50920Sigor@sysoev.ru {
510*62Sigor@sysoev.ru     nxt_conn_t  *c;
51120Sigor@sysoev.ru 
51220Sigor@sysoev.ru     c = obj;
51320Sigor@sysoev.ru 
51420Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
51520Sigor@sysoev.ru 
51620Sigor@sysoev.ru     nxt_mem_pool_destroy(c->mem_pool);
51720Sigor@sysoev.ru 
51820Sigor@sysoev.ru     //nxt_free(c);
51920Sigor@sysoev.ru }
52027Svbart@nginx.com 
52127Svbart@nginx.com 
52227Svbart@nginx.com static nxt_int_t
52360Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
52460Svbart@nginx.com     uintptr_t data, nxt_log_t *log)
52527Svbart@nginx.com {
52627Svbart@nginx.com     off_t                     length;
52727Svbart@nginx.com     nxt_controller_request_t  *r;
52827Svbart@nginx.com 
52927Svbart@nginx.com     r = ctx;
53027Svbart@nginx.com 
53160Svbart@nginx.com     length = nxt_off_t_parse(field->value.start, field->value.length);
53227Svbart@nginx.com 
53327Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
53460Svbart@nginx.com         nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
53527Svbart@nginx.com 
53627Svbart@nginx.com         r->length = length;
53727Svbart@nginx.com         return NXT_OK;
53827Svbart@nginx.com     }
53927Svbart@nginx.com 
54060Svbart@nginx.com     nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
54127Svbart@nginx.com 
54227Svbart@nginx.com     return NXT_ERROR;
54327Svbart@nginx.com }
54427Svbart@nginx.com 
54527Svbart@nginx.com 
54627Svbart@nginx.com static void
547*62Sigor@sysoev.ru nxt_controller_process_request(nxt_task_t *task, nxt_conn_t *c,
54844Svbart@nginx.com     nxt_controller_request_t *req)
54927Svbart@nginx.com {
55051Svbart@nginx.com     nxt_int_t                  rc;
55146Svbart@nginx.com     nxt_str_t                  path;
55251Svbart@nginx.com     nxt_uint_t                 status;
55351Svbart@nginx.com     nxt_buf_mem_t              *mbuf;
55451Svbart@nginx.com     nxt_mem_pool_t             *mp;
55551Svbart@nginx.com     nxt_conf_json_op_t         *ops;
55646Svbart@nginx.com     nxt_conf_json_value_t      *value;
55744Svbart@nginx.com     nxt_controller_response_t  resp;
55844Svbart@nginx.com 
55951Svbart@nginx.com     static const nxt_str_t empty_obj = nxt_string("{}");
56051Svbart@nginx.com 
56151Svbart@nginx.com     path.start = req->parser.target_start;
56251Svbart@nginx.com 
56351Svbart@nginx.com     if (req->parser.args_start != NULL) {
56451Svbart@nginx.com         path.length = req->parser.args_start - path.start;
56551Svbart@nginx.com 
56651Svbart@nginx.com     } else {
56751Svbart@nginx.com         path.length = req->parser.target_end - path.start;
56851Svbart@nginx.com     }
56951Svbart@nginx.com 
57051Svbart@nginx.com     if (path.length > 1 && path.start[path.length - 1] == '/') {
57151Svbart@nginx.com         path.length--;
57251Svbart@nginx.com     }
57351Svbart@nginx.com 
57444Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
57544Svbart@nginx.com 
57644Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
57746Svbart@nginx.com 
57851Svbart@nginx.com         value = nxt_conf_json_get_value(nxt_controller_conf.root, &path);
57951Svbart@nginx.com 
58051Svbart@nginx.com         if (value == NULL) {
58151Svbart@nginx.com             status = 404;
58251Svbart@nginx.com             goto done;
58351Svbart@nginx.com         }
58451Svbart@nginx.com 
58551Svbart@nginx.com         resp.json_value = value;
58646Svbart@nginx.com 
58751Svbart@nginx.com         status = 200;
58851Svbart@nginx.com         goto done;
58951Svbart@nginx.com     }
59051Svbart@nginx.com 
59151Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
59246Svbart@nginx.com 
59351Svbart@nginx.com         mp = nxt_mem_pool_create(512);
59451Svbart@nginx.com 
59551Svbart@nginx.com         if (nxt_slow_path(mp == NULL)) {
59651Svbart@nginx.com             status = 500;
59751Svbart@nginx.com             goto done;
59846Svbart@nginx.com         }
59946Svbart@nginx.com 
60051Svbart@nginx.com         mbuf = &c->read->mem;
60151Svbart@nginx.com 
60251Svbart@nginx.com         value = nxt_conf_json_parse(mbuf->pos, mbuf->free - mbuf->pos, mp);
60351Svbart@nginx.com 
60451Svbart@nginx.com         if (value == NULL) {
60551Svbart@nginx.com             nxt_mem_pool_destroy(mp);
60651Svbart@nginx.com             status = 400;
60751Svbart@nginx.com             goto done;
60851Svbart@nginx.com         }
60951Svbart@nginx.com 
61051Svbart@nginx.com         if (path.length != 1) {
61151Svbart@nginx.com             rc = nxt_conf_json_op_compile(nxt_controller_conf.root, value,
61251Svbart@nginx.com                                           &ops, &path, c->mem_pool);
61346Svbart@nginx.com 
61451Svbart@nginx.com             if (rc != NXT_OK) {
61551Svbart@nginx.com                 if (rc == NXT_DECLINED) {
61651Svbart@nginx.com                     status = 404;
61751Svbart@nginx.com                     goto done;
61851Svbart@nginx.com                 }
61946Svbart@nginx.com 
62051Svbart@nginx.com                 status = 500;
62151Svbart@nginx.com                 goto done;
62251Svbart@nginx.com             }
62351Svbart@nginx.com 
62451Svbart@nginx.com             value = nxt_conf_json_clone_value(nxt_controller_conf.root,
62551Svbart@nginx.com                                               ops, mp);
62651Svbart@nginx.com 
62751Svbart@nginx.com             if (nxt_slow_path(value == NULL)) {
62851Svbart@nginx.com                 nxt_mem_pool_destroy(mp);
62951Svbart@nginx.com                 status = 500;
63051Svbart@nginx.com                 goto done;
63151Svbart@nginx.com             }
63246Svbart@nginx.com         }
63344Svbart@nginx.com 
63451Svbart@nginx.com         nxt_mem_pool_destroy(nxt_controller_conf.pool);
63551Svbart@nginx.com 
63651Svbart@nginx.com         nxt_controller_conf.root = value;
63751Svbart@nginx.com         nxt_controller_conf.pool = mp;
63844Svbart@nginx.com 
63951Svbart@nginx.com         nxt_str_set(&resp.json_string, "{ \"success\": \"Updated.\" }");
64051Svbart@nginx.com 
64151Svbart@nginx.com         status = 200;
64251Svbart@nginx.com         goto done;
64351Svbart@nginx.com     }
64427Svbart@nginx.com 
64551Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "DELETE", 6)) {
64651Svbart@nginx.com 
64751Svbart@nginx.com         if (path.length == 1) {
64851Svbart@nginx.com             mp = nxt_mem_pool_create(128);
64944Svbart@nginx.com 
65051Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
65151Svbart@nginx.com                 status = 500;
65251Svbart@nginx.com                 goto done;
65351Svbart@nginx.com             }
65451Svbart@nginx.com 
65551Svbart@nginx.com             value = nxt_conf_json_parse(empty_obj.start, empty_obj.length, mp);
65627Svbart@nginx.com 
65744Svbart@nginx.com         } else {
65851Svbart@nginx.com             rc = nxt_conf_json_op_compile(nxt_controller_conf.root, NULL, &ops,
65951Svbart@nginx.com                                           &path, c->mem_pool);
66051Svbart@nginx.com 
66151Svbart@nginx.com             if (rc != NXT_OK) {
66251Svbart@nginx.com                 if (rc == NXT_DECLINED) {
66351Svbart@nginx.com                     status = 404;
66451Svbart@nginx.com                     goto done;
66551Svbart@nginx.com                 }
66651Svbart@nginx.com 
66751Svbart@nginx.com                 status = 500;
66851Svbart@nginx.com                 goto done;
66951Svbart@nginx.com             }
67051Svbart@nginx.com 
67151Svbart@nginx.com             mp = nxt_mem_pool_create(512);
67251Svbart@nginx.com 
67351Svbart@nginx.com             if (nxt_slow_path(mp == NULL)) {
67451Svbart@nginx.com                 status = 500;
67551Svbart@nginx.com                 goto done;
67651Svbart@nginx.com             }
67751Svbart@nginx.com 
67851Svbart@nginx.com             value = nxt_conf_json_clone_value(nxt_controller_conf.root,
67951Svbart@nginx.com                                               ops, mp);
68051Svbart@nginx.com         }
68151Svbart@nginx.com 
68251Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
68351Svbart@nginx.com             nxt_mem_pool_destroy(mp);
68451Svbart@nginx.com             status = 500;
68551Svbart@nginx.com             goto done;
68644Svbart@nginx.com         }
68744Svbart@nginx.com 
68851Svbart@nginx.com         nxt_mem_pool_destroy(nxt_controller_conf.pool);
68951Svbart@nginx.com 
69051Svbart@nginx.com         nxt_controller_conf.root = value;
69151Svbart@nginx.com         nxt_controller_conf.pool = mp;
69251Svbart@nginx.com 
69351Svbart@nginx.com         nxt_str_set(&resp.json_string, "{ \"success\": \"Deleted.\" }");
69451Svbart@nginx.com 
69551Svbart@nginx.com         status = 200;
69651Svbart@nginx.com         goto done;
69751Svbart@nginx.com     }
69851Svbart@nginx.com 
69951Svbart@nginx.com     status = 405;
70051Svbart@nginx.com 
70151Svbart@nginx.com done:
70251Svbart@nginx.com 
70351Svbart@nginx.com     switch (status) {
70451Svbart@nginx.com 
70551Svbart@nginx.com     case 200:
70651Svbart@nginx.com         nxt_str_set(&resp.status_line, "200 OK");
70751Svbart@nginx.com         break;
70851Svbart@nginx.com 
70951Svbart@nginx.com     case 400:
71051Svbart@nginx.com         nxt_str_set(&resp.status_line, "400 Bad Request");
71151Svbart@nginx.com         nxt_str_set(&resp.json_string,
71251Svbart@nginx.com                     "{ \"error\": \"Invalid JSON.\" }");
71351Svbart@nginx.com         break;
71451Svbart@nginx.com 
71551Svbart@nginx.com     case 404:
71651Svbart@nginx.com         nxt_str_set(&resp.status_line, "404 Not Found");
71751Svbart@nginx.com         nxt_str_set(&resp.json_string,
71851Svbart@nginx.com                     "{ \"error\": \"Value doesn't exist.\" }");
71951Svbart@nginx.com         break;
72051Svbart@nginx.com 
72151Svbart@nginx.com     case 405:
72244Svbart@nginx.com         nxt_str_set(&resp.status_line, "405 Method Not Allowed");
72351Svbart@nginx.com         nxt_str_set(&resp.json_string, "{ \"error\": \"Invalid method.\" }");
72451Svbart@nginx.com         break;
72551Svbart@nginx.com 
72651Svbart@nginx.com     case 500:
72751Svbart@nginx.com         nxt_str_set(&resp.status_line, "500 Internal Server Error");
72851Svbart@nginx.com         nxt_str_set(&resp.json_string,
72951Svbart@nginx.com                     "{ \"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
740*62Sigor@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 
768*62Sigor@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 *
77545Svbart@nginx.com nxt_controller_response_body(nxt_controller_response_t *resp,
77645Svbart@nginx.com     nxt_mem_pool_t *pool)
77745Svbart@nginx.com {
77845Svbart@nginx.com     size_t                  size;
77945Svbart@nginx.com     nxt_buf_t               *b;
78045Svbart@nginx.com     nxt_conf_json_value_t   *value;
78145Svbart@nginx.com     nxt_conf_json_pretty_t  pretty;
78245Svbart@nginx.com 
78345Svbart@nginx.com     if (resp->json_value) {
78445Svbart@nginx.com         value = resp->json_value;
78545Svbart@nginx.com 
78645Svbart@nginx.com     } else {
78745Svbart@nginx.com         value = nxt_conf_json_parse(resp->json_string.start,
78845Svbart@nginx.com                                     resp->json_string.length, pool);
78945Svbart@nginx.com 
79045Svbart@nginx.com         if (nxt_slow_path(value == NULL)) {
79145Svbart@nginx.com             return NULL;
79245Svbart@nginx.com         }
79345Svbart@nginx.com     }
79445Svbart@nginx.com 
79545Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
79645Svbart@nginx.com 
79745Svbart@nginx.com     size = nxt_conf_json_print_value(NULL, value, &pretty) + 2;
79845Svbart@nginx.com 
79945Svbart@nginx.com     b = nxt_buf_mem_alloc(pool, size, 0);
80045Svbart@nginx.com     if (nxt_slow_path(b == NULL)) {
80145Svbart@nginx.com         return NULL;
80245Svbart@nginx.com     }
80345Svbart@nginx.com 
80445Svbart@nginx.com     nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t));
80545Svbart@nginx.com 
80645Svbart@nginx.com     b->mem.free = (u_char *) nxt_conf_json_print_value(b->mem.free, value,
80745Svbart@nginx.com                                                        &pretty);
80845Svbart@nginx.com 
80945Svbart@nginx.com     *b->mem.free++ = '\r';
81045Svbart@nginx.com     *b->mem.free++ = '\n';
81145Svbart@nginx.com 
81245Svbart@nginx.com     return b;
81345Svbart@nginx.com }
814