From 738aa102f3b9e31a14266ecac72d11446ee43db3 Mon Sep 17 00:00:00 2001 From: Artem Lavrov Date: Wed, 13 Jun 2018 14:38:13 +0300 Subject: [PATCH] fix `Resource with the 304 status code and the 'content-length: x' header does not load` (close #1602) (#1638) * fix `Resource with the 304 status code and the 'content-length: x' header does not load` (close #1602) * fix tests * fix review issue --- src/request-pipeline/index.js | 8 +++++-- test/server/proxy-test.js | 40 +++++++++++++++++++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/request-pipeline/index.js b/src/request-pipeline/index.js index c5ad5d370..ca30b3e06 100644 --- a/src/request-pipeline/index.js +++ b/src/request-pipeline/index.js @@ -96,7 +96,11 @@ const stages = { ctx.onResponseEventDataWithoutBody.push({ rule, opts: configureResponseEvent.opts }); }); - ctx.destRes.pipe(ctx.res); + if (ctx.contentInfo.isNotModified) + ctx.res.end(); + else + ctx.destRes.pipe(ctx.res); + if (ctx.onResponseEventDataWithoutBody.length) { ctx.res.on('finish', () => { const responseInfo = requestEventInfo.createResponseInfo(ctx); @@ -117,7 +121,7 @@ const stages = { } } else - ctx.res.end(''); + ctx.res.end(); return; } diff --git a/test/server/proxy-test.js b/test/server/proxy-test.js index ab192a935..104f66905 100644 --- a/test/server/proxy-test.js +++ b/test/server/proxy-test.js @@ -3338,5 +3338,45 @@ describe('Proxy', () => { testRedirectRequestStatusCode(202, false) ]); }); + + it('Should not pipe the request with the 304 status code (Not Modified) (GH-1602)', () => { + const server = net.createServer(socket => { + socket.on('data', () => { + socket.write([ + 'HTTP/1.1 304 Not Modified', + 'Content-Length: 5', + '', + '' + ].join('\r\n')); + }); + }); + + return getFreePort() + .then(port => new Promise(resolve => server.listen(port, () => resolve(port)))) + .then(port => new Promise((resolve, reject) => { + const proxyUrl = proxy.openSession(`http://127.0.0.1:${port}/`, session); + const reqOptions = Object.assign(urlLib.parse(proxyUrl), { + method: 'GET', + headers: { 'if-none-match': 'NQQ6Iyi1ttEATRNQs+U9yQ==' } + }); + + const req = http.request(reqOptions, res => { + const chunks = []; + + res.on('data', chunk => chunks.push(chunk)); + res.on('end', () => resolve({ res, body: Buffer.concat(chunks).toString() })); + }); + + req.on('error', reject); + req.setTimeout(1500, () => reject('timeout')); + req.end(); + })) + .then(({ res, body }) => { + expect(res.statusCode).eql(304); + expect(res.headers['content-length']).eql('5'); + expect(body).eql(''); + server.close(); + }); + }); }); });