web/views/apacherewrite.py
branchtls-sprint
changeset 12560 dc6d44e69a70
parent 1802 d628defebc17
child 1936 c5af2fbda5b6
equal deleted inserted replaced
2022:31412adee482 12560:dc6d44e69a70
    26         self.process = getattr(self, 'action_%s' % action)
    26         self.process = getattr(self, 'action_%s' % action)
    27 
    27 
    28     def match(self, **kwargs):
    28     def match(self, **kwargs):
    29         self._match = self.condition.match(kwargs[self.match_part])
    29         self._match = self.condition.match(kwargs[self.match_part])
    30         return not self._match is None
    30         return not self._match is None
    31     
    31 
    32     def action_rewrite(self, path):
    32     def action_rewrite(self, path):
    33         for rgx, replace in self.rules:
    33         for rgx, replace in self.rules:
    34             if not rgx.match(path) is None:
    34             if not rgx.match(path) is None:
    35                 matchdict = self._match.groupdict() or None
    35                 matchdict = self._match.groupdict() or None
    36                 if not matchdict is None:
    36                 if not matchdict is None:
    43         raise Redirect(url)
    43         raise Redirect(url)
    44 
    44 
    45     def action_stop(self, path):
    45     def action_stop(self, path):
    46         return path
    46         return path
    47 
    47 
    48     
    48 
    49 class ApacheURLRewrite(Component):
    49 class ApacheURLRewrite(Component):
    50     """inherit from this class with actual rules to activate apache style rewriting
    50     """inherit from this class with actual rules to activate apache style rewriting
    51 
    51 
    52     rules should have the form :
    52     rules should have the form :
    53 
    53 
    67 
    67 
    68         RewriteCond %{HTTP_HOST} ^(.+)\.logilab\.fr
    68         RewriteCond %{HTTP_HOST} ^(.+)\.logilab\.fr
    69         RewriteRule ^/(data/.*) http://localhost:8080/$1 [L,P]
    69         RewriteRule ^/(data/.*) http://localhost:8080/$1 [L,P]
    70         RewriteRule ^/(json.*) http://localhost:8080/$1 [L,P]
    70         RewriteRule ^/(json.*) http://localhost:8080/$1 [L,P]
    71         RewriteRule ^/(.*) http://localhost:8080/m_%1/$1 [L,P]
    71         RewriteRule ^/(.*) http://localhost:8080/m_%1/$1 [L,P]
    72     
    72 
    73     could be written (considering that no "host rewritting" is necessary):
    73     could be written (considering that no "host rewritting" is necessary):
    74 
    74 
    75       class MyAppRules(ApacheURLRewrite): 
    75       class MyAppRules(ApacheURLRewrite):
    76         rules = [
    76         rules = [
    77           RewriteCond('logilab\.fr', match='host',
    77           RewriteCond('logilab\.fr', match='host',
    78                       rules=[('/(.*)', r'http://www.logilab.fr/\1')],
    78                       rules=[('/(.*)', r'http://www.logilab.fr/\1')],
    79                       action='redirect'),
    79                       action='redirect'),
    80           RewriteCond('(www)\.logilab\.fr', match='host', action='stop'),
    80           RewriteCond('(www)\.logilab\.fr', match='host', action='stop'),
    81           RewriteCond('/(data|json)/', match='path', action='stop'),
    81           RewriteCond('/(data|json)/', match='path', action='stop'),
    82           RewriteCond('(?P<cat>.*)\.logilab\.fr', match='host', 
    82           RewriteCond('(?P<cat>.*)\.logilab\.fr', match='host',
    83                       rules=[('/(.*)', r'/m_%(cat)s/\1')]),
    83                       rules=[('/(.*)', r'/m_%(cat)s/\1')]),
    84         ]
    84         ]
    85     """
    85     """
    86     __abstract__ = True
    86     __abstract__ = True
    87     id = 'urlrewriter'
    87     id = 'urlrewriter'
    88     rules = []
    88     rules = []
    89         
    89 
    90     def rewrite(self, host, path):
    90     def rewrite(self, host, path):
    91         for cond in self.rules:
    91         for cond in self.rules:
    92             if cond.match(host=host, path=path):
    92             if cond.match(host=host, path=path):
    93                 return cond.process(path)
    93                 return cond.process(path)
    94         return path
    94         return path