Skip to content

Commit

Permalink
Add http cache mode autogate, initialize cache control headers, repea…
Browse files Browse the repository at this point in the history
…t of #2074
  • Loading branch information
AdityaAtulTewari committed Aug 8, 2024
1 parent 9371ec7 commit 6c71226
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 5 deletions.
12 changes: 11 additions & 1 deletion src/workerd/api/http-test-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ export const cacheMode = {
await assertFetchCacheRejectsError(cacheMode);
}
} else {
for(var cacheMode of allowedCacheModes) {
{
const req = new Request('https://example.org', {cache : "no-cache"});
assert.strictEqual(req.cache, "no-cache");
}
{
const req = new Request('https://example.org', {cache : "no-store"});
assert.strictEqual(req.cache, "no-store");
}
let failureCacheModes: Array<RequestCache> =
["default", "force-cache", "only-if-cached", "reload"];
for(var cacheMode of failureCacheModes) {
await assertRequestCacheThrowsError(cacheMode,
'TypeError',
'Unsupported cache mode: ' + cacheMode);
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/api/http-test-ts.ts-wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,7 @@ const unitTests :Workerd.Config = (
)
),
],
autogates = [
"workerd-autogate-http-request-cache",
]
);
39 changes: 37 additions & 2 deletions src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <kj/parse/char.h>
#include <workerd/io/features.h>
#include <workerd/util/http-util.h>
#include <workerd/util/autogate.h>
#include <workerd/util/mimetype.h>
#include <workerd/util/stream-utils.h>
#include <workerd/util/thread-scopes.h>
Expand Down Expand Up @@ -1195,6 +1196,16 @@ void Request::shallowCopyHeadersTo(kj::HttpHeaders& out) {
}

kj::Maybe<kj::String> Request::serializeCfBlobJson(jsg::Lock& js) {
if(cacheMode != CacheMode::NONE &&
util::Autogate::isEnabled(util::AutogateKey::HTTP_REQUEST_CACHE)) {
auto clone = cf.deepClone(js);
auto obj = KJ_ASSERT_NONNULL(clone.get(js));
// Works because of the fact there are only two non-none cache modes
auto ttl = (CacheMode::NOSTORE == cacheMode) ? -1 : 0;

obj.set(js, "cacheTtl", js.num(ttl));
return clone.serialize(js);
}
return cf.serialize(js);
}

Expand All @@ -1204,7 +1215,9 @@ void RequestInitializerDict::validate(jsg::Lock& js) {
JSG_REQUIRE(FeatureFlags::get(js).getCacheOptionEnabled(), Error, kj::str(
"The 'cache' field on 'RequestInitializerDict' is not implemented."));

JSG_FAIL_REQUIRE(TypeError, kj::str("Unsupported cache mode: ", c));
JSG_REQUIRE(util::Autogate::isEnabled(util::AutogateKey::HTTP_REQUEST_CACHE), TypeError, kj::str("Unsupported cache mode: ", c));
// Validate that the cache type is valid
getCacheModeFromName(c);
}
}

