Exemplo n.º 1
0
 def setup(self):
     if self.server.cert_file:
         self.connection = self.request
         self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
         self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
     else:
         WSGIRequestHandler.setup(self)
Exemplo n.º 2
0
 def setup(self):
     """Setup the connection."""
     self.connection = self.request
     if trace: print 'SETUP', self.connection
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
     if trace: print 'AFTER', self.rfile, self.wfile
Exemplo n.º 3
0
 def makefile(self, mode='r', bufsize=-1):
     self.__makefile_refs += 1
     try:
         return socket._fileobject(self, mode, bufsize, close=True)
     except TypeError:
         # Python 2.2 compatibility hacks.
         return socket._fileobject(self, mode, bufsize)
Exemplo n.º 4
0
 def __init__(self, sock, client, args):
     self.connection = sock
     self.rfile = socket._fileobject(self.connection, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.connection, "wb", self.wbufsize)
     self.client_address = client
     self.args = args
     self.setup()
Exemplo n.º 5
0
 def setup(self):
     """Override SocketServer.StreamRequestHandler.setup."""
     self.connection = self.request
     self.rfile = memorizingfile.MemorizingFile(
             socket._fileobject(self.request, 'rb', self.rbufsize),
             max_memorized_lines=self._MAX_MEMORIZED_LINES)
     self.wfile = socket._fileobject(self.request, 'wb', self.wbufsize)
Exemplo n.º 6
0
 def setup(self):
     self.connection = self.request
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
     #トランザクションログ・トランザクションデータログ用のロガー
     self._logger = logging.getLogger('vhut.tran')
     self._logger_data = logging.getLogger('vhut.trandata')
    def setup(self):
        self.connection = self.request
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
	
	print self.rfile
	print self.wfile
Exemplo n.º 8
0
 def __ssl_setup(self):
     if self.server_key == None or self.server_cert == None:
         sys.exit("ServerRequestHandler not correctly initialized " +
                  "to use SSL encryption! (Missing key or cert file)")
     self.connection = self.request
     self.rfile = socket._fileobject(self.connection, 'rb', self.rbufsize)
     self.wfile = socket._fileobject(self.connection, 'wb', self.wbufsize)
Exemplo n.º 9
0
    def __init__(self, sock, client, args):
        self.conn = sock
        self.rfile = socket._fileobject(self.conn, "rb", -1)
        self.wfile = socket._fileobject(self.conn, "wb", 0)
        self.client_address = client

        self.read_buffer = ""
        self.buffer_start = 0
Exemplo n.º 10
0
 def makefile(self, mode='rb', bufsize=-1):
     log.debug('self.socket = %s, mode = "%s", bufsize = %d',
               self.socket, mode, bufsize)
     if not six.PY3:
         return socket._fileobject(self, mode, bufsize)
     else:
         # FIXME This is nonsense, I know, it is here just as filling
         return socket._fileobject(self, mode, bufsize)
Exemplo n.º 11
0
	def setup(self):
		self.connection = self.request
		if self.server.sslOpts:
			self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
			self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
		else:
			self.rfile = self.connection.makefile('rb', self.rbufsize)
			self.wfile = self.connection.makefile('wb', self.wbufsize)	
Exemplo n.º 12
0
 def setup(self):
   self.suppress_body = False
   if self.server.enable_ssl:
     self.connection = self.request
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
   else:
     SimpleXMLRPCRequestHandler.setup(self)
Exemplo n.º 13
0
	def setup(self):
		"""initial connection setup, setting rfile, wfile, and connection"""
		if hasattr(self.request, 'do_handshake'): #ssl mode
			self.connection = self.request
			self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
			self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
		else:
			httpserver.BaseHTTPRequestHandler.setup(self)
Exemplo n.º 14
0
 def setup(self):
     """
     We need to use socket._fileobject Because SSL.Connection
     doesn't have a 'dup'. Not exactly sure WHY this is, but
     this is backed up by comments in socket.py and SSL/connection.c
     """
     self.connection = self.request # for do_POST
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
Exemplo n.º 15
0
 def setup(self):
   self.connection = self.request
   if self.server.is_https:
     # TODO(user): SSL.Connection.makefile() raises NotImplemented - why?
     self.rfile = socket._fileobject(self.request, 'rb', self.rbufsize)
     self.wfile = socket._fileobject(self.request, 'wb', self.wbufsize)
   else:
     self.rfile = self.connection.makefile('rb', self.rbufsize)
     self.wfile = self.connection.makefile('wb', self.wbufsize)
Exemplo n.º 16
0
 def setup(self):
     self.connection = self.request
     if self.timeout is not None:
         self.connection.settimeout(self.timeout)
     if self.disable_nagle_algorithm:
         self.connection.setsockopt(socket.IPPROTO_TCP,
                                    socket.TCP_NODELAY, True)
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
Exemplo n.º 17
0
Arquivo: edna.py Projeto: plux/edna
  def setup(self):
    if self.server.config.get('server', 'https') == '1':
      self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
      self.wfile = socket._fileobject(self.request, "wb", self.wbufsize) 
    else: 
      SocketServer.StreamRequestHandler.setup(self)

    # wrap the wfile with a class that will eat up "Broken pipe" errors
    self.wfile = _SocketWriter(self.wfile)
