author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 07 Oct 2009 13:52:36 +0200 | |
branch | stable |
changeset 3592 | 4c08780e54ed |
parent 2000 | bcb9a55a89e0 |
child 3408 | c92170fca813 |
child 4212 | ab6573088b4a |
permissions | -rw-r--r-- |
0 | 1 |
"""provide class to do Apache rewrite rules'job inside cubicweb (though functionnalities |
2 |
are much more limited for the moment) |
|
3 |
||
4 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1936
diff
changeset
|
5 |
:copyright: 2007-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 6 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1936
diff
changeset
|
7 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 8 |
""" |
9 |
||
10 |
__docformat__ = "restructuredtext en" |
|
11 |
||
12 |
from re import compile |
|
13 |
||
14 |
from cubicweb.web import Redirect |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
15 |
from cubicweb.web.component import Component |
0 | 16 |
|
17 |
class RewriteCond(object): |
|
18 |
def __init__(self, condition, match='host', rules=(), action='rewrite'): |
|
19 |
self.condition = compile(condition) |
|
20 |
assert match in ('host', 'path'), match |
|
21 |
self.match_part = match |
|
22 |
self.rules = [] |
|
23 |
for rule, replace in rules: |
|
24 |
rulergx = compile(rule) |
|
25 |
self.rules.append( (rulergx, replace) ) |
|
26 |
assert action in ('rewrite', 'redirect', 'stop'), action |
|
27 |
self.process = getattr(self, 'action_%s' % action) |
|
28 |
||
29 |
def match(self, **kwargs): |
|
30 |
self._match = self.condition.match(kwargs[self.match_part]) |
|
31 |
return not self._match is None |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
32 |
|
0 | 33 |
def action_rewrite(self, path): |
34 |
for rgx, replace in self.rules: |
|
35 |
if not rgx.match(path) is None: |
|
36 |
matchdict = self._match.groupdict() or None |
|
37 |
if not matchdict is None: |
|
38 |
replace = replace % matchdict |
|
39 |
return rgx.sub(replace, path) |
|
40 |
return path |
|
41 |
||
42 |
def action_redirect(self, path): |
|
43 |
url = self.action_rewrite(path) |
|
44 |
raise Redirect(url) |
|
45 |
||
46 |
def action_stop(self, path): |
|
47 |
return path |
|
48 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
49 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
50 |
class ApacheURLRewrite(Component): |
0 | 51 |
"""inherit from this class with actual rules to activate apache style rewriting |
52 |
||
53 |
rules should have the form : |
|
54 |
||
55 |
[('condition pattern 1', [('rule1 pattern', 'replace expression'), |
|
56 |
('rule2 pattern', 'replace expression')], |
|
57 |
('condition pattern 2', [('rule1 pattern', 'replace expression'), |
|
58 |
('rule2 pattern', 'replace expression')] |
|
59 |
] |
|
60 |
||
61 |
for instance the equivalent of the following apache rules: |
|
62 |
||
63 |
RewriteCond %{HTTP_HOST} ^logilab\.fr |
|
64 |
RewriteRule ^/(.*) http://www.logilab.fr/$1 [L,R=301] |
|
65 |
||
66 |
RewriteCond %{HTTP_HOST} ^www\.logilab\.fr |
|
67 |
RewriteRule ^/(.*) http://localhost:8080/$1 [L,P] |
|
68 |
||
69 |
RewriteCond %{HTTP_HOST} ^(.+)\.logilab\.fr |
|
70 |
RewriteRule ^/(data/.*) http://localhost:8080/$1 [L,P] |
|
71 |
RewriteRule ^/(json.*) http://localhost:8080/$1 [L,P] |
|
72 |
RewriteRule ^/(.*) http://localhost:8080/m_%1/$1 [L,P] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
73 |
|
0 | 74 |
could be written (considering that no "host rewritting" is necessary): |
75 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
76 |
class MyAppRules(ApacheURLRewrite): |
0 | 77 |
rules = [ |
78 |
RewriteCond('logilab\.fr', match='host', |
|
79 |
rules=[('/(.*)', r'http://www.logilab.fr/\1')], |
|
80 |
action='redirect'), |
|
81 |
RewriteCond('(www)\.logilab\.fr', match='host', action='stop'), |
|
82 |
RewriteCond('/(data|json)/', match='path', action='stop'), |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
83 |
RewriteCond('(?P<cat>.*)\.logilab\.fr', match='host', |
0 | 84 |
rules=[('/(.*)', r'/m_%(cat)s/\1')]), |
85 |
] |
|
86 |
""" |
|
87 |
__abstract__ = True |
|
88 |
id = 'urlrewriter' |
|
89 |
rules = [] |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
90 |
|
2000
bcb9a55a89e0
do not store req as an attribute of ApacheURLRewrite - fix test
Florent <florent@secondweb.fr>
parents:
1977
diff
changeset
|
91 |
def get_rules(self, req): |
bcb9a55a89e0
do not store req as an attribute of ApacheURLRewrite - fix test
Florent <florent@secondweb.fr>
parents:
1977
diff
changeset
|
92 |
return self.rules |
bcb9a55a89e0
do not store req as an attribute of ApacheURLRewrite - fix test
Florent <florent@secondweb.fr>
parents:
1977
diff
changeset
|
93 |
|
1936
c5af2fbda5b6
pass request to ApacheRewriter rewrite method
Florent <florent@secondweb.fr>
parents:
1802
diff
changeset
|
94 |
def rewrite(self, host, path, req): |
2000
bcb9a55a89e0
do not store req as an attribute of ApacheURLRewrite - fix test
Florent <florent@secondweb.fr>
parents:
1977
diff
changeset
|
95 |
for cond in self.get_rules(req): |
0 | 96 |
if cond.match(host=host, path=path): |
97 |
return cond.process(path) |
|
98 |
return path |