Skip to content

Commit

Permalink
Do not duplicate Content-Type and Accept headers if provided
Browse files Browse the repository at this point in the history
Fixes: #4
  • Loading branch information
rs committed Dec 2, 2019
1 parent 5dc09b9 commit a5b6653
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
21 changes: 21 additions & 0 deletions args/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,34 @@ func (opts Opts) Has(opt string) bool {
return opts.index(opt) != -1
}

// Val return the value of the first occurance of opt.
func (opts Opts) Val(opt string) string {
if idx := opts.index(opt); idx != -1 && idx+1 < len(opts) {
return opts[idx+1]
}
return ""
}

// Vals return the values of all occurances of opt.
func (opts Opts) Vals(opt string) []string {
var vals []string
off := 1
if len(opt) > 1 {
off = 2
}
for i, o := range opts {
if len(o) >= 2 && o[0] == '-' {
if o[off:] == opt {
if i+1 < len(opts) {
i++
vals = append(vals, opts[i])
}
}
}
}
return vals
}

// Remove removes all occurrences of opt and return true if found.
func (opts *Opts) Remove(opt string) bool {
found := false
Expand Down
18 changes: 16 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,14 @@ func main() {
hasInput = false
}
if hasInput {
opts = append(opts, "-H", "Content-Type: application/json")
if !headerSupplied(opts, "Content-Type") {
opts = append(opts, "-H", "Content-Type: application/json")
}
}
}
opts = append(opts, "-H", "Accept: application/json, */*")
if !headerSupplied(opts, "Accept") {
opts = append(opts, "-H", "Accept: application/json, */*")
}
if opts.Has("curl") {
opts.Remove("curl")
fmt.Print("curl")
Expand Down Expand Up @@ -128,3 +132,13 @@ func main() {
}
os.Exit(status)
}

func headerSupplied(opts args.Opts, header string) bool {
header = strings.ToLower(header)
for _, h := range append(opts.Vals("H"), opts.Vals("header")...) {
if strings.HasPrefix(strings.ToLower(h), header+":") {
return true
}
}
return false
}

0 comments on commit a5b6653

Please sign in to comment.