@@ -2,9 +2,11 @@ package api
2
2
3
3
import (
4
4
"bytes"
5
+ "compress/gzip"
5
6
"context"
6
7
"encoding/json"
7
8
"errors"
9
+ "io"
8
10
"io/ioutil"
9
11
"net/http"
10
12
"net/http/httputil"
@@ -178,14 +180,26 @@ func rewriteBitBucketLink(link, endpointAPIURL, proxyAPIURL string) string {
178
180
}
179
181
180
182
func rewriteLinksInBitBucketResponse (resp * http.Response , endpointAPIURL , proxyAPIURL string ) error {
181
- body , err := ioutil .ReadAll (resp .Body )
183
+ var bodyReader io.ReadCloser
184
+ var err error
185
+ switch resp .Header .Get ("Content-Encoding" ) {
186
+ case "gzip" :
187
+ bodyReader , err = gzip .NewReader (resp .Body )
188
+ if err != nil {
189
+ return nil
190
+ }
191
+ defer bodyReader .Close ()
192
+ default :
193
+ bodyReader = resp .Body
194
+ }
195
+ body , err := ioutil .ReadAll (bodyReader )
182
196
if err != nil {
183
- return err
197
+ return nil
184
198
}
185
199
186
200
var b map [string ]interface {}
187
201
if err := json .Unmarshal (body , & b ); err != nil {
188
- return err
202
+ return nil
189
203
}
190
204
191
205
if next , ok := b ["next" ].(string ); ok {
@@ -196,9 +210,21 @@ func rewriteLinksInBitBucketResponse(resp *http.Response, endpointAPIURL, proxyA
196
210
b ["previous" ] = rewriteBitBucketLink (prev , endpointAPIURL , proxyAPIURL )
197
211
}
198
212
199
- newBody , err := json .Marshal (b )
200
- println (newBody )
201
- resp .Body = ioutil .NopCloser (bytes .NewReader (newBody ))
213
+ newBodyBytes , err := json .Marshal (b )
214
+
215
+ switch resp .Header .Get ("Content-Encoding" ) {
216
+ case "gzip" :
217
+ var compressedBody bytes.Buffer
218
+ w := gzip .NewWriter (& compressedBody )
219
+ defer w .Close ()
220
+ _ , err = w .Write (newBodyBytes )
221
+ if err != nil {
222
+ return err
223
+ }
224
+ resp .Body = ioutil .NopCloser (& compressedBody )
225
+ default :
226
+ resp .Body = ioutil .NopCloser (bytes .NewReader (newBodyBytes ))
227
+ }
202
228
203
229
return nil
204
230
}
0 commit comments