Skip to content

expose capability and charset of connections to server #588

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type Conn struct {

serverConf *Server
capability uint32
charset uint8
authPluginName string
connectionID uint32
status uint16
Expand Down Expand Up @@ -133,6 +134,14 @@ func (c *Conn) GetUser() string {
return c.user
}

func (c *Conn) Capability() uint32 {
return c.capability
}

func (c *Conn) Charset() uint8 {
return c.charset
}

func (c *Conn) ConnectionID() uint32 {
return c.connectionID
}
Expand Down
8 changes: 6 additions & 2 deletions server/handshake_resp.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ func (c *Conn) readFirstPart() ([]byte, int, error) {
return nil, 0, err
}

return c.decodeFirstPart(data)
}

func (c *Conn) decodeFirstPart(data []byte) ([]byte, int, error) {
pos := 0

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

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

//skip reserved 23[00]
Expand Down
24 changes: 24 additions & 0 deletions server/handshake_resp_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package server

import (
"bytes"
"testing"

"github.com/go-mysql-org/go-mysql/mysql"
Expand Down Expand Up @@ -28,3 +29,26 @@ func TestReadAuthData(t *testing.T) {
t.Fatalf("expected %d read bytes, got %d", len(data)-1, readBytes)
}
}

func TestDecodeFirstPart(t *testing.T) {
data := []byte{141, 174, 255, 1, 0, 0, 0, 1, 8}

c := &Conn{}

result, pos, err := c.decodeFirstPart(data)
if err != nil {
t.Fatalf("expected nil error, got %v", err)
}
if !bytes.Equal(result, data) {
t.Fatal("expected same data, got something else")
}
if pos != 32 {
t.Fatalf("unexpected pos, got %d", pos)
}
if c.capability != 33533581 {
t.Fatalf("unexpected capability, got %d", c.capability)
}
if c.charset != 8 {
t.Fatalf("unexpected capability, got %d", c.capability)
}
}