Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Request to support cache option #2074

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 47 additions & 4 deletions src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,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 @@ -1163,6 +1164,28 @@ void Request::shallowCopyHeadersTo(kj::HttpHeaders& out) {
}

kj::Maybe<kj::String> Request::serializeCfBlobJson(jsg::Lock& js) {
// When the HTTP_REQUEST_CACHE autogate is enabled and the CacheMode
// is anything but CacheMode::NONE, we'll need to possibly modify the
// request's cf property to include the appropriate cache mode to pass
// down.
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 = 0;
switch (cacheMode) {
case CacheMode::NOSTORE:
ttl = -1;
break;
case CacheMode::NOCACHE:
ttl = 0;
break;
case CacheMode::NONE:
KJ_UNREACHABLE;
}
obj.set(js, "cacheTtl", js.num(ttl));
return clone.serialize(js);
}
return cf.serialize(js);
}

Expand Down Expand Up @@ -1775,10 +1798,30 @@ jsg::Promise<jsg::Ref<Response>> fetchImplNoOutputLock(
// If the jsRequest has a CacheMode, we need to handle that here.
// Currently, the ony 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)))));
if (util::Autogate::isEnabled(util::AutogateKey::HTTP_REQUEST_CACHE)) {
auto cacheMode = jsRequest->getCacheMode();
if (cacheMode != Request::CacheMode::NONE) {
if (headers.get(ioContext.getHeaderIds().cacheControl) == kj::none) {
headers.set(ioContext.getHeaderIds().cacheControl, "no-cache"_kj);
}
if (headers.get(ioContext.getHeaderIds().pragma) == kj::none) {
headers.set(ioContext.getHeaderIds().pragma, "no-cache"_kj);
}
}
if (cacheMode == Request::CacheMode::NOSTORE &&
headers.get(ioContext.getHeaderIds().cfCacheLevel) == kj::none) {
// TODO(now): Validate that this is the correct way to specify the Cf-Cache-Level
// to "byc" to tell the backend to ignore the cache for this requests.
headers.set(ioContext.getHeaderIds().cfCacheLevel, "byc"_kj);
}
} else {
// TODO(soon): Remove this branch once the HTTP_REQUEST_CACHE autogate is
// permanently enabled.
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
1 change: 1 addition & 0 deletions src/workerd/io/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ wd_cc_library(
"//src/workerd/api:analytics-engine_capnp",
"//src/workerd/api:r2-api_capnp",
"//src/workerd/jsg",
"//src/workerd/util:autogate",
"//src/workerd/util:duration-exceeded-logger",
"//src/workerd/util:sqlite",
"@capnp-cpp//src/capnp:capnp-rpc",
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
5 changes: 4 additions & 1 deletion src/workerd/server/server-test.c++
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include "server.h"
#include <kj/test.h>
#include <workerd/util/autogate.h>
#include <workerd/util/capnp-mock.h>
#include <workerd/jsg/setup.h>
#include <kj/async-queue.h>
Expand Down Expand Up @@ -317,7 +318,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 @@ -13,6 +13,8 @@ kj::StringPtr KJ_STRINGIFY(AutogateKey key) {
switch (key) {
case AutogateKey::TEST_WORKERD:
return "test-workerd"_kj;
case 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
Loading