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> 10240Sigor@sysoev.ru #include <nxt_main_process.h> 1129Svbart@nginx.com #include <nxt_conf.h> 1220Sigor@sysoev.ru 1320Sigor@sysoev.ru 1427Svbart@nginx.com typedef struct { 15106Svbart@nginx.com nxt_conf_value_t *root; 16106Svbart@nginx.com nxt_mp_t *pool; 1744Svbart@nginx.com } nxt_controller_conf_t; 1844Svbart@nginx.com 1944Svbart@nginx.com 2044Svbart@nginx.com typedef struct { 2127Svbart@nginx.com nxt_http_request_parse_t parser; 2227Svbart@nginx.com size_t length; 2344Svbart@nginx.com nxt_controller_conf_t conf; 24140Svbart@nginx.com nxt_conn_t *conn; 25140Svbart@nginx.com nxt_queue_link_t link; 2627Svbart@nginx.com } nxt_controller_request_t; 2727Svbart@nginx.com 2827Svbart@nginx.com 2944Svbart@nginx.com typedef struct { 30208Svbart@nginx.com nxt_uint_t status; 31106Svbart@nginx.com nxt_conf_value_t *conf; 32208Svbart@nginx.com 33208Svbart@nginx.com u_char *title; 34357Svbart@nginx.com nxt_str_t detail; 35208Svbart@nginx.com ssize_t offset; 36208Svbart@nginx.com nxt_uint_t line; 37208Svbart@nginx.com nxt_uint_t column; 3844Svbart@nginx.com } nxt_controller_response_t; 3944Svbart@nginx.com 4044Svbart@nginx.com 41248Svbart@nginx.com static void nxt_controller_process_new_port_handler(nxt_task_t *task, 42248Svbart@nginx.com nxt_port_recv_msg_t *msg); 43249Svbart@nginx.com static nxt_int_t nxt_controller_conf_default(void); 44249Svbart@nginx.com static void nxt_controller_conf_init_handler(nxt_task_t *task, 45249Svbart@nginx.com nxt_port_recv_msg_t *msg, void *data); 46249Svbart@nginx.com static nxt_int_t nxt_controller_conf_send(nxt_task_t *task, 47249Svbart@nginx.com nxt_conf_value_t *conf, nxt_port_rpc_handler_t handler, void *data); 48248Svbart@nginx.com 4920Sigor@sysoev.ru static void nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data); 5020Sigor@sysoev.ru static void nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data); 5162Sigor@sysoev.ru static nxt_msec_t nxt_controller_conn_timeout_value(nxt_conn_t *c, 5220Sigor@sysoev.ru uintptr_t data); 5320Sigor@sysoev.ru static void nxt_controller_conn_read_error(nxt_task_t *task, void *obj, 5420Sigor@sysoev.ru void *data); 5520Sigor@sysoev.ru static void nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, 5620Sigor@sysoev.ru void *data); 5727Svbart@nginx.com static void nxt_controller_conn_body_read(nxt_task_t *task, void *obj, 5827Svbart@nginx.com void *data); 5927Svbart@nginx.com static void nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data); 6027Svbart@nginx.com static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj, 6127Svbart@nginx.com void *data); 6227Svbart@nginx.com static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, 6327Svbart@nginx.com void *data); 6420Sigor@sysoev.ru static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data); 6520Sigor@sysoev.ru static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data); 6620Sigor@sysoev.ru 6727Svbart@nginx.com static nxt_int_t nxt_controller_request_content_length(void *ctx, 68*417Svbart@nginx.com nxt_http_field_t *field, uintptr_t data); 6927Svbart@nginx.com 7027Svbart@nginx.com static void nxt_controller_process_request(nxt_task_t *task, 71140Svbart@nginx.com nxt_controller_request_t *req); 72238Svbart@nginx.com static void nxt_controller_conf_handler(nxt_task_t *task, 73238Svbart@nginx.com nxt_port_recv_msg_t *msg, void *data); 74314Svbart@nginx.com static void nxt_controller_conf_store(nxt_task_t *task, 75314Svbart@nginx.com nxt_conf_value_t *conf); 76140Svbart@nginx.com static void nxt_controller_response(nxt_task_t *task, 77140Svbart@nginx.com nxt_controller_request_t *req, nxt_controller_response_t *resp); 78208Svbart@nginx.com static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now, 79208Svbart@nginx.com struct tm *tm, size_t size, const char *format); 8027Svbart@nginx.com 8127Svbart@nginx.com 82*417Svbart@nginx.com static nxt_http_field_proc_t nxt_controller_request_fields[] = { 8327Svbart@nginx.com { nxt_string("Content-Length"), 8427Svbart@nginx.com &nxt_controller_request_content_length, 0 }, 8527Svbart@nginx.com }; 8627Svbart@nginx.com 87*417Svbart@nginx.com static nxt_lvlhsh_t nxt_controller_fields_hash; 8827Svbart@nginx.com 89314Svbart@nginx.com static nxt_uint_t nxt_controller_listening; 90238Svbart@nginx.com static nxt_controller_conf_t nxt_controller_conf; 91238Svbart@nginx.com static nxt_queue_t nxt_controller_waiting_requests; 9227Svbart@nginx.com 9320Sigor@sysoev.ru 9420Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_read_state; 9527Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_body_read_state; 9627Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_write_state; 9720Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_close_state; 9820Sigor@sysoev.ru 9920Sigor@sysoev.ru 100320Smax.romanov@nginx.com nxt_port_handlers_t nxt_controller_process_port_handlers = { 101320Smax.romanov@nginx.com .quit = nxt_worker_process_quit_handler, 102320Smax.romanov@nginx.com .new_port = nxt_controller_process_new_port_handler, 103320Smax.romanov@nginx.com .change_file = nxt_port_change_log_file_handler, 104320Smax.romanov@nginx.com .mmap = nxt_port_mmap_handler, 105320Smax.romanov@nginx.com .data = nxt_port_data_handler, 106320Smax.romanov@nginx.com .remove_pid = nxt_port_remove_pid_handler, 107320Smax.romanov@nginx.com .rpc_ready = nxt_port_rpc_handler, 108320Smax.romanov@nginx.com .rpc_error = nxt_port_rpc_handler, 109248Svbart@nginx.com }; 110248Svbart@nginx.com 111248Svbart@nginx.com 11220Sigor@sysoev.ru nxt_int_t 113141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data) 11420Sigor@sysoev.ru { 115*417Svbart@nginx.com nxt_mp_t *mp; 116*417Svbart@nginx.com nxt_int_t ret; 117*417Svbart@nginx.com nxt_str_t *json; 118*417Svbart@nginx.com nxt_runtime_t *rt; 119*417Svbart@nginx.com nxt_conf_value_t *conf; 120*417Svbart@nginx.com nxt_event_engine_t *engine; 121*417Svbart@nginx.com nxt_conf_validation_t vldt; 12227Svbart@nginx.com 123141Smax.romanov@nginx.com rt = task->thread->runtime; 124141Smax.romanov@nginx.com 125337Sigor@sysoev.ru engine = task->thread->engine; 126337Sigor@sysoev.ru 127337Sigor@sysoev.ru engine->mem_pool = nxt_mp_create(4096, 128, 1024, 64); 128337Sigor@sysoev.ru if (nxt_slow_path(engine->mem_pool == NULL)) { 129337Sigor@sysoev.ru return NXT_ERROR; 130337Sigor@sysoev.ru } 131337Sigor@sysoev.ru 132*417Svbart@nginx.com ret = nxt_http_fields_hash(&nxt_controller_fields_hash, rt->mem_pool, 133*417Svbart@nginx.com nxt_controller_request_fields, 134*417Svbart@nginx.com nxt_nitems(nxt_controller_request_fields)); 135*417Svbart@nginx.com 136*417Svbart@nginx.com if (nxt_slow_path(ret != NXT_OK)) { 13727Svbart@nginx.com return NXT_ERROR; 13827Svbart@nginx.com } 13927Svbart@nginx.com 140248Svbart@nginx.com nxt_queue_init(&nxt_controller_waiting_requests); 14127Svbart@nginx.com 142314Svbart@nginx.com json = data; 143314Svbart@nginx.com 144314Svbart@nginx.com if (json->length == 0) { 145314Svbart@nginx.com return NXT_OK; 146314Svbart@nginx.com } 147314Svbart@nginx.com 148314Svbart@nginx.com mp = nxt_mp_create(1024, 128, 256, 32); 149314Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 150314Svbart@nginx.com return NXT_ERROR; 151314Svbart@nginx.com } 152314Svbart@nginx.com 153314Svbart@nginx.com conf = nxt_conf_json_parse_str(mp, json); 154314Svbart@nginx.com nxt_free(json->start); 155314Svbart@nginx.com 156314Svbart@nginx.com if (nxt_slow_path(conf == NULL)) { 157314Svbart@nginx.com nxt_log(task, NXT_LOG_ALERT, 158314Svbart@nginx.com "failed to restore previous configuration: " 159314Svbart@nginx.com "file is corrupted or not enough memory"); 160314Svbart@nginx.com 161314Svbart@nginx.com nxt_mp_destroy(mp); 162314Svbart@nginx.com return NXT_OK; 163314Svbart@nginx.com } 164314Svbart@nginx.com 165357Svbart@nginx.com nxt_memzero(&vldt, sizeof(nxt_conf_validation_t)); 166314Svbart@nginx.com 167357Svbart@nginx.com vldt.pool = nxt_mp_create(1024, 128, 256, 32); 168357Svbart@nginx.com if (nxt_slow_path(vldt.pool == NULL)) { 169357Svbart@nginx.com return NXT_ERROR; 170314Svbart@nginx.com } 171314Svbart@nginx.com 172357Svbart@nginx.com vldt.conf = conf; 173357Svbart@nginx.com 174357Svbart@nginx.com ret = nxt_conf_validate(&vldt); 175357Svbart@nginx.com 176357Svbart@nginx.com if (nxt_slow_path(ret != NXT_OK)) { 177357Svbart@nginx.com 178357Svbart@nginx.com if (ret == NXT_DECLINED) { 179357Svbart@nginx.com nxt_log(task, NXT_LOG_ALERT, 180357Svbart@nginx.com "the previous configuration is invalid: %V", &vldt.error); 181357Svbart@nginx.com 182357Svbart@nginx.com nxt_mp_destroy(vldt.pool); 183357Svbart@nginx.com nxt_mp_destroy(mp); 184357Svbart@nginx.com 185357Svbart@nginx.com return NXT_OK; 186357Svbart@nginx.com } 187357Svbart@nginx.com 188357Svbart@nginx.com /* ret == NXT_ERROR */ 189357Svbart@nginx.com 190357Svbart@nginx.com return NXT_ERROR; 191357Svbart@nginx.com } 192357Svbart@nginx.com 193357Svbart@nginx.com nxt_mp_destroy(vldt.pool); 194314Svbart@nginx.com 195314Svbart@nginx.com nxt_controller_conf.root = conf; 196314Svbart@nginx.com nxt_controller_conf.pool = mp; 197314Svbart@nginx.com 198248Svbart@nginx.com return NXT_OK; 199248Svbart@nginx.com } 200248Svbart@nginx.com 201248Svbart@nginx.com 202248Svbart@nginx.com static void 203248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task, 204248Svbart@nginx.com nxt_port_recv_msg_t *msg) 205248Svbart@nginx.com { 206249Svbart@nginx.com nxt_int_t rc; 207248Svbart@nginx.com nxt_runtime_t *rt; 208248Svbart@nginx.com nxt_conf_value_t *conf; 209248Svbart@nginx.com 210248Svbart@nginx.com nxt_port_new_port_handler(task, msg); 211248Svbart@nginx.com 212347Smax.romanov@nginx.com if (msg->u.new_port->type != NXT_PROCESS_ROUTER) { 213248Svbart@nginx.com return; 21420Sigor@sysoev.ru } 21520Sigor@sysoev.ru 216249Svbart@nginx.com conf = nxt_controller_conf.root; 217249Svbart@nginx.com 218249Svbart@nginx.com if (conf != NULL) { 219249Svbart@nginx.com rc = nxt_controller_conf_send(task, conf, 220249Svbart@nginx.com nxt_controller_conf_init_handler, NULL); 22144Svbart@nginx.com 222249Svbart@nginx.com if (nxt_fast_path(rc == NXT_OK)) { 223249Svbart@nginx.com return; 224249Svbart@nginx.com } 225249Svbart@nginx.com 226249Svbart@nginx.com nxt_mp_destroy(nxt_controller_conf.pool); 227249Svbart@nginx.com 228249Svbart@nginx.com if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) { 229249Svbart@nginx.com nxt_abort(); 230249Svbart@nginx.com } 23144Svbart@nginx.com } 23244Svbart@nginx.com 233249Svbart@nginx.com if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) { 234248Svbart@nginx.com nxt_abort(); 23544Svbart@nginx.com } 23644Svbart@nginx.com 237248Svbart@nginx.com rt = task->thread->runtime; 238140Svbart@nginx.com 239248Svbart@nginx.com if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) { 240248Svbart@nginx.com nxt_abort(); 241248Svbart@nginx.com } 242314Svbart@nginx.com 243314Svbart@nginx.com nxt_controller_listening = 1; 24420Sigor@sysoev.ru } 24520Sigor@sysoev.ru 24620Sigor@sysoev.ru 247249Svbart@nginx.com static nxt_int_t 248249Svbart@nginx.com nxt_controller_conf_default(void) 249249Svbart@nginx.com { 250249Svbart@nginx.com nxt_mp_t *mp; 251249Svbart@nginx.com nxt_conf_value_t *conf; 252249Svbart@nginx.com 253249Svbart@nginx.com static const nxt_str_t json 254249Svbart@nginx.com = nxt_string("{ \"listeners\": {}, \"applications\": {} }"); 255249Svbart@nginx.com 256249Svbart@nginx.com mp = nxt_mp_create(1024, 128, 256, 32); 257249Svbart@nginx.com 258249Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 259249Svbart@nginx.com return NXT_ERROR; 260249Svbart@nginx.com } 261249Svbart@nginx.com 262249Svbart@nginx.com conf = nxt_conf_json_parse_str(mp, &json); 263249Svbart@nginx.com 264249Svbart@nginx.com if (nxt_slow_path(conf == NULL)) { 265249Svbart@nginx.com return NXT_ERROR; 266249Svbart@nginx.com } 267249Svbart@nginx.com 268249Svbart@nginx.com nxt_controller_conf.root = conf; 269249Svbart@nginx.com nxt_controller_conf.pool = mp; 270249Svbart@nginx.com 271249Svbart@nginx.com return NXT_OK; 272249Svbart@nginx.com } 273249Svbart@nginx.com 274249Svbart@nginx.com 275249Svbart@nginx.com static void 276249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, 277249Svbart@nginx.com void *data) 278249Svbart@nginx.com { 279314Svbart@nginx.com nxt_runtime_t *rt; 280314Svbart@nginx.com 281249Svbart@nginx.com if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) { 282314Svbart@nginx.com nxt_log(task, NXT_LOG_ALERT, "failed to apply previous configuration"); 283314Svbart@nginx.com 284249Svbart@nginx.com nxt_mp_destroy(nxt_controller_conf.pool); 285249Svbart@nginx.com 286249Svbart@nginx.com if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) { 287249Svbart@nginx.com nxt_abort(); 288249Svbart@nginx.com } 289249Svbart@nginx.com } 290314Svbart@nginx.com 291314Svbart@nginx.com if (nxt_controller_listening == 0) { 292314Svbart@nginx.com rt = task->thread->runtime; 293314Svbart@nginx.com 294314Svbart@nginx.com if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) 295314Svbart@nginx.com == NULL)) 296314Svbart@nginx.com { 297314Svbart@nginx.com nxt_abort(); 298314Svbart@nginx.com } 299314Svbart@nginx.com 300314Svbart@nginx.com nxt_controller_listening = 1; 301314Svbart@nginx.com } 302249Svbart@nginx.com } 303249Svbart@nginx.com 304249Svbart@nginx.com 305249Svbart@nginx.com static nxt_int_t 306249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf, 307249Svbart@nginx.com nxt_port_rpc_handler_t handler, void *data) 308249Svbart@nginx.com { 309249Svbart@nginx.com size_t size; 310249Svbart@nginx.com uint32_t stream; 311249Svbart@nginx.com nxt_int_t rc; 312249Svbart@nginx.com nxt_buf_t *b; 313249Svbart@nginx.com nxt_port_t *router_port, *controller_port; 314249Svbart@nginx.com nxt_runtime_t *rt; 315249Svbart@nginx.com 316249Svbart@nginx.com rt = task->thread->runtime; 317249Svbart@nginx.com 318249Svbart@nginx.com router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; 319249Svbart@nginx.com 320249Svbart@nginx.com if (nxt_slow_path(router_port == NULL)) { 321249Svbart@nginx.com return NXT_DECLINED; 322249Svbart@nginx.com } 323249Svbart@nginx.com 324249Svbart@nginx.com controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER]; 325249Svbart@nginx.com 326249Svbart@nginx.com size = nxt_conf_json_length(conf, NULL); 327249Svbart@nginx.com 328249Svbart@nginx.com b = nxt_port_mmap_get_buf(task, router_port, size); 329379Smax.romanov@nginx.com if (nxt_slow_path(b == NULL)) { 330379Smax.romanov@nginx.com return NXT_ERROR; 331379Smax.romanov@nginx.com } 332249Svbart@nginx.com 333249Svbart@nginx.com b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL); 334249Svbart@nginx.com 335249Svbart@nginx.com stream = nxt_port_rpc_register_handler(task, controller_port, 336249Svbart@nginx.com handler, handler, 337249Svbart@nginx.com router_port->pid, data); 338249Svbart@nginx.com 339249Svbart@nginx.com rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1, 340249Svbart@nginx.com stream, controller_port->id, b); 341249Svbart@nginx.com 342249Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 343249Svbart@nginx.com nxt_port_rpc_cancel(task, controller_port, stream); 344249Svbart@nginx.com return NXT_ERROR; 345249Svbart@nginx.com } 346249Svbart@nginx.com 347249Svbart@nginx.com return NXT_OK; 348249Svbart@nginx.com } 349249Svbart@nginx.com 350249Svbart@nginx.com 35120Sigor@sysoev.ru nxt_int_t 35220Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt) 35320Sigor@sysoev.ru { 35420Sigor@sysoev.ru nxt_sockaddr_t *sa; 35520Sigor@sysoev.ru nxt_listen_socket_t *ls; 35620Sigor@sysoev.ru 35720Sigor@sysoev.ru sa = rt->controller_listen; 35820Sigor@sysoev.ru 35965Sigor@sysoev.ru ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t)); 36020Sigor@sysoev.ru if (ls == NULL) { 36120Sigor@sysoev.ru return NXT_ERROR; 36220Sigor@sysoev.ru } 36320Sigor@sysoev.ru 36420Sigor@sysoev.ru ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr, 36520Sigor@sysoev.ru sa->socklen, sa->length); 36620Sigor@sysoev.ru if (ls->sockaddr == NULL) { 36720Sigor@sysoev.ru return NXT_ERROR; 36820Sigor@sysoev.ru } 36920Sigor@sysoev.ru 37020Sigor@sysoev.ru ls->sockaddr->type = sa->type; 371312Sigor@sysoev.ru nxt_sockaddr_text(ls->sockaddr); 37220Sigor@sysoev.ru 373359Sigor@sysoev.ru nxt_listen_socket_remote_size(ls); 37420Sigor@sysoev.ru 37520Sigor@sysoev.ru ls->socket = -1; 37620Sigor@sysoev.ru ls->backlog = NXT_LISTEN_BACKLOG; 37720Sigor@sysoev.ru ls->read_after_accept = 1; 37820Sigor@sysoev.ru ls->flags = NXT_NONBLOCK; 37920Sigor@sysoev.ru 38020Sigor@sysoev.ru #if 0 38120Sigor@sysoev.ru /* STUB */ 38265Sigor@sysoev.ru wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t)); 38320Sigor@sysoev.ru if (wq == NULL) { 38420Sigor@sysoev.ru return NXT_ERROR; 38520Sigor@sysoev.ru } 38620Sigor@sysoev.ru nxt_work_queue_name(wq, "listen"); 38720Sigor@sysoev.ru /**/ 38820Sigor@sysoev.ru 38920Sigor@sysoev.ru ls->work_queue = wq; 39020Sigor@sysoev.ru #endif 39120Sigor@sysoev.ru ls->handler = nxt_controller_conn_init; 39220Sigor@sysoev.ru 39320Sigor@sysoev.ru if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) { 39420Sigor@sysoev.ru return NXT_ERROR; 39520Sigor@sysoev.ru } 39620Sigor@sysoev.ru 39720Sigor@sysoev.ru rt->controller_socket = ls; 39820Sigor@sysoev.ru 39920Sigor@sysoev.ru return NXT_OK; 40020Sigor@sysoev.ru } 40120Sigor@sysoev.ru 40220Sigor@sysoev.ru 40320Sigor@sysoev.ru static void 40420Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data) 40520Sigor@sysoev.ru { 40627Svbart@nginx.com nxt_buf_t *b; 40762Sigor@sysoev.ru nxt_conn_t *c; 40827Svbart@nginx.com nxt_event_engine_t *engine; 40927Svbart@nginx.com nxt_controller_request_t *r; 41020Sigor@sysoev.ru 41120Sigor@sysoev.ru c = obj; 41220Sigor@sysoev.ru 41320Sigor@sysoev.ru nxt_debug(task, "controller conn init fd:%d", c->socket.fd); 41420Sigor@sysoev.ru 41565Sigor@sysoev.ru r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t)); 41627Svbart@nginx.com if (nxt_slow_path(r == NULL)) { 41727Svbart@nginx.com nxt_controller_conn_free(task, c, NULL); 41827Svbart@nginx.com return; 41927Svbart@nginx.com } 42027Svbart@nginx.com 421140Svbart@nginx.com r->conn = c; 422140Svbart@nginx.com 42360Svbart@nginx.com if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool) 42460Svbart@nginx.com != NXT_OK)) 42560Svbart@nginx.com { 42660Svbart@nginx.com nxt_controller_conn_free(task, c, NULL); 42760Svbart@nginx.com return; 42860Svbart@nginx.com } 42927Svbart@nginx.com 43020Sigor@sysoev.ru b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0); 43120Sigor@sysoev.ru if (nxt_slow_path(b == NULL)) { 43220Sigor@sysoev.ru nxt_controller_conn_free(task, c, NULL); 43320Sigor@sysoev.ru return; 43420Sigor@sysoev.ru } 43520Sigor@sysoev.ru 43620Sigor@sysoev.ru c->read = b; 43727Svbart@nginx.com c->socket.data = r; 43820Sigor@sysoev.ru c->socket.read_ready = 1; 43920Sigor@sysoev.ru c->read_state = &nxt_controller_conn_read_state; 44020Sigor@sysoev.ru 44120Sigor@sysoev.ru engine = task->thread->engine; 44220Sigor@sysoev.ru c->read_work_queue = &engine->read_work_queue; 44327Svbart@nginx.com c->write_work_queue = &engine->write_work_queue; 44420Sigor@sysoev.ru 44562Sigor@sysoev.ru nxt_conn_read(engine, c); 44620Sigor@sysoev.ru } 44720Sigor@sysoev.ru 44820Sigor@sysoev.ru 44920Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_read_state 45020Sigor@sysoev.ru nxt_aligned(64) = 45120Sigor@sysoev.ru { 45256Sigor@sysoev.ru .ready_handler = nxt_controller_conn_read, 45356Sigor@sysoev.ru .close_handler = nxt_controller_conn_close, 45456Sigor@sysoev.ru .error_handler = nxt_controller_conn_read_error, 45520Sigor@sysoev.ru 45656Sigor@sysoev.ru .timer_handler = nxt_controller_conn_read_timeout, 45756Sigor@sysoev.ru .timer_value = nxt_controller_conn_timeout_value, 45856Sigor@sysoev.ru .timer_data = 60 * 1000, 45920Sigor@sysoev.ru }; 46020Sigor@sysoev.ru 46120Sigor@sysoev.ru 46220Sigor@sysoev.ru static void 46320Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) 46420Sigor@sysoev.ru { 46527Svbart@nginx.com size_t preread; 46627Svbart@nginx.com nxt_buf_t *b; 46727Svbart@nginx.com nxt_int_t rc; 46862Sigor@sysoev.ru nxt_conn_t *c; 46927Svbart@nginx.com nxt_controller_request_t *r; 47020Sigor@sysoev.ru 47120Sigor@sysoev.ru c = obj; 47227Svbart@nginx.com r = data; 47320Sigor@sysoev.ru 47420Sigor@sysoev.ru nxt_debug(task, "controller conn read"); 47520Sigor@sysoev.ru 47627Svbart@nginx.com nxt_queue_remove(&c->link); 47727Svbart@nginx.com nxt_queue_self(&c->link); 47827Svbart@nginx.com 47927Svbart@nginx.com b = c->read; 48027Svbart@nginx.com 48127Svbart@nginx.com rc = nxt_http_parse_request(&r->parser, &b->mem); 48227Svbart@nginx.com 48327Svbart@nginx.com if (nxt_slow_path(rc != NXT_DONE)) { 48427Svbart@nginx.com 48527Svbart@nginx.com if (rc == NXT_AGAIN) { 48627Svbart@nginx.com if (nxt_buf_mem_free_size(&b->mem) == 0) { 48727Svbart@nginx.com nxt_log(task, NXT_LOG_ERR, "too long request headers"); 48827Svbart@nginx.com nxt_controller_conn_close(task, c, r); 48927Svbart@nginx.com return; 49027Svbart@nginx.com } 49127Svbart@nginx.com 49262Sigor@sysoev.ru nxt_conn_read(task->thread->engine, c); 49327Svbart@nginx.com return; 49427Svbart@nginx.com } 49527Svbart@nginx.com 49627Svbart@nginx.com /* rc == NXT_ERROR */ 49727Svbart@nginx.com 49827Svbart@nginx.com nxt_log(task, NXT_LOG_ERR, "parsing error"); 49927Svbart@nginx.com 50027Svbart@nginx.com nxt_controller_conn_close(task, c, r); 50127Svbart@nginx.com return; 50227Svbart@nginx.com } 50327Svbart@nginx.com 504*417Svbart@nginx.com rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash, 505*417Svbart@nginx.com r); 50660Svbart@nginx.com 50760Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 50860Svbart@nginx.com nxt_controller_conn_close(task, c, r); 50960Svbart@nginx.com return; 51060Svbart@nginx.com } 51160Svbart@nginx.com 51227Svbart@nginx.com preread = nxt_buf_mem_used_size(&b->mem); 51327Svbart@nginx.com 51427Svbart@nginx.com nxt_debug(task, "controller request header parsing complete, " 515107Svbart@nginx.com "body length: %uz, preread: %uz", 51627Svbart@nginx.com r->length, preread); 51727Svbart@nginx.com 51827Svbart@nginx.com if (preread >= r->length) { 519140Svbart@nginx.com nxt_controller_process_request(task, r); 52027Svbart@nginx.com return; 52127Svbart@nginx.com } 52227Svbart@nginx.com 52327Svbart@nginx.com if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) { 52427Svbart@nginx.com b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0); 52527Svbart@nginx.com if (nxt_slow_path(b == NULL)) { 52627Svbart@nginx.com nxt_controller_conn_free(task, c, NULL); 52727Svbart@nginx.com return; 52827Svbart@nginx.com } 52927Svbart@nginx.com 53027Svbart@nginx.com b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread); 53127Svbart@nginx.com 53227Svbart@nginx.com c->read = b; 53327Svbart@nginx.com } 53427Svbart@nginx.com 53527Svbart@nginx.com c->read_state = &nxt_controller_conn_body_read_state; 53627Svbart@nginx.com 53762Sigor@sysoev.ru nxt_conn_read(task->thread->engine, c); 53820Sigor@sysoev.ru } 53920Sigor@sysoev.ru 54020Sigor@sysoev.ru 54120Sigor@sysoev.ru static nxt_msec_t 54262Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data) 54320Sigor@sysoev.ru { 54420Sigor@sysoev.ru return (nxt_msec_t) data; 54520Sigor@sysoev.ru } 54620Sigor@sysoev.ru 54720Sigor@sysoev.ru 54820Sigor@sysoev.ru static void 54920Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data) 55020Sigor@sysoev.ru { 55162Sigor@sysoev.ru nxt_conn_t *c; 55220Sigor@sysoev.ru 55320Sigor@sysoev.ru c = obj; 55420Sigor@sysoev.ru 55520Sigor@sysoev.ru nxt_debug(task, "controller conn read error"); 55620Sigor@sysoev.ru 55727Svbart@nginx.com nxt_controller_conn_close(task, c, data); 55820Sigor@sysoev.ru } 55920Sigor@sysoev.ru 56020Sigor@sysoev.ru 56120Sigor@sysoev.ru static void 56220Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data) 56320Sigor@sysoev.ru { 56462Sigor@sysoev.ru nxt_timer_t *timer; 56562Sigor@sysoev.ru nxt_conn_t *c; 56620Sigor@sysoev.ru 56762Sigor@sysoev.ru timer = obj; 56820Sigor@sysoev.ru 56962Sigor@sysoev.ru c = nxt_read_timer_conn(timer); 57020Sigor@sysoev.ru c->socket.timedout = 1; 57120Sigor@sysoev.ru c->socket.closed = 1; 57220Sigor@sysoev.ru 57320Sigor@sysoev.ru nxt_debug(task, "controller conn read timeout"); 57420Sigor@sysoev.ru 57527Svbart@nginx.com nxt_controller_conn_close(task, c, data); 57627Svbart@nginx.com } 57727Svbart@nginx.com 57827Svbart@nginx.com 57927Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_body_read_state 58027Svbart@nginx.com nxt_aligned(64) = 58127Svbart@nginx.com { 58256Sigor@sysoev.ru .ready_handler = nxt_controller_conn_body_read, 58356Sigor@sysoev.ru .close_handler = nxt_controller_conn_close, 58456Sigor@sysoev.ru .error_handler = nxt_controller_conn_read_error, 58527Svbart@nginx.com 58656Sigor@sysoev.ru .timer_handler = nxt_controller_conn_read_timeout, 58756Sigor@sysoev.ru .timer_value = nxt_controller_conn_timeout_value, 58856Sigor@sysoev.ru .timer_data = 60 * 1000, 58956Sigor@sysoev.ru .timer_autoreset = 1, 59027Svbart@nginx.com }; 59127Svbart@nginx.com 59227Svbart@nginx.com 59327Svbart@nginx.com static void 59427Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data) 59527Svbart@nginx.com { 596107Svbart@nginx.com size_t read; 597107Svbart@nginx.com nxt_buf_t *b; 598107Svbart@nginx.com nxt_conn_t *c; 599107Svbart@nginx.com nxt_controller_request_t *r; 60027Svbart@nginx.com 60127Svbart@nginx.com c = obj; 602107Svbart@nginx.com r = data; 60327Svbart@nginx.com b = c->read; 60427Svbart@nginx.com 605107Svbart@nginx.com read = nxt_buf_mem_used_size(&b->mem); 60627Svbart@nginx.com 607107Svbart@nginx.com nxt_debug(task, "controller conn body read: %uz of %uz", 608107Svbart@nginx.com read, r->length); 60927Svbart@nginx.com 610107Svbart@nginx.com if (read >= r->length) { 611140Svbart@nginx.com nxt_controller_process_request(task, r); 61227Svbart@nginx.com return; 61327Svbart@nginx.com } 61427Svbart@nginx.com 61562Sigor@sysoev.ru nxt_conn_read(task->thread->engine, c); 61627Svbart@nginx.com } 61727Svbart@nginx.com 61827Svbart@nginx.com 61927Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_write_state 62027Svbart@nginx.com nxt_aligned(64) = 62127Svbart@nginx.com { 62256Sigor@sysoev.ru .ready_handler = nxt_controller_conn_write, 62356Sigor@sysoev.ru .error_handler = nxt_controller_conn_write_error, 62427Svbart@nginx.com 62556Sigor@sysoev.ru .timer_handler = nxt_controller_conn_write_timeout, 62656Sigor@sysoev.ru .timer_value = nxt_controller_conn_timeout_value, 62756Sigor@sysoev.ru .timer_data = 60 * 1000, 62856Sigor@sysoev.ru .timer_autoreset = 1, 62927Svbart@nginx.com }; 63027Svbart@nginx.com 63127Svbart@nginx.com 63227Svbart@nginx.com static void 63327Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data) 63427Svbart@nginx.com { 63562Sigor@sysoev.ru nxt_buf_t *b; 63662Sigor@sysoev.ru nxt_conn_t *c; 63727Svbart@nginx.com 63827Svbart@nginx.com c = obj; 63927Svbart@nginx.com 64027Svbart@nginx.com nxt_debug(task, "controller conn write"); 64127Svbart@nginx.com 64227Svbart@nginx.com b = c->write; 64327Svbart@nginx.com 64427Svbart@nginx.com if (b->mem.pos != b->mem.free) { 64562Sigor@sysoev.ru nxt_conn_write(task->thread->engine, c); 64627Svbart@nginx.com return; 64727Svbart@nginx.com } 64827Svbart@nginx.com 64927Svbart@nginx.com nxt_debug(task, "controller conn write complete"); 65027Svbart@nginx.com 65127Svbart@nginx.com nxt_controller_conn_close(task, c, data); 65227Svbart@nginx.com } 65327Svbart@nginx.com 65427Svbart@nginx.com 65527Svbart@nginx.com static void 65627Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data) 65727Svbart@nginx.com { 65862Sigor@sysoev.ru nxt_conn_t *c; 65927Svbart@nginx.com 66027Svbart@nginx.com c = obj; 66127Svbart@nginx.com 66227Svbart@nginx.com nxt_debug(task, "controller conn write error"); 66327Svbart@nginx.com 66427Svbart@nginx.com nxt_controller_conn_close(task, c, data); 66527Svbart@nginx.com } 66627Svbart@nginx.com 66727Svbart@nginx.com 66827Svbart@nginx.com static void 66927Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data) 67027Svbart@nginx.com { 67162Sigor@sysoev.ru nxt_conn_t *c; 67262Sigor@sysoev.ru nxt_timer_t *timer; 67327Svbart@nginx.com 67462Sigor@sysoev.ru timer = obj; 67527Svbart@nginx.com 67662Sigor@sysoev.ru c = nxt_write_timer_conn(timer); 67727Svbart@nginx.com c->socket.timedout = 1; 67827Svbart@nginx.com c->socket.closed = 1; 67927Svbart@nginx.com 68027Svbart@nginx.com nxt_debug(task, "controller conn write timeout"); 68127Svbart@nginx.com 68227Svbart@nginx.com nxt_controller_conn_close(task, c, data); 68320Sigor@sysoev.ru } 68420Sigor@sysoev.ru 68520Sigor@sysoev.ru 68620Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_close_state 68720Sigor@sysoev.ru nxt_aligned(64) = 68820Sigor@sysoev.ru { 68956Sigor@sysoev.ru .ready_handler = nxt_controller_conn_free, 69020Sigor@sysoev.ru }; 69120Sigor@sysoev.ru 69220Sigor@sysoev.ru 69320Sigor@sysoev.ru static void 69420Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data) 69520Sigor@sysoev.ru { 69662Sigor@sysoev.ru nxt_conn_t *c; 69720Sigor@sysoev.ru 69820Sigor@sysoev.ru c = obj; 69920Sigor@sysoev.ru 70020Sigor@sysoev.ru nxt_debug(task, "controller conn close"); 70120Sigor@sysoev.ru 70227Svbart@nginx.com nxt_queue_remove(&c->link); 70327Svbart@nginx.com 70420Sigor@sysoev.ru c->write_state = &nxt_controller_conn_close_state; 70520Sigor@sysoev.ru 70662Sigor@sysoev.ru nxt_conn_close(task->thread->engine, c); 70720Sigor@sysoev.ru } 70820Sigor@sysoev.ru 70920Sigor@sysoev.ru 71020Sigor@sysoev.ru static void 71120Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data) 71220Sigor@sysoev.ru { 71362Sigor@sysoev.ru nxt_conn_t *c; 71420Sigor@sysoev.ru 71520Sigor@sysoev.ru c = obj; 71620Sigor@sysoev.ru 71720Sigor@sysoev.ru nxt_debug(task, "controller conn free"); 71820Sigor@sysoev.ru 719337Sigor@sysoev.ru nxt_sockaddr_cache_free(task->thread->engine, c); 720337Sigor@sysoev.ru 721386Sigor@sysoev.ru nxt_conn_free(task, c); 72220Sigor@sysoev.ru } 72327Svbart@nginx.com 72427Svbart@nginx.com 72527Svbart@nginx.com static nxt_int_t 72660Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field, 727*417Svbart@nginx.com uintptr_t data) 72827Svbart@nginx.com { 72927Svbart@nginx.com off_t length; 73027Svbart@nginx.com nxt_controller_request_t *r; 73127Svbart@nginx.com 73227Svbart@nginx.com r = ctx; 73327Svbart@nginx.com 734*417Svbart@nginx.com length = nxt_off_t_parse(field->value, field->value_length); 73527Svbart@nginx.com 73627Svbart@nginx.com if (nxt_fast_path(length > 0)) { 737107Svbart@nginx.com 738107Svbart@nginx.com if (nxt_slow_path(length > NXT_SIZE_T_MAX)) { 739*417Svbart@nginx.com nxt_log_error(NXT_LOG_ERR, &r->conn->log, 740*417Svbart@nginx.com "Content-Length is too big"); 741107Svbart@nginx.com return NXT_ERROR; 742107Svbart@nginx.com } 74327Svbart@nginx.com 74427Svbart@nginx.com r->length = length; 74527Svbart@nginx.com return NXT_OK; 74627Svbart@nginx.com } 74727Svbart@nginx.com 748*417Svbart@nginx.com nxt_log_error(NXT_LOG_ERR, &r->conn->log, "Content-Length is invalid"); 74927Svbart@nginx.com 75027Svbart@nginx.com return NXT_ERROR; 75127Svbart@nginx.com } 75227Svbart@nginx.com 75327Svbart@nginx.com 75427Svbart@nginx.com static void 755140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) 75627Svbart@nginx.com { 75765Sigor@sysoev.ru nxt_mp_t *mp; 75851Svbart@nginx.com nxt_int_t rc; 75946Svbart@nginx.com nxt_str_t path; 760140Svbart@nginx.com nxt_conn_t *c; 76151Svbart@nginx.com nxt_buf_mem_t *mbuf; 762106Svbart@nginx.com nxt_conf_op_t *ops; 763106Svbart@nginx.com nxt_conf_value_t *value; 764357Svbart@nginx.com nxt_conf_validation_t vldt; 765208Svbart@nginx.com nxt_conf_json_error_t error; 76644Svbart@nginx.com nxt_controller_response_t resp; 76744Svbart@nginx.com 76851Svbart@nginx.com static const nxt_str_t empty_obj = nxt_string("{}"); 76951Svbart@nginx.com 770140Svbart@nginx.com c = req->conn; 771112Smax.romanov@nginx.com path = req->parser.path; 77251Svbart@nginx.com 77351Svbart@nginx.com if (path.length > 1 && path.start[path.length - 1] == '/') { 77451Svbart@nginx.com path.length--; 77551Svbart@nginx.com } 77651Svbart@nginx.com 77744Svbart@nginx.com nxt_memzero(&resp, sizeof(nxt_controller_response_t)); 77844Svbart@nginx.com 77944Svbart@nginx.com if (nxt_str_eq(&req->parser.method, "GET", 3)) { 78046Svbart@nginx.com 781106Svbart@nginx.com value = nxt_conf_get_path(nxt_controller_conf.root, &path); 78251Svbart@nginx.com 78351Svbart@nginx.com if (value == NULL) { 784208Svbart@nginx.com goto not_found; 78551Svbart@nginx.com } 78651Svbart@nginx.com 787208Svbart@nginx.com resp.status = 200; 788106Svbart@nginx.com resp.conf = value; 78946Svbart@nginx.com 790208Svbart@nginx.com nxt_controller_response(task, req, &resp); 791208Svbart@nginx.com return; 79251Svbart@nginx.com } 79351Svbart@nginx.com 79451Svbart@nginx.com if (nxt_str_eq(&req->parser.method, "PUT", 3)) { 79546Svbart@nginx.com 796238Svbart@nginx.com if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) { 797238Svbart@nginx.com nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link); 798238Svbart@nginx.com return; 799238Svbart@nginx.com } 800238Svbart@nginx.com 80165Sigor@sysoev.ru mp = nxt_mp_create(1024, 128, 256, 32); 80251Svbart@nginx.com 80351Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 804208Svbart@nginx.com goto alloc_fail; 80546Svbart@nginx.com } 80646Svbart@nginx.com 80751Svbart@nginx.com mbuf = &c->read->mem; 80851Svbart@nginx.com 809208Svbart@nginx.com nxt_memzero(&error, sizeof(nxt_conf_json_error_t)); 810208Svbart@nginx.com 811208Svbart@nginx.com value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error); 81251Svbart@nginx.com 81351Svbart@nginx.com if (value == NULL) { 81465Sigor@sysoev.ru nxt_mp_destroy(mp); 815208Svbart@nginx.com 816208Svbart@nginx.com if (error.pos == NULL) { 817208Svbart@nginx.com goto alloc_fail; 818208Svbart@nginx.com } 819208Svbart@nginx.com 820208Svbart@nginx.com resp.status = 400; 821208Svbart@nginx.com resp.title = (u_char *) "Invalid JSON."; 822357Svbart@nginx.com resp.detail.length = nxt_strlen(error.detail); 823357Svbart@nginx.com resp.detail.start = error.detail; 824208Svbart@nginx.com resp.offset = error.pos - mbuf->pos; 825208Svbart@nginx.com 826208Svbart@nginx.com nxt_conf_json_position(mbuf->pos, error.pos, 827208Svbart@nginx.com &resp.line, &resp.column); 828208Svbart@nginx.com 829208Svbart@nginx.com nxt_controller_response(task, req, &resp); 830208Svbart@nginx.com return; 83151Svbart@nginx.com } 83251Svbart@nginx.com 83351Svbart@nginx.com if (path.length != 1) { 834106Svbart@nginx.com rc = nxt_conf_op_compile(c->mem_pool, &ops, 835106Svbart@nginx.com nxt_controller_conf.root, 836106Svbart@nginx.com &path, value); 83746Svbart@nginx.com 83851Svbart@nginx.com if (rc != NXT_OK) { 83951Svbart@nginx.com if (rc == NXT_DECLINED) { 840208Svbart@nginx.com goto not_found; 84151Svbart@nginx.com } 84246Svbart@nginx.com 843208Svbart@nginx.com goto alloc_fail; 84451Svbart@nginx.com } 84551Svbart@nginx.com 846106Svbart@nginx.com value = nxt_conf_clone(mp, ops, nxt_controller_conf.root); 84751Svbart@nginx.com 84851Svbart@nginx.com if (nxt_slow_path(value == NULL)) { 84965Sigor@sysoev.ru nxt_mp_destroy(mp); 850208Svbart@nginx.com goto alloc_fail; 85151Svbart@nginx.com } 85246Svbart@nginx.com } 85344Svbart@nginx.com 854357Svbart@nginx.com nxt_memzero(&vldt, sizeof(nxt_conf_validation_t)); 855357Svbart@nginx.com 856357Svbart@nginx.com vldt.conf = value; 857357Svbart@nginx.com vldt.pool = c->mem_pool; 858357Svbart@nginx.com 859357Svbart@nginx.com rc = nxt_conf_validate(&vldt); 860357Svbart@nginx.com 861357Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 862121Svbart@nginx.com nxt_mp_destroy(mp); 863357Svbart@nginx.com 864357Svbart@nginx.com if (rc == NXT_DECLINED) { 865357Svbart@nginx.com resp.detail = vldt.error; 866357Svbart@nginx.com goto invalid_conf; 867357Svbart@nginx.com } 868357Svbart@nginx.com 869357Svbart@nginx.com /* rc == NXT_ERROR */ 870357Svbart@nginx.com goto alloc_fail; 871116Svbart@nginx.com } 872116Svbart@nginx.com 873249Svbart@nginx.com rc = nxt_controller_conf_send(task, value, 874249Svbart@nginx.com nxt_controller_conf_handler, req); 875247Svbart@nginx.com 876247Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 877121Svbart@nginx.com nxt_mp_destroy(mp); 878247Svbart@nginx.com 879247Svbart@nginx.com if (rc == NXT_DECLINED) { 880247Svbart@nginx.com goto no_router; 881247Svbart@nginx.com } 882247Svbart@nginx.com 883247Svbart@nginx.com /* rc == NXT_ERROR */ 884208Svbart@nginx.com goto alloc_fail; 885121Svbart@nginx.com } 886121Svbart@nginx.com 887249Svbart@nginx.com req->conf.root = value; 888249Svbart@nginx.com req->conf.pool = mp; 889249Svbart@nginx.com 890249Svbart@nginx.com nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link); 891249Svbart@nginx.com 892140Svbart@nginx.com return; 89351Svbart@nginx.com } 89427Svbart@nginx.com 89551Svbart@nginx.com if (nxt_str_eq(&req->parser.method, "DELETE", 6)) { 89651Svbart@nginx.com 897238Svbart@nginx.com if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) { 898238Svbart@nginx.com nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link); 899238Svbart@nginx.com return; 900238Svbart@nginx.com } 901238Svbart@nginx.com 90251Svbart@nginx.com if (path.length == 1) { 90365Sigor@sysoev.ru mp = nxt_mp_create(1024, 128, 256, 32); 90444Svbart@nginx.com 90551Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 906208Svbart@nginx.com goto alloc_fail; 90751Svbart@nginx.com } 90851Svbart@nginx.com 909106Svbart@nginx.com value = nxt_conf_json_parse_str(mp, &empty_obj); 91027Svbart@nginx.com 91144Svbart@nginx.com } else { 912106Svbart@nginx.com rc = nxt_conf_op_compile(c->mem_pool, &ops, 913106Svbart@nginx.com nxt_controller_conf.root, 914106Svbart@nginx.com &path, NULL); 91551Svbart@nginx.com 91651Svbart@nginx.com if (rc != NXT_OK) { 91751Svbart@nginx.com if (rc == NXT_DECLINED) { 918208Svbart@nginx.com goto not_found; 91951Svbart@nginx.com } 92051Svbart@nginx.com 921208Svbart@nginx.com goto alloc_fail; 92251Svbart@nginx.com } 92351Svbart@nginx.com 92465Sigor@sysoev.ru mp = nxt_mp_create(1024, 128, 256, 32); 92551Svbart@nginx.com 92651Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 927208Svbart@nginx.com goto alloc_fail; 92851Svbart@nginx.com } 92951Svbart@nginx.com 930106Svbart@nginx.com value = nxt_conf_clone(mp, ops, nxt_controller_conf.root); 93151Svbart@nginx.com } 93251Svbart@nginx.com 93351Svbart@nginx.com if (nxt_slow_path(value == NULL)) { 93465Sigor@sysoev.ru nxt_mp_destroy(mp); 935208Svbart@nginx.com goto alloc_fail; 93644Svbart@nginx.com } 93744Svbart@nginx.com 938357Svbart@nginx.com nxt_memzero(&vldt, sizeof(nxt_conf_validation_t)); 939357Svbart@nginx.com 940357Svbart@nginx.com vldt.conf = value; 941357Svbart@nginx.com vldt.pool = c->mem_pool; 942357Svbart@nginx.com 943357Svbart@nginx.com rc = nxt_conf_validate(&vldt); 944357Svbart@nginx.com 945357Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 946121Svbart@nginx.com nxt_mp_destroy(mp); 947357Svbart@nginx.com 948357Svbart@nginx.com if (rc == NXT_DECLINED) { 949357Svbart@nginx.com resp.detail = vldt.error; 950357Svbart@nginx.com goto invalid_conf; 951357Svbart@nginx.com } 952357Svbart@nginx.com 953357Svbart@nginx.com /* rc == NXT_ERROR */ 954357Svbart@nginx.com goto alloc_fail; 955116Svbart@nginx.com } 956116Svbart@nginx.com 957249Svbart@nginx.com rc = nxt_controller_conf_send(task, value, 958249Svbart@nginx.com nxt_controller_conf_handler, req); 959247Svbart@nginx.com 960247Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 961121Svbart@nginx.com nxt_mp_destroy(mp); 962247Svbart@nginx.com 963247Svbart@nginx.com if (rc == NXT_DECLINED) { 964247Svbart@nginx.com goto no_router; 965247Svbart@nginx.com } 966247Svbart@nginx.com 967247Svbart@nginx.com /* rc == NXT_ERROR */ 968208Svbart@nginx.com goto alloc_fail; 969121Svbart@nginx.com } 970121Svbart@nginx.com 971249Svbart@nginx.com req->conf.root = value; 972249Svbart@nginx.com req->conf.pool = mp; 973249Svbart@nginx.com 974249Svbart@nginx.com nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link); 975249Svbart@nginx.com 976140Svbart@nginx.com return; 97751Svbart@nginx.com } 97851Svbart@nginx.com 979208Svbart@nginx.com resp.status = 405; 980208Svbart@nginx.com resp.title = (u_char *) "Invalid method."; 981208Svbart@nginx.com resp.offset = -1; 98251Svbart@nginx.com 983208Svbart@nginx.com nxt_controller_response(task, req, &resp); 984208Svbart@nginx.com return; 98551Svbart@nginx.com 986208Svbart@nginx.com not_found: 987208Svbart@nginx.com 988208Svbart@nginx.com resp.status = 404; 989208Svbart@nginx.com resp.title = (u_char *) "Value doesn't exist."; 990208Svbart@nginx.com resp.offset = -1; 991208Svbart@nginx.com 992208Svbart@nginx.com nxt_controller_response(task, req, &resp); 993208Svbart@nginx.com return; 994208Svbart@nginx.com 995208Svbart@nginx.com invalid_conf: 996208Svbart@nginx.com 997208Svbart@nginx.com resp.status = 400; 998208Svbart@nginx.com resp.title = (u_char *) "Invalid configuration."; 999208Svbart@nginx.com resp.offset = -1; 1000208Svbart@nginx.com 1001208Svbart@nginx.com nxt_controller_response(task, req, &resp); 1002208Svbart@nginx.com return; 1003247Svbart@nginx.com 1004247Svbart@nginx.com alloc_fail: 1005247Svbart@nginx.com 1006247Svbart@nginx.com resp.status = 500; 1007247Svbart@nginx.com resp.title = (u_char *) "Memory allocation failed."; 1008247Svbart@nginx.com resp.offset = -1; 1009247Svbart@nginx.com 1010247Svbart@nginx.com nxt_controller_response(task, req, &resp); 1011247Svbart@nginx.com return; 1012247Svbart@nginx.com 1013247Svbart@nginx.com no_router: 1014247Svbart@nginx.com 1015247Svbart@nginx.com resp.status = 500; 1016247Svbart@nginx.com resp.title = (u_char *) "Router process isn't available."; 1017247Svbart@nginx.com resp.offset = -1; 1018247Svbart@nginx.com 1019247Svbart@nginx.com nxt_controller_response(task, req, &resp); 1020247Svbart@nginx.com return; 102127Svbart@nginx.com } 102227Svbart@nginx.com 102327Svbart@nginx.com 1024193Smax.romanov@nginx.com static void 1025193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, 1026193Smax.romanov@nginx.com void *data) 1027140Svbart@nginx.com { 1028238Svbart@nginx.com nxt_queue_t queue; 1029140Svbart@nginx.com nxt_controller_request_t *req; 1030140Svbart@nginx.com nxt_controller_response_t resp; 1031140Svbart@nginx.com 1032238Svbart@nginx.com req = data; 1033238Svbart@nginx.com 1034201Svbart@nginx.com nxt_debug(task, "controller conf ready: %*s", 1035201Svbart@nginx.com nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos); 1036140Svbart@nginx.com 1037238Svbart@nginx.com nxt_queue_remove(&req->link); 1038140Svbart@nginx.com 1039238Svbart@nginx.com nxt_memzero(&resp, sizeof(nxt_controller_response_t)); 1040140Svbart@nginx.com 1041193Smax.romanov@nginx.com if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) { 1042140Svbart@nginx.com nxt_mp_destroy(nxt_controller_conf.pool); 1043140Svbart@nginx.com 1044140Svbart@nginx.com nxt_controller_conf = req->conf; 1045140Svbart@nginx.com 1046314Svbart@nginx.com nxt_controller_conf_store(task, req->conf.root); 1047314Svbart@nginx.com 1048208Svbart@nginx.com resp.status = 200; 1049208Svbart@nginx.com resp.title = (u_char *) "Reconfiguration done."; 1050140Svbart@nginx.com 1051140Svbart@nginx.com } else { 1052140Svbart@nginx.com nxt_mp_destroy(req->conf.pool); 1053140Svbart@nginx.com 1054208Svbart@nginx.com resp.status = 500; 1055208Svbart@nginx.com resp.title = (u_char *) "Failed to apply new configuration."; 1056208Svbart@nginx.com resp.offset = -1; 1057140Svbart@nginx.com } 1058140Svbart@nginx.com 1059140Svbart@nginx.com nxt_controller_response(task, req, &resp); 1060140Svbart@nginx.com 1061238Svbart@nginx.com nxt_queue_init(&queue); 1062238Svbart@nginx.com nxt_queue_add(&queue, &nxt_controller_waiting_requests); 1063140Svbart@nginx.com 1064238Svbart@nginx.com nxt_queue_init(&nxt_controller_waiting_requests); 1065121Svbart@nginx.com 1066238Svbart@nginx.com nxt_queue_each(req, &queue, nxt_controller_request_t, link) { 1067238Svbart@nginx.com nxt_controller_process_request(task, req); 1068238Svbart@nginx.com } nxt_queue_loop; 1069121Svbart@nginx.com } 1070121Svbart@nginx.com 1071121Svbart@nginx.com 1072140Svbart@nginx.com static void 1073314Svbart@nginx.com nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf) 1074314Svbart@nginx.com { 1075314Svbart@nginx.com size_t size; 1076314Svbart@nginx.com nxt_buf_t *b; 1077314Svbart@nginx.com nxt_port_t *main_port; 1078314Svbart@nginx.com nxt_runtime_t *rt; 1079314Svbart@nginx.com 1080314Svbart@nginx.com rt = task->thread->runtime; 1081314Svbart@nginx.com 1082314Svbart@nginx.com main_port = rt->port_by_type[NXT_PROCESS_MAIN]; 1083314Svbart@nginx.com 1084314Svbart@nginx.com size = nxt_conf_json_length(conf, NULL); 1085314Svbart@nginx.com 1086342Smax.romanov@nginx.com b = nxt_buf_mem_ts_alloc(task, task->thread->engine->mem_pool, size); 1087314Svbart@nginx.com 1088314Svbart@nginx.com if (nxt_fast_path(b != NULL)) { 1089314Svbart@nginx.com b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL); 1090314Svbart@nginx.com 1091314Svbart@nginx.com (void) nxt_port_socket_write(task, main_port, NXT_PORT_MSG_CONF_STORE, 1092314Svbart@nginx.com -1, 0, -1, b); 1093314Svbart@nginx.com } 1094314Svbart@nginx.com } 1095314Svbart@nginx.com 1096314Svbart@nginx.com 1097314Svbart@nginx.com static void 1098140Svbart@nginx.com nxt_controller_response(nxt_task_t *task, nxt_controller_request_t *req, 109944Svbart@nginx.com nxt_controller_response_t *resp) 110033Svbart@nginx.com { 1101208Svbart@nginx.com size_t size; 1102208Svbart@nginx.com nxt_str_t status_line, str; 1103208Svbart@nginx.com nxt_buf_t *b, *body; 1104208Svbart@nginx.com nxt_conn_t *c; 1105208Svbart@nginx.com nxt_uint_t n; 1106208Svbart@nginx.com nxt_conf_value_t *value, *location; 1107208Svbart@nginx.com nxt_conf_json_pretty_t pretty; 1108208Svbart@nginx.com 1109208Svbart@nginx.com static nxt_str_t success_str = nxt_string("success"); 1110208Svbart@nginx.com static nxt_str_t error_str = nxt_string("error"); 1111208Svbart@nginx.com static nxt_str_t detail_str = nxt_string("detail"); 1112208Svbart@nginx.com static nxt_str_t location_str = nxt_string("location"); 1113208Svbart@nginx.com static nxt_str_t offset_str = nxt_string("offset"); 1114208Svbart@nginx.com static nxt_str_t line_str = nxt_string("line"); 1115208Svbart@nginx.com static nxt_str_t column_str = nxt_string("column"); 1116208Svbart@nginx.com 1117208Svbart@nginx.com static nxt_time_string_t date_cache = { 1118208Svbart@nginx.com (nxt_atomic_uint_t) -1, 1119208Svbart@nginx.com nxt_controller_date, 1120208Svbart@nginx.com "%s, %02d %s %4d %02d:%02d:%02d GMT", 1121208Svbart@nginx.com sizeof("Wed, 31 Dec 1986 16:40:00 GMT") - 1, 1122208Svbart@nginx.com NXT_THREAD_TIME_GMT, 1123208Svbart@nginx.com NXT_THREAD_TIME_SEC, 1124208Svbart@nginx.com }; 1125208Svbart@nginx.com 1126208Svbart@nginx.com switch (resp->status) { 1127208Svbart@nginx.com 1128208Svbart@nginx.com case 200: 1129208Svbart@nginx.com nxt_str_set(&status_line, "200 OK"); 1130208Svbart@nginx.com break; 1131208Svbart@nginx.com 1132208Svbart@nginx.com case 400: 1133208Svbart@nginx.com nxt_str_set(&status_line, "400 Bad Request"); 1134208Svbart@nginx.com break; 1135208Svbart@nginx.com 1136208Svbart@nginx.com case 404: 1137208Svbart@nginx.com nxt_str_set(&status_line, "404 Not Found"); 1138208Svbart@nginx.com break; 1139208Svbart@nginx.com 1140208Svbart@nginx.com case 405: 1141208Svbart@nginx.com nxt_str_set(&status_line, "405 Method Not Allowed"); 1142208Svbart@nginx.com break; 1143208Svbart@nginx.com 1144209Svbart@nginx.com default: 1145208Svbart@nginx.com nxt_str_set(&status_line, "500 Internal Server Error"); 1146208Svbart@nginx.com break; 1147208Svbart@nginx.com } 1148140Svbart@nginx.com 1149140Svbart@nginx.com c = req->conn; 1150208Svbart@nginx.com value = resp->conf; 115133Svbart@nginx.com 1152208Svbart@nginx.com if (value == NULL) { 1153208Svbart@nginx.com n = 1 1154357Svbart@nginx.com + (resp->detail.length != 0) 1155208Svbart@nginx.com + (resp->status >= 400 && resp->offset != -1); 1156208Svbart@nginx.com 1157208Svbart@nginx.com value = nxt_conf_create_object(c->mem_pool, n); 1158208Svbart@nginx.com 1159208Svbart@nginx.com if (nxt_slow_path(value == NULL)) { 1160208Svbart@nginx.com nxt_controller_conn_close(task, c, req); 1161208Svbart@nginx.com return; 1162208Svbart@nginx.com } 1163208Svbart@nginx.com 1164208Svbart@nginx.com str.length = nxt_strlen(resp->title); 1165208Svbart@nginx.com str.start = resp->title; 1166208Svbart@nginx.com 1167208Svbart@nginx.com if (resp->status < 400) { 1168208Svbart@nginx.com nxt_conf_set_member_string(value, &success_str, &str, 0); 1169208Svbart@nginx.com 1170208Svbart@nginx.com } else { 1171208Svbart@nginx.com nxt_conf_set_member_string(value, &error_str, &str, 0); 1172208Svbart@nginx.com } 1173208Svbart@nginx.com 1174208Svbart@nginx.com n = 0; 1175208Svbart@nginx.com 1176357Svbart@nginx.com if (resp->detail.length != 0) { 1177208Svbart@nginx.com n++; 1178208Svbart@nginx.com 1179357Svbart@nginx.com nxt_conf_set_member_string(value, &detail_str, &resp->detail, n); 1180208Svbart@nginx.com } 1181208Svbart@nginx.com 1182208Svbart@nginx.com if (resp->status >= 400 && resp->offset != -1) { 1183208Svbart@nginx.com n++; 1184208Svbart@nginx.com 1185208Svbart@nginx.com location = nxt_conf_create_object(c->mem_pool, 1186208Svbart@nginx.com resp->line != 0 ? 3 : 1); 1187208Svbart@nginx.com 1188208Svbart@nginx.com nxt_conf_set_member(value, &location_str, location, n); 1189208Svbart@nginx.com 1190208Svbart@nginx.com nxt_conf_set_member_integer(location, &offset_str, resp->offset, 0); 1191208Svbart@nginx.com 1192208Svbart@nginx.com if (resp->line != 0) { 1193208Svbart@nginx.com nxt_conf_set_member_integer(location, &line_str, 1194208Svbart@nginx.com resp->line, 1); 1195208Svbart@nginx.com 1196208Svbart@nginx.com nxt_conf_set_member_integer(location, &column_str, 1197208Svbart@nginx.com resp->column, 2); 1198208Svbart@nginx.com } 1199208Svbart@nginx.com } 1200208Svbart@nginx.com } 1201208Svbart@nginx.com 1202208Svbart@nginx.com nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t)); 1203208Svbart@nginx.com 1204208Svbart@nginx.com size = nxt_conf_json_length(value, &pretty) + 2; 1205208Svbart@nginx.com 1206208Svbart@nginx.com body = nxt_buf_mem_alloc(c->mem_pool, size, 0); 1207208Svbart@nginx.com if (nxt_slow_path(body == NULL)) { 1208208Svbart@nginx.com nxt_controller_conn_close(task, c, req); 1209208Svbart@nginx.com return; 1210208Svbart@nginx.com } 1211208Svbart@nginx.com 1212208Svbart@nginx.com nxt_memzero(&pretty, sizeof(nxt_conf_json_pretty_t)); 1213208Svbart@nginx.com 1214208Svbart@nginx.com body->mem.free = nxt_conf_json_print(body->mem.free, value, &pretty); 1215208Svbart@nginx.com 1216208Svbart@nginx.com body->mem.free = nxt_cpymem(body->mem.free, "\r\n", 2); 1217208Svbart@nginx.com 1218208Svbart@nginx.com size = sizeof("HTTP/1.1 " "\r\n") - 1 + status_line.length 1219259Sigor@sysoev.ru + sizeof("Server: unit/" NXT_VERSION "\r\n") - 1 1220208Svbart@nginx.com + sizeof("Date: Wed, 31 Dec 1986 16:40:00 GMT\r\n") - 1 1221208Svbart@nginx.com + sizeof("Content-Type: application/json\r\n") - 1 1222208Svbart@nginx.com + sizeof("Content-Length: " "\r\n") - 1 + NXT_SIZE_T_LEN 1223208Svbart@nginx.com + sizeof("Connection: close\r\n") - 1 1224208Svbart@nginx.com + sizeof("\r\n") - 1; 122533Svbart@nginx.com 122644Svbart@nginx.com b = nxt_buf_mem_alloc(c->mem_pool, size, 0); 122733Svbart@nginx.com if (nxt_slow_path(b == NULL)) { 1228140Svbart@nginx.com nxt_controller_conn_close(task, c, req); 1229140Svbart@nginx.com return; 123033Svbart@nginx.com } 123133Svbart@nginx.com 1232208Svbart@nginx.com b->next = body; 1233208Svbart@nginx.com 1234208Svbart@nginx.com nxt_str_set(&str, "HTTP/1.1 "); 123544Svbart@nginx.com 1236208Svbart@nginx.com b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); 1237208Svbart@nginx.com b->mem.free = nxt_cpymem(b->mem.free, status_line.start, 1238208Svbart@nginx.com status_line.length); 1239208Svbart@nginx.com 1240208Svbart@nginx.com nxt_str_set(&str, "\r\n" 1241259Sigor@sysoev.ru "Server: unit/" NXT_VERSION "\r\n" 1242208Svbart@nginx.com "Date: "); 1243208Svbart@nginx.com 1244208Svbart@nginx.com b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); 124544Svbart@nginx.com 1246208Svbart@nginx.com b->mem.free = nxt_thread_time_string(task->thread, &date_cache, 1247208Svbart@nginx.com b->mem.free); 1248208Svbart@nginx.com 1249208Svbart@nginx.com nxt_str_set(&str, "\r\n" 1250208Svbart@nginx.com "Content-Type: application/json\r\n" 1251208Svbart@nginx.com "Content-Length: "); 1252208Svbart@nginx.com 1253208Svbart@nginx.com b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); 125445Svbart@nginx.com 1255208Svbart@nginx.com b->mem.free = nxt_sprintf(b->mem.free, b->mem.end, "%uz", 1256208Svbart@nginx.com nxt_buf_mem_used_size(&body->mem)); 1257208Svbart@nginx.com 1258208Svbart@nginx.com nxt_str_set(&str, "\r\n" 1259208Svbart@nginx.com "Connection: close\r\n" 1260208Svbart@nginx.com "\r\n"); 1261208Svbart@nginx.com 1262208Svbart@nginx.com b->mem.free = nxt_cpymem(b->mem.free, str.start, str.length); 126333Svbart@nginx.com 126433Svbart@nginx.com c->write = b; 126544Svbart@nginx.com c->write_state = &nxt_controller_conn_write_state; 126633Svbart@nginx.com 126762Sigor@sysoev.ru nxt_conn_write(task->thread->engine, c); 126833Svbart@nginx.com } 126945Svbart@nginx.com 127045Svbart@nginx.com 1271208Svbart@nginx.com static u_char * 1272208Svbart@nginx.com nxt_controller_date(u_char *buf, nxt_realtime_t *now, struct tm *tm, 1273208Svbart@nginx.com size_t size, const char *format) 127445Svbart@nginx.com { 1275208Svbart@nginx.com static const char *week[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", 1276208Svbart@nginx.com "Sat" }; 127745Svbart@nginx.com 1278208Svbart@nginx.com static const char *month[] = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", 1279208Svbart@nginx.com "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" }; 128045Svbart@nginx.com 1281208Svbart@nginx.com return nxt_sprintf(buf, buf + size, format, 1282208Svbart@nginx.com week[tm->tm_wday], tm->tm_mday, 1283208Svbart@nginx.com month[tm->tm_mon], tm->tm_year + 1900, 1284208Svbart@nginx.com tm->tm_hour, tm->tm_min, tm->tm_sec); 128545Svbart@nginx.com } 1286