Skip to content

Commit

Permalink
Use fasthttp for IsSupportClientHints
Browse files Browse the repository at this point in the history
Also fix the test cases and the example

Signed-off-by: Raphiel Rollerscaperers <[email protected]>
  • Loading branch information
raphielscape committed Sep 15, 2024
1 parent 4c3977a commit 7b3d3e6
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 43 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down
7 changes: 3 additions & 4 deletions example/is_support.go
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
12 changes: 6 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
27 changes: 13 additions & 14 deletions uach.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package goclienthints

import (
"errors"
"net/http"
"strconv"
"strings"

Expand All @@ -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 {
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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 != ""
}
Expand Down
18 changes: 8 additions & 10 deletions uach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}

Expand Down

0 comments on commit 7b3d3e6

Please sign in to comment.