Exemplo n.º 1
0
 def worker_info(self, worker, info):
     info['sockets'] = sockets = []
     for server in worker.servers.get(self.name, ()):
         address = format_address(server.address)
         sockets.append({
             'address': format_address(server.address),
             'read_timeout': server.timeout,
             'concurrent_connections': server.concurrent_connections,
             'received_connections': server.received})
Exemplo n.º 2
0
 def worker_info(self, worker, info):
     info['sockets'] = sockets = []
     for server in worker.servers.get(self.name, ()):
         address = format_address(server.address)
         sockets.append({
             'address': format_address(server.address),
             'read_timeout': server.timeout,
             'concurrent_connections': server.concurrent_connections,
             'received_connections': server.received
         })
Exemplo n.º 3
0
    async def create_endpoint(self, **kw):
        """create the server endpoint.

        :return: a :class:`~asyncio.Future` called back when the server is
            serving the socket.
        """
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            try:
                transports = []
                loop = self._loop
                if sockets:
                    for sock in sockets:
                        transport, _ = await loop.create_datagram_endpoint(
                            self.create_protocol, sock=sock)
                        transports.append(transport)
                else:
                    transport, _ = await loop.create_datagram_endpoint(
                        self.create_protocol, local_addr=address)
                    transports.append(transport)
                self._transports = transports
                self._started = loop.time()
                for transport in self._transports:
                    address = transport.get_extra_info('sockname')
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.logger.exception('Error while starting UDP server')
                self.fire_event('start', exc=exc)
                self.fire_event('stop')
Exemplo n.º 4
0
    async def create_endpoint(self, **kw):
        """create the server endpoint.

        :return: a :class:`~asyncio.Future` called back when the server is
            serving the socket.
        """
        if hasattr(self, "_params"):
            address = self._params["address"]
            sockets = self._params["sockets"]
            del self._params
            try:
                transports = []
                loop = self._loop
                if sockets:
                    for sock in sockets:
                        transport, _ = await loop.create_datagram_endpoint(self.create_protocol, sock=sock)
                        transports.append(transport)
                else:
                    transport, _ = await loop.create_datagram_endpoint(self.create_protocol, local_addr=address)
                    transports.append(transport)
                self._transports = transports
                self._started = loop.time()
                for transport in self._transports:
                    address = transport.get_extra_info("sockname")
                    self.logger.info("%s serving on %s", self._name, format_address(address))
                self.fire_event("start")
            except Exception as exc:
                self.logger.exception("Error while starting UDP server")
                self.fire_event("start", exc=exc)
                self.fire_event("stop")
Exemplo n.º 5
0
    async def start_serving(self, backlog=100, sslcontext=None):
        """Start serving.

        :param backlog: Number of maximum connections
        :param sslcontext: optional SSLContext object.
        :return: a :class:`.Future` called back when the server is
            serving the socket.
        """
        assert not self._server
        if hasattr(self, "_params"):
            address = self._params["address"]
            sockets = self._params["sockets"]
            del self._params
            create_server = self._loop.create_server
            if sockets:
                server = None
                for sock in sockets:
                    srv = await create_server(self.create_protocol, sock=sock, backlog=backlog, ssl=sslcontext)
                    if server:
                        server.sockets.extend(srv.sockets)
                    else:
                        server = srv
            else:
                if isinstance(address, tuple):
                    server = await create_server(
                        self.create_protocol, host=address[0], port=address[1], backlog=backlog, ssl=sslcontext
                    )
                else:
                    raise NotImplementedError
            self._server = server
            self._started = self._loop.time()
            for sock in server.sockets:
                address = sock.getsockname()
                self.logger.info("%s serving on %s", self._name, format_address(address))
            self._loop.call_soon(self.fire_event, "start")
