Skip to content

Commit 23b0fea

Browse files
authored
Fix SSL connection handshake charset not respecting client configuration (#776)
fixes #775, closes #648
1 parent fe0120b commit 23b0fea

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

CHANGES.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
Changes
22
-------
33

4+
next (unreleased)
5+
^^^^^^^^^^^^^^^^^
6+
7+
* Fix SSL connection handshake charset not respecting client configuration #776
8+
49
0.1.0 (2022-04-11)
510
^^^^^^^^^^^^^^^^^^
611

aiomysql/connection.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -733,12 +733,12 @@ async def _request_authentication(self):
733733
if self.user is None:
734734
raise ValueError("Did not specify a username")
735735

736-
if self._ssl_context and self.server_capabilities & CLIENT.SSL:
737-
# capablities, max packet, charset
738-
data = struct.pack('<IIB', self.client_flag, 16777216, 33)
739-
data += b'\x00' * (32 - len(data))
736+
charset_id = charset_by_name(self.charset).id
737+
data_init = struct.pack('<iIB23s', self.client_flag, MAX_PACKET_LEN,
738+
charset_id, b'')
740739

741-
self.write_packet(data)
740+
if self._ssl_context and self.server_capabilities & CLIENT.SSL:
741+
self.write_packet(data_init)
742742

743743
# Stop sending events to data_received
744744
self._writer.transport.pause_reading()
@@ -762,15 +762,11 @@ async def _request_authentication(self):
762762

763763
self._secure = True
764764

765-
charset_id = charset_by_name(self.charset).id
766765
if isinstance(self.user, str):
767766
_user = self.user.encode(self.encoding)
768767
else:
769768
_user = self.user
770769

771-
data_init = struct.pack('<iIB23s', self.client_flag, MAX_PACKET_LEN,
772-
charset_id, b'')
773-
774770
data = data_init + _user + b'\0'
775771

776772
authresp = b''

tests/test_basic.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,20 @@ async def test_string(cursor, table_cleanup):
106106
assert (test_value,) == r
107107

108108

109+
@pytest.mark.run_loop
110+
async def test_string_with_emoji(cursor, table_cleanup):
111+
await cursor.execute("DROP TABLE IF EXISTS test_string_with_emoji;")
112+
await cursor.execute("CREATE TABLE test_string_with_emoji (a text) "
113+
"DEFAULT CHARACTER SET=\"utf8mb4\"")
114+
test_value = "I am a test string with emoji 😄"
115+
table_cleanup('test_string_with_emoji')
116+
await cursor.execute("INSERT INTO test_string_with_emoji (a) VALUES (%s)",
117+
test_value)
118+
await cursor.execute("SELECT a FROM test_string_with_emoji")
119+
r = await cursor.fetchone()
120+
assert (test_value,) == r
121+
122+
109123
@pytest.mark.run_loop
110124
async def test_integer(cursor, table_cleanup):
111125
await cursor.execute("CREATE TABLE test_integer (a INTEGER)")

0 commit comments

Comments
 (0)