@@ -94,11 +94,29 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
94
94
} else {
95
95
const serverResData = Buffer . concat ( resDataChunks ) ;
96
96
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 = / g z i p / i. test ( contentEncoding ) ;
101
+ const isServerDeflated = / d e f l a t e / 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
+
97
114
// set origin content length into header
98
115
resHeader [ 'x-anyproxy-origin-content-length' ] = originContentLen ;
99
-
116
+
100
117
// only do unzip when there is res data
101
118
if ( ifServerGzipped && originContentLen ) {
119
+ refactContentEncoding ( ) ;
102
120
zlib . gunzip ( serverResData , ( err , buff ) => { // TODO test case to cover
103
121
if ( err ) {
104
122
rejectParsing ( err ) ;
@@ -107,6 +125,7 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
107
125
}
108
126
} ) ;
109
127
} else if ( isServerDeflated && originContentLen ) {
128
+ refactContentEncoding ( ) ;
110
129
zlib . inflateRaw ( serverResData , ( err , buff ) => { // TODO test case to cover
111
130
if ( err ) {
112
131
rejectParsing ( err ) ;
@@ -131,12 +150,6 @@ function fetchRemoteResponse(protocol, options, reqData, config) {
131
150
} ) ;
132
151
} ;
133
152
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 = / g z i p / i. test ( contentEncoding ) ;
138
- const isServerDeflated = / d e f l a t e / i. test ( contentEncoding ) ;
139
-
140
153
//deal response data
141
154
res . on ( 'data' , ( chunk ) => {
142
155
rawResChunks . push ( chunk ) ;
@@ -265,20 +278,13 @@ function getUserReqHandler(userRule, recorder) {
265
278
const responseBody = responseInfo . body || '' ;
266
279
267
280
const transferEncoding = resHeader [ 'transfer-encoding' ] || resHeader [ 'Transfer-Encoding' ] || '' ;
268
- const contentEncoding = resHeader [ 'content-encoding' ] || resHeader [ 'Content-Encoding' ] ;
269
281
const contentLength = resHeader [ 'content-length' ] || resHeader [ 'Content-Length' ] ;
270
282
const connection = resHeader . Connection || resHeader . connection ;
271
283
if ( contentLength ) {
272
284
delete resHeader [ 'content-length' ] ;
273
285
delete resHeader [ 'Content-Length' ] ;
274
286
}
275
287
276
- if ( contentEncoding ) {
277
- resHeader [ 'x-anyproxy-origin-content-encoding' ] = contentEncoding ;
278
- delete resHeader [ 'content-encoding' ] ;
279
- delete resHeader [ 'Content-Encoding' ] ;
280
- }
281
-
282
288
// set proxy-connection
283
289
if ( connection ) {
284
290
resHeader [ 'x-anyproxy-origin-connection' ] = connection ;
0 commit comments