Skip to content

Commit

Permalink
Do not cache specific mime types.
Browse files Browse the repository at this point in the history
b/241983765

Change-Id: I64b67201d40f9b1618a3ff17fe3899151dc180ca
NOKEYCHECK=True
GitOrigin-RevId: 9ea670dd8943444ebf9996c895240d3e34a3a466
  • Loading branch information
Cobalt Team authored and andrewsavage1 committed Sep 3, 2022
1 parent 45cf9a0 commit 65bd736
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
32 changes: 31 additions & 1 deletion net/http/http_cache_transaction.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <algorithm>
#include <string>
#include <utility>
#include <vector>

#include "base/auto_reset.h"
#include "base/bind.h"
Expand Down Expand Up @@ -61,6 +62,15 @@ using CacheEntryStatus = HttpResponseInfo::CacheEntryStatus;

namespace {

#if defined(STARBOARD)
// Default allowlist based off MIME types associated with top
// resource types defined in resource_type.h.
static const char* const kMimeTypesCacheAllowlist[] = {
"text/html", "text/css", "image/gif", "image/jpeg",
"image/png", "image/svg+xml", "image/webp", "font/otf",
"font/ttf", "font/woff", "font/woff2", "text/javascript"};
#endif

constexpr TimeDelta kStaleRevalidateTimeout = TimeDelta::FromSeconds(60);

// From http://tools.ietf.org/html/draft-ietf-httpbis-p6-cache-21#section-6
Expand Down Expand Up @@ -3070,7 +3080,27 @@ int HttpCache::Transaction::WriteResponseInfoToEntry(bool truncated) {
// (even though the cert status contains the actual errors) and no SSL
// blocking page is shown. An alternative would be to reverse-map the cert
// status to a net error and replay the net error.
if ((response_.headers->HasHeaderValue("cache-control", "no-store")) ||

#if defined(STARBOARD)
// Only allow caching for specific mime types.
std::string mime_type;
response_.headers->GetMimeType(&mime_type);
// TODO(b/243727663): Empty mime types should not get cached either.
bool is_allowed_mime_type = mime_type.empty();
if (!is_allowed_mime_type) {
for (auto allowed_type : kMimeTypesCacheAllowlist) {
if (mime_type.compare(allowed_type) == 0) {
is_allowed_mime_type = true;
break;
}
}
}
#else
bool is_allowed_mime_type = true;
#endif

if (!is_allowed_mime_type ||
(response_.headers->HasHeaderValue("cache-control", "no-store")) ||
IsCertStatusError(response_.ssl_info.cert_status)) {
bool stopped = StopCachingImpl(false);
DCHECK(stopped);
Expand Down
5 changes: 5 additions & 0 deletions net/test/embedded_test_server/default_handlers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,12 @@ std::unique_ptr<HttpResponse> HandleEchoHeader(const std::string& url,

http_response->AddCustomHeader("Vary", vary);
http_response->set_content(content);
#if defined(STARBOARD)
// Cobalt does not currently support text/plain caching.
http_response->set_content_type("text/html");
#else
http_response->set_content_type("text/plain");
#endif
http_response->AddCustomHeader("Cache-Control", cache_control);
return std::move(http_response);
}
Expand Down
4 changes: 3 additions & 1 deletion third_party/web_platform_tests/cors/resources/304.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ def main(request, response):
else:
status = 200, "OK"
headers.append(("Access-Control-Allow-Origin", "*"))
headers.append(("Content-Type", "text/plain"))
# Cobalt does not support text/plain caching.
# headers.append(("Content-Type", "text/plain"))
headers.append(("Content-Type", "text/html"))
headers.append(("Cache-Control", "private, max-age=3, must-revalidate"))
headers.append(("ETag", etag))
return status, headers, "Success"
Expand Down

0 comments on commit 65bd736

Please sign in to comment.