mirror of
https://github.com/alibaba/anyproxy.git
synced 2025-04-23 20:31:25 +00:00
Only update the content-header
when the content is handled by AnyProxy
This commit is contained in:
parent
9cdac33e97
commit
ee0f285e78
@ -94,11 +94,29 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
|
|||||||
} else {
|
} else {
|
||||||
const serverResData = Buffer.concat(resDataChunks);
|
const serverResData = Buffer.concat(resDataChunks);
|
||||||
const originContentLen = util.getByteSize(serverResData);
|
const originContentLen = util.getByteSize(serverResData);
|
||||||
|
// remove gzip related header, and ungzip the content
|
||||||
|
// note there are other compression types like deflate
|
||||||
|
const contentEncoding = resHeader['content-encoding'] || resHeader['Content-Encoding'];
|
||||||
|
const ifServerGzipped = /gzip/i.test(contentEncoding);
|
||||||
|
const isServerDeflated = /deflate/i.test(contentEncoding);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* when the content is unzipped, update the header content
|
||||||
|
*/
|
||||||
|
const refactContentEncoding = () => {
|
||||||
|
if (contentEncoding) {
|
||||||
|
resHeader['x-anyproxy-origin-content-encoding'] = contentEncoding;
|
||||||
|
delete resHeader['content-encoding'];
|
||||||
|
delete resHeader['Content-Encoding'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// set origin content length into header
|
// set origin content length into header
|
||||||
resHeader['x-anyproxy-origin-content-length'] = originContentLen;
|
resHeader['x-anyproxy-origin-content-length'] = originContentLen;
|
||||||
|
|
||||||
// only do unzip when there is res data
|
// only do unzip when there is res data
|
||||||
if (ifServerGzipped && originContentLen) {
|
if (ifServerGzipped && originContentLen) {
|
||||||
|
refactContentEncoding();
|
||||||
zlib.gunzip(serverResData, (err, buff) => { // TODO test case to cover
|
zlib.gunzip(serverResData, (err, buff) => { // TODO test case to cover
|
||||||
if (err) {
|
if (err) {
|
||||||
rejectParsing(err);
|
rejectParsing(err);
|
||||||
@ -107,6 +125,7 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
} else if (isServerDeflated && originContentLen) {
|
} else if (isServerDeflated && originContentLen) {
|
||||||
|
refactContentEncoding();
|
||||||
zlib.inflateRaw(serverResData, (err, buff) => { // TODO test case to cover
|
zlib.inflateRaw(serverResData, (err, buff) => { // TODO test case to cover
|
||||||
if (err) {
|
if (err) {
|
||||||
rejectParsing(err);
|
rejectParsing(err);
|
||||||
@ -131,12 +150,6 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
// remove gzip related header, and ungzip the content
|
|
||||||
// note there are other compression types like deflate
|
|
||||||
const contentEncoding = resHeader['content-encoding'] || resHeader['Content-Encoding'];
|
|
||||||
const ifServerGzipped = /gzip/i.test(contentEncoding);
|
|
||||||
const isServerDeflated = /deflate/i.test(contentEncoding);
|
|
||||||
|
|
||||||
//deal response data
|
//deal response data
|
||||||
res.on('data', (chunk) => {
|
res.on('data', (chunk) => {
|
||||||
rawResChunks.push(chunk);
|
rawResChunks.push(chunk);
|
||||||
@ -265,7 +278,6 @@ function getUserReqHandler(userRule, recorder) {
|
|||||||
const responseBody = responseInfo.body || '';
|
const responseBody = responseInfo.body || '';
|
||||||
|
|
||||||
const transferEncoding = resHeader['transfer-encoding'] || resHeader['Transfer-Encoding'] || '';
|
const transferEncoding = resHeader['transfer-encoding'] || resHeader['Transfer-Encoding'] || '';
|
||||||
const contentEncoding = resHeader['content-encoding'] || resHeader['Content-Encoding'];
|
|
||||||
const contentLength = resHeader['content-length'] || resHeader['Content-Length'];
|
const contentLength = resHeader['content-length'] || resHeader['Content-Length'];
|
||||||
const connection = resHeader.Connection || resHeader.connection;
|
const connection = resHeader.Connection || resHeader.connection;
|
||||||
if (contentLength) {
|
if (contentLength) {
|
||||||
@ -273,12 +285,6 @@ function getUserReqHandler(userRule, recorder) {
|
|||||||
delete resHeader['Content-Length'];
|
delete resHeader['Content-Length'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (contentEncoding) {
|
|
||||||
resHeader['x-anyproxy-origin-content-encoding'] = contentEncoding;
|
|
||||||
delete resHeader['content-encoding'];
|
|
||||||
delete resHeader['Content-Encoding'];
|
|
||||||
}
|
|
||||||
|
|
||||||
// set proxy-connection
|
// set proxy-connection
|
||||||
if (connection) {
|
if (connection) {
|
||||||
resHeader['x-anyproxy-origin-connection'] = connection;
|
resHeader['x-anyproxy-origin-connection'] = connection;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user