Skip to content

Commit

Permalink
Move to fasthttp implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Raphiel Rollerscaperers <[email protected]>
  • Loading branch information
raphielscape committed Feb 2, 2024
1 parent faef56d commit 4c3977a
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 79 deletions.
68 changes: 34 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,55 +1,55 @@
[![Go Reference](https://pkg.go.dev/badge/github.com/cateiru/go-client-hints/v2.svg)](https://pkg.go.dev/github.com/cateiru/go-client-hints/v2)
[![Go Reference](https://pkg.go.dev/badge/github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints.svg)](https://pkg.go.dev/github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints)
[![Go](https://github.com/cateiru/go-client-hints/actions/workflows/go.yml/badge.svg?branch=main)](https://github.com/cateiru/go-client-hints/actions/workflows/go.yml)

# Go Client Hints

## Install

```bash
go get -u github.com/cateiru/go-client-hints/v2
go get -u github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints
```

## Parse Client Hints

```go
import (
"fmt"
"net/http"
"fmt"
"net/http"

goclienthints "github.com/cateiru/go-client-hints/v2"
goclienthints "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints"
)

func Handler(w http.ResponseWriter, r *http.Request) {
clientHints, err := goclienthints.Parse(&r.Header)
if err != nil {
return
}
func Handler(ctx *fasthttp.RequestCtx) {
clientHints, err := clienthint.Parse(&ctx.Request.Header)
if err != nil {
return
}

// Sec-CH-UA field
fmt.Println("Brand: ", clientHints.Brand.Brand)
fmt.Println("Brand Version: ", clientHints.BrandVersion)
fmt.Println("Brands: ", clientHints.Brands)
// Sec-CH-UA field
fmt.Println("Brand: ", clientHints.Brand.Brand)
fmt.Println("Brand Version: ", clientHints.BrandVersion)
fmt.Println("Brands: ", clientHints.Brands)

// Sec-Ch-Ua-Platform filed
fmt.Println("Platform: ", clientHints.Platform)
// Sec-Ch-Ua-Platform filed
fmt.Println("Platform: ", clientHints.Platform)

// Sec-CH-UA-Platform-Version filed
fmt.Println("Platform Version: ", clientHints.PlatformVersion)
// Sec-CH-UA-Platform-Version filed
fmt.Println("Platform Version: ", clientHints.PlatformVersion)

// Sec-Ch-Ua-Mobile filed
fmt.Println("IsMobile: ", clientHints.IsMobile)
// Sec-Ch-Ua-Mobile filed
fmt.Println("IsMobile: ", clientHints.IsMobile)

// Sec-CH-UA-Arch filed
fmt.Println("Arch: ", clientHints.Architecture)
// Sec-CH-UA-Arch filed
fmt.Println("Arch: ", clientHints.Architecture)

// Sec-CH-UA-Bitness filed
fmt.Println("Bitness: ", clientHints.Bitness)
// Sec-CH-UA-Bitness filed
fmt.Println("Bitness: ", clientHints.Bitness)

// Sec-CH-UA-Model filed
fmt.Println("Model: ", clientHints.Model)
// Sec-CH-UA-Model filed
fmt.Println("Model: ", clientHints.Model)

// Sec-Ch-Ua-Full-Version filed
fmt.Println("Full Version: ", clientHints.FullVersion)
// Sec-Ch-Ua-Full-Version filed
fmt.Println("Full Version: ", clientHints.FullVersion)
}

```
Expand All @@ -58,17 +58,17 @@ func Handler(w http.ResponseWriter, r *http.Request) {

```go
import (
"net/http"
"net/http"

clienthint "github.com/cateiru/go-client-hints/v2"
clienthint "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints"
)

func Handler2(w http.ResponseWriter, r *http.Request) {
isSupport := clienthint.IsSupportClientHints(&r.Header)
isSupport := clienthint.IsSupportClientHints(&r.Header)

if isSupport {
// ...do something
}
if isSupport {
// ...do something
}
}
```

Expand Down
2 changes: 1 addition & 1 deletion example/is_support.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package example
import (
"net/http"

clienthint "github.com/cateiru/go-client-hints/v2"
clienthint "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints"
)

func Handler2(w http.ResponseWriter, r *http.Request) {
Expand Down
8 changes: 4 additions & 4 deletions example/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package example

import (
"fmt"
"net/http"

clienthint "github.com/cateiru/go-client-hints/v2"
clienthint "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints"
"github.com/valyala/fasthttp"
)

func Handler(w http.ResponseWriter, r *http.Request) {
clientHints, err := clienthint.Parse(&r.Header)
func Handler(ctx *fasthttp.RequestCtx) {
clientHints, err := clienthint.Parse(&ctx.Request.Header)
if err != nil {
return
}
Expand Down
10 changes: 7 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
module github.com/cateiru/go-client-hints/v2
module github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints

go 1.20

require (
github.com/dunglas/httpsfv v1.0.1
github.com/stretchr/testify v1.8.2
github.com/dunglas/httpsfv v1.0.2
github.com/stretchr/testify v1.8.4
)

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/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasthttp v1.51.0
gopkg.in/yaml.v3 v3.0.1 // indirect
)
23 changes: 12 additions & 11 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY=
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.1 h1:OjTvfzHJuwjuoyUPkDL1lJzcUP//AUd7cWvn1Nvo03w=
github.com/dunglas/httpsfv v1.0.1/go.mod h1:zID2mqw9mFsnt7YC3vYQ9/cjq30q41W+1AnDwH8TiMg=
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/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/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
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/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=
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.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
39 changes: 20 additions & 19 deletions uach.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/dunglas/httpsfv"
"github.com/valyala/fasthttp"
)

// Sec-CH-UA field
Expand Down Expand Up @@ -110,38 +111,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(headers *http.Header) (*ClientHints, error) {
func Parse(request *fasthttp.RequestHeader) (*ClientHints, error) {
// If you can get the full version, use it.
chUa := headers.Get(HeaderSecChUaFullVersionList)
if chUa == "" {
chUa = headers.Get(HeaderSecChUa)
chUa := request.Peek(HeaderSecChUaFullVersionList)
if len(chUa) == 0 {
chUa = request.Peek(HeaderSecChUa)
}
brand, err := ParseSecChUa(chUa)
brand, err := ParseSecChUa(string(chUa))
if err != nil {
return nil, err
}

platform, err := ParsePlatform(headers.Get(HeaderSecChUaPlatform))
platform, err := ParsePlatform(request.Peek(HeaderSecChUaPlatform))
if err != nil {
return nil, err
}

platformVersion, err := ParseItem(headers.Get(HeaderSecChUaPlatformVersion))
platformVersion, err := ParseItem(request.Peek(HeaderSecChUaPlatformVersion))
if err != nil {
return nil, err
}

isMobile, err := ParseBool(headers.Get(HeaderSecChUaMobile))
isMobile, err := ParseBool(request.Peek(HeaderSecChUaMobile))
if err != nil {
return nil, err
}

arch, err := ParseItem(headers.Get(HeaderSecChUaArch))
arch, err := ParseItem(request.Peek(HeaderSecChUaArch))
if err != nil {
return nil, err
}

bitnessStr, err := ParseItem(headers.Get(HeaderSecChUaBitness))
bitnessStr, err := ParseItem(request.Peek(HeaderSecChUaBitness))
if err != nil {
return nil, err
}
Expand All @@ -155,12 +156,12 @@ func Parse(headers *http.Header) (*ClientHints, error) {
}
}

model, err := ParseItem(headers.Get(HeaderSecChUaModel))
model, err := ParseItem(request.Peek(HeaderSecChUaModel))
if err != nil {
return nil, err
}

fullVersion, err := ParseItem(headers.Get(HeaderSecChUaFullVersion))
fullVersion, err := ParseItem(request.Peek(HeaderSecChUaFullVersion))
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -232,7 +233,7 @@ func ParseSecChUa(h string) (*Brand, error) {
}

// Prase the `Sec-CH-UA-Platform` header
func ParsePlatform(h string) (Platform, error) {
func ParsePlatform(h []byte) (Platform, error) {
platform, err := ParseItem(h)
if err != nil {
return "", err
Expand Down Expand Up @@ -267,12 +268,12 @@ func IsSupportClientHints(headers *http.Header) bool {
return chUa != ""
}

func ParseItem(h string) (string, error) {
if h == "" {
func ParseItem(h []byte) (string, error) {
if len(h) == 0 {
return "", nil
}

item, err := httpsfv.UnmarshalItem([]string{h})
item, err := httpsfv.UnmarshalItem([]string{string(h)})
if err != nil {
return "", err
}
Expand All @@ -286,12 +287,12 @@ func ParseItem(h string) (string, error) {
return itemStr, nil
}

func ParseBool(h string) (bool, error) {
if h == "" {
func ParseBool(h []byte) (bool, error) {
if len(h) == 0 {
return false, nil
}

item, err := httpsfv.UnmarshalItem([]string{h})
item, err := httpsfv.UnmarshalItem([]string{string(h)})
if err != nil {
return false, err
}
Expand Down
24 changes: 17 additions & 7 deletions uach_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"net/http"
"testing"

goclienthints "github.com/cateiru/go-client-hints/v2"
goclienthints "github.com/hentaiOS-Infrastructure/fasthttp-go-client-hints"
"github.com/stretchr/testify/require"
"github.com/valyala/fasthttp"
)

type TestParseCase struct {
Expand Down Expand Up @@ -61,7 +62,8 @@ func TestParse(t *testing.T) {
Brands: map[string]string{
"Google Chrome": "84",
"Chromium": "84",
}},
},
},
Platform: goclienthints.MacOS,
PlatformVersion: "11",
IsMobile: false,
Expand Down Expand Up @@ -92,7 +94,8 @@ func TestParse(t *testing.T) {
"Microsoft Edge": "92.0.902.73",
"Chromium": "92.0.4515.131",
"?Not:Your Browser": "3.1.2.0",
}},
},
},
Platform: goclienthints.MacOS,
PlatformVersion: "11",
IsMobile: false,
Expand Down Expand Up @@ -134,7 +137,14 @@ func TestParse(t *testing.T) {

for i, c := range cases {
t.Run(fmt.Sprintf("Case %d", i), func(t *testing.T) {
clientHints, err := goclienthints.Parse(&c.Header)
req := &fasthttp.Request{}

for key, values := range c.Header {
for _, value := range values {
req.Header.Add(key, value)
}
}
clientHints, err := goclienthints.Parse(&req.Header)

if c.IsSuccess {
require.NoError(t, err)
Expand Down Expand Up @@ -269,7 +279,7 @@ func TestParsePlatform(t *testing.T) {

for _, c := range cases {
t.Run(c.Value, func(t *testing.T) {
p, err := goclienthints.ParsePlatform(c.Value)
p, err := goclienthints.ParsePlatform([]byte(c.Value))

if c.IsSuccess {
require.NoError(t, err)
Expand Down Expand Up @@ -319,7 +329,7 @@ func TestParseItem(t *testing.T) {

for _, c := range cases {
t.Run(c.Value, func(t *testing.T) {
item, err := goclienthints.ParseItem(c.Value)
item, err := goclienthints.ParseItem([]byte(c.Value))

if c.IsSuccess {
require.NoError(t, err)
Expand Down Expand Up @@ -356,7 +366,7 @@ func TestParseBool(t *testing.T) {

for _, c := range cases {
t.Run(c.Value, func(t *testing.T) {
s, err := goclienthints.ParseBool(c.Value)
s, err := goclienthints.ParseBool([]byte(c.Value))

if c.IsSuccess {
require.NoError(t, err)
Expand Down

0 comments on commit 4c3977a

Please sign in to comment.