-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
51 lines (43 loc) · 937 Bytes
/
util.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package fasthttp2curl
import (
"fmt"
"io"
"io/ioutil"
"net/http"
"sort"
"strings"
)
func getBaseCommand() *CurlCommand {
command := CurlCommand{}
command.Append("curl")
return &command
}
// add body to curl
func (command *CurlCommand) addBody(inBody io.ReadCloser) error {
if inBody != nil {
body, err := ioutil.ReadAll(inBody)
if err != nil {
return err
}
command.addBodyBytes(body)
}
return nil
}
// add body to curl from bytes
func (command *CurlCommand) addBodyBytes(body []byte) {
if len(string(body)) > 0 {
bodyEscaped := bashEscape(string(body))
command.Append("-d", bodyEscaped)
}
}
// add body to curl from bytes
func (command *CurlCommand) addHeaders(headers http.Header) {
var keys []string
for k := range headers {
keys = append(keys, k)
}
sort.Strings(keys)
for _, k := range keys {
command.Append("-H", bashEscape(fmt.Sprintf("%s: %s", k, strings.Join(headers[k], " "))))
}
}