Skip to content

Commit

Permalink
rrequest now adapts body size accordinly to Content-Length
Browse files Browse the repository at this point in the history
  • Loading branch information
SakiiR committed Nov 8, 2019
1 parent 5c9744b commit 38c4dcc
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 11 deletions.
22 changes: 21 additions & 1 deletion internal/pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"bufio"
"bytes"
"fmt"
"io"
"io/ioutil"
"net/http"
"net/http/httputil"
"strconv"

"github.com/sirupsen/logrus"

Expand Down Expand Up @@ -34,12 +36,17 @@ func (parser *Parser) Init() error {

body, err := ioutil.ReadAll(buf)
if err != nil {
logrus.Warn("Failed to read body: '%s'", err)
logrus.Warn("Failed to read body: '", err)
return err
}

req.Body = ioutil.NopCloser(bytes.NewBuffer(body))

// Fix Body Size
bodySize := getBodySize(req, body)
req.Header.Set("Content-Length", strconv.FormatInt(bodySize, 10)) //len(dec)
req.ContentLength = bodySize

parser.Body = body
req.RequestURI = ""
req.URL.Scheme = "http"
Expand All @@ -62,6 +69,19 @@ func (parser *Parser) Init() error {
return nil
}

func getBodySize(request *http.Request, body []byte) int64 {
buf := &bytes.Buffer{}
nRead, err := io.Copy(buf, request.Body)
if err != nil {
logrus.Warn("Failed to copy body buffer: ", err)
return 0
}

request.Body = ioutil.NopCloser(bytes.NewBuffer(body))

return nRead
}

// Do sends the request via the parser HTTP Client and return the response
func (parser *Parser) Do(request *http.Request) (*http.Response, error) {
resp, err := parser.Client.Do(request)
Expand Down
23 changes: 13 additions & 10 deletions internal/pkg/reducer/reducer.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,20 @@ func reduceHeaders(request *http.Request, parser *parser.Parser) http.Request {
headers := request.Header
for key, values := range headers {

request.Header.Del(key)

status, _ := validateResponse(parser.InitialResponse, request, parser)
if status == true {
logrus.Info(fmt.Sprintf("Ok, header %s hasn't impact on response", key))
} else {

logrus.Info(fmt.Sprintf("Ok, header %s has impact on response", key))
for _, value := range values {
request.Header.Add(key, value)
if key != "Content-Length" {
request.Header.Del(key)

status, _ := validateResponse(parser.InitialResponse, request, parser)
if status == true {
logrus.Info(fmt.Sprintf("Ok, header %s hasn't impact on response", key))
} else {

logrus.Info(fmt.Sprintf("Ok, header %s has impact on response", key))
for _, value := range values {
request.Header.Add(key, value)
}
}

}

}
Expand Down

0 comments on commit 38c4dcc

Please sign in to comment.