Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
nfjBill committed Feb 16, 2022
1 parent 8a4accb commit e839640
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ db, err := gorm.Open(dm.Open(dsn), &gorm.Config{})

## 入门

- `clone`本项目到本地,进入项目目录
- `clone`本项目到本地,进入项目目录,打开`dm_test.go`
- 修改`dsn`调整为本地正确信息
- 运行`go test -v`
- 如果无报错,参考`dm_test.go`使用即可
Expand Down
3 changes: 2 additions & 1 deletion dm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ var db *gorm.DB

func init() {
var err error
dsn := "dm://sysdba:[email protected]:5236?autoCommit=true"
//dsn := "dm://sysdba:[email protected]:5236?autoCommit=true"
dsn := "dm://sysdba:[email protected]:5237?autoCommit=true"
db, err = gorm.Open(Open(dsn), &gorm.Config{
DisableForeignKeyConstraintWhenMigrating: true,
})
Expand Down
57 changes: 56 additions & 1 deletion dmr/n.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"context"
"database/sql/driver"
"fmt"
"net"
"net/url"
"os"
Expand All @@ -16,6 +17,7 @@ import (
"runtime"
"strconv"
"strings"
"unicode/utf8"

"github.com/nfjBill/gorm-driver-dm/dmr/util"
)
Expand Down Expand Up @@ -778,7 +780,9 @@ func (c *DmConnector) mergeConfigs(dsn string) error {
c.group = group
} else {
host, port, err := net.SplitHostPort(host)
if err != nil || net.ParseIP(host) == nil {
errDomain := checkDomain(host)
isDomain := errDomain == nil
if err != nil || (net.ParseIP(host) == nil && !isDomain) {
c.host = hostDef
} else {
c.host = host
Expand Down Expand Up @@ -895,3 +899,54 @@ func (c *DmConnector) connectSingle(ctx context.Context) (*DmConnection, error)

return dc, nil
}

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
}

0 comments on commit e839640

Please sign in to comment.