Skip to content

Commit

Permalink
fix: allow query args in http config path
Browse files Browse the repository at this point in the history
  • Loading branch information
tcolgate committed Apr 23, 2020
1 parent c769558 commit 9dd53f2
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
8 changes: 7 additions & 1 deletion config.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,16 @@ func checkModuleConfig(name string, cfg *moduleConfig) error {
if err != nil {
return fmt.Errorf("could not create tls config, %w", err)
}

dirFunc, err := cfg.getReverseProxyDirectorFunc()
if err != nil {
return err
}

cfg.HTTP.tlsConfig = tlsConfig
cfg.HTTP.ReverseProxy = &httputil.ReverseProxy{
Transport: &http.Transport{TLSClientConfig: tlsConfig},
Director: cfg.getReverseProxyDirectorFunc(),
Director: dirFunc,
ErrorHandler: cfg.getReverseProxyErrorHandlerFunc(),
}
if *cfg.HTTP.Verify {
Expand Down
28 changes: 22 additions & 6 deletions http.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ import (
"compress/gzip"
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"net/url"
"strconv"
"strings"

Expand All @@ -46,19 +48,33 @@ type VerifyError struct {
func (e *VerifyError) Error() string { return e.msg + ": " + e.cause.Error() }
func (e *VerifyError) Unwrap() error { return e.cause }

func (cfg moduleConfig) getReverseProxyDirectorFunc() func(*http.Request) {
func (cfg moduleConfig) getReverseProxyDirectorFunc() (func(*http.Request), error) {
base, err := url.Parse(cfg.HTTP.Path)
if err != nil {
return nil, fmt.Errorf("http configuration path should be a valid URL path with options, %w", err)
}

cvs := base.Query()

return func(r *http.Request) {
vs := r.URL.Query()
vs["module"] = vs["module"][1:]
r.URL.RawQuery = vs.Encode()
qvs := r.URL.Query()
for k, vs := range cvs {
for _, v := range vs {
qvs.Add(k, v)
}
}
qvs["module"] = qvs["module"][1:]

r.URL.RawQuery = qvs.Encode()

for k, v := range cfg.HTTP.Headers {
r.Header.Add(k, v)
}

r.URL.Scheme = cfg.HTTP.Scheme
r.URL.Host = net.JoinHostPort(cfg.HTTP.Address, strconv.Itoa(cfg.HTTP.Port))
r.URL.Path = cfg.HTTP.Path
}
r.URL.Path = base.Path
}, nil
}

func (cfg moduleConfig) getReverseProxyModifyResponseFunc() func(*http.Response) error {
Expand Down

0 comments on commit 9dd53f2

Please sign in to comment.