Skip to content

Commit 91cfd93

Browse files
committed
Merge pull request #733 from sodabrew/get_ssl_cipher
Add method Client#ssl_cipher to expose mysql_get_ssl_cipher
2 parents 32e8883 + b0d44c7 commit 91cfd93

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

ext/mysql2/client.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,26 @@ static VALUE rb_mysql_info(VALUE self) {
332332
return rb_str;
333333
}
334334

335+
static VALUE rb_mysql_get_ssl_cipher(VALUE self)
336+
{
337+
const char *cipher;
338+
VALUE rb_str;
339+
GET_CLIENT(self);
340+
341+
cipher = mysql_get_ssl_cipher(wrapper->client);
342+
343+
if (cipher == NULL) {
344+
return Qnil;
345+
}
346+
347+
rb_str = rb_str_new2(cipher);
348+
#ifdef HAVE_RUBY_ENCODING_H
349+
rb_enc_associate(rb_str, rb_utf8_encoding());
350+
#endif
351+
352+
return rb_str;
353+
}
354+
335355
static VALUE rb_connect(VALUE self, VALUE user, VALUE pass, VALUE host, VALUE port, VALUE database, VALUE socket, VALUE flags) {
336356
struct nogvl_connect_args args;
337357
time_t start_time, end_time, elapsed_time, connect_timeout;
@@ -1302,6 +1322,7 @@ void init_mysql2_client() {
13021322
rb_define_method(cMysql2Client, "reconnect=", set_reconnect, 1);
13031323
rb_define_method(cMysql2Client, "warning_count", rb_mysql_client_warning_count, 0);
13041324
rb_define_method(cMysql2Client, "query_info_string", rb_mysql_info, 0);
1325+
rb_define_method(cMysql2Client, "ssl_cipher", rb_mysql_get_ssl_cipher, 0);
13051326
#ifdef HAVE_RUBY_ENCODING_H
13061327
rb_define_method(cMysql2Client, "encoding", rb_mysql_client_encoding, 0);
13071328
#endif

spec/mysql2/client_spec.rb

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,12 @@ def connect(*args)
146146
# rubocop:enable Style/TrailingComma
147147
}.not_to raise_error
148148

149-
results = ssl_client.query("SHOW STATUS WHERE Variable_name = \"Ssl_version\" OR Variable_name = \"Ssl_cipher\"").to_a
150-
expect(results[0]['Variable_name']).to eql('Ssl_cipher')
151-
expect(results[0]['Value']).not_to be_nil
152-
expect(results[0]['Value']).to be_an_instance_of(String)
153-
expect(results[0]['Value']).not_to be_empty
154-
155-
expect(results[1]['Variable_name']).to eql('Ssl_version')
156-
expect(results[1]['Value']).not_to be_nil
157-
expect(results[1]['Value']).to be_an_instance_of(String)
158-
expect(results[1]['Value']).not_to be_empty
149+
results = Hash[ssl_client.query('SHOW STATUS WHERE Variable_name LIKE "Ssl_%"').map { |x| x.values_at('Variable_name', 'Value') }]
150+
expect(results['Ssl_cipher']).not_to be_empty
151+
expect(results['Ssl_version']).not_to be_empty
152+
153+
expect(ssl_client.ssl_cipher).not_to be_empty
154+
expect(results['Ssl_cipher']).to eql(ssl_client.ssl_cipher)
159155

160156
ssl_client.close
161157
end

0 commit comments

Comments
 (0)