@cryptotaxi247 / netdata-1 / commits / 982890f8e

HTTP response message (#6595)

* health_connection: http_error_pattern This commit brings an unique pattern for the Netdata webserver errors, now Netdata uses define for all web error * http_error_pattern: API v1 This PR also brings the pattern for the web_api_v1.c

thiagoftsm committed Sep 3, 2019 at 11:23 UTC 982890f8eab8ccf53d030478d7ea5ad6e3207b40
8 files changed +46 -40
web/api/badges/web_buffer_svg.c
+7 -7
@@ -789,7 +789,7 @@ void buffer_svg(BUFFER *wb, const char *label, calculated_number value, const ch
789 }
790
791 int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *url) {
792 - int ret = 400;
792 + int ret = HTTP_RESP_BAD_REQUEST;
793 buffer_flush(w->response.data);
794
795 BUFFER *dimensions = NULL;
@@ -867,7 +867,7 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
867 if(!st) {
868 buffer_no_cacheable(w->response.data);
869 buffer_svg(w->response.data, "chart not found", NAN, "", NULL, NULL, -1, scale, 0);
870 - ret = 200;
870 + ret = HTTP_RESP_OK;
871 goto cleanup;
872 }
873 st->last_accessed_time = now_realtime_sec();
@@ -878,7 +878,7 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
878 if (!rc) {
879 buffer_no_cacheable(w->response.data);
880 buffer_svg(w->response.data, "alarm not found", NAN, "", NULL, NULL, -1, scale, 0);
881 - ret = 200;
881 + ret = HTTP_RESP_OK;
882 goto cleanup;
883 }
884 }
@@ -997,13 +997,13 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
997 scale,
998 options
999 );
1000 - ret = 200;
1000 + ret = HTTP_RESP_OK;
1001 }
1002 else {
1003 time_t latest_timestamp = 0;
1004 int value_is_null = 1;
1005 calculated_number n = NAN;
1006 - ret = 500;
1006 + ret = HTTP_RESP_INTERNAL_SERVER_ERROR;
1007
1008 // if the collected value is too old, don't calculate its value
1009 if (rrdset_last_entry_t(st) >= (now_realtime_sec() - (st->update_every * st->gap_when_lost_iterations_above)))
@@ -1011,11 +1011,11 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
1011 , points, after, before, group, 0, options, NULL, &latest_timestamp, &value_is_null);
1012
1013 // if the value cannot be calculated, show empty badge
1014 - if (ret != 200) {
1014 + if (ret != HTTP_RESP_OK) {
1015 buffer_no_cacheable(w->response.data);
1016 value_is_null = 1;
1017 n = 0;
1018 - ret = 200;
1018 + ret = HTTP_RESP_OK;
1019 }
1020 else if (refresh > 0) {
1021 buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh);
web/api/exporters/allmetrics.c
+5 -5
@@ -75,12 +75,12 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
75 case ALLMETRICS_JSON:
76 w->response.data->contenttype = CT_APPLICATION_JSON;
77 rrd_stats_api_v1_charts_allmetrics_json(host, w->response.data);
78 - return 200;
78 + return HTTP_RESP_OK;
79
80 case ALLMETRICS_SHELL:
81 w->response.data->contenttype = CT_TEXT_PLAIN;
82 rrd_stats_api_v1_charts_allmetrics_shell(host, w->response.data);
83 - return 200;
83 + return HTTP_RESP_OK;
84
85 case ALLMETRICS_PROMETHEUS:
86 w->response.data->contenttype = CT_PROMETHEUS;
@@ -92,7 +92,7 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
92 , prometheus_backend_options
93 , prometheus_output_options
94 );
95 - return 200;
95 + return HTTP_RESP_OK;
96
97 case ALLMETRICS_PROMETHEUS_ALL_HOSTS:
98 w->response.data->contenttype = CT_PROMETHEUS;
@@ -104,11 +104,11 @@ inline int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client
104 , prometheus_backend_options
105 , prometheus_output_options
106 );
107 - return 200;
107 + return HTTP_RESP_OK;
108
109 default:
110 w->response.data->contenttype = CT_TEXT_PLAIN;
111 buffer_strcat(w->response.data, "Which format? '" ALLMETRICS_FORMAT_SHELL "', '" ALLMETRICS_FORMAT_PROMETHEUS "', '" ALLMETRICS_FORMAT_PROMETHEUS_ALL_HOSTS "' and '" ALLMETRICS_FORMAT_JSON "' are currently supported.");
112 - return 400;
112 + return HTTP_RESP_BAD_REQUEST;
113 }
114 }
web/api/exporters/allmetrics.h
+1
@@ -5,6 +5,7 @@
5
6 #include "web/api/formatters/rrd2json.h"
7 #include "shell/allmetrics_shell.h"
8 +#include "web/server/web_client.h"
9
10 extern int web_client_api_request_v1_allmetrics(RRDHOST *host, struct web_client *w, char *url);
11
web/api/formatters/rrd2json.c
+5 -5
@@ -72,7 +72,7 @@ int rrdset2value_api_v1(
72 RRDR *r = rrd2rrdr(st, points, after, before, group_method, group_time, options, dimensions);
73 if(!r) {
74 if(value_is_null) *value_is_null = 1;
75 - return 500;
75 + return HTTP_RESP_INTERNAL_SERVER_ERROR;
76 }
77
78 if(rrdr_rows(r) == 0) {
@@ -82,7 +82,7 @@ int rrdset2value_api_v1(
82 if(db_before) *db_before = 0;
83 if(value_is_null) *value_is_null = 1;
84
85 - return 400;
85 + return HTTP_RESP_BAD_REQUEST;
86 }
87
88 if(wb) {
@@ -99,7 +99,7 @@ int rrdset2value_api_v1(
99 *n = rrdr2value(r, i, options, value_is_null);
100
101 rrdr_free(r);
102 - return 200;
102 + return HTTP_RESP_OK;
103 }
104
105 int rrdset2anything_api_v1(
@@ -120,7 +120,7 @@ int rrdset2anything_api_v1(
120 RRDR *r = rrd2rrdr(st, points, after, before, group_method, group_time, options, dimensions?buffer_tostring(dimensions):NULL);
121 if(!r) {
122 buffer_strcat(wb, "Cannot generate output with these parameters on this chart.");
123 - return 500;
123 + return HTTP_RESP_INTERNAL_SERVER_ERROR;
124 }
125
126 if(r->result_options & RRDR_RESULT_OPTION_RELATIVE)
@@ -294,5 +294,5 @@ int rrdset2anything_api_v1(
294 }
295
296 rrdr_free(r);
297 - return 200;
297 + return HTTP_RESP_OK;
298 }
web/api/formatters/rrd2json.h
+2
@@ -16,6 +16,8 @@
16 #include "web/api/formatters/charts2json.h"
17 #include "web/api/formatters/json_wrapper.h"
18
19 +#include "web/server/web_client.h"
20 +
21 #define HOSTNAME_MAX 1024
22
23 #define API_RELATIVE_TIME_MAX (3 * 365 * 86400)
web/api/health/health_cmdapi.c
+5 -5
@@ -116,7 +116,7 @@ void health_silencers2file(BUFFER *wb) {
116 * @return It returns 200 on success and another code otherwise.
117 */
118 int web_client_api_request_v1_mgmt_health(RRDHOST *host, struct web_client *w, char *url) {
119 - int ret = 400;
119 + int ret;
120 (void) host;
121
122 BUFFER *wb = w->response.data;
@@ -131,12 +131,12 @@ int web_client_api_request_v1_mgmt_health(RRDHOST *host, struct web_client *w, c
131
132 if (!w->auth_bearer_token) {
133 buffer_strcat(wb, HEALTH_CMDAPI_MSG_AUTHERROR);
134 - ret = 403;
134 + ret = HTTP_RESP_FORBIDDEN;
135 } else {
136 debug(D_HEALTH, "HEALTH command API: Comparing secret '%s' to '%s'", w->auth_bearer_token, api_secret);
137 if (strcmp(w->auth_bearer_token, api_secret)) {
138 buffer_strcat(wb, HEALTH_CMDAPI_MSG_AUTHERROR);
139 - ret = 403;
139 + ret = HTTP_RESP_FORBIDDEN;
140 } else {
141 while (url) {
142 char *value = mystrsep(&url, "&");
@@ -190,12 +190,12 @@ int web_client_api_request_v1_mgmt_health(RRDHOST *host, struct web_client *w, c
190 if (unlikely(silencers->stype != STYPE_NONE && !silencers->all_alarms && !silencers->silencers)) {
191 buffer_strcat(wb, HEALTH_CMDAPI_MSG_NOSELECTORWARNING);
192 }
193 - ret = 200;
193 + ret = HTTP_RESP_OK;
194 }
195 }
196 w->response.data = wb;
197 buffer_no_cacheable(w->response.data);
198 - if (ret == 200 && config_changed) {
198 + if (ret == HTTP_RESP_OK && config_changed) {
199 BUFFER *jsonb = buffer_create(200);
200 health_silencers2json(jsonb);
201 health_silencers2file(jsonb);
web/api/web_api_v1.c
+18 -18
@@ -210,7 +210,7 @@ inline int web_client_api_request_v1_alarms(RRDHOST *host, struct web_client *w,
210 w->response.data->contenttype = CT_APPLICATION_JSON;
211 health_alarms2json(host, w->response.data, all);
212 buffer_no_cacheable(w->response.data);
213 - return 200;
213 + return HTTP_RESP_OK;
214 }
215
216 inline int web_client_api_request_v1_alarm_count(RRDHOST *host, struct web_client *w, char *url) {
@@ -274,11 +274,11 @@ inline int web_client_api_request_v1_alarm_log(RRDHOST *host, struct web_client
274 buffer_flush(w->response.data);
275 w->response.data->contenttype = CT_APPLICATION_JSON;
276 health_alarm_log2json(host, w->response.data, after);
277 - return 200;
277 + return HTTP_RESP_OK;
278 }
279
280 inline int web_client_api_request_single_chart(RRDHOST *host, struct web_client *w, char *url, void callback(RRDSET *st, BUFFER *buf)) {
281 - int ret = 400;
281 + int ret = HTTP_RESP_BAD_REQUEST;
282 char *chart = NULL;
283
284 buffer_flush(w->response.data);
@@ -311,14 +311,14 @@ inline int web_client_api_request_single_chart(RRDHOST *host, struct web_client
311 if(!st) {
312 buffer_strcat(w->response.data, "Chart is not found: ");
313 buffer_strcat_htmlescape(w->response.data, chart);
314 - ret = 404;
314 + ret = HTTP_RESP_NOT_FOUND;
315 goto cleanup;
316 }
317
318 w->response.data->contenttype = CT_APPLICATION_JSON;
319 st->last_accessed_time = now_realtime_sec();
320 callback(st, w->response.data);
321 - return 200;
321 + return HTTP_RESP_OK;
322
323 cleanup:
324 return ret;
@@ -334,7 +334,7 @@ inline int web_client_api_request_v1_charts(RRDHOST *host, struct web_client *w,
334 buffer_flush(w->response.data);
335 w->response.data->contenttype = CT_APPLICATION_JSON;
336 charts2json(host, w->response.data);
337 - return 200;
337 + return HTTP_RESP_OK;
338 }
339
340 inline int web_client_api_request_v1_chart(RRDHOST *host, struct web_client *w, char *url) {
@@ -354,7 +354,7 @@ void fix_google_param(char *s) {
354 inline int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, char *url) {
355 debug(D_WEB_CLIENT, "%llu: API v1 data with URL '%s'", w->id, url);
356
357 - int ret = 400;
357 + int ret = HTTP_RESP_BAD_REQUEST;
358 BUFFER *dimensions = NULL;
359
360 buffer_flush(w->response.data);
@@ -467,7 +467,7 @@ inline int web_client_api_request_v1_data(RRDHOST *host, struct web_client *w, c
467 if(!st) {
468 buffer_strcat(w->response.data, "Chart is not found: ");
469 buffer_strcat_htmlescape(w->response.data, chart);
470 - ret = 404;
470 + ret = HTTP_RESP_NOT_FOUND;
471 goto cleanup;
472 }
473 st->last_accessed_time = now_realtime_sec();
@@ -654,7 +654,7 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
654 if(unlikely(respect_web_browser_do_not_track_policy && web_client_has_donottrack(w))) {
655 buffer_flush(w->response.data);
656 buffer_sprintf(w->response.data, "Your web browser is sending 'DNT: 1' (Do Not Track). The registry requires persistent cookies on your browser to work.");
657 - return 400;
657 + return HTTP_RESP_BAD_REQUEST;
658 }
659
660 if(unlikely(action == 'H')) {
@@ -674,7 +674,7 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
674 error("Invalid registry request - access requires these parameters: machine ('%s'), url ('%s'), name ('%s')", machine_guid ? machine_guid : "UNSET", machine_url ? machine_url : "UNSET", url_name ? url_name : "UNSET");
675 buffer_flush(w->response.data);
676 buffer_strcat(w->response.data, "Invalid registry Access request.");
677 - return 400;
677 + return HTTP_RESP_BAD_REQUEST;
678 }
679
680 web_client_enable_tracking_required(w);
@@ -685,7 +685,7 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
685 error("Invalid registry request - delete requires these parameters: machine ('%s'), url ('%s'), delete_url ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", delete_url?delete_url:"UNSET");
686 buffer_flush(w->response.data);
687 buffer_strcat(w->response.data, "Invalid registry Delete request.");
688 - return 400;
688 + return HTTP_RESP_BAD_REQUEST;
689 }
690
691 web_client_enable_tracking_required(w);
@@ -696,7 +696,7 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
696 error("Invalid registry request - search requires these parameters: machine ('%s'), url ('%s'), for ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", search_machine_guid?search_machine_guid:"UNSET");
697 buffer_flush(w->response.data);
698 buffer_strcat(w->response.data, "Invalid registry Search request.");
699 - return 400;
699 + return HTTP_RESP_BAD_REQUEST;
700 }
701
702 web_client_enable_tracking_required(w);
@@ -707,7 +707,7 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
707 error("Invalid registry request - switching identity requires these parameters: machine ('%s'), url ('%s'), to ('%s')", machine_guid?machine_guid:"UNSET", machine_url?machine_url:"UNSET", to_person_guid?to_person_guid:"UNSET");
708 buffer_flush(w->response.data);
709 buffer_strcat(w->response.data, "Invalid registry Switch request.");
710 - return 400;
710 + return HTTP_RESP_BAD_REQUEST;
711 }
712
713 web_client_enable_tracking_required(w);
@@ -719,7 +719,7 @@ inline int web_client_api_request_v1_registry(RRDHOST *host, struct web_client *
719 default:
720 buffer_flush(w->response.data);
721 buffer_strcat(w->response.data, "Invalid registry request - you need to set an action: hello, access, delete, search");
722 - return 400;
722 + return HTTP_RESP_BAD_REQUEST;
723 }
724 }
725
@@ -763,7 +763,7 @@ static inline void web_client_api_request_v1_info_mirrored_hosts(BUFFER *wb) {
763
764 inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, char *url) {
765 (void)url;
766 - if (!netdata_ready) return 503;
766 + if (!netdata_ready) return HTTP_RESP_BACKEND_FETCH_FAILED;
767
768 BUFFER *wb = w->response.data;
769 buffer_flush(wb);
@@ -801,7 +801,7 @@ inline int web_client_api_request_v1_info(RRDHOST *host, struct web_client *w, c
801
802 buffer_strcat(wb, "}");
803 buffer_no_cacheable(wb);
804 - return 200;
804 + return HTTP_RESP_OK;
805 }
806
807 static struct api_command {
@@ -860,11 +860,11 @@ inline int web_client_api_request_v1(RRDHOST *host, struct web_client *w, char *
860 buffer_flush(w->response.data);
861 buffer_strcat(w->response.data, "Unsupported v1 API command: ");
862 buffer_strcat_htmlescape(w->response.data, url);
863 - return 404;
863 + return HTTP_RESP_NOT_FOUND;
864 }
865 else {
866 buffer_flush(w->response.data);
867 buffer_sprintf(w->response.data, "Which API v1 command?");
868 - return 400;
868 + return HTTP_RESP_BAD_REQUEST;
869 }
870 }
web/server/web_client.h
+3
@@ -25,6 +25,9 @@ extern int web_enable_gzip,
25 #define HTTP_RESP_NOT_FOUND 404
26 #define HTTP_RESP_PRECOND_FAIL 412
27
28 +// HTTP_CODES 5XX Server Errors
29 +#define HTTP_RESP_INTERNAL_SERVER_ERROR 500
30 +#define HTTP_RESP_BACKEND_FETCH_FAILED 503
31
32 extern int respect_web_browser_do_not_track_policy;
33 extern char *web_x_frame_options;