xref: /unit/src/nxt_controller.c (revision 44)
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 {
15*44Svbart@nginx.com     nxt_conf_json_value_t  *root;
16*44Svbart@nginx.com     nxt_mem_pool_t         *pool;
17*44Svbart@nginx.com } nxt_controller_conf_t;
18*44Svbart@nginx.com 
19*44Svbart@nginx.com 
20*44Svbart@nginx.com typedef struct {
2127Svbart@nginx.com     nxt_http_request_parse_t  parser;
2227Svbart@nginx.com     size_t                    length;
2333Svbart@nginx.com 
24*44Svbart@nginx.com     nxt_controller_conf_t     conf;
2527Svbart@nginx.com } nxt_controller_request_t;
2627Svbart@nginx.com 
2727Svbart@nginx.com 
28*44Svbart@nginx.com typedef struct {
29*44Svbart@nginx.com     nxt_str_t              status_line;
30*44Svbart@nginx.com     nxt_conf_json_value_t  *json_value;
31*44Svbart@nginx.com     nxt_str_t              json_string;
32*44Svbart@nginx.com } nxt_controller_response_t;
33*44Svbart@nginx.com 
34*44Svbart@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);
3720Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_event_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,
5427Svbart@nginx.com     nxt_str_t *name, nxt_str_t *value, uintptr_t data);
5527Svbart@nginx.com 
5627Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task,
5727Svbart@nginx.com     nxt_event_conn_t *c, nxt_controller_request_t *r);
5829Svbart@nginx.com static nxt_int_t nxt_controller_request_body_parse(nxt_task_t *task,
5933Svbart@nginx.com     nxt_event_conn_t *c, nxt_controller_request_t *r);
60*44Svbart@nginx.com static nxt_int_t nxt_controller_response(nxt_task_t *task, nxt_event_conn_t *c,
61*44Svbart@nginx.com     nxt_controller_response_t *resp);
6227Svbart@nginx.com 
6327Svbart@nginx.com 
6427Svbart@nginx.com static nxt_http_fields_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 
71*44Svbart@nginx.com static nxt_http_fields_hash_t  *nxt_controller_request_fields_hash;
7227Svbart@nginx.com 
73*44Svbart@nginx.com 
74*44Svbart@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 {
86*44Svbart@nginx.com     nxt_mem_pool_t          *mp;
87*44Svbart@nginx.com     nxt_conf_json_value_t   *conf;
8827Svbart@nginx.com     nxt_http_fields_hash_t  *hash;
8927Svbart@nginx.com 
90*44Svbart@nginx.com     static const nxt_str_t json
91*44Svbart@nginx.com         = nxt_string("{ \"sockets\": {}, \"applications\": {} }");
92*44Svbart@nginx.com 
9327Svbart@nginx.com     hash = nxt_http_fields_hash(nxt_controller_request_fields, rt->mem_pool);
9427Svbart@nginx.com 
9527Svbart@nginx.com     if (nxt_slow_path(hash == NULL)) {
9627Svbart@nginx.com         return NXT_ERROR;
9727Svbart@nginx.com     }
9827Svbart@nginx.com 
9927Svbart@nginx.com     nxt_controller_request_fields_hash = hash;
10027Svbart@nginx.com 
10120Sigor@sysoev.ru     if (nxt_event_conn_listen(task, rt->controller_socket) != NXT_OK) {
10220Sigor@sysoev.ru         return NXT_ERROR;
10320Sigor@sysoev.ru     }
10420Sigor@sysoev.ru 
105*44Svbart@nginx.com     mp = nxt_mem_pool_create(256);
106*44Svbart@nginx.com 
107*44Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
108*44Svbart@nginx.com         return NXT_ERROR;
109*44Svbart@nginx.com     }
110*44Svbart@nginx.com 
111*44Svbart@nginx.com     conf = nxt_conf_json_parse(json.start, json.length, mp);
112*44Svbart@nginx.com 
113*44Svbart@nginx.com     if (conf == NULL) {
114*44Svbart@nginx.com         return NXT_ERROR;
115*44Svbart@nginx.com     }
116*44Svbart@nginx.com 
117*44Svbart@nginx.com     nxt_controller_conf.root = conf;
118*44Svbart@nginx.com     nxt_controller_conf.pool = mp;
119*44Svbart@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)
18720Sigor@sysoev.ru                         + sizeof(nxt_event_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;
20427Svbart@nginx.com     nxt_event_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 
21827Svbart@nginx.com     r->parser.hash = nxt_controller_request_fields_hash;
21927Svbart@nginx.com     r->parser.ctx = r;
22027Svbart@nginx.com 
22120Sigor@sysoev.ru     b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
22220Sigor@sysoev.ru     if (nxt_slow_path(b == NULL)) {
22320Sigor@sysoev.ru         nxt_controller_conn_free(task, c, NULL);
22420Sigor@sysoev.ru         return;
22520Sigor@sysoev.ru     }
22620Sigor@sysoev.ru 
22720Sigor@sysoev.ru     c->read = b;
22827Svbart@nginx.com     c->socket.data = r;
22920Sigor@sysoev.ru     c->socket.read_ready = 1;
23020Sigor@sysoev.ru     c->read_state = &nxt_controller_conn_read_state;
23120Sigor@sysoev.ru 
23220Sigor@sysoev.ru     engine = task->thread->engine;
23320Sigor@sysoev.ru     c->read_work_queue = &engine->read_work_queue;
23427Svbart@nginx.com     c->write_work_queue = &engine->write_work_queue;
23520Sigor@sysoev.ru 
23620Sigor@sysoev.ru     nxt_event_conn_read(engine, c);
23720Sigor@sysoev.ru }
23820Sigor@sysoev.ru 
23920Sigor@sysoev.ru 
24020Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_read_state
24120Sigor@sysoev.ru     nxt_aligned(64) =
24220Sigor@sysoev.ru {
24320Sigor@sysoev.ru     NXT_EVENT_NO_BUF_PROCESS,
24420Sigor@sysoev.ru     NXT_EVENT_TIMER_NO_AUTORESET,
24520Sigor@sysoev.ru 
24620Sigor@sysoev.ru     nxt_controller_conn_read,
24720Sigor@sysoev.ru     nxt_controller_conn_close,
24820Sigor@sysoev.ru     nxt_controller_conn_read_error,
24920Sigor@sysoev.ru 
25020Sigor@sysoev.ru     nxt_controller_conn_read_timeout,
25120Sigor@sysoev.ru     nxt_controller_conn_timeout_value,
25220Sigor@sysoev.ru     60 * 1000,
25320Sigor@sysoev.ru };
25420Sigor@sysoev.ru 
25520Sigor@sysoev.ru 
25620Sigor@sysoev.ru static void
25720Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data)
25820Sigor@sysoev.ru {
25927Svbart@nginx.com     size_t                    preread;
26027Svbart@nginx.com     nxt_buf_t                 *b;
26127Svbart@nginx.com     nxt_int_t                 rc;
26227Svbart@nginx.com     nxt_event_conn_t          *c;
26327Svbart@nginx.com     nxt_controller_request_t  *r;
26420Sigor@sysoev.ru 
26520Sigor@sysoev.ru     c = obj;
26627Svbart@nginx.com     r = data;
26720Sigor@sysoev.ru 
26820Sigor@sysoev.ru     nxt_debug(task, "controller conn read");
26920Sigor@sysoev.ru 
27027Svbart@nginx.com     nxt_queue_remove(&c->link);
27127Svbart@nginx.com     nxt_queue_self(&c->link);
27227Svbart@nginx.com 
27327Svbart@nginx.com     b = c->read;
27427Svbart@nginx.com 
27527Svbart@nginx.com     rc = nxt_http_parse_request(&r->parser, &b->mem);
27627Svbart@nginx.com 
27727Svbart@nginx.com     if (nxt_slow_path(rc != NXT_DONE)) {
27827Svbart@nginx.com 
27927Svbart@nginx.com         if (rc == NXT_AGAIN) {
28027Svbart@nginx.com             if (nxt_buf_mem_free_size(&b->mem) == 0) {
28127Svbart@nginx.com                 nxt_log(task, NXT_LOG_ERR, "too long request headers");
28227Svbart@nginx.com                 nxt_controller_conn_close(task, c, r);
28327Svbart@nginx.com                 return;
28427Svbart@nginx.com             }
28527Svbart@nginx.com 
28627Svbart@nginx.com             nxt_event_conn_read(task->thread->engine, c);
28727Svbart@nginx.com             return;
28827Svbart@nginx.com         }
28927Svbart@nginx.com 
29027Svbart@nginx.com         /* rc == NXT_ERROR */
29127Svbart@nginx.com 
29227Svbart@nginx.com         nxt_log(task, NXT_LOG_ERR, "parsing error");
29327Svbart@nginx.com 
29427Svbart@nginx.com         nxt_controller_conn_close(task, c, r);
29527Svbart@nginx.com         return;
29627Svbart@nginx.com     }
29727Svbart@nginx.com 
29827Svbart@nginx.com     preread = nxt_buf_mem_used_size(&b->mem);
29927Svbart@nginx.com 
30027Svbart@nginx.com     nxt_debug(task, "controller request header parsing complete, "
30127Svbart@nginx.com                     "body length: %O, preread: %uz",
30227Svbart@nginx.com                     r->length, preread);
30327Svbart@nginx.com 
30427Svbart@nginx.com     if (preread >= r->length) {
30527Svbart@nginx.com         nxt_controller_process_request(task, c, r);
30627Svbart@nginx.com         return;
30727Svbart@nginx.com     }
30827Svbart@nginx.com 
30927Svbart@nginx.com     if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) {
31027Svbart@nginx.com         b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0);
31127Svbart@nginx.com         if (nxt_slow_path(b == NULL)) {
31227Svbart@nginx.com             nxt_controller_conn_free(task, c, NULL);
31327Svbart@nginx.com             return;
31427Svbart@nginx.com         }
31527Svbart@nginx.com 
31627Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread);
31727Svbart@nginx.com 
31827Svbart@nginx.com         c->read = b;
31927Svbart@nginx.com     }
32027Svbart@nginx.com 
32127Svbart@nginx.com     c->read_state = &nxt_controller_conn_body_read_state;
32227Svbart@nginx.com 
32327Svbart@nginx.com     nxt_event_conn_read(task->thread->engine, c);
32420Sigor@sysoev.ru }
32520Sigor@sysoev.ru 
32620Sigor@sysoev.ru 
32720Sigor@sysoev.ru static nxt_msec_t
32820Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_event_conn_t *c, uintptr_t data)
32920Sigor@sysoev.ru {
33020Sigor@sysoev.ru     return (nxt_msec_t) data;
33120Sigor@sysoev.ru }
33220Sigor@sysoev.ru 
33320Sigor@sysoev.ru 
33420Sigor@sysoev.ru static void
33520Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data)
33620Sigor@sysoev.ru {
33720Sigor@sysoev.ru     nxt_event_conn_t  *c;
33820Sigor@sysoev.ru 
33920Sigor@sysoev.ru     c = obj;
34020Sigor@sysoev.ru 
34120Sigor@sysoev.ru     nxt_debug(task, "controller conn read error");
34220Sigor@sysoev.ru 
34327Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
34420Sigor@sysoev.ru }
34520Sigor@sysoev.ru 
34620Sigor@sysoev.ru 
34720Sigor@sysoev.ru static void
34820Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data)
34920Sigor@sysoev.ru {
35020Sigor@sysoev.ru     nxt_timer_t       *ev;
35120Sigor@sysoev.ru     nxt_event_conn_t  *c;
35220Sigor@sysoev.ru 
35320Sigor@sysoev.ru     ev = obj;
35420Sigor@sysoev.ru 
35520Sigor@sysoev.ru     c = nxt_event_read_timer_conn(ev);
35620Sigor@sysoev.ru     c->socket.timedout = 1;
35720Sigor@sysoev.ru     c->socket.closed = 1;
35820Sigor@sysoev.ru 
35920Sigor@sysoev.ru     nxt_debug(task, "controller conn read timeout");
36020Sigor@sysoev.ru 
36127Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
36227Svbart@nginx.com }
36327Svbart@nginx.com 
36427Svbart@nginx.com 
36527Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_body_read_state
36627Svbart@nginx.com     nxt_aligned(64) =
36727Svbart@nginx.com {
36827Svbart@nginx.com     NXT_EVENT_NO_BUF_PROCESS,
36927Svbart@nginx.com     NXT_EVENT_TIMER_AUTORESET,
37027Svbart@nginx.com 
37127Svbart@nginx.com     nxt_controller_conn_body_read,
37227Svbart@nginx.com     nxt_controller_conn_close,
37327Svbart@nginx.com     nxt_controller_conn_read_error,
37427Svbart@nginx.com 
37527Svbart@nginx.com     nxt_controller_conn_read_timeout,
37627Svbart@nginx.com     nxt_controller_conn_timeout_value,
37727Svbart@nginx.com     60 * 1000,
37827Svbart@nginx.com };
37927Svbart@nginx.com 
38027Svbart@nginx.com 
38127Svbart@nginx.com static void
38227Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data)
38327Svbart@nginx.com {
38427Svbart@nginx.com     size_t            rest;
38527Svbart@nginx.com     nxt_buf_t         *b;
38627Svbart@nginx.com     nxt_event_conn_t  *c;
38727Svbart@nginx.com 
38827Svbart@nginx.com     c = obj;
38927Svbart@nginx.com 
39027Svbart@nginx.com     nxt_debug(task, "controller conn body read");
39127Svbart@nginx.com 
39227Svbart@nginx.com     b = c->read;
39327Svbart@nginx.com 
39427Svbart@nginx.com     rest = nxt_buf_mem_free_size(&b->mem);
39527Svbart@nginx.com 
39627Svbart@nginx.com     if (rest == 0) {
39727Svbart@nginx.com         nxt_debug(task, "controller conn body read complete");
39827Svbart@nginx.com 
39927Svbart@nginx.com         nxt_controller_process_request(task, c, data);
40027Svbart@nginx.com         return;
40127Svbart@nginx.com     }
40227Svbart@nginx.com 
40327Svbart@nginx.com     nxt_debug(task, "controller conn body read again, rest: %uz", rest);
40427Svbart@nginx.com 
40527Svbart@nginx.com     nxt_event_conn_read(task->thread->engine, c);
40627Svbart@nginx.com }
40727Svbart@nginx.com 
40827Svbart@nginx.com 
40927Svbart@nginx.com static const nxt_event_conn_state_t  nxt_controller_conn_write_state
41027Svbart@nginx.com     nxt_aligned(64) =
41127Svbart@nginx.com {
41227Svbart@nginx.com     NXT_EVENT_NO_BUF_PROCESS,
41327Svbart@nginx.com     NXT_EVENT_TIMER_AUTORESET,
41427Svbart@nginx.com 
41527Svbart@nginx.com     nxt_controller_conn_write,
41627Svbart@nginx.com     NULL,
41727Svbart@nginx.com     nxt_controller_conn_write_error,
41827Svbart@nginx.com 
41927Svbart@nginx.com     nxt_controller_conn_write_timeout,
42027Svbart@nginx.com     nxt_controller_conn_timeout_value,
42127Svbart@nginx.com     60 * 1000,
42227Svbart@nginx.com };
42327Svbart@nginx.com 
42427Svbart@nginx.com 
42527Svbart@nginx.com static void
42627Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data)
42727Svbart@nginx.com {
42827Svbart@nginx.com     nxt_buf_t         *b;
42927Svbart@nginx.com     nxt_event_conn_t  *c;
43027Svbart@nginx.com 
43127Svbart@nginx.com     c = obj;
43227Svbart@nginx.com 
43327Svbart@nginx.com     nxt_debug(task, "controller conn write");
43427Svbart@nginx.com 
43527Svbart@nginx.com     b = c->write;
43627Svbart@nginx.com 
43727Svbart@nginx.com     if (b->mem.pos != b->mem.free) {
43827Svbart@nginx.com         nxt_event_conn_write(task->thread->engine, c);
43927Svbart@nginx.com         return;
44027Svbart@nginx.com     }
44127Svbart@nginx.com 
44227Svbart@nginx.com     nxt_debug(task, "controller conn write complete");
44327Svbart@nginx.com 
44427Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
44527Svbart@nginx.com }
44627Svbart@nginx.com 
44727Svbart@nginx.com 
44827Svbart@nginx.com static void
44927Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data)
45027Svbart@nginx.com {
45127Svbart@nginx.com     nxt_event_conn_t  *c;
45227Svbart@nginx.com 
45327Svbart@nginx.com     c = obj;
45427Svbart@nginx.com 
45527Svbart@nginx.com     nxt_debug(task, "controller conn write error");
45627Svbart@nginx.com 
45727Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
45827Svbart@nginx.com }
45927Svbart@nginx.com 
46027Svbart@nginx.com 
46127Svbart@nginx.com static void
46227Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data)
46327Svbart@nginx.com {
46427Svbart@nginx.com     nxt_timer_t       *ev;
46527Svbart@nginx.com     nxt_event_conn_t  *c;
46627Svbart@nginx.com 
46727Svbart@nginx.com     ev = obj;
46827Svbart@nginx.com 
46927Svbart@nginx.com     c = nxt_event_write_timer_conn(ev);
47027Svbart@nginx.com     c->socket.timedout = 1;
47127Svbart@nginx.com     c->socket.closed = 1;
47227Svbart@nginx.com 
47327Svbart@nginx.com     nxt_debug(task, "controller conn write timeout");
47427Svbart@nginx.com 
47527Svbart@nginx.com     nxt_controller_conn_close(task, c, data);
47620Sigor@sysoev.ru }
47720Sigor@sysoev.ru 
47820Sigor@sysoev.ru 
47920Sigor@sysoev.ru static const nxt_event_conn_state_t  nxt_controller_conn_close_state
48020Sigor@sysoev.ru     nxt_aligned(64) =
48120Sigor@sysoev.ru {
48220Sigor@sysoev.ru     NXT_EVENT_NO_BUF_PROCESS,
48320Sigor@sysoev.ru     NXT_EVENT_TIMER_NO_AUTORESET,
48420Sigor@sysoev.ru 
48520Sigor@sysoev.ru     nxt_controller_conn_free,
48620Sigor@sysoev.ru     NULL,
48720Sigor@sysoev.ru     NULL,
48820Sigor@sysoev.ru 
48920Sigor@sysoev.ru     NULL,
49020Sigor@sysoev.ru     NULL,
49120Sigor@sysoev.ru     0,
49220Sigor@sysoev.ru };
49320Sigor@sysoev.ru 
49420Sigor@sysoev.ru 
49520Sigor@sysoev.ru static void
49620Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data)
49720Sigor@sysoev.ru {
49820Sigor@sysoev.ru     nxt_event_conn_t  *c;
49920Sigor@sysoev.ru 
50020Sigor@sysoev.ru     c = obj;
50120Sigor@sysoev.ru 
50220Sigor@sysoev.ru     nxt_debug(task, "controller conn close");
50320Sigor@sysoev.ru 
50427Svbart@nginx.com     nxt_queue_remove(&c->link);
50527Svbart@nginx.com 
50620Sigor@sysoev.ru     c->write_state = &nxt_controller_conn_close_state;
50720Sigor@sysoev.ru 
50820Sigor@sysoev.ru     nxt_event_conn_close(task->thread->engine, c);
50920Sigor@sysoev.ru }
51020Sigor@sysoev.ru 
51120Sigor@sysoev.ru 
51220Sigor@sysoev.ru static void
51320Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data)
51420Sigor@sysoev.ru {
51520Sigor@sysoev.ru     nxt_event_conn_t  *c;
51620Sigor@sysoev.ru 
51720Sigor@sysoev.ru     c = obj;
51820Sigor@sysoev.ru 
51920Sigor@sysoev.ru     nxt_debug(task, "controller conn free");
52020Sigor@sysoev.ru 
52120Sigor@sysoev.ru     nxt_mem_pool_destroy(c->mem_pool);
52220Sigor@sysoev.ru 
52320Sigor@sysoev.ru     //nxt_free(c);
52420Sigor@sysoev.ru }
52527Svbart@nginx.com 
52627Svbart@nginx.com 
52727Svbart@nginx.com static nxt_int_t
52827Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_str_t *name,
52927Svbart@nginx.com     nxt_str_t *value, uintptr_t data)
53027Svbart@nginx.com {
53127Svbart@nginx.com     off_t                     length;
53227Svbart@nginx.com     nxt_controller_request_t  *r;
53327Svbart@nginx.com 
53427Svbart@nginx.com     r = ctx;
53527Svbart@nginx.com 
53627Svbart@nginx.com     length = nxt_off_t_parse(value->start, value->length);
53727Svbart@nginx.com 
53827Svbart@nginx.com     if (nxt_fast_path(length > 0)) {
53927Svbart@nginx.com         /* TODO length too big */
54027Svbart@nginx.com 
54127Svbart@nginx.com         r->length = length;
54227Svbart@nginx.com         return NXT_OK;
54327Svbart@nginx.com     }
54427Svbart@nginx.com 
54527Svbart@nginx.com     /* TODO logging (task?) */
54627Svbart@nginx.com 
54727Svbart@nginx.com     return NXT_ERROR;
54827Svbart@nginx.com }
54927Svbart@nginx.com 
55027Svbart@nginx.com 
55127Svbart@nginx.com static void
55227Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_event_conn_t *c,
553*44Svbart@nginx.com     nxt_controller_request_t *req)
55427Svbart@nginx.com {
555*44Svbart@nginx.com     nxt_controller_response_t  resp;
556*44Svbart@nginx.com 
557*44Svbart@nginx.com     nxt_memzero(&resp, sizeof(nxt_controller_response_t));
558*44Svbart@nginx.com 
559*44Svbart@nginx.com     if (nxt_str_eq(&req->parser.method, "GET", 3)) {
560*44Svbart@nginx.com         nxt_str_set(&resp.status_line, "200 OK");
561*44Svbart@nginx.com         resp.json_value = nxt_controller_conf.root;
562*44Svbart@nginx.com 
563*44Svbart@nginx.com     } else if (nxt_str_eq(&req->parser.method, "PUT", 3)) {
564*44Svbart@nginx.com 
565*44Svbart@nginx.com         if (nxt_controller_request_body_parse(task, c, req) == NXT_OK) {
56627Svbart@nginx.com 
567*44Svbart@nginx.com             nxt_mem_pool_destroy(nxt_controller_conf.pool);
568*44Svbart@nginx.com             nxt_controller_conf = req->conf;
569*44Svbart@nginx.com 
570*44Svbart@nginx.com             nxt_str_set(&resp.status_line, "201 Created");
571*44Svbart@nginx.com             nxt_str_set(&resp.json_string,
572*44Svbart@nginx.com                         "{ \"success\": \"Configuration updated\" }");
57327Svbart@nginx.com 
574*44Svbart@nginx.com         } else {
575*44Svbart@nginx.com             nxt_str_set(&resp.status_line, "400 Bad Request");
576*44Svbart@nginx.com             nxt_str_set(&resp.json_string,
577*44Svbart@nginx.com                         "{ \"error\": \"Invalid JSON\" }");
578*44Svbart@nginx.com         }
579*44Svbart@nginx.com 
580*44Svbart@nginx.com     } else {
581*44Svbart@nginx.com         nxt_str_set(&resp.status_line, "405 Method Not Allowed");
582*44Svbart@nginx.com         nxt_str_set(&resp.json_string, "{ \"error\": \"Invalid method\" }");
58329Svbart@nginx.com     }
58427Svbart@nginx.com 
585*44Svbart@nginx.com     if (nxt_controller_response(task, c, &resp) != NXT_OK) {
586*44Svbart@nginx.com         nxt_controller_conn_close(task, c, req);
58727Svbart@nginx.com     }
58827Svbart@nginx.com }
58927Svbart@nginx.com 
59027Svbart@nginx.com 
59127Svbart@nginx.com static nxt_int_t
59233Svbart@nginx.com nxt_controller_request_body_parse(nxt_task_t *task, nxt_event_conn_t *c,
59333Svbart@nginx.com     nxt_controller_request_t *r)
59427Svbart@nginx.com {
595*44Svbart@nginx.com     nxt_buf_mem_t          *mbuf;
596*44Svbart@nginx.com     nxt_mem_pool_t         *mp;
59729Svbart@nginx.com     nxt_conf_json_value_t  *value;
59829Svbart@nginx.com 
599*44Svbart@nginx.com     mp = nxt_mem_pool_create(512);
60029Svbart@nginx.com 
601*44Svbart@nginx.com     if (nxt_slow_path(mp == NULL)) {
602*44Svbart@nginx.com         return NXT_ERROR;
603*44Svbart@nginx.com     }
604*44Svbart@nginx.com 
605*44Svbart@nginx.com     mbuf = &c->read->mem;
606*44Svbart@nginx.com 
607*44Svbart@nginx.com     value = nxt_conf_json_parse(mbuf->pos, mbuf->free - mbuf->pos, mp);
60829Svbart@nginx.com 
60929Svbart@nginx.com     if (value == NULL) {
61029Svbart@nginx.com         return NXT_ERROR;
61129Svbart@nginx.com     }
61229Svbart@nginx.com 
613*44Svbart@nginx.com     r->conf.root = value;
614*44Svbart@nginx.com     r->conf.pool = mp;
61533Svbart@nginx.com 
61627Svbart@nginx.com     return NXT_OK;
61727Svbart@nginx.com }
61833Svbart@nginx.com 
61933Svbart@nginx.com 
620*44Svbart@nginx.com static nxt_int_t
621*44Svbart@nginx.com nxt_controller_response(nxt_task_t *task, nxt_event_conn_t *c,
622*44Svbart@nginx.com     nxt_controller_response_t *resp)
62333Svbart@nginx.com {
624*44Svbart@nginx.com     size_t     size;
62533Svbart@nginx.com     nxt_buf_t  *b;
62633Svbart@nginx.com 
627*44Svbart@nginx.com     size = sizeof("HTTP/1.0 " "\r\n\r\n") - 1
628*44Svbart@nginx.com            + resp->status_line.length
629*44Svbart@nginx.com            + resp->json_string.length;
63033Svbart@nginx.com 
631*44Svbart@nginx.com     b = nxt_buf_mem_alloc(c->mem_pool, size, 0);
63233Svbart@nginx.com     if (nxt_slow_path(b == NULL)) {
633*44Svbart@nginx.com         return NXT_ERROR;
63433Svbart@nginx.com     }
63533Svbart@nginx.com 
636*44Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, "HTTP/1.0 ", sizeof("HTTP/1.0 ") - 1);
637*44Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, resp->status_line.start,
638*44Svbart@nginx.com                              resp->status_line.length);
639*44Svbart@nginx.com 
640*44Svbart@nginx.com     b->mem.free = nxt_cpymem(b->mem.free, "\r\n\r\n", sizeof("\r\n\r\n") - 1);
641*44Svbart@nginx.com 
642*44Svbart@nginx.com     if (resp->json_string.length > 0) {
643*44Svbart@nginx.com         b->mem.free = nxt_cpymem(b->mem.free, resp->json_string.start,
644*44Svbart@nginx.com                                  resp->json_string.length);
645*44Svbart@nginx.com     }
64633Svbart@nginx.com 
64733Svbart@nginx.com     c->write = b;
648*44Svbart@nginx.com     c->write_state = &nxt_controller_conn_write_state;
64933Svbart@nginx.com 
650*44Svbart@nginx.com     if (resp->json_value != NULL) {
651*44Svbart@nginx.com         b = nxt_conf_json_print(resp->json_value, c->mem_pool);
65233Svbart@nginx.com 
653*44Svbart@nginx.com         if (b == NULL) {
654*44Svbart@nginx.com             return NXT_ERROR;
655*44Svbart@nginx.com         }
656*44Svbart@nginx.com 
657*44Svbart@nginx.com         c->write->next = b;
65833Svbart@nginx.com     }
65933Svbart@nginx.com 
660*44Svbart@nginx.com     nxt_event_conn_write(task->thread->engine, c);
66133Svbart@nginx.com 
662*44Svbart@nginx.com     return NXT_OK;
66333Svbart@nginx.com }
664