From 4d800d8c0c6666a32e1bcf60686dead78539ee6a Mon Sep 17 00:00:00 2001 From: Ice3man543 Date: Tue, 12 Jan 2021 02:00:11 +0530 Subject: [PATCH] Fixed bugs with progress and http path / handling --- v2/pkg/protocols/common/executer/executer.go | 2 +- v2/pkg/protocols/http/build_request.go | 20 +++++++++----------- v2/pkg/protocols/http/http.go | 19 +++++++++++++++---- v2/pkg/protocols/http/raw/raw.go | 7 +++---- v2/pkg/protocols/http/request.go | 4 ++-- v2/pkg/templates/compile.go | 2 +- 6 files changed, 31 insertions(+), 23 deletions(-) diff --git a/v2/pkg/protocols/common/executer/executer.go b/v2/pkg/protocols/common/executer/executer.go index 09bccca9..12f8a365 100644 --- a/v2/pkg/protocols/common/executer/executer.go +++ b/v2/pkg/protocols/common/executer/executer.go @@ -33,7 +33,7 @@ func (e *Executer) Compile() error { func (e *Executer) Requests() int { var count int for _, request := range e.requests { - count += int(request.Requests()) + count += request.Requests() } return count } diff --git a/v2/pkg/protocols/http/build_request.go b/v2/pkg/protocols/http/build_request.go index 0b8d21ff..b2c515ba 100644 --- a/v2/pkg/protocols/http/build_request.go +++ b/v2/pkg/protocols/http/build_request.go @@ -111,9 +111,6 @@ func (r *requestGenerator) Make(baseURL string, dynamicValues map[string]interfa } ctx := context.Background() - if strings.HasSuffix(baseURL, "/") { - baseURL = strings.TrimSuffix(baseURL, "/") - } parsed, err := url.Parse(baseURL) if err != nil { return nil, err @@ -158,6 +155,9 @@ func baseURLWithTemplatePrefs(data string, parsedURL *url.URL) string { // MakeHTTPRequestFromModel creates a *http.Request from a request template func (r *requestGenerator) makeHTTPRequestFromModel(ctx context.Context, data string, values map[string]interface{}) (*generatedRequest, error) { + if strings.HasSuffix(values["BaseURL"].(string), "/") { + data = strings.TrimPrefix(data, "/") + } URL := replacer.New(values).Replace(data) // Build a request on the specified URL @@ -190,24 +190,22 @@ func (r *requestGenerator) makeHTTPRequestFromRaw(ctx context.Context, baseURL, } // handleRawWithPaylods handles raw requests along with paylaods -func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest, baseURL string, values, genValues map[string]interface{}) (*generatedRequest, error) { +func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest, baseURL string, values, generatorValues map[string]interface{}) (*generatedRequest, error) { baseValues := generators.CopyMap(values) - finValues := generators.MergeMaps(baseValues, genValues) + finalValues := generators.MergeMaps(baseValues, generatorValues) // Replace the dynamic variables in the URL if any - rawRequest = replacer.New(finValues).Replace(rawRequest) + rawRequest = replacer.New(finalValues).Replace(rawRequest) dynamicValues := make(map[string]interface{}) for _, match := range templateExpressionRegex.FindAllString(rawRequest, -1) { // check if the match contains a dynamic variable expr := generators.TrimDelimiters(match) compiled, err := govaluate.NewEvaluableExpressionWithFunctions(expr, dsl.HelperFunctions()) - if err != nil { return nil, err } - - result, err := compiled.Evaluate(finValues) + result, err := compiled.Evaluate(finalValues) if err != nil { return nil, err } @@ -223,7 +221,7 @@ func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest, // rawhttp if r.request.Unsafe { - unsafeReq := &generatedRequest{rawRequest: rawRequestData, meta: genValues, original: r.request} + unsafeReq := &generatedRequest{rawRequest: rawRequestData, meta: generatorValues, original: r.request} return unsafeReq, nil } @@ -250,7 +248,7 @@ func (r *requestGenerator) handleRawWithPaylods(ctx context.Context, rawRequest, if err != nil { return nil, err } - return &generatedRequest{request: request, meta: genValues, original: r.request}, nil + return &generatedRequest{request: request, meta: generatorValues, original: r.request}, nil } // fillRequest fills various headers in the request with values diff --git a/v2/pkg/protocols/http/http.go b/v2/pkg/protocols/http/http.go index 40c8757b..d1e71476 100644 --- a/v2/pkg/protocols/http/http.go +++ b/v2/pkg/protocols/http/http.go @@ -114,11 +114,22 @@ func (r *Request) Compile(options *protocols.ExecuterOptions) error { // Requests returns the total number of requests the YAML rule will perform func (r *Request) Requests() int { if r.generator != nil { - payloadRequests := r.generator.NewIterator().Total() - return len(r.Raw) * payloadRequests + payloadRequests := r.generator.NewIterator().Total() * len(r.Raw) + if r.Threads != 0 { + payloadRequests = payloadRequests * r.Threads + } + return payloadRequests } if len(r.Raw) > 0 { - return len(r.Raw) + requests := len(r.Raw) + if r.Threads != 0 { + requests = requests * r.Threads + } + return requests } - return len(r.Path) + requests := len(r.Path) + if r.Threads != 0 { + requests = requests * r.Threads + } + return requests } diff --git a/v2/pkg/protocols/http/raw/raw.go b/v2/pkg/protocols/http/raw/raw.go index c14bf399..53401f55 100644 --- a/v2/pkg/protocols/http/raw/raw.go +++ b/v2/pkg/protocols/http/raw/raw.go @@ -94,12 +94,11 @@ func Parse(request, baseURL string, unsafe bool) (*Request, error) { if rawRequest.Path == "" { rawRequest.Path = parsedURL.Path } else if strings.HasPrefix(rawRequest.Path, "?") { - // requests generated from http.ReadRequest have incorrect RequestURI, so they - // cannot be used to perform another request directly, we need to generate a new one - // with the new target url rawRequest.Path = fmt.Sprintf("%s%s", parsedURL.Path, rawRequest.Path) } - + if strings.HasSuffix(baseURL, "/") { + rawRequest.Path = strings.TrimPrefix(rawRequest.Path, "/") + } rawRequest.FullURL = fmt.Sprintf("%s://%s%s", parsedURL.Scheme, strings.TrimSpace(hostURL), rawRequest.Path) // Set the request body diff --git a/v2/pkg/protocols/http/request.go b/v2/pkg/protocols/http/request.go index 38d4653f..903d0839 100644 --- a/v2/pkg/protocols/http/request.go +++ b/v2/pkg/protocols/http/request.go @@ -264,8 +264,8 @@ func (r *Request) executeRequest(reqURL string, request *generatedRequest, dynam } } if err != nil { - if resp != nil { - _, _ = io.Copy(ioutil.Discard, resp.Body) + if resp != nil && resp.Body != nil { + // _, _ = io.Copy(ioutil.Discard, resp.Body) resp.Body.Close() } r.options.Output.Request(r.options.TemplateID, reqURL, "http", err) diff --git a/v2/pkg/templates/compile.go b/v2/pkg/templates/compile.go index 132385e9..6d77e40c 100644 --- a/v2/pkg/templates/compile.go +++ b/v2/pkg/templates/compile.go @@ -74,11 +74,11 @@ func Parse(filePath string, options *protocols.ExecuterOptions) (*Template, erro } if template.Executer != nil { - template.TotalRequests += template.Executer.Requests() err := template.Executer.Compile() if err != nil { return nil, errors.Wrap(err, "could not compile request") } + template.TotalRequests += template.Executer.Requests() } return template, nil }