Skip to content

Commit 2e03a39

Browse files
authored
Merge pull request #171 from mumugoah/master
Read gzipped response and request marshal include headers
2 parents e7ff7f4 + f740ece commit 2e03a39

File tree

3 files changed

+21
-11
lines changed

3 files changed

+21
-11
lines changed

colly.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ func (c *Collector) UnmarshalRequest(r []byte) (*Request, error) {
459459
Body: bytes.NewReader(req.Body),
460460
Ctx: ctx,
461461
ID: atomic.AddUint32(&c.requestCount, 1),
462-
Headers: &http.Header{},
462+
Headers: &req.Headers,
463463
collector: c,
464464
}, nil
465465
}

http_backend.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ import (
2828
"sync"
2929
"time"
3030

31+
"compress/gzip"
32+
3133
"github.com/gobwas/glob"
3234
)
3335

@@ -187,6 +189,12 @@ func (h *httpBackend) Do(request *http.Request, bodySize int) (*Response, error)
187189
if bodySize > 0 {
188190
bodyReader = io.LimitReader(bodyReader, int64(bodySize))
189191
}
192+
if res.Uncompressed && res.Header.Get("Content-Encoding") == "gzip" {
193+
bodyReader, err = gzip.NewReader(bodyReader)
194+
if err != nil {
195+
return nil, err
196+
}
197+
}
190198
body, err := ioutil.ReadAll(bodyReader)
191199
defer res.Body.Close()
192200
if err != nil {

request.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,12 @@ type Request struct {
5151
}
5252

5353
type serializableRequest struct {
54-
URL string
55-
Method string
56-
Body []byte
57-
ID uint32
58-
Ctx map[string]interface{}
54+
URL string
55+
Method string
56+
Body []byte
57+
ID uint32
58+
Ctx map[string]interface{}
59+
Headers http.Header
5960
}
6061

6162
// New creates a new request with the context of the original request
@@ -164,10 +165,11 @@ func (r *Request) Marshal() ([]byte, error) {
164165
}
165166
}
166167
return json.Marshal(&serializableRequest{
167-
URL: r.URL.String(),
168-
Method: r.Method,
169-
Body: body,
170-
ID: r.ID,
171-
Ctx: ctx,
168+
URL: r.URL.String(),
169+
Method: r.Method,
170+
Body: body,
171+
ID: r.ID,
172+
Ctx: ctx,
173+
Headers: *r.Headers,
172174
})
173175
}

0 commit comments

Comments
 (0)