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-02-11 11:40:59 +01: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 11:36:32 +02: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 11:40:59 +01:00
|
|
|
for _, e := range ui.URLMap {
|
2021-03-05 17:21:11 +01:00
|
|
|
for _, sp := range e.SrcPaths {
|
|
|
|
if sp.match(u.Path) {
|
2021-02-11 11:40:59 +01:00
|
|
|
return e.URLPrefix + u.RequestURI(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(ui.URLPrefix) > 0 {
|
|
|
|
return ui.URLPrefix + u.RequestURI(), nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("missing route for %q", u)
|
2020-05-07 11:36:32 +02:00
|
|
|
}
|