30 lines
754 B
Go
30 lines
754 B
Go
package service
|
|
|
|
import (
|
|
"strings"
|
|
)
|
|
|
|
// CookieValues returns all values for the given cookie name from a raw Cookie header.
|
|
// It preserves header order so callers can try candidates until one validates.
|
|
func CookieValues(rawHeader, cookieName string) []string {
|
|
rawHeader = strings.TrimSpace(rawHeader)
|
|
cookieName = strings.TrimSpace(cookieName)
|
|
if rawHeader == "" || cookieName == "" {
|
|
return nil
|
|
}
|
|
|
|
prefix := cookieName + "="
|
|
values := make([]string, 0, 2)
|
|
for _, part := range strings.Split(rawHeader, ";") {
|
|
part = strings.TrimSpace(part)
|
|
if !strings.HasPrefix(part, prefix) {
|
|
continue
|
|
}
|
|
value := strings.TrimSpace(strings.TrimPrefix(part, prefix))
|
|
if value != "" {
|
|
values = append(values, value)
|
|
}
|
|
}
|
|
return values
|
|
}
|