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-05-28 22:00:23 +00:00
|
|
|
func (up *URLPrefix) mergeURLs(requestURI *url.URL) *url.URL {
|
|
|
|
pu := up.getNextURL()
|
|
|
|
return mergeURLs(pu, requestURI)
|
|
|
|
}
|
|
|
|
|
2021-04-21 07:55:29 +00:00
|
|
|
func mergeURLs(uiURL, requestURI *url.URL) *url.URL {
|
|
|
|
targetURL := *uiURL
|
|
|
|
targetURL.Path += requestURI.Path
|
2021-04-20 07:51:03 +00:00
|
|
|
requestParams := requestURI.Query()
|
|
|
|
// fast path
|
|
|
|
if len(requestParams) == 0 {
|
2021-04-21 07:55:29 +00:00
|
|
|
return &targetURL
|
2021-04-20 07:51:03 +00:00
|
|
|
}
|
|
|
|
// merge query parameters from requests.
|
2021-04-21 07:55:29 +00:00
|
|
|
uiParams := targetURL.Query()
|
2021-04-20 07:51:03 +00:00
|
|
|
for k, v := range requestParams {
|
|
|
|
// skip clashed query params from original request
|
2021-04-21 07:55:29 +00:00
|
|
|
if exist := uiParams.Get(k); len(exist) > 0 {
|
2021-04-20 07:51:03 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := range v {
|
2021-04-21 07:55:29 +00:00
|
|
|
uiParams.Add(k, v[i])
|
2021-04-20 07:51:03 +00:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
targetURL.RawQuery = uiParams.Encode()
|
|
|
|
return &targetURL
|
2021-04-20 07:51:03 +00:00
|
|
|
}
|
|
|
|
|
2021-04-21 07:55:29 +00:00
|
|
|
func createTargetURL(ui *UserInfo, uOrig *url.URL) (*url.URL, error) {
|
|
|
|
u := *uOrig
|
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-08-25 10:28:50 +00:00
|
|
|
u.Path = strings.TrimSuffix(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-05-28 22:00:23 +00:00
|
|
|
return e.URLPrefix.mergeURLs(&u), nil
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
if ui.URLPrefix != nil {
|
2021-05-28 22:00:23 +00:00
|
|
|
return ui.URLPrefix.mergeURLs(&u), nil
|
2021-02-11 10:40:59 +00:00
|
|
|
}
|
2021-04-21 07:55:29 +00:00
|
|
|
return nil, fmt.Errorf("missing route for %q", u.String())
|
2020-05-07 09:36:32 +00:00
|
|
|
}
|