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

Changeset 1330

Show
Ignore:
Timestamp:
09/04/06 01:19:27
Author:
fumanchu
Message:

New tools.referer, and moved test_response_headers into new test_misc_tools.

Files:

Legend:

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

    r1325 r1330  
    269269        else: 
    270270            # 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 
    272274     
    273275    def _setup(self): 
     
    314316default_toolbox.nsgmls = Tool('before_finalize', tidy.nsgmls) 
    315317default_toolbox.ignore_headers = Tool('before_request_body', cptools.ignore_headers) 
     318default_toolbox.referer = Tool('before_request_body', cptools.referer) 
    316319 
    317320 
  • trunk/cherrypy/lib/cptools.py

    r1324 r1330  
    11"""Functions for builtin CherryPy tools.""" 
     2 
     3import re 
    24 
    35import cherrypy 
     
    141143        cherrypy.response.headers[name] = value 
    142144response_headers.failsafe = True 
     145 
     146 
     147def 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) 
    143167 
    144168 
  • trunk/cherrypy/test/test.py

    r1326 r1330  
    314314        'test_gzip', 
    315315        'test_objectmapping', 
    316         'test_response_headers', 
     316        'test_misc_tools', 
    317317        'test_static', 
    318318        'test_tutorials', 
  • trunk/cherrypy/test/test_misc_tools.py

    r1322 r1330  
    2323            } 
    2424     
    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) 
    2642    cherrypy.config.update({'environment': 'test_suite'}) 
    2743 
     
    4157        self.assertHeader('Content-Type', 'text/plain') 
    4258 
     59 
     60class 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 
    4381if __name__ == "__main__": 
    4482    setup_server() 

Hosted by WebFaction

Log in as guest/cpguest to create tickets