iostream.py 65 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681
  1. #
  2. # Copyright 2009 Facebook
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License"); you may
  5. # not use this file except in compliance with the License. You may obtain
  6. # a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. # License for the specific language governing permissions and limitations
  14. # under the License.
  15. """Utility classes to write to and read from non-blocking files and sockets.
  16. Contents:
  17. * `BaseIOStream`: Generic interface for reading and writing.
  18. * `IOStream`: Implementation of BaseIOStream using non-blocking sockets.
  19. * `SSLIOStream`: SSL-aware version of IOStream.
  20. * `PipeIOStream`: Pipe-based IOStream implementation.
  21. """
  22. import asyncio
  23. import collections
  24. import errno
  25. import io
  26. import numbers
  27. import os
  28. import socket
  29. import ssl
  30. import sys
  31. import re
  32. from tornado.concurrent import Future, future_set_result_unless_cancelled
  33. from tornado import ioloop
  34. from tornado.log import gen_log
  35. from tornado.netutil import ssl_wrap_socket, _client_ssl_defaults, _server_ssl_defaults
  36. from tornado.util import errno_from_exception
  37. import typing
  38. from typing import (
  39. Union,
  40. Optional,
  41. Awaitable,
  42. Callable,
  43. Pattern,
  44. Any,
  45. Dict,
  46. TypeVar,
  47. Tuple,
  48. )
  49. from types import TracebackType
  50. if typing.TYPE_CHECKING:
  51. from typing import Deque, List, Type # noqa: F401
  52. _IOStreamType = TypeVar("_IOStreamType", bound="IOStream")
  53. try:
  54. from tornado.platform.posix import _set_nonblocking
  55. except ImportError:
  56. _set_nonblocking = None # type: ignore
  57. # These errnos indicate that a non-blocking operation must be retried
  58. # at a later time. On most platforms they're the same value, but on
  59. # some they differ.
  60. _ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)
  61. if hasattr(errno, "WSAEWOULDBLOCK"):
  62. _ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,) # type: ignore
  63. # These errnos indicate that a connection has been abruptly terminated.
  64. # They should be caught and handled less noisily than other errors.
  65. _ERRNO_CONNRESET = (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE, errno.ETIMEDOUT)
  66. if hasattr(errno, "WSAECONNRESET"):
  67. _ERRNO_CONNRESET += ( # type: ignore
  68. errno.WSAECONNRESET, # type: ignore
  69. errno.WSAECONNABORTED, # type: ignore
  70. errno.WSAETIMEDOUT, # type: ignore
  71. )
  72. if sys.platform == "darwin":
  73. # OSX appears to have a race condition that causes send(2) to return
  74. # EPROTOTYPE if called while a socket is being torn down:
  75. # http://erickt.github.io/blog/2014/11/19/adventures-in-debugging-a-potential-osx-kernel-bug/
  76. # Since the socket is being closed anyway, treat this as an ECONNRESET
  77. # instead of an unexpected error.
  78. _ERRNO_CONNRESET += (errno.EPROTOTYPE,) # type: ignore
  79. # More non-portable errnos:
  80. _ERRNO_INPROGRESS = (errno.EINPROGRESS,)
  81. if hasattr(errno, "WSAEINPROGRESS"):
  82. _ERRNO_INPROGRESS += (errno.WSAEINPROGRESS,) # type: ignore
  83. _WINDOWS = sys.platform.startswith("win")
  84. class StreamClosedError(IOError):
  85. """Exception raised by `IOStream` methods when the stream is closed.
  86. Note that the close callback is scheduled to run *after* other
  87. callbacks on the stream (to allow for buffered data to be processed),
  88. so you may see this error before you see the close callback.
  89. The ``real_error`` attribute contains the underlying error that caused
  90. the stream to close (if any).
  91. .. versionchanged:: 4.3
  92. Added the ``real_error`` attribute.
  93. """
  94. def __init__(self, real_error: BaseException = None) -> None:
  95. super(StreamClosedError, self).__init__("Stream is closed")
  96. self.real_error = real_error
  97. class UnsatisfiableReadError(Exception):
  98. """Exception raised when a read cannot be satisfied.
  99. Raised by ``read_until`` and ``read_until_regex`` with a ``max_bytes``
  100. argument.
  101. """
  102. pass
  103. class StreamBufferFullError(Exception):
  104. """Exception raised by `IOStream` methods when the buffer is full.
  105. """
  106. class _StreamBuffer(object):
  107. """
  108. A specialized buffer that tries to avoid copies when large pieces
  109. of data are encountered.
  110. """
  111. def __init__(self) -> None:
  112. # A sequence of (False, bytearray) and (True, memoryview) objects
  113. self._buffers = (
  114. collections.deque()
  115. ) # type: Deque[Tuple[bool, Union[bytearray, memoryview]]]
  116. # Position in the first buffer
  117. self._first_pos = 0
  118. self._size = 0
  119. def __len__(self) -> int:
  120. return self._size
  121. # Data above this size will be appended separately instead
  122. # of extending an existing bytearray
  123. _large_buf_threshold = 2048
  124. def append(self, data: Union[bytes, bytearray, memoryview]) -> None:
  125. """
  126. Append the given piece of data (should be a buffer-compatible object).
  127. """
  128. size = len(data)
  129. if size > self._large_buf_threshold:
  130. if not isinstance(data, memoryview):
  131. data = memoryview(data)
  132. self._buffers.append((True, data))
  133. elif size > 0:
  134. if self._buffers:
  135. is_memview, b = self._buffers[-1]
  136. new_buf = is_memview or len(b) >= self._large_buf_threshold
  137. else:
  138. new_buf = True
  139. if new_buf:
  140. self._buffers.append((False, bytearray(data)))
  141. else:
  142. b += data # type: ignore
  143. self._size += size
  144. def peek(self, size: int) -> memoryview:
  145. """
  146. Get a view over at most ``size`` bytes (possibly fewer) at the
  147. current buffer position.
  148. """
  149. assert size > 0
  150. try:
  151. is_memview, b = self._buffers[0]
  152. except IndexError:
  153. return memoryview(b"")
  154. pos = self._first_pos
  155. if is_memview:
  156. return typing.cast(memoryview, b[pos : pos + size])
  157. else:
  158. return memoryview(b)[pos : pos + size]
  159. def advance(self, size: int) -> None:
  160. """
  161. Advance the current buffer position by ``size`` bytes.
  162. """
  163. assert 0 < size <= self._size
  164. self._size -= size
  165. pos = self._first_pos
  166. buffers = self._buffers
  167. while buffers and size > 0:
  168. is_large, b = buffers[0]
  169. b_remain = len(b) - size - pos
  170. if b_remain <= 0:
  171. buffers.popleft()
  172. size -= len(b) - pos
  173. pos = 0
  174. elif is_large:
  175. pos += size
  176. size = 0
  177. else:
  178. # Amortized O(1) shrink for Python 2
  179. pos += size
  180. if len(b) <= 2 * pos:
  181. del typing.cast(bytearray, b)[:pos]
  182. pos = 0
  183. size = 0
  184. assert size == 0
  185. self._first_pos = pos
  186. class BaseIOStream(object):
  187. """A utility class to write to and read from a non-blocking file or socket.
  188. We support a non-blocking ``write()`` and a family of ``read_*()``
  189. methods. When the operation completes, the ``Awaitable`` will resolve
  190. with the data read (or ``None`` for ``write()``). All outstanding
  191. ``Awaitables`` will resolve with a `StreamClosedError` when the
  192. stream is closed; `.BaseIOStream.set_close_callback` can also be used
  193. to be notified of a closed stream.
  194. When a stream is closed due to an error, the IOStream's ``error``
  195. attribute contains the exception object.
  196. Subclasses must implement `fileno`, `close_fd`, `write_to_fd`,
  197. `read_from_fd`, and optionally `get_fd_error`.
  198. """
  199. def __init__(
  200. self,
  201. max_buffer_size: int = None,
  202. read_chunk_size: int = None,
  203. max_write_buffer_size: int = None,
  204. ) -> None:
  205. """`BaseIOStream` constructor.
  206. :arg max_buffer_size: Maximum amount of incoming data to buffer;
  207. defaults to 100MB.
  208. :arg read_chunk_size: Amount of data to read at one time from the
  209. underlying transport; defaults to 64KB.
  210. :arg max_write_buffer_size: Amount of outgoing data to buffer;
  211. defaults to unlimited.
  212. .. versionchanged:: 4.0
  213. Add the ``max_write_buffer_size`` parameter. Changed default
  214. ``read_chunk_size`` to 64KB.
  215. .. versionchanged:: 5.0
  216. The ``io_loop`` argument (deprecated since version 4.1) has been
  217. removed.
  218. """
  219. self.io_loop = ioloop.IOLoop.current()
  220. self.max_buffer_size = max_buffer_size or 104857600
  221. # A chunk size that is too close to max_buffer_size can cause
  222. # spurious failures.
  223. self.read_chunk_size = min(read_chunk_size or 65536, self.max_buffer_size // 2)
  224. self.max_write_buffer_size = max_write_buffer_size
  225. self.error = None # type: Optional[BaseException]
  226. self._read_buffer = bytearray()
  227. self._read_buffer_pos = 0
  228. self._read_buffer_size = 0
  229. self._user_read_buffer = False
  230. self._after_user_read_buffer = None # type: Optional[bytearray]
  231. self._write_buffer = _StreamBuffer()
  232. self._total_write_index = 0
  233. self._total_write_done_index = 0
  234. self._read_delimiter = None # type: Optional[bytes]
  235. self._read_regex = None # type: Optional[Pattern]
  236. self._read_max_bytes = None # type: Optional[int]
  237. self._read_bytes = None # type: Optional[int]
  238. self._read_partial = False
  239. self._read_until_close = False
  240. self._read_future = None # type: Optional[Future]
  241. self._write_futures = (
  242. collections.deque()
  243. ) # type: Deque[Tuple[int, Future[None]]]
  244. self._close_callback = None # type: Optional[Callable[[], None]]
  245. self._connect_future = None # type: Optional[Future[IOStream]]
  246. # _ssl_connect_future should be defined in SSLIOStream
  247. # but it's here so we can clean it up in _signal_closed
  248. # TODO: refactor that so subclasses can add additional futures
  249. # to be cancelled.
  250. self._ssl_connect_future = None # type: Optional[Future[SSLIOStream]]
  251. self._connecting = False
  252. self._state = None # type: Optional[int]
  253. self._closed = False
  254. def fileno(self) -> Union[int, ioloop._Selectable]:
  255. """Returns the file descriptor for this stream."""
  256. raise NotImplementedError()
  257. def close_fd(self) -> None:
  258. """Closes the file underlying this stream.
  259. ``close_fd`` is called by `BaseIOStream` and should not be called
  260. elsewhere; other users should call `close` instead.
  261. """
  262. raise NotImplementedError()
  263. def write_to_fd(self, data: memoryview) -> int:
  264. """Attempts to write ``data`` to the underlying file.
  265. Returns the number of bytes written.
  266. """
  267. raise NotImplementedError()
  268. def read_from_fd(self, buf: Union[bytearray, memoryview]) -> Optional[int]:
  269. """Attempts to read from the underlying file.
  270. Reads up to ``len(buf)`` bytes, storing them in the buffer.
  271. Returns the number of bytes read. Returns None if there was
  272. nothing to read (the socket returned `~errno.EWOULDBLOCK` or
  273. equivalent), and zero on EOF.
  274. .. versionchanged:: 5.0
  275. Interface redesigned to take a buffer and return a number
  276. of bytes instead of a freshly-allocated object.
  277. """
  278. raise NotImplementedError()
  279. def get_fd_error(self) -> Optional[Exception]:
  280. """Returns information about any error on the underlying file.
  281. This method is called after the `.IOLoop` has signaled an error on the
  282. file descriptor, and should return an Exception (such as `socket.error`
  283. with additional information, or None if no such information is
  284. available.
  285. """
  286. return None
  287. def read_until_regex(self, regex: bytes, max_bytes: int = None) -> Awaitable[bytes]:
  288. """Asynchronously read until we have matched the given regex.
  289. The result includes the data that matches the regex and anything
  290. that came before it.
  291. If ``max_bytes`` is not None, the connection will be closed
  292. if more than ``max_bytes`` bytes have been read and the regex is
  293. not satisfied.
  294. .. versionchanged:: 4.0
  295. Added the ``max_bytes`` argument. The ``callback`` argument is
  296. now optional and a `.Future` will be returned if it is omitted.
  297. .. versionchanged:: 6.0
  298. The ``callback`` argument was removed. Use the returned
  299. `.Future` instead.
  300. """
  301. future = self._start_read()
  302. self._read_regex = re.compile(regex)
  303. self._read_max_bytes = max_bytes
  304. try:
  305. self._try_inline_read()
  306. except UnsatisfiableReadError as e:
  307. # Handle this the same way as in _handle_events.
  308. gen_log.info("Unsatisfiable read, closing connection: %s" % e)
  309. self.close(exc_info=e)
  310. return future
  311. except:
  312. # Ensure that the future doesn't log an error because its
  313. # failure was never examined.
  314. future.add_done_callback(lambda f: f.exception())
  315. raise
  316. return future
  317. def read_until(self, delimiter: bytes, max_bytes: int = None) -> Awaitable[bytes]:
  318. """Asynchronously read until we have found the given delimiter.
  319. The result includes all the data read including the delimiter.
  320. If ``max_bytes`` is not None, the connection will be closed
  321. if more than ``max_bytes`` bytes have been read and the delimiter
  322. is not found.
  323. .. versionchanged:: 4.0
  324. Added the ``max_bytes`` argument. The ``callback`` argument is
  325. now optional and a `.Future` will be returned if it is omitted.
  326. .. versionchanged:: 6.0
  327. The ``callback`` argument was removed. Use the returned
  328. `.Future` instead.
  329. """
  330. future = self._start_read()
  331. self._read_delimiter = delimiter
  332. self._read_max_bytes = max_bytes
  333. try:
  334. self._try_inline_read()
  335. except UnsatisfiableReadError as e:
  336. # Handle this the same way as in _handle_events.
  337. gen_log.info("Unsatisfiable read, closing connection: %s" % e)
  338. self.close(exc_info=e)
  339. return future
  340. except:
  341. future.add_done_callback(lambda f: f.exception())
  342. raise
  343. return future
  344. def read_bytes(self, num_bytes: int, partial: bool = False) -> Awaitable[bytes]:
  345. """Asynchronously read a number of bytes.
  346. If ``partial`` is true, data is returned as soon as we have
  347. any bytes to return (but never more than ``num_bytes``)
  348. .. versionchanged:: 4.0
  349. Added the ``partial`` argument. The callback argument is now
  350. optional and a `.Future` will be returned if it is omitted.
  351. .. versionchanged:: 6.0
  352. The ``callback`` and ``streaming_callback`` arguments have
  353. been removed. Use the returned `.Future` (and
  354. ``partial=True`` for ``streaming_callback``) instead.
  355. """
  356. future = self._start_read()
  357. assert isinstance(num_bytes, numbers.Integral)
  358. self._read_bytes = num_bytes
  359. self._read_partial = partial
  360. try:
  361. self._try_inline_read()
  362. except:
  363. future.add_done_callback(lambda f: f.exception())
  364. raise
  365. return future
  366. def read_into(self, buf: bytearray, partial: bool = False) -> Awaitable[int]:
  367. """Asynchronously read a number of bytes.
  368. ``buf`` must be a writable buffer into which data will be read.
  369. If ``partial`` is true, the callback is run as soon as any bytes
  370. have been read. Otherwise, it is run when the ``buf`` has been
  371. entirely filled with read data.
  372. .. versionadded:: 5.0
  373. .. versionchanged:: 6.0
  374. The ``callback`` argument was removed. Use the returned
  375. `.Future` instead.
  376. """
  377. future = self._start_read()
  378. # First copy data already in read buffer
  379. available_bytes = self._read_buffer_size
  380. n = len(buf)
  381. if available_bytes >= n:
  382. end = self._read_buffer_pos + n
  383. buf[:] = memoryview(self._read_buffer)[self._read_buffer_pos : end]
  384. del self._read_buffer[:end]
  385. self._after_user_read_buffer = self._read_buffer
  386. elif available_bytes > 0:
  387. buf[:available_bytes] = memoryview(self._read_buffer)[
  388. self._read_buffer_pos :
  389. ]
  390. # Set up the supplied buffer as our temporary read buffer.
  391. # The original (if it had any data remaining) has been
  392. # saved for later.
  393. self._user_read_buffer = True
  394. self._read_buffer = buf
  395. self._read_buffer_pos = 0
  396. self._read_buffer_size = available_bytes
  397. self._read_bytes = n
  398. self._read_partial = partial
  399. try:
  400. self._try_inline_read()
  401. except:
  402. future.add_done_callback(lambda f: f.exception())
  403. raise
  404. return future
  405. def read_until_close(self) -> Awaitable[bytes]:
  406. """Asynchronously reads all data from the socket until it is closed.
  407. This will buffer all available data until ``max_buffer_size``
  408. is reached. If flow control or cancellation are desired, use a
  409. loop with `read_bytes(partial=True) <.read_bytes>` instead.
  410. .. versionchanged:: 4.0
  411. The callback argument is now optional and a `.Future` will
  412. be returned if it is omitted.
  413. .. versionchanged:: 6.0
  414. The ``callback`` and ``streaming_callback`` arguments have
  415. been removed. Use the returned `.Future` (and `read_bytes`
  416. with ``partial=True`` for ``streaming_callback``) instead.
  417. """
  418. future = self._start_read()
  419. if self.closed():
  420. self._finish_read(self._read_buffer_size, False)
  421. return future
  422. self._read_until_close = True
  423. try:
  424. self._try_inline_read()
  425. except:
  426. future.add_done_callback(lambda f: f.exception())
  427. raise
  428. return future
  429. def write(self, data: Union[bytes, memoryview]) -> "Future[None]":
  430. """Asynchronously write the given data to this stream.
  431. This method returns a `.Future` that resolves (with a result
  432. of ``None``) when the write has been completed.
  433. The ``data`` argument may be of type `bytes` or `memoryview`.
  434. .. versionchanged:: 4.0
  435. Now returns a `.Future` if no callback is given.
  436. .. versionchanged:: 4.5
  437. Added support for `memoryview` arguments.
  438. .. versionchanged:: 6.0
  439. The ``callback`` argument was removed. Use the returned
  440. `.Future` instead.
  441. """
  442. self._check_closed()
  443. if data:
  444. if (
  445. self.max_write_buffer_size is not None
  446. and len(self._write_buffer) + len(data) > self.max_write_buffer_size
  447. ):
  448. raise StreamBufferFullError("Reached maximum write buffer size")
  449. self._write_buffer.append(data)
  450. self._total_write_index += len(data)
  451. future = Future() # type: Future[None]
  452. future.add_done_callback(lambda f: f.exception())
  453. self._write_futures.append((self._total_write_index, future))
  454. if not self._connecting:
  455. self._handle_write()
  456. if self._write_buffer:
  457. self._add_io_state(self.io_loop.WRITE)
  458. self._maybe_add_error_listener()
  459. return future
  460. def set_close_callback(self, callback: Optional[Callable[[], None]]) -> None:
  461. """Call the given callback when the stream is closed.
  462. This mostly is not necessary for applications that use the
  463. `.Future` interface; all outstanding ``Futures`` will resolve
  464. with a `StreamClosedError` when the stream is closed. However,
  465. it is still useful as a way to signal that the stream has been
  466. closed while no other read or write is in progress.
  467. Unlike other callback-based interfaces, ``set_close_callback``
  468. was not removed in Tornado 6.0.
  469. """
  470. self._close_callback = callback
  471. self._maybe_add_error_listener()
  472. def close(
  473. self,
  474. exc_info: Union[
  475. None,
  476. bool,
  477. BaseException,
  478. Tuple[
  479. "Optional[Type[BaseException]]",
  480. Optional[BaseException],
  481. Optional[TracebackType],
  482. ],
  483. ] = False,
  484. ) -> None:
  485. """Close this stream.
  486. If ``exc_info`` is true, set the ``error`` attribute to the current
  487. exception from `sys.exc_info` (or if ``exc_info`` is a tuple,
  488. use that instead of `sys.exc_info`).
  489. """
  490. if not self.closed():
  491. if exc_info:
  492. if isinstance(exc_info, tuple):
  493. self.error = exc_info[1]
  494. elif isinstance(exc_info, BaseException):
  495. self.error = exc_info
  496. else:
  497. exc_info = sys.exc_info()
  498. if any(exc_info):
  499. self.error = exc_info[1]
  500. if self._read_until_close:
  501. self._read_until_close = False
  502. self._finish_read(self._read_buffer_size, False)
  503. elif self._read_future is not None:
  504. # resolve reads that are pending and ready to complete
  505. try:
  506. pos = self._find_read_pos()
  507. except UnsatisfiableReadError:
  508. pass
  509. else:
  510. if pos is not None:
  511. self._read_from_buffer(pos)
  512. if self._state is not None:
  513. self.io_loop.remove_handler(self.fileno())
  514. self._state = None
  515. self.close_fd()
  516. self._closed = True
  517. self._signal_closed()
  518. def _signal_closed(self) -> None:
  519. futures = [] # type: List[Future]
  520. if self._read_future is not None:
  521. futures.append(self._read_future)
  522. self._read_future = None
  523. futures += [future for _, future in self._write_futures]
  524. self._write_futures.clear()
  525. if self._connect_future is not None:
  526. futures.append(self._connect_future)
  527. self._connect_future = None
  528. for future in futures:
  529. if not future.done():
  530. future.set_exception(StreamClosedError(real_error=self.error))
  531. # Reference the exception to silence warnings. Annoyingly,
  532. # this raises if the future was cancelled, but just
  533. # returns any other error.
  534. try:
  535. future.exception()
  536. except asyncio.CancelledError:
  537. pass
  538. if self._ssl_connect_future is not None:
  539. # _ssl_connect_future expects to see the real exception (typically
  540. # an ssl.SSLError), not just StreamClosedError.
  541. if not self._ssl_connect_future.done():
  542. if self.error is not None:
  543. self._ssl_connect_future.set_exception(self.error)
  544. else:
  545. self._ssl_connect_future.set_exception(StreamClosedError())
  546. self._ssl_connect_future.exception()
  547. self._ssl_connect_future = None
  548. if self._close_callback is not None:
  549. cb = self._close_callback
  550. self._close_callback = None
  551. self.io_loop.add_callback(cb)
  552. # Clear the buffers so they can be cleared immediately even
  553. # if the IOStream object is kept alive by a reference cycle.
  554. # TODO: Clear the read buffer too; it currently breaks some tests.
  555. self._write_buffer = None # type: ignore
  556. def reading(self) -> bool:
  557. """Returns ``True`` if we are currently reading from the stream."""
  558. return self._read_future is not None
  559. def writing(self) -> bool:
  560. """Returns ``True`` if we are currently writing to the stream."""
  561. return bool(self._write_buffer)
  562. def closed(self) -> bool:
  563. """Returns ``True`` if the stream has been closed."""
  564. return self._closed
  565. def set_nodelay(self, value: bool) -> None:
  566. """Sets the no-delay flag for this stream.
  567. By default, data written to TCP streams may be held for a time
  568. to make the most efficient use of bandwidth (according to
  569. Nagle's algorithm). The no-delay flag requests that data be
  570. written as soon as possible, even if doing so would consume
  571. additional bandwidth.
  572. This flag is currently defined only for TCP-based ``IOStreams``.
  573. .. versionadded:: 3.1
  574. """
  575. pass
  576. def _handle_connect(self) -> None:
  577. raise NotImplementedError()
  578. def _handle_events(self, fd: Union[int, ioloop._Selectable], events: int) -> None:
  579. if self.closed():
  580. gen_log.warning("Got events for closed stream %s", fd)
  581. return
  582. try:
  583. if self._connecting:
  584. # Most IOLoops will report a write failed connect
  585. # with the WRITE event, but SelectIOLoop reports a
  586. # READ as well so we must check for connecting before
  587. # either.
  588. self._handle_connect()
  589. if self.closed():
  590. return
  591. if events & self.io_loop.READ:
  592. self._handle_read()
  593. if self.closed():
  594. return
  595. if events & self.io_loop.WRITE:
  596. self._handle_write()
  597. if self.closed():
  598. return
  599. if events & self.io_loop.ERROR:
  600. self.error = self.get_fd_error()
  601. # We may have queued up a user callback in _handle_read or
  602. # _handle_write, so don't close the IOStream until those
  603. # callbacks have had a chance to run.
  604. self.io_loop.add_callback(self.close)
  605. return
  606. state = self.io_loop.ERROR
  607. if self.reading():
  608. state |= self.io_loop.READ
  609. if self.writing():
  610. state |= self.io_loop.WRITE
  611. if state == self.io_loop.ERROR and self._read_buffer_size == 0:
  612. # If the connection is idle, listen for reads too so
  613. # we can tell if the connection is closed. If there is
  614. # data in the read buffer we won't run the close callback
  615. # yet anyway, so we don't need to listen in this case.
  616. state |= self.io_loop.READ
  617. if state != self._state:
  618. assert (
  619. self._state is not None
  620. ), "shouldn't happen: _handle_events without self._state"
  621. self._state = state
  622. self.io_loop.update_handler(self.fileno(), self._state)
  623. except UnsatisfiableReadError as e:
  624. gen_log.info("Unsatisfiable read, closing connection: %s" % e)
  625. self.close(exc_info=e)
  626. except Exception as e:
  627. gen_log.error("Uncaught exception, closing connection.", exc_info=True)
  628. self.close(exc_info=e)
  629. raise
  630. def _read_to_buffer_loop(self) -> Optional[int]:
  631. # This method is called from _handle_read and _try_inline_read.
  632. if self._read_bytes is not None:
  633. target_bytes = self._read_bytes # type: Optional[int]
  634. elif self._read_max_bytes is not None:
  635. target_bytes = self._read_max_bytes
  636. elif self.reading():
  637. # For read_until without max_bytes, or
  638. # read_until_close, read as much as we can before
  639. # scanning for the delimiter.
  640. target_bytes = None
  641. else:
  642. target_bytes = 0
  643. next_find_pos = 0
  644. while not self.closed():
  645. # Read from the socket until we get EWOULDBLOCK or equivalent.
  646. # SSL sockets do some internal buffering, and if the data is
  647. # sitting in the SSL object's buffer select() and friends
  648. # can't see it; the only way to find out if it's there is to
  649. # try to read it.
  650. if self._read_to_buffer() == 0:
  651. break
  652. # If we've read all the bytes we can use, break out of
  653. # this loop.
  654. # If we've reached target_bytes, we know we're done.
  655. if target_bytes is not None and self._read_buffer_size >= target_bytes:
  656. break
  657. # Otherwise, we need to call the more expensive find_read_pos.
  658. # It's inefficient to do this on every read, so instead
  659. # do it on the first read and whenever the read buffer
  660. # size has doubled.
  661. if self._read_buffer_size >= next_find_pos:
  662. pos = self._find_read_pos()
  663. if pos is not None:
  664. return pos
  665. next_find_pos = self._read_buffer_size * 2
  666. return self._find_read_pos()
  667. def _handle_read(self) -> None:
  668. try:
  669. pos = self._read_to_buffer_loop()
  670. except UnsatisfiableReadError:
  671. raise
  672. except asyncio.CancelledError:
  673. raise
  674. except Exception as e:
  675. gen_log.warning("error on read: %s" % e)
  676. self.close(exc_info=e)
  677. return
  678. if pos is not None:
  679. self._read_from_buffer(pos)
  680. def _start_read(self) -> Future:
  681. if self._read_future is not None:
  682. # It is an error to start a read while a prior read is unresolved.
  683. # However, if the prior read is unresolved because the stream was
  684. # closed without satisfying it, it's better to raise
  685. # StreamClosedError instead of AssertionError. In particular, this
  686. # situation occurs in harmless situations in http1connection.py and
  687. # an AssertionError would be logged noisily.
  688. #
  689. # On the other hand, it is legal to start a new read while the
  690. # stream is closed, in case the read can be satisfied from the
  691. # read buffer. So we only want to check the closed status of the
  692. # stream if we need to decide what kind of error to raise for
  693. # "already reading".
  694. #
  695. # These conditions have proven difficult to test; we have no
  696. # unittests that reliably verify this behavior so be careful
  697. # when making changes here. See #2651 and #2719.
  698. self._check_closed()
  699. assert self._read_future is None, "Already reading"
  700. self._read_future = Future()
  701. return self._read_future
  702. def _finish_read(self, size: int, streaming: bool) -> None:
  703. if self._user_read_buffer:
  704. self._read_buffer = self._after_user_read_buffer or bytearray()
  705. self._after_user_read_buffer = None
  706. self._read_buffer_pos = 0
  707. self._read_buffer_size = len(self._read_buffer)
  708. self._user_read_buffer = False
  709. result = size # type: Union[int, bytes]
  710. else:
  711. result = self._consume(size)
  712. if self._read_future is not None:
  713. future = self._read_future
  714. self._read_future = None
  715. future_set_result_unless_cancelled(future, result)
  716. self._maybe_add_error_listener()
  717. def _try_inline_read(self) -> None:
  718. """Attempt to complete the current read operation from buffered data.
  719. If the read can be completed without blocking, schedules the
  720. read callback on the next IOLoop iteration; otherwise starts
  721. listening for reads on the socket.
  722. """
  723. # See if we've already got the data from a previous read
  724. pos = self._find_read_pos()
  725. if pos is not None:
  726. self._read_from_buffer(pos)
  727. return
  728. self._check_closed()
  729. pos = self._read_to_buffer_loop()
  730. if pos is not None:
  731. self._read_from_buffer(pos)
  732. return
  733. # We couldn't satisfy the read inline, so make sure we're
  734. # listening for new data unless the stream is closed.
  735. if not self.closed():
  736. self._add_io_state(ioloop.IOLoop.READ)
  737. def _read_to_buffer(self) -> Optional[int]:
  738. """Reads from the socket and appends the result to the read buffer.
  739. Returns the number of bytes read. Returns 0 if there is nothing
  740. to read (i.e. the read returns EWOULDBLOCK or equivalent). On
  741. error closes the socket and raises an exception.
  742. """
  743. try:
  744. while True:
  745. try:
  746. if self._user_read_buffer:
  747. buf = memoryview(self._read_buffer)[
  748. self._read_buffer_size :
  749. ] # type: Union[memoryview, bytearray]
  750. else:
  751. buf = bytearray(self.read_chunk_size)
  752. bytes_read = self.read_from_fd(buf)
  753. except (socket.error, IOError, OSError) as e:
  754. if errno_from_exception(e) == errno.EINTR:
  755. continue
  756. # ssl.SSLError is a subclass of socket.error
  757. if self._is_connreset(e):
  758. # Treat ECONNRESET as a connection close rather than
  759. # an error to minimize log spam (the exception will
  760. # be available on self.error for apps that care).
  761. self.close(exc_info=e)
  762. return None
  763. self.close(exc_info=e)
  764. raise
  765. break
  766. if bytes_read is None:
  767. return 0
  768. elif bytes_read == 0:
  769. self.close()
  770. return 0
  771. if not self._user_read_buffer:
  772. self._read_buffer += memoryview(buf)[:bytes_read]
  773. self._read_buffer_size += bytes_read
  774. finally:
  775. # Break the reference to buf so we don't waste a chunk's worth of
  776. # memory in case an exception hangs on to our stack frame.
  777. del buf
  778. if self._read_buffer_size > self.max_buffer_size:
  779. gen_log.error("Reached maximum read buffer size")
  780. self.close()
  781. raise StreamBufferFullError("Reached maximum read buffer size")
  782. return bytes_read
  783. def _read_from_buffer(self, pos: int) -> None:
  784. """Attempts to complete the currently-pending read from the buffer.
  785. The argument is either a position in the read buffer or None,
  786. as returned by _find_read_pos.
  787. """
  788. self._read_bytes = self._read_delimiter = self._read_regex = None
  789. self._read_partial = False
  790. self._finish_read(pos, False)
  791. def _find_read_pos(self) -> Optional[int]:
  792. """Attempts to find a position in the read buffer that satisfies
  793. the currently-pending read.
  794. Returns a position in the buffer if the current read can be satisfied,
  795. or None if it cannot.
  796. """
  797. if self._read_bytes is not None and (
  798. self._read_buffer_size >= self._read_bytes
  799. or (self._read_partial and self._read_buffer_size > 0)
  800. ):
  801. num_bytes = min(self._read_bytes, self._read_buffer_size)
  802. return num_bytes
  803. elif self._read_delimiter is not None:
  804. # Multi-byte delimiters (e.g. '\r\n') may straddle two
  805. # chunks in the read buffer, so we can't easily find them
  806. # without collapsing the buffer. However, since protocols
  807. # using delimited reads (as opposed to reads of a known
  808. # length) tend to be "line" oriented, the delimiter is likely
  809. # to be in the first few chunks. Merge the buffer gradually
  810. # since large merges are relatively expensive and get undone in
  811. # _consume().
  812. if self._read_buffer:
  813. loc = self._read_buffer.find(
  814. self._read_delimiter, self._read_buffer_pos
  815. )
  816. if loc != -1:
  817. loc -= self._read_buffer_pos
  818. delimiter_len = len(self._read_delimiter)
  819. self._check_max_bytes(self._read_delimiter, loc + delimiter_len)
  820. return loc + delimiter_len
  821. self._check_max_bytes(self._read_delimiter, self._read_buffer_size)
  822. elif self._read_regex is not None:
  823. if self._read_buffer:
  824. m = self._read_regex.search(self._read_buffer, self._read_buffer_pos)
  825. if m is not None:
  826. loc = m.end() - self._read_buffer_pos
  827. self._check_max_bytes(self._read_regex, loc)
  828. return loc
  829. self._check_max_bytes(self._read_regex, self._read_buffer_size)
  830. return None
  831. def _check_max_bytes(self, delimiter: Union[bytes, Pattern], size: int) -> None:
  832. if self._read_max_bytes is not None and size > self._read_max_bytes:
  833. raise UnsatisfiableReadError(
  834. "delimiter %r not found within %d bytes"
  835. % (delimiter, self._read_max_bytes)
  836. )
  837. def _handle_write(self) -> None:
  838. while True:
  839. size = len(self._write_buffer)
  840. if not size:
  841. break
  842. assert size > 0
  843. try:
  844. if _WINDOWS:
  845. # On windows, socket.send blows up if given a
  846. # write buffer that's too large, instead of just
  847. # returning the number of bytes it was able to
  848. # process. Therefore we must not call socket.send
  849. # with more than 128KB at a time.
  850. size = 128 * 1024
  851. num_bytes = self.write_to_fd(self._write_buffer.peek(size))
  852. if num_bytes == 0:
  853. break
  854. self._write_buffer.advance(num_bytes)
  855. self._total_write_done_index += num_bytes
  856. except (socket.error, IOError, OSError) as e:
  857. if e.args[0] in _ERRNO_WOULDBLOCK:
  858. break
  859. else:
  860. if not self._is_connreset(e):
  861. # Broken pipe errors are usually caused by connection
  862. # reset, and its better to not log EPIPE errors to
  863. # minimize log spam
  864. gen_log.warning("Write error on %s: %s", self.fileno(), e)
  865. self.close(exc_info=e)
  866. return
  867. while self._write_futures:
  868. index, future = self._write_futures[0]
  869. if index > self._total_write_done_index:
  870. break
  871. self._write_futures.popleft()
  872. future_set_result_unless_cancelled(future, None)
  873. def _consume(self, loc: int) -> bytes:
  874. # Consume loc bytes from the read buffer and return them
  875. if loc == 0:
  876. return b""
  877. assert loc <= self._read_buffer_size
  878. # Slice the bytearray buffer into bytes, without intermediate copying
  879. b = (
  880. memoryview(self._read_buffer)[
  881. self._read_buffer_pos : self._read_buffer_pos + loc
  882. ]
  883. ).tobytes()
  884. self._read_buffer_pos += loc
  885. self._read_buffer_size -= loc
  886. # Amortized O(1) shrink
  887. # (this heuristic is implemented natively in Python 3.4+
  888. # but is replicated here for Python 2)
  889. if self._read_buffer_pos > self._read_buffer_size:
  890. del self._read_buffer[: self._read_buffer_pos]
  891. self._read_buffer_pos = 0
  892. return b
  893. def _check_closed(self) -> None:
  894. if self.closed():
  895. raise StreamClosedError(real_error=self.error)
  896. def _maybe_add_error_listener(self) -> None:
  897. # This method is part of an optimization: to detect a connection that
  898. # is closed when we're not actively reading or writing, we must listen
  899. # for read events. However, it is inefficient to do this when the
  900. # connection is first established because we are going to read or write
  901. # immediately anyway. Instead, we insert checks at various times to
  902. # see if the connection is idle and add the read listener then.
  903. if self._state is None or self._state == ioloop.IOLoop.ERROR:
  904. if (
  905. not self.closed()
  906. and self._read_buffer_size == 0
  907. and self._close_callback is not None
  908. ):
  909. self._add_io_state(ioloop.IOLoop.READ)
  910. def _add_io_state(self, state: int) -> None:
  911. """Adds `state` (IOLoop.{READ,WRITE} flags) to our event handler.
  912. Implementation notes: Reads and writes have a fast path and a
  913. slow path. The fast path reads synchronously from socket
  914. buffers, while the slow path uses `_add_io_state` to schedule
  915. an IOLoop callback.
  916. To detect closed connections, we must have called
  917. `_add_io_state` at some point, but we want to delay this as
  918. much as possible so we don't have to set an `IOLoop.ERROR`
  919. listener that will be overwritten by the next slow-path
  920. operation. If a sequence of fast-path ops do not end in a
  921. slow-path op, (e.g. for an @asynchronous long-poll request),
  922. we must add the error handler.
  923. TODO: reevaluate this now that callbacks are gone.
  924. """
  925. if self.closed():
  926. # connection has been closed, so there can be no future events
  927. return
  928. if self._state is None:
  929. self._state = ioloop.IOLoop.ERROR | state
  930. self.io_loop.add_handler(self.fileno(), self._handle_events, self._state)
  931. elif not self._state & state:
  932. self._state = self._state | state
  933. self.io_loop.update_handler(self.fileno(), self._state)
  934. def _is_connreset(self, exc: BaseException) -> bool:
  935. """Return ``True`` if exc is ECONNRESET or equivalent.
  936. May be overridden in subclasses.
  937. """
  938. return (
  939. isinstance(exc, (socket.error, IOError))
  940. and errno_from_exception(exc) in _ERRNO_CONNRESET
  941. )
  942. class IOStream(BaseIOStream):
  943. r"""Socket-based `IOStream` implementation.
  944. This class supports the read and write methods from `BaseIOStream`
  945. plus a `connect` method.
  946. The ``socket`` parameter may either be connected or unconnected.
  947. For server operations the socket is the result of calling
  948. `socket.accept <socket.socket.accept>`. For client operations the
  949. socket is created with `socket.socket`, and may either be
  950. connected before passing it to the `IOStream` or connected with
  951. `IOStream.connect`.
  952. A very simple (and broken) HTTP client using this class:
  953. .. testcode::
  954. import tornado.ioloop
  955. import tornado.iostream
  956. import socket
  957. async def main():
  958. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
  959. stream = tornado.iostream.IOStream(s)
  960. await stream.connect(("friendfeed.com", 80))
  961. await stream.write(b"GET / HTTP/1.0\r\nHost: friendfeed.com\r\n\r\n")
  962. header_data = await stream.read_until(b"\r\n\r\n")
  963. headers = {}
  964. for line in header_data.split(b"\r\n"):
  965. parts = line.split(b":")
  966. if len(parts) == 2:
  967. headers[parts[0].strip()] = parts[1].strip()
  968. body_data = await stream.read_bytes(int(headers[b"Content-Length"]))
  969. print(body_data)
  970. stream.close()
  971. if __name__ == '__main__':
  972. tornado.ioloop.IOLoop.current().run_sync(main)
  973. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
  974. stream = tornado.iostream.IOStream(s)
  975. stream.connect(("friendfeed.com", 80), send_request)
  976. tornado.ioloop.IOLoop.current().start()
  977. .. testoutput::
  978. :hide:
  979. """
  980. def __init__(self, socket: socket.socket, *args: Any, **kwargs: Any) -> None:
  981. self.socket = socket
  982. self.socket.setblocking(False)
  983. super(IOStream, self).__init__(*args, **kwargs)
  984. def fileno(self) -> Union[int, ioloop._Selectable]:
  985. return self.socket
  986. def close_fd(self) -> None:
  987. self.socket.close()
  988. self.socket = None # type: ignore
  989. def get_fd_error(self) -> Optional[Exception]:
  990. errno = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
  991. return socket.error(errno, os.strerror(errno))
  992. def read_from_fd(self, buf: Union[bytearray, memoryview]) -> Optional[int]:
  993. try:
  994. return self.socket.recv_into(buf, len(buf))
  995. except socket.error as e:
  996. if e.args[0] in _ERRNO_WOULDBLOCK:
  997. return None
  998. else:
  999. raise
  1000. finally:
  1001. del buf
  1002. def write_to_fd(self, data: memoryview) -> int:
  1003. try:
  1004. return self.socket.send(data) # type: ignore
  1005. finally:
  1006. # Avoid keeping to data, which can be a memoryview.
  1007. # See https://github.com/tornadoweb/tornado/pull/2008
  1008. del data
  1009. def connect(
  1010. self: _IOStreamType, address: tuple, server_hostname: str = None
  1011. ) -> "Future[_IOStreamType]":
  1012. """Connects the socket to a remote address without blocking.
  1013. May only be called if the socket passed to the constructor was
  1014. not previously connected. The address parameter is in the
  1015. same format as for `socket.connect <socket.socket.connect>` for
  1016. the type of socket passed to the IOStream constructor,
  1017. e.g. an ``(ip, port)`` tuple. Hostnames are accepted here,
  1018. but will be resolved synchronously and block the IOLoop.
  1019. If you have a hostname instead of an IP address, the `.TCPClient`
  1020. class is recommended instead of calling this method directly.
  1021. `.TCPClient` will do asynchronous DNS resolution and handle
  1022. both IPv4 and IPv6.
  1023. If ``callback`` is specified, it will be called with no
  1024. arguments when the connection is completed; if not this method
  1025. returns a `.Future` (whose result after a successful
  1026. connection will be the stream itself).
  1027. In SSL mode, the ``server_hostname`` parameter will be used
  1028. for certificate validation (unless disabled in the
  1029. ``ssl_options``) and SNI (if supported; requires Python
  1030. 2.7.9+).
  1031. Note that it is safe to call `IOStream.write
  1032. <BaseIOStream.write>` while the connection is pending, in
  1033. which case the data will be written as soon as the connection
  1034. is ready. Calling `IOStream` read methods before the socket is
  1035. connected works on some platforms but is non-portable.
  1036. .. versionchanged:: 4.0
  1037. If no callback is given, returns a `.Future`.
  1038. .. versionchanged:: 4.2
  1039. SSL certificates are validated by default; pass
  1040. ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
  1041. suitably-configured `ssl.SSLContext` to the
  1042. `SSLIOStream` constructor to disable.
  1043. .. versionchanged:: 6.0
  1044. The ``callback`` argument was removed. Use the returned
  1045. `.Future` instead.
  1046. """
  1047. self._connecting = True
  1048. future = Future() # type: Future[_IOStreamType]
  1049. self._connect_future = typing.cast("Future[IOStream]", future)
  1050. try:
  1051. self.socket.connect(address)
  1052. except socket.error as e:
  1053. # In non-blocking mode we expect connect() to raise an
  1054. # exception with EINPROGRESS or EWOULDBLOCK.
  1055. #
  1056. # On freebsd, other errors such as ECONNREFUSED may be
  1057. # returned immediately when attempting to connect to
  1058. # localhost, so handle them the same way as an error
  1059. # reported later in _handle_connect.
  1060. if (
  1061. errno_from_exception(e) not in _ERRNO_INPROGRESS
  1062. and errno_from_exception(e) not in _ERRNO_WOULDBLOCK
  1063. ):
  1064. if future is None:
  1065. gen_log.warning(
  1066. "Connect error on fd %s: %s", self.socket.fileno(), e
  1067. )
  1068. self.close(exc_info=e)
  1069. return future
  1070. self._add_io_state(self.io_loop.WRITE)
  1071. return future
  1072. def start_tls(
  1073. self,
  1074. server_side: bool,
  1075. ssl_options: Union[Dict[str, Any], ssl.SSLContext] = None,
  1076. server_hostname: str = None,
  1077. ) -> Awaitable["SSLIOStream"]:
  1078. """Convert this `IOStream` to an `SSLIOStream`.
  1079. This enables protocols that begin in clear-text mode and
  1080. switch to SSL after some initial negotiation (such as the
  1081. ``STARTTLS`` extension to SMTP and IMAP).
  1082. This method cannot be used if there are outstanding reads
  1083. or writes on the stream, or if there is any data in the
  1084. IOStream's buffer (data in the operating system's socket
  1085. buffer is allowed). This means it must generally be used
  1086. immediately after reading or writing the last clear-text
  1087. data. It can also be used immediately after connecting,
  1088. before any reads or writes.
  1089. The ``ssl_options`` argument may be either an `ssl.SSLContext`
  1090. object or a dictionary of keyword arguments for the
  1091. `ssl.wrap_socket` function. The ``server_hostname`` argument
  1092. will be used for certificate validation unless disabled
  1093. in the ``ssl_options``.
  1094. This method returns a `.Future` whose result is the new
  1095. `SSLIOStream`. After this method has been called,
  1096. any other operation on the original stream is undefined.
  1097. If a close callback is defined on this stream, it will be
  1098. transferred to the new stream.
  1099. .. versionadded:: 4.0
  1100. .. versionchanged:: 4.2
  1101. SSL certificates are validated by default; pass
  1102. ``ssl_options=dict(cert_reqs=ssl.CERT_NONE)`` or a
  1103. suitably-configured `ssl.SSLContext` to disable.
  1104. """
  1105. if (
  1106. self._read_future
  1107. or self._write_futures
  1108. or self._connect_future
  1109. or self._closed
  1110. or self._read_buffer
  1111. or self._write_buffer
  1112. ):
  1113. raise ValueError("IOStream is not idle; cannot convert to SSL")
  1114. if ssl_options is None:
  1115. if server_side:
  1116. ssl_options = _server_ssl_defaults
  1117. else:
  1118. ssl_options = _client_ssl_defaults
  1119. socket = self.socket
  1120. self.io_loop.remove_handler(socket)
  1121. self.socket = None # type: ignore
  1122. socket = ssl_wrap_socket(
  1123. socket,
  1124. ssl_options,
  1125. server_hostname=server_hostname,
  1126. server_side=server_side,
  1127. do_handshake_on_connect=False,
  1128. )
  1129. orig_close_callback = self._close_callback
  1130. self._close_callback = None
  1131. future = Future() # type: Future[SSLIOStream]
  1132. ssl_stream = SSLIOStream(socket, ssl_options=ssl_options)
  1133. ssl_stream.set_close_callback(orig_close_callback)
  1134. ssl_stream._ssl_connect_future = future
  1135. ssl_stream.max_buffer_size = self.max_buffer_size
  1136. ssl_stream.read_chunk_size = self.read_chunk_size
  1137. return future
  1138. def _handle_connect(self) -> None:
  1139. try:
  1140. err = self.socket.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
  1141. except socket.error as e:
  1142. # Hurd doesn't allow SO_ERROR for loopback sockets because all
  1143. # errors for such sockets are reported synchronously.
  1144. if errno_from_exception(e) == errno.ENOPROTOOPT:
  1145. err = 0
  1146. if err != 0:
  1147. self.error = socket.error(err, os.strerror(err))
  1148. # IOLoop implementations may vary: some of them return
  1149. # an error state before the socket becomes writable, so
  1150. # in that case a connection failure would be handled by the
  1151. # error path in _handle_events instead of here.
  1152. if self._connect_future is None:
  1153. gen_log.warning(
  1154. "Connect error on fd %s: %s",
  1155. self.socket.fileno(),
  1156. errno.errorcode[err],
  1157. )
  1158. self.close()
  1159. return
  1160. if self._connect_future is not None:
  1161. future = self._connect_future
  1162. self._connect_future = None
  1163. future_set_result_unless_cancelled(future, self)
  1164. self._connecting = False
  1165. def set_nodelay(self, value: bool) -> None:
  1166. if self.socket is not None and self.socket.family in (
  1167. socket.AF_INET,
  1168. socket.AF_INET6,
  1169. ):
  1170. try:
  1171. self.socket.setsockopt(
  1172. socket.IPPROTO_TCP, socket.TCP_NODELAY, 1 if value else 0
  1173. )
  1174. except socket.error as e:
  1175. # Sometimes setsockopt will fail if the socket is closed
  1176. # at the wrong time. This can happen with HTTPServer
  1177. # resetting the value to ``False`` between requests.
  1178. if e.errno != errno.EINVAL and not self._is_connreset(e):
  1179. raise
  1180. class SSLIOStream(IOStream):
  1181. """A utility class to write to and read from a non-blocking SSL socket.
  1182. If the socket passed to the constructor is already connected,
  1183. it should be wrapped with::
  1184. ssl.wrap_socket(sock, do_handshake_on_connect=False, **kwargs)
  1185. before constructing the `SSLIOStream`. Unconnected sockets will be
  1186. wrapped when `IOStream.connect` is finished.
  1187. """
  1188. socket = None # type: ssl.SSLSocket
  1189. def __init__(self, *args: Any, **kwargs: Any) -> None:
  1190. """The ``ssl_options`` keyword argument may either be an
  1191. `ssl.SSLContext` object or a dictionary of keywords arguments
  1192. for `ssl.wrap_socket`
  1193. """
  1194. self._ssl_options = kwargs.pop("ssl_options", _client_ssl_defaults)
  1195. super(SSLIOStream, self).__init__(*args, **kwargs)
  1196. self._ssl_accepting = True
  1197. self._handshake_reading = False
  1198. self._handshake_writing = False
  1199. self._server_hostname = None # type: Optional[str]
  1200. # If the socket is already connected, attempt to start the handshake.
  1201. try:
  1202. self.socket.getpeername()
  1203. except socket.error:
  1204. pass
  1205. else:
  1206. # Indirectly start the handshake, which will run on the next
  1207. # IOLoop iteration and then the real IO state will be set in
  1208. # _handle_events.
  1209. self._add_io_state(self.io_loop.WRITE)
  1210. def reading(self) -> bool:
  1211. return self._handshake_reading or super(SSLIOStream, self).reading()
  1212. def writing(self) -> bool:
  1213. return self._handshake_writing or super(SSLIOStream, self).writing()
  1214. def _do_ssl_handshake(self) -> None:
  1215. # Based on code from test_ssl.py in the python stdlib
  1216. try:
  1217. self._handshake_reading = False
  1218. self._handshake_writing = False
  1219. self.socket.do_handshake()
  1220. except ssl.SSLError as err:
  1221. if err.args[0] == ssl.SSL_ERROR_WANT_READ:
  1222. self._handshake_reading = True
  1223. return
  1224. elif err.args[0] == ssl.SSL_ERROR_WANT_WRITE:
  1225. self._handshake_writing = True
  1226. return
  1227. elif err.args[0] in (ssl.SSL_ERROR_EOF, ssl.SSL_ERROR_ZERO_RETURN):
  1228. return self.close(exc_info=err)
  1229. elif err.args[0] == ssl.SSL_ERROR_SSL:
  1230. try:
  1231. peer = self.socket.getpeername()
  1232. except Exception:
  1233. peer = "(not connected)"
  1234. gen_log.warning(
  1235. "SSL Error on %s %s: %s", self.socket.fileno(), peer, err
  1236. )
  1237. return self.close(exc_info=err)
  1238. raise
  1239. except socket.error as err:
  1240. # Some port scans (e.g. nmap in -sT mode) have been known
  1241. # to cause do_handshake to raise EBADF and ENOTCONN, so make
  1242. # those errors quiet as well.
  1243. # https://groups.google.com/forum/?fromgroups#!topic/python-tornado/ApucKJat1_0
  1244. # Errno 0 is also possible in some cases (nc -z).
  1245. # https://github.com/tornadoweb/tornado/issues/2504
  1246. if self._is_connreset(err) or err.args[0] in (
  1247. 0,
  1248. errno.EBADF,
  1249. errno.ENOTCONN,
  1250. ):
  1251. return self.close(exc_info=err)
  1252. raise
  1253. except AttributeError as err:
  1254. # On Linux, if the connection was reset before the call to
  1255. # wrap_socket, do_handshake will fail with an
  1256. # AttributeError.
  1257. return self.close(exc_info=err)
  1258. else:
  1259. self._ssl_accepting = False
  1260. if not self._verify_cert(self.socket.getpeercert()):
  1261. self.close()
  1262. return
  1263. self._finish_ssl_connect()
  1264. def _finish_ssl_connect(self) -> None:
  1265. if self._ssl_connect_future is not None:
  1266. future = self._ssl_connect_future
  1267. self._ssl_connect_future = None
  1268. future_set_result_unless_cancelled(future, self)
  1269. def _verify_cert(self, peercert: Any) -> bool:
  1270. """Returns ``True`` if peercert is valid according to the configured
  1271. validation mode and hostname.
  1272. The ssl handshake already tested the certificate for a valid
  1273. CA signature; the only thing that remains is to check
  1274. the hostname.
  1275. """
  1276. if isinstance(self._ssl_options, dict):
  1277. verify_mode = self._ssl_options.get("cert_reqs", ssl.CERT_NONE)
  1278. elif isinstance(self._ssl_options, ssl.SSLContext):
  1279. verify_mode = self._ssl_options.verify_mode
  1280. assert verify_mode in (ssl.CERT_NONE, ssl.CERT_REQUIRED, ssl.CERT_OPTIONAL)
  1281. if verify_mode == ssl.CERT_NONE or self._server_hostname is None:
  1282. return True
  1283. cert = self.socket.getpeercert()
  1284. if cert is None and verify_mode == ssl.CERT_REQUIRED:
  1285. gen_log.warning("No SSL certificate given")
  1286. return False
  1287. try:
  1288. ssl.match_hostname(peercert, self._server_hostname)
  1289. except ssl.CertificateError as e:
  1290. gen_log.warning("Invalid SSL certificate: %s" % e)
  1291. return False
  1292. else:
  1293. return True
  1294. def _handle_read(self) -> None:
  1295. if self._ssl_accepting:
  1296. self._do_ssl_handshake()
  1297. return
  1298. super(SSLIOStream, self)._handle_read()
  1299. def _handle_write(self) -> None:
  1300. if self._ssl_accepting:
  1301. self._do_ssl_handshake()
  1302. return
  1303. super(SSLIOStream, self)._handle_write()
  1304. def connect(
  1305. self, address: Tuple, server_hostname: str = None
  1306. ) -> "Future[SSLIOStream]":
  1307. self._server_hostname = server_hostname
  1308. # Ignore the result of connect(). If it fails,
  1309. # wait_for_handshake will raise an error too. This is
  1310. # necessary for the old semantics of the connect callback
  1311. # (which takes no arguments). In 6.0 this can be refactored to
  1312. # be a regular coroutine.
  1313. # TODO: This is trickier than it looks, since if write()
  1314. # is called with a connect() pending, we want the connect
  1315. # to resolve before the write. Or do we care about this?
  1316. # (There's a test for it, but I think in practice users
  1317. # either wait for the connect before performing a write or
  1318. # they don't care about the connect Future at all)
  1319. fut = super(SSLIOStream, self).connect(address)
  1320. fut.add_done_callback(lambda f: f.exception())
  1321. return self.wait_for_handshake()
  1322. def _handle_connect(self) -> None:
  1323. # Call the superclass method to check for errors.
  1324. super(SSLIOStream, self)._handle_connect()
  1325. if self.closed():
  1326. return
  1327. # When the connection is complete, wrap the socket for SSL
  1328. # traffic. Note that we do this by overriding _handle_connect
  1329. # instead of by passing a callback to super().connect because
  1330. # user callbacks are enqueued asynchronously on the IOLoop,
  1331. # but since _handle_events calls _handle_connect immediately
  1332. # followed by _handle_write we need this to be synchronous.
  1333. #
  1334. # The IOLoop will get confused if we swap out self.socket while the
  1335. # fd is registered, so remove it now and re-register after
  1336. # wrap_socket().
  1337. self.io_loop.remove_handler(self.socket)
  1338. old_state = self._state
  1339. assert old_state is not None
  1340. self._state = None
  1341. self.socket = ssl_wrap_socket(
  1342. self.socket,
  1343. self._ssl_options,
  1344. server_hostname=self._server_hostname,
  1345. do_handshake_on_connect=False,
  1346. )
  1347. self._add_io_state(old_state)
  1348. def wait_for_handshake(self) -> "Future[SSLIOStream]":
  1349. """Wait for the initial SSL handshake to complete.
  1350. If a ``callback`` is given, it will be called with no
  1351. arguments once the handshake is complete; otherwise this
  1352. method returns a `.Future` which will resolve to the
  1353. stream itself after the handshake is complete.
  1354. Once the handshake is complete, information such as
  1355. the peer's certificate and NPN/ALPN selections may be
  1356. accessed on ``self.socket``.
  1357. This method is intended for use on server-side streams
  1358. or after using `IOStream.start_tls`; it should not be used
  1359. with `IOStream.connect` (which already waits for the
  1360. handshake to complete). It may only be called once per stream.
  1361. .. versionadded:: 4.2
  1362. .. versionchanged:: 6.0
  1363. The ``callback`` argument was removed. Use the returned
  1364. `.Future` instead.
  1365. """
  1366. if self._ssl_connect_future is not None:
  1367. raise RuntimeError("Already waiting")
  1368. future = self._ssl_connect_future = Future()
  1369. if not self._ssl_accepting:
  1370. self._finish_ssl_connect()
  1371. return future
  1372. def write_to_fd(self, data: memoryview) -> int:
  1373. try:
  1374. return self.socket.send(data) # type: ignore
  1375. except ssl.SSLError as e:
  1376. if e.args[0] == ssl.SSL_ERROR_WANT_WRITE:
  1377. # In Python 3.5+, SSLSocket.send raises a WANT_WRITE error if
  1378. # the socket is not writeable; we need to transform this into
  1379. # an EWOULDBLOCK socket.error or a zero return value,
  1380. # either of which will be recognized by the caller of this
  1381. # method. Prior to Python 3.5, an unwriteable socket would
  1382. # simply return 0 bytes written.
  1383. return 0
  1384. raise
  1385. finally:
  1386. # Avoid keeping to data, which can be a memoryview.
  1387. # See https://github.com/tornadoweb/tornado/pull/2008
  1388. del data
  1389. def read_from_fd(self, buf: Union[bytearray, memoryview]) -> Optional[int]:
  1390. try:
  1391. if self._ssl_accepting:
  1392. # If the handshake hasn't finished yet, there can't be anything
  1393. # to read (attempting to read may or may not raise an exception
  1394. # depending on the SSL version)
  1395. return None
  1396. try:
  1397. return self.socket.recv_into(buf, len(buf))
  1398. except ssl.SSLError as e:
  1399. # SSLError is a subclass of socket.error, so this except
  1400. # block must come first.
  1401. if e.args[0] == ssl.SSL_ERROR_WANT_READ:
  1402. return None
  1403. else:
  1404. raise
  1405. except socket.error as e:
  1406. if e.args[0] in _ERRNO_WOULDBLOCK:
  1407. return None
  1408. else:
  1409. raise
  1410. finally:
  1411. del buf
  1412. def _is_connreset(self, e: BaseException) -> bool:
  1413. if isinstance(e, ssl.SSLError) and e.args[0] == ssl.SSL_ERROR_EOF:
  1414. return True
  1415. return super(SSLIOStream, self)._is_connreset(e)
  1416. class PipeIOStream(BaseIOStream):
  1417. """Pipe-based `IOStream` implementation.
  1418. The constructor takes an integer file descriptor (such as one returned
  1419. by `os.pipe`) rather than an open file object. Pipes are generally
  1420. one-way, so a `PipeIOStream` can be used for reading or writing but not
  1421. both.
  1422. """
  1423. def __init__(self, fd: int, *args: Any, **kwargs: Any) -> None:
  1424. self.fd = fd
  1425. self._fio = io.FileIO(self.fd, "r+")
  1426. _set_nonblocking(fd)
  1427. super(PipeIOStream, self).__init__(*args, **kwargs)
  1428. def fileno(self) -> int:
  1429. return self.fd
  1430. def close_fd(self) -> None:
  1431. self._fio.close()
  1432. def write_to_fd(self, data: memoryview) -> int:
  1433. try:
  1434. return os.write(self.fd, data) # type: ignore
  1435. finally:
  1436. # Avoid keeping to data, which can be a memoryview.
  1437. # See https://github.com/tornadoweb/tornado/pull/2008
  1438. del data
  1439. def read_from_fd(self, buf: Union[bytearray, memoryview]) -> Optional[int]:
  1440. try:
  1441. return self._fio.readinto(buf) # type: ignore
  1442. except (IOError, OSError) as e:
  1443. if errno_from_exception(e) == errno.EBADF:
  1444. # If the writing half of a pipe is closed, select will
  1445. # report it as readable but reads will fail with EBADF.
  1446. self.close(exc_info=e)
  1447. return None
  1448. else:
  1449. raise
  1450. finally:
  1451. del buf
  1452. def doctests() -> Any:
  1453. import doctest
  1454. return doctest.DocTestSuite()