0
0
Fork 0
mirror of https://github.com/netdata/netdata.git synced 2025-04-13 09:11:50 +00:00

h2o add api/v2 support ()

* add api/v2 support to fix v2 dashboard
* search by node_id as well
This commit is contained in:
Timotej S 2023-11-07 08:42:54 +01:00 committed by GitHub
parent 240cf98375
commit ec8b59006f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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;
}