forked from programaths/go-basex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
basex.go
153 lines (128 loc) · 2.7 KB
/
basex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package basex
import (
"bufio"
"bytes"
"crypto/md5"
"encoding/hex"
"errors"
"net"
"strings"
"sync"
)
type BaseXClient struct {
*bufio.ReadWriter
con net.Conn
bufPool *sync.Pool
}
func (b *BaseXClient) exec(cmd byte, arg string) {
b.ReadWriter.WriteByte(cmd)
b.send(arg)
}
func (b *BaseXClient) send(str string) {
strLen := len(str)
for i := 0; i < strLen; i++ {
if str[i] == 0 || str[i] == 255 {
b.ReadWriter.WriteByte(0xFF)
}
b.ReadWriter.WriteByte(str[i])
}
b.WriteByte(0)
b.ReadWriter.Flush()
}
func (b *BaseXClient) ok() bool {
d, _ := b.ReadWriter.ReadByte()
return d == 0
}
func New(addr string, user string, pass string) (cli *BaseXClient, err error) {
cli = &BaseXClient{
bufPool: &sync.Pool{
New: func() interface{} {
return bytes.NewBuffer(nil)
},
},
}
cli.con, err = net.Dial("tcp", addr)
if err != nil {
cli = nil
return
}
cli.ReadWriter = bufio.NewReadWriter(bufio.NewReader(cli.con), bufio.NewWriter(cli.con))
ts := cli.ReadString()
var ok bool
cli.send(user)
if i := strings.Index(ts, ":"); i != -1 {
ok = cli.login(user, pass, string(ts[:i]), string(ts[i+1:]))
} else {
ok = cli.loginLegacy(pass, ts)
}
if !ok {
err = errors.New("Login error")
cli = nil
}
return
}
func (b *BaseXClient) Close() {
b.con.Close()
}
func (b *BaseXClient) Command(cmd string) (string, string) {
b.send(cmd)
result := b.ReadString()
info := b.ReadString()
b.ok()
return result, info
}
func (b *BaseXClient) Query(qry string) *Query {
b.exec(0, qry)
id := b.ReadString()
if !b.ok() {
panic(b.ReadString())
}
return &Query{
id: id,
cli: b,
hasNext: false,
lastResult: "",
state: 0,
}
}
func (b *BaseXClient) WriteString(str string) {
b.ReadWriter.WriteString(str)
b.ReadWriter.WriteByte(0)
b.ReadWriter.Flush()
}
func (b *BaseXClient) WriteByte(bte byte) {
b.ReadWriter.WriteByte(bte)
b.Flush()
}
func (b *BaseXClient) ReadString() string {
// bytes.Buffer is a large structure, try to recycle one
buf := b.bufPool.Get().(*bytes.Buffer)
buf.Reset()
for {
d, err := b.ReadWriter.ReadByte()
if err != nil || d == 0 {
break
}
if d == 255 {
d, err = b.ReadWriter.ReadByte()
}
buf.WriteByte(d)
}
str := buf.String()
// Put the buffer back into the pool
b.bufPool.Put(buf)
return str
}
func (this *BaseXClient) login(user, password, realm, nonce string) bool {
this.send(md5Hex(md5Hex(user+":"+realm+":"+password) + nonce))
return this.ok()
}
func (this *BaseXClient) loginLegacy(password, nonce string) bool {
this.send(md5Hex(md5Hex(password) + nonce))
return this.ok()
}
func md5Hex(str string) string {
hash := md5.New()
hash.Write([]byte(str))
return hex.EncodeToString(hash.Sum(nil))
}