Skip to content

Commit eaf3318

Browse files
committed
Generate ETags for all request responses
closes expressjs#2546
1 parent e1057bd commit eaf3318

File tree

4 files changed

+125
-100
lines changed

4 files changed

+125
-100
lines changed

History.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
3.x
22
===
33

4+
* Generate `ETag`s for all request responses
5+
- No longer restricted to only responses for `GET` and `HEAD` requests
46
* Use `content-type` to parse `Content-Type` headers
57
68
- Use `content-type` to parse `Content-Type` headers

LICENSE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
(The MIT License)
22

33
Copyright (c) 2009-2013 TJ Holowaychuk <[email protected]>
4+
Copyright (c) 2013 Roman Shtylman <[email protected]>
5+
Copyright (c) 2014-2015 Douglas Christopher Wilson <[email protected]>
46

57
Permission is hereby granted, free of charge, to any person obtaining
68
a copy of this software and associated documentation files (the

lib/response.js

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
1+
/*!
2+
* express
3+
* Copyright(c) 2009-2013 TJ Holowaychuk
4+
* Copyright(c) 2014-2015 Douglas Christopher Wilson
5+
* MIT Licensed
6+
*/
7+
18
/**
29
* Module dependencies.
10+
* @api private
311
*/
412

513
var contentDisposition = require('content-disposition');
@@ -86,7 +94,6 @@ res.links = function(links){
8694

8795
res.send = function(body){
8896
var req = this.req;
89-
var head = 'HEAD' == req.method;
9097
var type;
9198
var encoding;
9299
var len;
@@ -153,12 +160,12 @@ res.send = function(body){
153160
this.set('Content-Length', len);
154161
}
155162

156-
// ETag support
157-
var etag = len !== undefined && app.get('etag fn');
158-
if (etag && ('GET' === req.method || 'HEAD' === req.method)) {
159-
if (!this.get('ETag')) {
160-
etag = etag(body, encoding);
161-
etag && this.set('ETag', etag);
163+
// populate ETag
164+
var etag;
165+
var generateETag = len !== undefined && app.get('etag fn');
166+
if (typeof generateETag === 'function' && !this.get('ETag')) {
167+
if ((etag = generateETag(body, encoding))) {
168+
this.set('ETag', etag);
162169
}
163170
}
164171

@@ -173,8 +180,13 @@ res.send = function(body){
173180
body = '';
174181
}
175182

176-
// respond
177-
this.end((head ? null : body), encoding);
183+
if (req.method === 'HEAD') {
184+
// skip body for HEAD
185+
this.end();
186+
} else {
187+
// respond
188+
this.end(body, encoding);
189+
}
178190

179191
return this;
180192
};

0 commit comments

Comments
 (0)