Exemplo n.º 18
0
    def setup(self):
        self.connection = self.request
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
        
	def shutdown_request(self,request):
		request.shutdown()
	
	def shutdown(self, args=None):
         return self._con.shutdown()
Exemplo n.º 19
0
 def setup(self):
     """ Function only for SSL
     """
     use_ssl = self.server.handler_params[0].use_ssl
     if use_ssl == True:
         self.connection = self.request
         self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
         self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
     else:
         BaseHTTPRequestHandler.setup(self)
Exemplo n.º 20
0
    def __init__(self, sock, client, args, logger=logging):
        self.connection = sock
        sock.settimeout(300)
        self.rfile = socket._fileobject(self.connection, "rb", self.rbufsize, close=True)
        self.wfile = socket._fileobject(self.connection, "wb", self.wbufsize, close=True)
        self.client_address = client
        self.args = args
        self.logger = logger

        self.setup()
Exemplo n.º 21
0
 def setup(self):
     self.log=logging.getLogger(__name__)
     self.application_root = "/copernicus"
     self.connection = self.request
     self.responseCode = 200
     self.set_cookie = None
     self.regexp = '^%s[/?]?'%self.application_root  #checks if a request is referring to application root
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
     self.request.revertSocket = False
        def setup(self):
                # added a try except block in case of transmission errors
                try:

                        self.connection = self.request
                        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
                        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)

                # except errors and pass them
                except: pass
Exemplo n.º 23
0
    def setup(self):
        """
        Overriding StreamRequestHandler.setup() in SocketServer.py

        We need to use socket._fileobject Because SSL.Connection
        doesn't have a 'dup'. Not exactly sure WHY this is, but
        this is backed up by comments in socket.py and SSL/connection.c
        """
        self.connection = self.request
        self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
        self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
Exemplo n.º 24
0
 def __init__(self, request, client_address, server):
     self.request = request
     self.client_address = client_address
     self.server = server
     try:
         self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
         self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
         self.handle()
         self.finish()
     finally:
         sys.exc_traceback = None    # Help garbage collection
Exemplo n.º 25
0
    def __init__(self, sock, client, args, logger=None):
        self.connection = sock
        sock.setblocking(1)
        sock.settimeout(300)
        self.rfile = socket._fileobject(self.connection, "rb", self.rbufsize, close=True)
        self.wfile = socket._fileobject(self.connection, "wb", self.wbufsize, close=True)
        self.client_address = client
        self.args = args
        if logger:
            self.logger = logger
        else:
            self.logger = xlog.getLogger("simple_http_server")

        self.setup()
Exemplo n.º 26
0
    def test_connect_ssl(self):
        def accept_once(listenfd):
            try:
                conn, addr = listenfd.accept()
                conn.write(b'hello\r\n')
                greenio.shutdown_safe(conn)
                conn.close()
            finally:
                greenio.shutdown_safe(listenfd)
                listenfd.close()

        server = api.ssl_listener(('0.0.0.0', 0),
                                  self.certificate_file,
                                  self.private_key_file)
        api.spawn(accept_once, server)

        raw_client = eventlet.connect(('127.0.0.1', server.getsockname()[1]))
        client = util.wrap_ssl(raw_client)
        fd = socket._fileobject(client, 'rb', 8192)

        assert fd.readline() == b'hello\r\n'
        try:
            self.assertEqual(b'', fd.read(10))
        except greenio.SSL.ZeroReturnError:
            # if it's a GreenSSL object it'll do this
            pass
        greenio.shutdown_safe(client)
        client.close()

        check_hub()
Exemplo n.º 27
0
 def _test_readline_no_buffer(self, size=-1):
     mock_sock = self.MockSocket(
         recv_funcs=[lambda: "aa", lambda: "\n", lambda: "BB", self._raise_eintr, lambda: "bb", lambda: ""]
     )
     fo = socket._fileobject(mock_sock, bufsize=0)
     self.assertEquals(fo.readline(size), "aa\n")
     self.assertEquals(fo.readline(size), "BBbb")
Exemplo n.º 28
0
    def makefile(self, *args, **kwargs):
        # Making sure socket is closed when this file is closed
        # since we now avoid closing socket on connection close
        # see new close method under VerifiedHTTPSConnection
        kwargs['close'] = True

        return socket._fileobject(self.connection, *args, **kwargs)
