diff options
author | Valentin Bartenev <vbart@nginx.com> | 2017-05-18 20:40:19 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2017-05-18 20:40:19 +0300 |
commit | c7be5bd6ae38bdc7d29ee0b3170e5eb828e92d2d (patch) | |
tree | 97cbf3d6a4ced6b0ab14e5336292d97178bf78b4 | |
parent | dc95b2f3de76df03b64bab9bbc5e6a9dc7015550 (diff) | |
download | unit-c7be5bd6ae38bdc7d29ee0b3170e5eb828e92d2d.tar.gz unit-c7be5bd6ae38bdc7d29ee0b3170e5eb828e92d2d.tar.bz2 |
Controller: partial retrieving of configuration.
-rw-r--r-- | src/nxt_conf.h | 2 | ||||
-rw-r--r-- | src/nxt_conf_json.c | 40 | ||||
-rw-r--r-- | src/nxt_controller.c | 25 |
3 files changed, 61 insertions, 6 deletions
diff --git a/src/nxt_conf.h b/src/nxt_conf.h index 4d427bce..cf073e3a 100644 --- a/src/nxt_conf.h +++ b/src/nxt_conf.h @@ -18,6 +18,8 @@ typedef struct { } nxt_conf_json_pretty_t; +nxt_conf_json_value_t *nxt_conf_json_value_get(nxt_conf_json_value_t *value, + nxt_str_t *path); nxt_conf_json_value_t *nxt_conf_json_parse(u_char *pos, size_t length, nxt_mem_pool_t *pool); uintptr_t nxt_conf_json_print_value(u_char *pos, nxt_conf_json_value_t *value, diff --git a/src/nxt_conf_json.c b/src/nxt_conf_json.c index 063fa988..0a9db0b3 100644 --- a/src/nxt_conf_json.c +++ b/src/nxt_conf_json.c @@ -50,10 +50,8 @@ static nxt_int_t nxt_conf_json_object_hash_test(nxt_lvlhsh_query_t *lhq, void *data); static nxt_int_t nxt_conf_json_object_member_add(nxt_lvlhsh_t *lvlhsh, nxt_conf_json_obj_member_t *member, nxt_mem_pool_t *pool); -#if 0 static nxt_conf_json_value_t *nxt_conf_json_object_member_get( nxt_lvlhsh_t *lvlhsh, u_char *name, size_t length); -#endif static u_char *nxt_conf_json_skip_space(u_char *pos, u_char *end); @@ -159,7 +157,6 @@ nxt_conf_json_object_member_add(nxt_lvlhsh_t *lvlhsh, } -#if 0 static nxt_conf_json_value_t * nxt_conf_json_object_member_get(nxt_lvlhsh_t *lvlhsh, u_char *name, size_t length) @@ -179,7 +176,42 @@ nxt_conf_json_object_member_get(nxt_lvlhsh_t *lvlhsh, u_char *name, return NULL; } -#endif + + +nxt_conf_json_value_t * +nxt_conf_json_value_get(nxt_conf_json_value_t *value, nxt_str_t *path) +{ + u_char *p, *start, *end; + + p = path->start; + end = p + path->length; + + if (p != end && end[-1] == '/') { + end--; + } + + while (p != end) { + start = p + 1; + p = start; + + while (p != end && *p != '/') { + p++; + } + + if (value->type != NXT_CONF_JSON_OBJECT) { + return NULL; + } + + value = nxt_conf_json_object_member_get(value->u.object, start, + p - start); + + if (value == NULL) { + return NULL; + } + } + + return value; +} nxt_conf_json_value_t * diff --git a/src/nxt_controller.c b/src/nxt_controller.c index f02b1747..f70b481c 100644 --- a/src/nxt_controller.c +++ b/src/nxt_controller.c @@ -554,13 +554,34 @@ static void nxt_controller_process_request(nxt_task_t *task, nxt_event_conn_t *c, nxt_controller_request_t *req) { + nxt_str_t path; + nxt_conf_json_value_t *value; nxt_controller_response_t resp; nxt_memzero(&resp, sizeof(nxt_controller_response_t)); if (nxt_str_eq(&req->parser.method, "GET", 3)) { - nxt_str_set(&resp.status_line, "200 OK"); - resp.json_value = nxt_controller_conf.root; + + path.start = req->parser.target_start; + + if (req->parser.args_start != NULL) { + path.length = req->parser.args_start - path.start; + + } else { + path.length = req->parser.target_end - path.start; + } + + value = nxt_conf_json_value_get(nxt_controller_conf.root, &path); + + if (value != NULL) { + nxt_str_set(&resp.status_line, "200 OK"); + resp.json_value = value; + + } else { + nxt_str_set(&resp.status_line, "404 Not Found"); + nxt_str_set(&resp.json_string, + "{ \"error\": \"Requested value doesn't exist\" }"); + } } else if (nxt_str_eq(&req->parser.method, "PUT", 3)) { |