diff --git a/v2/pkg/protocols/http/build_request.go b/v2/pkg/protocols/http/build_request.go index 040cc7978..1137f3904 100644 --- a/v2/pkg/protocols/http/build_request.go +++ b/v2/pkg/protocols/http/build_request.go @@ -114,7 +114,11 @@ func (r *requestGenerator) makeHTTPRequestFromModel(ctx context.Context, data st // makeHTTPRequestFromRaw creates a *http.Request from a raw request func (r *requestGenerator) makeHTTPRequestFromRaw(ctx context.Context, baseURL, data string, values, payloads map[string]interface{}) (*generatedRequest, error) { - data += "\r\n" + // add "\r\n" only to RCF compliant requests without body + if !rawHasBody(data) { + data += "\r\n" + } + return r.handleRawWithPaylods(ctx, data, baseURL, values, payloads) } diff --git a/v2/pkg/protocols/http/utils.go b/v2/pkg/protocols/http/utils.go index 6ad3f6032..45adf5f9f 100644 --- a/v2/pkg/protocols/http/utils.go +++ b/v2/pkg/protocols/http/utils.go @@ -1,8 +1,10 @@ package http import ( + "bufio" "bytes" "compress/gzip" + "io" "io/ioutil" "net/http" "net/http/httputil" @@ -116,3 +118,27 @@ func handleDecompression(resp *http.Response, bodyOrig []byte) (bodyDec []byte, } return bodyOrig, nil } + +// rawHasBody checks if a RFC compliant request has the body +func rawHasBody(data string) bool { + b := bufio.NewReader(strings.NewReader(data)) + req, err := http.ReadRequest(b) + if err == io.EOF { + return false + } + if err != nil { + return false + } + + if req.Body == http.NoBody { + return false + } + + // It's enough to read a chunk to check the presence of the body + body, err := ioutil.ReadAll(io.LimitReader(req.Body, 512)) + if err != nil { + return false + } + + return len(body) > 0 +}