Ice3man543 2021-08-30 16:58:22 +05:30
commit d33f6eb502
1 changed files with 27 additions and 19 deletions

View File

@ -349,7 +349,9 @@ func (r *Request) executeRequest(reqURL string, request *generatedRequest, previ
return err
}
defer func() {
if resp.StatusCode != http.StatusSwitchingProtocols {
_, _ = io.CopyN(ioutil.Discard, resp.Body, drainReqSize)
}
resp.Body.Close()
}()
@ -363,13 +365,16 @@ func (r *Request) executeRequest(reqURL string, request *generatedRequest, previ
return errors.Wrap(err, "could not dump http response")
}
var data, redirectedResponse []byte
// If the status code is HTTP 101, we should not proceed with reading body.
if resp.StatusCode != http.StatusSwitchingProtocols {
var bodyReader io.Reader
if r.MaxSize != 0 {
bodyReader = io.LimitReader(resp.Body, int64(r.MaxSize))
} else {
bodyReader = resp.Body
}
data, err := ioutil.ReadAll(bodyReader)
data, err = ioutil.ReadAll(bodyReader)
if err != nil {
// Ignore body read due to server misconfiguration errors
if stringsutil.ContainsAny(err.Error(), "gzip: invalid header") {
@ -380,10 +385,13 @@ func (r *Request) executeRequest(reqURL string, request *generatedRequest, previ
}
resp.Body.Close()
redirectedResponse, err := dumpResponseWithRedirectChain(resp, data)
redirectedResponse, err = dumpResponseWithRedirectChain(resp, data)
if err != nil {
return errors.Wrap(err, "could not read http response with redirect chain")
}
} else {
redirectedResponse = dumpedResponseHeaders
}
// net/http doesn't automatically decompress the response body if an
// encoding has been specified by the user in the request so in case we have to