From 6844355a928b60ffb1006e6b1d9f8af7b44647d5 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Sun, 18 Jun 2023 17:41:38 +0200 Subject: Imported and modified evcli's api client code --- external/evcli/url.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 external/evcli/url.go (limited to 'external/evcli/url.go') diff --git a/external/evcli/url.go b/external/evcli/url.go new file mode 100644 index 0000000..e1e99a8 --- /dev/null +++ b/external/evcli/url.go @@ -0,0 +1,42 @@ +package evcli + +import ( + "bytes" + "net/url" +) + +// The url package has an extremely confusing interface. One could believe +// setting RawPath is enough for it to be used during encoding, but this is +// not the case. Instead, *both* must be set, and (*url.URL).EscapedPath will +// check that RawPath is a valid encoding of RawPath. If this is not the case, +// RawPath will be ignored. +// +// In most cases, the problem is not apparent. But if the path contains +// segments with slash or space characters, it is almost guaranteed to misuse +// Path and RawPath and double-encode these characters. +// +// The problem was signaled years ago on +// https://github.com/golang/go/issues/17340 but was of course ignored and +// buried. +// +// As usual with standard library issues, the only thing we can do is add +// utils to work around it. + +func NewURL(pathSegments ...string) *url.URL { + var buf bytes.Buffer + buf.WriteByte('/') + for i, s := range pathSegments { + if i > 0 { + buf.WriteByte('/') + } + buf.WriteString(url.PathEscape(s)) + } + rawPath := buf.String() + + path, _ := url.PathUnescape(rawPath) + + return &url.URL{ + Path: path, + RawPath: rawPath, + } +} -- cgit v1.2.3