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

Changeset 1608

Show
Ignore:
Timestamp:
02/01/07 13:06:34
Author:
fumanchu
Message:

New engine.release method, which decouples request and engine. Also new server.base method, which simplifies cherrypy.url. Finally, cherrypy._serving is promoted to cherrypy.serving, and has a new "load" method.

Files:

Legend:

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

    r1602 r1608  
    1 """Global module that all modules developing with CherryPy should import.""" 
     1"""CherryPy is a pythonic, object-oriented HTTP framework. 
     2 
     3 
     4CherryPy consists of not one, but four separate API layers. 
     5 
     6The APPLICATION LAYER is the simplest. CherryPy applications are written as 
     7a tree of classes and methods, where each branch in the tree corresponds to 
     8a branch in the URL path. Each method is a 'page handler', which receives 
     9GET and POST params as keyword arguments, and returns or yields the (HTML) 
     10body of the response. The special method name 'index' is used for paths 
     11that end in a slash, and the special method name 'default' is used to 
     12handle multiple paths via a single handler. This layer also includes: 
     13 
     14 * the 'exposed' attribute (and cherrypy.expose) 
     15 * cherrypy.quickstart() 
     16 * _cp_config attributes 
     17 * cherrypy.tools (including cherrypy.session) 
     18 * cherrypy.url() 
     19 
     20The ENVIRONMENT LAYER is used by developers at all levels. It provides 
     21information about the current request and response, plus the application 
     22and server environment, via a (default) set of top-level objects: 
     23 
     24 * cherrypy.request 
     25 * cherrypy.response 
     26 * cherrypy.engine 
     27 * cherrypy.server 
     28 * cherrypy.tree 
     29 * cherrypy.config 
     30 * cherrypy.thread_data 
     31 * cherrypy.log 
     32 * cherrypy.HTTPError, NotFound, and HTTPRedirect 
     33 * cherrypy.lib 
     34 
     35The EXTENSION LAYER allows advanced users to construct and share their own 
     36plugins. It consists of: 
     37 
     38 * Hook API 
     39 * Tool API 
     40 * Toolbox API 
     41 * Dispatch API 
     42 * Config Namespace API 
     43 
     44Finally, there is the CORE LAYER, which uses the core API's to construct 
     45the default components which are available at higher layers. You can think 
     46of the default components as the 'reference implementation' for CherryPy. 
     47Megaframeworks (and advanced users) may replace the default components 
     48with customized or extended components. The core API's are: 
     49 
     50 * Application API 
     51 * Engine API 
     52 * Request API 
     53 * Server API 
     54 * WSGI API 
     55 
     56These API's are described in the CherryPy specification: 
     57http://www.cherrypy.org/wiki/CherryPySpec 
     58""" 
    259 
    360__version__ = "3.0.1alpha" 
     
    133190# a new HTTP conversation, yet still refer to them as module-level globals 
    134191# in a thread-safe way. 
    135 _serving = _local() 
     192class _Serving(_local): 
     193    """An interface for registering request and response objects.""" 
     194     
     195    def load(self, request, response): 
     196        self.request = request 
     197        self.response = response 
     198     
     199    def clear(self): 
     200        """Remove all attributes of self.""" 
     201        self.__dict__.clear() 
     202 
     203serving = _serving = _Serving() 
    136204 
    137205 
     
    327395        # if you're using vhosts or tools.proxy. 
    328396        if base is None: 
    329             f = server.socket_file 
    330             if f: 
    331                 base = f 
    332             else: 
    333                 host = server.socket_host 
    334                 if not host: 
    335                     # The empty string signifies INADDR_ANY. 
    336                     # Look up the host name, which should be 
    337                     # the safest thing to spit out in a URL. 
    338                     import socket 
    339                     host = socket.gethostname() 
    340                 port = server.socket_port 
    341                 if server.ssl_certificate: 
    342                     scheme = "https" 
    343                     if port != 443: 
    344                         host += ":%s" % port 
    345                 else: 
    346                     scheme = "http" 
    347                     if port != 80: 
    348                         host += ":%s" % port 
    349                 base = "%s://%s" % (scheme, host) 
     397            base = server.base() 
     398         
    350399        path = (script_name or "") + path 
    351400        newurl = base + path + qs 
  • trunk/cherrypy/_cpengine.py

    r1590 r1608  
    206206    def wait(self): 
    207207        """Block the caller until ready to receive requests (or error).""" 
    208         while not self.ready
     208        while not (self.state == STARTED)
    209209            time.sleep(.1) 
    210      
    211     def _is_ready(self): 
    212         return bool(self.state == STARTED) 
    213     ready = property(_is_ready, doc="Return True if the engine is ready to" 
    214                                     " receive requests, False otherwise.") 
    215210     
    216211    def request(self, local_host, remote_host, scheme="http", 
    217212                server_protocol="HTTP/1.1"): 
    218         """Obtain an HTTP Request object. 
     213        """Obtain and return an HTTP Request object. (Core) 
    219214         
    220215        local_host should be an http.Host object with the server info. 
     
    237232            req = self.request_class(local_host, remote_host, scheme, 
    238233                                     server_protocol) 
    239         cherrypy._serving.request = req 
    240         cherrypy._serving.response = resp = self.response_class(
     234        resp = self.response_class() 
     235        cherrypy.serving.load(req, resp
    241236        self.servings.append((req, resp)) 
    242237        return req 
    243238     
     239    def release(self): 
     240        """Close and de-reference the current request and response. (Core)""" 
     241        req = cherrypy.serving.request 
     242         
     243        try: 
     244            req.close() 
     245        except: 
     246            cherrypy.log(traceback=True) 
     247         
     248        try: 
     249            self.servings.remove((req, cherrypy.serving.response)) 
     250        except ValueError: 
     251            pass 
     252         
     253        cherrypy.serving.clear() 
     254     
    244255    def monitor(self): 
    245         """Check timeout on all responses.""" 
     256        """Check timeout on all responses. (Internal)""" 
    246257        if self.state == STARTED: 
    247258            for req, resp in self.servings: 
  • trunk/cherrypy/_cpmodpy.py

    r1433 r1608  
    179179                    break 
    180180                except cherrypy.InternalRedirect, ir: 
    181                     request.close() 
     181                    cherrypy.engine.release() 
    182182                    prev = request 
    183183                     
     
    199199             
    200200            send_response(req, response.status, response.header_list, response.body) 
    201             request.close() 
     201            cherrypy.engine.release() 
    202202    except: 
    203203        tb = format_exc() 
  • trunk/cherrypy/_cpserver.py

    r1528 r1608  
    175175        self.stop() 
    176176        self.start() 
     177     
     178    def base(self): 
     179        """Return the base (scheme://host) for this server manager.""" 
     180        if self.socket_file: 
     181            return self.socket_file 
     182         
     183        host = self.socket_host 
     184        if not host: 
     185            # The empty string signifies INADDR_ANY. Look up the host name, 
     186            # which should be the safest thing to spit out in a URL. 
     187            host = socket.gethostname() 
     188         
     189        port = self.socket_port 
     190         
     191        if self.ssl_certificate: 
     192            scheme = "https" 
     193            if port != 443: 
     194                host += ":%s" % port 
     195        else: 
     196            scheme = "http" 
     197            if port != 80: 
     198                host += ":%s" % port 
     199         
     200        return "%s://%s" % (scheme, host) 
    177201 
    178202 
  • trunk/cherrypy/_cptools.py

    r1580 r1608  
    1616     
    1717    CherryPy config: 
    18         Hookpoints are places in the CherryPy request-handling process 
    19         which may hand off control to registered callbacks. The Request 
    20         object possesses a "hooks" attribute (a HookMap) for manipulating 
    21         this. If a tool exposes a "_setup" callable, it will be called 
     18        If a tool exposes a "_setup" callable, it will be called 
    2219        once per Request (if the feature is "turned on" via config). 
    2320 
  • trunk/cherrypy/_cpwsgi.py

    r1531 r1608  
    193193     
    194194    def close(self): 
    195         if hasattr(self.request, "close"): 
    196             try: 
    197                 self.request.close() 
    198             except: 
    199                 _cherrypy.log(traceback=True) 
     195        _cherrypy.engine.release() 
    200196     
    201197    def get_engine_request(self, environ, cpapp): 

Hosted by WebFaction

Log in as guest/cpguest to create tickets