Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix Resource with the 304 status code and the 'content-length: x' header does not load (close #1602) #1638

Merged
merged 3 commits into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions src/request-pipeline/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -117,7 +121,7 @@ const stages = {
}
}
else
ctx.res.end('');
ctx.res.end();

return;
}
Expand Down
40 changes: 40 additions & 0 deletions test/server/proxy-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
});
});
});