Skip to content

Commit ef66833

Browse files
jpetazzoindexzero
authored andcommitted
Fix truncated chunked responses
1 parent af9eb06 commit ef66833

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

Diff for: lib/node-http-proxy/http-proxy.js

+20-1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,20 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
277277
if (!ended) { response.emit('end') }
278278
});
279279

280+
//
281+
// After reading a chunked response, the underlying socket
282+
// will hit EOF and emit a 'end' event, which will abort
283+
// the request. If the socket was paused at that time,
284+
// pending data gets discarded, truncating the response.
285+
// This code makes sure that we flush pending data.
286+
//
287+
response.connection.on('end', function () {
288+
if (response.readable && response.resume) {
289+
{
290+
response.resume();
291+
}
292+
});
293+
280294
response.on('end', function () {
281295
ended = true;
282296
if (!errState) {
@@ -296,7 +310,12 @@ HttpProxy.prototype.proxyRequest = function (req, res, buffer) {
296310

297311
function ondata(chunk) {
298312
if (res.writable) {
299-
if (false === res.write(chunk) && response.pause) {
313+
// Only pause if the underlying buffers are full,
314+
// *and* the connection is not in 'closing' state.
315+
// Otherwise, the pause will cause pending data to
316+
// be discarded and silently lost.
317+
if (false === res.write(chunk) && response.pause
318+
&& response.connection.readable) {
300319
response.pause();
301320
}
302321
}

0 commit comments

Comments
 (0)