From 2af2a432b07917499387866064bd1d2e8206daa3 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Mon, 2 Jan 2023 06:32:13 +0600 Subject: [PATCH] :recycle: Refactored ClientID domain contents, translated fasthttp.URI to url.URL --- internal/domain/client.go | 6 ++--- internal/domain/client_id.go | 44 +++++++++--------------------------- 2 files changed, 14 insertions(+), 36 deletions(-) diff --git a/internal/domain/client.go b/internal/domain/client.go index 936dcb1..a983113 100644 --- a/internal/domain/client.go +++ b/internal/domain/client.go @@ -65,12 +65,12 @@ func (c *Client) ValidateRedirectURI(redirectURI *URL) bool { rHost = string(redirectURI.Host()) } - cHost, cPort, err := net.SplitHostPort(string(c.ID.clientID.Host())) + cHost, cPort, err := net.SplitHostPort(c.ID.clientID.Host) if err != nil { - cHost = string(c.ID.clientID.Host()) + cHost = c.ID.clientID.Hostname() } - if bytes.EqualFold(redirectURI.Scheme(), c.ID.clientID.Scheme()) && + if bytes.EqualFold(redirectURI.Scheme(), []byte(c.ID.clientID.Scheme)) && strings.EqualFold(rHost, cHost) && strings.EqualFold(rPort, cPort) { return true diff --git a/internal/domain/client_id.go b/internal/domain/client_id.go index f100b9e..f972db5 100644 --- a/internal/domain/client_id.go +++ b/internal/domain/client_id.go @@ -7,13 +7,12 @@ import ( "strings" "testing" - http "github.com/valyala/fasthttp" "inet.af/netaddr" ) // ClientID is a URL client identifier. type ClientID struct { - clientID *http.URI + clientID *url.URL } //nolint:gochecknoglobals // slices cannot be constants @@ -26,8 +25,8 @@ var ( // //nolint:funlen,cyclop func ParseClientID(src string) (*ClientID, error) { - cid := http.AcquireURI() - if err := cid.Parse(nil, []byte(src)); err != nil { + cid, err := url.Parse(src) + if err != nil { return nil, NewError( ErrorCodeInvalidRequest, err.Error(), @@ -35,8 +34,7 @@ func ParseClientID(src string) (*ClientID, error) { ) } - scheme := string(cid.Scheme()) - if scheme != "http" && scheme != "https" { + if cid.Scheme != "http" && cid.Scheme != "https" { return nil, NewError( ErrorCodeInvalidRequest, "client identifier URL MUST have either an https or http scheme", @@ -44,8 +42,7 @@ func ParseClientID(src string) (*ClientID, error) { ) } - path := string(cid.PathOriginal()) - if path == "" || strings.Contains(path, "/.") || strings.Contains(path, "/..") { + if cid.Path == "" || strings.Contains(cid.Path, "/.") || strings.Contains(cid.Path, "/..") { return nil, NewError( ErrorCodeInvalidRequest, "client identifier URL MUST contain a path component and MUST NOT contain "+ @@ -54,7 +51,7 @@ func ParseClientID(src string) (*ClientID, error) { ) } - if cid.Hash() != nil { + if cid.Fragment != "" { return nil, NewError( ErrorCodeInvalidRequest, "client identifier URL MUST NOT contain a fragment component", @@ -62,7 +59,7 @@ func ParseClientID(src string) (*ClientID, error) { ) } - if cid.Username() != nil || cid.Password() != nil { + if cid.User != nil { return nil, NewError( ErrorCodeInvalidRequest, "client identifier URL MUST NOT contain a username or password component", @@ -70,7 +67,7 @@ func ParseClientID(src string) (*ClientID, error) { ) } - domain := string(cid.Host()) + domain := cid.Hostname() if domain == "" { return nil, NewError( ErrorCodeInvalidRequest, @@ -150,30 +147,11 @@ func (cid ClientID) MarshalJSON() ([]byte, error) { return []byte(strconv.Quote(cid.String())), nil } -// URI returns copy of parsed *fasthttp.URI. -// -// WARN(toby3d): This copy MUST be released via fasthttp.ReleaseURI. -func (cid ClientID) URI() *http.URI { - uri := http.AcquireURI() - cid.clientID.CopyTo(uri) - - return uri -} - // URL returns url.URL representation of client ID. func (cid ClientID) URL() *url.URL { - return &url.URL{ - ForceQuery: false, - Fragment: string(cid.clientID.Hash()), - Host: string(cid.clientID.Host()), - Opaque: "", - Path: string(cid.clientID.Path()), - RawFragment: "", - RawPath: string(cid.clientID.PathOriginal()), - RawQuery: string(cid.clientID.QueryString()), - Scheme: string(cid.clientID.Scheme()), - User: nil, - } + out, _ := url.Parse(cid.clientID.String()) + + return out } // String returns string representation of client ID.