protocol.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. # Python implementation of low level MySQL client-server protocol
  2. # http://dev.mysql.com/doc/internals/en/client-server-protocol.html
  3. from .charset import MBLENGTH
  4. from .constants import FIELD_TYPE, SERVER_STATUS
  5. from . import err
  6. import struct
  7. import sys
  8. DEBUG = False
  9. NULL_COLUMN = 251
  10. UNSIGNED_CHAR_COLUMN = 251
  11. UNSIGNED_SHORT_COLUMN = 252
  12. UNSIGNED_INT24_COLUMN = 253
  13. UNSIGNED_INT64_COLUMN = 254
  14. def dump_packet(data): # pragma: no cover
  15. def printable(data):
  16. if 32 <= data < 127:
  17. return chr(data)
  18. return "."
  19. try:
  20. print("packet length:", len(data))
  21. for i in range(1, 7):
  22. f = sys._getframe(i)
  23. print("call[%d]: %s (line %d)" % (i, f.f_code.co_name, f.f_lineno))
  24. print("-" * 66)
  25. except ValueError:
  26. pass
  27. dump_data = [data[i : i + 16] for i in range(0, min(len(data), 256), 16)]
  28. for d in dump_data:
  29. print(
  30. " ".join(f"{x:02X}" for x in d)
  31. + " " * (16 - len(d))
  32. + " " * 2
  33. + "".join(printable(x) for x in d)
  34. )
  35. print("-" * 66)
  36. print()
  37. class MysqlPacket:
  38. """Representation of a MySQL response packet.
  39. Provides an interface for reading/parsing the packet results.
  40. """
  41. __slots__ = ("_position", "_data")
  42. def __init__(self, data, encoding):
  43. self._position = 0
  44. self._data = data
  45. def get_all_data(self):
  46. return self._data
  47. def read(self, size):
  48. """Read the first 'size' bytes in packet and advance cursor past them."""
  49. result = self._data[self._position : (self._position + size)]
  50. if len(result) != size:
  51. error = (
  52. "Result length not requested length:\n"
  53. f"Expected={size}. Actual={len(result)}. Position: {self._position}. Data Length: {len(self._data)}"
  54. )
  55. if DEBUG:
  56. print(error)
  57. self.dump()
  58. raise AssertionError(error)
  59. self._position += size
  60. return result
  61. def read_all(self):
  62. """Read all remaining data in the packet.
  63. (Subsequent read() will return errors.)
  64. """
  65. result = self._data[self._position :]
  66. self._position = None # ensure no subsequent read()
  67. return result
  68. def advance(self, length):
  69. """Advance the cursor in data buffer 'length' bytes."""
  70. new_position = self._position + length
  71. if new_position < 0 or new_position > len(self._data):
  72. raise Exception(
  73. f"Invalid advance amount ({length}) for cursor. Position={new_position}"
  74. )
  75. self._position = new_position
  76. def rewind(self, position=0):
  77. """Set the position of the data buffer cursor to 'position'."""
  78. if position < 0 or position > len(self._data):
  79. raise Exception("Invalid position to rewind cursor to: %s." % position)
  80. self._position = position
  81. def get_bytes(self, position, length=1):
  82. """Get 'length' bytes starting at 'position'.
  83. Position is start of payload (first four packet header bytes are not
  84. included) starting at index '0'.
  85. No error checking is done. If requesting outside end of buffer
  86. an empty string (or string shorter than 'length') may be returned!
  87. """
  88. return self._data[position : (position + length)]
  89. def read_uint8(self):
  90. result = self._data[self._position]
  91. self._position += 1
  92. return result
  93. def read_uint16(self):
  94. result = struct.unpack_from("<H", self._data, self._position)[0]
  95. self._position += 2
  96. return result
  97. def read_uint24(self):
  98. low, high = struct.unpack_from("<HB", self._data, self._position)
  99. self._position += 3
  100. return low + (high << 16)
  101. def read_uint32(self):
  102. result = struct.unpack_from("<I", self._data, self._position)[0]
  103. self._position += 4
  104. return result
  105. def read_uint64(self):
  106. result = struct.unpack_from("<Q", self._data, self._position)[0]
  107. self._position += 8
  108. return result
  109. def read_string(self):
  110. end_pos = self._data.find(b"\0", self._position)
  111. if end_pos < 0:
  112. return None
  113. result = self._data[self._position : end_pos]
  114. self._position = end_pos + 1
  115. return result
  116. def read_length_encoded_integer(self):
  117. """Read a 'Length Coded Binary' number from the data buffer.
  118. Length coded numbers can be anywhere from 1 to 9 bytes depending
  119. on the value of the first byte.
  120. """
  121. c = self.read_uint8()
  122. if c == NULL_COLUMN:
  123. return None
  124. if c < UNSIGNED_CHAR_COLUMN:
  125. return c
  126. elif c == UNSIGNED_SHORT_COLUMN:
  127. return self.read_uint16()
  128. elif c == UNSIGNED_INT24_COLUMN:
  129. return self.read_uint24()
  130. elif c == UNSIGNED_INT64_COLUMN:
  131. return self.read_uint64()
  132. def read_length_coded_string(self):
  133. """Read a 'Length Coded String' from the data buffer.
  134. A 'Length Coded String' consists first of a length coded
  135. (unsigned, positive) integer represented in 1-9 bytes followed by
  136. that many bytes of binary data. (For example "cat" would be "3cat".)
  137. """
  138. length = self.read_length_encoded_integer()
  139. if length is None:
  140. return None
  141. return self.read(length)
  142. def read_struct(self, fmt):
  143. s = struct.Struct(fmt)
  144. result = s.unpack_from(self._data, self._position)
  145. self._position += s.size
  146. return result
  147. def is_ok_packet(self):
  148. # https://dev.mysql.com/doc/internals/en/packet-OK_Packet.html
  149. return self._data[0] == 0 and len(self._data) >= 7
  150. def is_eof_packet(self):
  151. # http://dev.mysql.com/doc/internals/en/generic-response-packets.html#packet-EOF_Packet
  152. # Caution: \xFE may be LengthEncodedInteger.
  153. # If \xFE is LengthEncodedInteger header, 8bytes followed.
  154. return self._data[0] == 0xFE and len(self._data) < 9
  155. def is_auth_switch_request(self):
  156. # http://dev.mysql.com/doc/internals/en/connection-phase-packets.html#packet-Protocol::AuthSwitchRequest
  157. return self._data[0] == 0xFE
  158. def is_extra_auth_data(self):
  159. # https://dev.mysql.com/doc/internals/en/successful-authentication.html
  160. return self._data[0] == 1
  161. def is_resultset_packet(self):
  162. field_count = self._data[0]
  163. return 1 <= field_count <= 250
  164. def is_load_local_packet(self):
  165. return self._data[0] == 0xFB
  166. def is_error_packet(self):
  167. return self._data[0] == 0xFF
  168. def check_error(self):
  169. if self.is_error_packet():
  170. self.raise_for_error()
  171. def raise_for_error(self):
  172. self.rewind()
  173. self.advance(1) # field_count == error (we already know that)
  174. errno = self.read_uint16()
  175. if DEBUG:
  176. print("errno =", errno)
  177. err.raise_mysql_exception(self._data)
  178. def dump(self):
  179. dump_packet(self._data)
  180. class FieldDescriptorPacket(MysqlPacket):
  181. """A MysqlPacket that represents a specific column's metadata in the result.
  182. Parsing is automatically done and the results are exported via public
  183. attributes on the class such as: db, table_name, name, length, type_code.
  184. """
  185. def __init__(self, data, encoding):
  186. MysqlPacket.__init__(self, data, encoding)
  187. self._parse_field_descriptor(encoding)
  188. def _parse_field_descriptor(self, encoding):
  189. """Parse the 'Field Descriptor' (Metadata) packet.
  190. This is compatible with MySQL 4.1+ (not compatible with MySQL 4.0).
  191. """
  192. self.catalog = self.read_length_coded_string()
  193. self.db = self.read_length_coded_string()
  194. self.table_name = self.read_length_coded_string().decode(encoding)
  195. self.org_table = self.read_length_coded_string().decode(encoding)
  196. self.name = self.read_length_coded_string().decode(encoding)
  197. self.org_name = self.read_length_coded_string().decode(encoding)
  198. (
  199. self.charsetnr,
  200. self.length,
  201. self.type_code,
  202. self.flags,
  203. self.scale,
  204. ) = self.read_struct("<xHIBHBxx")
  205. # 'default' is a length coded binary and is still in the buffer?
  206. # not used for normal result sets...
  207. def description(self):
  208. """Provides a 7-item tuple compatible with the Python PEP249 DB Spec."""
  209. return (
  210. self.name,
  211. self.type_code,
  212. None, # TODO: display_length; should this be self.length?
  213. self.get_column_length(), # 'internal_size'
  214. self.get_column_length(), # 'precision' # TODO: why!?!?
  215. self.scale,
  216. self.flags % 2 == 0,
  217. )
  218. def get_column_length(self):
  219. if self.type_code == FIELD_TYPE.VAR_STRING:
  220. mblen = MBLENGTH.get(self.charsetnr, 1)
  221. return self.length // mblen
  222. return self.length
  223. def __str__(self):
  224. return "{} {!r}.{!r}.{!r}, type={}, flags={:x}".format(
  225. self.__class__,
  226. self.db,
  227. self.table_name,
  228. self.name,
  229. self.type_code,
  230. self.flags,
  231. )
  232. class OKPacketWrapper:
  233. """
  234. OK Packet Wrapper. It uses an existing packet object, and wraps
  235. around it, exposing useful variables while still providing access
  236. to the original packet objects variables and methods.
  237. """
  238. def __init__(self, from_packet):
  239. if not from_packet.is_ok_packet():
  240. raise ValueError(
  241. "Cannot create "
  242. + str(self.__class__.__name__)
  243. + " object from invalid packet type"
  244. )
  245. self.packet = from_packet
  246. self.packet.advance(1)
  247. self.affected_rows = self.packet.read_length_encoded_integer()
  248. self.insert_id = self.packet.read_length_encoded_integer()
  249. self.server_status, self.warning_count = self.read_struct("<HH")
  250. self.message = self.packet.read_all()
  251. self.has_next = self.server_status & SERVER_STATUS.SERVER_MORE_RESULTS_EXISTS
  252. def __getattr__(self, key):
  253. return getattr(self.packet, key)
  254. class EOFPacketWrapper:
  255. """
  256. EOF Packet Wrapper. It uses an existing packet object, and wraps
  257. around it, exposing useful variables while still providing access
  258. to the original packet objects variables and methods.
  259. """
  260. def __init__(self, from_packet):
  261. if not from_packet.is_eof_packet():
  262. raise ValueError(
  263. f"Cannot create '{self.__class__}' object from invalid packet type"
  264. )
  265. self.packet = from_packet
  266. self.warning_count, self.server_status = self.packet.read_struct("<xhh")
  267. if DEBUG:
  268. print("server_status=", self.server_status)
  269. self.has_next = self.server_status & SERVER_STATUS.SERVER_MORE_RESULTS_EXISTS
  270. def __getattr__(self, key):
  271. return getattr(self.packet, key)
  272. class LoadLocalPacketWrapper:
  273. """
  274. Load Local Packet Wrapper. It uses an existing packet object, and wraps
  275. around it, exposing useful variables while still providing access
  276. to the original packet objects variables and methods.
  277. """
  278. def __init__(self, from_packet):
  279. if not from_packet.is_load_local_packet():
  280. raise ValueError(
  281. f"Cannot create '{self.__class__}' object from invalid packet type"
  282. )
  283. self.packet = from_packet
  284. self.filename = self.packet.get_all_data()[1:]
  285. if DEBUG:
  286. print("filename=", self.filename)
  287. def __getattr__(self, key):
  288. return getattr(self.packet, key)