fix expiration dates for API responses (#15546)
Costa Tsaousis committed
Jul 26, 2023 at 16:10 UTC
6daedef25bff12f39d46c582ea1010ebcac95329
10 files changed
+26
-9
database/contexts/query_target.c
+1
-1
@@ -1052,7 +1052,7 @@ QUERY_TARGET *query_target_create(QUERY_TARGET_REQUEST *qtr) {
1052
if(query_target_has_percentage_of_group(qt))
1053
qt->window.options &= ~RRDR_OPTION_PERCENTAGE;
1054
1055
- rrdr_relative_window_to_absolute(&qt->window.after, &qt->window.before, &qt->window.now);
1055
+ qt->internal.relative = rrdr_relative_window_to_absolute(&qt->window.after, &qt->window.before, &qt->window.now);
1056
1057
// prepare our local variables - we need these across all these functions
1058
QUERY_TARGET_LOCALS qtl = {
database/contexts/rrdcontext.h
+1
@@ -409,6 +409,7 @@ typedef struct query_target {
409
struct {
410
SPINLOCK spinlock;
411
bool used; // when true, this query is currently being used
412
+ bool relative; // when true, this query uses relative timestamps
413
size_t queries; // how many query we have done so far with this QUERY_TARGET - not related to database queries
414
struct query_target *prev;
415
struct query_target *next;
libnetdata/buffer/buffer.c
+3
@@ -15,6 +15,7 @@ void buffer_reset(BUFFER *wb) {
15
wb->options = 0;
16
wb->date = 0;
17
wb->expires = 0;
18
+ buffer_no_cacheable(wb);
19
20
buffer_overflow_check(wb);
21
}
@@ -254,6 +255,7 @@ BUFFER *buffer_create(size_t size, size_t *statistics)
255
b->size = size;
256
b->content_type = CT_TEXT_PLAIN;
257
b->statistics = statistics;
258
+ buffer_no_cacheable(b);
259
buffer_overflow_init(b);
260
buffer_overflow_check(b);
261
@@ -317,6 +319,7 @@ void buffer_json_initialize(BUFFER *wb, const char *key_quote, const char *value
319
buffer_fast_strcat(wb, "{", 1);
320
321
wb->content_type = CT_APPLICATION_JSON;
322
+ buffer_no_cacheable(wb);
323
}
324
325
void buffer_json_finalize(BUFFER *wb) {
web/api/badges/web_buffer_svg.c
+3
-1
@@ -1057,8 +1057,10 @@ int web_client_api_request_v1_badge(RRDHOST *host, struct web_client *w, char *u
1057
buffer_sprintf(w->response.header, "Refresh: %d\r\n", refresh);
1058
w->response.data->date = now_realtime_sec();
1059
w->response.data->expires = w->response.data->date + refresh;
1060
+ buffer_cacheable(w->response.data);
1061
}
1061
- else buffer_no_cacheable(w->response.data);
1062
+ else
1063
+ buffer_no_cacheable(w->response.data);
1064
1065
if(!value_color) {
1066
switch(rc->status) {
web/api/queries/weights.c
+2
@@ -1808,6 +1808,8 @@ int web_api_v12_weights(BUFFER *wb, QUERY_WEIGHTS_REQUEST *qwr) {
1808
1809
if(!rrdr_relative_window_to_absolute(&qwr->after, &qwr->before, NULL))
1810
buffer_no_cacheable(wb);
1811
+ else
1812
+ buffer_cacheable(wb);
1813
1814
if (qwr->before <= qwr->after) {
1815
resp = HTTP_RESP_BAD_REQUEST;
web/api/web_api.c
+2
@@ -32,6 +32,8 @@ static bool web_client_check_acl_and_bearer(struct web_client *w, WEB_CLIENT_ACL
32
}
33
34
int web_client_api_request_vX(RRDHOST *host, struct web_client *w, char *url_path_endpoint, struct web_api_command *api_commands) {
35
+ buffer_no_cacheable(w->response.data);
36
+
37
if(unlikely(!url_path_endpoint || !*url_path_endpoint)) {
38
buffer_flush(w->response.data);
39
buffer_sprintf(w->response.data, "Which API command?");
web/api/web_api_v1.c
+5
@@ -883,6 +883,11 @@ static inline int web_client_api_request_v1_data(RRDHOST *host, struct web_clien
883
else if(format == DATASOURCE_JSONP)
884
buffer_strcat(w->response.data, ");");
885
886
+ if(qt->internal.relative)
887
+ buffer_no_cacheable(w->response.data);
888
+ else
889
+ buffer_cacheable(w->response.data);
890
+
891
cleanup:
892
query_target_release(qt);
893
onewayalloc_destroy(owa);
web/api/web_api_v2.c
+5
@@ -629,6 +629,11 @@ static int web_client_api_request_v2_data(RRDHOST *host __maybe_unused, struct w
629
else if(format == DATASOURCE_JSONP)
630
buffer_strcat(w->response.data, ");");
631
632
+ if(qt->internal.relative)
633
+ buffer_no_cacheable(w->response.data);
634
+ else
635
+ buffer_cacheable(w->response.data);
636
+
637
cleanup:
638
query_target_release(qt);
639
onewayalloc_destroy(owa);
web/rtc/webrtc.c
+1
-1
@@ -304,7 +304,7 @@ static void webrtc_execute_api_request(WEBRTC_DC *chan, const char *request, siz
304
web_client_timeout_checkpoint_set(w, 0);
305
web_client_decode_path_and_query_string(w, path);
306
path = (char *)buffer_tostring(w->url_path_decoded);
307
- w->response.code = (short)web_client_api_request_with_node_selection(localhost, w, path);
307
+ w->response.code = (short)web_client_api_request_with_node_selection(rrdb.localhost, w, path);
308
web_client_timeout_checkpoint_response_ready(w, NULL);
309
310
size_t sent_bytes = 0;
web/server/web_client.c
+3
-6
@@ -1274,12 +1274,9 @@ void web_client_build_http_header(struct web_client *w) {
1274
w->response.data->date = now_realtime_sec();
1275
1276
// set a proper expiration date, if not already set
1277
- if(unlikely(!w->response.data->expires)) {
1278
- if(w->response.data->options & WB_CONTENT_NO_CACHEABLE)
1279
- w->response.data->expires = w->response.data->date + rrdb.localhost->update_every;
1280
- else
1281
- w->response.data->expires = w->response.data->date + 86400;
1282
- }
1277
+ if(unlikely(!w->response.data->expires))
1278
+ w->response.data->expires = w->response.data->date +
1279
+ ((w->response.data->options & WB_CONTENT_NO_CACHEABLE) ? 0 : 86400);
1280
1281
// prepare the HTTP response header
1282
netdata_log_debug(D_WEB_CLIENT, "%llu: Generating HTTP header with response %d.", w->id, w->response.code);