Skip to content

Commit 403f48f

Browse files
authored
Merge pull request #616 from skoef/clientCaps
allow to set certain client-side capabilities
2 parents 6ede102 + ee1c11a commit 403f48f

File tree

3 files changed

+59
-2
lines changed

3 files changed

+59
-2
lines changed

client/auth.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,18 @@ func (c *Conn) writeAuthHandshake() error {
140140
if !authPluginAllowed(c.authPluginName) {
141141
return fmt.Errorf("unknow auth plugin name '%s'", c.authPluginName)
142142
}
143-
// Adjust client capability flags based on server support
143+
144+
// Set default client capabilities that reflect the abilities of this library
144145
capability := CLIENT_PROTOCOL_41 | CLIENT_SECURE_CONNECTION |
145-
CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_PLUGIN_AUTH | c.capability&CLIENT_LONG_FLAG
146+
CLIENT_LONG_PASSWORD | CLIENT_TRANSACTIONS | CLIENT_PLUGIN_AUTH
147+
// Adjust client capability flags based on server support
148+
capability |= c.capability & CLIENT_LONG_FLAG
149+
// Adjust client capability flags on specific client requests
150+
// Only flags that would make any sense setting and aren't handled elsewhere
151+
// in the library are supported here
152+
capability |= c.ccaps&CLIENT_FOUND_ROWS | c.ccaps&CLIENT_IGNORE_SPACE |
153+
c.ccaps&CLIENT_MULTI_STATEMENTS | c.ccaps&CLIENT_MULTI_RESULTS |
154+
c.ccaps&CLIENT_PS_MULTI_RESULTS | c.ccaps&CLIENT_CONNECT_ATTRS
146155

147156
// To enable TLS / SSL
148157
if c.tlsConfig != nil {

client/client_test.go

+35
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,41 @@ func (s *clientTestSuite) TestConn_Ping(c *C) {
9494
c.Assert(err, IsNil)
9595
}
9696

97+
func (s *clientTestSuite) TestConn_SetCapability(c *C) {
98+
caps := []uint32{
99+
mysql.CLIENT_LONG_PASSWORD,
100+
mysql.CLIENT_FOUND_ROWS,
101+
mysql.CLIENT_LONG_FLAG,
102+
mysql.CLIENT_CONNECT_WITH_DB,
103+
mysql.CLIENT_NO_SCHEMA,
104+
mysql.CLIENT_COMPRESS,
105+
mysql.CLIENT_ODBC,
106+
mysql.CLIENT_LOCAL_FILES,
107+
mysql.CLIENT_IGNORE_SPACE,
108+
mysql.CLIENT_PROTOCOL_41,
109+
mysql.CLIENT_INTERACTIVE,
110+
mysql.CLIENT_SSL,
111+
mysql.CLIENT_IGNORE_SIGPIPE,
112+
mysql.CLIENT_TRANSACTIONS,
113+
mysql.CLIENT_RESERVED,
114+
mysql.CLIENT_SECURE_CONNECTION,
115+
mysql.CLIENT_MULTI_STATEMENTS,
116+
mysql.CLIENT_MULTI_RESULTS,
117+
mysql.CLIENT_PS_MULTI_RESULTS,
118+
mysql.CLIENT_PLUGIN_AUTH,
119+
mysql.CLIENT_CONNECT_ATTRS,
120+
mysql.CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA,
121+
}
122+
123+
for _, cap := range caps {
124+
c.Assert(s.c.ccaps&cap > 0, IsFalse)
125+
s.c.SetCapability(cap)
126+
c.Assert(s.c.ccaps&cap > 0, IsTrue)
127+
s.c.UnsetCapability(cap)
128+
c.Assert(s.c.ccaps&cap > 0, IsFalse)
129+
}
130+
}
131+
97132
// NOTE for MySQL 5.5 and 5.6, server side has to config SSL to pass the TLS test, otherwise, it will throw error that
98133
// MySQL server does not support TLS required by the client. However, for MySQL 5.7 and above, auto generated certificates
99134
// are used by default so that manual config is no longer necessary.

client/conn.go

+13
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,10 @@ type Conn struct {
2121
tlsConfig *tls.Config
2222
proto string
2323

24+
// server capabilities
2425
capability uint32
26+
// client-set capabilities only
27+
ccaps uint32
2528

2629
status uint16
2730

@@ -120,6 +123,16 @@ func (c *Conn) Ping() error {
120123
return nil
121124
}
122125

126+
// SetCapability enables the use of a specific capability
127+
func (c *Conn) SetCapability(cap uint32) {
128+
c.ccaps |= cap
129+
}
130+
131+
// UnsetCapability disables the use of a specific capability
132+
func (c *Conn) UnsetCapability(cap uint32) {
133+
c.ccaps &= ^cap
134+
}
135+
123136
// UseSSL: use default SSL
124137
// pass to options when connect
125138
func (c *Conn) UseSSL(insecureSkipVerify bool) {

0 commit comments

Comments
 (0)