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)
|
* [Overview](#pkg-overview)
|
||||||
* [Index](#pkg-index)
|
* [Index](#pkg-index)
|
||||||
|
* [Examples](#pkg-examples)
|
||||||
* [Subdirectories](#pkg-subdirectories)
|
* [Subdirectories](#pkg-subdirectories)
|
||||||
|
|
||||||
### <a name="pkg-overview">Overview</a>
|
### <a name="pkg-overview">Overview</a>
|
||||||
@ -58,6 +59,7 @@ Package gowebdav A golang WebDAV library
|
|||||||
* [func FixSlash(s string) string](#FixSlash)
|
* [func FixSlash(s string) string](#FixSlash)
|
||||||
* [func FixSlashes(s string) string](#FixSlashes)
|
* [func FixSlashes(s string) string](#FixSlashes)
|
||||||
* [func Join(path0 string, path1 string) string](#Join)
|
* [func Join(path0 string, path1 string) string](#Join)
|
||||||
|
* [func PathEscape(path string) string](#PathEscape)
|
||||||
* [func String(r io.Reader) string](#String)
|
* [func String(r io.Reader) string](#String)
|
||||||
* [type Client](#Client)
|
* [type Client](#Client)
|
||||||
* [func NewClient(uri string, user string, pw string) *Client](#NewClient)
|
* [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) String() string](#File.String)
|
||||||
* [func (f File) Sys() interface{}](#File.Sys)
|
* [func (f File) Sys() interface{}](#File.Sys)
|
||||||
|
|
||||||
|
##### <a name="pkg-examples">Examples</a>
|
||||||
|
* [PathEscape](#example_PathEscape)
|
||||||
|
|
||||||
##### <a name="pkg-files">Package files</a>
|
##### <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)
|
[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
|
``` go
|
||||||
func FixSlash(s string) string
|
func FixSlash(s string) string
|
||||||
```
|
```
|
||||||
FixSlash appends a trailing / to our 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
|
``` go
|
||||||
func FixSlashes(s string) string
|
func FixSlashes(s string) string
|
||||||
```
|
```
|
||||||
FixSlashes appends and prepends a / if they are missing
|
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
|
``` go
|
||||||
func Join(path0 string, path1 string) string
|
func Join(path0 string, path1 string) string
|
||||||
```
|
```
|
||||||
Join joins two paths
|
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
|
``` go
|
||||||
func String(r io.Reader) string
|
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) {
|
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)
|
r, err := http.NewRequest(method, PathEscape(Join(c.root, path)), body)
|
||||||
path = strings.Replace(path, "#", "%23", -1)
|
|
||||||
r, err := http.NewRequest(method, Join(c.root, path), body)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
10
utils.go
10
utils.go
@ -5,6 +5,7 @@ import (
|
|||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"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
|
// FixSlash appends a trailing / to our string
|
||||||
func FixSlash(s string) string {
|
func FixSlash(s string) string {
|
||||||
if !strings.HasSuffix(s, "/") {
|
if !strings.HasSuffix(s, "/") {
|
||||||
|
@ -1,6 +1,10 @@
|
|||||||
package gowebdav
|
package gowebdav
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/url"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
func TestJoin(t *testing.T) {
|
func TestJoin(t *testing.T) {
|
||||||
eq(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+"'")
|
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