Skip to content
Merged
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
13 changes: 13 additions & 0 deletions src/storage/backend/file.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,19 @@ describe('FileBackend conditional reads', () => {
await expect(statusFor({ ifNoneMatch: head.eTag })).resolves.toBe(304)
})

it.each([
['a weak tag', (eTag: string) => `W/${eTag}`],
['a tag list', (eTag: string) => `"stale-etag", ${eTag}`],
['a wildcard', () => '*'],
])('returns 304 when if-none-match is %s matching the etag', async (_name, toHeader) => {
const head = await backend.headObject(bucket, key, version)
await expect(statusFor({ ifNoneMatch: toHeader(head.eTag) })).resolves.toBe(304)
})

it('returns 200 when no tag in an if-none-match list matches', async () => {
await expect(statusFor({ ifNoneMatch: '"stale-etag", W/"other-etag"' })).resolves.toBe(200)
})

it('ignores if-modified-since when if-none-match is present and does not match', async () => {
await expect(
statusFor({ ifNoneMatch: '"stale-etag"', ifModifiedSince: lastModifiedHeader })
Expand Down
11 changes: 10 additions & 1 deletion src/storage/backend/file.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,15 @@ const METADATA_ATTR_KEYS = {
},
}

// RFC 9110 13.1.2: "*" matches any current representation;
// otherwise any listed entity-tag matches under weak comparison.
function ifNoneMatchMatches(ifNoneMatch: string, eTag: string): boolean {
if (ifNoneMatch.trim() === '*') {
return true
}
return ifNoneMatch.split(',').some((tag) => tag.trim().replace(/^W\//, '') === eTag)
}

/**
* FileBackend
* Interacts with the file system with this FileBackend adapter
Expand Down Expand Up @@ -101,7 +110,7 @@ export class FileBackend implements StorageBackendAdapter {
const { cacheControl, contentType } = await this.getFileMetadata(file)
const lastModified = data.mtime

if (headers?.ifNoneMatch && headers.ifNoneMatch === eTag) {
if (headers?.ifNoneMatch && ifNoneMatchMatches(headers.ifNoneMatch, eTag)) {
return {
metadata: {
cacheControl: cacheControl || 'no-cache',
Expand Down
Loading