Escape URL
This commit is contained in:
parent
c49c91989e
commit
375f391c8a
19
README.md
19
README.md
@ -49,6 +49,7 @@ Method <ARGS>
|
||||
|
||||
* [Overview](#pkg-overview)
|
||||
* [Index](#pkg-index)
|
||||
* [Examples](#pkg-examples)
|
||||
* [Subdirectories](#pkg-subdirectories)
|
||||
|
||||
### <a name="pkg-overview">Overview</a>
|
||||
@ -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)
|
||||
|
||||
##### <a name="pkg-examples">Examples</a>
|
||||
* [PathEscape](#example_PathEscape)
|
||||
|
||||
##### <a name="pkg-files">Package files</a>
|
||||
[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)
|
||||
|
||||
### <a name="FixSlash">func</a> [FixSlash](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=491:521#L35)
|
||||
### <a name="FixSlash">func</a> [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
|
||||
|
||||
### <a name="FixSlashes">func</a> [FixSlashes](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=643:675#L43)
|
||||
### <a name="FixSlashes">func</a> [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
|
||||
|
||||
### <a name="Join">func</a> [Join](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=760:804#L51)
|
||||
### <a name="Join">func</a> [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
|
||||
|
||||
### <a name="String">func</a> [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=934:965#L56)
|
||||
### <a name="PathEscape">func</a> [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
|
||||
|
||||
### <a name="String">func</a> [String](https://github.com/studio-b12/gowebdav/blob/master/utils.go?s=1150:1181#L66)
|
||||
``` go
|
||||
func String(r io.Reader) string
|
||||
```
|
||||
|
@ -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
|
||||
}
|
||||
|
10
utils.go
10
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, "/") {
|
||||
|
@ -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())
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user