2020-05-07 09:36:32 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-11 10:40:59 +00:00
|
|
|
"fmt"
|
2020-05-07 09:36:32 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-04-20 07:51:03 +00:00
|
|
|
func mergeURLs(uiURL string, requestURI *url.URL) (string, error) {
|
|
|
|
prefixURL, err := url.Parse(uiURL)
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("BUG - cannot parse userInfo url: %q, err: %w", uiURL, err)
|
|
|
|
}
|
|
|
|
prefixURL.Path += requestURI.Path
|
|
|
|
requestParams := requestURI.Query()
|
|
|
|
// fast path
|
|
|
|
if len(requestParams) == 0 {
|
|
|
|
return prefixURL.String(), nil
|
|
|
|
}
|
|
|
|
// merge query parameters from requests.
|
|
|
|
userInfoParams := prefixURL.Query()
|
|
|
|
for k, v := range requestParams {
|
|
|
|
// skip clashed query params from original request
|
|
|
|
if exist := userInfoParams.Get(k); len(exist) > 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := range v {
|
|
|
|
userInfoParams.Add(k, v[i])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
prefixURL.RawQuery = userInfoParams.Encode()
|
|
|
|
return prefixURL.String(), nil
|
|
|
|
}
|
|
|
|
|
2021-02-11 10:40:59 +00:00
|
|
|
func createTargetURL(ui *UserInfo, uOrig *url.URL) (string, error) {
|
|
|
|
u, err := url.Parse(uOrig.String())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("cannot make a copy of %q: %w", u, err)
|
|
|
|
}
|
2020-05-07 09:36:32 +00:00
|
|
|
// Prevent from attacks with using `..` in r.URL.Path
|
|
|
|
u.Path = path.Clean(u.Path)
|
|
|
|
if !strings.HasPrefix(u.Path, "/") {
|
|
|
|
u.Path = "/" + u.Path
|
|
|
|
}
|
2021-02-11 10:40:59 +00:00
|
|
|
for _, e := range ui.URLMap {
|
2021-03-05 16:21:11 +00:00
|
|
|
for _, sp := range e.SrcPaths {
|
|
|
|
if sp.match(u.Path) {
|
2021-04-20 07:51:03 +00:00
|
|
|
return mergeURLs(e.URLPrefix, u)
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ui.URLPrefix) > 0 {
|
2021-04-20 07:51:03 +00:00
|
|
|
return mergeURLs(ui.URLPrefix, u)
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
|
|
|
return "", fmt.Errorf("missing route for %q", u)
|
2020-05-07 09:36:32 +00:00
|
|
|
}
|