Skip to content

Commit 240f50b

Browse files
petervostefwalter
authored andcommitted
ws: Set package cache headers
When a file is served from packages use a private shortish cache that vary's on cookie, unless the file was requested by hash, in which case we set a long public cache. Closes cockpit-project#4064 Reviewed-by: Stef Walter <[email protected]>
1 parent d7b912f commit 240f50b

File tree

3 files changed

+18
-20
lines changed

3 files changed

+18
-20
lines changed

src/bridge/cockpitpackages.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -640,18 +640,6 @@ handle_package_checksum (CockpitWebServer *server,
640640
return TRUE;
641641
}
642642

643-
static void
644-
add_cache_header (GHashTable *headers,
645-
CockpitPackages *packages,
646-
GHashTable *out_headers)
647-
{
648-
const gchar *pragma;
649-
650-
pragma = g_hash_table_lookup (headers, "Pragma");
651-
if (packages->checksum && (!pragma || !strstr (pragma, "no-cache")))
652-
g_hash_table_insert (out_headers, g_strdup ("Cache-Control"), g_strdup ("max-age=31556926, public"));
653-
}
654-
655643
static gboolean
656644
handle_package_manifests_js (CockpitWebServer *server,
657645
const gchar *path,
@@ -669,7 +657,6 @@ handle_package_manifests_js (CockpitWebServer *server,
669657
suffix = g_bytes_new_static (");", 2);
670658

671659
out_headers = cockpit_web_server_new_table ();
672-
add_cache_header (headers, packages, out_headers);
673660

674661
cockpit_web_response_content (response, out_headers, prefix, content, suffix, NULL);
675662

@@ -691,7 +678,6 @@ handle_package_manifests_json (CockpitWebServer *server,
691678
GBytes *content;
692679

693680
out_headers = cockpit_web_server_new_table ();
694-
add_cache_header (headers, packages, out_headers);
695681

696682
content = cockpit_json_write_bytes (packages->json);
697683

@@ -738,8 +724,6 @@ handle_packages (CockpitWebServer *server,
738724
goto out;
739725
}
740726

741-
add_cache_header (headers, packages, out_headers);
742-
743727
bytes = cockpit_web_response_negotiation (filename, package->paths, &chosen, &error);
744728
if (error)
745729
{

src/ws/cockpitchannelresponse.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -485,7 +485,9 @@ cockpit_channel_response_serve (CockpitWebService *service,
485485
{
486486
CockpitChannelResponse *chesp = NULL;
487487
CockpitTransport *transport = NULL;
488+
CockpitCacheType cache_type = COCKPIT_WEB_RESPONSE_CACHE_PRIVATE;
488489
const gchar *host = NULL;
490+
const gchar *pragma;
489491
gchar *quoted_etag = NULL;
490492
GHashTable *out_headers = NULL;
491493
gchar *val = NULL;
@@ -514,9 +516,12 @@ cockpit_channel_response_serve (CockpitWebService *service,
514516
else if (where[0] == '$')
515517
{
516518
quoted_etag = g_strdup_printf ("\"%s\"", where);
519+
cache_type = COCKPIT_WEB_RESPONSE_CACHE_FOREVER;
520+
pragma = g_hash_table_lookup (in_headers, "Pragma");
517521

518-
if (g_strcmp0 (g_hash_table_lookup (in_headers, "If-None-Match"), where) == 0 ||
519-
g_strcmp0 (g_hash_table_lookup (in_headers, "If-None-Match"), quoted_etag) == 0)
522+
if ((!pragma || !strstr (pragma, "no-cache")) &&
523+
(g_strcmp0 (g_hash_table_lookup (in_headers, "If-None-Match"), where) == 0 ||
524+
g_strcmp0 (g_hash_table_lookup (in_headers, "If-None-Match"), quoted_etag) == 0))
520525
{
521526
cockpit_web_response_headers (response, 304, "Not Modified", 0, "ETag", quoted_etag, NULL);
522527
cockpit_web_response_complete (response);
@@ -540,6 +545,7 @@ cockpit_channel_response_serve (CockpitWebService *service,
540545
goto out;
541546
}
542547

548+
cockpit_web_response_set_cache_type (response, cache_type);
543549
object = cockpit_transport_build_json ("command", "open",
544550
"payload", "http-stream1",
545551
"internal", "packages",

src/ws/test-channelresponse.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,8 @@ test_resource_simple (TestResourceCase *tc,
151151
"Content-Security-Policy: default-src 'self'; connect-src 'self' ws: wss:\r\n"
152152
"Content-Type: text/html\r\n"
153153
"Transfer-Encoding: chunked\r\n"
154+
"Cache-Control: max-age=86400, private\r\n"
155+
"Vary: Cookie\r\n"
154156
"\r\n"
155157
"52\r\n"
156158
"<html>\n"
@@ -331,8 +333,8 @@ test_resource_checksum (TestResourceCase *tc,
331333
cockpit_assert_bytes_eq (bytes,
332334
"HTTP/1.1 200 OK\r\n"
333335
"ETag: \"$386257ed81a663cdd7ee12633056dee18d60ddca\"\r\n"
334-
"Cache-Control: max-age=31556926, public\r\n"
335336
"Transfer-Encoding: chunked\r\n"
337+
"Cache-Control: max-age=31556926, public\r\n"
336338
"\r\n"
337339
"32\r\n"
338340
"These are the contents of file.ext\nOh marmalaaade\n"
@@ -518,6 +520,8 @@ test_resource_language_suffix (TestResourceCase *tc,
518520
"Content-Security-Policy: default-src 'self'; connect-src 'self' ws: wss:\r\n"
519521
"Content-Type: text/html\r\n"
520522
"Transfer-Encoding: chunked\r\n"
523+
"Cache-Control: max-age=86400, private\r\n"
524+
"Vary: Cookie\r\n"
521525
"\r\n"
522526
"62\r\n"
523527
"<html>\n"
@@ -557,6 +561,8 @@ test_resource_language_fallback (TestResourceCase *tc,
557561
"Content-Security-Policy: default-src 'self'; connect-src 'self' ws: wss:\r\n"
558562
"Content-Type: text/html\r\n"
559563
"Transfer-Encoding: chunked\r\n"
564+
"Cache-Control: max-age=86400, private\r\n"
565+
"Vary: Cookie\r\n"
560566
"\r\n"
561567
"52\r\n"
562568
"<html>\n"
@@ -595,11 +601,13 @@ test_resource_gzip_encoding (TestResourceCase *tc,
595601
"Content-Encoding: gzip\r\n"
596602
"Content-Type: text/plain\r\n"
597603
"Transfer-Encoding: chunked\r\n"
604+
"Cache-Control: max-age=86400, private\r\n"
605+
"Vary: Cookie\r\n"
598606
"\r\n"
599607
"34\r\n"
600608
"\x1F\x8B\x08\x08N1\x03U\x00\x03test-file.txt\x00sT(\xCEM\xCC\xC9Q(I-"
601609
".QH\xCB\xCCI\xE5\x02\x00>PjG\x12\x00\x00\x00\x0D\x0A" "0\x0D\x0A\x0D\x0A",
602-
160);
610+
213);
603611
g_bytes_unref (bytes);
604612
g_object_unref (response);
605613
}

0 commit comments

Comments
 (0)