Download Install Tutorial Docs FAQ Tools WikiLicense Team IRC Planet Involvement Shop Book

Changeset 1528

Show
Ignore:
Timestamp:
12/11/06 20:17:21
Author:
fumanchu
Message:

Changes to socket_host:

  1. wsgiserver now treats a host of "" as an alias for INADDR_ANY. The getaddrinfo call now passes host=None and sets AI_PASSIVE in this case.
  2. Server.httpserver_from_self doesn't change an empty host ("") to localhost anymore.
  3. The test suite has a new --host=<name or IP> flag.
  4. The webtest module now allows WebCase?.HOST to be "", and will connect on '127.0.0.1' if so.
  5. Lots of comments throughout to explain that the server's treatment of socket_host="" (all IPs) is different than the client's (pick any IP).
  6. Hopefully, this will fix #619 (long delay on startup).
Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/cherrypy/__init__.py

    r1469 r1528  
    220220                host = server.socket_host 
    221221                if not host: 
     222                    # The empty string signifies INADDR_ANY. 
     223                    # Look up the host name, which should be 
     224                    # the safest thing to spit out in a URL. 
    222225                    import socket 
    223226                    host = socket.gethostname() 
  • trunk/cherrypy/_cpserver.py

    r1460 r1528  
    8484        host = self.socket_host 
    8585        port = self.socket_port 
    86         if not host: 
    87             host = 'localhost' 
    8886        return httpserver, (host, port) 
    8987     
     
    182180    """Raise an error if the given port is not free on the given host.""" 
    183181    if not host: 
     182        # The empty string signifies INADDR_ANY, 
     183        # which should respond on localhost. 
    184184        host = 'localhost' 
    185185    port = int(port) 
     
    209209    """Wait for the specified port to become free (drop requests).""" 
    210210    if not host: 
     211        # The empty string signifies INADDR_ANY, 
     212        # which should respond on localhost. 
    211213        host = 'localhost' 
    212214     
     
    227229    """Wait for the specified port to become active (receive requests).""" 
    228230    if not host: 
     231        # The empty string signifies INADDR_ANY, 
     232        # which should respond on localhost. 
    229233        host = 'localhost' 
    230234     
  • trunk/cherrypy/test/helper.py

    r1403 r1528  
    4242        else: 
    4343            port = ":%s" % self.PORT 
    44         return "%s://%s%s%s" % (self.scheme, self.HOST, port, 
     44         
     45        host = self.HOST 
     46        if not host: 
     47            # The empty string signifies INADDR_ANY, 
     48            # which should respond on localhost. 
     49            host = "127.0.0.1" 
     50         
     51        return "%s://%s%s%s" % (self.scheme, host, port, 
    4552                                self.script_name.rstrip("/")) 
    4653     
  • trunk/cherrypy/test/test.py

    r1467 r1528  
    2323     
    2424    def __init__(self, tests=None, server=None, protocol="HTTP/1.1", 
    25                  port=8000, scheme="http", interactive=True): 
     25                 port=8000, scheme="http", interactive=True, host='127.0.0.1'): 
    2626        """Constructor to populate the TestHarness instance. 
    2727         
     
    3232        self.protocol = protocol 
    3333        self.port = port 
     34        self.host = host 
    3435        self.scheme = scheme 
    3536        self.interactive = interactive 
     
    5051        if isinstance(conf, basestring): 
    5152            conf = cherrypy.config._Parser().dict_from_file(conf) 
    52         baseconf = {'server.socket_host': '127.0.0.1'
     53        baseconf = {'server.socket_host': self.host
    5354                    'server.socket_port': self.port, 
    5455                    'server.thread_pool': 10, 
     
    7273        from cherrypy.test import helper, webtest 
    7374        webtest.WebCase.PORT = self.port 
     75        webtest.WebCase.HOST = self.host 
    7476        webtest.WebCase.harness = self 
    7577        helper.CPWebCase.scheme = self.scheme 
     
    9193    protocol = "HTTP/1.1" 
    9294    port = 8080 
     95    host = '127.0.0.1' 
    9396    cover = False 
    9497    profile = False 
     
    111114        longopts = ['cover', 'profile', 'validate', 'conquer', 'dumb', 
    112115                    '1.0', 'ssl', 'help', 
    113                     'basedir=', 'port=', 'server='
     116                    'basedir=', 'port=', 'server=', 'host='
    114117        longopts.extend(self.available_tests) 
    115118        try: 
     
    144147            elif o == "--port": 
    145148                self.port = int(a) 
     149            elif o == "--host": 
     150                self.host = a 
    146151            elif o == "--server": 
    147152                if a in self.available_servers: 
     
    170175        print """CherryPy Test Program 
    171176    Usage: 
    172         test.py --server=* --port=%s --1.0 --cover --basedir=path --profile --validate --conquer --dumb --tests** 
    173          
    174     """ % self.__class__.port 
     177        test.py --server=* --host=%s --port=%s --1.0 --cover --basedir=path --profile --validate --conquer --dumb --tests** 
     178         
     179    """ % (self.__class__.host, self.__class__.port) 
    175180        print '    * servers:' 
    176181        for name, val in self.available_servers.iteritems(): 
     
    182187        print """ 
    183188     
     189    --host=<name or IP addr>: use a host other than the default (%s). 
     190        Not yet available with mod_python servers. 
    184191    --port=<int>: use a port other than the default (%s) 
    185192    --1.0: use HTTP/1.0 servers instead of default HTTP/1.1 
     
    192199    --conquer: use wsgiconq (which uses pyconquer) to trace calls. 
    193200    --dumb: turn off the interactive output features. 
    194     """ % self.__class__.port 
     201    """ % (self.__class__.host, self.__class__.port) 
    195202         
    196203        print '    ** tests:' 
     
    319326        else: 
    320327            h = TestHarness(self.tests, self.server, self.protocol, 
    321                             self.port, self.scheme, self.interactive) 
     328                            self.port, self.scheme, self.interactive, 
     329                            self.host) 
    322330         
    323331        success = h.run(conf) 
  • trunk/cherrypy/test/test_core.py

    r1433 r1528  
    297297            hMap['content-length'] = 18 
    298298            hMap['server'] = 'CherryPy headertest' 
    299             hMap['location'] = ('%s://127.0.0.1:%s/headers/' 
    300                                 % (cherrypy.request.remote.port, 
     299            hMap['location'] = ('%s://%s:%s/headers/' 
     300                                % (cherrypy.request.local.ip, 
     301                                   cherrypy.request.local.port, 
    301302                                   cherrypy.request.scheme)) 
    302303             
     
    481482         
    482483        data = open(log_access_file, "rb").readlines() 
    483         self.assertEqual(data[0][:15], '127.0.0.1 - - [') 
     484         
     485        host = self.HOST 
     486        if not host: 
     487            # The empty string signifies INADDR_ANY, 
     488            # which should respond on localhost. 
     489            host = "127.0.0.1" 
     490        intro = '%s - - [' % host 
     491         
     492        if not data[0].startswith(intro): 
     493            self.fail("%r doesn't start with %r" % (data[0], intro)) 
    484494        haslength = False 
    485495        for k, v in self.headers: 
     
    496506                self.fail(line) 
    497507         
    498         self.assertEqual(data[-1][:15], '127.0.0.1 - - [') 
     508        if not data[-1].startswith(intro): 
     509            self.fail("%r doesn't start with %r" % (data[-1], intro)) 
    499510        haslength = False 
    500511        for k, v in self.headers: 
     
    952963        lines256 = "x" * 248 
    953964        self.getPage("/", 
    954                      headers=[('Host', '127.0.0.1:%s' % self.PORT), 
     965                     headers=[('Host', '%s:%s' % (self.HOST, self.PORT)), 
    955966                              ('From', lines256)]) 
    956967         
  • trunk/cherrypy/test/test_proxy.py

    r1369 r1528  
    9191            elif self.scheme == "https" and self.PORT != 443: 
    9292                port = ":%s" % self.PORT 
     93            host = self.HOST 
     94            if host == '': 
     95                import socket 
     96                host = socket.gethostname() 
    9397            self.assertEqual(cherrypy.url("/this/new/page", script_name=sn), 
    9498                             "%s://%s%s%s/this/new/page" 
    95                              % (self.scheme, self.HOST, port, sn)) 
     99                             % (self.scheme, host, port, sn)) 
    96100         
    97101        # Test trailing slash (see http://www.cherrypy.org/ticket/562). 
  • trunk/cherrypy/test/test_xmlrpc.py

    r1431 r1528  
    9696        # load the appropriate xmlrpc proxy 
    9797        if getattr(self.harness, "scheme", "http") == "https": 
    98             url = 'https://localhost:%s/xmlrpc/' % self.PORT 
     98            url = 'https://%s:%s/xmlrpc/' % (self.HOST, self.PORT) 
    9999            proxy = xmlrpclib.ServerProxy(url, transport=HTTPSTransport()) 
    100100        else: 
    101             url = 'http://localhost:%s/xmlrpc/' % self.PORT 
     101            url = 'http://%s:%s/xmlrpc/' % (self.HOST, self.PORT) 
    102102            proxy = xmlrpclib.ServerProxy(url) 
    103103         
  • trunk/cherrypy/test/webtest.py

    r1474 r1528  
    164164         
    165165        if on: 
    166             self.HTTP_CONN = cls(self.HOST, self.PORT) 
     166            host = self.HOST 
     167            if not host: 
     168                # The empty string signifies INADDR_ANY, 
     169                # which should respond on localhost. 
     170                host = "127.0.0.1" 
     171            self.HTTP_CONN = cls(host, self.PORT) 
    167172            # Automatically re-connect? 
    168173            self.HTTP_CONN.auto_open = auto_open 
     
    182187         
    183188        self.url = url 
    184         result = openURL(url, headers, method, body, self.HOST, self.PORT, 
     189        host = self.HOST 
     190        if not host: 
     191            # The empty string signifies INADDR_ANY, 
     192            # which should respond on localhost. 
     193            host = "127.0.0.1" 
     194        result = openURL(url, headers, method, body, host, self.PORT, 
    185195                         self.HTTP_CONN, protocol or self.PROTOCOL) 
    186196        self.status, self.headers, self.body = result 
  • trunk/cherrypy/wsgiserver.py

    r1478 r1528  
    665665            # Get the correct address family for our host (allows IPv6 addresses) 
    666666            host, port = self.bind_addr 
     667            flags = 0 
     668            if host == '': 
     669                # Despite the socket module docs, using '' does not 
     670                # allow AI_PASSIVE to work. Passing None instead 
     671                # returns '0.0.0.0' like we want. 
     672                host = None 
     673                flags = socket.AI_PASSIVE 
    667674            try: 
    668675                info = socket.getaddrinfo(host, port, socket.AF_UNSPEC, 
    669                                           socket.SOCK_STREAM
     676                                          socket.SOCK_STREAM, 0, flags
    670677            except socket.gaierror: 
    671678                # Probably a DNS issue. Assume IPv4. 
     
    756763                        raise 
    757764                else: 
     765                    # Note that we're explicitly NOT using AI_PASSIVE, 
     766                    # here, because we want an actual IP to touch. 
     767                    # localhost won't work if we've bound to a public IP, 
     768                    # but it would if we bound to INADDR_ANY via host = ''. 
    758769                    for res in socket.getaddrinfo(host, port, socket.AF_UNSPEC, 
    759770                                                  socket.SOCK_STREAM): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets