Skip to content

Commit

Permalink
add additional header support to url-tool
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Aug 21, 2023
1 parent 9b98acc commit da7f393
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions cmd/url-tool/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package main
import (
"errors"
"fmt"
"net/http"
"net/url"
"os"
"strings"
Expand All @@ -19,8 +20,9 @@ import (

// EncodeCommand holds command options for the encode command
type EncodeCommand struct {
Base string `short:"b" long:"base" default:"hex" description:"Encode/Decode base. Either hex or base64"`
Prefix string `short:"p" long:"prefix" default:"" description:"Optional url prefix used by encode output"`
Base string `short:"b" long:"base" default:"hex" description:"Encode/Decode base. Either hex or base64"`
Prefix string `short:"p" long:"prefix" default:"" description:"Optional url prefix used by encode output"`
AddHeaders map[string]string `short:"H" long:"header" default:"" description:"Add an additional header to the encoded url. May be supplied multiple times. Requires camo additional headers support."`
Positional struct {
Url string `positional-arg-name:"URL"`
} `positional-args:"yes" required:"true"`
Expand All @@ -36,14 +38,21 @@ func (c *EncodeCommand) Execute(args []string) error {
return errors.New("no url argument provided")
}

addHeaders := http.Header{}
if len(c.AddHeaders) > 0 {
for k, v := range c.AddHeaders {
addHeaders.Add(k, v)
}
}

hmacKeyBytes := []byte(opts.HmacKey)
var outURL string
var err error
switch c.Base {
case "base64":
outURL, err = encoding.B64EncodeURL(hmacKeyBytes, c.Positional.Url, nil)
outURL, err = encoding.B64EncodeURL(hmacKeyBytes, c.Positional.Url, addHeaders)
case "hex":
outURL, err = encoding.HexEncodeURL(hmacKeyBytes, c.Positional.Url, nil)
outURL, err = encoding.HexEncodeURL(hmacKeyBytes, c.Positional.Url, addHeaders)
default:
return errors.New("invalid base provided")
}
Expand Down

0 comments on commit da7f393

Please sign in to comment.