Exemplo n.º 6
0
    def create_endpoint(self, **kw):
        '''create the server endpoint.

        :return: a :class:`~asyncio.Future` called back when the server is
            serving the socket.
        '''
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            try:
                transports = []
                if sockets:
                    for transport in sockets:
                        proto = self.create_protocol()
                        transports.append(transport(self._loop, proto))
                else:
                    loop = self._loop
                    transport, _ = yield from loop.create_datagram_endpoint(
                        self.protocol_factory, local_addr=address)
                    transports.append(transport)
                self._transports = transports
                self._started = self._loop.time()
                for transport in self._transports:
                    address = transport.get_extra_info('sockname')
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.logger.exception('Error while starting UDP server')
                self.fire_event('start', exc=exc)
                self.fire_event('stop')
Exemplo n.º 7
0
 def test_start_serving_ipv6(self):
     loop = get_event_loop()
     sockets = yield loop.start_serving(Protocol,'::1', 0)
     self.assertEqual(len(sockets), 1)
     sock = sockets[0]
     self.assertEqual(sock.family, socket.AF_INET6)
     address = sock.getsockname()
     faddress = format_address(address)
     self.assertEqual(faddress, '[::1]:%s' % address[1])
     loop.stop_serving(sock)
     self.assertTrue(is_socket_closed(sock))
Exemplo n.º 8
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {"uptime_in_seconds": up, "sockets": sockets, "max_requests": self._max_requests}
     clients = {"requests_processed": self._requests_processed}
     if self._transports:
         for transport in self._transports:
             sock = transport.get_extra_info("socket")
             if sock:
                 sockets.append({"address": format_address(sock.getsockname())})
     return {"server": server, "clients": clients}
Exemplo n.º 9
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {'uptime_in_seconds': up,
               'sockets': sockets,
               'max_requests': self._max_requests}
     clients = {'requests_processed': self._requests_processed}
     if self._transports:
         for transport in self._transports:
             sockets.append({
                 'address': format_address(transport._sock.getsockname())})
     return {'server': server,
             'clients': clients}
Exemplo n.º 10
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {
         "pulsar_version": pulsar.__version__,
         "python_version": sys.version,
         "uptime_in_seconds": up,
         "sockets": sockets,
         "max_requests": self._max_requests,
     }
     clients = {"requests_processed": self._requests_processed}
     if self._transports:
         for transport in self._transports:
             sockets.append({"address": format_address(transport._sock.getsockname())})
     return {"server": server, "clients": clients}
Exemplo n.º 11
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {'uptime_in_seconds': up,
               'sockets': sockets,
               'max_requests': self._max_requests,
               'keep_alive': self._keep_alive}
     clients = {'processed_clients': self._sessions,
                'connected_clients': len(self._concurrent_connections),
                'requests_processed': self._requests_processed}
     if self._server:
         for sock in self._server.sockets:
             sockets.append({
                 'address': format_address(sock.getsockname())})
     return {'server': server,
             'clients': clients}
Exemplo n.º 12
0
 def info(self):
     sockets = []
     up = int(self._loop.time() - self._started) if self._started else 0
     server = {
         "uptime_in_seconds": up,
         "sockets": sockets,
         "max_requests": self._max_requests,
         "keep_alive": self._keep_alive,
     }
     clients = {
         "processed_clients": self._sessions,
         "connected_clients": len(self._concurrent_connections),
         "requests_processed": self._requests_processed,
     }
     if self._server:
         for sock in self._server.sockets:
             sockets.append({"address": format_address(sock.getsockname())})
     return {"server": server, "clients": clients}
