Skip to content

Commit

Permalink
[fix] Make ETag header comply with standard. (socketio#2603)
Browse files Browse the repository at this point in the history
The standard says that an ETag must be surrounded in double quotes:

https://tools.ietf.org/html/rfc7232#section-2.3

Although browsers tend to be lenient, omitting the quotes can confuse/break some kinds of proxies and other tools that demand compliant formatting. For example, Sandstorm.io enforces strict HTTP usage for security reasons and will block responses with invalid ETags.
  • Loading branch information
kentonv authored and darrachequesne committed Oct 30, 2016
1 parent fdf64cc commit d026c00
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 4 deletions.
8 changes: 6 additions & 2 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,13 @@ Server.prototype.attachServe = function(srv){
*/

Server.prototype.serve = function(req, res){
// Per the standard, ETags must be quoted:
// https://tools.ietf.org/html/rfc7232#section-2.3
var expectedEtag = '"' + clientVersion + '"';

var etag = req.headers['if-none-match'];
if (etag) {
if (clientVersion == etag) {
if (expectedEtag == etag) {
debug('serve client 304');
res.writeHead(304);
res.end();
Expand All @@ -284,7 +288,7 @@ Server.prototype.serve = function(req, res){

debug('serve client source');
res.setHeader('Content-Type', 'application/javascript');
res.setHeader('ETag', clientVersion);
res.setHeader('ETag', expectedEtag);
res.writeHead(200);
res.end(clientSource);
};
Expand Down
4 changes: 2 additions & 2 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ describe('socket.io', function(){
if (err) return done(err);
var ctype = res.headers['content-type'];
expect(ctype).to.be('application/javascript');
expect(res.headers.etag).to.be(clientVersion);
expect(res.headers.etag).to.be('"' + clientVersion + '"');
expect(res.text).to.match(/engine\.io/);
expect(res.status).to.be(200);
done();
Expand All @@ -179,7 +179,7 @@ describe('socket.io', function(){
io(srv);
request(srv)
.get('/socket.io/socket.io.js')
.set('If-None-Match', clientVersion)
.set('If-None-Match', '"' + clientVersion + '"')
.end(function(err, res){
if (err) return done(err);
expect(res.statusCode).to.be(304);
Expand Down

0 comments on commit d026c00

Please sign in to comment.