author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 21 Oct 2015 16:10:47 +0200 | |
changeset 10853 | de741492538d |
parent 10606 | 718e78ef6699 |
permissions | -rw-r--r-- |
9287
e70c8c70e344
[wsgi] add the simplest possible wsgi (debug) server
Aurelien Campeas
parents:
8695
diff
changeset
|
1 |
# copyright 2003-2013 LOGILAB S.A. (Paris, FRANCE), all rights reserved. |
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
|
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/>. |
8396
8d58fcf68539
[web] remove some unused imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8314
diff
changeset
|
18 |
"""WSGI request handler for cubicweb""" |
0 | 19 |
|
20 |
__docformat__ = "restructuredtext en" |
|
21 |
||
10606
718e78ef6699
[py3k] import zip using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9443
diff
changeset
|
22 |
from itertools import chain, repeat |
718e78ef6699
[py3k] import zip using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9443
diff
changeset
|
23 |
|
718e78ef6699
[py3k] import zip using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9443
diff
changeset
|
24 |
from six.moves import zip |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
25 |
|
9443
6ff9401c37c1
[wsgi/handler] fix api change from 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9402
diff
changeset
|
26 |
from cubicweb import AuthenticationError |
8396
8d58fcf68539
[web] remove some unused imports
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
8314
diff
changeset
|
27 |
from cubicweb.web import DirectResponse |
0 | 28 |
from cubicweb.web.application import CubicWebPublisher |
29 |
from cubicweb.wsgi.request import CubicWebWsgiRequest |
|
30 |
||
31 |
# See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html |
|
32 |
STATUS_CODE_TEXT = { |
|
33 |
100: 'CONTINUE', |
|
34 |
101: 'SWITCHING PROTOCOLS', |
|
35 |
200: 'OK', |
|
36 |
201: 'CREATED', |
|
37 |
202: 'ACCEPTED', |
|
38 |
203: 'NON-AUTHORITATIVE INFORMATION', |
|
39 |
204: 'NO CONTENT', |
|
40 |
205: 'RESET CONTENT', |
|
41 |
206: 'PARTIAL CONTENT', |
|
42 |
300: 'MULTIPLE CHOICES', |
|
43 |
301: 'MOVED PERMANENTLY', |
|
44 |
302: 'FOUND', |
|
45 |
303: 'SEE OTHER', |
|
46 |
304: 'NOT MODIFIED', |
|
47 |
305: 'USE PROXY', |
|
48 |
306: 'RESERVED', |
|
49 |
307: 'TEMPORARY REDIRECT', |
|
50 |
400: 'BAD REQUEST', |
|
51 |
401: 'UNAUTHORIZED', |
|
52 |
402: 'PAYMENT REQUIRED', |
|
53 |
403: 'FORBIDDEN', |
|
54 |
404: 'NOT FOUND', |
|
55 |
405: 'METHOD NOT ALLOWED', |
|
56 |
406: 'NOT ACCEPTABLE', |
|
57 |
407: 'PROXY AUTHENTICATION REQUIRED', |
|
58 |
408: 'REQUEST TIMEOUT', |
|
59 |
409: 'CONFLICT', |
|
60 |
410: 'GONE', |
|
61 |
411: 'LENGTH REQUIRED', |
|
62 |
412: 'PRECONDITION FAILED', |
|
63 |
413: 'REQUEST ENTITY TOO LARGE', |
|
64 |
414: 'REQUEST-URI TOO LONG', |
|
65 |
415: 'UNSUPPORTED MEDIA TYPE', |
|
66 |
416: 'REQUESTED RANGE NOT SATISFIABLE', |
|
67 |
417: 'EXPECTATION FAILED', |
|
68 |
500: 'INTERNAL SERVER ERROR', |
|
69 |
501: 'NOT IMPLEMENTED', |
|
70 |
502: 'BAD GATEWAY', |
|
71 |
503: 'SERVICE UNAVAILABLE', |
|
72 |
504: 'GATEWAY TIMEOUT', |
|
73 |
505: 'HTTP VERSION NOT SUPPORTED', |
|
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) |
|
10606
718e78ef6699
[py3k] import zip using six.moves
Rémi Cardona <remi.cardona@logilab.fr>
parents:
9443
diff
changeset
|
83 |
self.headers = list(chain(*[zip(repeat(k), v) |
8314
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
84 |
for k, v in req.headers_out.getAllRawHeaders()])) |
cfd6ab461849
[Web-Request] Use rich header (closes #2204164)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8312
diff
changeset
|
85 |
self.headers = [(str(k), str(v)) for k, v in self.headers] |
0 | 86 |
if body: |
87 |
self.body = [body] |
|
88 |
else: |
|
89 |
self.body = [] |
|
90 |
||
91 |
def __iter__(self): |
|
92 |
return iter(self.body) |
|
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
93 |
|
0 | 94 |
|
95 |
class CubicWebWSGIApplication(object): |
|
96 |
"""This is the wsgi application which will be called by the |
|
97 |
wsgi server with the WSGI ``environ`` and ``start_response`` |
|
98 |
parameters. |
|
99 |
""" |
|
100 |
||
9443
6ff9401c37c1
[wsgi/handler] fix api change from 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9402
diff
changeset
|
101 |
def __init__(self, config): |
6ff9401c37c1
[wsgi/handler] fix api change from 3.18
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
9402
diff
changeset
|
102 |
self.appli = CubicWebPublisher(config.repository(), config) |
0 | 103 |
self.config = config |
8593
41259e1f9d48
[wsgi] saner use of `self.config` instead of `config`
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8396
diff
changeset
|
104 |
self.base_url = self.config['base-url'] |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2058
diff
changeset
|
105 |
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
|
106 |
|
0 | 107 |
def _render(self, req): |
108 |
"""this function performs the actual rendering |
|
109 |
""" |
|
110 |
try: |
|
8312
6c2119509fac
[web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
111 |
path = req.path |
6c2119509fac
[web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
112 |
result = self.appli.handle_request(req, path) |
8695
358d8bed9626
[toward-py3k] rewrite to "except AnException as exc:" (part of #2711624)
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
8593
diff
changeset
|
113 |
except DirectResponse as ex: |
8312
6c2119509fac
[web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
114 |
return ex.response |
6c2119509fac
[web] Move request handling logic into cubicweb application. (closes #2200684)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8309
diff
changeset
|
115 |
return WSGIResponse(req.status_out, req, result) |
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
116 |
|
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
117 |
|
0 | 118 |
def __call__(self, environ, start_response): |
119 |
"""WSGI protocol entry point""" |
|
8309
48ef505aa9f9
[request] gather all base_url logic in a single place (closes #2200756)
Pierre-Yves David <pierre-yves.david@logilab.fr>
parents:
8272
diff
changeset
|
120 |
req = CubicWebWsgiRequest(environ, self.appli.vreg) |
0 | 121 |
response = self._render(req) |
122 |
start_response(response.status, response.headers) |
|
123 |
return response.body |
|
124 |
||
1802
d628defebc17
delete-trailing-whitespace + some copyright update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
0
diff
changeset
|
125 |
|
0 | 126 |
|
7083
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5442
diff
changeset
|
127 |
# these are overridden by set_log_methods below |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5442
diff
changeset
|
128 |
# only defining here to prevent pylint from complaining |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5442
diff
changeset
|
129 |
info = warning = error = critical = exception = debug = lambda msg,*a,**kw: None |
b8e35cde46e9
help pylint by explicitely defining some attributes
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5442
diff
changeset
|
130 |
|
0 | 131 |
|
132 |
from logging import getLogger |
|
133 |
from cubicweb import set_log_methods |
|
134 |
set_log_methods(CubicWebWSGIApplication, getLogger('cubicweb.wsgi')) |