Exemplo n.º 29
0
def doping(s):
    s.settimeout(14)
    pong = 0

    send("PING:%s:%s:%s:%i" %(socket.gethostname(), sys.platform, os.name, 
        time.time()), s)

    try:
        fd = socket._fileobject(s)
        for line in fd:
            line = line.strip()
            #print "-->", [line]
            if line.upper().startswith("PONG"):
                print "Pong from %s, remote info: Platform: %s, Name: %s,\
Time: %s" %(line.split(":")[1], line.split(":")[2], 
                    line.split(":")[3], strftime("%Y-%m-%d %H:%M:%S", 
                        line.split(":")[4]))
                s.close()
                conf['lastpong'] = time.time()
                conf['alarm']['pong']['ip'] = conf['remote']['host']
                conf['alarm']['pong']['hostname'] = line.split(":")[1]
                return 1
    except Exception, e:
        #print e
        return 0
Exemplo n.º 30
0
    def makefile(self, mode='r', bufsize=-1):
        """Create a file object for the TLS connection (socket emulation).

        :rtype: socket._fileobject
        """
        self._refCount += 1
        # So, it is pretty fragile to be using Python internal objects
        # like this, but it is probably the best/easiest way to provide
        # matching behavior for socket emulation purposes.  The 'close'
        # argument is nice, its apparently a recent addition to this
        # class, so that when fileobject.close() gets called, it will
        # close() us, causing the refcount to be decremented (decrefAsync).
        #
        # If this is the last close() on the outstanding fileobjects / 
        # TLSConnection, then the "actual" close alerts will be sent,
        # socket closed, etc.

        # for writes, we MUST buffer otherwise the lengths of headers leak
        # through record layer boundaries
        if 'w' in mode and bufsize <= 0:
            bufsize = 2**14

        if sys.version_info < (3,):
            return socket._fileobject(self, mode, bufsize, close=True)
        else:
            if 'w' in mode:
                return io.BufferedWriter(socket.SocketIO(self, mode), bufsize)
            else:
                return socket.SocketIO(self, mode)
Exemplo n.º 31
0
        logging.error('Exception in SNI handler: %s', e)

    context.set_tlsext_servername_callback(handle_servername)
    self.connection = certutils.get_ssl_connection(context, self.connection)
    self.connection.set_accept_state()
    try:
      self.connection.do_handshake()
    except certutils.Error, v:
      host = self.connection.get_servername()
      if not host:
        logging.error('Dropping request without SNI')
        return ''
      raise certutils.Error('SSL handshake error %s: %s' % (host, str(v)))

    # Re-wrap the read/write streams with our new connection.
    self.rfile = socket._fileobject(self.connection, 'rb', self.rbufsize,
                                    close=False)
    self.wfile = socket._fileobject(self.connection, 'wb', self.wbufsize,
                                    close=False)

  def finish(self):
    self.connection.shutdown()
    self.connection.close()


def wrap_handler(handler_class):
  """Wraps a BaseHTTPHandler wtih SSL MITM certificates."""
  if certutils.openssl_import_error:
    raise certutils.openssl_import_error

  class WrappedHandler(SslHandshakeHandler, handler_class):
Exemplo n.º 32
0
 def setup(self):
     self.connection = self.request._sock
     self.request._sock.setblocking(1)
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.rbufsize)
Exemplo n.º 33
0
 def makefile(self, *args, **kwargs):
     return socket._fileobject(self.connection, *args, **kwargs)
Exemplo n.º 34
0
Arquivo: oscssl.py Projeto: zilti/osc
    def https_open(self, req):
        # https://docs.python.org/3.3/library/urllib.request.html#urllib.request.Request.get_host
        try:  # up to python-3.2
            host = req.get_host()
        except AttributeError:  # from python-3.3
            host = req.host
        if not host:
            raise M2Crypto.m2urllib2.URLError('no host given')

        # Our change: Check to see if we're using a proxy.
        # Then create an appropriate ssl-aware connection.
        full_url = req.get_full_url()
        target_host = urlparse(full_url)[1]

        if target_host != host:
            request_uri = urldefrag(full_url)[0]
            h = myProxyHTTPSConnection(host=host,
                                       appname=self.appname,
                                       ssl_context=self.ctx)
        else:
            try:  # up to python-3.2
                request_uri = req.get_selector()
            except AttributeError:  # from python-3.3
                request_uri = req.selector
            h = myHTTPSConnection(host=host,
                                  appname=self.appname,
                                  ssl_context=self.ctx)
        # End our change
        h.set_debuglevel(self._debuglevel)

        headers = dict(req.headers)
        headers.update(req.unredirected_hdrs)
        # We want to make an HTTP/1.1 request, but the addinfourl
        # class isn't prepared to deal with a persistent connection.
        # It will try to read all remaining data from the socket,
        # which will block while the server waits for the next request.
        # So make sure the connection gets closed after the (only)
        # request.
        headers["Connection"] = "close"
        try:
            h.request(req.get_method(), request_uri, req.data, headers)
            r = h.getresponse()
        except socket.error as err:  # XXX what error?
            raise M2Crypto.m2urllib2.URLError(err)

        # Pick apart the HTTPResponse object to get the addinfourl
        # object initialized properly.

        # Wrap the HTTPResponse object in socket's file object adapter
        # for Windows.  That adapter calls recv(), so delegate recv()
        # to read().  This weird wrapping allows the returned object to
        # have readline() and readlines() methods.
        r.recv = r.read
        if (sys.version_info < (3, 0)):
            fp = socket._fileobject(r, close=True)
        else:
            r._decref_socketios = lambda: None
            r.ssl = h.sock.ssl
            r._timeout = -1.0
            # hack to bypass python3 bug with 0 buffer size and
            # http/client.py readinto method for response class
            if r.length is not None and r.length == 0:
                r.readinto = lambda b: 0
            r.recv_into = r.readinto
            fp = socket.SocketIO(r, 'rb')

        resp = addinfourl(fp, r.msg, req.get_full_url())
        resp.code = r.status
        resp.msg = r.reason
        return resp
