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 Sep 3, 2024
1 parent 50fa4e7 commit e51a319
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 17 deletions.
20 changes: 17 additions & 3 deletions src/workerd/api/http-test-ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async function assertFetchCacheRejectsError(

export const cacheMode = {
async test(ctrl: any, env: any, ctx: any) {
let allowedCacheModes: Array<RequestCache> = [
const allowedCacheModes: RequestCache[] = [
'default',
'force-cache',
'no-cache',
Expand All @@ -56,12 +56,26 @@ export const cacheMode = {
assert.strictEqual(req.cache, undefined);
}
if (!env.CACHE_ENABLED) {
for (var cacheMode of allowedCacheModes) {
for (const cacheMode of allowedCacheModes) {
await assertRequestCacheThrowsError(cacheMode);
await assertFetchCacheRejectsError(cacheMode);
}
} else {
for (var cacheMode of allowedCacheModes) {
{
const req = new Request('https://example.org', { cache: 'no-store' });
assert.strictEqual(req.cache, 'no-store');
}
{
await fetch('https://example.org', { cache: 'no-store' });
}
const failureCacheModes: RequestCache[] = [
'default',
'no-cache',
'force-cache',
'only-if-cached',
'reload',
];
for (const cacheMode of failureCacheModes) {
await assertRequestCacheThrowsError(
cacheMode,
'TypeError',
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",
]
);
17 changes: 7 additions & 10 deletions src/workerd/api/http-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,11 +303,13 @@ export const cacheMode = {
await assertFetchCacheRejectsError('no-transform');
await assertFetchCacheRejectsError('unsupported');
} else {
await assertRequestCacheThrowsError(
'no-store',
'TypeError',
'Unsupported cache mode: no-store'
);
{
const req = new Request('https://example.org', { cache: 'no-store' });
assert.strictEqual(req.cache, 'no-store');
}
{
const req = await fetch('https://example.org', { cache: 'no-store' });
}
await assertRequestCacheThrowsError(
'no-cache',
'TypeError',
Expand All @@ -323,11 +325,6 @@ export const cacheMode = {
'TypeError',
'Unsupported cache mode: unsupported'
);
await assertFetchCacheRejectsError(
'no-store',
'TypeError',
'Unsupported cache mode: no-store'
);
await assertFetchCacheRejectsError(
'no-cache',
'TypeError',
Expand Down
3 changes: 3 additions & 0 deletions src/workerd/api/http-test.wd-test
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,7 @@ const unitTests :Workerd.Config = (
compatibilityFlags = ["nodejs_compat", "service_binding_extra_handlers", "cache_option_enabled"],
))
],
autogates = [
"workerd-autogate-http-request-cache",
]
);
69 changes: 65 additions & 4 deletions src/workerd/api/http.c++
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@
#include "util.h"
#include "queue.h"
#include "worker-rpc.h"
#include "workerd/jsg/jsvalue.h"
#include <kj/encoding.h>
#include <kj/compat/url.h>
#include <kj/memory.h>
#include <kj/parse/char.h>
#include <workerd/io/features.h>
#include <workerd/util/abortable.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 @@ -1176,6 +1178,41 @@ 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)) {
// Works because of the fact there are only two non-none cache modes
int ttl = 2;
switch (cacheMode) {
case CacheMode::NOSTORE:
ttl = -1;
break;
case CacheMode::NOCACHE:
ttl = 0;
case CacheMode::NONE:
KJ_ASSERT(false);
}

CfProperty clone;
KJ_IF_SOME(obj, cf.get(js)) {
(void)obj;
clone = cf.deepClone(js);
} else {
clone = CfProperty(js, js.obj());
}
auto obj = KJ_ASSERT_NONNULL(clone.get(js));

if (obj.has(js, "cacheTtl")) {
jsg::JsValue oldTtl = obj.get(js, "cacheTtl");
JSG_REQUIRE(oldTtl == js.num(ttl), TypeError,
kj::str("CacheTtl: ", oldTtl, ", is not compatible with cache: ",
getCacheModeName(cacheMode).orDefault("none"_kj), " header."));
} else {
obj.set(js, "cacheTtl", js.num(ttl));
}

obj.set(js, "cf-cache-level", js.str("byc"_kjc));
return clone.serialize(js);
}
return cf.serialize(js);
}

Expand All @@ -1185,7 +1222,12 @@ 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
auto cacheMode = getCacheModeFromName(c);
JSG_REQUIRE(cacheMode != Request::CacheMode::NOCACHE, TypeError,
kj::str("Unsupported cache mode: ", c));
}
}

Expand Down Expand Up @@ -1798,9 +1840,28 @@ jsg::Promise<jsg::Ref<Response>> fetchImplNoOutputLock(jsg::Lock& js,
jsRequest->shallowCopyHeadersTo(headers);

// 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);
// Currently, the only cache mode we support is undefined and no-store (behind an autogate),
// but we will soon support no-cache.
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:
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
1 change: 1 addition & 0 deletions src/workerd/io/io-thread-context.c++
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ 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")),
Expand Down
1 change: 1 addition & 0 deletions src/workerd/io/io-thread-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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
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 @@ -19,6 +19,8 @@ kj::StringPtr KJ_STRINGIFY(AutogateKey key) {
return "pyodide-load-external"_kj;
case AutogateKey::RESPONSE_STREAM_DISCONNECTED_STATUS:
return "response-stream-disconnected-status"_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 @@ -16,6 +16,7 @@ enum class AutogateKey {
PYODIDE_LOAD_EXTERNAL,
// Enables reporting of disconnection during deferred proxying as a new status.
RESPONSE_STREAM_DISCONNECTED_STATUS,
HTTP_REQUEST_CACHE,
NumOfKeys // Reserved for iteration.
};

Expand Down

0 comments on commit e51a319

Please sign in to comment.