From 7b3d3e6ce670dbdb4fa3f59c5d780624395a4f2c Mon Sep 17 00:00:00 2001 From: Raphiel Rollerscaperers Date: Sun, 15 Sep 2024 21:34:37 +0800 Subject: [PATCH] Use fasthttp for IsSupportClientHints Also fix the test cases and the example Signed-off-by: Raphiel Rollerscaperers --- README.md | 10 +++++----- example/is_support.go | 7 +++---- example/parse.go | 2 +- go.mod | 6 +++--- go.sum | 12 ++++++------ uach.go | 27 +++++++++++++-------------- uach_test.go | 18 ++++++++---------- 7 files changed, 39 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 987fdc6..30ce365 100644 --- a/README.md +++ b/README.md @@ -14,13 +14,13 @@ go get -u github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints ```go import ( "fmt" - "net/http" + "github.com/valyala/fasthttp" goclienthints "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints" ) func Handler(ctx *fasthttp.RequestCtx) { - clientHints, err := clienthint.Parse(&ctx.Request.Header) + clientHints, err := clienthint.Parse(ctx) if err != nil { return } @@ -58,13 +58,13 @@ func Handler(ctx *fasthttp.RequestCtx) { ```go import ( - "net/http" + "github.com/valyala/fasthttp" clienthint "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints" ) -func Handler2(w http.ResponseWriter, r *http.Request) { - isSupport := clienthint.IsSupportClientHints(&r.Header) +func Handler2(ctx *fasthttp.RequestCtx) { + isSupport := clienthint.IsSupportClientHints(ctx) if isSupport { // ...do something diff --git a/example/is_support.go b/example/is_support.go index 2724d33..6796a8e 100644 --- a/example/is_support.go +++ b/example/is_support.go @@ -1,13 +1,12 @@ package example import ( - "net/http" - clienthint "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints" + "github.com/valyala/fasthttp" ) -func Handler2(w http.ResponseWriter, r *http.Request) { - isSupport := clienthint.IsSupportClientHints(&r.Header) +func Handler2(ctx *fasthttp.RequestCtx) { + isSupport := clienthint.IsSupportClientHints(ctx) if isSupport { // ...do something diff --git a/example/parse.go b/example/parse.go index 02295bf..b77b34c 100644 --- a/example/parse.go +++ b/example/parse.go @@ -8,7 +8,7 @@ import ( ) func Handler(ctx *fasthttp.RequestCtx) { - clientHints, err := clienthint.Parse(&ctx.Request.Header) + clientHints, err := clienthint.Parse(ctx) if err != nil { return } diff --git a/go.mod b/go.mod index 15a3510..0bf52f4 100644 --- a/go.mod +++ b/go.mod @@ -4,15 +4,15 @@ go 1.20 require ( github.com/dunglas/httpsfv v1.0.2 - github.com/stretchr/testify v1.8.4 + github.com/stretchr/testify v1.9.0 ) require ( github.com/andybalholm/brotli v1.1.0 // indirect github.com/davecgh/go-spew v1.1.1 // indirect - github.com/klauspost/compress v1.17.5 // indirect + github.com/klauspost/compress v1.17.9 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect - github.com/valyala/fasthttp v1.51.0 + github.com/valyala/fasthttp v1.55.0 gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index c38d000..ef6fc85 100644 --- a/go.sum +++ b/go.sum @@ -4,16 +4,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dunglas/httpsfv v1.0.2 h1:iERDp/YAfnojSDJ7PW3dj1AReJz4MrwbECSSE59JWL0= github.com/dunglas/httpsfv v1.0.2/go.mod h1:zID2mqw9mFsnt7YC3vYQ9/cjq30q41W+1AnDwH8TiMg= -github.com/klauspost/compress v1.17.5 h1:d4vBd+7CHydUqpFBgUEKkSdtSugf9YFmSkvUYPquI5E= -github.com/klauspost/compress v1.17.5/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/klauspost/compress v1.17.9 h1:6KIumPrER1LHsvBVuDa0r5xaG0Es51mhhB9BQB2qeMA= +github.com/klauspost/compress v1.17.9/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= -github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= -github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/fasthttp v1.55.0 h1:Zkefzgt6a7+bVKHnu/YaYSOPfNYNisSVBo/unVCf8k8= +github.com/valyala/fasthttp v1.55.0/go.mod h1:NkY9JtkrpPKmgwV3HTaS2HWaJss9RSIsRVfcxxoHiOM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= diff --git a/uach.go b/uach.go index de95dec..3c1a356 100644 --- a/uach.go +++ b/uach.go @@ -2,7 +2,6 @@ package goclienthints import ( "errors" - "net/http" "strconv" "strings" @@ -12,9 +11,9 @@ import ( // Sec-CH-UA field type Brand struct { + Brands map[string]string `json:"brands,omitempty"` Brand string `json:"brand,omitempty"` BrandVersion string `json:"brand_version,omitempty"` - Brands map[string]string `json:"brands,omitempty"` } type ClientHints struct { @@ -111,38 +110,38 @@ var SecondaryBrands = []string{ // - Sec-Ch-Ua-Platform-Version // // If the header does not exist, its value will be the empty string, the number 0, or false. -func Parse(request *fasthttp.RequestHeader) (*ClientHints, error) { +func Parse(ctx *fasthttp.RequestCtx) (*ClientHints, error) { // If you can get the full version, use it. - chUa := request.Peek(HeaderSecChUaFullVersionList) + chUa := ctx.Request.Header.Peek(HeaderSecChUaFullVersionList) if len(chUa) == 0 { - chUa = request.Peek(HeaderSecChUa) + chUa = ctx.Request.Header.Peek(HeaderSecChUa) } brand, err := ParseSecChUa(string(chUa)) if err != nil { return nil, err } - platform, err := ParsePlatform(request.Peek(HeaderSecChUaPlatform)) + platform, err := ParsePlatform(ctx.Request.Header.Peek(HeaderSecChUaPlatform)) if err != nil { return nil, err } - platformVersion, err := ParseItem(request.Peek(HeaderSecChUaPlatformVersion)) + platformVersion, err := ParseItem(ctx.Request.Header.Peek(HeaderSecChUaPlatformVersion)) if err != nil { return nil, err } - isMobile, err := ParseBool(request.Peek(HeaderSecChUaMobile)) + isMobile, err := ParseBool(ctx.Request.Header.Peek(HeaderSecChUaMobile)) if err != nil { return nil, err } - arch, err := ParseItem(request.Peek(HeaderSecChUaArch)) + arch, err := ParseItem(ctx.Request.Header.Peek(HeaderSecChUaArch)) if err != nil { return nil, err } - bitnessStr, err := ParseItem(request.Peek(HeaderSecChUaBitness)) + bitnessStr, err := ParseItem(ctx.Request.Header.Peek(HeaderSecChUaBitness)) if err != nil { return nil, err } @@ -156,12 +155,12 @@ func Parse(request *fasthttp.RequestHeader) (*ClientHints, error) { } } - model, err := ParseItem(request.Peek(HeaderSecChUaModel)) + model, err := ParseItem(ctx.Request.Header.Peek(HeaderSecChUaModel)) if err != nil { return nil, err } - fullVersion, err := ParseItem(request.Peek(HeaderSecChUaFullVersion)) + fullVersion, err := ParseItem(ctx.Request.Header.Peek(HeaderSecChUaFullVersion)) if err != nil { return nil, err } @@ -262,8 +261,8 @@ func ParsePlatform(h []byte) (Platform, error) { // Determines if ClientHint is supported. // // It is determined by the presence or absence of the `Sec-Ch-Ua` header. -func IsSupportClientHints(headers *http.Header) bool { - chUa := headers.Get(HeaderSecChUa) +func IsSupportClientHints(ctx *fasthttp.RequestCtx) bool { + chUa := string(ctx.Request.Header.Peek(HeaderSecChUa)) return chUa != "" } diff --git a/uach_test.go b/uach_test.go index 63bd102..c7698de 100644 --- a/uach_test.go +++ b/uach_test.go @@ -137,14 +137,14 @@ func TestParse(t *testing.T) { for i, c := range cases { t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) { - req := &fasthttp.Request{} + req := &fasthttp.RequestCtx{} for key, values := range c.Header { for _, value := range values { - req.Header.Add(key, value) + req.Request.Header.Add(key, value) } } - clientHints, err := goclienthints.Parse(&req.Header) + clientHints, err := goclienthints.Parse(req) if c.IsSuccess { require.NoError(t, err) @@ -292,15 +292,13 @@ func TestParsePlatform(t *testing.T) { } func TestIsClientHints(t *testing.T) { - supportClientHintHeader := http.Header{ - "Sec-Ch-Ua": {`"Chrome"; v="74", ";Not)Your=Browser"; v="13"`}, - } - noSupportClientHintHeader := http.Header{} - - isSupport := goclienthints.IsSupportClientHints(&supportClientHintHeader) + ctx := &fasthttp.RequestCtx{} + ctx.Request.Header.Set("Sec-Ch-Ua", `"Chrome"; v="74", ";Not)Your=Browser"; v="13"`) + isSupport := goclienthints.IsSupportClientHints(ctx) require.True(t, isSupport) - isSupport = goclienthints.IsSupportClientHints(&noSupportClientHintHeader) + ctx = &fasthttp.RequestCtx{} // New context without the header + isSupport = goclienthints.IsSupportClientHints(ctx) require.False(t, isSupport) }