Exemplo n.º 35
0
 def makefile(self, mode='rb', bufsize=-1):
     if mode == 'rb':
         return socket._fileobject(self, mode, bufsize)
     else:
         return self.sock.makefile(mode, bufsize)
    def _reuse(self):
        self._makefile_refs += 1

    def _drop(self):
        if self._makefile_refs < 1:
            self.close()
        else:
            self._makefile_refs -= 1


if _fileobject:  # Platform-specific: Python 2

    def makefile(self, mode, bufsize=-1):
        self._makefile_refs += 1
        return _fileobject(self, mode, bufsize, close=True)


else:  # Platform-specific: Python 3

    def makefile(self, mode="r", buffering=None, *args, **kwargs):
        # We disable buffering with SecureTransport because it conflicts with
        # the buffering that ST does internally (see issue #1153 for more).
        buffering = 0
        return backport_makefile(self, mode, buffering, *args, **kwargs)


WrappedSocket.makefile = makefile


class SecureTransportContext(object):
Exemplo n.º 37
0
 def _makefile(self, mode='r', bufsize=-1):
     from socket import _fileobject
     fo = _fileobject(self, mode, bufsize)
     return fo
Exemplo n.º 38
0
 def create_readline_wrapper(fh):
     return socket._fileobject(fh, close=True)
Exemplo n.º 39
0
 def setup(self):
     self.connection = self.request
     self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
     self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
Exemplo n.º 40
0
    def sha1_digest(bytes):
        return sha.new(bytes).hexdigest()

    def md5_digest(bytes):
        return md5.new(bytes).hexdigest()
else:

    def sha1_digest(bytes):
        return hashlib.sha1(bytes).hexdigest()

    def md5_digest(bytes):
        return hashlib.md5(bytes).hexdigest()


try:
    socket._fileobject("fake socket", close=True)
except TypeError:
    # python <= 2.4
    create_readline_wrapper = socket._fileobject
else:

    def create_readline_wrapper(fh):
        return socket._fileobject(fh, close=True)


# python 2.4 splithost has a bug in empty path component case
_hostprog = None


def splithost(url):
    """splithost('//host[:port]/path') --> 'host[:port]', '/path'."""
Exemplo n.º 41
0
 def create_readline_wrapper(fh):
     if not hasattr(fh, '_drop'):
         fh._drop = lambda: None
         fh._reuse = lambda: None
     return socket._fileobject(fh, close=True)
Exemplo n.º 42
0
 def wrap_input_socket(self, sock):
     return socket._fileobject(sock, 'rb', 512)
Exemplo n.º 43
0
 def makefile(self, *args, **kwargs):
     # Ensure the socket is closed when this file is closed
     kwargs['close'] = True
     return socket._fileobject(self.connection, *args, **kwargs)
Exemplo n.º 44
0
    def https_open(self, req):
        host = req.get_host()
        if not host:
            raise M2Crypto.m2urllib2.URLError('no host given: ' +
                                              req.get_full_url())

        # Our change: Check to see if we're using a proxy.
        # Then create an appropriate ssl-aware connection.
        full_url = req.get_full_url()
        target_host = urlparse(full_url)[1]

        if (target_host != host):
            h = myProxyHTTPSConnection(host=host,
                                       appname=self.appname,
                                       ssl_context=self.ctx)
            # M2Crypto.ProxyHTTPSConnection.putrequest expects a fullurl
            selector = full_url
        else:
            h = myHTTPSConnection(host=host,
                                  appname=self.appname,
                                  ssl_context=self.ctx)
            selector = req.get_selector()
        # End our change
        h.set_debuglevel(self._debuglevel)
        if self.saved_session:
            h.set_session(self.saved_session)

        headers = dict(req.headers)
        headers.update(req.unredirected_hdrs)
        # We want to make an HTTP/1.1 request, but the addinfourl
        # class isn't prepared to deal with a persistent connection.
        # It will try to read all remaining data from the socket,
        # which will block while the server waits for the next request.
        # So make sure the connection gets closed after the (only)
        # request.
        headers["Connection"] = "close"
        try:
            h.request(req.get_method(), selector, req.data, headers)
            s = h.get_session()
            if s:
                self.saved_session = s
            r = h.getresponse()
        except socket.error as err:  # XXX what error?
            err.filename = full_url
            raise M2Crypto.m2urllib2.URLError(err)

        # Pick apart the HTTPResponse object to get the addinfourl
        # object initialized properly.

        # Wrap the HTTPResponse object in socket's file object adapter
        # for Windows.  That adapter calls recv(), so delegate recv()
        # to read().  This weird wrapping allows the returned object to
        # have readline() and readlines() methods.

        # XXX It might be better to extract the read buffering code
        # out of socket._fileobject() and into a base class.

        r.recv = r.read
        fp = socket._fileobject(r)

        resp = addinfourl(fp, r.msg, req.get_full_url())
        resp.code = r.status
        resp.msg = r.reason
        return resp
