Skip to content

Commit fb1602e

Browse files
committed
Normalize an empty path
1 parent 261f29c commit fb1602e

File tree

2 files changed

+8
-4
lines changed

2 files changed

+8
-4
lines changed

handler_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ func Test_WrapHandler(t *testing.T) {
3939
defer ts.Close()
4040

4141
signer, err := NewHMACSHA256Signer("key", bytes.Repeat([]byte{1}, 64), nil,
42-
Headers("@method", "content-digest"))
42+
Headers("@method", "content-digest", "@request-target"))
4343
assert.NoError(t, err)
4444

4545
verifier, err := NewHMACSHA256Verifier("key", bytes.Repeat([]byte{0}, 64), NewVerifyConfig(), *NewFields())
4646
assert.NoError(t, err)
4747
client := NewDefaultClient(NewClientConfig().SetSignatureName("sig1").SetSigner(signer).SetVerifier(verifier).SetDigestSchemesSend([]string{DigestSha256}))
48-
res, err := client.Post(ts.URL, "text/plain", strings.NewReader("Message body here"))
48+
res, err := client.Post(ts.URL+"?foo", "text/plain", strings.NewReader("Message body here"))
4949
assert.NoError(t, err)
5050
if res != nil {
5151
_, err = io.ReadAll(res.Body)

httpparse.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,10 +145,14 @@ func scQuery(url *url.URL) string {
145145
}
146146

147147
func scRequestTarget(url *url.URL) string {
148+
path := url.Path
149+
if path == "" {
150+
path = "/" // Normalize path, issue #8, and see https://www.rfc-editor.org/rfc/rfc9110#section-4.2.3
151+
}
148152
if url.RawQuery == "" {
149-
return url.Path
153+
return path
150154
}
151-
return url.Path + "?" + url.RawQuery
155+
return path + "?" + url.RawQuery
152156
}
153157

154158
func scScheme(url *url.URL) string {

0 commit comments

Comments
 (0)