From 81a281df490e72ea5062eb5740aea2274f995bab Mon Sep 17 00:00:00 2001 From: Tianyi Wang Date: Wed, 13 Nov 2024 14:56:02 -0500 Subject: [PATCH] fix path replace issue of duplicate key prefix --- encoding/httpbinding/path_replace.go | 30 +++++++++++------------ encoding/httpbinding/path_replace_test.go | 12 +++++++++ 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/encoding/httpbinding/path_replace.go b/encoding/httpbinding/path_replace.go index e78926c9a..3546b2bd9 100644 --- a/encoding/httpbinding/path_replace.go +++ b/encoding/httpbinding/path_replace.go @@ -22,33 +22,33 @@ func bufCap(b []byte, n int) []byte { // replacePathElement replaces a single element in the path []byte. // Escape is used to control whether the value will be escaped using Amazon path escape style. func replacePathElement(path, fieldBuf []byte, key, val string, escape bool) ([]byte, []byte, error) { - fieldBuf = bufCap(fieldBuf, len(key)+3) // { [+] } + fieldBuf = bufCap(fieldBuf, len(key)+2) // { } fieldBuf = append(fieldBuf, uriTokenStart) fieldBuf = append(fieldBuf, key...) + fieldBuf = append(fieldBuf, uriTokenStop) start := bytes.Index(path, fieldBuf) - end := start + len(fieldBuf) - if start < 0 || len(path[end:]) == 0 { - // TODO what to do about error? - return path, fieldBuf, fmt.Errorf("invalid path index, start=%d,end=%d. %s", start, end, path) - } - encodeSep := true - if path[end] == uriTokenSkip { - // '+' token means do not escape slashes + if start < 0 { + fieldBuf = bufCap(fieldBuf, len(key)+3) // { [+] } + fieldBuf = append(fieldBuf, uriTokenStart) + fieldBuf = append(fieldBuf, key...) + fieldBuf = append(fieldBuf, uriTokenSkip) + fieldBuf = append(fieldBuf, uriTokenStop) + + start = bytes.Index(path, fieldBuf) + if start < 0 { + // TODO what to do about error? + return path, fieldBuf, fmt.Errorf("invalid path index, start=%d. %s", start, path) + } encodeSep = false - end++ } + end := start + len(fieldBuf) if escape { val = EscapePath(val, encodeSep) } - if path[end] != uriTokenStop { - return path, fieldBuf, fmt.Errorf("invalid path element, does not contain token stop, %s", path) - } - end++ - fieldBuf = bufCap(fieldBuf, len(val)) fieldBuf = append(fieldBuf, val...) diff --git a/encoding/httpbinding/path_replace_test.go b/encoding/httpbinding/path_replace_test.go index 689733ca0..10b9ade28 100644 --- a/encoding/httpbinding/path_replace_test.go +++ b/encoding/httpbinding/path_replace_test.go @@ -40,6 +40,18 @@ func TestPathReplace(t *testing.T) { ExpRawPath: []byte("/reallylongvaluegoesheregrowingarray/{key+}"), Key: "bucket", Val: "reallylongvaluegoesheregrowingarray", }, + { + Orig: []byte("/{namespace}/{name}"), + ExpPath: []byte("/{namespace}/value"), + ExpRawPath: []byte("/{namespace}/value"), + Key: "name", Val: "value", + }, + { + Orig: []byte("/{name}/{namespace}"), + ExpPath: []byte("/value/{namespace}"), + ExpRawPath: []byte("/value/{namespace}"), + Key: "name", Val: "value", + }, } var buffer [64]byte