Skip to content

Commit

Permalink
refactor: bring PR #884 into v3 branch, cleanup and improve
Browse files Browse the repository at this point in the history
Co-authored-by: yaziedda <[email protected]>
  • Loading branch information
jeevatkm and yaziedda-la committed Nov 2, 2024
1 parent f50470e commit f61ae03
Show file tree
Hide file tree
Showing 8 changed files with 253 additions and 212 deletions.
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,9 @@ func (c *Client) CookieJar() http.CookieJar {
//
// client.SetCookieJar(nil)
func (c *Client) SetCookieJar(jar http.CookieJar) *Client {
c.Client().Jar = jar
c.lock.Lock()
defer c.lock.Unlock()
c.httpClient.Jar = jar
return c
}

Expand Down
37 changes: 28 additions & 9 deletions util_curl.go → curl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,44 @@ import (
"bytes"
"io"
"net/http"
"regexp"

"net/url"
"strings"

"github.com/go-resty/resty/v3/shellescape"
)

func buildCurlRequest(req *Request) (curl string) {
func buildCurlCmd(req *Request) string {
// 1. Generate curl raw headers
curl = "curl -X " + req.Method + " "
var curl = "curl -X " + req.Method + " "
headers := dumpCurlHeaders(req.RawRequest)
for _, kv := range *headers {
curl += "-H " + shellescape.Quote(kv[0]+": "+kv[1]) + " "
curl += "-H " + cmdQuote(kv[0]+": "+kv[1]) + " "
}

// 2. Generate curl cookies
// TODO validate this block of code, I think its not required since cookie captured via Headers
if cookieJar := req.client.CookieJar(); cookieJar != nil {
if cookies := cookieJar.Cookies(req.RawRequest.URL); len(cookies) > 0 {
curl += " -H " + shellescape.Quote(dumpCurlCookies(cookies)) + " "
curl += "-H " + cmdQuote(dumpCurlCookies(cookies)) + " "
}
}

// 3. Generate curl body
if req.RawRequest.GetBody != nil {
body, err := req.RawRequest.GetBody()
if err != nil {
req.log.Errorf("curl: %v", err)
return ""
}
buf, _ := io.ReadAll(body)
curl += "-d " + shellescape.Quote(string(bytes.TrimRight(buf, "\n")))
curl += "-d " + cmdQuote(string(bytes.TrimRight(buf, "\n"))) + " "
}

urlString := shellescape.Quote(req.RawRequest.URL.String())
urlString := cmdQuote(req.RawRequest.URL.String())
if urlString == "''" {
urlString = "'http://unexecuted-request'"
}
curl += " " + urlString
curl += urlString
return curl
}

Expand Down Expand Up @@ -75,3 +75,22 @@ func dumpCurlHeaders(req *http.Request) *[][2]string {
}
return &headers
}

var regexCmdQuote = regexp.MustCompile(`[^\w@%+=:,./-]`)

// cmdQuote method to escape arbitrary strings for a safe use as
// command line arguments in the most common POSIX shells.
//
// The original Python package which this work was inspired by can be found
// at https://pypi.python.org/pypi/shellescape.
func cmdQuote(s string) string {
if len(s) == 0 {
return "''"
}

if regexCmdQuote.MatchString(s) {
return "'" + strings.ReplaceAll(s, "'", "'\"'\"'") + "'"
}

return s
}
146 changes: 0 additions & 146 deletions curl_cmd_test.go

This file was deleted.

Loading

0 comments on commit f61ae03

Please sign in to comment.