Fixed fatal panic in http header map read (#2488)

dev
Ice3man 2022-08-24 23:29:22 +05:30 committed by GitHub
parent 77c81834b2
commit 8165db2633
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 9 additions and 14 deletions

View File

@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"compress/gzip" "compress/gzip"
"compress/zlib" "compress/zlib"
"context"
"io" "io"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
@ -115,31 +116,25 @@ func normalizeResponseBody(resp *http.Response, response *redirectedResponse) er
// dump creates a dump of the http request in form of a byte slice // dump creates a dump of the http request in form of a byte slice
func dump(req *generatedRequest, reqURL string) ([]byte, error) { func dump(req *generatedRequest, reqURL string) ([]byte, error) {
if req.request != nil { if req.request != nil {
cloned := req.request.Clone(context.Background())
// Create a copy on the fly of the request body - ignore errors // Create a copy on the fly of the request body - ignore errors
bodyBytes, _ := req.request.BodyBytes() bodyBytes, _ := req.request.BodyBytes()
var dumpBody bool var dumpBody bool
if len(bodyBytes) > 0 { if len(bodyBytes) > 0 {
dumpBody = true dumpBody = true
req.request.Request.ContentLength = int64(len(bodyBytes)) cloned.ContentLength = int64(len(bodyBytes))
req.request.Request.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes)) cloned.Body = ioutil.NopCloser(bytes.NewReader(bodyBytes))
} else { } else {
req.request.Request.ContentLength = 0 cloned.ContentLength = 0
req.request.Request.Body = nil cloned.Body = nil
delete(req.request.Request.Header, "Content-length") delete(cloned.Header, "Content-length")
} }
dumpBytes, err := httputil.DumpRequestOut(req.request.Request, dumpBody) dumpBytes, err := httputil.DumpRequestOut(cloned, dumpBody)
if err != nil { if err != nil {
return nil, err return nil, err
} }
// The original req.Body gets modified indirectly by httputil.DumpRequestOut so we set it again to nil if it was empty
// Otherwise redirects like 307/308 would fail (as they require the body to be sent along)
if len(bodyBytes) == 0 {
req.request.Request.ContentLength = 0
req.request.Request.Body = nil
}
return dumpBytes, nil return dumpBytes, nil
} }
rawHttpOptions := &rawhttp.Options{CustomHeaders: req.rawRequest.UnsafeHeaders, CustomRawBytes: req.rawRequest.UnsafeRawBytes} rawHttpOptions := &rawhttp.Options{CustomHeaders: req.rawRequest.UnsafeHeaders, CustomRawBytes: req.rawRequest.UnsafeRawBytes}