Skip to content

Instantly share code, notes, and snippets.

@chmike
Last active August 20, 2024 02:29
Show Gist options
  • Select an option

  • Save chmike/d4126a3247a6d9a70922fc0e8b4f4013 to your computer and use it in GitHub Desktop.

Select an option

Save chmike/d4126a3247a6d9a70922fc0e8b4f4013 to your computer and use it in GitHub Desktop.

Revisions

  1. chmike revised this gist Oct 28, 2023. 1 changed file with 13 additions and 13 deletions.
    26 changes: 13 additions & 13 deletions checkDomain.go
    Original file line number Diff line number Diff line change
    @@ -1,14 +1,14 @@
    // Please use the package https://github.com/chmike/domain as is it maintained up to date with tests.

    // checkDomain returns an error if the domain name is not valid
    // checkDomain returns an error if the domain name is not valid.
    // See https://tools.ietf.org/html/rfc1034#section-3.5 and
    // https://tools.ietf.org/html/rfc1123#section-2.
    func checkDomain(name string) error {
    switch {
    case len(name) == 0:
    return nil // an empty domain name will result in a cookie without a domain restriction
    case len(name) > 255:
    return fmt.Errorf("cookie domain: name length is %d, can't exceed 255", len(name))
    return fmt.Errorf("domain name length is %d, can't exceed 255", len(name))
    }
    var l int
    for i := 0; i < len(name); i++ {
    @@ -17,13 +17,13 @@ func checkDomain(name string) error {
    // check domain labels validity
    switch {
    case i == l:
    return fmt.Errorf("cookie domain: invalid character '%c' at offset %d: label can't begin with a period", b, i)
    return fmt.Errorf("domain has invalid character '.' at offset %d, label can't begin with a period", i)
    case i-l > 63:
    return fmt.Errorf("cookie domain: byte length of label '%s' is %d, can't exceed 63", name[l:i], i-l)
    return fmt.Errorf("domain byte length of label '%s' is %d, can't exceed 63", name[l:i], i-l)
    case name[l] == '-':
    return fmt.Errorf("cookie domain: label '%s' at offset %d begins with a hyphen", name[l:i], l)
    return fmt.Errorf("domain label '%s' at offset %d begins with a hyphen", name[l:i], l)
    case name[i-1] == '-':
    return fmt.Errorf("cookie domain: label '%s' at offset %d ends with a hyphen", name[l:i], l)
    return fmt.Errorf("domain label '%s' at offset %d ends with a hyphen", name[l:i], l)
    }
    l = i + 1
    continue
    @@ -33,23 +33,23 @@ func checkDomain(name string) error {
    // show the printable unicode character starting at byte offset i
    c, _ := utf8.DecodeRuneInString(name[i:])
    if c == utf8.RuneError {
    return fmt.Errorf("cookie domain: invalid rune at offset %d", i)
    return fmt.Errorf("domain has invalid rune at offset %d", i)
    }
    return fmt.Errorf("cookie domain: invalid character '%c' at offset %d", c, i)
    return fmt.Errorf("domain has invalid character '%c' at offset %d", c, i)
    }
    }
    // check top level domain validity
    switch {
    case l == len(name):
    return fmt.Errorf("cookie domain: missing top level domain, domain can't end with a period")
    return fmt.Errorf("domain has missing top level domain, domain can't end with a period")
    case len(name)-l > 63:
    return fmt.Errorf("cookie domain: byte length of top level domain '%s' is %d, can't exceed 63", name[l:], len(name)-l)
    return fmt.Errorf("domain's top level domain '%s' has byte length %d, can't exceed 63", name[l:], len(name)-l)
    case name[l] == '-':
    return fmt.Errorf("cookie domain: top level domain '%s' at offset %d begins with a hyphen", name[l:], l)
    return fmt.Errorf("domain's top level domain '%s' at offset %d begin with a hyphen", name[l:], l)
    case name[len(name)-1] == '-':
    return fmt.Errorf("cookie domain: top level domain '%s' at offset %d ends with a hyphen", name[l:], l)
    return fmt.Errorf("domain's top level domain '%s' at offset %d ends with a hyphen", name[l:], l)
    case name[l] >= '0' && name[l] <= '9':
    return fmt.Errorf("cookie domain: top level domain '%s' at offset %d begins with a digit", name[l:], l)
    return fmt.Errorf("domain's top level domain '%s' at offset %d begins with a digit", name[l:], l)
    }
    return nil
    }
  2. chmike revised this gist Oct 28, 2023. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions checkDomain.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Please use the package https://github.com/chmike/domain as is it maintained up to date with tests.

    // checkDomain returns an error if the domain name is not valid
    // See https://tools.ietf.org/html/rfc1034#section-3.5 and
    // https://tools.ietf.org/html/rfc1123#section-2.
  3. chmike created this gist Jun 29, 2018.
    53 changes: 53 additions & 0 deletions checkDomain.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    // checkDomain returns an error if the domain name is not valid
    // See https://tools.ietf.org/html/rfc1034#section-3.5 and
    // https://tools.ietf.org/html/rfc1123#section-2.
    func checkDomain(name string) error {
    switch {
    case len(name) == 0:
    return nil // an empty domain name will result in a cookie without a domain restriction
    case len(name) > 255:
    return fmt.Errorf("cookie domain: name length is %d, can't exceed 255", len(name))
    }
    var l int
    for i := 0; i < len(name); i++ {
    b := name[i]
    if b == '.' {
    // check domain labels validity
    switch {
    case i == l:
    return fmt.Errorf("cookie domain: invalid character '%c' at offset %d: label can't begin with a period", b, i)
    case i-l > 63:
    return fmt.Errorf("cookie domain: byte length of label '%s' is %d, can't exceed 63", name[l:i], i-l)
    case name[l] == '-':
    return fmt.Errorf("cookie domain: label '%s' at offset %d begins with a hyphen", name[l:i], l)
    case name[i-1] == '-':
    return fmt.Errorf("cookie domain: label '%s' at offset %d ends with a hyphen", name[l:i], l)
    }
    l = i + 1
    continue
    }
    // test label character validity, note: tests are ordered by decreasing validity frequency
    if !(b >= 'a' && b <= 'z' || b >= '0' && b <= '9' || b == '-' || b >= 'A' && b <= 'Z') {
    // show the printable unicode character starting at byte offset i
    c, _ := utf8.DecodeRuneInString(name[i:])
    if c == utf8.RuneError {
    return fmt.Errorf("cookie domain: invalid rune at offset %d", i)
    }
    return fmt.Errorf("cookie domain: invalid character '%c' at offset %d", c, i)
    }
    }
    // check top level domain validity
    switch {
    case l == len(name):
    return fmt.Errorf("cookie domain: missing top level domain, domain can't end with a period")
    case len(name)-l > 63:
    return fmt.Errorf("cookie domain: byte length of top level domain '%s' is %d, can't exceed 63", name[l:], len(name)-l)
    case name[l] == '-':
    return fmt.Errorf("cookie domain: top level domain '%s' at offset %d begins with a hyphen", name[l:], l)
    case name[len(name)-1] == '-':
    return fmt.Errorf("cookie domain: top level domain '%s' at offset %d ends with a hyphen", name[l:], l)
    case name[l] >= '0' && name[l] <= '9':
    return fmt.Errorf("cookie domain: top level domain '%s' at offset %d begins with a digit", name[l:], l)
    }
    return nil
    }