2020-05-07 11:36:32 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-02-11 11:40:59 +01:00
|
|
|
"fmt"
|
2020-05-07 11:36:32 +02:00
|
|
|
"net/url"
|
|
|
|
"path"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2021-04-21 09:55:29 +02:00
|
|
|
func mergeURLs(uiURL, requestURI *url.URL) *url.URL {
|
|
|
|
targetURL := *uiURL
|
|
|
|
targetURL.Path += requestURI.Path
|
2021-04-20 09:51:03 +02:00
|
|
|
requestParams := requestURI.Query()
|
|
|
|
// fast path
|
|
|
|
if len(requestParams) == 0 {
|
2021-04-21 09:55:29 +02:00
|
|
|
return &targetURL
|
2021-04-20 09:51:03 +02:00
|
|
|
}
|
|
|
|
// merge query parameters from requests.
|
2021-04-21 09:55:29 +02:00
|
|
|
uiParams := targetURL.Query()
|
2021-04-20 09:51:03 +02:00
|
|
|
for k, v := range requestParams {
|
|
|
|
// skip clashed query params from original request
|
2021-04-21 09:55:29 +02:00
|
|
|
if exist := uiParams.Get(k); len(exist) > 0 {
|
2021-04-20 09:51:03 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
for i := range v {
|
2021-04-21 09:55:29 +02:00
|
|
|
uiParams.Add(k, v[i])
|
2021-04-20 09:51:03 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-21 09:55:29 +02:00
|
|
|
targetURL.RawQuery = uiParams.Encode()
|
|
|
|
return &targetURL
|
2021-04-20 09:51:03 +02:00
|
|
|
}
|
|
|
|
|
2023-02-11 09:27:40 +01:00
|
|
|
func (ui *UserInfo) getURLPrefixAndHeaders(u *url.URL) (*URLPrefix, []Header, error) {
|
2023-02-10 06:05:13 +01:00
|
|
|
for _, e := range ui.URLMaps {
|
|
|
|
for _, sp := range e.SrcPaths {
|
|
|
|
if sp.match(u.Path) {
|
|
|
|
return e.URLPrefix, e.Headers, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ui.URLPrefix != nil {
|
|
|
|
return ui.URLPrefix, ui.Headers, nil
|
|
|
|
}
|
|
|
|
missingRouteRequests.Inc()
|
|
|
|
return nil, nil, fmt.Errorf("missing route for %q", u.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeURL(uOrig *url.URL) *url.URL {
|
2021-04-21 09:55:29 +02:00
|
|
|
u := *uOrig
|
2020-05-07 11:36:32 +02:00
|
|
|
// Prevent from attacks with using `..` in r.URL.Path
|
|
|
|
u.Path = path.Clean(u.Path)
|
2022-10-01 15:52:27 +02:00
|
|
|
if !strings.HasSuffix(u.Path, "/") && strings.HasSuffix(uOrig.Path, "/") {
|
|
|
|
// The path.Clean() removes traling slash.
|
|
|
|
// Return it back if needed.
|
|
|
|
// This should fix https://github.com/VictoriaMetrics/VictoriaMetrics/issues/1752
|
|
|
|
u.Path += "/"
|
|
|
|
}
|
2020-05-07 11:36:32 +02:00
|
|
|
if !strings.HasPrefix(u.Path, "/") {
|
|
|
|
u.Path = "/" + u.Path
|
|
|
|
}
|
2022-10-01 15:52:27 +02:00
|
|
|
if u.Path == "/" {
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/pull/1554
|
|
|
|
u.Path = ""
|
|
|
|
}
|
2023-02-10 06:05:13 +01:00
|
|
|
return &u
|
2020-05-07 11:36:32 +02:00
|
|
|
}
|