update cookie.go to be aligned with quality standars

Former-commit-id: 54aea6a281106f60000645efc7f77ea04e72d7ac
This commit is contained in:
Gerasimos (Makis) Maropoulos 2018-04-22 13:59:21 +03:00 committed by GitHub
parent bb59ad8cf2
commit d4c20b8e2a

View File

@ -52,7 +52,7 @@ func RemoveCookie(ctx context.Context, config Config) {
cookie.MaxAge = -1
cookie.Value = ""
cookie.Path = "/"
cookie.Domain = FormatCookieDomain(ctx, config.DisableSubdomainPersistence)
cookie.Domain = formatCookieDomain(ctx, config.DisableSubdomainPersistence)
AddCookie(ctx, cookie, config.AllowReclaim)
@ -92,14 +92,19 @@ func IsValidCookieDomain(domain string) bool {
return true
}
func FormatCookieDomain(ctx context.Context, DisableSubdomainPersistence bool) string {
if !DisableSubdomainPersistence {
func formatCookieDomain(ctx context.Context, disableSubdomainPersistence bool) string {
if disableSubdomainPersistence {
return ""
}
requestDomain := ctx.Host()
if portIdx := strings.IndexByte(requestDomain, ':'); portIdx > 0 {
requestDomain = requestDomain[0:portIdx]
}
if IsValidCookieDomain(requestDomain) {
if !IsValidCookieDomain(requestDomain) {
return ""
}
// RFC2109, we allow level 1 subdomains, but no further
// if we have localhost.com , we want the localhost.cos.
@ -121,7 +126,4 @@ func FormatCookieDomain(ctx context.Context, DisableSubdomainPersistence bool) s
}
// finally set the .localhost.com (for(1-level) || .mysubdomain.localhost.com (for 2-level subdomain allow)
return "." + requestDomain // . to allow persistence
}
}
return ""
}