_utils.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459
  1. from typing import Union
  2. """
  3. _url.py
  4. websocket - WebSocket client library for Python
  5. Copyright 2024 engn33r
  6. Licensed under the Apache License, Version 2.0 (the "License");
  7. you may not use this file except in compliance with the License.
  8. You may obtain a copy of the License at
  9. http://www.apache.org/licenses/LICENSE-2.0
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. """
  16. __all__ = ["NoLock", "validate_utf8", "extract_err_message", "extract_error_code"]
  17. class NoLock:
  18. def __enter__(self) -> None:
  19. pass
  20. def __exit__(self, exc_type, exc_value, traceback) -> None:
  21. pass
  22. try:
  23. # If wsaccel is available we use compiled routines to validate UTF-8
  24. # strings.
  25. from wsaccel.utf8validator import Utf8Validator
  26. def _validate_utf8(utfbytes: Union[str, bytes]) -> bool:
  27. result: bool = Utf8Validator().validate(utfbytes)[0]
  28. return result
  29. except ImportError:
  30. # UTF-8 validator
  31. # python implementation of http://bjoern.hoehrmann.de/utf-8/decoder/dfa/
  32. _UTF8_ACCEPT = 0
  33. _UTF8_REJECT = 12
  34. _UTF8D = [
  35. # The first part of the table maps bytes to character classes that
  36. # to reduce the size of the transition table and create bitmasks.
  37. 0,
  38. 0,
  39. 0,
  40. 0,
  41. 0,
  42. 0,
  43. 0,
  44. 0,
  45. 0,
  46. 0,
  47. 0,
  48. 0,
  49. 0,
  50. 0,
  51. 0,
  52. 0,
  53. 0,
  54. 0,
  55. 0,
  56. 0,
  57. 0,
  58. 0,
  59. 0,
  60. 0,
  61. 0,
  62. 0,
  63. 0,
  64. 0,
  65. 0,
  66. 0,
  67. 0,
  68. 0,
  69. 0,
  70. 0,
  71. 0,
  72. 0,
  73. 0,
  74. 0,
  75. 0,
  76. 0,
  77. 0,
  78. 0,
  79. 0,
  80. 0,
  81. 0,
  82. 0,
  83. 0,
  84. 0,
  85. 0,
  86. 0,
  87. 0,
  88. 0,
  89. 0,
  90. 0,
  91. 0,
  92. 0,
  93. 0,
  94. 0,
  95. 0,
  96. 0,
  97. 0,
  98. 0,
  99. 0,
  100. 0,
  101. 0,
  102. 0,
  103. 0,
  104. 0,
  105. 0,
  106. 0,
  107. 0,
  108. 0,
  109. 0,
  110. 0,
  111. 0,
  112. 0,
  113. 0,
  114. 0,
  115. 0,
  116. 0,
  117. 0,
  118. 0,
  119. 0,
  120. 0,
  121. 0,
  122. 0,
  123. 0,
  124. 0,
  125. 0,
  126. 0,
  127. 0,
  128. 0,
  129. 0,
  130. 0,
  131. 0,
  132. 0,
  133. 0,
  134. 0,
  135. 0,
  136. 0,
  137. 0,
  138. 0,
  139. 0,
  140. 0,
  141. 0,
  142. 0,
  143. 0,
  144. 0,
  145. 0,
  146. 0,
  147. 0,
  148. 0,
  149. 0,
  150. 0,
  151. 0,
  152. 0,
  153. 0,
  154. 0,
  155. 0,
  156. 0,
  157. 0,
  158. 0,
  159. 0,
  160. 0,
  161. 0,
  162. 0,
  163. 0,
  164. 0,
  165. 1,
  166. 1,
  167. 1,
  168. 1,
  169. 1,
  170. 1,
  171. 1,
  172. 1,
  173. 1,
  174. 1,
  175. 1,
  176. 1,
  177. 1,
  178. 1,
  179. 1,
  180. 1,
  181. 9,
  182. 9,
  183. 9,
  184. 9,
  185. 9,
  186. 9,
  187. 9,
  188. 9,
  189. 9,
  190. 9,
  191. 9,
  192. 9,
  193. 9,
  194. 9,
  195. 9,
  196. 9,
  197. 7,
  198. 7,
  199. 7,
  200. 7,
  201. 7,
  202. 7,
  203. 7,
  204. 7,
  205. 7,
  206. 7,
  207. 7,
  208. 7,
  209. 7,
  210. 7,
  211. 7,
  212. 7,
  213. 7,
  214. 7,
  215. 7,
  216. 7,
  217. 7,
  218. 7,
  219. 7,
  220. 7,
  221. 7,
  222. 7,
  223. 7,
  224. 7,
  225. 7,
  226. 7,
  227. 7,
  228. 7,
  229. 8,
  230. 8,
  231. 2,
  232. 2,
  233. 2,
  234. 2,
  235. 2,
  236. 2,
  237. 2,
  238. 2,
  239. 2,
  240. 2,
  241. 2,
  242. 2,
  243. 2,
  244. 2,
  245. 2,
  246. 2,
  247. 2,
  248. 2,
  249. 2,
  250. 2,
  251. 2,
  252. 2,
  253. 2,
  254. 2,
  255. 2,
  256. 2,
  257. 2,
  258. 2,
  259. 2,
  260. 2,
  261. 10,
  262. 3,
  263. 3,
  264. 3,
  265. 3,
  266. 3,
  267. 3,
  268. 3,
  269. 3,
  270. 3,
  271. 3,
  272. 3,
  273. 3,
  274. 4,
  275. 3,
  276. 3,
  277. 11,
  278. 6,
  279. 6,
  280. 6,
  281. 5,
  282. 8,
  283. 8,
  284. 8,
  285. 8,
  286. 8,
  287. 8,
  288. 8,
  289. 8,
  290. 8,
  291. 8,
  292. 8,
  293. # The second part is a transition table that maps a combination
  294. # of a state of the automaton and a character class to a state.
  295. 0,
  296. 12,
  297. 24,
  298. 36,
  299. 60,
  300. 96,
  301. 84,
  302. 12,
  303. 12,
  304. 12,
  305. 48,
  306. 72,
  307. 12,
  308. 12,
  309. 12,
  310. 12,
  311. 12,
  312. 12,
  313. 12,
  314. 12,
  315. 12,
  316. 12,
  317. 12,
  318. 12,
  319. 12,
  320. 0,
  321. 12,
  322. 12,
  323. 12,
  324. 12,
  325. 12,
  326. 0,
  327. 12,
  328. 0,
  329. 12,
  330. 12,
  331. 12,
  332. 24,
  333. 12,
  334. 12,
  335. 12,
  336. 12,
  337. 12,
  338. 24,
  339. 12,
  340. 24,
  341. 12,
  342. 12,
  343. 12,
  344. 12,
  345. 12,
  346. 12,
  347. 12,
  348. 12,
  349. 12,
  350. 24,
  351. 12,
  352. 12,
  353. 12,
  354. 12,
  355. 12,
  356. 24,
  357. 12,
  358. 12,
  359. 12,
  360. 12,
  361. 12,
  362. 12,
  363. 12,
  364. 24,
  365. 12,
  366. 12,
  367. 12,
  368. 12,
  369. 12,
  370. 12,
  371. 12,
  372. 12,
  373. 12,
  374. 36,
  375. 12,
  376. 36,
  377. 12,
  378. 12,
  379. 12,
  380. 36,
  381. 12,
  382. 12,
  383. 12,
  384. 12,
  385. 12,
  386. 36,
  387. 12,
  388. 36,
  389. 12,
  390. 12,
  391. 12,
  392. 36,
  393. 12,
  394. 12,
  395. 12,
  396. 12,
  397. 12,
  398. 12,
  399. 12,
  400. 12,
  401. 12,
  402. 12,
  403. ]
  404. def _decode(state: int, codep: int, ch: int) -> tuple:
  405. tp = _UTF8D[ch]
  406. codep = (
  407. (ch & 0x3F) | (codep << 6) if (state != _UTF8_ACCEPT) else (0xFF >> tp) & ch
  408. )
  409. state = _UTF8D[256 + state + tp]
  410. return state, codep
  411. def _validate_utf8(utfbytes: Union[str, bytes]) -> bool:
  412. state = _UTF8_ACCEPT
  413. codep = 0
  414. for i in utfbytes:
  415. state, codep = _decode(state, codep, int(i))
  416. if state == _UTF8_REJECT:
  417. return False
  418. return True
  419. def validate_utf8(utfbytes: Union[str, bytes]) -> bool:
  420. """
  421. validate utf8 byte string.
  422. utfbytes: utf byte string to check.
  423. return value: if valid utf8 string, return true. Otherwise, return false.
  424. """
  425. return _validate_utf8(utfbytes)
  426. def extract_err_message(exception: Exception) -> Union[str, None]:
  427. if exception.args:
  428. exception_message: str = exception.args[0]
  429. return exception_message
  430. else:
  431. return None
  432. def extract_error_code(exception: Exception) -> Union[int, None]:
  433. if exception.args and len(exception.args) > 1:
  434. return exception.args[0] if isinstance(exception.args[0], int) else None