Changeset 1201
- Timestamp:
- 07/10/06 23:33:48
- Files:
-
- trunk/cherrypy/_cprequest.py (modified) (16 diffs)
- trunk/cherrypy/config.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cprequest.py
r1200 r1201 28 28 self.callbacks[point].append(wrapper) 29 29 30 def run(self, point , *args, **kwargs):30 def run(self, point): 31 31 """Execute all registered callbacks for the given point.""" 32 32 failsafe = point in self.failsafe … … 40 40 if failsafe: 41 41 try: 42 callback( *args, **kwargs)42 callback() 43 43 except (KeyboardInterrupt, SystemExit): 44 44 raise … … 46 46 cherrypy.log(traceback=True) 47 47 else: 48 callback( *args, **kwargs)48 callback() 49 49 50 50 … … 267 267 268 268 # Process the headers into self.headers 269 headers = self.headers 269 270 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() 270 274 value = value.strip() 271 275 … … 273 277 # only Konqueror does that), only the last one will remain in headers 274 278 # (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) 276 283 277 284 # Handle cookies differently because on Konqueror, multiple 278 285 # cookies come on different lines with the same key 279 if name .title()== 'Cookie':286 if name == 'Cookie': 280 287 self.simple_cookie.load(value) 281 288 282 host = self.headers.get('Host') 283 if host is None: 289 if not dict.__contains__(headers, 'Host'): 284 290 # All Internet-based HTTP/1.1 servers MUST respond with a 400 285 291 # (Bad Request) status code to any HTTP/1.1 request message … … 288 294 msg = "HTTP/1.1 requires a 'Host' request header." 289 295 raise cherrypy.HTTPError(400, msg) 296 host = dict.__getitem__(headers, 'Host') 290 297 if not host: 291 298 host = cherrypy.config.get('server.socket_host', '') … … 306 313 break 307 314 308 env = nodeconf.get("environment")309 if env:310 d = cherrypy.config.environments[env].get("dispatch")311 if d:312 dispatch = d313 break314 315 315 lastslash = trail.rfind("/") 316 316 if lastslash == -1: … … 327 327 """Populate self.toolmap and set up each tool.""" 328 328 # 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] 333 334 if namespace == "tools": 334 toolname = atoms .pop(0)335 bucket = self.toolmap.setdefault(toolname, {})336 bucket[ ".".join(atoms)] = v335 toolname = atoms[1] 336 bucket = tm.setdefault(toolname, {}) 337 bucket[atoms[2]] = reqconf[k] 337 338 elif namespace == "hooks": 338 339 # Attach bare hooks declared in config. 339 hookpoint = atoms[0] 340 hookpoint = atoms[1] 341 v = reqconf[k] 340 342 if isinstance(v, basestring): 341 343 v = cherrypy.lib.attributes(v) … … 343 345 344 346 # 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) 348 351 tool._setup() 349 352 … … 437 440 438 441 # Get config for the root object/path. 442 environments = cherrypy.config.environments 439 443 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["/"]) 442 454 object_trail = [('root', root, nodeconf, curpath)] 443 455 … … 452 464 if node is not None: 453 465 # 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] 455 478 456 479 # Mix in values from app.conf for this path. 457 480 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]) 469 483 470 484 object_trail.append((objname, node, nodeconf, curpath)) … … 473 487 """Set cherrypy.request.config.""" 474 488 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. 477 491 for name, obj, conf, curpath in object_trail: 478 492 base.update(conf) 479 493 if 'tools.staticdir.dir' in conf: 480 494 base['tools.staticdir.section'] = curpath 481 re quest.config =base495 return base 482 496 483 497 # Try successive objects (reverse order) … … 485 499 486 500 name, candidate, nodeconf, curpath = object_trail[i] 501 if candidate is None: 502 continue 487 503 488 504 # 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] 496 513 497 514 # Uncomment the next line to restrict positional params to "default". … … 500 517 # Try the current leaf. 501 518 if getattr(candidate, 'exposed', False): 502 set_conf()519 request.config = set_conf() 503 520 if i == len(object_trail) - 1: 504 521 # We found the extra ".index". Check if the original path 505 522 # had a trailing slash (otherwise, do a redirect). 506 if not path.endswith('/'):523 if path[-1:] != '/': 507 524 atoms = request.browser_url.split("?", 1) 508 525 newUrl = atoms.pop(0) + '/' … … 513 530 514 531 # We didn't find anything 515 set_conf()532 request.config = set_conf() 516 533 return None, [] 517 534 … … 628 645 self.headers = http.HeaderMap() 629 646 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, { 631 650 "Content-Type": content_type, 632 651 "Server": "CherryPy/" + cherrypy.__version__, trunk/cherrypy/config.py
r1183 r1201 45 45 # Load other into base 46 46 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 47 55 base.setdefault(section, {}).update(value_map) 48 56 … … 79 87 if isinstance(conf.get("global", None), dict): 80 88 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 81 99 globalconf.update(conf) 82 100 … … 128 146 def get(key, default=None): 129 147 """Return the config value corresponding to key, or default.""" 130 131 148 try: 132 149 conf = cherrypy.request.config … … 137 154 conf = globalconf 138 155 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) 147 157 148 158

