Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions httpparse.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,24 @@ func generateReqDerivedComponents(method string, u *url.URL, authority string, c
}

func scPath(theURL *url.URL) string {
return theURL.EscapedPath()
path := theURL.EscapedPath()
if path == "" {
// RFC 9421 §2.2.6 / HTTP §4.2.3: empty path normalizes to "/".
return "/"
}
return path
}

func scQuery(url *url.URL) string {
return "?" + url.RawQuery
}

func scRequestTarget(url *url.URL) string {
path := url.Path
path := url.EscapedPath()
if path == "" {
path = "/" // Normalize path, issue #8, and see https://www.rfc-editor.org/rfc/rfc9110#section-4.2.3
}
if url.RawQuery == "" {
if url.RawQuery == "" && !url.ForceQuery {
return path
}
return path + "?" + url.RawQuery
Expand Down
135 changes: 135 additions & 0 deletions httpparse_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
package httpsign

import (
"net/http"
"net/url"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestScRequestTarget(t *testing.T) {
t.Parallel()

cases := []struct {
name string
u *url.URL
want string
}{
{
name: "plain path",
u: mustParseURL(t, "https://example.com/foo"),
want: "/foo",
},
{
name: "path with query",
u: mustParseURL(t, "https://example.com/foo?a=b"),
want: "/foo?a=b",
},
{
name: "escaped slash in path preserved",
u: mustParseURL(t, "https://example.com/a%2Fb"),
want: "/a%2Fb",
},
{
name: "escaped space in path preserved",
u: mustParseURL(t, "https://example.com/a%20b"),
want: "/a%20b",
},
{
name: "escaped path with query",
u: mustParseURL(t, "https://example.com/a%2Fb?x=1"),
want: "/a%2Fb?x=1",
},
{
name: "empty query delimiter preserved",
u: mustParseURL(t, "https://example.com/path?"),
want: "/path?",
},
{
name: "escaped path with empty query delimiter",
u: mustParseURL(t, "https://example.com/a%2Fb?"),
want: "/a%2Fb?",
},
{
name: "empty path normalized to slash",
u: mustParseURL(t, "https://example.com"),
want: "/",
},
{
name: "empty path with empty query delimiter",
u: mustParseURL(t, "https://example.com?"),
want: "/?",
},
{
name: "raw path set without re-encoding via Path alone",
u: &url.URL{Path: "/a/b", RawPath: "/a%2Fb"},
want: "/a%2Fb",
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, scRequestTarget(tc.u))
})
}
}

func TestScPath(t *testing.T) {
t.Parallel()

cases := []struct {
name string
u *url.URL
want string
}{
{name: "plain path", u: mustParseURL(t, "https://example.com/foo"), want: "/foo"},
{name: "escaped slash preserved", u: mustParseURL(t, "https://example.com/a%2Fb"), want: "/a%2Fb"},
{name: "query ignored", u: mustParseURL(t, "https://example.com/foo?a=b"), want: "/foo"},
{name: "empty query delimiter ignored", u: mustParseURL(t, "https://example.com/path?"), want: "/path"},
{name: "empty path normalized to slash", u: mustParseURL(t, "https://example.com"), want: "/"},
{name: "raw path preserved", u: &url.URL{Path: "/a/b", RawPath: "/a%2Fb"}, want: "/a%2Fb"},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, scPath(tc.u))
})
}
}

func TestDerivedRequestTarget(t *testing.T) {
t.Parallel()

cases := []struct {
name string
raw string
wantRT string
wantPath string
}{
{name: "escaped slash", raw: "https://example.com/a%2Fb", wantRT: "/a%2Fb", wantPath: "/a%2Fb"},
{name: "empty query", raw: "https://example.com/path?", wantRT: "/path?", wantPath: "/path"},
{name: "escaped slash and empty query", raw: "https://example.com/a%2Fb?", wantRT: "/a%2Fb?", wantPath: "/a%2Fb"},
{name: "normal query", raw: "https://example.com/foo?a=b", wantRT: "/foo?a=b", wantPath: "/foo"},
{name: "empty path", raw: "https://example.com", wantRT: "/", wantPath: "/"},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
req, err := http.NewRequest("GET", tc.raw, nil)
require.NoError(t, err)
msg, err := parseRequest(req, false, 0, "https")
require.NoError(t, err)
assert.Equal(t, tc.wantRT, msg.derived["@request-target"])
assert.Equal(t, tc.wantPath, msg.derived["@path"])
})
}
}

func mustParseURL(t *testing.T, raw string) *url.URL {
t.Helper()
u, err := url.Parse(raw)
require.NoError(t, err)
return u
}
Loading