Exemplo n.º 45
0
    headers["Connection"] = "close"
    headers = dict((name.title(), val) for name, val in headers.items())

    if req.has_data() and not isinstance(req.data, str):
        reqdata = req
    else:
        reqdata = req.data

    try:
        h.request(req.get_method(), req.get_selector(), reqdata, headers)
        r = h.getresponse()
    except socket.error, err:  # XXX what error?
        raise URLError(err)

    r.recv = r.read
    fp = socket._fileobject(r, close=True)

    resp = addinfourl(fp, r.msg, req.get_full_url())
    resp.code = r.status
    resp.msg = r.reason
    return resp


urllib2.AbstractHTTPHandler.do_open = do_open


def _send_request(self, method, url, body, headers):
    # honour explicitly requested Host: and Accept-Encoding headers
    header_names = dict.fromkeys([k.lower() for k in headers])
    skips = {}
    if 'host' in header_names:
Exemplo n.º 46
0
    context.set_tlsext_servername_callback(handle_servername)
    handler.connection = certutils.get_ssl_connection(context,
                                                      handler.connection)
    handler.connection.set_accept_state()
    try:
        handler.connection.do_handshake()
    except certutils.Error, v:
        host = handler.connection.get_servername()
        if not host:
            logging.error('Dropping request without SNI')
            return ''
        raise certutils.Error('SSL handshake error %s: %s' % (host, str(v)))

    # Re-wrap the read/write streams with our new connection.
    handler.rfile = socket._fileobject(handler.connection,
                                       'rb',
                                       handler.rbufsize,
                                       close=False)
    handler.wfile = socket._fileobject(handler.connection,
                                       'wb',
                                       handler.wbufsize,
                                       close=False)


def wrap_handler(handler_class):
    """Wraps a BaseHTTPHandler with SSL MITM certificates."""
    if certutils.openssl_import_error:
        # pylint: disable=raising-bad-type
        raise certutils.openssl_import_error

    class WrappedHandler(handler_class):
        def setup(self):
Exemplo n.º 47
0
 def makefile(self, mode='r', bufsize=-1):
     # use std lib socket's _fileobject
     return _socket._fileobject(self._sock, mode, bufsize)
Exemplo n.º 48
0
 def makefile(self, mode, bufsize):
     fo = socket._fileobject(self.__conn, mode, bufsize)
     return RetryingFile(fo)
