Skip to content

Commit

Permalink
Add support for custom headers
Browse files Browse the repository at this point in the history
This is potentially useful in a proxied linx-server setup. Perhaps you
have an instance that requires a particular Authorization header being
set that is processed by some middleware...now you can set that header
without modifying the code!
  • Loading branch information
mutantmonkey committed Aug 21, 2019
1 parent 84e78ec commit bec40c6
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions linx.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type Config struct {
ApiKey string
Proxy string
UploadLog string
Headers []string
}

type LinxJSON struct {
Expand Down Expand Up @@ -101,6 +102,11 @@ func linx(config *Config, filename string, size int64, f io.Reader, ttl int, del
req.Header.Add("Linx-Expiry", strconv.Itoa(ttl))
req.Header.Add("Linx-Randomize", "yes")

for _, header := range config.Headers {
h := strings.SplitN(header, ": ", 2)
req.Header.Add(h[0], h[1])
}

if config.ApiKey != "" {
req.Header.Set("Linx-Api-Key", config.ApiKey)
}
Expand Down Expand Up @@ -174,6 +180,11 @@ func unlinx(config *Config, url string, deleteKey string) bool {
req.Header.Add("User-Agent", "golinx")
req.Header.Add("Linx-Delete-Key", deleteKey)

for _, header := range config.Headers {
h := strings.SplitN(header, ": ", 2)
req.Header.Add(h[0], h[1])
}

if config.ApiKey != "" {
req.Header.Set("Linx-Api-Key", config.ApiKey)
}
Expand All @@ -193,6 +204,18 @@ func unlinx(config *Config, url string, deleteKey string) bool {
}
}

// Type used for passing multiple parameters using the same flag
type multiStringFlag []string

func (m *multiStringFlag) String() string {
return "[" + strings.Join(*m, " ") + "]"
}

func (m *multiStringFlag) Set(value string) error {
*m = append(*m, value)
return nil
}

func main() {
config := &Config{}
var flags struct {
Expand All @@ -205,6 +228,7 @@ func main() {
proxy string
uploadLog string
makeCollection bool
headers multiStringFlag
}

defaultConfigPath, err := xdg.ConfigFile("golinx/config.yml")
Expand All @@ -231,6 +255,8 @@ func main() {
"Path to the upload log file")
flag.BoolVar(&flags.makeCollection, "collection", false,
"Create a collection when uploading multiple files")
flag.Var(&flags.headers, "H",
"Headers to add to the request; multiple -H flags are accepted")
flag.Parse()

if flags.configPath != "" {
Expand Down Expand Up @@ -258,6 +284,10 @@ func main() {
config.UploadLog = flags.uploadLog
}

if len(flags.headers) > 0 {
config.Headers = append(config.Headers, flags.headers...)
}

if config.Server == "" {
log.Fatal("Required option server not specified in config or as a flag")
}
Expand Down

0 comments on commit bec40c6

Please sign in to comment.