]> 127.0.0.1 Git - part/.git/commitdiff
1 v0.28.20241111145249
authorqydysky <qydysky@foxmail.com>
Mon, 11 Nov 2024 14:35:56 +0000 (22:35 +0800)
committerqydysky <qydysky@foxmail.com>
Mon, 11 Nov 2024 14:35:56 +0000 (22:35 +0800)
reqf/cookie.go

index 743515085c7c3100f00d31b5856db516d6d42186..7e8cf1e9cecf7d87a5bfc5a1f9602a7810821b45 100644 (file)
@@ -2,6 +2,7 @@ package part
 
 import (
        "encoding/json"
+       "net"
        "net/http"
        "strings"
        "time"
@@ -146,3 +147,65 @@ func Cookies_List_2_String(Cookies []*http.Cookie) (o string) {
        o = string(t[:len(t)-2])
        return
 }
+
+func ValidCookieDomain(v string) bool {
+       if isCookieDomainName(v) {
+               return true
+       }
+       if net.ParseIP(v) != nil && !strings.Contains(v, ":") {
+               return true
+       }
+       return false
+}
+
+func isCookieDomainName(s string) bool {
+       if len(s) == 0 {
+               return false
+       }
+       if len(s) > 255 {
+               return false
+       }
+
+       if s[0] == '.' {
+               // A cookie a domain attribute may start with a leading dot.
+               s = s[1:]
+       }
+       last := byte('.')
+       ok := false // Ok once we've seen a letter.
+       partlen := 0
+       for i := 0; i < len(s); i++ {
+               c := s[i]
+               switch {
+               default:
+                       return false
+               case 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z':
+                       // No '_' allowed here (in contrast to package net).
+                       ok = true
+                       partlen++
+               case '0' <= c && c <= '9':
+                       // fine
+                       partlen++
+               case c == '-':
+                       // Byte before dash cannot be dot.
+                       if last == '.' {
+                               return false
+                       }
+                       partlen++
+               case c == '.':
+                       // Byte before dot cannot be dot, dash.
+                       if last == '.' || last == '-' {
+                               return false
+                       }
+                       if partlen > 63 || partlen == 0 {
+                               return false
+                       }
+                       partlen = 0
+               }
+               last = c
+       }
+       if last == '-' || partlen > 63 {
+               return false
+       }
+
+       return ok
+}