[Winopenrpg-developer] openrpg1/plugins/cherrypy/lib/filter __init__.py,NONE,1.1 basefilter.py,NONE,
Status: Inactive
Brought to you by:
digitalxero
|
From: Digital X. <dig...@us...> - 2006-01-26 17:33:28
|
Update of /cvsroot/winopenrpg/openrpg1/plugins/cherrypy/lib/filter In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv30930/plugins/cherrypy/lib/filter Added Files: __init__.py basefilter.py baseurlfilter.py cachefilter.py decodingfilter.py encodingfilter.py gzipfilter.py logdebuginfofilter.py tidyfilter.py virtualhostfilter.py xmlrpcfilter.py Log Message: Initial commit of OpenRPG++ python --- NEW FILE: xmlrpcfilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ ########################################################################## ## Remco Boerma ## ## History: ## 1.0.3 : 2005-01-28 Bugfix on content-length in 1.0.2 code fixed by ## Gian Paolo Ciceri ## 1.0.2 : 2005-01-26 changed infile dox based on ticket #97 ## 1.0.1 : 2005-01-26 Speedup due to generator usage in CP2. ## The result is now converted to a list with length 1. So the complete ## xmlrpc result is written at once, and not per character. Thanks to ## Gian Paolo Ciceri for reporting the slowdown. ## 1.0.0 : 2004-12-29 Released with CP2 ## 0.0.9 : 2004-12-23 made it CP2 #59 compatible (returns an iterable) ## Please note: as the xmlrpc doesn't know what you would want to return ## (and for the logic of marshalling) it will return Generator objects, as ## it is.. So it'll brake on that one!! ## NOTE: __don't try to return a Generator object to the caller__ ## You could of course handle the generator usage internally, before sending ## the result. This breaks from the general cherrypy way of handling generators... ## 0.0.8 : 2004-12-23 cpg.request.paramList should now be a filter. ## 0.0.7 : 2004-12-07 inserted in the experimental branch (all remco boerma till here) ## 0.0.6 : 2004-12-02 Converted basefilter to baseinputfileter,baseoutputfilter ## 0.0.5 : 2004-11-22 "RPC2/" now changed to "/RPC2/" with the new mapping function ## Gian paolo ciceri notified me with the lack of passing parameters. ## Thanks Gian, it's now implemented against the latest trunk. ## Gian also came up with the idea of lazy content-type checking: if it's sent ## as a header, it should be 'text/xml', if not sent at all, it should be ## accepted. (While this it not the xml/rpc standard, it's handy for those ## xml-rpc client implementations wich don't send this header) ## 0.0.4 : 2004-11-20 in setting the path, the dot is replaces by a slash ## therefore the regular CP2 routines knows how to handle things, as ## dots are not allowed in object names, it's varely easily adopted. ## Path + method handling. The default path is 'RPC2', this one is ## stripped. In case of path 'someurl' it is used for 'someurl' + method ## and 'someurl/someotherurl' is mapped to someurl.someotherurl + method. ## this way python serverproxies initialised with an url other than ## just the host are handled well. I don't hope any other service would map ## it to 'RPC2/someurl/someotherurl', cause then it would break i think. . ## 0.0.3 : 2004-11-19 changed some examples (includes error checking ## wich returns marshalled Fault objects if the request is an RPC call. ## took testing code form afterRequestHeader and put it in ## testValidityOfRequest to make things a little simpler. ## simply log the requested function with parameters to stdout ## 0.0.2 : 2004-11-19 the required cgi.py patch is no longer needed ## (thanks remi for noticing). Webbased calls to regular objects ## are now possible again ;) so it's no longer a dedicated xmlrpc ## server. The test script is also in a ready to run file named ## testRPC.py along with the test server: filterExample.py ## 0.0.1 : 2004-11-19 informing the public, dropping loads of useless ## tests and debugging ## 0.0.0 : 2004-11-19 initial alpha ## ##--------------------------------------------------------------------- ## ## EXAMPLE CODE FOR THE SERVER: ## from cherrypy.lib.filter.xmlrpcfilter import XmlRpcFilter ## from cherrypy import cpg ## ## class Root: ## _cpFilterList = [XmlRpcFilter()] ## ## def longString(self,s,times): ## return s*times ## longString.exposed = True ## ## cpg.root = Root() ## if __name__=='__main__': ## cpg.server.start(configMap = {'socketPort': 9001, ## 'threadPool':0, ## 'socketQueueSize':10 }) ## EXAMPLE CODE FOR THE CLIENT: ## >>> import xmlrpclib ## >>> server = xmlrpclib.ServerProxy('http://localhost:9001') ## >>> assert server.longString('abc',3) == 'abcabcabc' ## >>> ###################################################################### from basefilter import BaseInputFilter, BaseOutputFilter from cherrypy import cpg import xmlrpclib class XmlRpcFilter(BaseInputFilter,BaseOutputFilter): """ Derivative of basefilter. Test to convert XMLRPC to CherryPy2 object system and reverse PLEASE NOTE: afterRequestHeader: Unmarshalls the posted data to a methodname and parameters. - These are stored in cpg.request.rpcMethod and cpg.request.rpcParams - The method is also stored in cpg.request.path, so CP2 will find the right method to call for you. Based on the root's position beforeResponse: Marshalls the result of the excecuted function (in cpg.response.body) to xmlrpc. - Until resolved: the result must be a python souce string with the results, this string is 'eval'ed to return the results. This will be resolved in the future. - the Content-Type and -Length are set according to the new (marshalled) data. """ def testValidityOfRequest(self): # test if the content-length was sent result = int(cpg.request.headerMap.get('Content-Length',0)) > 0 result = result and cpg.request.headerMap.get('Content-Type','text/xml').lower() in ['text/xml'] return result def afterRequestHeader(self): """ Called after the request header has been read/parsed""" cpg.request.isRPC = self.testValidityOfRequest() if not cpg.request.isRPC: # used for debugging or more info # print 'not a valid xmlrpc call' return # break this if it's not for this filter!! # used for debugging, or more info: # print "xmlrpcmethod...", cpg.request.parsePostData = 0 dataLength = int(cpg.request.headerMap.get('Content-Length',0)) data = cpg.request.rfile.read(dataLength) try: params, method = xmlrpclib.loads(data) except Exception,e: params, method = ('ERROR PARAMS',),'ERRORMETHOD' cpg.request.rpcMethod, cpg.request.rpcParams = method,params # patch the path. .there are only a few options: # - 'RPC2' + method >> method # - 'someurl' + method >> someurl.method # - 'someurl/someother' + method >> someurl.someother.method if not cpg.request.path.endswith('/'): cpg.request.path+='/' if cpg.request.path.startswith('/RPC2/'): cpg.request.path=cpg.request.path[5:] ## strip the irst /rpc2 cpg.request.path+=str(method).replace('.','/') cpg.request.paramList = list(params) # used for debugging and more info # print "XMLRPC Filter: calling '%s' with args: '%s' " % (cpg.request.path,params) def beforeResponse(self): """ Called before starting to write response """ if not cpg.request.isRPC: return # it's not an RPC call, so just let it go with the normal flow try: cpg.response.body = [xmlrpclib.dumps((cpg.response.body[0],), methodresponse=1,allow_none=1)] except xmlrpclib.Fault,fault: cpg.response.body = xmlrpclib.dumps(fault,allow_none=1) except Exception,e: print 'EXCEPTION: ',e cpg.response.headerMap['Content-Type']='text/xml' try: cpg.response.headerMap['Content-Length']=`len(cpg.response.body[0])` except TypeError: # 1.0.3 : in case of an error, cpg.response.body is unscriptable pass --- NEW FILE: gzipfilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import zlib import struct import time from basefilter import BaseOutputFilter from cherrypy import cpg class GzipFilter(BaseOutputFilter): """ Filter that gzips the response. """ def __init__(self, mimeTypeList = ['text/html'], compresslevel=9): # List of mime-types to compress self.mimeTypeList = mimeTypeList self.compresslevel = compresslevel def beforeResponse(self): if not cpg.response.body: # Response body is empty (might be a 304 for instance) return ct = cpg.response.headerMap.get('Content-Type').split(';')[0] ae = cpg.request.headerMap.get('Accept-Encoding', '') if (ct in self.mimeTypeList) and ('gzip' in ae): # Set header cpg.response.headerMap['Content-Encoding'] = 'gzip' # Return a generator that compresses the page cpg.response.body = self.zip_body(cpg.response.body) def write_gzip_header(self): """ Adapted from the gzip.py standard module code """ header = '\037\213' # magic header header += '\010' # compression method header += '\0' header += struct.pack("<L", long(time.time())) header += '\002' header += '\377' return header def write_gzip_trailer(self, crc, size): footer = struct.pack("<l", crc) footer += struct.pack("<L", size & 0xFFFFFFFFL) return footer def zip_body(self, body): # Compress page yield self.write_gzip_header() crc = zlib.crc32("") size = 0 zobj = zlib.compressobj(self.compresslevel, zlib.DEFLATED, -zlib.MAX_WBITS, zlib.DEF_MEM_LEVEL, 0) for line in body: size += len(line) crc = zlib.crc32(line, crc) yield zobj.compress(line) yield zobj.flush() yield self.write_gzip_trailer(crc, size) --- NEW FILE: cachefilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import threading import Queue import time import cStringIO from basefilter import BaseInputFilter, RequestHandled from cherrypy import cpg def defaultCacheKey(): return cpg.request.browserUrl class Tee: """ Wraps a stream object; chains the content that is written and keep a copy in a StringIO for caching purposes. """ def __init__(self, wfile, maxobjsize): self.wfile = wfile self.cache = cStringIO.StringIO() self.maxobjsize = maxobjsize self.caching = True self.size = 0 def write(self, s): self.wfile.write(s) if self.caching: self.size += len(s) if self.size < self.maxobjsize: self.cache.write(s) else: # exceeded the limit, aborts caching self.stopCaching() def flush(self): self.wfile.flush() def close(self): self.wfile.close() if self.caching: self.stopCaching() def stopCaching(self): self.caching = False self.cache.close() class MemoryCache: def __init__(self, key, delay, maxobjsize, maxsize, maxobjects): self.key = key self.delay = delay self.maxobjsize = maxobjsize self.maxsize = maxsize self.maxobjects = maxobjects self.cursize = 0 self.cache = {} self.expirationQueue = Queue.Queue() self.expirationThread = threading.Thread(target=self.expireCache, name='expireCache') self.expirationThread.setDaemon(True) self.expirationThread.start() self.totPuts = 0 self.totGets = 0 self.totHits = 0 self.totExpires = 0 self.totNonModified = 0 def expireCache(self): while True: expirationTime, objSize, objKey = self.expirationQueue.get(block=True, timeout=None) while (time.time() < expirationTime): time.sleep(0.1) try: del self.cache[objKey] self.totExpires += 1 self.cursize -= objSize except KeyError: # the key may have been deleted elsewhere pass def get(self): """ If the content is in the cache, returns a tuple containing the expiration time, the lastModified response header and the object (rendered as a string); returns None if the key is not found. """ self.totGets += 1 cacheItem = self.cache.get(self.key(), None) if cacheItem: self.totHits += 1 return cacheItem else: return None def put(self, lastModified, obj): objSize = len(obj) totalSize = self.cursize + objSize # checks if there's space for the object if ((objSize < self.maxobjsize) and (totalSize < self.maxsize) and (len(self.cache) < self.maxobjects)): # add to the expirationQueue & cache try: expirationTime = time.time() + self.delay objKey = self.key() self.expirationQueue.put((expirationTime, objSize, objKey)) self.totPuts += 1 self.cursize += objSize except Queue.Full: # can't add because the queue is full return self.cache[objKey] = (expirationTime, lastModified, obj) class CacheInputFilter(BaseInputFilter): """ Works on the input chain. If the page is already stored in the cache serves the contents. If the page is not in the cache, it wraps the cpg.response.wfile object; in this way, everything that is written is recorded, independent if it was sent directly or not. """ def __init__( self, CacheClass=MemoryCache, key=defaultCacheKey, delay=600, # 10 minutes maxobjsize=100000, # 100 KB maxsize=10000000, # 10 MB maxobjects=1000 # 1000 objects ): cpg._cache = CacheClass(key, delay, maxobjsize, maxsize, maxobjects) def afterRequestBody(self): """ Checks if the page is already in the cache """ cacheData = cpg._cache.get() if cacheData: expirationTime, lastModified, obj = cacheData # found a hit! check the if-modified-since request header modifiedSince = cpg.request.headerMap.get('If-Modified-Since', None) #print "Cache hit: If-Modified-Since=%s, lastModified=%s" % (modifiedSince, lastModified) if modifiedSince == lastModified: cpg._cache.totNonModified += 1 # the code below was borrowed from the sendResponse function # it should be refactored & put into a function to allow reuse cpg.response.wfile.write('%s %s\r\n' % (cpg.configOption.protocolVersion, 304)) # the code below doesn't work because the data isn't available at this point... #cpg.response.wfile.write('%s: %s\r\n' % ('Date', cpg.request.headerMap['Date'])) # should the cache record & replay cookies it too? cpg.response.wfile.write('\r\n') raise RequestHandled else: # serve it & get out from the request cpg.response.wfile.write(obj) raise RequestHandled else: # sets a wrapper to cache the contents cpg.response.wfile = Tee(cpg.response.wfile, cpg._cache.maxobjsize) cpg.threadData.cacheable = True class CacheOutputFilter(object): """ Works on the output chain. Stores the content of the page in the cache. """ def beforeResponse(self): """ Checks if the page is cacheable; if not so disables the cache. Uses a flag that may be reset by intermediate filters. Note that the output filter is usually the last filter in the chain, so this method is probably the last one called before the response is written. """ if isinstance(cpg.response.wfile, Tee): if cpg.threadData.cacheable: return # cancel caching wrapper = cpg.response.wfile wrapper.stopCaching() cpg.response.wfile = wrapper.wfile def afterResponse(self): """ Close & fix the cache entry after content was fully written """ if isinstance(cpg.response.wfile, Tee): wrapper = cpg.response.wfile if wrapper.caching: if cpg.response.headerMap.get('Pragma', None) != 'no-cache': lastModified = cpg.response.headerMap.get('Last-Modified', None) # saves the cache data cpg._cache.put(lastModified, wrapper.cache.getvalue()) # closes the wrapper wrapper.stopCaching() cpg.response.wfile = wrapper.wfile def percentual(n,d): """calculates the percentual, dealing with div by zeros""" if d == 0: return 0 else: return (float(n)/float(d))*100 def formatSize(n): """formats a number as a memory size, in bytes, kbytes, MB, GB)""" if n < 1024: return "%4d bytes" % n elif n < 1024*1024: return "%4d kbytes" % (n / 1024) elif n < 1024*1024*1024: return "%4d MB" % (n / (1024*1024)) else: return "%4d GB" % (n / (1024*1024*1024)) class CacheStats: def index(self): cpg.response.headerMap['Content-Type'] = 'text/plain' cpg.response.headerMap['Pragma'] = 'no-cache' cache = cpg._cache yield "Cache statistics\n" yield "Maximum object size: %s\n" % formatSize(cache.maxobjsize) yield "Maximum cache size: %s\n" % formatSize(cache.maxsize) yield "Maximum number of objects: %d\n" % cache.maxobjects yield "Current cache size: %s\n" % formatSize(cache.cursize) yield "Approximated expiration queue size: %d\n" % cache.expirationQueue.qsize() yield "Number of cache entries: %d\n" % len(cache.cache) yield "Total cache writes: %d\n" % cache.totPuts yield "Total cache read attempts: %d\n" % cache.totGets yield "Total hits: %d (%1.2f%%)\n" % (cache.totHits, percentual(cache.totHits, cache.totGets)) yield "Total misses: %d (%1.2f%%)\n" % (cache.totGets-cache.totHits, percentual(cache.totGets-cache.totHits, cache.totGets)) yield "Total expires: %d\n" % cache.totExpires yield "Total non-modified content: %d\n" % cache.totNonModified index.exposed = True --- NEW FILE: __init__.py --- --- NEW FILE: baseurlfilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ from basefilter import BaseInputFilter from cherrypy import cpg class BaseUrlFilter(BaseInputFilter): """ Filter that changes the base URL. Useful when running a CP server behind Apache. """ def __init__(self, baseUrl = 'http://localhost', useXForwardedHost = True): # New baseUrl self.baseUrl = baseUrl self.useXForwardedHost = useXForwardedHost def afterRequestHeader(self): if self.useXForwardedHost: newBaseUrl = cpg.request.headerMap.get("X-Forwarded-Host", self.baseUrl) else: newBaseUrl = self.baseUrl if newBaseUrl.find("://") == -1: # add http:// or https:// if needed newBaseUrl = cpg.request.base[:cpg.request.base.find("://") + 3] + newBaseUrl cpg.request.browserUrl = cpg.request.browserUrl.replace( cpg.request.base, newBaseUrl) cpg.request.base = newBaseUrl --- NEW FILE: tidyfilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import os, cgi from basefilter import BaseOutputFilter from cherrypy import cpg class TidyFilter(BaseOutputFilter): """ Filter that runs the response through Tidy. Note that we use the standalone Tidy tool rather than the python mxTidy module. This is because this module doesn't seem to be stable and it crashes on some HTML pages (which means that the server would also crash) """ def __init__(self, tidyPath, tmpDir, errorsToIgnore = []): self.tidyPath = tidyPath self.tmpDir = tmpDir self.errorsToIgnore = errorsToIgnore def beforeResponse(self): # the tidy filter, by its very nature it's not generator friendly, # so we just collect the body and work with it. originalBody = ''.join(cpg.response.body) cpg.response.body = [originalBody] fct = cpg.response.headerMap.get('Content-Type', '') ct = fct.split(';')[0] if ct == 'text/html': pageFile = os.path.join(self.tmpDir, 'page.html') outFile = os.path.join(self.tmpDir, 'tidy.out') errFile = os.path.join(self.tmpDir, 'tidy.err') f = open(pageFile, 'wb') f.write(originalBody) f.close() encoding = '' i = fct.find('charset=') if i != -1: encoding = fct[i+8:] encoding = encoding.replace('utf-8', 'utf8') if encoding: encoding = '-' + encoding os.system('"%s" %s -f %s -o %s %s' % ( self.tidyPath, encoding, errFile, outFile, pageFile)) f = open(errFile, 'rb') err = f.read() f.close() errList = err.splitlines() newErrList = [] for err in errList: if (err.find('Warning') != -1 or err.find('Error') != -1): ignore = 0 for errIgn in self.errorsToIgnore: if err.find(errIgn) != -1: ignore = 1 break if not ignore: newErrList.append(err) if newErrList: newBody = "Wrong HTML:<br>" + cgi.escape('\n'.join(newErrList)).replace('\n','<br>') newBody += '<br><br>' i=0 for line in originalBody.splitlines(): i += 1 newBody += "%03d - "%i + cgi.escape(line).replace('\t',' ').replace(' ',' ') + '<br>' cpg.response.body = [newBody] --- NEW FILE: encodingfilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ from basefilter import BaseOutputFilter from cherrypy import cpg import types class EncodingFilter(BaseOutputFilter): """ Filter that automatically encodes the response. """ def __init__(self, encoding = 'utf-8', mimeTypeList = ['text/html']): self.encoding = encoding self.mimeTypeList = mimeTypeList def beforeResponse(self): contentType = cpg.response.headerMap.get("Content-Type") if contentType: ctlist = contentType.split(';')[0] if (ctlist in self.mimeTypeList): # Add "charset=..." to response Content-Type header if contentType and 'charset' not in contentType: cpg.response.headerMap["Content-Type"] += ";charset=%s" % self.encoding # Return a generator that encodes the sequence cpg.response.body = self.encode_body(cpg.response.body) def encode_body(self, body): for line in body: yield line.encode(self.encoding) --- NEW FILE: basefilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ class InternalRedirect(Exception): pass class RequestHandled(Exception): pass class BaseInputFilter(object): """ Base class for input filters. Derive new filter classes from this, then override some of the methods to add some side-effects. """ def afterRequestHeader(self): """ Called after the request header has been read/parsed""" pass def afterRequestBody(self): """ Called after the request body has been read/parsed""" pass class BaseOutputFilter(object): """ Base class for output filters. Derive new filter classes from this, then override some of the methods to add some side-effects. """ def beforeResponse(self): """ Called before starting to write response """ pass def afterResponse(self): """ Called after writing the response (header & body included) """ pass --- NEW FILE: decodingfilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ from basefilter import BaseInputFilter from cherrypy import cpg import types class DecodingFilter(BaseInputFilter): """ Filter that automatically decodes the request parameters (except files being uploaded). """ def __init__(self, encoding = 'utf-8'): self.encoding = encoding def afterRequestBody(self): for key, value in cpg.request.paramMap.items(): if key in cpg.request.filenameMap: # This is a file being uploaded: skip it continue if isinstance(value, list): # value is a list: decode each element newValue = [v.decode(self.encoding) for v in value] else: # value is a regular string: decode it newValue = value.decode(self.encoding) cpg.request.paramMap[key] = newValue --- NEW FILE: virtualhostfilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import basefilter from cherrypy import cpg, _cphttptools class VirtualHostFilter(basefilter.BaseInputFilter): """ Filter that changes the ObjectPath based on the Host. Useful when running multiple sites within one CP server. See CherryPy recipes for the documentation. """ def __init__(self, siteMap, useXForwardedHost = True): self.siteMap = siteMap self.useXForwardedHost = useXForwardedHost def afterRequestHeader(self): domain = cpg.request.base.split('//')[1] if self.useXForwardedHost: domain = cpg.request.headerMap.get( "X-Forwarded-Host", domain) prefix = self.siteMap.get(domain) if prefix: # Re-use "mapPathToObject" function to find the actual # objectPath candidate, objectPathList, virtualPathList = \ _cphttptools.mapPathToObject( prefix + cpg.request.path ) cpg.request.objectPath = '/' + '/'.join(objectPathList[1:]) raise basefilter.InternalRedirect --- NEW FILE: logdebuginfofilter.py --- """ Copyright (c) 2004, CherryPy Team (te...@ch...) All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the CherryPy Team nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. """ import time, StringIO, pickle from basefilter import BaseInputFilter, BaseOutputFilter from cherrypy import cpg from itertools import chain class LogDebugInfoStartFilter(BaseInputFilter, BaseOutputFilter): """ Filter that adds debug information to the page """ def __init__(self, mimeTypeList = ['text/html'], preTag = '<br><br>', logBuildTime = True, logPageSize = True, logSessionSize = True, logAsComment = False): # List of mime-types to which this applies self.mimeTypeList = mimeTypeList self.preTag = preTag self.logBuildTime = logBuildTime self.logPageSize = logPageSize self.logSessionSize = logSessionSize self.logAsComment = logAsComment def afterRequestBody(self): cpg.request.startBuilTime = time.time() def beforeResponse(self): ct = cpg.response.headerMap.get('Content-Type') if (ct in self.mimeTypeList): debuginfo = '\n' if self.logAsComment: debuginfo += '<!-- ' else: debuginfo += self.preTag logList = [] if self.logBuildTime: logList.append("Build time: %.03fs" % ( time.time() - cpg.request.startBuilTime)) if self.logPageSize: logList.append("Page size: %.02fKB" % ( len(cpg.response.body)/float(1024))) if self.logSessionSize and cpg.configOption.sessionStorageType: # Pickle session data to get its size f = StringIO.StringIO() pickle.dump(cpg.request.sessionMap, f, 1) dumpStr = f.getvalue() f.close() logList.append("Session data size: %.02fKB" % ( len(dumpStr)/float(1024))) debuginfo += ', '.join(logList) if self.logAsComment: debuginfo += '-->' cpg.response.body = chain(cpg.response.body, [debuginfo]) |