예제 #1
0
 def test_del_noclose(self):
     r, w = os.pipe()
     s = SocketAdapter(w, close=False)
     s.sendall('x')
     del s
     os.close(w)
     self.assertEqual(FileObject(r).read(), 'x')
예제 #2
0
 def _test_del(self, **kwargs):
     r, w = os.pipe()
     s = SocketAdapter(w)
     s.sendall('x')
     del s
     try:
         os.close(w)
     except OSError:
         pass  # expected, because SocketAdapter already closed it
     else:
         raise AssertionError('os.close(%r) must not succeed' % w)
     self.assertEqual(FileObject(r).read(), 'x')
예제 #3
0
    def _handle_connect(self, environ, start_response):
        """HTTP tunneling."""
        netloc = self.resolver(environ['PATH_INFO'])
        try:
            hostname, port = netloc.split(':', 1)
            port = int(port)
        except TypeError:
            raise
        except ValueError:
            raise

        sock = socket.socket()
        sock.connect((hostname, port))

        def _forward(dst, src):
            while True:
                data = src.recv(4096)
                if data:
                    dst.sendall(data)
                else:
                    break

        write = start_response('200 OK', [('Connection', 'close')])
        write('')

        from gevent.fileobject import FileObject, SocketAdapter

        input = SocketAdapter(os.dup(environ['wsgi.input'].rfile.fileno()),
                              'rb')
        gevent.spawn(_forward, sock, input)
        try:
            while True:
                data = sock.recv(4096)
                if data:
                    yield data
                else:
                    break
        finally:
            sock.close()