Skip to content

Commit cbfcf8c

Browse files
authored
Merge pull request alibaba#295 from alibaba/304-gzip
do not unzip the response body when it's empty, such as 304
2 parents 87c4eee + b6c7088 commit cbfcf8c

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
lines changed

lib/requestHandler.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,20 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
9393
fulfill(resDataStream);
9494
} else {
9595
const serverResData = Buffer.concat(resDataChunks);
96+
const originContentLen = util.getByteSize(serverResData);
97+
// set origin content length into header
98+
resHeader['x-anyproxy-origin-content-length'] = originContentLen;
9699

97-
// put origin content length into header
98-
resHeader['x-anyproxy-origin-content-length'] = util.getByteSize(serverResData);
99-
100-
if (ifServerGzipped) {
100+
// only do unzip when there is res data
101+
if (ifServerGzipped && originContentLen) {
101102
zlib.gunzip(serverResData, (err, buff) => { // TODO test case to cover
102103
if (err) {
103104
rejectParsing(err);
104105
} else {
105106
fulfill(buff);
106107
}
107108
});
108-
} else if (isServerDeflated) {
109+
} else if (isServerDeflated && originContentLen) {
109110
zlib.inflateRaw(serverResData, (err, buff) => { // TODO test case to cover
110111
if (err) {
111112
rejectParsing(err);

test/jasmine.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
"../node_modules/babel-register/lib/node.js",
99
"../node_modules/babel-polyfill/dist/polyfill.js"
1010
],
11-
"stopSpecOnExpectationFailure": true,
11+
"stopSpecOnExpectationFailure": false,
1212
"random": false
1313
}

test/server/server.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ KoaServer.prototype.constructRouter = function () {
103103
});
104104
});
105105

106+
router.get('/test/response/304', this.logRequest, function *(next) {
107+
this.response.set('Content-Encoding', 'gzip');
108+
this.status = 304;
109+
});
110+
106111
router.get('/test/response/303', function *(next) {
107112
printLog('now to redirect 303');
108113
this.redirect('/test');

test/spec_rule/no_rule_spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,31 @@ function testRequest(protocol = 'http') {
212212
});
213213
});
214214

215+
it('304 should work as direct without proxy rules', (done) => {
216+
const url = constructUrl('/test/response/304');
217+
218+
proxyGet(url, CommonRequestHeader)
219+
.then(proxyRes => {
220+
directGet(url, CommonRequestHeader)
221+
.then(directRes => {
222+
expect(directRes.statusCode).toEqual(304);
223+
expect(directRes.body).toEqual('');
224+
225+
expect(directRes.statusCode).toEqual(proxyRes.statusCode);
226+
expect(isCommonResHeaderEqual(directRes.headers, proxyRes.headers, url)).toBe(true);
227+
expect(directRes.body).toEqual(proxyRes.body);
228+
expect(isCommonReqEqual(url, serverInstance)).toBe(true);
229+
done();
230+
}, error => {
231+
console.error('error happened in direct 304 request:', error);
232+
done.fail('error happened in direct 304 request');
233+
});
234+
}, error => {
235+
console.error('error happened in proxy 304 request:', error);
236+
done.fail('error happened in proxy 304 request');
237+
});
238+
})
239+
215240
describe('Response code should be honored as direct without proxy rules', () => {
216241
[301, 302, 303].forEach(code => {
217242
testRedirect(code);

test/util/CommonUtil.js

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,6 @@ function isCommonReqEqual(url, serverInstance) {
130130
isEqual = isEqual && proxyReqObj.headers['via-proxy'] === 'true';
131131
delete proxyReqObj.headers['via-proxy'];
132132

133-
// exclued accept-encoding from comparing, since the proxy will remove it before sending it out
134-
delete directReqObj.headers['accept-encoding'];
135-
136-
// TODO: 我这里proxy出去的options里没有accept-encoding, 但node自己加上了。Why ?
137-
// By 加里 2017.1.31
138-
delete proxyReqObj.headers['accept-encoding'];
139-
140133
directReqObj.headers['content-type'] = trimFormContentType(directReqObj.headers['content-type']);
141134
proxyReqObj.headers['content-type'] = trimFormContentType(proxyReqObj.headers['content-type']);
142135

0 commit comments

Comments
 (0)