Expand Down Expand Up @@ -1816,7 +1829,29 @@ jsg::Promise<jsg::Ref<Response>> fetchImplNoOutputLock(
// If the jsRequest has a CacheMode, we need to handle that here.
// Currently, the only cache mode we support is undefined, but we will soon support
// no-cache and no-store. These additional modes will be hidden behind an autogate.
KJ_ASSERT(jsRequest->getCacheMode() == Request::CacheMode::NONE);
auto headerIds = ioContext.getHeaderIds();
const auto cacheMode = jsRequest->getCacheMode();
if(util::Autogate::isEnabled(util::AutogateKey::HTTP_REQUEST_CACHE)) {
switch(cacheMode) {
case Request::CacheMode::NOSTORE:
case Request::CacheMode::NOCACHE:
if(headers.get(headerIds.cacheControl) == kj::none) {
headers.set(headerIds.cacheControl, "no-cache");
}
if(headers.get(headerIds.pragma) == kj::none) {
headers.set(headerIds.pragma, "no-cache");
}
case Request::CacheMode::NONE:
if(headers.get(headerIds.cfCacheLevel) == kj::none) {
headers.set(headerIds.cfCacheLevel, "byc");
}
break;
default:
KJ_UNREACHABLE;
}
} else {
KJ_ASSERT(cacheMode == Request::CacheMode::NONE);
}

kj::String url = uriEncodeControlChars(
urlList.back().toString(kj::Url::HTTP_PROXY_REQUEST).asBytes());
Expand Down
4 changes: 3 additions & 1 deletion src/workerd/io/io-thread-context.c++
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,15 @@ ThreadContext::HeaderIdBundle::HeaderIdBundle(kj::HttpHeaderTable::Builder& buil
contentEncoding(builder.add("Content-Encoding")),
cfCacheStatus(builder.add("CF-Cache-Status")),
cacheControl(builder.add("Cache-Control")),
pragma(builder.add("Pragma")),
cfCacheNamespace(builder.add("CF-Cache-Namespace")),
cfKvMetadata(builder.add("CF-KV-Metadata")),
cfR2ErrorHeader(builder.add("CF-R2-Error")),
cfBlobMetadataSize(builder.add("CF-R2-Metadata-Size")),
cfBlobRequest(builder.add("CF-R2-Request")),
authorization(builder.add("Authorization")),
secWebSocketProtocol(builder.add("Sec-WebSocket-Protocol")) {}
secWebSocketProtocol(builder.add("Sec-WebSocket-Protocol")),
cfCacheLevel(builder.add("Cf-Cache-Level")) {}

ThreadContext::ThreadContext(
kj::Timer& timer, kj::EntropySource& entropySource,
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/io/io-thread-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ class ThreadContext {
const kj::HttpHeaderId contentEncoding;
const kj::HttpHeaderId cfCacheStatus; // used by cache API implementation
const kj::HttpHeaderId cacheControl;
const kj::HttpHeaderId pragma;
const kj::HttpHeaderId cfCacheNamespace; // used by Cache binding implementation
const kj::HttpHeaderId cfKvMetadata; // used by KV binding implementation
const kj::HttpHeaderId cfR2ErrorHeader; // used by R2 binding implementation
const kj::HttpHeaderId cfBlobMetadataSize; // used by R2 binding implementation
const kj::HttpHeaderId cfBlobRequest; // used by R2 binding implementation
const kj::HttpHeaderId authorization; // used by R2 binding implementation
const kj::HttpHeaderId secWebSocketProtocol;
const kj::HttpHeaderId cfCacheLevel;
};

ThreadContext(
Expand Down
4 changes: 3 additions & 1 deletion src/workerd/server/server-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,9 @@ public:
}
}),
fakeDate(kj::UNIX_EPOCH),
mockNetwork(*this, {}, {}) {}
mockNetwork(*this, {}, {}) {
//workerd::util::Autogate::initAutogate({});
}

~TestServer() noexcept(false) {
for (auto& subq: subrequests) {
Expand Down
2 changes: 2 additions & 0 deletions src/workerd/util/autogate.c++
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ kj::StringPtr KJ_STRINGIFY(AutogateKey key) {
return "test-workerd"_kj;
case AutogateKey::PYODIDE_LOAD_EXTERNAL:
return "pyodide-load-external"_kj;
case workerd::util::AutogateKey::HTTP_REQUEST_CACHE:
return "http-request-cache"_kj;
case AutogateKey::NumOfKeys:
KJ_FAIL_ASSERT("NumOfKeys should not be used in getName");
}
Expand Down
1 change: 1 addition & 0 deletions src/workerd/util/autogate.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace workerd::util {
enum class AutogateKey {
TEST_WORKERD,
PYODIDE_LOAD_EXTERNAL,
HTTP_REQUEST_CACHE,
NumOfKeys // Reserved for iteration.
};

Expand Down

0 comments on commit 6c71226

Please sign in to comment.