Exemplo n.º 49
0
class MakeFile_PY2(getattr(socket, '_fileobject', object)):
    """Faux file object attached to a socket object."""
    def __init__(self, *args, **kwargs):
        """Initialize faux file object."""
        self.bytes_read = 0
        self.bytes_written = 0
        socket._fileobject.__init__(self, *args, **kwargs)
        self._refcount = 0

    def _reuse(self):
        self._refcount += 1

    def _drop(self):
        if self._refcount < 0:
            self.close()
        else:
            self._refcount -= 1

    def write(self, data):
        """Send entire data contents for non-blocking sockets."""
        bytes_sent = 0
        data_mv = memoryview(data)
        payload_size = len(data_mv)
        while bytes_sent < payload_size:
            try:
                bytes_sent += self.send(
                    data_mv[bytes_sent:bytes_sent + SOCK_WRITE_BLOCKSIZE], )
            except socket.error as e:
                if e.args[0] not in errors.socket_errors_nonblocking:
                    raise

    def send(self, data):
        """Send some part of message to the socket."""
        bytes_sent = self._sock.send(extract_bytes(data))
        self.bytes_written += bytes_sent
        return bytes_sent

    def flush(self):
        """Write all data from buffer to socket and reset write buffer."""
        if self._wbuf:
            buffer = ''.join(self._wbuf)
            self._wbuf = []
            self.write(buffer)

    def recv(self, size):
        """Receive message of a size from the socket."""
        while True:
            try:
                data = self._sock.recv(size)
                self.bytes_read += len(data)
                return data
            except socket.error as e:
                what = (e.args[0] not in errors.socket_errors_nonblocking
                        and e.args[0] not in errors.socket_error_eintr)
                if what:
                    raise

    class FauxSocket:
        """Faux socket with the minimal interface required by pypy."""
        def _reuse(self):
            pass

    _fileobject_uses_str_type = six.PY2 and isinstance(
        socket._fileobject(FauxSocket())._rbuf,
        six.string_types,
    )

    # FauxSocket is no longer needed
    del FauxSocket

    if not _fileobject_uses_str_type:  # noqa: C901  # FIXME

        def read(self, size=-1):
            """Read data from the socket to buffer."""
            # Use max, disallow tiny reads in a loop as they are very
            # inefficient.
            # We never leave read() with any leftover data from a new recv()
            # call in our internal buffer.
            rbufsize = max(self._rbufsize, self.default_bufsize)
            # Our use of StringIO rather than lists of string objects returned
            # by recv() minimizes memory usage and fragmentation that occurs
            # when rbufsize is large compared to the typical return value of
            # recv().
            buf = self._rbuf
            buf.seek(0, 2)  # seek end
            if size < 0:
                # Read until EOF
                # reset _rbuf.  we consume it via buf.
                self._rbuf = io.BytesIO()
                while True:
                    data = self.recv(rbufsize)
                    if not data:
                        break
                    buf.write(data)
                return buf.getvalue()
            else:
                # Read until size bytes or EOF seen, whichever comes first
                buf_len = buf.tell()
                if buf_len >= size:
                    # Already have size bytes in our buffer?  Extract and
                    # return.
                    buf.seek(0)
                    rv = buf.read(size)
                    self._rbuf = io.BytesIO()
                    self._rbuf.write(buf.read())
                    return rv

                # reset _rbuf.  we consume it via buf.
                self._rbuf = io.BytesIO()
                while True:
                    left = size - buf_len
                    # recv() will malloc the amount of memory given as its
                    # parameter even though it often returns much less data
                    # than that.  The returned data string is short lived
                    # as we copy it into a StringIO and free it.  This avoids
                    # fragmentation issues on many platforms.
                    data = self.recv(left)
                    if not data:
                        break
                    n = len(data)
                    if n == size and not buf_len:
                        # Shortcut.  Avoid buffer data copies when:
                        # - We have no data in our buffer.
                        # AND
                        # - Our call to recv returned exactly the
                        #   number of bytes we were asked to read.
                        return data
                    if n == left:
                        buf.write(data)
                        del data  # explicit free
                        break
                    assert n <= left, 'recv(%d) returned %d bytes' % (left, n)
                    buf.write(data)
                    buf_len += n
                    del data  # explicit free
                    # assert buf_len == buf.tell()
                return buf.getvalue()

        def readline(self, size=-1):
            """Read line from the socket to buffer."""
            buf = self._rbuf
            buf.seek(0, 2)  # seek end
            if buf.tell() > 0:
                # check if we already have it in our buffer
                buf.seek(0)
                bline = buf.readline(size)
                if bline.endswith('\n') or len(bline) == size:
                    self._rbuf = io.BytesIO()
                    self._rbuf.write(buf.read())
                    return bline
                del bline
            if size < 0:
                # Read until \n or EOF, whichever comes first
                if self._rbufsize <= 1:
                    # Speed up unbuffered case
                    buf.seek(0)
                    buffers = [buf.read()]
                    # reset _rbuf.  we consume it via buf.
                    self._rbuf = io.BytesIO()
                    data = None
                    recv = self.recv
                    while data != '\n':
                        data = recv(1)
                        if not data:
                            break
                        buffers.append(data)
                    return ''.join(buffers)

                buf.seek(0, 2)  # seek end
                # reset _rbuf.  we consume it via buf.
                self._rbuf = io.BytesIO()
                while True:
                    data = self.recv(self._rbufsize)
                    if not data:
                        break
                    nl = data.find('\n')
                    if nl >= 0:
                        nl += 1
                        buf.write(data[:nl])
                        self._rbuf.write(data[nl:])
                        del data
                        break
                    buf.write(data)
                return buf.getvalue()

            else:
                # Read until size bytes or \n or EOF seen, whichever comes
                # first
                buf.seek(0, 2)  # seek end
                buf_len = buf.tell()
                if buf_len >= size:
                    buf.seek(0)
                    rv = buf.read(size)
                    self._rbuf = io.BytesIO()
                    self._rbuf.write(buf.read())
                    return rv
                # reset _rbuf.  we consume it via buf.
                self._rbuf = io.BytesIO()
                while True:
                    data = self.recv(self._rbufsize)
                    if not data:
                        break
                    left = size - buf_len
                    # did we just receive a newline?
                    nl = data.find('\n', 0, left)
                    if nl >= 0:
                        nl += 1
                        # save the excess data to _rbuf
                        self._rbuf.write(data[nl:])
                        if buf_len:
                            buf.write(data[:nl])
                            break
                        else:
                            # Shortcut.  Avoid data copy through buf when
                            # returning a substring of our first recv().
                            return data[:nl]
                    n = len(data)
                    if n == size and not buf_len:
                        # Shortcut.  Avoid data copy through buf when
                        # returning exactly all of our first recv().
                        return data
                    if n >= left:
                        buf.write(data[:left])
                        self._rbuf.write(data[left:])
                        break
                    buf.write(data)
                    buf_len += n
                    # assert buf_len == buf.tell()
                return buf.getvalue()

        def has_data(self):
            """Return true if there is buffered data to read."""
            return bool(self._rbuf.getvalue())

    else:

        def read(self, size=-1):
            """Read data from the socket to buffer."""
            if size < 0:
                # Read until EOF
                buffers = [self._rbuf]
                self._rbuf = ''
                if self._rbufsize <= 1:
                    recv_size = self.default_bufsize
                else:
                    recv_size = self._rbufsize

                while True:
                    data = self.recv(recv_size)
                    if not data:
                        break
                    buffers.append(data)
                return ''.join(buffers)
            else:
                # Read until size bytes or EOF seen, whichever comes first
                data = self._rbuf
                buf_len = len(data)
                if buf_len >= size:
                    self._rbuf = data[size:]
                    return data[:size]
                buffers = []
                if data:
                    buffers.append(data)
                self._rbuf = ''
                while True:
                    left = size - buf_len
                    recv_size = max(self._rbufsize, left)
                    data = self.recv(recv_size)
                    if not data:
                        break
                    buffers.append(data)
                    n = len(data)
                    if n >= left:
                        self._rbuf = data[left:]
                        buffers[-1] = data[:left]
                        break
                    buf_len += n
                return ''.join(buffers)

        def readline(self, size=-1):
            """Read line from the socket to buffer."""
            data = self._rbuf
            if size < 0:
                # Read until \n or EOF, whichever comes first
                if self._rbufsize <= 1:
                    # Speed up unbuffered case
                    assert data == ''
                    buffers = []
                    while data != '\n':
                        data = self.recv(1)
                        if not data:
                            break
                        buffers.append(data)
                    return ''.join(buffers)
                nl = data.find('\n')
                if nl >= 0:
                    nl += 1
                    self._rbuf = data[nl:]
                    return data[:nl]
                buffers = []
                if data:
                    buffers.append(data)
                self._rbuf = ''
                while True:
                    data = self.recv(self._rbufsize)
                    if not data:
                        break
                    buffers.append(data)
                    nl = data.find('\n')
                    if nl >= 0:
                        nl += 1
                        self._rbuf = data[nl:]
                        buffers[-1] = data[:nl]
                        break
                return ''.join(buffers)
            else:
                # Read until size bytes or \n or EOF seen, whichever comes
                # first
                nl = data.find('\n', 0, size)
                if nl >= 0:
                    nl += 1
                    self._rbuf = data[nl:]
                    return data[:nl]
                buf_len = len(data)
                if buf_len >= size:
                    self._rbuf = data[size:]
                    return data[:size]
                buffers = []
                if data:
                    buffers.append(data)
                self._rbuf = ''
                while True:
                    data = self.recv(self._rbufsize)
                    if not data:
                        break
                    buffers.append(data)
                    left = size - buf_len
                    nl = data.find('\n', 0, left)
                    if nl >= 0:
                        nl += 1
                        self._rbuf = data[nl:]
                        buffers[-1] = data[:nl]
                        break
                    n = len(data)
                    if n >= left:
                        self._rbuf = data[left:]
                        buffers[-1] = data[:left]
                        break
                    buf_len += n
                return ''.join(buffers)

        def has_data(self):
            """Return true if there is buffered data to read."""
            return bool(self._rbuf)
