Skip to content

Commit 735aad5

Browse files
authored
Merge pull request #588 from skoef/clientCharsetCapability
expose capability and charset of connections to server
2 parents a4425c6 + 7b7fbf2 commit 735aad5

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

server/conn.go

+9
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type Conn struct {
1717

1818
serverConf *Server
1919
capability uint32
20+
charset uint8
2021
authPluginName string
2122
connectionID uint32
2223
status uint16
@@ -133,6 +134,14 @@ func (c *Conn) GetUser() string {
133134
return c.user
134135
}
135136

137+
func (c *Conn) Capability() uint32 {
138+
return c.capability
139+
}
140+
141+
func (c *Conn) Charset() uint8 {
142+
return c.charset
143+
}
144+
136145
func (c *Conn) ConnectionID() uint32 {
137146
return c.connectionID
138147
}

server/handshake_resp.go

+6-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ func (c *Conn) readFirstPart() ([]byte, int, error) {
5050
return nil, 0, err
5151
}
5252

53+
return c.decodeFirstPart(data)
54+
}
55+
56+
func (c *Conn) decodeFirstPart(data []byte) ([]byte, int, error) {
5357
pos := 0
5458

5559
// check CLIENT_PROTOCOL_41
@@ -67,8 +71,8 @@ func (c *Conn) readFirstPart() ([]byte, int, error) {
6771
//skip max packet size
6872
pos += 4
6973

70-
//charset, skip, if you want to use another charset, use set names
71-
//c.collation = CollationId(data[pos])
74+
// connection's default character set as defined
75+
c.charset = data[pos]
7276
pos++
7377

7478
//skip reserved 23[00]

server/handshake_resp_test.go

+24
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package server
22

33
import (
4+
"bytes"
45
"testing"
56

67
"github.com/go-mysql-org/go-mysql/mysql"
@@ -28,3 +29,26 @@ func TestReadAuthData(t *testing.T) {
2829
t.Fatalf("expected %d read bytes, got %d", len(data)-1, readBytes)
2930
}
3031
}
32+
33+
func TestDecodeFirstPart(t *testing.T) {
34+
data := []byte{141, 174, 255, 1, 0, 0, 0, 1, 8}
35+
36+
c := &Conn{}
37+
38+
result, pos, err := c.decodeFirstPart(data)
39+
if err != nil {
40+
t.Fatalf("expected nil error, got %v", err)
41+
}
42+
if !bytes.Equal(result, data) {
43+
t.Fatal("expected same data, got something else")
44+
}
45+
if pos != 32 {
46+
t.Fatalf("unexpected pos, got %d", pos)
47+
}
48+
if c.capability != 33533581 {
49+
t.Fatalf("unexpected capability, got %d", c.capability)
50+
}
51+
if c.charset != 8 {
52+
t.Fatalf("unexpected capability, got %d", c.capability)
53+
}
54+
}

0 commit comments

Comments
 (0)