@@ -5,18 +5,19 @@ import (
5
5
"encoding/base64"
6
6
"errors"
7
7
"fmt"
8
- "log"
9
8
"net/http"
10
9
"regexp"
11
10
"strings"
12
11
13
- "github.com/alecthomas/kingpin"
12
+ "github.com/alecthomas/kingpin/v2 "
14
13
"github.com/vearutop/plt/fasthttp"
15
14
"github.com/vearutop/plt/loadgen"
16
15
"github.com/vearutop/plt/nethttp"
17
16
)
18
17
19
18
// AddCommand registers curl command into CLI app.
19
+ //
20
+ //nolint:maintidx
20
21
func AddCommand (lf * loadgen.Flags , options ... func (lf * loadgen.Flags , f * nethttp.Flags , j loadgen.JobProducer )) {
21
22
var (
22
23
flags nethttp.Flags
@@ -25,6 +26,8 @@ func AddCommand(lf *loadgen.Flags, options ...func(lf *loadgen.Flags, f *nethttp
25
26
data []string
26
27
compressed bool
27
28
user string
29
+ output string
30
+ head bool
28
31
}
29
32
captureStrings = map [string ]* []string {
30
33
"header" : & capture .header ,
@@ -38,11 +41,13 @@ func AddCommand(lf *loadgen.Flags, options ...func(lf *loadgen.Flags, f *nethttp
38
41
"url" : & flags .URL ,
39
42
"request" : & flags .Method ,
40
43
"user" : & capture .user ,
44
+ "output" : & capture .output ,
41
45
}
42
46
captureBool = map [string ]* bool {
43
47
"compressed" : & capture .compressed ,
44
48
"no-keepalive" : & flags .NoKeepalive ,
45
49
"http2" : & flags .HTTP2 ,
50
+ "head" : & capture .head ,
46
51
}
47
52
ignoredString = map [string ]* string {}
48
53
ignoredBool = map [string ]* bool {}
@@ -118,18 +123,21 @@ func AddCommand(lf *loadgen.Flags, options ...func(lf *loadgen.Flags, f *nethttp
118
123
119
124
curl .Action (func (kp * kingpin.ParseContext ) error {
120
125
ignoredFlags := make ([]string , 0 )
126
+
121
127
for f , v := range ignoredString {
122
128
if v != nil && * v != "" {
123
129
ignoredFlags = append (ignoredFlags , f )
124
130
}
125
131
}
132
+
126
133
for f , v := range ignoredBool {
127
134
if v != nil && * v {
128
135
ignoredFlags = append (ignoredFlags , f )
129
136
}
130
137
}
138
+
131
139
if len (ignoredFlags ) > 0 {
132
- log . Printf ( "Warning, these Flags are ignored: %v\n " , ignoredFlags )
140
+ return fmt . Errorf ( " these flags are ignored: %v" , ignoredFlags )
133
141
}
134
142
135
143
if len (capture .data ) == 1 {
@@ -139,13 +147,19 @@ func AddCommand(lf *loadgen.Flags, options ...func(lf *loadgen.Flags, f *nethttp
139
147
}
140
148
141
149
flags .HeaderMap = make (map [string ]string , len (capture .header ))
150
+
142
151
if capture .user != "" {
143
152
if ! strings .Contains (capture .user , ":" ) {
144
153
return errors .New ("user parameter must be in form user:pass" )
145
154
}
155
+
146
156
flags .HeaderMap ["Authorization" ] = "Basic " + base64 .StdEncoding .EncodeToString ([]byte (capture .user ))
147
157
}
148
158
159
+ if capture .head {
160
+ flags .Method = http .MethodHead
161
+ }
162
+
149
163
if flags .Body != "" {
150
164
flags .HeaderMap ["Content-Type" ] = "application/x-www-form-urlencoded"
151
165
@@ -159,13 +173,20 @@ func AddCommand(lf *loadgen.Flags, options ...func(lf *loadgen.Flags, f *nethttp
159
173
if len (parts ) != 2 {
160
174
continue
161
175
}
176
+
162
177
flags .HeaderMap [http .CanonicalHeaderKey (parts [0 ])] = strings .Trim (parts [1 ], " " )
163
178
}
179
+
164
180
if capture .compressed {
165
181
if _ , ok := flags .HeaderMap ["Accept-Encoding" ]; ! ok {
166
182
flags .HeaderMap ["Accept-Encoding" ] = "gzip, deflate"
167
183
}
168
184
}
185
+
186
+ if flags .NoKeepalive && (capture .output == "/dev/null" || capture .output == "nul" ) {
187
+ flags .IgnoreResponseBody = true
188
+ }
189
+
169
190
if ! strings .HasPrefix (strings .ToLower (flags .URL ), "http://" ) &&
170
191
! strings .HasPrefix (strings .ToLower (flags .URL ), "https://" ) {
171
192
flags .URL = "http://" + flags .URL
0 commit comments