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; 34208Svbart@nginx.com u_char *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, 6867Svbart@nginx.com nxt_http_field_t *field, nxt_log_t *log); 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); 74*314Svbart@nginx.com static void nxt_controller_conf_store(nxt_task_t *task, 75*314Svbart@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 8260Svbart@nginx.com static nxt_http_fields_hash_entry_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 { nxt_null_string, NULL, 0 } 8727Svbart@nginx.com }; 8827Svbart@nginx.com 8960Svbart@nginx.com static nxt_http_fields_hash_t *nxt_controller_fields_hash; 9027Svbart@nginx.com 91*314Svbart@nginx.com static nxt_uint_t nxt_controller_listening; 92238Svbart@nginx.com static nxt_controller_conf_t nxt_controller_conf; 93238Svbart@nginx.com static nxt_queue_t nxt_controller_waiting_requests; 9427Svbart@nginx.com 9520Sigor@sysoev.ru 9620Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_read_state; 9727Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_body_read_state; 9827Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_write_state; 9920Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_close_state; 10020Sigor@sysoev.ru 10120Sigor@sysoev.ru 102248Svbart@nginx.com nxt_port_handler_t nxt_controller_process_port_handlers[] = { 103248Svbart@nginx.com nxt_worker_process_quit_handler, 104248Svbart@nginx.com nxt_controller_process_new_port_handler, 105248Svbart@nginx.com nxt_port_change_log_file_handler, 106248Svbart@nginx.com nxt_port_mmap_handler, 107248Svbart@nginx.com nxt_port_data_handler, 108248Svbart@nginx.com nxt_port_remove_pid_handler, 109248Svbart@nginx.com NULL, /* NXT_PORT_MSG_READY */ 110248Svbart@nginx.com NULL, /* NXT_PORT_MSG_START_WORKER */ 111248Svbart@nginx.com NULL, /* NXT_PORT_MSG_SOCKET */ 112248Svbart@nginx.com NULL, /* NXT_PORT_MSG_MODULES */ 113248Svbart@nginx.com nxt_port_rpc_handler, 114248Svbart@nginx.com nxt_port_rpc_handler, 115248Svbart@nginx.com }; 116248Svbart@nginx.com 117248Svbart@nginx.com 11820Sigor@sysoev.ru nxt_int_t 119141Smax.romanov@nginx.com nxt_controller_start(nxt_task_t *task, void *data) 12020Sigor@sysoev.ru { 121*314Svbart@nginx.com nxt_mp_t *mp; 122*314Svbart@nginx.com nxt_str_t *json; 123141Smax.romanov@nginx.com nxt_runtime_t *rt; 124*314Svbart@nginx.com nxt_conf_value_t *conf; 12527Svbart@nginx.com nxt_http_fields_hash_t *hash; 12627Svbart@nginx.com 127141Smax.romanov@nginx.com rt = task->thread->runtime; 128141Smax.romanov@nginx.com 12960Svbart@nginx.com hash = nxt_http_fields_hash_create(nxt_controller_request_fields, 13060Svbart@nginx.com rt->mem_pool); 13127Svbart@nginx.com if (nxt_slow_path(hash == NULL)) { 13227Svbart@nginx.com return NXT_ERROR; 13327Svbart@nginx.com } 13427Svbart@nginx.com 13560Svbart@nginx.com nxt_controller_fields_hash = hash; 136248Svbart@nginx.com nxt_queue_init(&nxt_controller_waiting_requests); 13727Svbart@nginx.com 138*314Svbart@nginx.com json = data; 139*314Svbart@nginx.com 140*314Svbart@nginx.com if (json->length == 0) { 141*314Svbart@nginx.com return NXT_OK; 142*314Svbart@nginx.com } 143*314Svbart@nginx.com 144*314Svbart@nginx.com mp = nxt_mp_create(1024, 128, 256, 32); 145*314Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 146*314Svbart@nginx.com return NXT_ERROR; 147*314Svbart@nginx.com } 148*314Svbart@nginx.com 149*314Svbart@nginx.com conf = nxt_conf_json_parse_str(mp, json); 150*314Svbart@nginx.com nxt_free(json->start); 151*314Svbart@nginx.com 152*314Svbart@nginx.com if (nxt_slow_path(conf == NULL)) { 153*314Svbart@nginx.com nxt_log(task, NXT_LOG_ALERT, 154*314Svbart@nginx.com "failed to restore previous configuration: " 155*314Svbart@nginx.com "file is corrupted or not enough memory"); 156*314Svbart@nginx.com 157*314Svbart@nginx.com nxt_mp_destroy(mp); 158*314Svbart@nginx.com return NXT_OK; 159*314Svbart@nginx.com } 160*314Svbart@nginx.com 161*314Svbart@nginx.com if (nxt_slow_path(nxt_conf_validate(conf) != NXT_OK)) { 162*314Svbart@nginx.com nxt_log(task, NXT_LOG_ALERT, 163*314Svbart@nginx.com "failed to restore previous configuration: " 164*314Svbart@nginx.com "invalid configuration"); 165*314Svbart@nginx.com 166*314Svbart@nginx.com nxt_mp_destroy(mp); 167*314Svbart@nginx.com return NXT_OK; 168*314Svbart@nginx.com } 169*314Svbart@nginx.com 170*314Svbart@nginx.com 171*314Svbart@nginx.com nxt_controller_conf.root = conf; 172*314Svbart@nginx.com nxt_controller_conf.pool = mp; 173*314Svbart@nginx.com 174248Svbart@nginx.com return NXT_OK; 175248Svbart@nginx.com } 176248Svbart@nginx.com 177248Svbart@nginx.com 178248Svbart@nginx.com static void 179248Svbart@nginx.com nxt_controller_process_new_port_handler(nxt_task_t *task, 180248Svbart@nginx.com nxt_port_recv_msg_t *msg) 181248Svbart@nginx.com { 182249Svbart@nginx.com nxt_int_t rc; 183248Svbart@nginx.com nxt_runtime_t *rt; 184248Svbart@nginx.com nxt_conf_value_t *conf; 185248Svbart@nginx.com 186248Svbart@nginx.com nxt_port_new_port_handler(task, msg); 187248Svbart@nginx.com 188249Svbart@nginx.com if (msg->new_port->type != NXT_PROCESS_ROUTER) { 189248Svbart@nginx.com return; 19020Sigor@sysoev.ru } 19120Sigor@sysoev.ru 192249Svbart@nginx.com conf = nxt_controller_conf.root; 193249Svbart@nginx.com 194249Svbart@nginx.com if (conf != NULL) { 195249Svbart@nginx.com rc = nxt_controller_conf_send(task, conf, 196249Svbart@nginx.com nxt_controller_conf_init_handler, NULL); 19744Svbart@nginx.com 198249Svbart@nginx.com if (nxt_fast_path(rc == NXT_OK)) { 199249Svbart@nginx.com return; 200249Svbart@nginx.com } 201249Svbart@nginx.com 202249Svbart@nginx.com nxt_mp_destroy(nxt_controller_conf.pool); 203249Svbart@nginx.com 204249Svbart@nginx.com if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) { 205249Svbart@nginx.com nxt_abort(); 206249Svbart@nginx.com } 20744Svbart@nginx.com } 20844Svbart@nginx.com 209249Svbart@nginx.com if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) { 210248Svbart@nginx.com nxt_abort(); 21144Svbart@nginx.com } 21244Svbart@nginx.com 213248Svbart@nginx.com rt = task->thread->runtime; 214140Svbart@nginx.com 215248Svbart@nginx.com if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) == NULL)) { 216248Svbart@nginx.com nxt_abort(); 217248Svbart@nginx.com } 218*314Svbart@nginx.com 219*314Svbart@nginx.com nxt_controller_listening = 1; 22020Sigor@sysoev.ru } 22120Sigor@sysoev.ru 22220Sigor@sysoev.ru 223249Svbart@nginx.com static nxt_int_t 224249Svbart@nginx.com nxt_controller_conf_default(void) 225249Svbart@nginx.com { 226249Svbart@nginx.com nxt_mp_t *mp; 227249Svbart@nginx.com nxt_conf_value_t *conf; 228249Svbart@nginx.com 229249Svbart@nginx.com static const nxt_str_t json 230249Svbart@nginx.com = nxt_string("{ \"listeners\": {}, \"applications\": {} }"); 231249Svbart@nginx.com 232249Svbart@nginx.com mp = nxt_mp_create(1024, 128, 256, 32); 233249Svbart@nginx.com 234249Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 235249Svbart@nginx.com return NXT_ERROR; 236249Svbart@nginx.com } 237249Svbart@nginx.com 238249Svbart@nginx.com conf = nxt_conf_json_parse_str(mp, &json); 239249Svbart@nginx.com 240249Svbart@nginx.com if (nxt_slow_path(conf == NULL)) { 241249Svbart@nginx.com return NXT_ERROR; 242249Svbart@nginx.com } 243249Svbart@nginx.com 244249Svbart@nginx.com nxt_controller_conf.root = conf; 245249Svbart@nginx.com nxt_controller_conf.pool = mp; 246249Svbart@nginx.com 247249Svbart@nginx.com return NXT_OK; 248249Svbart@nginx.com } 249249Svbart@nginx.com 250249Svbart@nginx.com 251249Svbart@nginx.com static void 252249Svbart@nginx.com nxt_controller_conf_init_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, 253249Svbart@nginx.com void *data) 254249Svbart@nginx.com { 255*314Svbart@nginx.com nxt_runtime_t *rt; 256*314Svbart@nginx.com 257249Svbart@nginx.com if (msg->port_msg.type != NXT_PORT_MSG_RPC_READY) { 258*314Svbart@nginx.com nxt_log(task, NXT_LOG_ALERT, "failed to apply previous configuration"); 259*314Svbart@nginx.com 260249Svbart@nginx.com nxt_mp_destroy(nxt_controller_conf.pool); 261249Svbart@nginx.com 262249Svbart@nginx.com if (nxt_slow_path(nxt_controller_conf_default() != NXT_OK)) { 263249Svbart@nginx.com nxt_abort(); 264249Svbart@nginx.com } 265249Svbart@nginx.com } 266*314Svbart@nginx.com 267*314Svbart@nginx.com if (nxt_controller_listening == 0) { 268*314Svbart@nginx.com rt = task->thread->runtime; 269*314Svbart@nginx.com 270*314Svbart@nginx.com if (nxt_slow_path(nxt_listen_event(task, rt->controller_socket) 271*314Svbart@nginx.com == NULL)) 272*314Svbart@nginx.com { 273*314Svbart@nginx.com nxt_abort(); 274*314Svbart@nginx.com } 275*314Svbart@nginx.com 276*314Svbart@nginx.com nxt_controller_listening = 1; 277*314Svbart@nginx.com } 278249Svbart@nginx.com } 279249Svbart@nginx.com 280249Svbart@nginx.com 281249Svbart@nginx.com static nxt_int_t 282249Svbart@nginx.com nxt_controller_conf_send(nxt_task_t *task, nxt_conf_value_t *conf, 283249Svbart@nginx.com nxt_port_rpc_handler_t handler, void *data) 284249Svbart@nginx.com { 285249Svbart@nginx.com size_t size; 286249Svbart@nginx.com uint32_t stream; 287249Svbart@nginx.com nxt_int_t rc; 288249Svbart@nginx.com nxt_buf_t *b; 289249Svbart@nginx.com nxt_port_t *router_port, *controller_port; 290249Svbart@nginx.com nxt_runtime_t *rt; 291249Svbart@nginx.com 292249Svbart@nginx.com rt = task->thread->runtime; 293249Svbart@nginx.com 294249Svbart@nginx.com router_port = rt->port_by_type[NXT_PROCESS_ROUTER]; 295249Svbart@nginx.com 296249Svbart@nginx.com if (nxt_slow_path(router_port == NULL)) { 297249Svbart@nginx.com return NXT_DECLINED; 298249Svbart@nginx.com } 299249Svbart@nginx.com 300249Svbart@nginx.com controller_port = rt->port_by_type[NXT_PROCESS_CONTROLLER]; 301249Svbart@nginx.com 302249Svbart@nginx.com size = nxt_conf_json_length(conf, NULL); 303249Svbart@nginx.com 304249Svbart@nginx.com b = nxt_port_mmap_get_buf(task, router_port, size); 305249Svbart@nginx.com 306249Svbart@nginx.com b->mem.free = nxt_conf_json_print(b->mem.free, conf, NULL); 307249Svbart@nginx.com 308249Svbart@nginx.com stream = nxt_port_rpc_register_handler(task, controller_port, 309249Svbart@nginx.com handler, handler, 310249Svbart@nginx.com router_port->pid, data); 311249Svbart@nginx.com 312249Svbart@nginx.com rc = nxt_port_socket_write(task, router_port, NXT_PORT_MSG_DATA_LAST, -1, 313249Svbart@nginx.com stream, controller_port->id, b); 314249Svbart@nginx.com 315249Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 316249Svbart@nginx.com nxt_port_rpc_cancel(task, controller_port, stream); 317249Svbart@nginx.com return NXT_ERROR; 318249Svbart@nginx.com } 319249Svbart@nginx.com 320249Svbart@nginx.com return NXT_OK; 321249Svbart@nginx.com } 322249Svbart@nginx.com 323249Svbart@nginx.com 32420Sigor@sysoev.ru nxt_int_t 32520Sigor@sysoev.ru nxt_runtime_controller_socket(nxt_task_t *task, nxt_runtime_t *rt) 32620Sigor@sysoev.ru { 32720Sigor@sysoev.ru nxt_sockaddr_t *sa; 32820Sigor@sysoev.ru nxt_listen_socket_t *ls; 32920Sigor@sysoev.ru 33020Sigor@sysoev.ru sa = rt->controller_listen; 33120Sigor@sysoev.ru 33265Sigor@sysoev.ru ls = nxt_mp_alloc(rt->mem_pool, sizeof(nxt_listen_socket_t)); 33320Sigor@sysoev.ru if (ls == NULL) { 33420Sigor@sysoev.ru return NXT_ERROR; 33520Sigor@sysoev.ru } 33620Sigor@sysoev.ru 33720Sigor@sysoev.ru ls->sockaddr = nxt_sockaddr_create(rt->mem_pool, &sa->u.sockaddr, 33820Sigor@sysoev.ru sa->socklen, sa->length); 33920Sigor@sysoev.ru if (ls->sockaddr == NULL) { 34020Sigor@sysoev.ru return NXT_ERROR; 34120Sigor@sysoev.ru } 34220Sigor@sysoev.ru 34320Sigor@sysoev.ru ls->sockaddr->type = sa->type; 344312Sigor@sysoev.ru nxt_sockaddr_text(ls->sockaddr); 34520Sigor@sysoev.ru 346312Sigor@sysoev.ru nxt_listen_socket_remote_size(ls, sa); 34720Sigor@sysoev.ru 34820Sigor@sysoev.ru ls->socket = -1; 34920Sigor@sysoev.ru ls->backlog = NXT_LISTEN_BACKLOG; 35020Sigor@sysoev.ru ls->read_after_accept = 1; 35120Sigor@sysoev.ru ls->flags = NXT_NONBLOCK; 35220Sigor@sysoev.ru 35320Sigor@sysoev.ru #if 0 35420Sigor@sysoev.ru /* STUB */ 35565Sigor@sysoev.ru wq = nxt_mp_zget(cf->mem_pool, sizeof(nxt_work_queue_t)); 35620Sigor@sysoev.ru if (wq == NULL) { 35720Sigor@sysoev.ru return NXT_ERROR; 35820Sigor@sysoev.ru } 35920Sigor@sysoev.ru nxt_work_queue_name(wq, "listen"); 36020Sigor@sysoev.ru /**/ 36120Sigor@sysoev.ru 36220Sigor@sysoev.ru ls->work_queue = wq; 36320Sigor@sysoev.ru #endif 36420Sigor@sysoev.ru ls->handler = nxt_controller_conn_init; 36520Sigor@sysoev.ru 36620Sigor@sysoev.ru if (nxt_listen_socket_create(task, ls, 0) != NXT_OK) { 36720Sigor@sysoev.ru return NXT_ERROR; 36820Sigor@sysoev.ru } 36920Sigor@sysoev.ru 37020Sigor@sysoev.ru rt->controller_socket = ls; 37120Sigor@sysoev.ru 37220Sigor@sysoev.ru return NXT_OK; 37320Sigor@sysoev.ru } 37420Sigor@sysoev.ru 37520Sigor@sysoev.ru 37620Sigor@sysoev.ru static void 37720Sigor@sysoev.ru nxt_controller_conn_init(nxt_task_t *task, void *obj, void *data) 37820Sigor@sysoev.ru { 37927Svbart@nginx.com nxt_buf_t *b; 38062Sigor@sysoev.ru nxt_conn_t *c; 38127Svbart@nginx.com nxt_event_engine_t *engine; 38227Svbart@nginx.com nxt_controller_request_t *r; 38320Sigor@sysoev.ru 38420Sigor@sysoev.ru c = obj; 38520Sigor@sysoev.ru 38620Sigor@sysoev.ru nxt_debug(task, "controller conn init fd:%d", c->socket.fd); 38720Sigor@sysoev.ru 38865Sigor@sysoev.ru r = nxt_mp_zget(c->mem_pool, sizeof(nxt_controller_request_t)); 38927Svbart@nginx.com if (nxt_slow_path(r == NULL)) { 39027Svbart@nginx.com nxt_controller_conn_free(task, c, NULL); 39127Svbart@nginx.com return; 39227Svbart@nginx.com } 39327Svbart@nginx.com 394140Svbart@nginx.com r->conn = c; 395140Svbart@nginx.com 39660Svbart@nginx.com if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool) 39760Svbart@nginx.com != NXT_OK)) 39860Svbart@nginx.com { 39960Svbart@nginx.com nxt_controller_conn_free(task, c, NULL); 40060Svbart@nginx.com return; 40160Svbart@nginx.com } 40227Svbart@nginx.com 40367Svbart@nginx.com r->parser.fields_hash = nxt_controller_fields_hash; 40467Svbart@nginx.com 40520Sigor@sysoev.ru b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0); 40620Sigor@sysoev.ru if (nxt_slow_path(b == NULL)) { 40720Sigor@sysoev.ru nxt_controller_conn_free(task, c, NULL); 40820Sigor@sysoev.ru return; 40920Sigor@sysoev.ru } 41020Sigor@sysoev.ru 41120Sigor@sysoev.ru c->read = b; 41227Svbart@nginx.com c->socket.data = r; 41320Sigor@sysoev.ru c->socket.read_ready = 1; 41420Sigor@sysoev.ru c->read_state = &nxt_controller_conn_read_state; 41520Sigor@sysoev.ru 41620Sigor@sysoev.ru engine = task->thread->engine; 41720Sigor@sysoev.ru c->read_work_queue = &engine->read_work_queue; 41827Svbart@nginx.com c->write_work_queue = &engine->write_work_queue; 41920Sigor@sysoev.ru 42062Sigor@sysoev.ru nxt_conn_read(engine, c); 42120Sigor@sysoev.ru } 42220Sigor@sysoev.ru 42320Sigor@sysoev.ru 42420Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_read_state 42520Sigor@sysoev.ru nxt_aligned(64) = 42620Sigor@sysoev.ru { 42756Sigor@sysoev.ru .ready_handler = nxt_controller_conn_read, 42856Sigor@sysoev.ru .close_handler = nxt_controller_conn_close, 42956Sigor@sysoev.ru .error_handler = nxt_controller_conn_read_error, 43020Sigor@sysoev.ru 43156Sigor@sysoev.ru .timer_handler = nxt_controller_conn_read_timeout, 43256Sigor@sysoev.ru .timer_value = nxt_controller_conn_timeout_value, 43356Sigor@sysoev.ru .timer_data = 60 * 1000, 43420Sigor@sysoev.ru }; 43520Sigor@sysoev.ru 43620Sigor@sysoev.ru 43720Sigor@sysoev.ru static void 43820Sigor@sysoev.ru nxt_controller_conn_read(nxt_task_t *task, void *obj, void *data) 43920Sigor@sysoev.ru { 44027Svbart@nginx.com size_t preread; 44127Svbart@nginx.com nxt_buf_t *b; 44227Svbart@nginx.com nxt_int_t rc; 44362Sigor@sysoev.ru nxt_conn_t *c; 44427Svbart@nginx.com nxt_controller_request_t *r; 44520Sigor@sysoev.ru 44620Sigor@sysoev.ru c = obj; 44727Svbart@nginx.com r = data; 44820Sigor@sysoev.ru 44920Sigor@sysoev.ru nxt_debug(task, "controller conn read"); 45020Sigor@sysoev.ru 45127Svbart@nginx.com nxt_queue_remove(&c->link); 45227Svbart@nginx.com nxt_queue_self(&c->link); 45327Svbart@nginx.com 45427Svbart@nginx.com b = c->read; 45527Svbart@nginx.com 45627Svbart@nginx.com rc = nxt_http_parse_request(&r->parser, &b->mem); 45727Svbart@nginx.com 45827Svbart@nginx.com if (nxt_slow_path(rc != NXT_DONE)) { 45927Svbart@nginx.com 46027Svbart@nginx.com if (rc == NXT_AGAIN) { 46127Svbart@nginx.com if (nxt_buf_mem_free_size(&b->mem) == 0) { 46227Svbart@nginx.com nxt_log(task, NXT_LOG_ERR, "too long request headers"); 46327Svbart@nginx.com nxt_controller_conn_close(task, c, r); 46427Svbart@nginx.com return; 46527Svbart@nginx.com } 46627Svbart@nginx.com 46762Sigor@sysoev.ru nxt_conn_read(task->thread->engine, c); 46827Svbart@nginx.com return; 46927Svbart@nginx.com } 47027Svbart@nginx.com 47127Svbart@nginx.com /* rc == NXT_ERROR */ 47227Svbart@nginx.com 47327Svbart@nginx.com nxt_log(task, NXT_LOG_ERR, "parsing error"); 47427Svbart@nginx.com 47527Svbart@nginx.com nxt_controller_conn_close(task, c, r); 47627Svbart@nginx.com return; 47727Svbart@nginx.com } 47827Svbart@nginx.com 47967Svbart@nginx.com rc = nxt_http_fields_process(r->parser.fields, r, task->log); 48060Svbart@nginx.com 48160Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 48260Svbart@nginx.com nxt_controller_conn_close(task, c, r); 48360Svbart@nginx.com return; 48460Svbart@nginx.com } 48560Svbart@nginx.com 48627Svbart@nginx.com preread = nxt_buf_mem_used_size(&b->mem); 48727Svbart@nginx.com 48827Svbart@nginx.com nxt_debug(task, "controller request header parsing complete, " 489107Svbart@nginx.com "body length: %uz, preread: %uz", 49027Svbart@nginx.com r->length, preread); 49127Svbart@nginx.com 49227Svbart@nginx.com if (preread >= r->length) { 493140Svbart@nginx.com nxt_controller_process_request(task, r); 49427Svbart@nginx.com return; 49527Svbart@nginx.com } 49627Svbart@nginx.com 49727Svbart@nginx.com if (r->length - preread > (size_t) nxt_buf_mem_free_size(&b->mem)) { 49827Svbart@nginx.com b = nxt_buf_mem_alloc(c->mem_pool, r->length, 0); 49927Svbart@nginx.com if (nxt_slow_path(b == NULL)) { 50027Svbart@nginx.com nxt_controller_conn_free(task, c, NULL); 50127Svbart@nginx.com return; 50227Svbart@nginx.com } 50327Svbart@nginx.com 50427Svbart@nginx.com b->mem.free = nxt_cpymem(b->mem.free, c->read->mem.pos, preread); 50527Svbart@nginx.com 50627Svbart@nginx.com c->read = b; 50727Svbart@nginx.com } 50827Svbart@nginx.com 50927Svbart@nginx.com c->read_state = &nxt_controller_conn_body_read_state; 51027Svbart@nginx.com 51162Sigor@sysoev.ru nxt_conn_read(task->thread->engine, c); 51220Sigor@sysoev.ru } 51320Sigor@sysoev.ru 51420Sigor@sysoev.ru 51520Sigor@sysoev.ru static nxt_msec_t 51662Sigor@sysoev.ru nxt_controller_conn_timeout_value(nxt_conn_t *c, uintptr_t data) 51720Sigor@sysoev.ru { 51820Sigor@sysoev.ru return (nxt_msec_t) data; 51920Sigor@sysoev.ru } 52020Sigor@sysoev.ru 52120Sigor@sysoev.ru 52220Sigor@sysoev.ru static void 52320Sigor@sysoev.ru nxt_controller_conn_read_error(nxt_task_t *task, void *obj, void *data) 52420Sigor@sysoev.ru { 52562Sigor@sysoev.ru nxt_conn_t *c; 52620Sigor@sysoev.ru 52720Sigor@sysoev.ru c = obj; 52820Sigor@sysoev.ru 52920Sigor@sysoev.ru nxt_debug(task, "controller conn read error"); 53020Sigor@sysoev.ru 53127Svbart@nginx.com nxt_controller_conn_close(task, c, data); 53220Sigor@sysoev.ru } 53320Sigor@sysoev.ru 53420Sigor@sysoev.ru 53520Sigor@sysoev.ru static void 53620Sigor@sysoev.ru nxt_controller_conn_read_timeout(nxt_task_t *task, void *obj, void *data) 53720Sigor@sysoev.ru { 53862Sigor@sysoev.ru nxt_timer_t *timer; 53962Sigor@sysoev.ru nxt_conn_t *c; 54020Sigor@sysoev.ru 54162Sigor@sysoev.ru timer = obj; 54220Sigor@sysoev.ru 54362Sigor@sysoev.ru c = nxt_read_timer_conn(timer); 54420Sigor@sysoev.ru c->socket.timedout = 1; 54520Sigor@sysoev.ru c->socket.closed = 1; 54620Sigor@sysoev.ru 54720Sigor@sysoev.ru nxt_debug(task, "controller conn read timeout"); 54820Sigor@sysoev.ru 54927Svbart@nginx.com nxt_controller_conn_close(task, c, data); 55027Svbart@nginx.com } 55127Svbart@nginx.com 55227Svbart@nginx.com 55327Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_body_read_state 55427Svbart@nginx.com nxt_aligned(64) = 55527Svbart@nginx.com { 55656Sigor@sysoev.ru .ready_handler = nxt_controller_conn_body_read, 55756Sigor@sysoev.ru .close_handler = nxt_controller_conn_close, 55856Sigor@sysoev.ru .error_handler = nxt_controller_conn_read_error, 55927Svbart@nginx.com 56056Sigor@sysoev.ru .timer_handler = nxt_controller_conn_read_timeout, 56156Sigor@sysoev.ru .timer_value = nxt_controller_conn_timeout_value, 56256Sigor@sysoev.ru .timer_data = 60 * 1000, 56356Sigor@sysoev.ru .timer_autoreset = 1, 56427Svbart@nginx.com }; 56527Svbart@nginx.com 56627Svbart@nginx.com 56727Svbart@nginx.com static void 56827Svbart@nginx.com nxt_controller_conn_body_read(nxt_task_t *task, void *obj, void *data) 56927Svbart@nginx.com { 570107Svbart@nginx.com size_t read; 571107Svbart@nginx.com nxt_buf_t *b; 572107Svbart@nginx.com nxt_conn_t *c; 573107Svbart@nginx.com nxt_controller_request_t *r; 57427Svbart@nginx.com 57527Svbart@nginx.com c = obj; 576107Svbart@nginx.com r = data; 57727Svbart@nginx.com b = c->read; 57827Svbart@nginx.com 579107Svbart@nginx.com read = nxt_buf_mem_used_size(&b->mem); 58027Svbart@nginx.com 581107Svbart@nginx.com nxt_debug(task, "controller conn body read: %uz of %uz", 582107Svbart@nginx.com read, r->length); 58327Svbart@nginx.com 584107Svbart@nginx.com if (read >= r->length) { 585140Svbart@nginx.com nxt_controller_process_request(task, r); 58627Svbart@nginx.com return; 58727Svbart@nginx.com } 58827Svbart@nginx.com 58962Sigor@sysoev.ru nxt_conn_read(task->thread->engine, c); 59027Svbart@nginx.com } 59127Svbart@nginx.com 59227Svbart@nginx.com 59327Svbart@nginx.com static const nxt_event_conn_state_t nxt_controller_conn_write_state 59427Svbart@nginx.com nxt_aligned(64) = 59527Svbart@nginx.com { 59656Sigor@sysoev.ru .ready_handler = nxt_controller_conn_write, 59756Sigor@sysoev.ru .error_handler = nxt_controller_conn_write_error, 59827Svbart@nginx.com 59956Sigor@sysoev.ru .timer_handler = nxt_controller_conn_write_timeout, 60056Sigor@sysoev.ru .timer_value = nxt_controller_conn_timeout_value, 60156Sigor@sysoev.ru .timer_data = 60 * 1000, 60256Sigor@sysoev.ru .timer_autoreset = 1, 60327Svbart@nginx.com }; 60427Svbart@nginx.com 60527Svbart@nginx.com 60627Svbart@nginx.com static void 60727Svbart@nginx.com nxt_controller_conn_write(nxt_task_t *task, void *obj, void *data) 60827Svbart@nginx.com { 60962Sigor@sysoev.ru nxt_buf_t *b; 61062Sigor@sysoev.ru nxt_conn_t *c; 61127Svbart@nginx.com 61227Svbart@nginx.com c = obj; 61327Svbart@nginx.com 61427Svbart@nginx.com nxt_debug(task, "controller conn write"); 61527Svbart@nginx.com 61627Svbart@nginx.com b = c->write; 61727Svbart@nginx.com 61827Svbart@nginx.com if (b->mem.pos != b->mem.free) { 61962Sigor@sysoev.ru nxt_conn_write(task->thread->engine, c); 62027Svbart@nginx.com return; 62127Svbart@nginx.com } 62227Svbart@nginx.com 62327Svbart@nginx.com nxt_debug(task, "controller conn write complete"); 62427Svbart@nginx.com 62527Svbart@nginx.com nxt_controller_conn_close(task, c, data); 62627Svbart@nginx.com } 62727Svbart@nginx.com 62827Svbart@nginx.com 62927Svbart@nginx.com static void 63027Svbart@nginx.com nxt_controller_conn_write_error(nxt_task_t *task, void *obj, void *data) 63127Svbart@nginx.com { 63262Sigor@sysoev.ru nxt_conn_t *c; 63327Svbart@nginx.com 63427Svbart@nginx.com c = obj; 63527Svbart@nginx.com 63627Svbart@nginx.com nxt_debug(task, "controller conn write error"); 63727Svbart@nginx.com 63827Svbart@nginx.com nxt_controller_conn_close(task, c, data); 63927Svbart@nginx.com } 64027Svbart@nginx.com 64127Svbart@nginx.com 64227Svbart@nginx.com static void 64327Svbart@nginx.com nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj, void *data) 64427Svbart@nginx.com { 64562Sigor@sysoev.ru nxt_conn_t *c; 64662Sigor@sysoev.ru nxt_timer_t *timer; 64727Svbart@nginx.com 64862Sigor@sysoev.ru timer = obj; 64927Svbart@nginx.com 65062Sigor@sysoev.ru c = nxt_write_timer_conn(timer); 65127Svbart@nginx.com c->socket.timedout = 1; 65227Svbart@nginx.com c->socket.closed = 1; 65327Svbart@nginx.com 65427Svbart@nginx.com nxt_debug(task, "controller conn write timeout"); 65527Svbart@nginx.com 65627Svbart@nginx.com nxt_controller_conn_close(task, c, data); 65720Sigor@sysoev.ru } 65820Sigor@sysoev.ru 65920Sigor@sysoev.ru 66020Sigor@sysoev.ru static const nxt_event_conn_state_t nxt_controller_conn_close_state 66120Sigor@sysoev.ru nxt_aligned(64) = 66220Sigor@sysoev.ru { 66356Sigor@sysoev.ru .ready_handler = nxt_controller_conn_free, 66420Sigor@sysoev.ru }; 66520Sigor@sysoev.ru 66620Sigor@sysoev.ru 66720Sigor@sysoev.ru static void 66820Sigor@sysoev.ru nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data) 66920Sigor@sysoev.ru { 67062Sigor@sysoev.ru nxt_conn_t *c; 67120Sigor@sysoev.ru 67220Sigor@sysoev.ru c = obj; 67320Sigor@sysoev.ru 67420Sigor@sysoev.ru nxt_debug(task, "controller conn close"); 67520Sigor@sysoev.ru 67627Svbart@nginx.com nxt_queue_remove(&c->link); 67727Svbart@nginx.com 67820Sigor@sysoev.ru c->write_state = &nxt_controller_conn_close_state; 67920Sigor@sysoev.ru 68062Sigor@sysoev.ru nxt_conn_close(task->thread->engine, c); 68120Sigor@sysoev.ru } 68220Sigor@sysoev.ru 68320Sigor@sysoev.ru 68420Sigor@sysoev.ru static void 68520Sigor@sysoev.ru nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data) 68620Sigor@sysoev.ru { 68762Sigor@sysoev.ru nxt_conn_t *c; 68820Sigor@sysoev.ru 68920Sigor@sysoev.ru c = obj; 69020Sigor@sysoev.ru 69120Sigor@sysoev.ru nxt_debug(task, "controller conn free"); 69220Sigor@sysoev.ru 69365Sigor@sysoev.ru nxt_mp_destroy(c->mem_pool); 69420Sigor@sysoev.ru 69520Sigor@sysoev.ru //nxt_free(c); 69620Sigor@sysoev.ru } 69727Svbart@nginx.com 69827Svbart@nginx.com 69927Svbart@nginx.com static nxt_int_t 70060Svbart@nginx.com nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field, 70167Svbart@nginx.com nxt_log_t *log) 70227Svbart@nginx.com { 70327Svbart@nginx.com off_t length; 70427Svbart@nginx.com nxt_controller_request_t *r; 70527Svbart@nginx.com 70627Svbart@nginx.com r = ctx; 70727Svbart@nginx.com 70860Svbart@nginx.com length = nxt_off_t_parse(field->value.start, field->value.length); 70927Svbart@nginx.com 71027Svbart@nginx.com if (nxt_fast_path(length > 0)) { 711107Svbart@nginx.com 712107Svbart@nginx.com if (nxt_slow_path(length > NXT_SIZE_T_MAX)) { 713107Svbart@nginx.com nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big"); 714107Svbart@nginx.com return NXT_ERROR; 715107Svbart@nginx.com } 71627Svbart@nginx.com 71727Svbart@nginx.com r->length = length; 71827Svbart@nginx.com return NXT_OK; 71927Svbart@nginx.com } 72027Svbart@nginx.com 72160Svbart@nginx.com nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid"); 72227Svbart@nginx.com 72327Svbart@nginx.com return NXT_ERROR; 72427Svbart@nginx.com } 72527Svbart@nginx.com 72627Svbart@nginx.com 72727Svbart@nginx.com static void 728140Svbart@nginx.com nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req) 72927Svbart@nginx.com { 73065Sigor@sysoev.ru nxt_mp_t *mp; 73151Svbart@nginx.com nxt_int_t rc; 73246Svbart@nginx.com nxt_str_t path; 733140Svbart@nginx.com nxt_conn_t *c; 73451Svbart@nginx.com nxt_buf_mem_t *mbuf; 735106Svbart@nginx.com nxt_conf_op_t *ops; 736106Svbart@nginx.com nxt_conf_value_t *value; 737208Svbart@nginx.com nxt_conf_json_error_t error; 73844Svbart@nginx.com nxt_controller_response_t resp; 73944Svbart@nginx.com 74051Svbart@nginx.com static const nxt_str_t empty_obj = nxt_string("{}"); 74151Svbart@nginx.com 742140Svbart@nginx.com c = req->conn; 743112Smax.romanov@nginx.com path = req->parser.path; 74451Svbart@nginx.com 74551Svbart@nginx.com if (path.length > 1 && path.start[path.length - 1] == '/') { 74651Svbart@nginx.com path.length--; 74751Svbart@nginx.com } 74851Svbart@nginx.com 74944Svbart@nginx.com nxt_memzero(&resp, sizeof(nxt_controller_response_t)); 75044Svbart@nginx.com 75144Svbart@nginx.com if (nxt_str_eq(&req->parser.method, "GET", 3)) { 75246Svbart@nginx.com 753106Svbart@nginx.com value = nxt_conf_get_path(nxt_controller_conf.root, &path); 75451Svbart@nginx.com 75551Svbart@nginx.com if (value == NULL) { 756208Svbart@nginx.com goto not_found; 75751Svbart@nginx.com } 75851Svbart@nginx.com 759208Svbart@nginx.com resp.status = 200; 760106Svbart@nginx.com resp.conf = value; 76146Svbart@nginx.com 762208Svbart@nginx.com nxt_controller_response(task, req, &resp); 763208Svbart@nginx.com return; 76451Svbart@nginx.com } 76551Svbart@nginx.com 76651Svbart@nginx.com if (nxt_str_eq(&req->parser.method, "PUT", 3)) { 76746Svbart@nginx.com 768238Svbart@nginx.com if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) { 769238Svbart@nginx.com nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link); 770238Svbart@nginx.com return; 771238Svbart@nginx.com } 772238Svbart@nginx.com 77365Sigor@sysoev.ru mp = nxt_mp_create(1024, 128, 256, 32); 77451Svbart@nginx.com 77551Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 776208Svbart@nginx.com goto alloc_fail; 77746Svbart@nginx.com } 77846Svbart@nginx.com 77951Svbart@nginx.com mbuf = &c->read->mem; 78051Svbart@nginx.com 781208Svbart@nginx.com nxt_memzero(&error, sizeof(nxt_conf_json_error_t)); 782208Svbart@nginx.com 783208Svbart@nginx.com value = nxt_conf_json_parse(mp, mbuf->pos, mbuf->free, &error); 78451Svbart@nginx.com 78551Svbart@nginx.com if (value == NULL) { 78665Sigor@sysoev.ru nxt_mp_destroy(mp); 787208Svbart@nginx.com 788208Svbart@nginx.com if (error.pos == NULL) { 789208Svbart@nginx.com goto alloc_fail; 790208Svbart@nginx.com } 791208Svbart@nginx.com 792208Svbart@nginx.com resp.status = 400; 793208Svbart@nginx.com resp.title = (u_char *) "Invalid JSON."; 794208Svbart@nginx.com resp.detail = error.detail; 795208Svbart@nginx.com resp.offset = error.pos - mbuf->pos; 796208Svbart@nginx.com 797208Svbart@nginx.com nxt_conf_json_position(mbuf->pos, error.pos, 798208Svbart@nginx.com &resp.line, &resp.column); 799208Svbart@nginx.com 800208Svbart@nginx.com nxt_controller_response(task, req, &resp); 801208Svbart@nginx.com return; 80251Svbart@nginx.com } 80351Svbart@nginx.com 80451Svbart@nginx.com if (path.length != 1) { 805106Svbart@nginx.com rc = nxt_conf_op_compile(c->mem_pool, &ops, 806106Svbart@nginx.com nxt_controller_conf.root, 807106Svbart@nginx.com &path, value); 80846Svbart@nginx.com 80951Svbart@nginx.com if (rc != NXT_OK) { 81051Svbart@nginx.com if (rc == NXT_DECLINED) { 811208Svbart@nginx.com goto not_found; 81251Svbart@nginx.com } 81346Svbart@nginx.com 814208Svbart@nginx.com goto alloc_fail; 81551Svbart@nginx.com } 81651Svbart@nginx.com 817106Svbart@nginx.com value = nxt_conf_clone(mp, ops, nxt_controller_conf.root); 81851Svbart@nginx.com 81951Svbart@nginx.com if (nxt_slow_path(value == NULL)) { 82065Sigor@sysoev.ru nxt_mp_destroy(mp); 821208Svbart@nginx.com goto alloc_fail; 82251Svbart@nginx.com } 82346Svbart@nginx.com } 82444Svbart@nginx.com 825116Svbart@nginx.com if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) { 826121Svbart@nginx.com nxt_mp_destroy(mp); 827208Svbart@nginx.com goto invalid_conf; 828116Svbart@nginx.com } 829116Svbart@nginx.com 830249Svbart@nginx.com rc = nxt_controller_conf_send(task, value, 831249Svbart@nginx.com nxt_controller_conf_handler, req); 832247Svbart@nginx.com 833247Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 834121Svbart@nginx.com nxt_mp_destroy(mp); 835247Svbart@nginx.com 836247Svbart@nginx.com if (rc == NXT_DECLINED) { 837247Svbart@nginx.com goto no_router; 838247Svbart@nginx.com } 839247Svbart@nginx.com 840247Svbart@nginx.com /* rc == NXT_ERROR */ 841208Svbart@nginx.com goto alloc_fail; 842121Svbart@nginx.com } 843121Svbart@nginx.com 844249Svbart@nginx.com req->conf.root = value; 845249Svbart@nginx.com req->conf.pool = mp; 846249Svbart@nginx.com 847249Svbart@nginx.com nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link); 848249Svbart@nginx.com 849140Svbart@nginx.com return; 85051Svbart@nginx.com } 85127Svbart@nginx.com 85251Svbart@nginx.com if (nxt_str_eq(&req->parser.method, "DELETE", 6)) { 85351Svbart@nginx.com 854238Svbart@nginx.com if (!nxt_queue_is_empty(&nxt_controller_waiting_requests)) { 855238Svbart@nginx.com nxt_queue_insert_tail(&nxt_controller_waiting_requests, &req->link); 856238Svbart@nginx.com return; 857238Svbart@nginx.com } 858238Svbart@nginx.com 85951Svbart@nginx.com if (path.length == 1) { 86065Sigor@sysoev.ru mp = nxt_mp_create(1024, 128, 256, 32); 86144Svbart@nginx.com 86251Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 863208Svbart@nginx.com goto alloc_fail; 86451Svbart@nginx.com } 86551Svbart@nginx.com 866106Svbart@nginx.com value = nxt_conf_json_parse_str(mp, &empty_obj); 86727Svbart@nginx.com 86844Svbart@nginx.com } else { 869106Svbart@nginx.com rc = nxt_conf_op_compile(c->mem_pool, &ops, 870106Svbart@nginx.com nxt_controller_conf.root, 871106Svbart@nginx.com &path, NULL); 87251Svbart@nginx.com 87351Svbart@nginx.com if (rc != NXT_OK) { 87451Svbart@nginx.com if (rc == NXT_DECLINED) { 875208Svbart@nginx.com goto not_found; 87651Svbart@nginx.com } 87751Svbart@nginx.com 878208Svbart@nginx.com goto alloc_fail; 87951Svbart@nginx.com } 88051Svbart@nginx.com 88165Sigor@sysoev.ru mp = nxt_mp_create(1024, 128, 256, 32); 88251Svbart@nginx.com 88351Svbart@nginx.com if (nxt_slow_path(mp == NULL)) { 884208Svbart@nginx.com goto alloc_fail; 88551Svbart@nginx.com } 88651Svbart@nginx.com 887106Svbart@nginx.com value = nxt_conf_clone(mp, ops, nxt_controller_conf.root); 88851Svbart@nginx.com } 88951Svbart@nginx.com 89051Svbart@nginx.com if (nxt_slow_path(value == NULL)) { 89165Sigor@sysoev.ru nxt_mp_destroy(mp); 892208Svbart@nginx.com goto alloc_fail; 89344Svbart@nginx.com } 89444Svbart@nginx.com 895116Svbart@nginx.com if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) { 896121Svbart@nginx.com nxt_mp_destroy(mp); 897208Svbart@nginx.com goto invalid_conf; 898116Svbart@nginx.com } 899116Svbart@nginx.com 900249Svbart@nginx.com rc = nxt_controller_conf_send(task, value, 901249Svbart@nginx.com nxt_controller_conf_handler, req); 902247Svbart@nginx.com 903247Svbart@nginx.com if (nxt_slow_path(rc != NXT_OK)) { 904121Svbart@nginx.com nxt_mp_destroy(mp); 905247Svbart@nginx.com 906247Svbart@nginx.com if (rc == NXT_DECLINED) { 907247Svbart@nginx.com goto no_router; 908247Svbart@nginx.com } 909247Svbart@nginx.com 910247Svbart@nginx.com /* rc == NXT_ERROR */ 911208Svbart@nginx.com goto alloc_fail; 912121Svbart@nginx.com } 913121Svbart@nginx.com 914249Svbart@nginx.com req->conf.root = value; 915249Svbart@nginx.com req->conf.pool = mp; 916249Svbart@nginx.com 917249Svbart@nginx.com nxt_queue_insert_head(&nxt_controller_waiting_requests, &req->link); 918249Svbart@nginx.com 919140Svbart@nginx.com return; 92051Svbart@nginx.com } 92151Svbart@nginx.com 922208Svbart@nginx.com resp.status = 405; 923208Svbart@nginx.com resp.title = (u_char *) "Invalid method."; 924208Svbart@nginx.com resp.offset = -1; 92551Svbart@nginx.com 926208Svbart@nginx.com nxt_controller_response(task, req, &resp); 927208Svbart@nginx.com return; 92851Svbart@nginx.com 929208Svbart@nginx.com not_found: 930208Svbart@nginx.com 931208Svbart@nginx.com resp.status = 404; 932208Svbart@nginx.com resp.title = (u_char *) "Value doesn't exist."; 933208Svbart@nginx.com resp.offset = -1; 934208Svbart@nginx.com 935208Svbart@nginx.com nxt_controller_response(task, req, &resp); 936208Svbart@nginx.com return; 937208Svbart@nginx.com 938208Svbart@nginx.com invalid_conf: 939208Svbart@nginx.com 940208Svbart@nginx.com resp.status = 400; 941208Svbart@nginx.com resp.title = (u_char *) "Invalid configuration."; 942208Svbart@nginx.com resp.offset = -1; 943208Svbart@nginx.com 944208Svbart@nginx.com nxt_controller_response(task, req, &resp); 945208Svbart@nginx.com return; 946247Svbart@nginx.com 947247Svbart@nginx.com alloc_fail: 948247Svbart@nginx.com 949247Svbart@nginx.com resp.status = 500; 950247Svbart@nginx.com resp.title = (u_char *) "Memory allocation failed."; 951247Svbart@nginx.com resp.offset = -1; 952247Svbart@nginx.com 953247Svbart@nginx.com nxt_controller_response(task, req, &resp); 954247Svbart@nginx.com return; 955247Svbart@nginx.com 956247Svbart@nginx.com no_router: 957247Svbart@nginx.com 958247Svbart@nginx.com resp.status = 500; 959247Svbart@nginx.com resp.title = (u_char *) "Router process isn't available."; 960247Svbart@nginx.com resp.offset = -1; 961247Svbart@nginx.com 962247Svbart@nginx.com nxt_controller_response(task, req, &resp); 963247Svbart@nginx.com return; 96427Svbart@nginx.com } 96527Svbart@nginx.com 96627Svbart@nginx.com 967193Smax.romanov@nginx.com static void 968193Smax.romanov@nginx.com nxt_controller_conf_handler(nxt_task_t *task, nxt_port_recv_msg_t *msg, 969193Smax.romanov@nginx.com void *data) 970140Svbart@nginx.com { 971238Svbart@nginx.com nxt_queue_t queue; 972140Svbart@nginx.com nxt_controller_request_t *req; 973140Svbart@nginx.com nxt_controller_response_t resp; 974140Svbart@nginx.com 975238Svbart@nginx.com req = data; 976238Svbart@nginx.com 977201Svbart@nginx.com nxt_debug(task, "controller conf ready: %*s", 978201Svbart@nginx.com nxt_buf_mem_used_size(&msg->buf->mem), msg->buf->mem.pos); 979140Svbart@nginx.com 980238Svbart@nginx.com nxt_queue_remove(&req->link); 981140Svbart@nginx.com 982238Svbart@nginx.com nxt_memzero(&resp, sizeof(nxt_controller_response_t)); 983140Svbart@nginx.com 984193Smax.romanov@nginx.com if (msg->port_msg.type == NXT_PORT_MSG_RPC_READY) { 985140Svbart@nginx.com nxt_mp_destroy(nxt_controller_conf.pool); 986140Svbart@nginx.com 987140Svbart@nginx.com nxt_controller_conf = req->conf; 988140Svbart@nginx.com 989*314Svbart@nginx.com nxt_controller_conf_store(task, req->conf.root); 990*314Svbart@nginx.com 991208Svbart@nginx.com resp.status = 200; 992208Svbart@nginx.com resp.title = (u_char *) "Reconfiguration done."; 993140Svbart@nginx.com 994140Svbart@nginx.com } else { 995140Svbart@nginx.com nxt_mp_destroy(req->conf.pool); 996140Svbart@nginx.com 997208Svbart@nginx.com resp.status = 500; 998208Svbart@nginx.com resp.title = (u_char *) "Failed to apply new configuration."; 999208Svbart@nginx.com resp.offset = -1; 1000140Svbart@nginx.com } 1001140Svbart@nginx.com 1002140Svbart@nginx.com nxt_controller_response(task, req, &resp); 1003140Svbart@nginx.com 1004238Svbart@nginx.com nxt_queue_init(&queue); 1005238Svbart@nginx.com nxt_queue_add(&queue, &nxt_controller_waiting_requests); 1006140Svbart@nginx.com 1007238Svbart@nginx.com nxt_queue_init(&nxt_controller_waiting_requests); 1008121Svbart@nginx.com 1009238Svbart@nginx.com nxt_queue_each(req, &queue, nxt_controller_request_t, link) { 1010238Svbart@nginx.com nxt_controller_process_request(task, req); 1011238Svbart@nginx.com } nxt_queue_loop; 1012121Svbart@nginx.com } 1013121Svbart@nginx.com 1014121Svbart@nginx.com 1015140Svbart@nginx.com static void 1016*314Svbart@nginx.com nxt_controller_conf_store(nxt_task_t *task, nxt_conf_value_t *conf) 1017*314Svbart@nginx.com { 1018*314Svbart@nginx.com size_t size; 1019*314Svbart@nginx.com nxt_buf_t *b; 1020*314Svbart@nginx.com nxt_port_t *main_port; 1021*314Svbart@nginx.com nxt_runtime_t *rt; 1022*314Svbart@nginx.com 1023*314Svbart@nginx.com rt = task->thread->runtime; 1024*314Svbart@nginx.com 1025*314Svbart@nginx.com main_port = rt->port_by_type[NXT_PROCESS_MAIN]; 1026*314Svbart@nginx.com 1027*314Svbart@nginx.com size = nxt_conf_json_length(conf, NULL); 1028*314Svbart@nginx.com 1029*314Svbart@nginx.com b = nxt_buf_mem_alloc(main_port->mem_pool, size, 0); 1030*314Svbart@nginx.com 1031*314