zrok/endpoints/util.go
2023-01-10 14:52:28 -05:00

67 lines
1.5 KiB
Go

package endpoints
import (
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/edge"
"github.com/sirupsen/logrus"
"net/url"
"strings"
"time"
)
type BackendHandler interface {
Requests() func() int32
}
type BackendRequest struct {
Stamp time.Time
RemoteAddr string
Method string
Path string
}
func GetRefreshedService(name string, ctx ziti.Context) (*edge.Service, bool) {
svc, found := ctx.GetService(name)
if !found {
if err := ctx.RefreshServices(); err != nil {
logrus.Errorf("error refreshing services: %v", err)
return nil, false
}
return ctx.GetService(name)
}
return svc, found
}
func JoinURLPath(a, b *url.URL) (path, rawpath string) {
if a.RawPath == "" && b.RawPath == "" {
return singleJoiningSlash(a.Path, b.Path), ""
}
// Same as singleJoiningSlash, but uses EscapedPath to determine
// whether a slash should be added
apath := a.EscapedPath()
bpath := b.EscapedPath()
aslash := strings.HasSuffix(apath, "/")
bslash := strings.HasPrefix(bpath, "/")
switch {
case aslash && bslash:
return a.Path + b.Path[1:], apath + bpath[1:]
case !aslash && !bslash:
return a.Path + "/" + b.Path, apath + "/" + bpath
}
return a.Path + b.Path, apath + bpath
}
func singleJoiningSlash(a, b string) string {
aslash := strings.HasSuffix(a, "/")
bslash := strings.HasPrefix(b, "/")
switch {
case aslash && bslash:
return a + b[1:]
case !aslash && !bslash:
return a + "/" + b
}
return a + b
}