1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Valentin V. Bartenev
5 * Copyright (C) NGINX, Inc.
6 */
7
8#include <nxt_main.h>

--- 17 unchanged lines hidden (view full) ---

26} nxt_controller_request_t;
27
28
29typedef struct {
30 nxt_uint_t status;
31 nxt_conf_value_t *conf;
32
33 u_char *title;
34 u_char *detail;
34 nxt_str_t detail;
35 ssize_t offset;
36 nxt_uint_t line;
37 nxt_uint_t column;
38} nxt_controller_response_t;
39
40
41static void nxt_controller_process_new_port_handler(nxt_task_t *task,
42 nxt_port_recv_msg_t *msg);

--- 67 unchanged lines hidden (view full) ---

110 .rpc_error = nxt_port_rpc_handler,
111};
112
113
114nxt_int_t
115nxt_controller_start(nxt_task_t *task, void *data)
116{
117 nxt_mp_t *mp;
118 nxt_int_t ret;
119 nxt_str_t *json;
120 nxt_runtime_t *rt;
121 nxt_conf_value_t *conf;
122 nxt_event_engine_t *engine;
123 nxt_conf_validation_t vldt;
124 nxt_http_fields_hash_t *hash;
125
126 rt = task->thread->runtime;
127
128 engine = task->thread->engine;
129
130 engine->mem_pool = nxt_mp_create(4096, 128, 1024, 64);
131 if (nxt_slow_path(engine->mem_pool == NULL)) {

--- 27 unchanged lines hidden (view full) ---

159 nxt_log(task, NXT_LOG_ALERT,
160 "failed to restore previous configuration: "
161 "file is corrupted or not enough memory");
162
163 nxt_mp_destroy(mp);
164 return NXT_OK;
165 }
166
165 if (nxt_slow_path(nxt_conf_validate(conf) != NXT_OK)) {
166 nxt_log(task, NXT_LOG_ALERT,
167 "failed to restore previous configuration: "
168 "invalid configuration");
167 nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
168
170 nxt_mp_destroy(mp);
171 return NXT_OK;
169 vldt.pool = nxt_mp_create(1024, 128, 256, 32);
170 if (nxt_slow_path(vldt.pool == NULL)) {
171 return NXT_ERROR;
172 }
173
174 vldt.conf = conf;
175
176 ret = nxt_conf_validate(&vldt);
177
178 if (nxt_slow_path(ret != NXT_OK)) {
179
180 if (ret == NXT_DECLINED) {
181 nxt_log(task, NXT_LOG_ALERT,
182 "the previous configuration is invalid: %V", &vldt.error);
183
184 nxt_mp_destroy(vldt.pool);
185 nxt_mp_destroy(mp);
186
187 return NXT_OK;
188 }
189
190 /* ret == NXT_ERROR */
191
192 return NXT_ERROR;
193 }
194
195 nxt_mp_destroy(vldt.pool);
196
197 nxt_controller_conf.root = conf;
198 nxt_controller_conf.pool = mp;
199
200 return NXT_OK;
201}
202
203
204static void

--- 552 unchanged lines hidden (view full) ---

757{
758 nxt_mp_t *mp;
759 nxt_int_t rc;
760 nxt_str_t path;
761 nxt_conn_t *c;
762 nxt_buf_mem_t *mbuf;
763 nxt_conf_op_t *ops;
764 nxt_conf_value_t *value;
765 nxt_conf_validation_t vldt;
766 nxt_conf_json_error_t error;
767 nxt_controller_response_t resp;
768
769 static const nxt_str_t empty_obj = nxt_string("{}");
770
771 c = req->conn;
772 path = req->parser.path;
773

--- 41 unchanged lines hidden (view full) ---

815 nxt_mp_destroy(mp);
816
817 if (error.pos == NULL) {
818 goto alloc_fail;
819 }
820
821 resp.status = 400;
822 resp.title = (u_char *) "Invalid JSON.";
800 resp.detail = error.detail;
823 resp.detail.length = nxt_strlen(error.detail);
824 resp.detail.start = error.detail;
825 resp.offset = error.pos - mbuf->pos;
826
827 nxt_conf_json_position(mbuf->pos, error.pos,
828 &resp.line, &resp.column);
829
830 nxt_controller_response(task, req, &resp);
831 return;
832 }

--- 14 unchanged lines hidden (view full) ---

847 value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
848
849 if (nxt_slow_path(value == NULL)) {
850 nxt_mp_destroy(mp);
851 goto alloc_fail;
852 }
853 }
854
831 if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
855 nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
856
857 vldt.conf = value;
858 vldt.pool = c->mem_pool;
859
860 rc = nxt_conf_validate(&vldt);
861
862 if (nxt_slow_path(rc != NXT_OK)) {
863 nxt_mp_destroy(mp);
833 goto invalid_conf;
864
865 if (rc == NXT_DECLINED) {
866 resp.detail = vldt.error;
867 goto invalid_conf;
868 }
869
870 /* rc == NXT_ERROR */
871 goto alloc_fail;
872 }
873
874 rc = nxt_controller_conf_send(task, value,
875 nxt_controller_conf_handler, req);
876
877 if (nxt_slow_path(rc != NXT_OK)) {
878 nxt_mp_destroy(mp);
879

--- 51 unchanged lines hidden (view full) ---

931 value = nxt_conf_clone(mp, ops, nxt_controller_conf.root);
932 }
933
934 if (nxt_slow_path(value == NULL)) {
935 nxt_mp_destroy(mp);
936 goto alloc_fail;
937 }
938
901 if (nxt_slow_path(nxt_conf_validate(value) != NXT_OK)) {
939 nxt_memzero(&vldt, sizeof(nxt_conf_validation_t));
940
941 vldt.conf = value;
942 vldt.pool = c->mem_pool;
943
944 rc = nxt_conf_validate(&vldt);
945
946 if (nxt_slow_path(rc != NXT_OK)) {
947 nxt_mp_destroy(mp);
903 goto invalid_conf;
948
949 if (rc == NXT_DECLINED) {
950 resp.detail = vldt.error;
951 goto invalid_conf;
952 }
953
954 /* rc == NXT_ERROR */
955 goto alloc_fail;
956 }
957
958 rc = nxt_controller_conf_send(task, value,
959 nxt_controller_conf_handler, req);
960
961 if (nxt_slow_path(rc != NXT_OK)) {
962 nxt_mp_destroy(mp);
963

--- 183 unchanged lines hidden (view full) ---

1147 break;
1148 }
1149
1150 c = req->conn;
1151 value = resp->conf;
1152
1153 if (value == NULL) {
1154 n = 1
1103 + (resp->detail != NULL)
1155 + (resp->detail.length != 0)
1156 + (resp->status >= 400 && resp->offset != -1);
1157
1158 value = nxt_conf_create_object(c->mem_pool, n);
1159
1160 if (nxt_slow_path(value == NULL)) {
1161 nxt_controller_conn_close(task, c, req);
1162 return;
1163 }

--- 5 unchanged lines hidden (view full) ---

1169 nxt_conf_set_member_string(value, &success_str, &str, 0);
1170
1171 } else {
1172 nxt_conf_set_member_string(value, &error_str, &str, 0);
1173 }
1174
1175 n = 0;
1176
1125 if (resp->detail != NULL) {
1126 str.length = nxt_strlen(resp->detail);
1127 str.start = resp->detail;
1128
1177 if (resp->detail.length != 0) {
1178 n++;
1179
1131 nxt_conf_set_member_string(value, &detail_str, &str, n);
1180 nxt_conf_set_member_string(value, &detail_str, &resp->detail, n);
1181 }
1182
1183 if (resp->status >= 400 && resp->offset != -1) {
1184 n++;
1185
1186 location = nxt_conf_create_object(c->mem_pool,
1187 resp->line != 0 ? 3 : 1);
1188

--- 98 unchanged lines hidden ---