Skip to content

Commit

Permalink
fix path replace issue of duplicate key prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
Tianyi Wang committed Nov 13, 2024
1 parent 253cd26 commit 81a281d
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
30 changes: 15 additions & 15 deletions encoding/httpbinding/path_replace.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) // { <key> [+] }
fieldBuf = bufCap(fieldBuf, len(key)+2) // { <key> }
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) // { <key> [+] }
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...)

Expand Down
12 changes: 12 additions & 0 deletions encoding/httpbinding/path_replace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 81a281d

Please sign in to comment.