author | Aurelien Campeas <aurelien.campeas@logilab.fr> |
Wed, 09 May 2012 17:15:44 +0200 | |
changeset 8394 | b8b23af60019 |
parent 5424 | 8ecbcbff9777 |
permissions | -rw-r--r-- |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
1 |
# copyright 2003-2010 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
2 |
# contact http://www.logilab.fr/ -- mailto:contact@logilab.fr |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
3 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
4 |
# This file is part of CubicWeb. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
5 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
6 |
# CubicWeb is free software: you can redistribute it and/or modify it under the |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
7 |
# terms of the GNU Lesser General Public License as published by the Free |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
8 |
# Software Foundation, either version 2.1 of the License, or (at your option) |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
9 |
# any later version. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
10 |
# |
5424
8ecbcbff9777
replace logilab-common by CubicWeb in disclaimer
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5421
diff
changeset
|
11 |
# CubicWeb is distributed in the hope that it will be useful, but WITHOUT |
5421
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
12 |
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
13 |
# FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
14 |
# details. |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
15 |
# |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
16 |
# You should have received a copy of the GNU Lesser General Public License along |
8167de96c523
proper licensing information (LGPL-2.1). Hope I get it right this time.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""provide class to do Apache rewrite rules'job inside cubicweb (though functionnalities |
19 |
are much more limited for the moment) |
|
20 |
||
21 |
""" |
|
22 |
||
23 |
__docformat__ = "restructuredtext en" |
|
24 |
||
25 |
from re import compile |
|
26 |
||
27 |
from cubicweb.web import Redirect |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
28 |
from cubicweb.web.component import Component |
0 | 29 |
|
30 |
class RewriteCond(object): |
|
31 |
def __init__(self, condition, match='host', rules=(), action='rewrite'): |
|
32 |
self.condition = compile(condition) |
|
33 |
assert match in ('host', 'path'), match |
|
34 |
self.match_part = match |
|
35 |
self.rules = [] |
|
36 |
for rule, replace in rules: |
|
37 |
rulergx = compile(rule) |
|
38 |
self.rules.append( (rulergx, replace) ) |
|
39 |
assert action in ('rewrite', 'redirect', 'stop'), action |
|
40 |
self.process = getattr(self, 'action_%s' % action) |
|
41 |
||
42 |
def match(self, **kwargs): |
|
43 |
self._match = self.condition.match(kwargs[self.match_part]) |
|
44 |
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
|
45 |
|
0 | 46 |
def action_rewrite(self, path): |
47 |
for rgx, replace in self.rules: |
|
48 |
if not rgx.match(path) is None: |
|
49 |
matchdict = self._match.groupdict() or None |
|
50 |
if not matchdict is None: |
|
51 |
replace = replace % matchdict |
|
52 |
return rgx.sub(replace, path) |
|
53 |
return path |
|
54 |
||
55 |
def action_redirect(self, path): |
|
56 |
url = self.action_rewrite(path) |
|
57 |
raise Redirect(url) |
|
58 |
||
59 |
def action_stop(self, path): |
|
60 |
return path |
|
61 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
62 |
|
661
4f61eb8a96b7
properly kill/depreciate component base class, only keep Component
sylvain.thenault@logilab.fr
parents:
0
diff
changeset
|
63 |
class ApacheURLRewrite(Component): |
0 | 64 |
"""inherit from this class with actual rules to activate apache style rewriting |
65 |
||
66 |
rules should have the form : |
|
67 |
||
68 |
[('condition pattern 1', [('rule1 pattern', 'replace expression'), |
|
69 |
('rule2 pattern', 'replace expression')], |
|
70 |
('condition pattern 2', [('rule1 pattern', 'replace expression'), |
|
71 |
('rule2 pattern', 'replace expression')] |
|
72 |
] |
|
73 |
||
74 |
for instance the equivalent of the following apache rules: |
|
75 |
||
76 |
RewriteCond %{HTTP_HOST} ^logilab\.fr |
|
77 |
RewriteRule ^/(.*) http://www.logilab.fr/$1 [L,R=301] |
|
78 |
||
79 |
RewriteCond %{HTTP_HOST} ^www\.logilab\.fr |
|
80 |
RewriteRule ^/(.*) http://localhost:8080/$1 [L,P] |
|
81 |
||
82 |
RewriteCond %{HTTP_HOST} ^(.+)\.logilab\.fr |
|
83 |
RewriteRule ^/(data/.*) http://localhost:8080/$1 [L,P] |
|
84 |
RewriteRule ^/(json.*) http://localhost:8080/$1 [L,P] |
|
85 |
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
|
86 |
|
0 | 87 |
could be written (considering that no "host rewritting" is necessary): |
88 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
89 |
class MyAppRules(ApacheURLRewrite): |
0 | 90 |
rules = [ |
91 |
RewriteCond('logilab\.fr', match='host', |
|
92 |
rules=[('/(.*)', r'http://www.logilab.fr/\1')], |
|
93 |
action='redirect'), |
|
94 |
RewriteCond('(www)\.logilab\.fr', match='host', action='stop'), |
|
95 |
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
|
96 |
RewriteCond('(?P<cat>.*)\.logilab\.fr', match='host', |
0 | 97 |
rules=[('/(.*)', r'/m_%(cat)s/\1')]), |
98 |
] |
|
99 |
""" |
|
100 |
__abstract__ = True |
|
3408
c92170fca813
[api] use __regid__ instead of deprecated id
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2000
diff
changeset
|
101 |
__regid__ = 'urlrewriter' |
0 | 102 |
rules = [] |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
661
diff
changeset
|
103 |
|
2000
bcb9a55a89e0
do not store req as an attribute of ApacheURLRewrite - fix test
Florent <florent@secondweb.fr>
parents:
1977
diff
changeset
|
104 |
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
|
105 |
return self.rules |
bcb9a55a89e0
do not store req as an attribute of ApacheURLRewrite - fix test
Florent <florent@secondweb.fr>
parents:
1977
diff
changeset
|
106 |
|
1936
c5af2fbda5b6
pass request to ApacheRewriter rewrite method
Florent <florent@secondweb.fr>
parents:
1802
diff
changeset
|
107 |
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
|
108 |
for cond in self.get_rules(req): |
0 | 109 |
if cond.match(host=host, path=path): |
110 |
return cond.process(path) |
|
111 |
return path |