Changeset 1141
- Timestamp:
- 06/12/06 01:46:25
- Files:
-
- trunk/cherrypy/__init__.py (modified) (1 diff)
- trunk/cherrypy/_cperror.py (modified) (3 diffs)
- trunk/cherrypy/_cprequest.py (modified) (9 diffs)
- trunk/cherrypy/_cpserver.py (modified) (2 diffs)
- trunk/cherrypy/_cptree.py (modified) (1 diff)
- trunk/cherrypy/_cpwsgi.py (modified) (3 diffs)
- trunk/cherrypy/config.py (modified) (2 diffs)
- trunk/cherrypy/lib/__init__.py (modified) (1 diff)
- trunk/cherrypy/lib/cptools.py (modified) (7 diffs)
- trunk/cherrypy/lib/http.py (moved) (moved from trunk/cherrypy/lib/httptools.py)
- trunk/cherrypy/lib/static.py (modified) (3 diffs)
- trunk/cherrypy/test/benchmark.py (modified) (2 diffs)
- trunk/cherrypy/test/helper.py (modified) (2 diffs)
- trunk/cherrypy/test/test_core.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/__init__.py
r1139 r1141 80 80 import datetime 81 81 now = datetime.datetime.now() 82 from cherrypy.lib import http tools83 month = http tools.monthname[now.month][:3].capitalize()82 from cherrypy.lib import http 83 month = http.monthname[now.month][:3].capitalize() 84 84 return '%02d/%s/%04d:%02d:%02d:%02d' % ( 85 85 now.day, month, now.year, now.hour, now.minute, now.second) trunk/cherrypy/_cperror.py
r1139 r1141 4 4 from sys import exc_info as _exc_info 5 5 from urlparse import urljoin as _urljoin 6 from cherrypy.lib import http tools as _httptools6 from cherrypy.lib import http as _http 7 7 8 8 … … 27 27 path, pm = path.split("?", 1) 28 28 request.query_string = pm 29 request.params = _http tools.parseQueryString(pm)29 request.params = _http.parseQueryString(pm) 30 30 31 31 # Note that urljoin will "do the right thing" whether url is: … … 225 225 226 226 try: 227 code, reason, message = _http tools.validStatus(status)227 code, reason, message = _http.validStatus(status) 228 228 except ValueError, x: 229 229 raise cherrypy.HTTPError(500, x.args[0]) trunk/cherrypy/_cprequest.py
r1139 r1141 9 9 from cherrypy import _cpcgifs 10 10 from cherrypy._cperror import format_exc, bare_error 11 from cherrypy.lib import http tools, profiler11 from cherrypy.lib import http, profiler 12 12 13 13 … … 105 105 self.header_list = list(headers) 106 106 self.rfile = rfile 107 self.headers = http tools.HeaderMap()107 self.headers = http.HeaderMap() 108 108 self.simple_cookie = Cookie.SimpleCookie() 109 109 self.handler = None … … 172 172 100 * 1024 * 1024)) 173 173 if mbs > 0: 174 self.rfile = http tools.SizeCheckWrapper(self.rfile, mbs)174 self.rfile = http.SizeCheckWrapper(self.rfile, mbs) 175 175 176 176 self.hooks.run('before_request_body') … … 195 195 """Parse the first line (e.g. "GET /path HTTP/1.1") of the request.""" 196 196 rl = self.request_line 197 method, path, qs, proto = http tools.parse_request_line(rl)197 method, path, qs, proto = http.parse_request_line(rl) 198 198 if path == "*": 199 199 path = "global" … … 221 221 222 222 # cherrypy.request.version == request.protocol in a Version instance. 223 self.version = http tools.version_from_http(self.protocol)223 self.version = http.version_from_http(self.protocol) 224 224 225 225 # cherrypy.response.version should be used to determine whether or 226 226 # not to include a given HTTP/1.1 feature in the response content. 227 227 server_v = cherrypy.config.get('server.protocol_version', 'HTTP/1.0') 228 server_v = http tools.version_from_http(server_v)228 server_v = http.version_from_http(server_v) 229 229 cherrypy.response.version = min(self.version, server_v) 230 230 231 231 def process_headers(self): 232 self.params = http tools.parseQueryString(self.query_string)232 self.params = http.parseQueryString(self.query_string) 233 233 234 234 # Process the headers into self.headers … … 330 330 environ=methenv, 331 331 keep_blank_values=1) 332 except http tools.MaxSizeExceeded:332 except http.MaxSizeExceeded: 333 333 # Post data is too big 334 334 raise cherrypy.HTTPError(413) … … 338 338 self.body = forms.file 339 339 else: 340 self.params.update(http tools.paramsFromCGIForm(forms))340 self.params.update(http.paramsFromCGIForm(forms)) 341 341 342 342 def handle_error(self, exc): … … 539 539 self.body = None 540 540 541 self.headers = http tools.HeaderMap()541 self.headers = http.HeaderMap() 542 542 content_type = cherrypy.config.get('default_content_type', 'text/html') 543 543 self.headers.update({ 544 544 "Content-Type": content_type, 545 545 "Server": "CherryPy/" + cherrypy.__version__, 546 "Date": http tools.HTTPDate(),546 "Date": http.HTTPDate(), 547 547 "Set-Cookie": [], 548 548 "Content-Length": None … … 559 559 560 560 try: 561 code, reason, _ = http tools.validStatus(self.status)561 code, reason, _ = http.validStatus(self.status) 562 562 except ValueError, x: 563 563 raise cherrypy.HTTPError(500, x.args[0]) trunk/cherrypy/_cpserver.py
r1125 r1141 5 5 6 6 import cherrypy 7 from cherrypy.lib import cptools7 from cherrypy.lib import attributes 8 8 9 9 … … 24 24 server = _cpwsgi.WSGIServer() 25 25 if isinstance(server, basestring): 26 server = cptools.attributes(server)()26 server = attributes(server)() 27 27 self.httpserver = server 28 28 trunk/cherrypy/_cptree.py
r1121 r1141 93 93 return path 94 94 95 from cherrypy.lib import http tools96 return http tools.urljoin(script_name, path)95 from cherrypy.lib import http 96 return http.urljoin(script_name, path) 97 97 trunk/cherrypy/_cpwsgi.py
r1109 r1141 5 5 from cherrypy import _cpwsgiserver 6 6 from cherrypy._cperror import format_exc, bare_error 7 from cherrypy.lib import http tools7 from cherrypy.lib import http 8 8 9 9 … … 139 139 500 * 1024)) 140 140 if mhs > 0: 141 self.rfile = http tools.SizeCheckWrapper(self.rfile, mhs)141 self.rfile = http.SizeCheckWrapper(self.rfile, mhs) 142 142 143 143 def parse_request(self): 144 144 try: 145 145 _cpwsgiserver.HTTPRequest.parse_request(self) 146 except http tools.MaxSizeExceeded:146 except http.MaxSizeExceeded: 147 147 msg = "Request Entity Too Large" 148 148 proto = self.environ.get("SERVER_PROTOCOL", "HTTP/1.0") … … 163 163 path = "global" 164 164 165 if isinstance(self.rfile, http tools.SizeCheckWrapper):165 if isinstance(self.rfile, http.SizeCheckWrapper): 166 166 # Unwrap the rfile 167 167 self.rfile = self.rfile.rfile trunk/cherrypy/config.py
r1122 r1141 5 5 6 6 import cherrypy 7 from cherrypy.lib import autoreload, cptools7 from cherrypy.lib import autoreload, unrepr 8 8 9 9 environments = { … … 139 139 value = configParser.get(section, option, raw, vars) 140 140 try: 141 value = cptools.unrepr(value)141 value = unrepr(value) 142 142 except Exception, x: 143 143 msg = ("section: %s, option: %s, value: %s" % trunk/cherrypy/lib/__init__.py
r762 r1141 1 """ 2 CherryPy Standard Library 3 """ 1 """CherryPy Library""" 4 2 3 import sys as _sys 4 5 6 def modules(modulePath): 7 """Load a module and retrieve a reference to that module.""" 8 try: 9 mod = _sys.modules[modulePath] 10 if mod is None: 11 raise KeyError() 12 except KeyError: 13 # The last [''] is important. 14 mod = __import__(modulePath, globals(), locals(), ['']) 15 return mod 16 17 def attributes(fullAttributeName): 18 """Load a module and retrieve an attribute of that module.""" 19 20 # Parse out the path, module, and attribute 21 lastDot = fullAttributeName.rfind(u".") 22 attrName = fullAttributeName[lastDot + 1:] 23 modPath = fullAttributeName[:lastDot] 24 25 aMod = modules(modPath) 26 # Let an AttributeError propagate outward. 27 try: 28 attr = getattr(aMod, attrName) 29 except AttributeError: 30 raise AttributeError("'%s' object has no attribute '%s'" 31 % (modPath, attrName)) 32 33 # Return a reference to the attribute. 34 return attr 35 36 37 # public domain "unrepr" implementation, found on the web and then improved. 38 39 class _Builder: 40 41 def build(self, o): 42 m = getattr(self, 'build_' + o.__class__.__name__, None) 43 if m is None: 44 raise TypeError("unrepr does not recognize %s" % 45 repr(o.__class__.__name__)) 46 return m(o) 47 48 def build_CallFunc(self, o): 49 callee, args, starargs, kwargs = map(self.build, o.getChildren()) 50 return callee(args, *(starargs or ()), **(kwargs or {})) 51 52 def build_List(self, o): 53 return map(self.build, o.getChildren()) 54 55 def build_Const(self, o): 56 return o.value 57 58 def build_Dict(self, o): 59 d = {} 60 i = iter(map(self.build, o.getChildren())) 61 for el in i: 62 d[el] = i.next() 63 return d 64 65 def build_Tuple(self, o): 66 return tuple(self.build_List(o)) 67 68 def build_Name(self, o): 69 if o.name == 'None': 70 return None 71 if o.name == 'True': 72 return True 73 if o.name == 'False': 74 return False 75 76 # See if the Name is a package or module 77 try: 78 return modules(o.name) 79 except ImportError: 80 pass 81 82 raise TypeError("unrepr could not resolve the name %s" % repr(o.name)) 83 84 def build_Add(self, o): 85 real, imag = map(self.build_Const, o.getChildren()) 86 try: 87 real = float(real) 88 except TypeError: 89 raise TypeError("unrepr could not parse real %s" % repr(real)) 90 if not isinstance(imag, complex) or imag.real != 0.0: 91 raise TypeError("unrepr could not parse imag %s" % repr(imag)) 92 return real+imag 93 94 def build_Getattr(self, o): 95 parent = self.build(o.expr) 96 return getattr(parent, o.attrname) 97 98 def build_NoneType(self, o): 99 return None 100 101 102 def unrepr(s): 103 """Return a Python object compiled from a string.""" 104 if not s: 105 return s 106 107 import compiler 108 p = compiler.parse("a=" + s) 109 obj = p.getChildren()[1].getChildren()[0].getChildren()[1] 110 111 return _Builder().build(obj) 112 trunk/cherrypy/lib/cptools.py
r1134 r1141 1 """Tools which CherryPy may invoke.""" 2 3 import md5 4 import sys 1 """Functions for builtin CherryPy tools.""" 5 2 6 3 import cherrypy 7 import httptools 8 9 10 def modules(modulePath): 11 """Load a module and retrieve a reference to that module.""" 12 try: 13 mod = sys.modules[modulePath] 14 if mod is None: 15 raise KeyError() 16 except KeyError: 17 # The last [''] is important. 18 mod = __import__(modulePath, globals(), locals(), ['']) 19 return mod 20 21 def attributes(fullAttributeName): 22 """Load a module and retrieve an attribute of that module.""" 23 24 # Parse out the path, module, and attribute 25 lastDot = fullAttributeName.rfind(u".") 26 attrName = fullAttributeName[lastDot + 1:] 27 modPath = fullAttributeName[:lastDot] 28 29 aMod = modules(modPath) 30 # Let an AttributeError propagate outward. 31 try: 32 attr = getattr(aMod, attrName) 33 except AttributeError: 34 raise AttributeError("'%s' object has no attribute '%s'" 35 % (modPath, attrName)) 36 37 # Return a reference to the attribute. 38 return attr 39 40 41 # public domain "unrepr" implementation, found on the web and then improved. 42 import compiler 43 44 def getObj(s): 45 s = "a=" + s 46 p = compiler.parse(s) 47 return p.getChildren()[1].getChildren()[0].getChildren()[1] 48 49 50 class UnknownType(Exception): 51 pass 52 53 54 class Builder: 55 56 def build(self, o): 57 m = getattr(self, 'build_' + o.__class__.__name__, None) 58 if m is None: 59 raise UnknownType(o.__class__.__name__) 60 return m(o) 61 62 def build_CallFunc(self, o): 63 callee, args, starargs, kwargs = map(self.build, o.getChildren()) 64 return callee(args, *(starargs or ()), **(kwargs or {})) 65 66 def build_List(self, o): 67 return map(self.build, o.getChildren()) 68 69 def build_Const(self, o): 70 return o.value 71 72 def build_Dict(self, o): 73 d = {} 74 i = iter(map(self.build, o.getChildren())) 75 for el in i: 76 d[el] = i.next() 77 return d 78 79 def build_Tuple(self, o): 80 return tuple(self.build_List(o)) 81 82 def build_Name(self, o): 83 if o.name == 'None': 84 return None 85 if o.name == 'True': 86 return True 87 if o.name == 'False': 88 return False 89 90 # See if the Name is a package or module 91 try: 92 return modules(o.name) 93 except ImportError: 94 pass 95 96 raise UnknownType(o.name) 97 98 def build_Add(self, o): 99 real, imag = map(self.build_Const, o.getChildren()) 100 try: 101 real = float(real) 102 except TypeError: 103 raise UnknownType('Add') 104 if not isinstance(imag, complex) or imag.real != 0.0: 105 raise UnknownType('Add') 106 return real+imag 107 108 def build_Getattr(self, o): 109 parent = self.build(o.expr) 110 return getattr(parent, o.attrname) 111 112 def build_NoneType(self, o): 113 return None 114 115 116 def unrepr(s): 117 if not s: 118 return s 119 return Builder().build(getObj(s)) 4 import http as _http 120 5 121 6 … … 131 16 132 17 if (not etag) and autotags: 18 import md5 133 19 etag = '"%s"' % md5.new(cherrypy.response.collapse_body()).hexdigest() 134 20 cherrypy.response.headers['ETag'] = etag … … 137 23 cherrypy.response.ETag = etag 138 24 139 status, reason, msg = httptools.validStatus(cherrypy.response.status)25 status, reason, msg = _http.validStatus(cherrypy.response.status) 140 26 141 27 conditions = cherrypy.request.headers.elements('If-Match') or [] … … 158 44 lastmod = cherrypy.response.headers.get('Last-Modified') 159 45 if lastmod: 160 status, reason, msg = httptools.validStatus(cherrypy.response.status)46 status, reason, msg = _http.validStatus(cherrypy.response.status) 161 47 162 48 since = cherrypy.request.headers.get('If-Unmodified-Since') … … 221 107 load_user_by_username=None, session_key='username', 222 108 on_login=None, on_logout=None, login_screen=None): 109 """Assert that the user is logged in.""" 223 110 224 111 if login_screen is None: … … 311 198 cherrypy.request.virtual_prefix = prefix = domains.get(domain, "") 312 199 if prefix: 313 raise cherrypy.InternalRedirect( httptools.urljoin(prefix, cherrypy.request.path_info))200 raise cherrypy.InternalRedirect(_http.urljoin(prefix, cherrypy.request.path_info)) 314 201 315 202 def log_traceback(): … … 319 206 320 207 def log_request_headers(): 321 """Write the last error's tracebackto the cherrypy error log."""208 """Write request headers to the cherrypy error log.""" 322 209 h = [" %s: %s" % (k, v) for k, v in cherrypy.request.header_list] 323 210 cherrypy.log('\nRequest Headers:\n' + '\n'.join(h), "HTTP") trunk/cherrypy/lib/static.py
r1137 r1141 11 11 12 12 import cherrypy 13 from cherrypy.lib import cptools, http tools13 from cherrypy.lib import cptools, http 14 14 15 15 … … 58 58 # Set the Last-Modified response header, so that 59 59 # modified-since validation code can work. 60 response.headers['Last-Modified'] = http tools.HTTPDate(time.gmtime(stat.st_mtime))60 response.headers['Last-Modified'] = http.HTTPDate(time.gmtime(stat.st_mtime)) 61 61 cptools.validate_since() 62 62 … … 77 77 if cherrypy.response.version >= (1, 1): 78 78 response.headers["Accept-Ranges"] = "bytes" 79 r = http tools.getRanges(cherrypy.request.headers.get('Range'), c_len)79 r = http.getRanges(cherrypy.request.headers.get('Range'), c_len) 80 80 if r == []: 81 81 response.headers['Content-Range'] = "bytes */%s" % c_len trunk/cherrypy/test/benchmark.py
r1102 r1141 32 32 33 33 import cherrypy 34 from cherrypy.lib import http tools34 from cherrypy.lib import http 35 35 36 36 … … 92 92 cherrypy.response.header_list = [("Content-Type", 'text/html'), 93 93 ("Server", "Null CherryPy"), 94 ("Date", http tools.HTTPDate()),94 ("Date", http.HTTPDate()), 95 95 ("Content-Length", "0"), 96 96 ] trunk/cherrypy/test/helper.py
r1096 r1141 24 24 25 25 import cherrypy 26 from cherrypy.lib import http tools26 from cherrypy.lib import http 27 27 import webtest 28 28 … … 44 44 """Open the url. Return status, headers, body.""" 45 45 if self.script_name: 46 url = http tools.urljoin(self.script_name, url)46 url = http.urljoin(self.script_name, url) 47 47 webtest.WebCase.getPage(self, url, headers, method, body, protocol) 48 48 trunk/cherrypy/test/test_core.py
r1138 r1141 6 6 import cherrypy 7 7 from cherrypy import _cptools, tools 8 from cherrypy.lib import http tools, static8 from cherrypy.lib import http, static 9 9 import types 10 10 … … 245 245 def get_ranges(self): 246 246 h = cherrypy.request.headers.get('Range') 247 return repr(http tools.getRanges(h, 8))247 return repr(http.getRanges(h, 8)) 248 248 249 249 def slice_file(self):