Exemplo n.º 13
0
    def start_serving(self, backlog=100, sslcontext=None):
        '''Start serving.

        :param backlog: Number of maximum connections
        :param sslcontext: optional SSLContext object.
        :return: a :class:`.Future` called back when the server is
            serving the socket.'''
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            create_server = self._loop.create_server
            try:
                if sockets:
                    server = None
                    for sock in sockets:
                        srv = yield From(
                            create_server(self.create_protocol,
                                          sock=sock,
                                          backlog=backlog,
                                          ssl=sslcontext))
                        if server:
                            server.sockets.extend(srv.sockets)
                        else:
                            server = srv
                else:
                    if isinstance(address, tuple):
                        server = yield From(
                            create_server(self.create_protocol,
                                          host=address[0],
                                          port=address[1],
                                          backlog=backlog,
                                          ssl=sslcontext))
                    else:
                        raise NotImplementedError
                self._server = server
                self._started = self._loop.time()
                for sock in server.sockets:
                    address = sock.getsockname()
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.fire_event('start', exc=exc)
Exemplo n.º 14
0
    def start_serving(self, backlog=100, sslcontext=None):
        '''Start serving.

        :param backlog: Number of maximum connections
        :param sslcontext: optional SSLContext object.
        :return: a :class:`.Future` called back when the server is
            serving the socket.'''
        assert not self._server
        if hasattr(self, '_params'):
            address = self._params['address']
            sockets = self._params['sockets']
            del self._params
            create_server = self._loop.create_server
            try:
                if sockets:
                    server = None
                    for sock in sockets:
                        srv = yield from create_server(self.create_protocol,
                                                       sock=sock,
                                                       backlog=backlog,
                                                       ssl=sslcontext)
                        if server:
                            server.sockets.extend(srv.sockets)
                        else:
                            server = srv
                else:
                    if isinstance(address, tuple):
                        server = yield from create_server(self.create_protocol,
                                                          host=address[0],
                                                          port=address[1],
                                                          backlog=backlog,
                                                          ssl=sslcontext)
                    else:
                        raise NotImplementedError
                self._server = server
                self._started = self._loop.time()
                for sock in server.sockets:
                    address = sock.getsockname()
                    self.logger.info('%s serving on %s', self._name,
                                     format_address(address))
                self.fire_event('start')
            except Exception as exc:
                self.fire_event('start', exc=exc)
Exemplo n.º 15
0
 def __repr__(self):
     return '%s %s' % (self.__class__.__name__, format_address(
         self.address))
Exemplo n.º 16
0
def wsgi_environ(stream,
                 address,
                 client_address,
                 headers,
                 server_software=None,
                 https=False,
                 extra=None):
    protocol = stream.protocol()
    parser = stream.parser
    request_headers = stream.headers
    raw_uri = parser.get_url()
    request_uri = urlparse(raw_uri)
    #
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2
    # If Request-URI is an absoluteURI, the host is part of the Request-URI.
    # Any Host header field value in the request MUST be ignored
    if request_uri.scheme:
        url_scheme = request_uri.scheme
        host = request_uri.netloc
    else:
        url_scheme = 'https' if https else 'http'
        host = None
    #
    environ = {
        "wsgi.input": stream,
        "wsgi.errors": sys.stderr,
        "wsgi.version": (1, 0),
        "wsgi.run_once": False,
        "wsgi.multithread": False,
        "wsgi.multiprocess": False,
        "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE,
        "REQUEST_METHOD": native_str(parser.get_method()),
        "QUERY_STRING": parser.get_query_string(),
        "RAW_URI": raw_uri,
        "SERVER_PROTOCOL": protocol,
        "CONTENT_TYPE": ''
    }
    forward = client_address
    script_name = os.environ.get("SCRIPT_NAME", "")
    for header, value in request_headers:
        header = header.lower()
        if header in HOP_HEADERS:
            headers[header] = value
        if header == 'x-forwarded-for':
            forward = value
        elif header == "x-forwarded-protocol" and value == "ssl":
            url_scheme = "https"
        elif header == "x-forwarded-ssl" and value == "on":
            url_scheme = "https"
        elif header == "host" and not host:
            host = value
        elif header == "script_name":
            script_name = value
        elif header == "content-type":
            environ['CONTENT_TYPE'] = value
            continue
        elif header == "content-length":
            environ['CONTENT_LENGTH'] = value
            continue
        key = 'HTTP_' + header.upper().replace('-', '_')
        environ[key] = value
    environ['wsgi.url_scheme'] = url_scheme
    if url_scheme == 'https':
        environ['HTTPS'] = 'on'
    if is_string(forward):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()
        remote = forward.split(":")
        if len(remote) < 2:
            remote.append('80')
    else:
        remote = forward
    environ['REMOTE_ADDR'] = remote[0]
    environ['REMOTE_PORT'] = str(remote[1])
    if not host and protocol == 'HTTP/1.0':
        host = format_address(address)
    if host:
        host = host_and_port_default(url_scheme, host)
        environ['SERVER_NAME'] = socket.getfqdn(host[0])
        environ['SERVER_PORT'] = host[1]
    path_info = request_uri.path
    if path_info is not None:
        if script_name:
            path_info = path_info.split(script_name, 1)[1]
        environ['PATH_INFO'] = unquote(path_info)
    environ['SCRIPT_NAME'] = script_name
    if extra:
        environ.update(extra)
    return environ
