Skip to content

Commit a170588

Browse files
authored
Merge pull request #100 from aykhans/refactor/config
🔨 Add 'User-Agent' in 'SetDefaults' function
2 parents 11bb8b3 + 2a0ac39 commit a170588

File tree

4 files changed

+19
-4
lines changed

4 files changed

+19
-4
lines changed

config/cli.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ Flags:
4545
-m, -method string HTTP Method for the request (default %s)
4646
-b, -body [string] Body for the request (e.g. "body text")
4747
-p, -param [string] Parameter for the request (e.g. "key1=value1")
48-
-H, -header [string] Header for the request (e.g. "key1: value1")
48+
-H, -header [string] Header for the request (e.g. "key1:value1")
4949
-c, -cookie [string] Cookie for the request (e.g. "key1=value1")
5050
-x, -proxy [string] Proxy for the request (e.g. "http://proxy.example.com:8080")`
5151

config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,4 +233,5 @@ func (config *Config) SetDefaults() {
233233
if config.Yes == nil {
234234
config.Yes = utils.ToPtr(DefaultYes)
235235
}
236+
config.Headers.SetIfNotExists("User-Agent", DefaultUserAgent)
236237
}

requests/request.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,6 @@ func setRequestHeaders(req *fasthttp.Request, headers []types.KeyValue[string, s
166166
for _, header := range headers {
167167
req.Header.Add(header.Key, header.Value)
168168
}
169-
if req.Header.UserAgent() == nil {
170-
req.Header.SetUserAgent(config.DefaultUserAgent)
171-
}
172169
}
173170

174171
// setRequestCookies adds the cookies of the given request with the provided key-value pairs.

types/headers.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ func (headers Headers) GetValue(key string) *[]string {
7373
return nil
7474
}
7575

76+
func (headers Headers) Has(key string) bool {
77+
for i := range headers {
78+
if headers[i].Key == key {
79+
return true
80+
}
81+
}
82+
return false
83+
}
84+
7685
func (headers *Headers) UnmarshalJSON(b []byte) error {
7786
var data []map[string]any
7887
if err := json.Unmarshal(b, &data); err != nil {
@@ -137,3 +146,11 @@ func (headers *Headers) Set(value string) error {
137146

138147
return nil
139148
}
149+
150+
func (headers *Headers) SetIfNotExists(key string, value string) bool {
151+
if headers.Has(key) {
152+
return false
153+
}
154+
*headers = append(*headers, KeyValue[string, []string]{Key: key, Value: []string{value}})
155+
return true
156+
}

0 commit comments

Comments
 (0)