author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 25 Mar 2010 14:49:24 +0100 | |
branch | stable |
changeset 5018 | 2f2d9bc6dca4 |
parent 4252 | 6c4f109c2b03 |
child 5421 | 8167de96c523 |
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 |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2000
diff
changeset
|
5 |
:copyright: 2007-2010 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 |
|
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2000
diff
changeset
|
88 |
__regid__ = 'urlrewriter' |
0 | 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 |