Exemplo n.º 50
0
 def makefile(self, mode='rb', bufsize=-1):
     # type: (AnyStr, int) -> socket._fileobject
     return socket._fileobject(self, mode, bufsize)
Exemplo n.º 51
0
 def makefile(self, mode, bufsize=None):
     self.iCopies += 1
     return socket._fileobject(self.sock, mode, bufsize)  # pylint: disable=no-member
Exemplo n.º 52
0
import socket


class FauxSocket(object):
    """Faux socket with the minimal interface required by pypy"""
    def _reuse(self):
        pass


_fileobject_uses_str_type = isinstance(
    socket._fileobject(FauxSocket())._rbuf, basestring)
del FauxSocket  # this class is not longer required for anything.

from cheroot.compat import StringIO
from cheroot import errors


class makefile(socket._fileobject):
    """Faux file object attached to a socket object."""
    def __init__(self, *args, **kwargs):
        self.bytes_read = 0
        self.bytes_written = 0
        socket._fileobject.__init__(self, *args, **kwargs)

    def sendall(self, data):
        """Sendall for non-blocking sockets."""
        while data:
            try:
                bytes_sent = self.send(data)
                data = data[bytes_sent:]
            except socket.error as e:
Exemplo n.º 53
0
 def makefile(self, mode, bufsize=-1):
     return _fileobject(self.connection, mode, bufsize)
