| 1 | package http2curl |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "fmt" |
| 6 | "io/ioutil" |
| 7 | "net/http" |
| 8 | "sort" |
| 9 | "strings" |
| 10 | ) |
| 11 | |
| 12 | // CurlCommand contains exec.Command compatible slice + helpers |
| 13 | type CurlCommand []string |
| 14 | |
| 15 | // append appends a string to the CurlCommand |
| 16 | func (c *CurlCommand) append(newSlice ...string) { |
| 17 | *c = append(*c, newSlice...) |
| 18 | } |
| 19 | |
| 20 | // String returns a ready to copy/paste command |
| 21 | func (c *CurlCommand) String() string { |
| 22 | return strings.Join(*c, " ") |
| 23 | } |
| 24 | |
| 25 | func bashEscape(str string) string { |
| 26 | return `'` + strings.Replace(str, `'`, `'\''`, -1) + `'` |
| 27 | } |
| 28 | |
| 29 | // GetCurlCommand returns a CurlCommand corresponding to an http.Request |
| 30 | func GetCurlCommand(req *http.Request) (*CurlCommand, error) { |
| 31 | if req.URL == nil { |
| 32 | return nil, fmt.Errorf("getCurlCommand: invalid request, req.URL is nil") |
| 33 | } |
| 34 | |
| 35 | command := CurlCommand{} |
| 36 | |
| 37 | command.append("curl") |
| 38 | |
| 39 | schema := req.URL.Scheme |
| 40 | requestURL := req.URL.String() |
| 41 | if schema == "" { |
| 42 | schema = "http" |
| 43 | if req.TLS != nil { |
| 44 | schema = "https" |
| 45 | } |
| 46 | requestURL = schema + "://" + req.Host + req.URL.Path |
| 47 | } |
| 48 | |
| 49 | if schema == "https" { |
| 50 | command.append("-k") |
| 51 | } |
| 52 | |
| 53 | command.append("-X", bashEscape(req.Method)) |
| 54 | |
| 55 | if req.Body != nil { |
| 56 | var buff bytes.Buffer |
| 57 | _, err := buff.ReadFrom(req.Body) |
| 58 | if err != nil { |
| 59 | return nil, fmt.Errorf("getCurlCommand: buffer read from body error: %w", err) |
| 60 | } |
| 61 | // reset body for potential re-reads |
| 62 | req.Body = ioutil.NopCloser(bytes.NewBuffer(buff.Bytes())) |
| 63 | if len(buff.String()) > 0 { |
| 64 | bodyEscaped := bashEscape(buff.String()) |
| 65 | command.append("-d", bodyEscaped) |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | var keys []string |
| 70 | |
| 71 | for k := range req.Header { |
| 72 | keys = append(keys, k) |
| 73 | } |
| 74 | sort.Strings(keys) |
| 75 | |
| 76 | for _, k := range keys { |
| 77 | command.append("-H", bashEscape(fmt.Sprintf("%s: %s", k, strings.Join(req.Header[k], " ")))) |
| 78 | } |
| 79 | |
| 80 | command.append(bashEscape(requestURL)) |
| 81 | |
| 82 | return &command, nil |
| 83 | } |
| 84 |