Exemplo n.º 17
0
 def __repr__(self):
     return '%s %s' % (self.__class__.__name__,
                       format_address(self.address))
Exemplo n.º 18
0
 def _got_sockets(self, sockets):
     self._sock = sockets[0]
     self.logger.info('%s serving on %s', self._name,
                      format_address(self.address))
     return self
Exemplo n.º 19
0
 def test_format_address(self):
     self.assertRaises(ValueError, format_address, (1, ))
     self.assertRaises(ValueError, format_address, (1, 2, 3))
     self.assertRaises(ValueError, format_address, (1, 2, 3, 4, 5))
     self.assertEqual(format_address(1), '1')
Exemplo n.º 20
0
 def test_format_address(self):
     self.assertRaises(ValueError, format_address, (1,))
     self.assertRaises(ValueError, format_address, (1, 2, 3))
     self.assertRaises(ValueError, format_address, (1, 2, 3, 4, 5))
     self.assertEqual(format_address(1), '1')
Exemplo n.º 21
0
def wsgi_environ(stream, address, client_address, headers,
                 server_software=None, https=False, extra=None):
    protocol = stream.protocol()
    parser = stream.parser
    request_headers = stream.headers
    raw_uri = parser.get_url()
    request_uri = urlparse(raw_uri)
    #
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2
    # If Request-URI is an absoluteURI, the host is part of the Request-URI.
    # Any Host header field value in the request MUST be ignored
    if request_uri.scheme:
        url_scheme = request_uri.scheme
        host = request_uri.netloc
    else:
        url_scheme = 'https' if https else 'http'
        host = None
    #
    environ = {"wsgi.input": stream,
               "wsgi.errors": sys.stderr,
               "wsgi.version": (1, 0),
               "wsgi.run_once": False,
               "wsgi.multithread": False,
               "wsgi.multiprocess": False,
               "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE,
               "REQUEST_METHOD": native_str(parser.get_method()),
               "QUERY_STRING": parser.get_query_string(),
               "RAW_URI": raw_uri,
               "SERVER_PROTOCOL": protocol,
               "CONTENT_TYPE": ''}
    forward = client_address
    script_name = os.environ.get("SCRIPT_NAME", "")
    for header, value in request_headers:
        header = header.lower()
        if header in HOP_HEADERS:
            headers[header] = value
        if header == 'x-forwarded-for':
            forward = value
        elif header == "x-forwarded-protocol" and value == "ssl":
            url_scheme = "https"
        elif header == "x-forwarded-ssl" and value == "on":
            url_scheme = "https"
        elif header == "host" and not host:
            host = value
        elif header == "script_name":
            script_name = value
        elif header == "content-type":
            environ['CONTENT_TYPE'] = value
            continue
        elif header == "content-length":
            environ['CONTENT_LENGTH'] = value
            continue
        key = 'HTTP_' + header.upper().replace('-', '_')
        environ[key] = value
    environ['wsgi.url_scheme'] = url_scheme
    if url_scheme == 'https':
        environ['HTTPS'] = 'on'
    if is_string(forward):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()
        remote = forward.split(":")
        if len(remote) < 2:
            remote.append('80')
    else:
        remote = forward
    environ['REMOTE_ADDR'] = remote[0]
    environ['REMOTE_PORT'] = str(remote[1])
    if not host and protocol == 'HTTP/1.0':
        host = format_address(address)
    if host:
        host = host_and_port_default(url_scheme, host)
        environ['SERVER_NAME'] = socket.getfqdn(host[0])
        environ['SERVER_PORT'] = host[1]
    path_info = parser.get_path()
    if path_info is not None:
        if script_name:
            path_info = path_info.split(script_name, 1)[1]
        environ['PATH_INFO'] = unquote(path_info)
    environ['SCRIPT_NAME'] = script_name
    if extra:
        environ.update(extra)
    return environ