Exemplo n.º 54
0
 def makefile(self, mode='r', bufsize=-1):
     self.__makefile_refs += 1
     if PY2:
         return socket._fileobject(self, mode, bufsize, close=True)
     else:
         return socket.SocketIO(self, mode)
class AbstractHTTPHandler(BaseHandler):
    def __init__(self, debuglevel=0):
        self._debuglevel = debuglevel

    def set_http_debuglevel(self, level):
        self._debuglevel = level

    def do_request_(self, request):
        host = request.get_host()
        if not host:
            raise URLError('no host given')

        if request.has_data():  # POST
            data = request.get_data()
            if not request.has_header('Content-type'):
                request.add_unredirected_header(
                    'Content-type', 'application/x-www-form-urlencoded')
            if not request.has_header('Content-length'):
                request.add_unredirected_header('Content-length',
                                                '%d' % len(data))

        sel_host = host
        if request.has_proxy():
            scheme, sel = splittype(request.get_selector())
            sel_host, sel_path = splithost(sel)

        if not request.has_header('Host'):
            request.add_unredirected_header('Host', sel_host)
        for name, value in self.parent.addheaders:
            name = name.capitalize()
            if not request.has_header(name):
                request.add_unredirected_header(name, value)

        return request

    def do_open(self, http_class, req):
        """Return an addinfourl object for the request, using http_class.

        http_class must implement the HTTPConnection API from httplib.
        The addinfourl return value is a file-like object.  It also
        has methods and attributes including:
            - info(): return a mimetools.Message object for the headers
            - geturl(): return the original request URL
            - code: HTTP status code
        """
        host = req.get_host()
        if not host:
            raise URLError('no host given')

        h = http_class(host, timeout=req.timeout)  # will parse host:port
        h.set_debuglevel(self._debuglevel)

        headers = dict(req.headers)
        headers.update(req.unredirected_hdrs)
        # We want to make an HTTP/1.1 request, but the addinfourl
        # class isn't prepared to deal with a persistent connection.
        # It will try to read all remaining data from the socket,
        # which will block while the server waits for the next request.
        # So make sure the connection gets closed after the (only)
        # request.
        headers["Connection"] = "close"
        headers = dict((name.title(), val) for name, val in headers.items())

        if req._tunnel_host:
            tunnel_headers = {}
            proxy_auth_hdr = "Proxy-Authorization"
            if proxy_auth_hdr in headers:
                tunnel_headers[proxy_auth_hdr] = headers[proxy_auth_hdr]
                # Proxy-Authorization should not be sent to origin
                # server.
                del headers[proxy_auth_hdr]
            h.set_tunnel(req._tunnel_host, headers=tunnel_headers)

        try:
            h.request(req.get_method(), req.get_selector(), req.data, headers)
            try:
                r = h.getresponse(buffering=True)
            except TypeError:  #buffering kw not supported
                r = h.getresponse()
        except socket.error, err:  # XXX what error?
            raise URLError(err)

        # Pick apart the HTTPResponse object to get the addinfourl
        # object initialized properly.

        # Wrap the HTTPResponse object in socket's file object adapter
        # for Windows.  That adapter calls recv(), so delegate recv()
        # to read().  This weird wrapping allows the returned object to
        # have readline() and readlines() methods.

        # XXX It might be better to extract the read buffering code
        # out of socket._fileobject() and into a base class.

        r.recv = r.read
        fp = socket._fileobject(r, close=True)

        resp = addinfourl(fp, r.msg, req.get_full_url())
        resp.code = r.status
        resp.msg = r.reason
        return resp
Exemplo n.º 56
0
 def __init__(self, sock, client, args):
     self.connection = sock
     self.rfile = socket._fileobject(self.connection, "rb", -1)
     self.wfile = socket._fileobject(self.connection, "wb", 0)
     self.client_address = client
     self.args = args
Exemplo n.º 57
0
 def makefile(self, mode, bufsize=-1):
     self._makefile_refs += 1
     return _fileobject(self, mode, bufsize, close=True)
 def makefile(self, mode='r', bufsize=-1):
     return socket._fileobject(self, mode, bufsize)
Exemplo n.º 59
0
 def makefile(self, mode, bufsize):
     return socket._fileobject(self._con, mode, bufsize)
Exemplo n.º 60
0
    def makefile(self, mode='r', bufsize=-1):
        """Ouch.  Need to make and return a file-like object that
        works with the SSL connection."""

        self._makefile_refs += 1
        return _fileobject(self, mode, bufsize)