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 Jul 22, 2024
1 parent d46550a commit dd748ad
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/workerd/api/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -232,10 +232,10 @@ export const cacheMode = {
// TODO(soon): The no-store and no-cache values will be supported
// soon, at which time this test will need to be updated.
await assert.rejects((async () => {
await fetch('http://example.org', { cache: 'no-store' });
await fetch('http://example.org', { cache: 'no-transform' });
})(), {
name: 'TypeError',
message: 'Unsupported cache mode: no-store',
message: 'Unsupported cache mode: no-transform',
});
}
};
39 changes: 35 additions & 4 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 @@ -1198,6 +1199,15 @@ 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));
auto ttl = (CacheMode::NOSTORE == cacheMode) ? -1 : 0;
// Works because of the fact there are only two non-none cache modes
obj.set(js, "cacheTtl", js.num(ttl));
return clone.serialize(js);
}
return cf.serialize(js);
}

Expand Down Expand Up @@ -1810,10 +1820,31 @@ 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.
if (jsRequest->getCacheMode() != Request::CacheMode::NONE) {
return js.rejectedPromise<jsg::Ref<Response>>(
js.typeError(kj::str("Unsupported cache mode: ",
KJ_ASSERT_NONNULL(jsRequest->getCache(js)))));
auto headerIds = ioContext.getHeaderIds();
if(util::Autogate::isEnabled(util::AutogateKey::HTTP_REQUEST_CACHE)) {
switch(jsRequest->getCacheMode()) {
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 {
if(jsRequest->getCacheMode() != Request::CacheMode::NONE) {
return js.rejectedPromise<jsg::Ref<Response>>(
js.typeError(kj::str("Unsupported cache mode: ",
KJ_ASSERT_NONNULL(jsRequest->getCache(js)))));
}
}

kj::String url = uriEncodeControlChars(
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 @@ -15,6 +15,8 @@ kj::StringPtr KJ_STRINGIFY(AutogateKey key) {
switch (key) {
case AutogateKey::TEST_WORKERD:
return "test-workerd"_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 @@ -13,6 +13,7 @@ namespace workerd::util {
// Workerd-specific list of autogate keys (can also be used in internal repo).
enum class AutogateKey {
TEST_WORKERD,
HTTP_REQUEST_CACHE,
NumOfKeys // Reserved for iteration.
};

Expand Down

0 comments on commit dd748ad

Please sign in to comment.