mirror of
https://github.com/ProtonMail/proton-bridge.git
synced 2025-12-10 12:46:46 +00:00
Improve user agent
This commit is contained in:
committed by
Michal Horejsek
parent
78fd73ee2a
commit
8a7b4bb919
@ -20,18 +20,33 @@ package pmapi
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// removeBrackets handle unwanted brackets in client identification string and join with given joinBy parameter
|
||||||
|
// Mac OS X Mail/13.0 (3601.0.4) -> Mac OS X Mail/13.0-3601.0.4 (joinBy = "-")
|
||||||
|
func removeBrackets(s string, joinBy string) (r string) {
|
||||||
|
r = strings.ReplaceAll(s, " (", joinBy)
|
||||||
|
r = strings.ReplaceAll(r, "(", joinBy) // Should be faster than regex.
|
||||||
|
r = strings.ReplaceAll(r, ")", "")
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
func formatUserAgent(clientName, clientVersion, os string) string {
|
func formatUserAgent(clientName, clientVersion, os string) string {
|
||||||
client := ""
|
client := ""
|
||||||
if clientName != "" {
|
if clientName != "" {
|
||||||
client = clientName
|
client = removeBrackets(clientName, "-")
|
||||||
if clientVersion != "" {
|
if clientVersion != "" {
|
||||||
client += "/" + clientVersion
|
client += "/" + removeBrackets(clientVersion, "-")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if os == "" {
|
if os == "" {
|
||||||
os = runtime.GOOS
|
os = runtime.GOOS
|
||||||
}
|
}
|
||||||
|
|
||||||
|
os = removeBrackets(os, " ")
|
||||||
|
|
||||||
return fmt.Sprintf("%s (%s)", client, os)
|
return fmt.Sprintf("%s (%s)", client, os)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -48,3 +48,8 @@ func TestUpdateCurrentUserAgentClientNameAndVersion(t *testing.T) {
|
|||||||
userAgent := formatUserAgent("mail", "ver", "os")
|
userAgent := formatUserAgent("mail", "ver", "os")
|
||||||
assert.Equal(t, "mail/ver (os)", userAgent)
|
assert.Equal(t, "mail/ver (os)", userAgent)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestRemoveBrackets(t *testing.T) {
|
||||||
|
userAgent := formatUserAgent("mail (submail)", "ver (subver)", "os (subos)")
|
||||||
|
assert.Equal(t, "mail-submail/ver-subver (os subos)", userAgent)
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user