summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorFedor Indutny <fedor@indutny.com>2016-06-03 23:59:04 -0400
committerEvan Lucas <evanlucas@me.com>2016-06-15 18:02:01 -0500
commit40e49dee823509a17a9ce49302f90ef9aafdc65c (patch)
treee538b2a52339f3f2d0f04743f4fc2443cad97c44 /lib
parentbfb7e3cc6ec6f9ef12f1d59e1c756d8ee627caaa (diff)
downloadnode-new-40e49dee823509a17a9ce49302f90ef9aafdc65c.tar.gz
http: wait for both prefinish/end to keepalive
When `free`ing the socket to be reused in keep-alive Agent wait for both `prefinish` and `end` events. Otherwise the next request may be written before the previous one has finished sending the body, leading to a parser errors. PR-URL: https://github.com/nodejs/node/pull/7149 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/_http_client.js27
1 files changed, 24 insertions, 3 deletions
diff --git a/lib/_http_client.js b/lib/_http_client.js
index 68eb125e0e..91855e5b87 100644
--- a/lib/_http_client.js
+++ b/lib/_http_client.js
@@ -195,6 +195,8 @@ function ClientRequest(options, cb) {
self._flush();
self = null;
});
+
+ this._ended = false;
}
util.inherits(ClientRequest, OutgoingMessage);
@@ -466,6 +468,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
// add our listener first, so that we guarantee socket cleanup
res.on('end', responseOnEnd);
+ req.on('prefinish', requestOnPrefinish);
var handled = req.emit('response', res);
// If the user did not listen for the 'response' event, then they
@@ -478,9 +481,7 @@ function parserOnIncomingClient(res, shouldKeepAlive) {
}
// client
-function responseOnEnd() {
- var res = this;
- var req = res.req;
+function responseKeepAlive(res, req) {
var socket = req.socket;
if (!req.shouldKeepAlive) {
@@ -504,6 +505,26 @@ function responseOnEnd() {
}
}
+function responseOnEnd() {
+ const res = this;
+ const req = this.req;
+
+ req._ended = true;
+ if (!req.shouldKeepAlive || req.finished)
+ responseKeepAlive(res, req);
+}
+
+function requestOnPrefinish() {
+ const req = this;
+ const res = this.res;
+
+ if (!req.shouldKeepAlive)
+ return;
+
+ if (req._ended)
+ responseKeepAlive(res, req);
+}
+
function emitFreeNT(socket) {
socket.emit('free');
}