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

Changeset 1201

Show
Ignore:
Timestamp:
07/10/06 23:33:48
Author:
fumanchu
Message:

Various speedups, the only notable one being that environments are now always flattened as soon as possible.

Files:

Legend:

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

    r1200 r1201  
    2828            self.callbacks[point].append(wrapper) 
    2929     
    30     def run(self, point, *args, **kwargs): 
     30    def run(self, point): 
    3131        """Execute all registered callbacks for the given point.""" 
    3232        failsafe = point in self.failsafe 
     
    4040            if failsafe: 
    4141                try: 
    42                     callback(*args, **kwargs
     42                    callback(
    4343                except (KeyboardInterrupt, SystemExit): 
    4444                    raise 
     
    4646                    cherrypy.log(traceback=True) 
    4747            else: 
    48                 callback(*args, **kwargs
     48                callback(
    4949 
    5050 
     
    267267         
    268268        # Process the headers into self.headers 
     269        headers = self.headers 
    269270        for name, value in self.header_list: 
     271            # Call title() now (and use dict.__method__(headers)) 
     272            # so title doesn't have to be called twice. 
     273            name = name.title() 
    270274            value = value.strip() 
    271275             
     
    273277            # only Konqueror does that), only the last one will remain in headers 
    274278            # (but they will be correctly stored in request.simple_cookie). 
    275             self.headers[name] = http.decode_TEXT(value) 
     279            if "=?" in value: 
     280                dict.__setitem__(headers, name, http.decode_TEXT(value)) 
     281            else: 
     282                dict.__setitem__(headers, name, value) 
    276283             
    277284            # Handle cookies differently because on Konqueror, multiple 
    278285            # cookies come on different lines with the same key 
    279             if name.title() == 'Cookie': 
     286            if name == 'Cookie': 
    280287                self.simple_cookie.load(value) 
    281288         
    282         host = self.headers.get('Host') 
    283         if host is None: 
     289        if not dict.__contains__(headers, 'Host'): 
    284290            # All Internet-based HTTP/1.1 servers MUST respond with a 400 
    285291            # (Bad Request) status code to any HTTP/1.1 request message 
     
    288294                msg = "HTTP/1.1 requires a 'Host' request header." 
    289295                raise cherrypy.HTTPError(400, msg) 
     296        host = dict.__getitem__(headers, 'Host') 
    290297        if not host: 
    291298            host = cherrypy.config.get('server.socket_host', '') 
     
    306313                break 
    307314             
    308             env = nodeconf.get("environment") 
    309             if env: 
    310                 d = cherrypy.config.environments[env].get("dispatch") 
    311                 if d: 
    312                     dispatch = d 
    313                     break 
    314              
    315315            lastslash = trail.rfind("/") 
    316316            if lastslash == -1: 
     
    327327        """Populate self.toolmap and set up each tool.""" 
    328328        # Get all 'tools.*' config entries as a {toolname: {k: v}} dict. 
    329         self.toolmap = {} 
    330         for k, v in self.config.iteritems(): 
    331             atoms = k.split(".") 
    332             namespace = atoms.pop(0) 
     329        self.toolmap = tm = {} 
     330        reqconf = self.config 
     331        for k in reqconf: 
     332            atoms = k.split(".", 2) 
     333            namespace = atoms[0] 
    333334            if namespace == "tools": 
    334                 toolname = atoms.pop(0) 
    335                 bucket = self.toolmap.setdefault(toolname, {}) 
    336                 bucket[".".join(atoms)] = v 
     335                toolname = atoms[1] 
     336                bucket = tm.setdefault(toolname, {}) 
     337                bucket[atoms[2]] = reqconf[k] 
    337338            elif namespace == "hooks": 
    338339                # Attach bare hooks declared in config. 
    339                 hookpoint = atoms[0] 
     340                hookpoint = atoms[1] 
     341                v = reqconf[k] 
    340342                if isinstance(v, basestring): 
    341343                    v = cherrypy.lib.attributes(v) 
     
    343345         
    344346        # Run tool._setup(conf) for each tool in the new toolmap. 
    345         for toolname, conf in self.toolmap.iteritems(): 
    346             if conf.get("on", False): 
    347                 tool = getattr(cherrypy.tools, toolname) 
     347        tools = cherrypy.tools 
     348        for toolname in tm: 
     349            if tm[toolname].get("on", False): 
     350                tool = getattr(tools, toolname) 
    348351                tool._setup() 
    349352     
     
    437440         
    438441        # Get config for the root object/path. 
     442        environments = cherrypy.config.environments 
    439443        curpath = "" 
    440         nodeconf = getattr(root, "_cp_config", {}).copy() 
    441         nodeconf.update(app.conf.get("/", {})) 
     444        nodeconf = {} 
     445        if hasattr(root, "_cp_config"): 
     446            nodeconf.update(root._cp_config) 
     447        if 'environment' in nodeconf: 
     448            env = environments[nodeconf['environment']] 
     449            for k in env: 
     450                if k not in nodeconf: 
     451                    nodeconf[k] = env[k] 
     452        if "/" in app.conf: 
     453            nodeconf.update(app.conf["/"]) 
    442454        object_trail = [('root', root, nodeconf, curpath)] 
    443455         
     
    452464            if node is not None: 
    453465                # Get _cp_config attached to this node. 
    454                 nodeconf = getattr(node, "_cp_config", {}).copy() 
     466                if hasattr(node, "_cp_config"): 
     467                    nodeconf.update(node._cp_config) 
     468                     
     469                    # Resolve "environment" entries. This must be done node-by-node 
     470                    # so that a child's "environment" can override concrete settings 
     471                    # of a parent. However, concrete settings in this node will 
     472                    # override "environment" settings in the same node. 
     473                    if 'environment' in nodeconf: 
     474                        env = environments[nodeconf['environment']] 
     475                        for k in env: 
     476                            if k not in nodeconf: 
     477                                nodeconf[k] = env[k] 
    455478             
    456479            # Mix in values from app.conf for this path. 
    457480            curpath = "/".join((curpath, name)) 
    458             nodeconf.update(app.conf.get(curpath, {})) 
    459              
    460             # Resolve "environment" entries. This must be done node-by-node 
    461             # so that a child's "environment" can override concrete settings 
    462             # of a parent. However, concrete settings in this node will 
    463             # override "environment" settings in the same node. 
    464             env = nodeconf.get("environment") 
    465             if env: 
    466                 for k, v in cherrypy.config.environments[env].iteritems(): 
    467                     if k not in nodeconf: 
    468                         nodeconf[k] = v 
     481            if curpath in app.conf: 
     482                nodeconf.update(app.conf[curpath]) 
    469483             
    470484            object_trail.append((objname, node, nodeconf, curpath)) 
     
    473487            """Set cherrypy.request.config.""" 
    474488            base = cherrypy.config.globalconf.copy() 
    475             if 'tools.staticdir.dir' in base: 
    476                 base['tools.staticdir.section'] = "global" 
     489            # Note that we merge the config from each node 
     490            # even if that node was None. 
    477491            for name, obj, conf, curpath in object_trail: 
    478492                base.update(conf) 
    479493                if 'tools.staticdir.dir' in conf: 
    480494                    base['tools.staticdir.section'] = curpath 
    481             request.config = base 
     495            return base 
    482496         
    483497        # Try successive objects (reverse order) 
     
    485499             
    486500            name, candidate, nodeconf, curpath = object_trail[i] 
     501            if candidate is None: 
     502                continue 
    487503             
    488504            # Try a "default" method on the current leaf. 
    489             defhandler = getattr(candidate, "default", None) 
    490             if getattr(defhandler, 'exposed', False): 
    491                 # Insert any extra _cp_config from the default handler. 
    492                 conf = getattr(defhandler, "_cp_config", {}) 
    493                 object_trail.insert(i+1, ("default", defhandler, conf, curpath)) 
    494                 set_conf() 
    495                 return defhandler, names[i:-1] 
     505            if hasattr(candidate, "default"): 
     506                defhandler = candidate.default 
     507                if getattr(defhandler, 'exposed', False): 
     508                    # Insert any extra _cp_config from the default handler. 
     509                    conf = getattr(defhandler, "_cp_config", {}) 
     510                    object_trail.insert(i+1, ("default", defhandler, conf, curpath)) 
     511                    request.config = set_conf() 
     512                    return defhandler, names[i:-1] 
    496513             
    497514            # Uncomment the next line to restrict positional params to "default". 
     
    500517            # Try the current leaf. 
    501518            if getattr(candidate, 'exposed', False): 
    502                 set_conf() 
     519                request.config = set_conf() 
    503520                if i == len(object_trail) - 1: 
    504521                    # We found the extra ".index". Check if the original path 
    505522                    # had a trailing slash (otherwise, do a redirect). 
    506                     if not path.endswith('/')
     523                    if path[-1:] != '/'
    507524                        atoms = request.browser_url.split("?", 1) 
    508525                        newUrl = atoms.pop(0) + '/' 
     
    513530         
    514531        # We didn't find anything 
    515         set_conf() 
     532        request.config = set_conf() 
    516533        return None, [] 
    517534 
     
    628645        self.headers = http.HeaderMap() 
    629646        content_type = cherrypy.config.get('default_content_type', 'text/html') 
    630         self.headers.update({ 
     647        # Since we know all our keys are titled strings, we can 
     648        # bypass HeaderMap.update and get a big speed boost. 
     649        dict.update(self.headers, { 
    631650            "Content-Type": content_type, 
    632651            "Server": "CherryPy/" + cherrypy.__version__, 
  • trunk/cherrypy/config.py

    r1183 r1201  
    4545    # Load other into base 
    4646    for section, value_map in other.iteritems(): 
     47        # Resolve "environment" entries 
     48        if 'environment' in value_map: 
     49            env = environments[value_map['environment']] 
     50            for k in env: 
     51                if k not in value_map: 
     52                    value_map[k] = env[k] 
     53            del value_map['environment'] 
     54         
    4755        base.setdefault(section, {}).update(value_map) 
    4856 
     
    7987    if isinstance(conf.get("global", None), dict): 
    8088        conf = conf["global"] 
     89     
     90    if 'environment' in conf: 
     91        env = environments[conf['environment']] 
     92        for k in env: 
     93            if k not in conf: 
     94                conf[k] = env[k] 
     95     
     96    if 'tools.staticdir.dir' in conf: 
     97        conf['tools.staticdir.section'] = "global" 
     98     
    8199    globalconf.update(conf) 
    82100     
     
    128146def get(key, default=None): 
    129147    """Return the config value corresponding to key, or default.""" 
    130      
    131148    try: 
    132149        conf = cherrypy.request.config 
     
    137154        conf = globalconf 
    138155     
    139     try: 
    140         return conf[key] 
    141     except KeyError: 
    142         try: 
    143             env = conf["environment"] 
    144             return environments[env][key] 
    145         except KeyError: 
    146             return default 
     156    return conf.get(key, default) 
    147157 
    148158 

Hosted by WebFaction

Log in as guest/cpguest to create tickets