Changeset 230
- Timestamp:
- 06/02/05 13:55:24
- Files:
-
- trunk/cherrypy/_cphttptools.py (modified) (1 diff)
- trunk/cherrypy/_cpwsgi.py (modified) (1 diff)
- trunk/cherrypy/lib/filter/baseurlfilter.py (modified) (1 diff)
- trunk/cherrypy/lib/filter/cachefilter.py (modified) (2 diffs)
- trunk/cherrypy/lib/filter/decodingfilter.py (modified) (1 diff)
- trunk/cherrypy/lib/filter/encodingfilter.py (modified) (2 diffs)
- trunk/cherrypy/lib/filter/gzipfilter.py (modified) (3 diffs)
- trunk/cherrypy/lib/filter/logdebuginfofilter.py (modified) (3 diffs)
- trunk/cherrypy/lib/filter/sessionfilter.py (modified) (3 diffs)
- trunk/cherrypy/lib/filter/staticfilter.py (modified) (1 diff)
- trunk/cherrypy/lib/filter/tidyfilter.py (modified) (6 diffs)
- trunk/cherrypy/lib/filter/virtualhostfilter.py (modified) (1 diff)
- trunk/cherrypy/lib/filter/xmlrpcfilter.py (modified) (2 diffs)
- trunk/cherrypy/test/testCore.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cphttptools.py
r229 r230 98 98 def run(self): 99 99 try: 100 self.processRequestHeaders()101 102 100 try: 103 # onStart has to be after processRequestHeaders104 # so that "path", for example, gets set.105 101 applyFilters('onStartResource') 106 102 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 117 118 finally: 118 119 applyFilters('onEndResource') 119 except basefilter.RequestHandled:120 pass121 120 except: 122 121 handleError(sys.exc_info()) trunk/cherrypy/_cpwsgi.py
r229 r230 207 207 self.wfile.write(data) 208 208 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 209 220 def handleError(self, exc): 210 221 self.close_connection = 1 trunk/cherrypy/lib/filter/baseurlfilter.py
r229 r230 35 35 """ 36 36 37 def onStartResource(self):37 def beforeRequestBody(self): 38 38 # We have to dynamically import cpg because Python can't handle 39 39 # circular module imports :-( 40 40 global cpg 41 41 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): 48 44 return 49 45 50 newBaseUrl = cpg. threadData.baseUrlFilterBaseUrl51 if cpg. threadData.baseUrlFilterUseXForwardedHost:46 newBaseUrl = cpg.config.get('baseUrlFilter.baseUrl', 'http://localhost') 47 if cpg.config.get('baseUrlFilter.useXForwardedHost', True): 52 48 newBaseUrl = cpg.request.headerMap.get("X-Forwarded-Host", newBaseUrl) 53 49 trunk/cherrypy/lib/filter/cachefilter.py
r229 r230 126 126 self.maxobjects = maxobjects 127 127 128 def onStartResource(self):128 def beforeMain(self): 129 129 # We have to dynamically import cpg because Python can't handle 130 130 # circular module imports :-( 131 131 global cpg 132 132 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'): 135 137 cpg._cache = self.CacheClass(self.key, self.delay, 136 138 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 return142 139 143 140 cacheData = cpg._cache.get() … … 162 159 def onEndResource(self): 163 160 """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): 165 162 return 166 163 trunk/cherrypy/lib/filter/decodingfilter.py
r229 r230 32 32 """Automatically decodes request parameters (except uploads).""" 33 33 34 def onStartResource(self):34 def beforeMain(self): 35 35 # We have to dynamically import cpg because Python can't handle 36 36 # circular module imports :-( 37 37 global cpg 38 38 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')41 39 42 def beforeMain(self): 43 if not cpg.threadData.decodingFilterOn: 40 if not cpg.config.get('decodingFilter.on', False): 44 41 return 45 42 46 enc = cpg. threadData.decodingFilterEncoding43 enc = cpg.config.get('decodingFilter.encoding', 'utf-8') 47 44 for key, value in cpg.request.paramMap.items(): 48 45 if cpg.request.filenameMap.get(key): trunk/cherrypy/lib/filter/encodingfilter.py
r229 r230 32 32 """Filter that automatically encodes the response.""" 33 33 34 def onStartResource(self):34 def beforeFinalize(self): 35 35 # We have to dynamically import cpg because Python can't handle 36 36 # circular module imports :-( 37 37 global cpg 38 38 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): 45 41 return 46 42 … … 48 44 if contentType: 49 45 ctlist = contentType.split(';')[0] 50 if (ctlist in cpg. threadData.encodingFilterMimeTypeList):51 enc = cpg. threadData.encodingFilterEncoding46 if (ctlist in cpg.config.get('encodingFilter.mimeTypeList', ['text/html'])): 47 enc = cpg.config.get('encodingFilter.encoding', 'utf-8') 52 48 53 49 # Add "charset=..." to response Content-Type header trunk/cherrypy/lib/filter/gzipfilter.py
r229 r230 35 35 """Filter that gzips the response.""" 36 36 37 def onStartResource(self):37 def beforeFinalize(self): 38 38 # We have to dynamically import cpg because Python can't handle 39 39 # circular module imports :-( 40 40 global cpg 41 41 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): 48 43 return 49 44 … … 54 49 ct = cpg.response.headerMap.get('Content-Type').split(';')[0] 55 50 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)): 57 53 cpg.response.headerMap['Content-Encoding'] = 'gzip' 58 54 # 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 61 58 def write_gzip_header(self): 62 59 """Adapted from the gzip.py standard module code""" … … 69 66 header += '\377' 70 67 return header 71 68 72 69 def write_gzip_trailer(self, crc, size): 73 70 footer = struct.pack("<l", crc) 74 71 footer += struct.pack("<L", size & 0xFFFFFFFFL) 75 72 return footer 76 77 def zip_body(self, body ):73 74 def zip_body(self, body, compress_level): 78 75 # Compress page 79 76 yield self.write_gzip_header() 80 77 crc = zlib.crc32("") 81 78 size = 0 82 zobj = zlib.compressobj(c pg.threadData.gzipFilterCompressLevel,79 zobj = zlib.compressobj(compress_level, 83 80 zlib.DEFLATED, -zlib.MAX_WBITS, 84 81 zlib.DEF_MEM_LEVEL, 0) trunk/cherrypy/lib/filter/logdebuginfofilter.py
r229 r230 33 33 """Filter that adds debug information to the page""" 34 34 35 def onStartResource(self):35 def beforeMain(self): 36 36 # We have to dynamically import cpg because Python can't handle 37 37 # circular module imports :-( 38 38 global cpg 39 39 from cherrypy import cpg 40 cpg.request.startBuilTime = time.time() 41 42 def beforeFinalize(self): 40 43 if cpg.config.get('server.environment') == 'dev': 41 44 # In "dev" environment, log everything by default … … 44 47 defaultOn = False 45 48 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): 55 50 return 56 51 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']) 63 53 ct = cpg.response.headerMap.get('Content-Type').split(';')[0] 64 if ct in cpg.threadData.logDebugInfoFilterMimeTypeList:54 if ct in mimelist: 65 55 body = ''.join(cpg.response.body) 66 56 debuginfo = '\n' 67 if cpg.threadData.logDebugInfoFilterLogAsComment: 57 58 logAsComment = cpg.config.get('logDebugInfoFilter.logAsComment', False) 59 if logAsComment: 68 60 debuginfo += '<!-- ' 69 61 else: 70 62 debuginfo += "<br/><br/>" 71 63 logList = [] 72 if cpg.threadData.logDebugInfoFilterLogBuildTime: 64 65 if cpg.config.get('logDebugInfoFilter.logBuildTime', True): 73 66 logList.append("Build time: %.03fs" % ( 74 67 time.time() - cpg.request.startBuilTime)) 75 if cpg.threadData.logDebugInfoFilterLogPageSize: 68 69 if cpg.config.get('logDebugInfoFilter.logPageSize', True): 76 70 logList.append("Page size: %.02fKB" % ( 77 71 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')): 80 75 # Pickle session data to get its size 81 76 try: … … 90 85 91 86 debuginfo += ', '.join(logList) 92 if cpg.threadData.logDebugInfoFilterLogAsComment:87 if logAsComment: 93 88 debuginfo += '-->' 94 89 trunk/cherrypy/lib/filter/sessionfilter.py
r229 r230 29 29 from basefilter import BaseFilter 30 30 import random, sha, string, time 31 alphanum = string.letters + string.digits 31 32 32 33 33 34 class SessionFilter(BaseFilter): 34 35 35 def onStartResource(self):36 def beforeMain(self): 36 37 # We have to dynamically import cpg because Python can't handle 37 38 # circular module imports :-( … … 39 40 from cherrypy import cpg, _cputil 40 41 cpg.threadData.sessionFilterOn = bool(cpg.config.get('session.storageType')) 41 42 def beforeMain(self): 42 43 43 if cpg.threadData.sessionFilterOn: 44 44 cleanupSessionData() … … 87 87 88 88 def 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)]) 92 90 s += '%s' % time.time() 93 91 return sha.sha(s).hexdigest() trunk/cherrypy/lib/filter/staticfilter.py
r229 r230 34 34 """Filter that handles static content.""" 35 35 36 def onStartResource(self):36 def beforeMain(self): 37 37 # We have to dynamically import cpg because Python can't handle 38 38 # circular module imports :-( 39 39 global cpg, _cphttptools, cperror 40 40 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): 50 43 return 51 44 52 if cpg.threadData.staticFilterFile:53 filename = cpg.threadData.staticFilterFile54 else:55 section = cpg. threadData.staticFilterConfigSection45 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) 56 49 extraPath = cpg.request.path[len(section) + 1:] 57 filename = os.path.join(cpg.threadData.staticFilterDir, 58 extraPath) 50 filename = os.path.join(staticDir, extraPath) 59 51 60 52 # Serve filename trunk/cherrypy/lib/filter/tidyfilter.py
r229 r230 39 39 """ 40 40 41 def onStartResource(self):41 def beforeFinalize(self): 42 42 # We have to dynamically import cpg because Python can't handle 43 43 # circular module imports :-( 44 44 global cpg 45 45 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): 54 48 return 55 49 … … 61 55 ct = cpg.response.headerMap.get('Content-Type', '').split(';')[0] 62 56 if ct == 'text/html': 63 tmpdir = cpg. threadData.tifyFilterTmpDir57 tmpdir = cpg.config.get('tidyFilter.tmpDir') 64 58 pageFile = os.path.join(tmpdir, 'page.html') 65 59 outFile = os.path.join(tmpdir, 'tidy.out') … … 75 69 if encoding: 76 70 encoding = '-' + encoding 71 77 72 strictXml = "" 78 if cpg. threadData.tidyFilterStrictXml:73 if cpg.config.get('tidyFilter.strictXml', False): 79 74 strictXml = ' -xml' 80 75 os.system('"%s" %s%s -f %s -o %s %s' % 81 (cpg. threadData.tifyFilterTidyPath, encoding,76 (cpg.config.get('tidyFilter.tidyPath'), encoding, 82 77 strictXml, errFile, outFile, pageFile)) 83 78 f = open(errFile, 'rb') … … 90 85 if (err.find('Warning') != -1 or err.find('Error') != -1): 91 86 ignore = 0 92 for errIgn in cpg. threadData.tidyFilterErrorsToIgnore:87 for errIgn in cpg.config.get('encodingFilter.errorsToIgnore', []): 93 88 if err.find(errIgn) != -1: 94 89 ignore = 1 95 90 break 96 if not ignore: newErrList.append(err) 91 if not ignore: 92 newErrList.append(err) 97 93 98 94 if newErrList: 99 95 newBody = "Wrong HTML:<br>" + cgi.escape('\n'.join(newErrList)).replace('\n','<br>') 100 96 newBody += '<br><br>' 101 i =097 i = 0 102 98 for line in originalBody.splitlines(): 103 99 i += 1 … … 106 102 cpg.response.body = [newBody] 107 103 108 elif cpg.threadData.tidyFilterStrictXml:104 elif strictXml: 109 105 # The HTML is OK, but is it valid XML 110 106 # Use elementtree to parse XML … … 121 117 newBody = "Wrong XML:<br>" + cgi.escape(bodyFile.getvalue().replace('\n','<br>')) 122 118 newBody += '<br><br>' 123 i =0119 i = 0 124 120 for line in originalBody.splitlines(): 125 121 i += 1 trunk/cherrypy/lib/filter/virtualhostfilter.py
r229 r230 36 36 """ 37 37 38 def onStartResource(self):38 def beforeRequestBody(self): 39 39 # We have to dynamically import cpg because Python can't handle 40 40 # circular module imports :-( 41 41 global cpg, _cphttptools 42 42 from cherrypy import cpg, _cphttptools 43 cpg.threadData.virtualFilterOn = cpg.config.get('virtualHostFilter.on', False)44 cpg.threadData.virtualFilterPrefix = cpg.config.get('virtualHostFilter.prefix', '/')45 43 46 def beforeRequestBody(self): 47 if not cpg.threadData.virtualFilterOn: 44 if not cpg.config.get('virtualHostFilter.on', False): 48 45 return 49 46 50 47 domain = cpg.request.base.split('//')[1] 51 48 # 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 53 51 c, objectPathList, v = _cphttptools.mapPathToObject(virtualPath) 54 52 cpg.request.objectPath = '/' + '/'.join(objectPathList[1:]) trunk/cherrypy/lib/filter/xmlrpcfilter.py
r229 r230 125 125 """ 126 126 127 def onStartResource(self):128 # We have to dynamically import cpg because Python can't handle129 # circular module imports :-(130 global cpg131 from cherrypy import cpg132 cpg.threadData.xmlRpcFilterOn = cpg.config.get('xmlRpcFilter.on', False)133 134 127 def testValidityOfRequest(self): 135 128 # test if the content-length was sent … … 141 134 def beforeRequestBody(self): 142 135 """ 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 143 142 if not cpg.threadData.xmlRpcFilterOn: 144 143 return True trunk/cherrypy/test/testCore.py
r229 r230 112 112 113 113 def page_http_1_1(self): 114 cpg.response.headerMap["Content-Length"] = 5114 cpg.response.headerMap["Content-Length"] = 39 115 115 def inner(): 116 116 yield "hello" 117 117 raise ValueError 118 yield "oops" 118 119 return inner() 119 120

