add NormalizeOKMessage() helper.

This commit is contained in:
fiatjaf 2023-11-28 22:23:51 -03:00
parent 7449f254db
commit ddc50028c3
No known key found for this signature in database
GPG Key ID: BAD43C4BE5C1A3A1

View File

@ -5,7 +5,8 @@ import (
"strings"
)
// NormalizeURL normalizes the url and replaces http://, https:// schemes by ws://, wss://.
// NormalizeURL normalizes the url and replaces http://, https:// schemes with ws://, wss://
// and normalizes the path.
func NormalizeURL(u string) string {
if u == "" {
return ""
@ -32,3 +33,12 @@ func NormalizeURL(u string) string {
return p.String()
}
// NormalizeOKMessage takes a string message that is to be sent in an `OK` or `CLOSED` command
// and prefixes it with "<prefix>: " if it doesn't already have an acceptable prefix.
func NormalizeOKMessage(reason string, prefix string) string {
if idx := strings.Index(reason, ": "); idx == -1 || strings.IndexByte(reason[0:idx], ' ') != -1 {
return prefix + ": " + reason
}
return reason
}