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

Changeset 527

Show
Ignore:
Timestamp:
08/10/05 18:46:29
Author:
fumanchu
Message:

1. First stage of new access.log (ticket #257). Access log must be enabled in config. Old log not affected (yet).
2. New tests for both logs.
3. New "debug" messages in _cphttptools.
4. Bugfix: staticfilter path when "global".
5. Bugfix: test suite was not honoring --1.1 flag.

Files:

Legend:

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

    r452 r527  
    7070 
    7171def log(msg, context='', severity=0): 
    72     """Syntactic sugar for writing to the log.""" 
     72    """Syntactic sugar for writing to the (error) log.""" 
     73    # Load _cputil lazily to avoid circular references, and 
     74    # to allow profiler and coverage tools to work on it. 
    7375    import _cputil 
    7476    logfunc = _cputil.getSpecialAttribute('_cpLogMessage') 
  • trunk/cherrypy/_cphttptools.py

    r525 r527  
    240240            # HEAD requests MUST NOT return a message-body in the response. 
    241241            cherrypy.response.body = [] 
     242         
     243        _cputil.getSpecialAttribute("_cpLogAccess")() 
    242244     
    243245    def parseFirstLine(self): 
     
    562564     
    563565    if cherrypy.response.headerMap.get('Content-Length') is None: 
    564         if cherrypy.request.version < "1.1": 
     566        if (cherrypy.request.version < "1.1" or 
     567            # OPTIONS requests MUST include a Content-Length of 0 if no body. 
     568            # Just punt and figure len for all OPTIONS requests. 
     569            cherrypy.request.method == "OPTIONS"): 
     570             
    565571            content = ''.join([chunk for chunk in cherrypy.response.body]) 
    566572            cherrypy.response.body = [content] 
     
    659665        stat = os.stat(filename) 
    660666    except OSError: 
     667        if getattr(cherrypy, "debug", None): 
     668            cherrypy.log("    NOT FOUND file: %s" % filename, "DEBUG") 
    661669        raise cherrypy.NotFound(cherrypy.request.path) 
    662670     
     
    677685            cherrypy.response.status = "304 Not Modified" 
    678686            cherrypy.response.body = [] 
     687            if getattr(cherrypy, "debug", None): 
     688                cherrypy.log("    Found file (304 Not Modified): %s" % filename, "DEBUG") 
    679689            return 
    680690    cherrypy.response.headerMap['Last-Modified'] = strModifTime 
     
    685695    bodyfile = open(filename, 'rb') 
    686696    cherrypy.response.body = fileGenerator(bodyfile) 
     697    if getattr(cherrypy, "debug", None): 
     698        cherrypy.log("    Found file: %s" % filename, "DEBUG") 
    687699 
    688700 
     
    700712        objname = objname.replace('.', '_') 
    701713        if getattr(cherrypy, "debug", None): 
    702             print "Attempting to call method: %s.%s" % (root, objname
     714            cherrypy.log("    Trying: %s.%s" % (root, objname), "DEBUG"
    703715        root = getattr(root, objname, None) 
    704716        if root is None: 
     
    738750     
    739751    if getattr(cherrypy, "debug", None): 
    740         print "Attempting to map path: %s" % tpath 
    741         print "    objectPathList: %s" % objectPathList 
     752        cherrypy.log("  Attempting to map path: %s using %s" 
     753                     % (tpath, objectPathList), "DEBUG") 
    742754     
    743755    # Try successive objects... (and also keep the remaining object list) 
     
    781793        else: 
    782794            # We didn't find anything 
     795            if getattr(cherrypy, "debug", None): 
     796                cherrypy.log("    NOT FOUND", "DEBUG") 
    783797            raise cherrypy.NotFound(path) 
    784798     
     
    791805            if cherrypy.request.queryString: 
    792806                newUrl += "?" + cherrypy.request.queryString 
     807            if getattr(cherrypy, "debug", None): 
     808                cherrypy.log("    Found: redirecting to %s" % newUrl, "DEBUG") 
    793809            raise cherrypy.HTTPRedirect(newUrl) 
    794810     
     811    if getattr(cherrypy, "debug", None): 
     812        cherrypy.log("    Found: %s" % candidate, "DEBUG") 
    795813    return candidate, objectPathList, virtualPathList 
    796814 
  • trunk/cherrypy/_cputil.py

    r522 r527  
    102102                                 % repr(name)) 
    103103 
     104 
     105def logtime(): 
     106    return '%04d/%02d/%02d %02d:%02d:%02d' % time.localtime(time.time())[:6] 
     107 
     108def _cpLogAccess(): 
     109    """ Default method for logging access """ 
     110     
     111    tmpl = '%(h)s %(l)s %(u)s [%(t)s] "%(r)s" %(s)s %(b)s' 
     112    s = tmpl % {'h': cherrypy.request.remoteHost, 
     113                'l': '-', 
     114                'u': getattr(cherrypy.request, "login", None) or "-", 
     115                't': logtime(), 
     116                'r': cherrypy.request.requestLine, 
     117                's': cherrypy.response.status.split(" ", 1)[0], 
     118                'b': cherrypy.response.headerMap.get('Content-Length', '') or "-", 
     119                } 
     120     
     121    if cherrypy.config.get('server.logToScreen', True): 
     122        print s 
     123     
     124    fname = cherrypy.config.get('server.logAccessFile', '') 
     125    if fname: 
     126        f = open(fname, 'ab') 
     127        f.write(s + '\n') 
     128        f.close() 
     129 
     130 
     131_log_severity_levels = {0: "INFO", 1: "WARNING", 2: "ERROR"} 
     132 
    104133def _cpLogMessage(msg, context = '', severity = 0): 
    105     """ Default method for logging messages """ 
    106      
    107     nowTuple = time.localtime(time.time()) 
    108     nowStr = '%04d/%02d/%02d %02d:%02d:%02d' % (nowTuple[:6]) 
    109     if severity == 0: 
    110         level = "INFO" 
    111     elif severity == 1: 
    112         level = "WARNING" 
    113     elif severity == 2: 
    114         level = "ERROR" 
    115     else: 
    116         level = "UNKNOWN" 
    117     try: 
    118         logToScreen = cherrypy.config.get('server.logToScreen') 
    119     except: 
    120         logToScreen = True 
    121     s = nowStr + ' ' + context + ' ' + level + ' ' + msg 
    122     if logToScreen: 
     134    """ Default method for logging messages (error log)""" 
     135     
     136    level = _log_severity_levels.get(severity, "UNKNOWN") 
     137    s = logtime() + ' ' + context + ' ' + level + ' ' + msg 
     138     
     139    if cherrypy.config.get('server.logToScreen', True): 
    123140        print s 
    124     if cherrypy.config.get('server.logFile'): 
    125         f = open(cherrypy.config.get('server.logFile'), 'ab') 
     141     
     142    fname = cherrypy.config.get('server.logFile', '') 
     143    if fname: 
     144        f = open(fname, 'ab') 
    126145        f.write(s + '\n') 
    127146        f.close() 
  • trunk/cherrypy/lib/filter/staticfilter.py

    r519 r527  
    5151            staticDir = config.get('staticFilter.dir') 
    5252            section = config.get('staticFilter.dir', returnSection=True) 
     53            if section == 'global': 
     54                section = "/" 
    5355            section = section.rstrip(r"\/") 
    5456            extraPath = request.path[len(section) + 1:] 
  • trunk/cherrypy/test/test.py

    r522 r527  
    9494            elif o == "--profile": 
    9595                self.profile = True 
    96             elif o == "-1.1": 
     96            elif o == "--1.1": 
    9797                self.protocol = "HTTP/1.1" 
    9898            elif o == "--basedir": 
  • trunk/cherrypy/test/test_core.py

    r523 r527  
    3131import cherrypy 
    3232import types 
     33import os 
     34localDir = os.path.dirname(__file__) 
     35 
    3336 
    3437class Root: 
     
    223226 
    224227 
     228logFile = os.path.join(localDir, "error.log") 
     229logAccessFile = os.path.join(localDir, "access.log") 
     230 
    225231cherrypy.config.update({ 
    226232    'global': {'server.logToScreen': False, 
    227233               'server.environment': 'production', 
     234               'server.protocolVersion': "HTTP/1.1", 
    228235               }, 
    229236    '/': { 
     
    239246        'bax': 'this4', 
    240247    }, 
     248    '/flatten': { 
     249        'server.logFile': logFile, 
     250        'server.logAccessFile': logAccessFile, 
     251    }, 
    241252}) 
    242253 
     
    245256 
    246257import helper 
    247 import os 
    248258 
    249259class CoreRequestHandlingTest(helper.CPWebCase): 
     
    294304        self.assertBody('hello' + (" " * 508)) 
    295305        self.assertStatus('500 Internal error') 
     306     
     307    def testLogging(self): 
     308        open(logFile, "wb").write("") 
     309        open(logAccessFile, "wb").write("") 
     310         
     311        self.getPage("/flatten/as_string") 
     312        self.assertBody('content') 
     313        self.assertStatus('200 OK') 
     314         
     315        self.getPage("/flatten/as_yield") 
     316        self.assertBody('content') 
     317        self.assertStatus('200 OK') 
     318         
     319        data = open(logFile, "rb").readlines() 
     320        self.assertEqual(data[0][-55:], ' HTTP INFO 127.0.0.1 - GET /flatten/as_string HTTP/1.1\n') 
     321        self.assertEqual(data[1][-54:], ' HTTP INFO 127.0.0.1 - GET /flatten/as_yield HTTP/1.1\n') 
     322         
     323        data = open(logAccessFile, "rb").readlines() 
     324        self.assertEqual(data[0][:15], '127.0.0.1 - - [') 
     325        haslength = False 
     326        for k, v in self.headers: 
     327            if k.lower() == 'content-length': 
     328                haslength = True 
     329        if haslength: 
     330            self.assertEqual(data[0][-42:], '] "GET /flatten/as_string HTTP/1.1" 200 7\n') 
     331        else: 
     332            self.assertEqual(data[0][-42:], '] "GET /flatten/as_string HTTP/1.1" 200 -\n') 
     333         
     334        self.assertEqual(data[1][:15], '127.0.0.1 - - [') 
     335        haslength = False 
     336        for k, v in self.headers: 
     337            if k.lower() == 'content-length': 
     338                haslength = True 
     339        if haslength: 
     340            self.assertEqual(data[1][-41:], '] "GET /flatten/as_yield HTTP/1.1" 200 7\n') 
     341        else: 
     342            self.assertEqual(data[1][-41:], '] "GET /flatten/as_yield HTTP/1.1" 200 -\n') 
    296343     
    297344    def testRedirect(self): 
     
    442489    def testFavicon(self): 
    443490        # Calls to favicon.ico are special-cased in _cphttptools. 
    444         localDir = os.path.dirname(__file__) 
    445491        icofilename = os.path.join(localDir, "../favicon.ico") 
    446492        icofile = open(icofilename, "rb") 
  • trunk/cherrypy/test/test_static_filter.py

    r519 r527  
    6262        # This should resolve relative to cherrypy.root.__module__. 
    6363        self.getPage("/static/index.html") 
     64        self.assertStatus('200 OK') 
    6465        self.assertHeader('Content-Type', 'text/html') 
    6566        self.assertBody('Hello, world\r\n') 
     
    6768        # Using a staticFilter.root value... 
    6869        self.getPage("/docroot/index.html") 
     70        self.assertStatus('200 OK') 
    6971        self.assertHeader('Content-Type', 'text/html') 
    7072        self.assertBody('Hello, world\r\n') 
     
    7274        # Check a filename with spaces in it 
    7375        self.getPage("/static/has%20space.html") 
     76        self.assertStatus('200 OK') 
    7477        self.assertHeader('Content-Type', 'text/html') 
    7578        self.assertBody('Hello, world\r\n') 
    7679         
    7780        self.getPage("/style.css") 
     81        self.assertStatus('200 OK') 
    7882        self.assertHeader('Content-Type', 'text/css') 
    7983        # Note: The body should be exactly 'Dummy stylesheet\n', but 

Hosted by WebFaction

Log in as guest/cpguest to create tickets