author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 28 Apr 2010 12:15:52 +0200 | |
branch | oldstable |
changeset 5424 | 8ecbcbff9777 |
parent 5421 | 8167de96c523 |
child 5426 | 0d4853a6e5ee |
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
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:
4721
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
0 | 18 |
"""WSGI request handler for cubicweb |
19 |
||
20 |
""" |
|
21 |
||
22 |
__docformat__ = "restructuredtext en" |
|
23 |
||
4719
aaed3f813ef8
kill dead/useless code as suggested by pylint
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4252
diff
changeset
|
24 |
from cubicweb import AuthenticationError |
0 | 25 |
from cubicweb.web import (NotFound, Redirect, DirectResponse, StatusResponse, |
4721
8f63691ccb7f
pylint style fixes
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4719
diff
changeset
|
26 |
ExplicitLogin) |
0 | 27 |
from cubicweb.web.application import CubicWebPublisher |
28 |
from cubicweb.wsgi.request import CubicWebWsgiRequest |
|
29 |
||
30 |
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
|
31 |
STATUS_CODE_TEXT = { |
|
32 |
100: 'CONTINUE', |
|
33 |
101: 'SWITCHING PROTOCOLS', |
|
34 |
200: 'OK', |
|
35 |
201: 'CREATED', |
|
36 |
202: 'ACCEPTED', |
|
37 |
203: 'NON-AUTHORITATIVE INFORMATION', |
|
38 |
204: 'NO CONTENT', |
|
39 |
205: 'RESET CONTENT', |
|
40 |
206: 'PARTIAL CONTENT', |
|
41 |
300: 'MULTIPLE CHOICES', |
|
42 |
301: 'MOVED PERMANENTLY', |
|
43 |
302: 'FOUND', |
|
44 |
303: 'SEE OTHER', |
|
45 |
304: 'NOT MODIFIED', |
|
46 |
305: 'USE PROXY', |
|
47 |
306: 'RESERVED', |
|
48 |
307: 'TEMPORARY REDIRECT', |
|
49 |
400: 'BAD REQUEST', |
|
50 |
401: 'UNAUTHORIZED', |
|
51 |
402: 'PAYMENT REQUIRED', |
|
52 |
403: 'FORBIDDEN', |
|
53 |
404: 'NOT FOUND', |
|
54 |
405: 'METHOD NOT ALLOWED', |
|
55 |
406: 'NOT ACCEPTABLE', |
|
56 |
407: 'PROXY AUTHENTICATION REQUIRED', |
|
57 |
408: 'REQUEST TIMEOUT', |
|
58 |
409: 'CONFLICT', |
|
59 |
410: 'GONE', |
|
60 |
411: 'LENGTH REQUIRED', |
|
61 |
412: 'PRECONDITION FAILED', |
|
62 |
413: 'REQUEST ENTITY TOO LARGE', |
|
63 |
414: 'REQUEST-URI TOO LONG', |
|
64 |
415: 'UNSUPPORTED MEDIA TYPE', |
|
65 |
416: 'REQUESTED RANGE NOT SATISFIABLE', |
|
66 |
417: 'EXPECTATION FAILED', |
|
67 |
500: 'INTERNAL SERVER ERROR', |
|
68 |
501: 'NOT IMPLEMENTED', |
|
69 |
502: 'BAD GATEWAY', |
|
70 |
503: 'SERVICE UNAVAILABLE', |
|
71 |
504: 'GATEWAY TIMEOUT', |
|
72 |
505: 'HTTP VERSION NOT SUPPORTED', |
|
73 |
} |
|
74 |
||
75 |
||
76 |
class WSGIResponse(object): |
|
77 |
"""encapsulates the wsgi response parameters |
|
78 |
(code, headers and body if there is one) |
|
79 |
""" |
|
80 |
def __init__(self, code, req, body=None): |
|
81 |
text = STATUS_CODE_TEXT.get(code, 'UNKNOWN STATUS CODE') |
|
82 |
self.status = '%s %s' % (code, text) |
|
83 |
self.headers = [(str(k), str(v)) for k, v in req.headers_out.items()] |
|
84 |
if body: |
|
85 |
self.body = [body] |
|
86 |
else: |
|
87 |
self.body = [] |
|
88 |
||
89 |
def __iter__(self): |
|
90 |
return iter(self.body) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
91 |
|
0 | 92 |
|
93 |
||
94 |
class CubicWebWSGIApplication(object): |
|
95 |
"""This is the wsgi application which will be called by the |
|
96 |
wsgi server with the WSGI ``environ`` and ``start_response`` |
|
97 |
parameters. |
|
98 |
||
99 |
XXX: missing looping tasks and proper repository shutdown when |
|
100 |
the application is stopped. |
|
101 |
NOTE: no pyro |
|
102 |
""" |
|
103 |
||
104 |
def __init__(self, config, debug=None, vreg=None): |
|
105 |
self.appli = CubicWebPublisher(config, debug=debug, vreg=vreg) |
|
106 |
self.debugmode = debug |
|
107 |
self.config = config |
|
108 |
self.base_url = None |
|
109 |
# self.base_url = config['base-url'] or config.default_base_url() |
|
110 |
# assert self.base_url[-1] == '/' |
|
111 |
# self.https_url = config['https-url'] |
|
112 |
# assert not self.https_url or self.https_url[-1] == '/' |
|
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2058
diff
changeset
|
113 |
self.url_rewriter = self.appli.vreg['components'].select_or_none('urlrewriter') |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
114 |
|
0 | 115 |
def _render(self, req): |
116 |
"""this function performs the actual rendering |
|
117 |
XXX missing: https handling, url rewriting, cache management, |
|
118 |
authentication |
|
119 |
""" |
|
120 |
if self.base_url is None: |
|
121 |
self.base_url = self.config._base_url = req.base_url() |
|
122 |
# XXX https handling needs to be implemented |
|
123 |
if req.authmode == 'http': |
|
124 |
# activate realm-based auth |
|
125 |
realm = self.config['realm'] |
|
126 |
req.set_header('WWW-Authenticate', [('Basic', {'realm' : realm })], raw=False) |
|
127 |
try: |
|
128 |
self.appli.connect(req) |
|
129 |
except AuthenticationError: |
|
130 |
return self.request_auth(req) |
|
131 |
except Redirect, ex: |
|
132 |
return self.redirect(req, ex.location) |
|
133 |
path = req.path |
|
134 |
if not path or path == "/": |
|
135 |
path = 'view' |
|
136 |
try: |
|
137 |
result = self.appli.publish(path, req) |
|
138 |
except DirectResponse, ex: |
|
139 |
return WSGIResponse(200, req, ex.response) |
|
140 |
except StatusResponse, ex: |
|
141 |
return WSGIResponse(ex.status, req, ex.content) |
|
142 |
except NotFound: |
|
143 |
result = self.appli.notfound_content(req) |
|
144 |
return WSGIResponse(404, req, result) |
|
145 |
except ExplicitLogin: # must be before AuthenticationError |
|
146 |
return self.request_auth(req) |
|
147 |
except AuthenticationError: |
|
148 |
if self.config['auth-mode'] == 'cookie': |
|
149 |
# in cookie mode redirecting to the index view is enough : |
|
150 |
# either anonymous connection is allowed and the page will |
|
151 |
# be displayed or we'll be redirected to the login form |
|
152 |
msg = req._('you have been logged out') |
|
153 |
# if req.https: |
|
154 |
# req._base_url = self.base_url |
|
155 |
# req.https = False |
|
156 |
url = req.build_url('view', vid='index', __message=msg) |
|
157 |
return self.redirect(req, url) |
|
158 |
else: |
|
159 |
# in http we have to request auth to flush current http auth |
|
160 |
# information |
|
161 |
return self.request_auth(req, loggedout=True) |
|
162 |
except Redirect, ex: |
|
163 |
return self.redirect(req, ex.location) |
|
164 |
if not result: |
|
165 |
# no result, something went wrong... |
|
166 |
self.error('no data (%s)', req) |
|
167 |
# 500 Internal server error |
|
168 |
return self.redirect(req, req.build_url('error')) |
|
169 |
return WSGIResponse(200, req, result) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
170 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
171 |
|
0 | 172 |
def __call__(self, environ, start_response): |
173 |
"""WSGI protocol entry point""" |
|
174 |
req = CubicWebWsgiRequest(environ, self.appli.vreg, self.base_url) |
|
175 |
response = self._render(req) |
|
176 |
start_response(response.status, response.headers) |
|
177 |
return response.body |
|
178 |
||
179 |
def redirect(self, req, location): |
|
180 |
"""convenience function which builds a redirect WSGIResponse""" |
|
181 |
self.debug('redirecting to %s', location) |
|
182 |
req.set_header('location', str(location)) |
|
183 |
return WSGIResponse(303, req) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
184 |
|
0 | 185 |
def request_auth(self, req, loggedout=False): |
186 |
"""returns the appropriate WSGIResponse to require the user to log in |
|
187 |
""" |
|
188 |
# if self.https_url and req.base_url() != self.https_url: |
|
189 |
# return self.redirect(self.https_url + 'login') |
|
190 |
if self.config['auth-mode'] == 'http': |
|
191 |
code = 401 # UNAUTHORIZED |
|
192 |
else: |
|
193 |
code = 403 # FORBIDDEN |
|
194 |
if loggedout: |
|
195 |
# if req.https: |
|
196 |
# req._base_url = self.base_url |
|
197 |
# req.https = False |
|
198 |
content = self.appli.loggedout_content(req) |
|
199 |
else: |
|
200 |
content = self.appli.need_login_content(req) |
|
201 |
return WSGIResponse(code, req, content) |
|
202 |
||
203 |
||
204 |
from logging import getLogger |
|
205 |
from cubicweb import set_log_methods |
|
206 |
set_log_methods(CubicWebWSGIApplication, getLogger('cubicweb.wsgi')) |