Commit
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,9 +226,22 @@ var transferEncodingExpression = /Transfer-Encoding/i; | |
var closeExpression = /close/i; | ||
var chunkExpression = /chunk/i; | ||
var contentLengthExpression = /Content-Length/i; | ||
var dateExpression = /Date/i; | ||
var expectExpression = /Expect/i; | ||
var continueExpression = /100-continue/i; | ||
|
||
var dateCache; | ||
function utcDate() { | ||
if (! dateCache) { | ||
var d = new Date(); | ||
dateCache = d.toUTCString(); | ||
setTimeout(function () { | ||
dateCache = undefined; | ||
}, 1000 - d.getMilliseconds()); | ||
} | ||
return dateCache; | ||
} | ||
|
||
|
||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
mnot
Author
|
||
/* Abstract base class for ServerRequest and ClientResponse. */ | ||
function IncomingMessage(socket) { | ||
|
@@ -383,6 +396,7 @@ function OutgoingMessage() { | |
this.chunkedEncoding = false; | ||
this.shouldKeepAlive = true; | ||
this.useChunkedEncodingByDefault = true; | ||
this.sendDate = false; | ||
|
||
this._hasBody = true; | ||
this._trailer = ''; | ||
|
@@ -473,6 +487,7 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) { | |
var sentConnectionHeader = false; | ||
var sentContentLengthHeader = false; | ||
var sentTransferEncodingHeader = false; | ||
var sentDateHeader = false; | ||
var sentExpect = false; | ||
|
||
// firstLine in the case of request is: 'GET /index.html HTTP/1.1\r\n' | ||
|
@@ -498,7 +513,8 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) { | |
|
||
} else if (contentLengthExpression.test(field)) { | ||
sentContentLengthHeader = true; | ||
|
||
} else if (dateExpression.test(field)) { | ||
sentDateHeader = true; | ||
} else if (expectExpression.test(field)) { | ||
sentExpect = true; | ||
} | ||
|
@@ -529,6 +545,11 @@ OutgoingMessage.prototype._storeHeader = function(firstLine, headers) { | |
} | ||
} | ||
|
||
// Date header | ||
if (this.sendDate == true && sentDateHeader == false) { | ||
messageHeader += "Date: " + utcDate() + CRLF; | ||
} | ||
|
||
// keep-alive logic | ||
if (sentConnectionHeader === false) { | ||
if (this.shouldKeepAlive && | ||
|
@@ -816,6 +837,8 @@ function ServerResponse(req) { | |
|
||
if (req.method === 'HEAD') this._hasBody = false; | ||
|
||
this.sendDate = true; | ||
|
||
if (req.httpVersionMajor < 1 || req.httpVersionMinor < 1) { | ||
this.useChunkedEncodingByDefault = false; | ||
this.shouldKeepAlive = false; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var common = require("../common"); | ||
var assert = require('assert'); | ||
var http = require("http"); | ||
|
||
var testResBody = "other stuff!\n"; | ||
|
||
var server = http.createServer(function(req, res) { | ||
assert.ok(! ("date" in req.headers), | ||
"Request headers contained a Date." | ||
); | ||
res.writeHead(200, { | ||
'Content-Type' : 'text/plain', | ||
}); | ||
res.end(testResBody); | ||
}); | ||
server.listen(common.PORT); | ||
|
||
|
||
server.addListener("listening", function() { | ||
var options = { | ||
port: common.PORT, | ||
path: "/", | ||
method: "GET" | ||
} | ||
var req = http.request(options, function (res) { | ||
assert.ok("date" in res.headers, | ||
"Response headers didn't contain a Date." | ||
); | ||
res.addListener('end', function () { | ||
server.close(); | ||
process.exit(); | ||
}); | ||
}); | ||
req.end(); | ||
}); |
4 comments
on commit 1e425e3
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Finally! Thanks @mnot
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now much does this slow down our hello-world benchmark?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Last I checked, it was in the noise; the expensive part was the toUTCString(), and that's cached.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
❤️
should this maybe be specific (or somehow tied to) server instances? so that on close the cyclic timeout stops