Improve user agent

This commit is contained in:
Pavel Škoda
2020-09-01 10:16:21 +02:00
committed by Michal Horejsek
parent 78fd73ee2a
commit 8a7b4bb919
2 changed files with 22 additions and 2 deletions

View File

@ -20,18 +20,33 @@ package pmapi
import (
"fmt"
"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 {
client := ""
if clientName != "" {
client = clientName
client = removeBrackets(clientName, "-")
if clientVersion != "" {
client += "/" + clientVersion
client += "/" + removeBrackets(clientVersion, "-")
}
}
if os == "" {
os = runtime.GOOS
}
os = removeBrackets(os, " ")
return fmt.Sprintf("%s (%s)", client, os)
}

View File

@ -48,3 +48,8 @@ func TestUpdateCurrentUserAgentClientNameAndVersion(t *testing.T) {
userAgent := formatUserAgent("mail", "ver", "os")
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)
}