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

Changeset 230

Show
Ignore:
Timestamp:
06/02/05 13:55:24
Author:
fumanchu
Message:

1. Moved onStartResource before processRequestHeader where it should have been in the first place. Most filters also changed as a result.
2. Overrode send_response in _cpwsgi. Fixes ticket 168.
3. Fixed ticket 167 (testCore.py).

Files:

Legend:

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

    r229 r230  
    9898    def run(self): 
    9999        try: 
    100             self.processRequestHeaders() 
    101              
    102100            try: 
    103                 # onStart has to be after processRequestHeaders 
    104                 # so that "path", for example, gets set. 
    105101                applyFilters('onStartResource') 
    106102                 
    107                 applyFilters('beforeRequestBody') 
    108                 if cpg.request.processRequestBody: 
    109                     self.processRequestBody() 
    110                  
    111                 applyFilters('beforeMain') 
    112                 if cpg.response.body is None: 
    113                     main() 
    114                  
    115                 applyFilters('beforeFinalize') 
    116                 finalize() 
     103                try: 
     104                    self.processRequestHeaders() 
     105                     
     106                    applyFilters('beforeRequestBody') 
     107                    if cpg.request.processRequestBody: 
     108                        self.processRequestBody() 
     109                     
     110                    applyFilters('beforeMain') 
     111                    if cpg.response.body is None: 
     112                        main() 
     113                     
     114                    applyFilters('beforeFinalize') 
     115                    finalize() 
     116                except basefilter.RequestHandled: 
     117                    pass 
    117118            finally: 
    118119                applyFilters('onEndResource') 
    119         except basefilter.RequestHandled: 
    120             pass 
    121120        except: 
    122121            handleError(sys.exc_info()) 
  • trunk/cherrypy/_cpwsgi.py

    r229 r230  
    207207        self.wfile.write(data) 
    208208     
     209    def send_response(self, code, message=None): 
     210        self.log_request(code) 
     211        if message is None: 
     212            if code in self.responses: 
     213                message = self.responses[code][0] 
     214            else: 
     215                message = '' 
     216        if self.request_version != 'HTTP/0.9': 
     217            self.wfile.write("%s %d %s\r\n" % 
     218                             (self.protocol_version, code, message)) 
     219     
    209220    def handleError(self, exc): 
    210221        self.close_connection = 1 
  • trunk/cherrypy/lib/filter/baseurlfilter.py

    r229 r230  
    3535    """ 
    3636     
    37     def onStartResource(self): 
     37    def beforeRequestBody(self): 
    3838        # We have to dynamically import cpg because Python can't handle 
    3939        #   circular module imports :-( 
    4040        global cpg 
    4141        from cherrypy import cpg 
    42         cpg.threadData.baseUrlFilterOn = cpg.config.get('baseUrlFilter.on', False) 
    43         cpg.threadData.baseUrlFilterBaseUrl = cpg.config.get('baseUrlFilter.baseUrl', 'http://localhost') 
    44         cpg.threadData.baseUrlFilterUseXForwardedHost = cpg.config.get('baseUrlFilter.useXForwardedHost', True) 
    45      
    46     def beforeRequestBody(self): 
    47         if not cpg.threadData.baseUrlFilterOn: 
     42         
     43        if not cpg.config.get('baseUrlFilter.on', False): 
    4844            return 
    4945         
    50         newBaseUrl = cpg.threadData.baseUrlFilterBaseUrl 
    51         if cpg.threadData.baseUrlFilterUseXForwardedHost
     46        newBaseUrl = cpg.config.get('baseUrlFilter.baseUrl', 'http://localhost') 
     47        if cpg.config.get('baseUrlFilter.useXForwardedHost', True)
    5248            newBaseUrl = cpg.request.headerMap.get("X-Forwarded-Host", newBaseUrl) 
    5349         
  • trunk/cherrypy/lib/filter/cachefilter.py

    r229 r230  
    126126        self.maxobjects = maxobjects 
    127127     
    128     def onStartResource(self): 
     128    def beforeMain(self): 
    129129        # We have to dynamically import cpg because Python can't handle 
    130130        #   circular module imports :-( 
    131131        global cpg 
    132132        from cherrypy import cpg 
    133         cpg.threadData.cacheFilterOn = cpg.config.get('cacheFilter.on', False) 
    134         if cpg.threadData.cacheFilterOn and not hasattr(cpg, '_cache'): 
     133        if not cpg.config.get('cacheFilter.on', False): 
     134            return 
     135         
     136        if not hasattr(cpg, '_cache'): 
    135137            cpg._cache = self.CacheClass(self.key, self.delay, 
    136138                self.maxobjsize, self.maxsize, self.maxobjects) 
    137      
    138     def beforeMain(self): 
    139         """Checks if the page is already in the cache""" 
    140         if not cpg.threadData.cacheFilterOn: 
    141             return 
    142139         
    143140        cacheData = cpg._cache.get() 
     
    162159    def onEndResource(self): 
    163160        """Close & fix the cache entry after content was fully written""" 
    164         if not cpg.threadData.cacheFilterOn
     161        if not cpg.config.get('cacheFilter.on', False)
    165162            return 
    166163         
  • trunk/cherrypy/lib/filter/decodingfilter.py

    r229 r230  
    3232    """Automatically decodes request parameters (except uploads).""" 
    3333     
    34     def onStartResource(self): 
     34    def beforeMain(self): 
    3535        # We have to dynamically import cpg because Python can't handle 
    3636        #   circular module imports :-( 
    3737        global cpg 
    3838        from cherrypy import cpg 
    39         cpg.threadData.decodingFilterOn = cpg.config.get('decodingFilter.on', False) 
    40         cpg.threadData.decodingFilterEncoding = cpg.config.get('decodingFilter.encoding', 'utf-8') 
    4139     
    42     def beforeMain(self): 
    43         if not cpg.threadData.decodingFilterOn: 
     40        if not cpg.config.get('decodingFilter.on', False): 
    4441            return 
    4542         
    46         enc = cpg.threadData.decodingFilterEncoding 
     43        enc = cpg.config.get('decodingFilter.encoding', 'utf-8') 
    4744        for key, value in cpg.request.paramMap.items(): 
    4845            if cpg.request.filenameMap.get(key): 
  • trunk/cherrypy/lib/filter/encodingfilter.py

    r229 r230  
    3232    """Filter that automatically encodes the response.""" 
    3333     
    34     def onStartResource(self): 
     34    def beforeFinalize(self): 
    3535        # We have to dynamically import cpg because Python can't handle 
    3636        #   circular module imports :-( 
    3737        global cpg 
    3838        from cherrypy import cpg 
    39         cpg.threadData.encodingFilterOn = cpg.config.get('encodingFilter.on', False) 
    40         cpg.threadData.encodingFilterEncoding = cpg.config.get('encodingFilter.encoding', 'utf-8') 
    41         cpg.threadData.encodingFilterMimeTypeList = cpg.config.get('encodingFilter.mimeTypeList', ['text/html']) 
    42      
    43     def beforeFinalize(self): 
    44         if not cpg.threadData.encodingFilterOn: 
     39         
     40        if not cpg.config.get('encodingFilter.on', False): 
    4541            return 
    4642         
     
    4844        if contentType: 
    4945            ctlist = contentType.split(';')[0] 
    50             if (ctlist in cpg.threadData.encodingFilterMimeTypeList): 
    51                 enc = cpg.threadData.encodingFilterEncoding 
     46            if (ctlist in cpg.config.get('encodingFilter.mimeTypeList', ['text/html'])): 
     47                enc = cpg.config.get('encodingFilter.encoding', 'utf-8') 
    5248                 
    5349                # Add "charset=..." to response Content-Type header 
  • trunk/cherrypy/lib/filter/gzipfilter.py

    r229 r230  
    3535    """Filter that gzips the response.""" 
    3636     
    37     def onStartResource(self): 
     37    def beforeFinalize(self): 
    3838        # We have to dynamically import cpg because Python can't handle 
    3939        #   circular module imports :-( 
    4040        global cpg 
    4141        from cherrypy import cpg 
    42         cpg.threadData.gzipFilterOn = cpg.config.get('gzipFilter.on', False) 
    43         cpg.threadData.gzipFilterMimeTypeList = cpg.config.get('gzipFilter.mimeTypeList', ['text/html']) 
    44         cpg.threadData.gzipFilterCompressLevel = cpg.config.get('gzipFilter.compresslevel', 9) 
    45      
    46     def beforeFinalize(self): 
    47         if not cpg.threadData.gzipFilterOn: 
     42        if not cpg.config.get('gzipFilter.on', False): 
    4843            return 
    4944         
     
    5449        ct = cpg.response.headerMap.get('Content-Type').split(';')[0] 
    5550        ae = cpg.request.headerMap.get('Accept-Encoding', '') 
    56         if (ct in cpg.threadData.gzipFilterMimeTypeList) and ('gzip' in ae): 
     51        if (ct in cpg.config.get('gzipFilter.mimeTypeList', ['text/html']) 
     52            and ('gzip' in ae)): 
    5753            cpg.response.headerMap['Content-Encoding'] = 'gzip' 
    5854            # Return a generator that compresses the page 
    59             cpg.response.body = self.zip_body(cpg.response.body) 
    60  
     55            level = cpg.config.get('gzipFilter.compresslevel', 9) 
     56            cpg.response.body = self.zip_body(cpg.response.body, level) 
     57     
    6158    def write_gzip_header(self): 
    6259        """Adapted from the gzip.py standard module code""" 
     
    6966        header += '\377' 
    7067        return header 
    71              
     68     
    7269    def write_gzip_trailer(self, crc, size): 
    7370        footer = struct.pack("<l", crc) 
    7471        footer += struct.pack("<L", size & 0xFFFFFFFFL) 
    7572        return footer 
    76  
    77     def zip_body(self, body): 
     73     
     74    def zip_body(self, body, compress_level): 
    7875        # Compress page 
    7976        yield self.write_gzip_header() 
    8077        crc = zlib.crc32("") 
    8178        size = 0 
    82         zobj = zlib.compressobj(cpg.threadData.gzipFilterCompressLevel, 
     79        zobj = zlib.compressobj(compress_level, 
    8380                                zlib.DEFLATED, -zlib.MAX_WBITS, 
    8481                                zlib.DEF_MEM_LEVEL, 0) 
  • trunk/cherrypy/lib/filter/logdebuginfofilter.py

    r229 r230  
    3333    """Filter that adds debug information to the page""" 
    3434     
    35     def onStartResource(self): 
     35    def beforeMain(self): 
    3636        # We have to dynamically import cpg because Python can't handle 
    3737        #   circular module imports :-( 
    3838        global cpg 
    3939        from cherrypy import cpg 
     40        cpg.request.startBuilTime = time.time() 
     41     
     42    def beforeFinalize(self): 
    4043        if cpg.config.get('server.environment') == 'dev': 
    4144            # In "dev" environment, log everything by default 
     
    4447            defaultOn = False 
    4548         
    46         cpg.threadData.logDebugInfoFilterOn = cpg.config.get('logDebugInfoFilter.on', defaultOn) 
    47         cpg.threadData.logDebugInfoFilterMimeTypeList = cpg.config.get('logDebugInfoFilter.mimeTypeList', ['text/html']) 
    48         cpg.threadData.logDebugInfoFilterLogBuildTime = cpg.config.get('logDebugInfoFilter.logBuildTime', True) 
    49         cpg.threadData.logDebugInfoFilterLogPageSize = cpg.config.get('logDebugInfoFilter.logPageSize', True) 
    50         cpg.threadData.logDebugInfoFilterLogSessionSize = cpg.config.get('logDebugInfoFilter.logSessionSize', True) 
    51         cpg.threadData.logDebugInfoFilterLogAsComment = cpg.config.get('logDebugInfoFilter.logAsComment', False) 
    52      
    53     def beforeMain(self): 
    54         if not cpg.threadData.logDebugInfoFilterOn: 
     49        if not cpg.config.get('logDebugInfoFilter.on', defaultOn): 
    5550            return 
    5651         
    57         cpg.request.startBuilTime = time.time() 
    58      
    59     def beforeFinalize(self): 
    60         if not cpg.threadData.logDebugInfoFilterOn: 
    61             return 
    62          
     52        mimelist = cpg.config.get('logDebugInfoFilter.mimeTypeList', ['text/html']) 
    6353        ct = cpg.response.headerMap.get('Content-Type').split(';')[0] 
    64         if ct in cpg.threadData.logDebugInfoFilterMimeTypeList: 
     54        if ct in mimelist: 
    6555            body = ''.join(cpg.response.body) 
    6656            debuginfo = '\n' 
    67             if cpg.threadData.logDebugInfoFilterLogAsComment: 
     57             
     58            logAsComment = cpg.config.get('logDebugInfoFilter.logAsComment', False) 
     59            if logAsComment: 
    6860                debuginfo += '<!-- ' 
    6961            else: 
    7062                debuginfo += "<br/><br/>" 
    7163            logList = [] 
    72             if cpg.threadData.logDebugInfoFilterLogBuildTime: 
     64             
     65            if cpg.config.get('logDebugInfoFilter.logBuildTime', True): 
    7366                logList.append("Build time: %.03fs" % ( 
    7467                    time.time() - cpg.request.startBuilTime)) 
    75             if cpg.threadData.logDebugInfoFilterLogPageSize: 
     68             
     69            if cpg.config.get('logDebugInfoFilter.logPageSize', True): 
    7670                logList.append("Page size: %.02fKB" % ( 
    7771                    len(body)/float(1024))) 
    78             if cpg.threadData.logDebugInfoFilterLogSessionSize and \ 
    79                     cpg.config.get('session.storageType'): 
     72             
     73            if (cpg.config.get('logDebugInfoFilter.logSessionSize', True) 
     74                and cpg.config.get('session.storageType')): 
    8075                # Pickle session data to get its size 
    8176                try: 
     
    9085             
    9186            debuginfo += ', '.join(logList) 
    92             if cpg.threadData.logDebugInfoFilterLogAsComment: 
     87            if logAsComment: 
    9388                debuginfo += '-->' 
    9489             
  • trunk/cherrypy/lib/filter/sessionfilter.py

    r229 r230  
    2929from basefilter import BaseFilter 
    3030import random, sha, string, time 
     31alphanum = string.letters + string.digits 
    3132 
    3233 
    3334class SessionFilter(BaseFilter): 
    3435     
    35     def onStartResource(self): 
     36    def beforeMain(self): 
    3637        # We have to dynamically import cpg because Python can't handle 
    3738        #   circular module imports :-( 
     
    3940        from cherrypy import cpg, _cputil 
    4041        cpg.threadData.sessionFilterOn = bool(cpg.config.get('session.storageType')) 
    41      
    42     def beforeMain(self): 
     42         
    4343        if cpg.threadData.sessionFilterOn: 
    4444            cleanupSessionData() 
     
    8787 
    8888def generateSessionId(): 
    89     s = '' 
    90     for i in xrange(50): 
    91         s += random.choice(string.letters + string.digits) 
     89    s = ''.join([random.choice(alphanum) for i in xrange(50)]) 
    9290    s += '%s' % time.time() 
    9391    return sha.sha(s).hexdigest() 
  • trunk/cherrypy/lib/filter/staticfilter.py

    r229 r230  
    3434    """Filter that handles static content.""" 
    3535     
    36     def onStartResource(self): 
     36    def beforeMain(self): 
    3737        # We have to dynamically import cpg because Python can't handle 
    3838        #   circular module imports :-( 
    3939        global cpg, _cphttptools, cperror 
    4040        from cherrypy import cpg, _cphttptools, cperror 
    41         cpg.threadData.staticFilterOn = p = cpg.config.get('staticFilter.on', False) 
    42         cpg.threadData.staticFilterFile = cpg.config.get('staticFilter.file') 
    43         cpg.threadData.staticFilterDir = cpg.config.get('staticFilter.dir') 
    44         if cpg.threadData.staticFilterDir: 
    45             cpg.threadData.staticFilterConfigSection = \ 
    46                 cpg.config.get('staticFilter.dir', returnSection = True) 
    47      
    48     def beforeMain(self): 
    49         if not cpg.threadData.staticFilterOn: 
     41         
     42        if not cpg.config.get('staticFilter.on', False): 
    5043            return 
    5144         
    52         if cpg.threadData.staticFilterFile: 
    53             filename = cpg.threadData.staticFilterFile 
    54         else: 
    55             section = cpg.threadData.staticFilterConfigSection 
     45        filename = cpg.config.get('staticFilter.file') 
     46        if not filename: 
     47            staticDir = cpg.config.get('staticFilter.dir') 
     48            section = cpg.config.get('staticFilter.dir', returnSection=True) 
    5649            extraPath = cpg.request.path[len(section) + 1:] 
    57             filename = os.path.join(cpg.threadData.staticFilterDir, 
    58                 extraPath) 
     50            filename = os.path.join(staticDir, extraPath) 
    5951         
    6052        # Serve filename 
  • trunk/cherrypy/lib/filter/tidyfilter.py

    r229 r230  
    3939    """ 
    4040     
    41     def onStartResource(self): 
     41    def beforeFinalize(self): 
    4242        # We have to dynamically import cpg because Python can't handle 
    4343        #   circular module imports :-( 
    4444        global cpg 
    4545        from cherrypy import cpg 
    46         cpg.threadData.tidyFilterOn = cpg.config.get('tidyFilter.on', False) 
    47         cpg.threadData.tifyFilterTidyPath = cpg.config.get('tidyFilter.tidyPath') 
    48         cpg.threadData.tifyFilterTmpDir = cpg.config.get('tidyFilter.tmpDir') 
    49         cpg.threadData.tidyFilterStrictXml = cpg.config.get('tidyFilter.strictXml', False) 
    50         cpg.threadData.tidyFilterErrorsToIgnore = cpg.config.get('encodingFilter.errorsToIgnore', []) 
    51      
    52     def beforeFinalize(self): 
    53         if not cpg.threadData.tidyFilterOn: 
     46         
     47        if not cpg.config.get('tidyFilter.on', False): 
    5448            return 
    5549         
     
    6155        ct = cpg.response.headerMap.get('Content-Type', '').split(';')[0] 
    6256        if ct == 'text/html': 
    63             tmpdir = cpg.threadData.tifyFilterTmpDir 
     57            tmpdir = cpg.config.get('tidyFilter.tmpDir') 
    6458            pageFile = os.path.join(tmpdir, 'page.html') 
    6559            outFile = os.path.join(tmpdir, 'tidy.out') 
     
    7569            if encoding: 
    7670                encoding = '-' + encoding 
     71             
    7772            strictXml = "" 
    78             if cpg.threadData.tidyFilterStrictXml
     73            if cpg.config.get('tidyFilter.strictXml', False)
    7974                strictXml = ' -xml' 
    8075            os.system('"%s" %s%s -f %s -o %s %s' % 
    81                       (cpg.threadData.tifyFilterTidyPath, encoding, 
     76                      (cpg.config.get('tidyFilter.tidyPath'), encoding, 
    8277                       strictXml, errFile, outFile, pageFile)) 
    8378            f = open(errFile, 'rb') 
     
    9085                if (err.find('Warning') != -1 or err.find('Error') != -1): 
    9186                    ignore = 0 
    92                     for errIgn in cpg.threadData.tidyFilterErrorsToIgnore
     87                    for errIgn in cpg.config.get('encodingFilter.errorsToIgnore', [])
    9388                        if err.find(errIgn) != -1: 
    9489                            ignore = 1 
    9590                            break 
    96                     if not ignore: newErrList.append(err) 
     91                    if not ignore: 
     92                        newErrList.append(err) 
    9793             
    9894            if newErrList: 
    9995                newBody = "Wrong HTML:<br>" + cgi.escape('\n'.join(newErrList)).replace('\n','<br>') 
    10096                newBody += '<br><br>' 
    101                 i=
     97                i =
    10298                for line in originalBody.splitlines(): 
    10399                    i += 1 
     
    106102                cpg.response.body = [newBody] 
    107103 
    108             elif cpg.threadData.tidyFilterStrictXml: 
     104            elif strictXml: 
    109105                # The HTML is OK, but is it valid XML 
    110106                # Use elementtree to parse XML 
     
    121117                    newBody = "Wrong XML:<br>" + cgi.escape(bodyFile.getvalue().replace('\n','<br>')) 
    122118                    newBody += '<br><br>' 
    123                     i=
     119                    i =
    124120                    for line in originalBody.splitlines(): 
    125121                        i += 1 
  • trunk/cherrypy/lib/filter/virtualhostfilter.py

    r229 r230  
    3636    """ 
    3737     
    38     def onStartResource(self): 
     38    def beforeRequestBody(self): 
    3939        # We have to dynamically import cpg because Python can't handle 
    4040        #   circular module imports :-( 
    4141        global cpg, _cphttptools 
    4242        from cherrypy import cpg, _cphttptools 
    43         cpg.threadData.virtualFilterOn = cpg.config.get('virtualHostFilter.on', False) 
    44         cpg.threadData.virtualFilterPrefix = cpg.config.get('virtualHostFilter.prefix', '/') 
    4543     
    46     def beforeRequestBody(self): 
    47         if not cpg.threadData.virtualFilterOn: 
     44        if not cpg.config.get('virtualHostFilter.on', False): 
    4845            return 
    4946         
    5047        domain = cpg.request.base.split('//')[1] 
    5148        # Re-use "mapPathToObject" function to find the actual objectPath 
    52         virtualPath = cpg.threadData.virtualFilterPrefix + cpg.request.path 
     49        prefix = cpg.config.get('virtualHostFilter.prefix', '/') 
     50        virtualPath = prefix + cpg.request.path 
    5351        c, objectPathList, v = _cphttptools.mapPathToObject(virtualPath) 
    5452        cpg.request.objectPath = '/' + '/'.join(objectPathList[1:]) 
  • trunk/cherrypy/lib/filter/xmlrpcfilter.py

    r229 r230  
    125125    """ 
    126126     
    127     def onStartResource(self): 
    128         # We have to dynamically import cpg because Python can't handle 
    129         #   circular module imports :-( 
    130         global cpg 
    131         from cherrypy import cpg 
    132         cpg.threadData.xmlRpcFilterOn = cpg.config.get('xmlRpcFilter.on', False) 
    133      
    134127    def testValidityOfRequest(self): 
    135128        # test if the content-length was sent 
     
    141134    def beforeRequestBody(self): 
    142135        """ Called after the request header has been read/parsed""" 
     136        # We have to dynamically import cpg because Python can't handle 
     137        #   circular module imports :-( 
     138        global cpg 
     139        from cherrypy import cpg 
     140        cpg.threadData.xmlRpcFilterOn = cpg.config.get('xmlRpcFilter.on', False) 
     141         
    143142        if not cpg.threadData.xmlRpcFilterOn: 
    144143            return True 
  • trunk/cherrypy/test/testCore.py

    r229 r230  
    112112     
    113113    def page_http_1_1(self): 
    114         cpg.response.headerMap["Content-Length"] = 5 
     114        cpg.response.headerMap["Content-Length"] = 39 
    115115        def inner(): 
    116116            yield "hello" 
    117117            raise ValueError 
     118            yield "oops" 
    118119        return inner() 
    119120 

Hosted by WebFaction

Log in as guest/cpguest to create tickets