Changeset 1330
- Timestamp:
- 09/04/06 01:19:27
- Files:
-
- trunk/cherrypy/_cptools.py (modified) (2 diffs)
- trunk/cherrypy/lib/cptools.py (modified) (2 diffs)
- trunk/cherrypy/test/test.py (modified) (1 diff)
- trunk/cherrypy/test/test_misc_tools.py (moved) (moved from trunk/cherrypy/test/test_response_headers.py) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/cherrypy/_cptools.py
r1325 r1330 269 269 else: 270 270 # Note the devious technique here of adding hooks on the fly 271 request.hooks.attach('before_finalize', _caching.tee_output) 271 request.hooks.attach('before_finalize', _caching.tee_output, 272 priority = 90) 273 _wrapper.priority = 20 272 274 273 275 def _setup(self): … … 314 316 default_toolbox.nsgmls = Tool('before_finalize', tidy.nsgmls) 315 317 default_toolbox.ignore_headers = Tool('before_request_body', cptools.ignore_headers) 318 default_toolbox.referer = Tool('before_request_body', cptools.referer) 316 319 317 320 trunk/cherrypy/lib/cptools.py
r1324 r1330 1 1 """Functions for builtin CherryPy tools.""" 2 3 import re 2 4 3 5 import cherrypy … … 141 143 cherrypy.response.headers[name] = value 142 144 response_headers.failsafe = True 145 146 147 def referer(pattern, accept=True, accept_missing=False, error=403, 148 message='Forbidden Referer header.'): 149 """Raise HTTPError if Referer header does not pass our test. 150 151 pattern: a regular expression pattern to test against the Referer. 152 accept: if True, the Referer must match the pattern; if False, 153 the Referer must NOT match the pattern. 154 accept_missing: if True, permit requests with no Referer header. 155 error: the HTTP error code to return to the client on failure. 156 message: a string to include in the response body on failure. 157 """ 158 try: 159 match = bool(re.match(pattern, cherrypy.request.headers['Referer'])) 160 if accept == match: 161 return 162 except KeyError: 163 if accept_missing: 164 return 165 166 raise cherrypy.HTTPError(error, message) 143 167 144 168 trunk/cherrypy/test/test.py
r1326 r1330 314 314 'test_gzip', 315 315 'test_objectmapping', 316 'test_ response_headers',316 'test_misc_tools', 317 317 'test_static', 318 318 'test_tutorials', trunk/cherrypy/test/test_misc_tools.py
r1322 r1330 23 23 } 24 24 25 cherrypy.tree.mount(Root()) 25 class Referer: 26 def accept(self): 27 return "Accepted!" 28 accept.exposed = True 29 reject = accept 30 31 conf = {'/referer': {'tools.referer.on': True, 32 'tools.referer.pattern': r'http://[^/]*thisdomain\.com', 33 }, 34 '/referer/reject': {'tools.referer.accept': False, 35 'tools.referer.accept_missing': True, 36 }, 37 } 38 39 root = Root() 40 root.referer = Referer() 41 cherrypy.tree.mount(root, config=conf) 26 42 cherrypy.config.update({'environment': 'test_suite'}) 27 43 … … 41 57 self.assertHeader('Content-Type', 'text/plain') 42 58 59 60 class RefererTest(helper.CPWebCase): 61 62 def testReferer(self): 63 self.getPage('/referer/accept') 64 self.assertErrorPage(403, 'Forbidden Referer header.') 65 66 self.getPage('/referer/accept', 67 headers=[('Referer', 'http://www.thisdomain.com/')]) 68 self.assertStatus(200) 69 self.assertBody('Accepted!') 70 71 # Reject 72 self.getPage('/referer/reject') 73 self.assertStatus(200) 74 self.assertBody('Accepted!') 75 76 self.getPage('/referer/reject', 77 headers=[('Referer', 'http://www.thisdomain.com/')]) 78 self.assertErrorPage(403, 'Forbidden Referer header.') 79 80 43 81 if __name__ == "__main__": 44 82 setup_server()

