summaryrefslogtreecommitdiffstats
path: root/web
diff options
context:
space:
mode:
authorTimotej S <6674623+underhood@users.noreply.github.com>2023-11-07 08:42:54 +0100
committerGitHub <noreply@github.com>2023-11-07 08:42:54 +0100
commitec8b59006f0d6c2ec0ecad55f299cae51d99a19b (patch)
tree8a91ba86a36d16d9855dc68fc3e6dc24fdc46a25 /web
parent240cf98375d7ed2102b52bce43c386b5471ea1a8 (diff)
h2o add api/v2 support (#16340)
* add api/v2 support to fix v2 dashboard * search by node_id as well
Diffstat (limited to 'web')
-rw-r--r--web/server/h2o/http_server.c33
1 files changed, 27 insertions, 6 deletions
diff --git a/web/server/h2o/http_server.c b/web/server/h2o/http_server.c
index 8c8c698bbc..6980db2737 100644
--- a/web/server/h2o/http_server.c
+++ b/web/server/h2o/http_server.c
@@ -17,6 +17,7 @@ static h2o_accept_ctx_t accept_ctx;
#define CONTENT_TEXT_UTF8 H2O_STRLIT("text/plain; charset=utf-8")
#define NBUF_INITIAL_SIZE_RESP (4096)
#define API_V1_PREFIX "/api/v1/"
+#define API_V2_PREFIX "/api/v2/"
#define HOST_SELECT_PREFIX "/host/"
#define HTTPD_CONFIG_SECTION "httpd"
@@ -136,6 +137,8 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
*host = rrdhost_find_by_hostname(c_host_id);
if (!*host)
*host = rrdhost_find_by_guid(c_host_id);
+ if (!*host)
+ *host = find_host_by_node_id(c_host_id);
if (!*host) {
req->res.status = HTTP_RESP_BAD_REQUEST;
req->res.reason = "Wrong host id";
@@ -173,13 +176,24 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
norm_path.len--;
}
- size_t api_loc = h2o_strstr(norm_path.base, norm_path.len, H2O_STRLIT(API_V1_PREFIX));
- if (api_loc == SIZE_MAX)
- return 1;
+ unsigned int api_version = 2;
+ size_t api_loc = h2o_strstr(norm_path.base, norm_path.len, H2O_STRLIT(API_V2_PREFIX));
+ if (api_loc == SIZE_MAX) {
+ api_version = 1;
+ api_loc = h2o_strstr(norm_path.base, norm_path.len, H2O_STRLIT(API_V1_PREFIX));
+ if (api_loc == SIZE_MAX)
+ return 1;
+ }
+
+ // API_V1_PREFIX and API_V2_PREFIX are the same length
+ // but I did this just in case someone changes the length of the prefix in future
+ // so he will not be shot in the leg here
+ // until then compiler will optimize this out
+ size_t api_len = api_version == 1 ? strlen(API_V1_PREFIX) : strlen(API_V2_PREFIX);
h2o_iovec_t api_command = norm_path;
- api_command.base += api_loc + strlen(API_V1_PREFIX);
- api_command.len -= api_loc + strlen(API_V1_PREFIX);
+ api_command.base += api_loc + api_len;
+ api_command.len -= api_loc + api_len;
if (!api_command.len)
return 1;
@@ -195,10 +209,12 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
w.response.data = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
w.response.header = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
w.url_query_string_decoded = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
+ w.url_as_received = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
w.acl = WEB_CLIENT_ACL_DASHBOARD;
char *path_c_str = iovec_to_cstr(&api_command);
char *path_unescaped = url_unescape(path_c_str);
+ buffer_strcat(w.url_as_received, iovec_to_cstr(&norm_path));
freez(path_c_str);
IF_HAS_URL_PARAMS(req) {
@@ -210,7 +226,11 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
freez(query_unescaped);
}
- web_client_api_request_v1(*host, &w, path_unescaped);
+//inline int web_client_api_request_v2(RRDHOST *host, struct web_client *w, char *url_path_endpoint) {
+ if (api_version == 2)
+ web_client_api_request_v2(*host, &w, path_unescaped);
+ else
+ web_client_api_request_v1(*host, &w, path_unescaped);
freez(path_unescaped);
h2o_iovec_t body = buffer_to_h2o_iovec(w.response.data);
@@ -234,6 +254,7 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
buffer_free(w.response.data);
buffer_free(w.response.header);
buffer_free(w.url_query_string_decoded);
+ buffer_free(w.url_as_received);
return 0;
}