Skip to content
Browse files

Add Date header to responses.

  • Loading branch information...
1 parent 353e256 commit 589e2459a40d03fe537c13eeaea7bef06d830961 @mnot committed Sep 29, 2010
Showing with 23 additions and 0 deletions.
  1. +12 −0 lib/http.js
  2. +11 −0 lib/sys.js
View
12 lib/http.js
@@ -173,6 +173,7 @@ var transferEncodingExpression = /Transfer-Encoding/i;
var closeExpression = /close/i;
var chunkExpression = /chunk/i;
var contentLengthExpression = /Content-Length/i;
+var dateExpression = /Date/i;
/* Abstract base class for ServerRequest and ClientResponse. */
@@ -279,6 +280,7 @@ function OutgoingMessage (socket) {
this.chunkedEncoding = false;
this.shouldKeepAlive = true;
this.useChunkedEncodingByDefault = true;
+ this.shouldSendDate = false;
this._hasBody = true;
this._trailer = '';
@@ -358,6 +360,7 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) {
var sentConnectionHeader = false;
var sentContentLengthHeader = false;
var sentTransferEncodingHeader = false;
+ var sentDateHeader = false;
// firstLine in the case of request is: "GET /index.html HTTP/1.1\r\n"
// in the case of response it is: "HTTP/1.1 200 OK\r\n"
@@ -383,6 +386,8 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) {
} else if (contentLengthExpression.test(field)) {
sentContentLengthHeader = true;
+ } else if (dateExpression.test(field)) {
+ sentDateHeader = true;
}
}
@@ -411,6 +416,11 @@ OutgoingMessage.prototype._storeHeader = function (firstLine, headers) {
}
}
+ // Date header
+ if (this.shouldSendDate == true && sentDateHeader == false) {
+ messageHeader += "Date: " + sys.utcDate() + CRLF;
+ }
+
// keep-alive logic
if (sentConnectionHeader == false) {
if (this.shouldKeepAlive &&
@@ -571,6 +581,8 @@ function ServerResponse (req) {
if (req.method === 'HEAD') this._hasBody = false;
+ this.shouldSendDate = true;
+
if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) {
this.useChunkedEncodingByDefault = false;
this.shouldKeepAlive = false;
View
11 lib/sys.js 100644 → 100755
@@ -276,6 +276,17 @@ function isDate (d) {
return JSON.stringify(proto) === JSON.stringify(properties);
}
+var date_cache;
@isaacs
isaacs added a note Sep 29, 2010

It seems like this is going to do extra date lookups when idle. How about this approach instead?

var dateCache = null
function utcDate () {
  if (dateCache) return dateCache;
  dateCache = new Date().toUTCString();
  setTimeout(function () {
    dateCache = null
  }, 1000)
  return dateCache;
}

Of course, it's not like 1 syscall every second is really all that expensive ;)

@mnot
Owner
mnot added a note Sep 29, 2010

Yep, I thought the same thing when I was out for lunch :) Also, it's not as accurate as it could be. See most recent commit fead685.

Cheers,

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+exports.utcDate = function () {
+ if (! date_cache) {
+ update_date();
+ }
+ return date_cache;
+}
+ function update_date() {
+ date_cache = new Date().toUTCString();
+ setTimeout(update_date, 1000);
+ }
var pWarning;

0 comments on commit 589e245

Please sign in to comment.
Something went wrong with that request. Please try again.