You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

507 lines
18KB

  1. import errno
  2. import os
  3. import socket
  4. import sys
  5. import time
  6. import warnings
  7. import eventlet
  8. from eventlet.hubs import trampoline, notify_opened, IOClosed
  9. from eventlet.support import get_errno
  10. import six
  11. __all__ = [
  12. 'GreenSocket', '_GLOBAL_DEFAULT_TIMEOUT', 'set_nonblocking',
  13. 'SOCKET_BLOCKING', 'SOCKET_CLOSED', 'CONNECT_ERR', 'CONNECT_SUCCESS',
  14. 'shutdown_safe', 'SSL',
  15. 'socket_timeout',
  16. ]
  17. BUFFER_SIZE = 4096
  18. CONNECT_ERR = set((errno.EINPROGRESS, errno.EALREADY, errno.EWOULDBLOCK))
  19. CONNECT_SUCCESS = set((0, errno.EISCONN))
  20. if sys.platform[:3] == "win":
  21. CONNECT_ERR.add(errno.WSAEINVAL) # Bug 67
  22. if six.PY2:
  23. _python2_fileobject = socket._fileobject
  24. _original_socket = eventlet.patcher.original('socket').socket
  25. socket_timeout = eventlet.timeout.wrap_is_timeout(socket.timeout)
  26. def socket_connect(descriptor, address):
  27. """
  28. Attempts to connect to the address, returns the descriptor if it succeeds,
  29. returns None if it needs to trampoline, and raises any exceptions.
  30. """
  31. err = descriptor.connect_ex(address)
  32. if err in CONNECT_ERR:
  33. return None
  34. if err not in CONNECT_SUCCESS:
  35. raise socket.error(err, errno.errorcode[err])
  36. return descriptor
  37. def socket_checkerr(descriptor):
  38. err = descriptor.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
  39. if err not in CONNECT_SUCCESS:
  40. raise socket.error(err, errno.errorcode[err])
  41. def socket_accept(descriptor):
  42. """
  43. Attempts to accept() on the descriptor, returns a client,address tuple
  44. if it succeeds; returns None if it needs to trampoline, and raises
  45. any exceptions.
  46. """
  47. try:
  48. return descriptor.accept()
  49. except socket.error as e:
  50. if get_errno(e) == errno.EWOULDBLOCK:
  51. return None
  52. raise
  53. if sys.platform[:3] == "win":
  54. # winsock sometimes throws ENOTCONN
  55. SOCKET_BLOCKING = set((errno.EAGAIN, errno.EWOULDBLOCK,))
  56. SOCKET_CLOSED = set((errno.ECONNRESET, errno.ENOTCONN, errno.ESHUTDOWN))
  57. else:
  58. # oddly, on linux/darwin, an unconnected socket is expected to block,
  59. # so we treat ENOTCONN the same as EWOULDBLOCK
  60. SOCKET_BLOCKING = set((errno.EAGAIN, errno.EWOULDBLOCK, errno.ENOTCONN))
  61. SOCKET_CLOSED = set((errno.ECONNRESET, errno.ESHUTDOWN, errno.EPIPE))
  62. def set_nonblocking(fd):
  63. """
  64. Sets the descriptor to be nonblocking. Works on many file-like
  65. objects as well as sockets. Only sockets can be nonblocking on
  66. Windows, however.
  67. """
  68. try:
  69. setblocking = fd.setblocking
  70. except AttributeError:
  71. # fd has no setblocking() method. It could be that this version of
  72. # Python predates socket.setblocking(). In that case, we can still set
  73. # the flag "by hand" on the underlying OS fileno using the fcntl
  74. # module.
  75. try:
  76. import fcntl
  77. except ImportError:
  78. # Whoops, Windows has no fcntl module. This might not be a socket
  79. # at all, but rather a file-like object with no setblocking()
  80. # method. In particular, on Windows, pipes don't support
  81. # non-blocking I/O and therefore don't have that method. Which
  82. # means fcntl wouldn't help even if we could load it.
  83. raise NotImplementedError("set_nonblocking() on a file object "
  84. "with no setblocking() method "
  85. "(Windows pipes don't support non-blocking I/O)")
  86. # We managed to import fcntl.
  87. fileno = fd.fileno()
  88. orig_flags = fcntl.fcntl(fileno, fcntl.F_GETFL)
  89. new_flags = orig_flags | os.O_NONBLOCK
  90. if new_flags != orig_flags:
  91. fcntl.fcntl(fileno, fcntl.F_SETFL, new_flags)
  92. else:
  93. # socket supports setblocking()
  94. setblocking(0)
  95. try:
  96. from socket import _GLOBAL_DEFAULT_TIMEOUT
  97. except ImportError:
  98. _GLOBAL_DEFAULT_TIMEOUT = object()
  99. class GreenSocket(object):
  100. """
  101. Green version of socket.socket class, that is intended to be 100%
  102. API-compatible.
  103. It also recognizes the keyword parameter, 'set_nonblocking=True'.
  104. Pass False to indicate that socket is already in non-blocking mode
  105. to save syscalls.
  106. """
  107. # This placeholder is to prevent __getattr__ from creating an infinite call loop
  108. fd = None
  109. def __init__(self, family=socket.AF_INET, *args, **kwargs):
  110. should_set_nonblocking = kwargs.pop('set_nonblocking', True)
  111. if isinstance(family, six.integer_types):
  112. fd = _original_socket(family, *args, **kwargs)
  113. # Notify the hub that this is a newly-opened socket.
  114. notify_opened(fd.fileno())
  115. else:
  116. fd = family
  117. # import timeout from other socket, if it was there
  118. try:
  119. self._timeout = fd.gettimeout() or socket.getdefaulttimeout()
  120. except AttributeError:
  121. self._timeout = socket.getdefaulttimeout()
  122. # Filter fd.fileno() != -1 so that won't call set non-blocking on
  123. # closed socket
  124. if should_set_nonblocking and fd.fileno() != -1:
  125. set_nonblocking(fd)
  126. self.fd = fd
  127. # when client calls setblocking(0) or settimeout(0) the socket must
  128. # act non-blocking
  129. self.act_non_blocking = False
  130. # Copy some attributes from underlying real socket.
  131. # This is the easiest way that i found to fix
  132. # https://bitbucket.org/eventlet/eventlet/issue/136
  133. # Only `getsockopt` is required to fix that issue, others
  134. # are just premature optimization to save __getattr__ call.
  135. self.bind = fd.bind
  136. self.close = fd.close
  137. self.fileno = fd.fileno
  138. self.getsockname = fd.getsockname
  139. self.getsockopt = fd.getsockopt
  140. self.listen = fd.listen
  141. self.setsockopt = fd.setsockopt
  142. self.shutdown = fd.shutdown
  143. self._closed = False
  144. @property
  145. def _sock(self):
  146. return self
  147. if six.PY3:
  148. def _get_io_refs(self):
  149. return self.fd._io_refs
  150. def _set_io_refs(self, value):
  151. self.fd._io_refs = value
  152. _io_refs = property(_get_io_refs, _set_io_refs)
  153. # Forward unknown attributes to fd, cache the value for future use.
  154. # I do not see any simple attribute which could be changed
  155. # so caching everything in self is fine.
  156. # If we find such attributes - only attributes having __get__ might be cached.
  157. # For now - I do not want to complicate it.
  158. def __getattr__(self, name):
  159. if self.fd is None:
  160. raise AttributeError(name)
  161. attr = getattr(self.fd, name)
  162. setattr(self, name, attr)
  163. return attr
  164. def _trampoline(self, fd, read=False, write=False, timeout=None, timeout_exc=None):
  165. """ We need to trampoline via the event hub.
  166. We catch any signal back from the hub indicating that the operation we
  167. were waiting on was associated with a filehandle that's since been
  168. invalidated.
  169. """
  170. if self._closed:
  171. # If we did any logging, alerting to a second trampoline attempt on a closed
  172. # socket here would be useful.
  173. raise IOClosed()
  174. try:
  175. return trampoline(fd, read=read, write=write, timeout=timeout,
  176. timeout_exc=timeout_exc,
  177. mark_as_closed=self._mark_as_closed)
  178. except IOClosed:
  179. # This socket's been obsoleted. De-fang it.
  180. self._mark_as_closed()
  181. raise
  182. def accept(self):
  183. if self.act_non_blocking:
  184. res = self.fd.accept()
  185. notify_opened(res[0].fileno())
  186. return res
  187. fd = self.fd
  188. _timeout_exc = socket_timeout('timed out')
  189. while True:
  190. res = socket_accept(fd)
  191. if res is not None:
  192. client, addr = res
  193. notify_opened(client.fileno())
  194. set_nonblocking(client)
  195. return type(self)(client), addr
  196. self._trampoline(fd, read=True, timeout=self.gettimeout(), timeout_exc=_timeout_exc)
  197. def _mark_as_closed(self):
  198. """ Mark this socket as being closed """
  199. self._closed = True
  200. def __del__(self):
  201. # This is in case self.close is not assigned yet (currently the constructor does it)
  202. close = getattr(self, 'close', None)
  203. if close is not None:
  204. close()
  205. def connect(self, address):
  206. if self.act_non_blocking:
  207. return self.fd.connect(address)
  208. fd = self.fd
  209. _timeout_exc = socket_timeout('timed out')
  210. if self.gettimeout() is None:
  211. while not socket_connect(fd, address):
  212. try:
  213. self._trampoline(fd, write=True)
  214. except IOClosed:
  215. raise socket.error(errno.EBADFD)
  216. socket_checkerr(fd)
  217. else:
  218. end = time.time() + self.gettimeout()
  219. while True:
  220. if socket_connect(fd, address):
  221. return
  222. if time.time() >= end:
  223. raise _timeout_exc
  224. timeout = end - time.time()
  225. try:
  226. self._trampoline(fd, write=True, timeout=timeout, timeout_exc=_timeout_exc)
  227. except IOClosed:
  228. # ... we need some workable errno here.
  229. raise socket.error(errno.EBADFD)
  230. socket_checkerr(fd)
  231. def connect_ex(self, address):
  232. if self.act_non_blocking:
  233. return self.fd.connect_ex(address)
  234. fd = self.fd
  235. if self.gettimeout() is None:
  236. while not socket_connect(fd, address):
  237. try:
  238. self._trampoline(fd, write=True)
  239. socket_checkerr(fd)
  240. except socket.error as ex:
  241. return get_errno(ex)
  242. except IOClosed:
  243. return errno.EBADFD
  244. else:
  245. end = time.time() + self.gettimeout()
  246. timeout_exc = socket.timeout(errno.EAGAIN)
  247. while True:
  248. try:
  249. if socket_connect(fd, address):
  250. return 0
  251. if time.time() >= end:
  252. raise timeout_exc
  253. self._trampoline(fd, write=True, timeout=end - time.time(),
  254. timeout_exc=timeout_exc)
  255. socket_checkerr(fd)
  256. except socket.error as ex:
  257. return get_errno(ex)
  258. except IOClosed:
  259. return errno.EBADFD
  260. def dup(self, *args, **kw):
  261. sock = self.fd.dup(*args, **kw)
  262. newsock = type(self)(sock, set_nonblocking=False)
  263. newsock.settimeout(self.gettimeout())
  264. return newsock
  265. if six.PY3:
  266. def makefile(self, *args, **kwargs):
  267. return _original_socket.makefile(self, *args, **kwargs)
  268. else:
  269. def makefile(self, *args, **kwargs):
  270. dupped = self.dup()
  271. res = _python2_fileobject(dupped, *args, **kwargs)
  272. if hasattr(dupped, "_drop"):
  273. dupped._drop()
  274. # Making the close function of dupped None so that when garbage collector
  275. # kicks in and tries to call del, which will ultimately call close, _drop
  276. # doesn't get called on dupped twice as it has been already explicitly called in
  277. # previous line
  278. dupped.close = None
  279. return res
  280. def makeGreenFile(self, *args, **kw):
  281. warnings.warn("makeGreenFile has been deprecated, please use "
  282. "makefile instead", DeprecationWarning, stacklevel=2)
  283. return self.makefile(*args, **kw)
  284. def _read_trampoline(self):
  285. self._trampoline(
  286. self.fd,
  287. read=True,
  288. timeout=self.gettimeout(),
  289. timeout_exc=socket_timeout('timed out'))
  290. def _recv_loop(self, recv_meth, empty_val, *args):
  291. fd = self.fd
  292. if self.act_non_blocking:
  293. return recv_meth(*args)
  294. while True:
  295. try:
  296. # recv: bufsize=0?
  297. # recv_into: buffer is empty?
  298. # This is needed because behind the scenes we use sockets in
  299. # nonblocking mode and builtin recv* methods. Attempting to read
  300. # 0 bytes from a nonblocking socket using a builtin recv* method
  301. # does not raise a timeout exception. Since we're simulating
  302. # a blocking socket here we need to produce a timeout exception
  303. # if needed, hence the call to trampoline.
  304. if not args[0]:
  305. self._read_trampoline()
  306. return recv_meth(*args)
  307. except socket.error as e:
  308. if get_errno(e) in SOCKET_BLOCKING:
  309. pass
  310. elif get_errno(e) in SOCKET_CLOSED:
  311. return empty_val
  312. else:
  313. raise
  314. try:
  315. self._read_trampoline()
  316. except IOClosed as e:
  317. # Perhaps we should return '' instead?
  318. raise EOFError()
  319. def recv(self, bufsize, flags=0):
  320. return self._recv_loop(self.fd.recv, b'', bufsize, flags)
  321. def recvfrom(self, bufsize, flags=0):
  322. return self._recv_loop(self.fd.recvfrom, b'', bufsize, flags)
  323. def recv_into(self, buffer, nbytes=0, flags=0):
  324. return self._recv_loop(self.fd.recv_into, 0, buffer, nbytes, flags)
  325. def recvfrom_into(self, buffer, nbytes=0, flags=0):
  326. return self._recv_loop(self.fd.recvfrom_into, 0, buffer, nbytes, flags)
  327. def _send_loop(self, send_method, data, *args):
  328. if self.act_non_blocking:
  329. return send_method(data, *args)
  330. _timeout_exc = socket_timeout('timed out')
  331. while True:
  332. try:
  333. return send_method(data, *args)
  334. except socket.error as e:
  335. eno = get_errno(e)
  336. if eno == errno.ENOTCONN or eno not in SOCKET_BLOCKING:
  337. raise
  338. try:
  339. self._trampoline(self.fd, write=True, timeout=self.gettimeout(),
  340. timeout_exc=_timeout_exc)
  341. except IOClosed:
  342. raise socket.error(errno.ECONNRESET, 'Connection closed by another thread')
  343. def send(self, data, flags=0):
  344. return self._send_loop(self.fd.send, data, flags)
  345. def sendto(self, data, *args):
  346. return self._send_loop(self.fd.sendto, data, *args)
  347. def sendall(self, data, flags=0):
  348. tail = self.send(data, flags)
  349. len_data = len(data)
  350. while tail < len_data:
  351. tail += self.send(data[tail:], flags)
  352. def setblocking(self, flag):
  353. if flag:
  354. self.act_non_blocking = False
  355. self._timeout = None
  356. else:
  357. self.act_non_blocking = True
  358. self._timeout = 0.0
  359. def settimeout(self, howlong):
  360. if howlong is None or howlong == _GLOBAL_DEFAULT_TIMEOUT:
  361. self.setblocking(True)
  362. return
  363. try:
  364. f = howlong.__float__
  365. except AttributeError:
  366. raise TypeError('a float is required')
  367. howlong = f()
  368. if howlong < 0.0:
  369. raise ValueError('Timeout value out of range')
  370. if howlong == 0.0:
  371. self.act_non_blocking = True
  372. self._timeout = 0.0
  373. else:
  374. self.act_non_blocking = False
  375. self._timeout = howlong
  376. def gettimeout(self):
  377. return self._timeout
  378. def __enter__(self):
  379. return self
  380. def __exit__(self, *args):
  381. self.close()
  382. if "__pypy__" in sys.builtin_module_names:
  383. def _reuse(self):
  384. getattr(self.fd, '_sock', self.fd)._reuse()
  385. def _drop(self):
  386. getattr(self.fd, '_sock', self.fd)._drop()
  387. def _operation_on_closed_file(*args, **kwargs):
  388. raise ValueError("I/O operation on closed file")
  389. greenpipe_doc = """
  390. GreenPipe is a cooperative replacement for file class.
  391. It will cooperate on pipes. It will block on regular file.
  392. Differneces from file class:
  393. - mode is r/w property. Should re r/o
  394. - encoding property not implemented
  395. - write/writelines will not raise TypeError exception when non-string data is written
  396. it will write str(data) instead
  397. - Universal new lines are not supported and newlines property not implementeded
  398. - file argument can be descriptor, file name or file object.
  399. """
  400. # import SSL module here so we can refer to greenio.SSL.exceptionclass
  401. try:
  402. from OpenSSL import SSL
  403. except ImportError:
  404. # pyOpenSSL not installed, define exceptions anyway for convenience
  405. class SSL(object):
  406. class WantWriteError(Exception):
  407. pass
  408. class WantReadError(Exception):
  409. pass
  410. class ZeroReturnError(Exception):
  411. pass
  412. class SysCallError(Exception):
  413. pass
  414. def shutdown_safe(sock):
  415. """Shuts down the socket. This is a convenience method for
  416. code that wants to gracefully handle regular sockets, SSL.Connection
  417. sockets from PyOpenSSL and ssl.SSLSocket objects from Python 2.7 interchangeably.
  418. Both types of ssl socket require a shutdown() before close,
  419. but they have different arity on their shutdown method.
  420. Regular sockets don't need a shutdown before close, but it doesn't hurt.
  421. """
  422. try:
  423. try:
  424. # socket, ssl.SSLSocket
  425. return sock.shutdown(socket.SHUT_RDWR)
  426. except TypeError:
  427. # SSL.Connection
  428. return sock.shutdown()
  429. except socket.error as e:
  430. # we don't care if the socket is already closed;
  431. # this will often be the case in an http server context
  432. if get_errno(e) not in (errno.ENOTCONN, errno.EBADF, errno.ENOTSOCK):
  433. raise