Skip to content

Commit

Permalink
feat: confirm a weak but matching ETag (#3485)
Browse files Browse the repository at this point in the history
When handling compression at the proxy server level, the client receives a weak ETag.
Weak ETags are prefixed with `W/`, e.g. `W/"2.2.0"`.
Upon cache validation we should take care of these too.

Reference: https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
  • Loading branch information
das7pad authored and darrachequesne committed Jan 15, 2021
1 parent d52532b commit 161091d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/index.ts
Expand Up @@ -437,10 +437,11 @@ export class Server extends EventEmitter {
// Per the standard, ETags must be quoted:
// https://tools.ietf.org/html/rfc7232#section-2.3
const expectedEtag = '"' + clientVersion + '"';
const weakEtag = "W/" + expectedEtag;

const etag = req.headers["if-none-match"];
if (etag) {
if (expectedEtag == etag) {
if (expectedEtag === etag || weakEtag === etag) {
debug("serve client %s 304", type);
res.writeHead(304);
res.end();
Expand Down
13 changes: 13 additions & 0 deletions test/socket.io.ts
Expand Up @@ -128,6 +128,19 @@ describe("socket.io", () => {
});
});

it("should handle 304", (done) => {
const srv = createServer();
new Server(srv);
request(srv)
.get("/socket.io/socket.io.js")
.set("If-None-Match", 'W/"' + clientVersion + '"')
.end((err, res) => {
if (err) return done(err);
expect(res.statusCode).to.be(304);
done();
});
});

it("should not serve static files", (done) => {
const srv = createServer();
new Server(srv, { serveClient: false });
Expand Down

0 comments on commit 161091d

Please sign in to comment.