Changeset 527
- Timestamp:
- 08/10/05 18:46:29
- Files:
-
- trunk/cherrypy/__init__.py (modified) (1 diff)
- trunk/cherrypy/_cphttptools.py (modified) (9 diffs)
- trunk/cherrypy/_cputil.py (modified) (1 diff)
- trunk/cherrypy/lib/filter/staticfilter.py (modified) (1 diff)
- trunk/cherrypy/test/test.py (modified) (1 diff)
- trunk/cherrypy/test/test_core.py (modified) (6 diffs)
- trunk/cherrypy/test/test_static_filter.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r452 r527 70 70 71 71 def 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. 73 75 import _cputil 74 76 logfunc = _cputil.getSpecialAttribute('_cpLogMessage') trunk/cherrypy/_cphttptools.py
r525 r527 240 240 # HEAD requests MUST NOT return a message-body in the response. 241 241 cherrypy.response.body = [] 242 243 _cputil.getSpecialAttribute("_cpLogAccess")() 242 244 243 245 def parseFirstLine(self): … … 562 564 563 565 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 565 571 content = ''.join([chunk for chunk in cherrypy.response.body]) 566 572 cherrypy.response.body = [content] … … 659 665 stat = os.stat(filename) 660 666 except OSError: 667 if getattr(cherrypy, "debug", None): 668 cherrypy.log(" NOT FOUND file: %s" % filename, "DEBUG") 661 669 raise cherrypy.NotFound(cherrypy.request.path) 662 670 … … 677 685 cherrypy.response.status = "304 Not Modified" 678 686 cherrypy.response.body = [] 687 if getattr(cherrypy, "debug", None): 688 cherrypy.log(" Found file (304 Not Modified): %s" % filename, "DEBUG") 679 689 return 680 690 cherrypy.response.headerMap['Last-Modified'] = strModifTime … … 685 695 bodyfile = open(filename, 'rb') 686 696 cherrypy.response.body = fileGenerator(bodyfile) 697 if getattr(cherrypy, "debug", None): 698 cherrypy.log(" Found file: %s" % filename, "DEBUG") 687 699 688 700 … … 700 712 objname = objname.replace('.', '_') 701 713 if getattr(cherrypy, "debug", None): 702 print "Attempting to call method: %s.%s" % (root, objname)714 cherrypy.log(" Trying: %s.%s" % (root, objname), "DEBUG") 703 715 root = getattr(root, objname, None) 704 716 if root is None: … … 738 750 739 751 if getattr(cherrypy, "debug", None): 740 print "Attempting to map path: %s" % tpath741 print " objectPathList: %s" % objectPathList752 cherrypy.log(" Attempting to map path: %s using %s" 753 % (tpath, objectPathList), "DEBUG") 742 754 743 755 # Try successive objects... (and also keep the remaining object list) … … 781 793 else: 782 794 # We didn't find anything 795 if getattr(cherrypy, "debug", None): 796 cherrypy.log(" NOT FOUND", "DEBUG") 783 797 raise cherrypy.NotFound(path) 784 798 … … 791 805 if cherrypy.request.queryString: 792 806 newUrl += "?" + cherrypy.request.queryString 807 if getattr(cherrypy, "debug", None): 808 cherrypy.log(" Found: redirecting to %s" % newUrl, "DEBUG") 793 809 raise cherrypy.HTTPRedirect(newUrl) 794 810 811 if getattr(cherrypy, "debug", None): 812 cherrypy.log(" Found: %s" % candidate, "DEBUG") 795 813 return candidate, objectPathList, virtualPathList 796 814 trunk/cherrypy/_cputil.py
r522 r527 102 102 % repr(name)) 103 103 104 105 def logtime(): 106 return '%04d/%02d/%02d %02d:%02d:%02d' % time.localtime(time.time())[:6] 107 108 def _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 104 133 def _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): 123 140 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') 126 145 f.write(s + '\n') 127 146 f.close() trunk/cherrypy/lib/filter/staticfilter.py
r519 r527 51 51 staticDir = config.get('staticFilter.dir') 52 52 section = config.get('staticFilter.dir', returnSection=True) 53 if section == 'global': 54 section = "/" 53 55 section = section.rstrip(r"\/") 54 56 extraPath = request.path[len(section) + 1:] trunk/cherrypy/test/test.py
r522 r527 94 94 elif o == "--profile": 95 95 self.profile = True 96 elif o == "- 1.1":96 elif o == "--1.1": 97 97 self.protocol = "HTTP/1.1" 98 98 elif o == "--basedir": trunk/cherrypy/test/test_core.py
r523 r527 31 31 import cherrypy 32 32 import types 33 import os 34 localDir = os.path.dirname(__file__) 35 33 36 34 37 class Root: … … 223 226 224 227 228 logFile = os.path.join(localDir, "error.log") 229 logAccessFile = os.path.join(localDir, "access.log") 230 225 231 cherrypy.config.update({ 226 232 'global': {'server.logToScreen': False, 227 233 'server.environment': 'production', 234 'server.protocolVersion': "HTTP/1.1", 228 235 }, 229 236 '/': { … … 239 246 'bax': 'this4', 240 247 }, 248 '/flatten': { 249 'server.logFile': logFile, 250 'server.logAccessFile': logAccessFile, 251 }, 241 252 }) 242 253 … … 245 256 246 257 import helper 247 import os248 258 249 259 class CoreRequestHandlingTest(helper.CPWebCase): … … 294 304 self.assertBody('hello' + (" " * 508)) 295 305 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') 296 343 297 344 def testRedirect(self): … … 442 489 def testFavicon(self): 443 490 # Calls to favicon.ico are special-cased in _cphttptools. 444 localDir = os.path.dirname(__file__)445 491 icofilename = os.path.join(localDir, "../favicon.ico") 446 492 icofile = open(icofilename, "rb") trunk/cherrypy/test/test_static_filter.py
r519 r527 62 62 # This should resolve relative to cherrypy.root.__module__. 63 63 self.getPage("/static/index.html") 64 self.assertStatus('200 OK') 64 65 self.assertHeader('Content-Type', 'text/html') 65 66 self.assertBody('Hello, world\r\n') … … 67 68 # Using a staticFilter.root value... 68 69 self.getPage("/docroot/index.html") 70 self.assertStatus('200 OK') 69 71 self.assertHeader('Content-Type', 'text/html') 70 72 self.assertBody('Hello, world\r\n') … … 72 74 # Check a filename with spaces in it 73 75 self.getPage("/static/has%20space.html") 76 self.assertStatus('200 OK') 74 77 self.assertHeader('Content-Type', 'text/html') 75 78 self.assertBody('Hello, world\r\n') 76 79 77 80 self.getPage("/style.css") 81 self.assertStatus('200 OK') 78 82 self.assertHeader('Content-Type', 'text/css') 79 83 # Note: The body should be exactly 'Dummy stylesheet\n', but

