Skip to content

Commit ee0f285

Browse files
committed
Only update the content-header when the content is handled by AnyProxy
1 parent 9cdac33 commit ee0f285

File tree

1 file changed

+20
-14
lines changed

1 file changed

+20
-14
lines changed

lib/requestHandler.js

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -94,11 +94,29 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
9494
} else {
9595
const serverResData = Buffer.concat(resDataChunks);
9696
const originContentLen = util.getByteSize(serverResData);
97+
// remove gzip related header, and ungzip the content
98+
// note there are other compression types like deflate
99+
const contentEncoding = resHeader['content-encoding'] || resHeader['Content-Encoding'];
100+
const ifServerGzipped = /gzip/i.test(contentEncoding);
101+
const isServerDeflated = /deflate/i.test(contentEncoding);
102+
103+
/**
104+
* when the content is unzipped, update the header content
105+
*/
106+
const refactContentEncoding = () => {
107+
if (contentEncoding) {
108+
resHeader['x-anyproxy-origin-content-encoding'] = contentEncoding;
109+
delete resHeader['content-encoding'];
110+
delete resHeader['Content-Encoding'];
111+
}
112+
}
113+
97114
// set origin content length into header
98115
resHeader['x-anyproxy-origin-content-length'] = originContentLen;
99-
116+
100117
// only do unzip when there is res data
101118
if (ifServerGzipped && originContentLen) {
119+
refactContentEncoding();
102120
zlib.gunzip(serverResData, (err, buff) => { // TODO test case to cover
103121
if (err) {
104122
rejectParsing(err);
@@ -107,6 +125,7 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
107125
}
108126
});
109127
} else if (isServerDeflated && originContentLen) {
128+
refactContentEncoding();
110129
zlib.inflateRaw(serverResData, (err, buff) => { // TODO test case to cover
111130
if (err) {
112131
rejectParsing(err);
@@ -131,12 +150,6 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
131150
});
132151
};
133152

134-
// remove gzip related header, and ungzip the content
135-
// note there are other compression types like deflate
136-
const contentEncoding = resHeader['content-encoding'] || resHeader['Content-Encoding'];
137-
const ifServerGzipped = /gzip/i.test(contentEncoding);
138-
const isServerDeflated = /deflate/i.test(contentEncoding);
139-
140153
//deal response data
141154
res.on('data', (chunk) => {
142155
rawResChunks.push(chunk);
@@ -265,20 +278,13 @@ function getUserReqHandler(userRule, recorder) {
265278
const responseBody = responseInfo.body || '';
266279

267280
const transferEncoding = resHeader['transfer-encoding'] || resHeader['Transfer-Encoding'] || '';
268-
const contentEncoding = resHeader['content-encoding'] || resHeader['Content-Encoding'];
269281
const contentLength = resHeader['content-length'] || resHeader['Content-Length'];
270282
const connection = resHeader.Connection || resHeader.connection;
271283
if (contentLength) {
272284
delete resHeader['content-length'];
273285
delete resHeader['Content-Length'];
274286
}
275287

276-
if (contentEncoding) {
277-
resHeader['x-anyproxy-origin-content-encoding'] = contentEncoding;
278-
delete resHeader['content-encoding'];
279-
delete resHeader['Content-Encoding'];
280-
}
281-
282288
// set proxy-connection
283289
if (connection) {
284290
resHeader['x-anyproxy-origin-connection'] = connection;

0 commit comments

Comments
 (0)