Apply conditional preconditions before range handling
Co-authored-by: Theo Browne <[email protected]>
This commit is contained in:
1 parent
8f2fd0919c
commit
28db4bb777
2 files changed
+37
-20
No files matched your search
@@ -432,9 +432,14 @@ async function main() {
|
||||
"If-None-Match": assetEtag,
|
||||
},
|
||||
});
|
||||
if (conditionalRangedAsset.status !== 206) {
|
||||
if (conditionalRangedAsset.status !== 304) {
|
||||
throw new Error(
|
||||
`Smoke test failed: expected conditional ranged asset status 206, received ${conditionalRangedAsset.status}.`,
|
||||
`Smoke test failed: expected conditional ranged asset status 304, received ${conditionalRangedAsset.status}.`,
|
||||
);
|
||||
}
|
||||
if (conditionalRangedAsset.headers.get("content-range") !== null) {
|
||||
throw new Error(
|
||||
"Smoke test failed: expected no content-range on conditional ranged If-None-Match response.",
|
||||
);
|
||||
}
|
||||
const ifRangeEtagAsset = await fetch(assetUrl, {
|
||||
@@ -465,9 +470,14 @@ async function main() {
|
||||
"If-Modified-Since": assetLastModified,
|
||||
},
|
||||
});
|
||||
if (rangedModifiedSinceAsset.status !== 206) {
|
||||
if (rangedModifiedSinceAsset.status !== 304) {
|
||||
throw new Error(
|
||||
`Smoke test failed: expected ranged If-Modified-Since asset status 206, received ${rangedModifiedSinceAsset.status}.`,
|
||||
`Smoke test failed: expected ranged If-Modified-Since asset status 304, received ${rangedModifiedSinceAsset.status}.`,
|
||||
);
|
||||
}
|
||||
if (rangedModifiedSinceAsset.headers.get("content-range") !== null) {
|
||||
throw new Error(
|
||||
"Smoke test failed: expected no content-range on ranged If-Modified-Since response.",
|
||||
);
|
||||
}
|
||||
const ifRangeMismatchAsset = await fetch(assetUrl, {
|
||||
|
||||
+23
-16
@@ -840,6 +840,29 @@ function startStaticWebServer(distRoot: string, port: number) {
|
||||
return;
|
||||
}
|
||||
|
||||
const etag = staticEtagFor(stats);
|
||||
const lastModified = stats.mtime.toUTCString();
|
||||
const hasIfNoneMatchHeader = request.headers["if-none-match"] !== undefined;
|
||||
const ifNoneMatchMatches = ifNoneMatchSatisfied(request.headers["if-none-match"], etag);
|
||||
const ifModifiedSinceMatches = hasIfNoneMatchHeader
|
||||
? false
|
||||
: ifModifiedSinceSatisfied(request.headers["if-modified-since"], stats.mtimeMs);
|
||||
const shouldReturnNotModified = ifNoneMatchMatches || ifModifiedSinceMatches;
|
||||
|
||||
if (shouldReturnNotModified) {
|
||||
response.statusCode = 304;
|
||||
response.setHeader("Content-Type", contentTypeFor(targetPath));
|
||||
response.setHeader("ETag", etag);
|
||||
response.setHeader("Last-Modified", lastModified);
|
||||
response.setHeader("Accept-Ranges", "bytes");
|
||||
response.setHeader("Vary", "Range");
|
||||
applyStaticSecurityHeaders(response, {
|
||||
cacheControl: cacheControlFor(targetPath),
|
||||
});
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const resolvedRange = parseByteRangeHeader(request.headers.range, stats.size);
|
||||
if (resolvedRange === "invalid") {
|
||||
respondText(416, "Range Not Satisfiable", {
|
||||
@@ -849,19 +872,10 @@ function startStaticWebServer(distRoot: string, port: number) {
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
const etag = staticEtagFor(stats);
|
||||
const lastModified = stats.mtime.toUTCString();
|
||||
const effectiveRange =
|
||||
resolvedRange && ifRangeSatisfied(request.headers["if-range"], etag, stats.mtimeMs)
|
||||
? resolvedRange
|
||||
: null;
|
||||
const hadRangeRequest = resolvedRange !== null;
|
||||
const hasIfNoneMatchHeader = request.headers["if-none-match"] !== undefined;
|
||||
const ifNoneMatchMatches = ifNoneMatchSatisfied(request.headers["if-none-match"], etag);
|
||||
const ifModifiedSinceMatches = hasIfNoneMatchHeader
|
||||
? false
|
||||
: ifModifiedSinceSatisfied(request.headers["if-modified-since"], stats.mtimeMs);
|
||||
|
||||
response.statusCode = effectiveRange ? 206 : 200;
|
||||
response.setHeader("Content-Type", contentTypeFor(targetPath));
|
||||
@@ -880,13 +894,6 @@ function startStaticWebServer(distRoot: string, port: number) {
|
||||
cacheControl: cacheControlFor(targetPath),
|
||||
});
|
||||
|
||||
if (!hadRangeRequest && (ifNoneMatchMatches || ifModifiedSinceMatches)) {
|
||||
response.statusCode = 304;
|
||||
response.removeHeader("Content-Length");
|
||||
response.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (requestMethod === "HEAD") {
|
||||
response.end();
|
||||
return;
|
||||
|
||||
Reference in new issue
Block a user