Exemplo n.º 22
0
 def _got_sockets(self, sockets):
     self._sock = sockets[0]
     self.logger.info('%s serving on %s', self._name,
                      format_address(self.address))
     return self
Exemplo n.º 23
0
def wsgi_environ(
    stream, parser, request_headers, address, client_address, headers, server_software=None, https=False, extra=None
):
    """Build the WSGI Environment dictionary

    :param stream: a wsgi stream object
    :param parser: pulsar HTTP parser
    :param request_headers: headers of request
    :param address: server address
    :param client_address: client address
    :param headers: container for response headers
    """
    protocol = http_protocol(parser)
    raw_uri = parser.get_url()
    request_uri = urlparse(raw_uri)
    #
    # http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.2
    # If Request-URI is an absoluteURI, the host is part of the Request-URI.
    # Any Host header field value in the request MUST be ignored
    if request_uri.scheme:
        url_scheme = request_uri.scheme
        host = request_uri.netloc
    else:
        url_scheme = "https" if https else "http"
        host = None
    #
    environ = {
        "wsgi.input": stream,
        "wsgi.errors": sys.stderr,
        "wsgi.version": (1, 0),
        "wsgi.run_once": False,
        "wsgi.multithread": False,
        "wsgi.multiprocess": False,
        "SERVER_SOFTWARE": server_software or pulsar.SERVER_SOFTWARE,
        "REQUEST_METHOD": native_str(parser.get_method()),
        "QUERY_STRING": parser.get_query_string(),
        "RAW_URI": raw_uri,
        "SERVER_PROTOCOL": protocol,
        "CONTENT_TYPE": "",
    }
    forward = client_address
    script_name = os.environ.get("SCRIPT_NAME", "")
    for header, value in request_headers:
        header = header.lower()
        if header in HOP_HEADERS:
            headers[header] = value
        if header == "x-forwarded-for":
            forward = value
        elif header == "x-forwarded-protocol" and value == "ssl":
            url_scheme = "https"
        elif header == "x-forwarded-ssl" and value == "on":
            url_scheme = "https"
        elif header == "host" and not host:
            host = value
        elif header == "script_name":
            script_name = value
        elif header == "content-type":
            environ["CONTENT_TYPE"] = value
            continue
        elif header == "content-length":
            environ["CONTENT_LENGTH"] = value
            continue
        key = "HTTP_" + header.upper().replace("-", "_")
        environ[key] = value
    environ["wsgi.url_scheme"] = url_scheme
    if url_scheme == "https":
        environ["HTTPS"] = "on"
    if isinstance(forward, str):
        # we only took the last one
        # http://en.wikipedia.org/wiki/X-Forwarded-For
        if forward.find(",") >= 0:
            forward = forward.rsplit(",", 1)[1].strip()
        remote = forward.split(":")
        if len(remote) < 2:
            remote.append("80")
    else:
        remote = forward
    environ["REMOTE_ADDR"] = remote[0]
    environ["REMOTE_PORT"] = str(remote[1])
    if not host and protocol == "HTTP/1.0":
        host = format_address(address)
    if host:
        h = host_and_port_default(url_scheme, host)
        environ["SERVER_NAME"] = socket.getfqdn(h[0]) if h[0] else "0.0.0.0"
        environ["SERVER_PORT"] = h[1]
    path_info = request_uri.path
    if path_info is not None:
        if script_name:
            path_info = path_info.split(script_name, 1)[1]
        environ["PATH_INFO"] = unquote(path_info)
    environ["SCRIPT_NAME"] = script_name
    if extra:
        environ.update(extra)
    return environ