-
Notifications
You must be signed in to change notification settings - Fork 18.8k
Closed
Labels
Milestone
Description
What version of Go are you using (go version)?
$ go version go version go1.13.4 darwin/amd64
Does this issue reproduce with the latest release?
It does.
What operating system and processor architecture are you using (go env)?
go env Output
$ go env GO111MODULE="" GOARCH="amd64" GOBIN="" GOCACHE="/Users/tao.yang/Library/Caches/go-build" GOENV="/Users/tao.yang/Library/Application Support/go/env" GOEXE="" GOFLAGS="" GOHOSTARCH="amd64" GOHOSTOS="darwin" GONOPROXY="" GONOSUMDB="" GOOS="darwin" GOPATH="/Users/tao.yang/codes/gocodes" GOPRIVATE="" GOPROXY="https://proxy.golang.org,direct" GOROOT="/usr/local/Cellar/go/1.13.4/libexec" GOSUMDB="off" GOTMPDIR="" GOTOOLDIR="/usr/local/Cellar/go/1.13.4/libexec/pkg/tool/darwin_amd64" GCCGO="gccgo" AR="ar" CC="clang" CXX="clang++" CGO_ENABLED="1" GOMOD="/Users/tao.yang/codes/gocodes/src/github.com/elastic/beats/filebeat/go.mod" CGO_CFLAGS="-g -O2" CGO_CPPFLAGS="" CGO_CXXFLAGS="-g -O2" CGO_FFLAGS="-g -O2" CGO_LDFLAGS="-g -O2" PKG_CONFIG="pkg-config" GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/2l/zkndzzg90t746hcwzwb9pbz00000gn/T/go-build399229267=/tmp/go-build -gno-record-gcc-switches -fno-common"
What did you do?
Use url.PathEscape to escape file system path.
package main
import (
"fmt"
"net/url"
)
func main() {
fmt.Println(url.PathEscape((`/a?/b`)))
}Notice that ? is a valid path character on Unix-like systems.
Don't we treat it as a query that follows.
What did you expect to see?
/a%3F/b
What did you see instead?
%2Fa%3F%2Fb
Why?
PathEscape is actually the path segment escaper.
// PathEscape escapes the string so it can be safely placed inside a URL path segment,
// replacing special characters (including /) with %XX sequences as needed.
func PathEscape(s string) string {
return escape(s, encodePathSegment)
}What I need is a path escaper, with mode == encodePath, not encodePathSegment. Something like:
func RealPathEscape(s string) string {
return escape(s, encodePath)
}Workarounds
escape path segments individually
package main
import (
"fmt"
"net/url"
"strings"
)
func main() {
segments := strings.Split(`/a?/b`, "/")
for i, segment := range segments {
segments[i] = url.PathEscape(segment)
}
fmt.Println(strings.Join(segments, "/"))
}use EscapedPath
package main
import (
"fmt"
"net/url"
)
func main() {
u := url.URL{Path: "/a?/b"}
fmt.Println(u.EscapedPath())
}