final \r\n with body - Fixes #535

This commit is contained in:
Mzack9999 2021-02-19 22:42:16 +01:00
parent d08cd93838
commit 68b3ef8ffa
2 changed files with 31 additions and 1 deletions

View File

@ -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)
}

View File

@ -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
}