Use PathEscape instead of QueryEscape (#49)

This commit is contained in:
davidmcleish 2022-12-21 04:26:36 +11:00 committed by GitHub
parent a139af4144
commit 3c50bed6df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 3 deletions

View File

@ -271,8 +271,8 @@ func (h *httpGetter) makeRequest(ctx context.Context, m string, in request, b io
u := fmt.Sprintf(
"%v%v/%v",
h.baseURL,
url.QueryEscape(in.GetGroup()),
url.QueryEscape(in.GetKey()),
url.PathEscape(in.GetGroup()),
url.PathEscape(in.GetKey()),
)
req, err := http.NewRequestWithContext(ctx, m, u, b)
if err != nil {
@ -300,7 +300,7 @@ func (h *httpGetter) Get(ctx context.Context, in *pb.GetRequest, out *pb.GetResp
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
msg, _ := ioutil.ReadAll(io.LimitReader(res.Body, 1024*1024)) // Limit reading the error body to max 1 MiB
return fmt.Errorf("server returned: %v, %v", res.Status, msg)
return fmt.Errorf("server returned: %v, %v", res.Status, string(msg))
}
b := bufferPool.Get().(*bytes.Buffer)
b.Reset()

View File

@ -174,6 +174,15 @@ func TestHTTPPool(t *testing.T) {
if !bytes.Equal(setValue, getValue.ByteSlice()) {
t.Fatal(errors.New(fmt.Sprintf("incorrect value retrieved after set: %s", getValue)))
}
// Key with non-URL characters to test URL encoding roundtrip
key = "a b/c,d"
if err := g.Get(ctx, key, StringSink(&value)); err != nil {
t.Fatal(err)
}
if suffix := ":" + key; !strings.HasSuffix(value, suffix) {
t.Errorf("Get(%q) = %q, want value ending in %q", key, value, suffix)
}
}
func testKeys(n int) (keys []string) {