From 375f391c8ad875587e81f1584f5fe4a4f30ee034 Mon Sep 17 00:00:00 2001 From: Christoph Polcin Date: Wed, 23 May 2018 13:41:06 +0200 Subject: [PATCH] Escape URL --- README.md | 19 +++++++++++++++---- requests.go | 4 +--- utils.go | 10 ++++++++++ utils_test.go | 29 ++++++++++++++++++++++++++++- 4 files changed, 54 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 08ec84a..ccf2ce5 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,7 @@ Method * [Overview](#pkg-overview) * [Index](#pkg-index) +* [Examples](#pkg-examples) * [Subdirectories](#pkg-subdirectories) ### Overview @@ -58,6 +59,7 @@ Package gowebdav A golang WebDAV library * [func FixSlash(s string) string](#FixSlash) * [func FixSlashes(s string) string](#FixSlashes) * [func Join(path0 string, path1 string) string](#Join) +* [func PathEscape(path string) string](#PathEscape) * [func String(r io.Reader) string](#String) * [type Client](#Client) * [func NewClient(uri string, user string, pw string) *Client](#NewClient) @@ -88,28 +90,37 @@ Package gowebdav A golang WebDAV library * [func (f File) String() string](#File.String) * [func (f File) Sys() interface{}](#File.Sys) +##### Examples +* [PathEscape](#example_PathEscape) + ##### Package files [client.go](https://github.com/studio-b12/gowebdav/blob/master/client.go) [file.go](https://github.com/studio-b12/gowebdav/blob/master/file.go) [requests.go](https://github.com/studio-b12/gowebdav/blob/master/requests.go) [utils.go](https://github.com/studio-b12/gowebdav/blob/master/utils.go) -### func [FixSlash](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=491:521#L35) +### func [FixSlash](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=707:737#L45) ``` go func FixSlash(s string) string ``` FixSlash appends a trailing / to our string -### func [FixSlashes](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=643:675#L43) +### func [FixSlashes](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=859:891#L53) ``` go func FixSlashes(s string) string ``` FixSlashes appends and prepends a / if they are missing -### func [Join](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=760:804#L51) +### func [Join](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=976:1020#L61) ``` go func Join(path0 string, path1 string) string ``` Join joins two paths -### func [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=934:965#L56) +### func [PathEscape](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=506:541#L36) +``` go +func PathEscape(path string) string +``` +PathEscape escapes all segemnts of a given path + +### func [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=1150:1181#L66) ``` go func String(r io.Reader) string ``` diff --git a/requests.go b/requests.go index 316e252..206dda8 100644 --- a/requests.go +++ b/requests.go @@ -8,9 +8,7 @@ import ( ) func (c *Client) req(method, path string, body io.Reader, intercept func(*http.Request)) (req *http.Response, err error) { - path = strings.Replace(path, "%", "%25", -1) - path = strings.Replace(path, "#", "%23", -1) - r, err := http.NewRequest(method, Join(c.root, path), body) + r, err := http.NewRequest(method, PathEscape(Join(c.root, path)), body) if err != nil { return nil, err } diff --git a/utils.go b/utils.go index 43b3725..e6caf50 100644 --- a/utils.go +++ b/utils.go @@ -5,6 +5,7 @@ import ( "encoding/xml" "fmt" "io" + "net/url" "os" "strconv" "strings" @@ -31,6 +32,15 @@ func newPathErrorErr(op string, path string, err error) error { } } +// PathEscape escapes all segemnts of a given path +func PathEscape(path string) string { + s := strings.Split(path, "/") + for i, e := range s { + s[i] = url.PathEscape(e) + } + return strings.Join(s, "/") +} + // FixSlash appends a trailing / to our string func FixSlash(s string) string { if !strings.HasSuffix(s, "/") { diff --git a/utils_test.go b/utils_test.go index 4e7dcca..9751c9c 100644 --- a/utils_test.go +++ b/utils_test.go @@ -1,6 +1,10 @@ package gowebdav -import "testing" +import ( + "fmt" + "net/url" + "testing" +) func TestJoin(t *testing.T) { eq(t, "/", "", "") @@ -16,3 +20,26 @@ func eq(t *testing.T, expected string, s0 string, s1 string) { t.Error("For", "'"+s0+"','"+s1+"'", "expeted", "'"+expected+"'", "got", "'"+s+"'") } } + +func ExamplePathEscape() { + fmt.Println(PathEscape("")) + fmt.Println(PathEscape("/")) + fmt.Println(PathEscape("/web")) + fmt.Println(PathEscape("/web/")) + fmt.Println(PathEscape("/w e b/d a v/s%u&c#k:s/")) + + // Output: + // + // / + // /web + // /web/ + // /w%20e%20b/d%20a%20v/s%25u&c%23k:s/ +} + +func TestEscapeURL(t *testing.T) { + ex := "https://foo.com/w%20e%20b/d%20a%20v/s%25u&c%23k:s/" + u, _ := url.Parse("https://foo.com" + PathEscape("/w e b/d a v/s%u&c#k:s/")) + if ex != u.String() { + t.Error("expected: " + ex + " got: " + u.String()) + } +}