diff --git a/internal/http/services/owncloud/ocdav/net/net.go b/internal/http/services/owncloud/ocdav/net/net.go index 586c8c7cc4..9fab05c963 100644 --- a/internal/http/services/owncloud/ocdav/net/net.go +++ b/internal/http/services/owncloud/ocdav/net/net.go @@ -19,9 +19,7 @@ package net import ( - "fmt" "net/url" - "regexp" "strings" "github.com/pkg/errors" @@ -69,37 +67,11 @@ func (d Depth) String() string { return string(d) } -// replaceAllStringSubmatchFunc is taken from 'Go: Replace String with Regular Expression Callback' -// see: https://elliotchance.medium.com/go-replace-string-with-regular-expression-callback-f89948bad0bb -func replaceAllStringSubmatchFunc(re *regexp.Regexp, str string, repl func([]string) string) string { - result := "" - lastIndex := 0 - for _, v := range re.FindAllSubmatchIndex([]byte(str), -1) { - groups := []string{} - for i := 0; i < len(v); i += 2 { - groups = append(groups, str[v[i]:v[i+1]]) - } - result += str[lastIndex:v[0]] + repl(groups) - lastIndex = v[1] - } - return result + str[lastIndex:] -} - -var hrefre = regexp.MustCompile(`([^A-Za-z0-9_\-.~()/:@!$])`) - // EncodePath encodes the path of a url. // // slashes (/) are treated as path-separators. -// ported from https://github.com/sabre-io/http/blob/bb27d1a8c92217b34e778ee09dcf79d9a2936e84/lib/functions.php#L369-L379 func EncodePath(path string) string { - return replaceAllStringSubmatchFunc(hrefre, path, func(groups []string) string { - b := groups[1] - var sb strings.Builder - for i := 0; i < len(b); i++ { - sb.WriteString(fmt.Sprintf("%%%x", b[i])) - } - return sb.String() - }) + return (&url.URL{Path: path}).EscapedPath() } // ParseDepth parses the depth header value defined in https://tools.ietf.org/html/rfc4918#section-9.1 diff --git a/internal/http/services/owncloud/ocdav/net/net_test.go b/internal/http/services/owncloud/ocdav/net/net_test.go index 24ee8da430..3b254067f3 100644 --- a/internal/http/services/owncloud/ocdav/net/net_test.go +++ b/internal/http/services/owncloud/ocdav/net/net_test.go @@ -64,7 +64,7 @@ var _ = Describe("Net", func() { Describe("EncodePath", func() { It("encodes paths", func() { Expect(net.EncodePath("foo")).To(Equal("foo")) - Expect(net.EncodePath("/some/path/Folder %^*(#1)")).To(Equal("/some/path/Folder%20%25%5e%2a(%231)")) + Expect(net.EncodePath("/some/path/Folder %^*(#1)")).To(Equal("/some/path/Folder%20%25%5E%2A%28%231%29")) }) /*