mirror of
https://github.com/casjaysdevdocker/ifconfig
synced 2025-09-18 03:57:52 -04:00
🦈🏠🐜❗ Initial Commit ❗🐜🦈🏠
This commit is contained in:
40
useragent/useragent.go
Normal file
40
useragent/useragent.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package useragent
|
||||
|
||||
import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
type UserAgent struct {
|
||||
Product string `json:"product,omitempty"`
|
||||
Version string `json:"version,omitempty"`
|
||||
Comment string `json:"comment,omitempty"`
|
||||
RawValue string `json:"raw_value,omitempty"`
|
||||
}
|
||||
|
||||
func Parse(s string) UserAgent {
|
||||
parts := strings.SplitN(s, "/", 2)
|
||||
var version, comment string
|
||||
if len(parts) > 1 {
|
||||
// If first character is a number, treat it as version
|
||||
if len(parts[1]) > 0 && parts[1][0] >= 48 && parts[1][0] <= 57 {
|
||||
rest := strings.SplitN(parts[1], " ", 2)
|
||||
version = rest[0]
|
||||
if len(rest) > 1 {
|
||||
comment = rest[1]
|
||||
}
|
||||
} else {
|
||||
comment = parts[1]
|
||||
}
|
||||
} else {
|
||||
parts = strings.SplitN(s, " ", 2)
|
||||
if len(parts) > 1 {
|
||||
comment = parts[1]
|
||||
}
|
||||
}
|
||||
return UserAgent{
|
||||
Product: parts[0],
|
||||
Version: version,
|
||||
Comment: comment,
|
||||
RawValue: s,
|
||||
}
|
||||
}
|
39
useragent/useragent_test.go
Normal file
39
useragent/useragent_test.go
Normal file
@@ -0,0 +1,39 @@
|
||||
package useragent
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestParse(t *testing.T) {
|
||||
var tests = []struct {
|
||||
in string
|
||||
out UserAgent
|
||||
}{
|
||||
{"", UserAgent{}},
|
||||
{"curl/", UserAgent{Product: "curl"}},
|
||||
{"curl/foo", UserAgent{Product: "curl", Comment: "foo"}},
|
||||
{"curl/7.26.0", UserAgent{Product: "curl", Version: "7.26.0"}},
|
||||
{"Wget/1.13.4 (linux-gnu)", UserAgent{Product: "Wget", Version: "1.13.4", Comment: "(linux-gnu)"}},
|
||||
{"Wget", UserAgent{Product: "Wget"}},
|
||||
{"fetch libfetch/2.0", UserAgent{Product: "fetch libfetch", Version: "2.0"}},
|
||||
{"Go 1.1 package http", UserAgent{Product: "Go", Comment: "1.1 package http"}},
|
||||
{"Mikrotik/6.x Fetch", UserAgent{Product: "Mikrotik", Version: "6.x", Comment: "Fetch"}},
|
||||
{"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_4) " +
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
|
||||
"Safari/537.36", UserAgent{Product: "Mozilla", Version: "5.0", Comment: "(Macintosh; Intel Mac OS X 10_8_4) " +
|
||||
"AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.28 " +
|
||||
"Safari/537.36"}},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
ua := Parse(tt.in)
|
||||
if got := ua.Product; got != tt.out.Product {
|
||||
t.Errorf("got Product=%q for %q, want %q", got, tt.in, tt.out.Product)
|
||||
}
|
||||
if got := ua.Version; got != tt.out.Version {
|
||||
t.Errorf("got Version=%q for %q, want %q", got, tt.in, tt.out.Version)
|
||||
}
|
||||
if got := ua.Comment; got != tt.out.Comment {
|
||||
t.Errorf("got Comment=%q for %q, want %q", got, tt.in, tt.out.Comment)
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user