@cryptotaxi247 / netdata-1 / commits / ec8b59006

h2o add api/v2 support (#16340)

* add api/v2 support to fix v2 dashboard * search by node_id as well

Timotej S committed Nov 7, 2023 at 08:42 UTC ec8b59006f0d6c2ec0ecad55f299cae51d99a19b
1 file changed +27 -6
web/server/h2o/http_server.c
+27 -6
@@ -17,6 +17,7 @@ static h2o_accept_ctx_t accept_ctx;
17 #define CONTENT_TEXT_UTF8 H2O_STRLIT("text/plain; charset=utf-8")
18 #define NBUF_INITIAL_SIZE_RESP (4096)
19 #define API_V1_PREFIX "/api/v1/"
20 +#define API_V2_PREFIX "/api/v2/"
21 #define HOST_SELECT_PREFIX "/host/"
22
23 #define HTTPD_CONFIG_SECTION "httpd"
@@ -136,6 +137,8 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
137 *host = rrdhost_find_by_hostname(c_host_id);
138 if (!*host)
139 *host = rrdhost_find_by_guid(c_host_id);
140 + if (!*host)
141 + *host = find_host_by_node_id(c_host_id);
142 if (!*host) {
143 req->res.status = HTTP_RESP_BAD_REQUEST;
144 req->res.reason = "Wrong host id";
@@ -173,13 +176,24 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
176 norm_path.len--;
177 }
178
176 - size_t api_loc = h2o_strstr(norm_path.base, norm_path.len, H2O_STRLIT(API_V1_PREFIX));
177 - if (api_loc == SIZE_MAX)
178 - return 1;
179 + unsigned int api_version = 2;
180 + size_t api_loc = h2o_strstr(norm_path.base, norm_path.len, H2O_STRLIT(API_V2_PREFIX));
181 + if (api_loc == SIZE_MAX) {
182 + api_version = 1;
183 + api_loc = h2o_strstr(norm_path.base, norm_path.len, H2O_STRLIT(API_V1_PREFIX));
184 + if (api_loc == SIZE_MAX)
185 + return 1;
186 + }
187 +
188 + // API_V1_PREFIX and API_V2_PREFIX are the same length
189 + // but I did this just in case someone changes the length of the prefix in future
190 + // so he will not be shot in the leg here
191 + // until then compiler will optimize this out
192 + size_t api_len = api_version == 1 ? strlen(API_V1_PREFIX) : strlen(API_V2_PREFIX);
193
194 h2o_iovec_t api_command = norm_path;
181 - api_command.base += api_loc + strlen(API_V1_PREFIX);
182 - api_command.len -= api_loc + strlen(API_V1_PREFIX);
195 + api_command.base += api_loc + api_len;
196 + api_command.len -= api_loc + api_len;
197
198 if (!api_command.len)
199 return 1;
@@ -195,10 +209,12 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
209 w.response.data = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
210 w.response.header = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
211 w.url_query_string_decoded = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
212 + w.url_as_received = buffer_create(NBUF_INITIAL_SIZE_RESP, NULL);
213 w.acl = WEB_CLIENT_ACL_DASHBOARD;
214
215 char *path_c_str = iovec_to_cstr(&api_command);
216 char *path_unescaped = url_unescape(path_c_str);
217 + buffer_strcat(w.url_as_received, iovec_to_cstr(&norm_path));
218 freez(path_c_str);
219
220 IF_HAS_URL_PARAMS(req) {
@@ -210,7 +226,11 @@ static inline int _netdata_uberhandler(h2o_req_t *req, RRDHOST **host)
226 freez(query_unescaped);
227 }
228
213 - web_client_api_request_v1(*host, &w, path_unescaped);
229 +//inline int web_client_api_request_v2(RRDHOST *host, struct web_client *w, char *url_path_endpoint) {
230 + if (api_version == 2)
231 + web_client_api_request_v2(*host, &w, path_unescaped);
232 + else
233 + web_client_api_request_v1(*host, &w, path_unescaped);
234 freez(path_unescaped);
235
236 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)
254 buffer_free(w.response.data);
255 buffer_free(w.response.header);
256 buffer_free(w.url_query_string_decoded);
257 + buffer_free(w.url_as_received);
258
259 return 0;
260 }