author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 13 Aug 2009 10:50:19 +0200 | |
changeset 2807 | 696ff03f9a58 |
parent 2770 | 356e9d7c356d |
child 3163 | edfe43ceaa35 |
permissions | -rw-r--r-- |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2181
diff
changeset
|
1 |
"""twisted server for CubicWeb web instances |
0 | 2 |
|
3 |
:organization: Logilab |
|
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1936
diff
changeset
|
4 |
:copyright: 2001-2009 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
: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
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
11 |
import os |
0 | 12 |
import select |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
13 |
from time import mktime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
14 |
from datetime import date, timedelta |
1520
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1420
diff
changeset
|
15 |
from urlparse import urlsplit, urlunsplit |
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
16 |
import hotshot |
0 | 17 |
|
18 |
from twisted.application import service, strports |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
19 |
from twisted.scripts._twistd_unix import daemonize |
0 | 20 |
from twisted.internet import reactor, task, threads |
21 |
from twisted.internet.defer import maybeDeferred |
|
22 |
from twisted.web2 import channel, http, server, iweb |
|
23 |
from twisted.web2 import static, resource, responsecode |
|
24 |
||
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
25 |
from cubicweb import ObjectNotFound, CW_EVENT_MANAGER |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
26 |
from cubicweb.web import (AuthenticationError, NotFound, Redirect, |
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
27 |
RemoteCallFailed, DirectResponse, StatusResponse, |
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
28 |
ExplicitLogin) |
0 | 29 |
from cubicweb.web.application import CubicWebPublisher |
30 |
||
31 |
from cubicweb.etwist.request import CubicWebTwistedRequestAdapter |
|
32 |
||
33 |
||
34 |
def start_task(interval, func): |
|
35 |
lc = task.LoopingCall(func) |
|
36 |
lc.start(interval) |
|
37 |
||
38 |
def start_looping_tasks(repo): |
|
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
39 |
for interval, func, args in repo._looping_tasks: |
0 | 40 |
repo.info('starting twisted task %s with interval %.2fs', |
41 |
func.__name__, interval) |
|
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
42 |
def catch_error_func(repo=repo, func=func, args=args): |
0 | 43 |
try: |
2708
60d728bdcba5
allow to specify arbitrary argument when recording a looping task func
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2705
diff
changeset
|
44 |
func(*args) |
0 | 45 |
except: |
46 |
repo.exception('error in looping task') |
|
47 |
start_task(interval, catch_error_func) |
|
48 |
# ensure no tasks will be further added |
|
49 |
repo._looping_tasks = () |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
50 |
|
1543
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
51 |
def host_prefixed_baseurl(baseurl, host): |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
52 |
scheme, netloc, url, query, fragment = urlsplit(baseurl) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
53 |
netloc_domain = '.' + '.'.join(netloc.split('.')[-2:]) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
54 |
if host.endswith(netloc_domain): |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
55 |
netloc = host |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
56 |
baseurl = urlunsplit((scheme, netloc, url, query, fragment)) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
57 |
return baseurl |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
58 |
|
0 | 59 |
|
60 |
class LongTimeExpiringFile(static.File): |
|
61 |
"""overrides static.File and sets a far futre ``Expires`` date |
|
62 |
on the resouce. |
|
63 |
||
64 |
versions handling is done by serving static files by different |
|
65 |
URLs for each version. For instance:: |
|
66 |
||
67 |
http://localhost:8080/data-2.48.2/cubicweb.css |
|
68 |
http://localhost:8080/data-2.49.0/cubicweb.css |
|
69 |
etc. |
|
70 |
||
71 |
""" |
|
72 |
def renderHTTP(self, request): |
|
73 |
def setExpireHeader(response): |
|
74 |
response = iweb.IResponse(response) |
|
75 |
# Don't provide additional resource information to error responses |
|
76 |
if response.code < 400: |
|
77 |
# the HTTP RFC recommands not going further than 1 year ahead |
|
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
78 |
expires = date.today() + timedelta(days=6*30) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
79 |
response.headers.setHeader('Expires', mktime(expires.timetuple())) |
0 | 80 |
return response |
81 |
d = maybeDeferred(super(LongTimeExpiringFile, self).renderHTTP, request) |
|
82 |
return d.addCallback(setExpireHeader) |
|
83 |
||
84 |
||
85 |
class CubicWebRootResource(resource.PostableResource): |
|
86 |
addSlash = False |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
87 |
|
0 | 88 |
def __init__(self, config, debug=None): |
89 |
self.appli = CubicWebPublisher(config, debug=debug) |
|
90 |
self.debugmode = debug |
|
91 |
self.config = config |
|
92 |
self.base_url = config['base-url'] or config.default_base_url() |
|
93 |
self.versioned_datadir = 'data%s' % config.instance_md5_version() |
|
94 |
assert self.base_url[-1] == '/' |
|
95 |
self.https_url = config['https-url'] |
|
96 |
assert not self.https_url or self.https_url[-1] == '/' |
|
97 |
# when we have an in-memory repository, clean unused sessions every XX |
|
98 |
# seconds and properly shutdown the server |
|
99 |
if config.repo_method == 'inmemory': |
|
100 |
reactor.addSystemEventTrigger('before', 'shutdown', |
|
101 |
self.shutdown_event) |
|
1115 | 102 |
# monkey patch start_looping_task to get proper reactor integration |
0 | 103 |
self.appli.repo.__class__.start_looping_tasks = start_looping_tasks |
104 |
if config.pyro_enabled(): |
|
105 |
# if pyro is enabled, we have to register to the pyro name |
|
106 |
# server, create a pyro daemon, and create a task to handle pyro |
|
107 |
# requests |
|
108 |
self.pyro_daemon = self.appli.repo.pyro_register() |
|
109 |
self.pyro_listen_timeout = 0.02 |
|
110 |
start_task(1, self.pyro_loop_event) |
|
111 |
self.appli.repo.start_looping_tasks() |
|
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
112 |
self.set_url_rewriter() |
2705
30bcdbd92820
[events] renamed source-reload into registry-reload to avoid potential confusions with datasources
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2694
diff
changeset
|
113 |
CW_EVENT_MANAGER.bind('after-registry-reload', self.set_url_rewriter) |
0 | 114 |
interval = min(config['cleanup-session-time'] or 120, |
115 |
config['cleanup-anonymous-session-time'] or 720) / 2. |
|
116 |
start_task(interval, self.appli.session_handler.clean_sessions) |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
117 |
|
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
118 |
def set_url_rewriter(self): |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
119 |
self.url_rewriter = self.appli.vreg['components'].select_or_none('urlrewriter') |
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
120 |
|
0 | 121 |
def shutdown_event(self): |
122 |
"""callback fired when the server is shutting down to properly |
|
123 |
clean opened sessions |
|
124 |
""" |
|
125 |
self.appli.repo.shutdown() |
|
126 |
||
127 |
def pyro_loop_event(self): |
|
128 |
"""listen for pyro events""" |
|
129 |
try: |
|
130 |
self.pyro_daemon.handleRequests(self.pyro_listen_timeout) |
|
131 |
except select.error: |
|
132 |
return |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
133 |
|
0 | 134 |
def locateChild(self, request, segments): |
135 |
"""Indicate which resource to use to process down the URL's path""" |
|
136 |
if segments: |
|
137 |
if segments[0] == 'https': |
|
138 |
segments = segments[1:] |
|
139 |
if len(segments) >= 2: |
|
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
140 |
if segments[0] in (self.versioned_datadir, 'data', 'static'): |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
141 |
# Anything in data/, static/ is treated as static files |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
142 |
if segments[0] == 'static': |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
143 |
# instance static directory |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
144 |
datadir = self.config.static_directory |
2169
124fb0b9fa4b
fckeditor may be a subdirectory of the data directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
145 |
elif segments[1] == 'fckeditor': |
124fb0b9fa4b
fckeditor may be a subdirectory of the data directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
146 |
fckeditordir = self.config.ext_resources['FCKEDITOR_PATH'] |
124fb0b9fa4b
fckeditor may be a subdirectory of the data directory
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
1977
diff
changeset
|
147 |
return static.File(fckeditordir), segments[2:] |
151
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
148 |
else: |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
149 |
# cube static data file |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
150 |
datadir = self.config.locate_resource(segments[1]) |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
151 |
if datadir is None: |
343e7a18675d
static files support
Sylvain Thenault <sylvain.thenault@logilab.fr>
parents:
0
diff
changeset
|
152 |
return None, [] |
0 | 153 |
self.info('static file %s from %s', segments[-1], datadir) |
154 |
if segments[0] == 'data': |
|
155 |
return static.File(str(datadir)), segments[1:] |
|
156 |
else: |
|
157 |
return LongTimeExpiringFile(datadir), segments[1:] |
|
158 |
elif segments[0] == 'fckeditor': |
|
159 |
fckeditordir = self.config.ext_resources['FCKEDITOR_PATH'] |
|
160 |
return static.File(fckeditordir), segments[1:] |
|
161 |
# Otherwise we use this single resource |
|
162 |
return self, () |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
163 |
|
0 | 164 |
def render(self, request): |
165 |
"""Render a page from the root resource""" |
|
166 |
# reload modified files (only in development or debug mode) |
|
167 |
if self.config.mode == 'dev' or self.debugmode: |
|
168 |
self.appli.vreg.register_objects(self.config.vregistry_path()) |
|
169 |
if self.config['profile']: # default profiler don't trace threads |
|
170 |
return self.render_request(request) |
|
171 |
else: |
|
172 |
return threads.deferToThread(self.render_request, request) |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
173 |
|
0 | 174 |
def render_request(self, request): |
175 |
origpath = request.path |
|
176 |
host = request.host |
|
177 |
# dual http/https access handling: expect a rewrite rule to prepend |
|
178 |
# 'https' to the path to detect https access |
|
179 |
if origpath.split('/', 2)[1] == 'https': |
|
180 |
origpath = origpath[6:] |
|
181 |
request.uri = request.uri[6:] |
|
182 |
https = True |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
183 |
baseurl = self.https_url or self.base_url |
0 | 184 |
else: |
185 |
https = False |
|
186 |
baseurl = self.base_url |
|
1520
b097057e629d
provide an option to substitute the base-url (left-most part) subdomain by the one of the current http query to easy multiple subdomains website management
Florent <florent@secondweb.fr>
parents:
1420
diff
changeset
|
187 |
if self.config['use-request-subdomain']: |
1543
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
188 |
baseurl = host_prefixed_baseurl(baseurl, host) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
189 |
self.warning('used baseurl is %s for this request', baseurl) |
0 | 190 |
req = CubicWebTwistedRequestAdapter(request, self.appli.vreg, https, baseurl) |
191 |
if req.authmode == 'http': |
|
192 |
# activate realm-based auth |
|
193 |
realm = self.config['realm'] |
|
194 |
req.set_header('WWW-Authenticate', [('Basic', {'realm' : realm })], raw=False) |
|
195 |
try: |
|
196 |
self.appli.connect(req) |
|
197 |
except AuthenticationError: |
|
198 |
return self.request_auth(req) |
|
199 |
except Redirect, ex: |
|
200 |
return self.redirect(req, ex.location) |
|
201 |
if https and req.cnx.anonymous_connection: |
|
202 |
# don't allow anonymous on https connection |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
203 |
return self.request_auth(req) |
0 | 204 |
if self.url_rewriter is not None: |
1115 | 205 |
# XXX should occur before authentication? |
0 | 206 |
try: |
1936
c5af2fbda5b6
pass request to ApacheRewriter rewrite method
Florent <florent@secondweb.fr>
parents:
1543
diff
changeset
|
207 |
path = self.url_rewriter.rewrite(host, origpath, req) |
0 | 208 |
except Redirect, ex: |
209 |
return self.redirect(req, ex.location) |
|
210 |
request.uri.replace(origpath, path, 1) |
|
211 |
else: |
|
212 |
path = origpath |
|
213 |
if not path or path == "/": |
|
214 |
path = 'view' |
|
215 |
try: |
|
216 |
result = self.appli.publish(path, req) |
|
217 |
except DirectResponse, ex: |
|
218 |
return ex.response |
|
219 |
except StatusResponse, ex: |
|
220 |
return http.Response(stream=ex.content, code=ex.status, |
|
221 |
headers=req.headers_out or None) |
|
222 |
except RemoteCallFailed, ex: |
|
223 |
req.set_header('content-type', 'application/json') |
|
224 |
return http.Response(stream=ex.dumps(), |
|
225 |
code=responsecode.INTERNAL_SERVER_ERROR) |
|
226 |
except NotFound: |
|
227 |
result = self.appli.notfound_content(req) |
|
228 |
return http.Response(stream=result, code=responsecode.NOT_FOUND, |
|
229 |
headers=req.headers_out or None) |
|
230 |
except ExplicitLogin: # must be before AuthenticationError |
|
231 |
return self.request_auth(req) |
|
232 |
except AuthenticationError: |
|
233 |
if self.config['auth-mode'] == 'cookie': |
|
234 |
# in cookie mode redirecting to the index view is enough : |
|
235 |
# either anonymous connection is allowed and the page will |
|
236 |
# be displayed or we'll be redirected to the login form |
|
237 |
msg = req._('you have been logged out') |
|
238 |
if req.https: |
|
239 |
req._base_url = self.base_url |
|
240 |
req.https = False |
|
241 |
url = req.build_url('view', vid='index', __message=msg) |
|
242 |
return self.redirect(req, url) |
|
243 |
else: |
|
244 |
# in http we have to request auth to flush current http auth |
|
245 |
# information |
|
246 |
return self.request_auth(req, loggedout=True) |
|
247 |
except Redirect, ex: |
|
248 |
return self.redirect(req, ex.location) |
|
249 |
# request may be referenced by "onetime callback", so clear its entity |
|
250 |
# cache to avoid memory usage |
|
251 |
req.drop_entity_cache() |
|
252 |
return http.Response(stream=result, code=responsecode.OK, |
|
253 |
headers=req.headers_out or None) |
|
254 |
||
255 |
def redirect(self, req, location): |
|
256 |
req.headers_out.setHeader('location', str(location)) |
|
257 |
self.debug('redirecting to %s', location) |
|
258 |
# 303 See other |
|
259 |
return http.Response(code=303, headers=req.headers_out) |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
260 |
|
0 | 261 |
def request_auth(self, req, loggedout=False): |
262 |
if self.https_url and req.base_url() != self.https_url: |
|
263 |
req.headers_out.setHeader('location', self.https_url + 'login') |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
264 |
return http.Response(code=303, headers=req.headers_out) |
0 | 265 |
if self.config['auth-mode'] == 'http': |
266 |
code = responsecode.UNAUTHORIZED |
|
267 |
else: |
|
268 |
code = responsecode.FORBIDDEN |
|
269 |
if loggedout: |
|
270 |
if req.https: |
|
271 |
req._base_url = self.base_url |
|
272 |
req.https = False |
|
273 |
content = self.appli.loggedout_content(req) |
|
274 |
else: |
|
275 |
content = self.appli.need_login_content(req) |
|
276 |
return http.Response(code, req.headers_out, content) |
|
277 |
||
278 |
from twisted.python import failure |
|
279 |
from twisted.internet import defer |
|
280 |
from twisted.web2 import fileupload |
|
281 |
||
282 |
# XXX set max file size to 100Mo: put max upload size in the configuration |
|
283 |
# line below for twisted >= 8.0, default param value for earlier version |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
284 |
resource.PostableResource.maxSize = 100*1024*1024 |
0 | 285 |
def parsePOSTData(request, maxMem=100*1024, maxFields=1024, |
286 |
maxSize=100*1024*1024): |
|
287 |
if request.stream.length == 0: |
|
288 |
return defer.succeed(None) |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
289 |
|
0 | 290 |
ctype = request.headers.getHeader('content-type') |
291 |
||
292 |
if ctype is None: |
|
293 |
return defer.succeed(None) |
|
294 |
||
295 |
def updateArgs(data): |
|
296 |
args = data |
|
297 |
request.args.update(args) |
|
298 |
||
299 |
def updateArgsAndFiles(data): |
|
300 |
args, files = data |
|
301 |
request.args.update(args) |
|
302 |
request.files.update(files) |
|
303 |
||
304 |
def error(f): |
|
305 |
f.trap(fileupload.MimeFormatError) |
|
306 |
raise http.HTTPError(responsecode.BAD_REQUEST) |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
307 |
|
0 | 308 |
if ctype.mediaType == 'application' and ctype.mediaSubtype == 'x-www-form-urlencoded': |
309 |
d = fileupload.parse_urlencoded(request.stream, keep_blank_values=True) |
|
310 |
d.addCallbacks(updateArgs, error) |
|
311 |
return d |
|
312 |
elif ctype.mediaType == 'multipart' and ctype.mediaSubtype == 'form-data': |
|
313 |
boundary = ctype.params.get('boundary') |
|
314 |
if boundary is None: |
|
315 |
return defer.fail(http.HTTPError( |
|
316 |
http.StatusResponse(responsecode.BAD_REQUEST, |
|
317 |
"Boundary not specified in Content-Type."))) |
|
318 |
d = fileupload.parseMultipartFormData(request.stream, boundary, |
|
319 |
maxMem, maxFields, maxSize) |
|
320 |
d.addCallbacks(updateArgsAndFiles, error) |
|
321 |
return d |
|
322 |
else: |
|
323 |
raise http.HTTPError(responsecode.BAD_REQUEST) |
|
324 |
||
325 |
server.parsePOSTData = parsePOSTData |
|
326 |
||
327 |
||
328 |
from logging import getLogger |
|
329 |
from cubicweb import set_log_methods |
|
330 |
set_log_methods(CubicWebRootResource, getLogger('cubicweb.twisted')) |
|
331 |
||
332 |
||
333 |
||
334 |
def _gc_debug(): |
|
335 |
import gc |
|
336 |
from pprint import pprint |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
337 |
from cubicweb.appobject import AppObject |
0 | 338 |
gc.collect() |
339 |
count = 0 |
|
340 |
acount = 0 |
|
341 |
ocount = {} |
|
342 |
for obj in gc.get_objects(): |
|
343 |
if isinstance(obj, CubicWebTwistedRequestAdapter): |
|
344 |
count += 1 |
|
2657
de974465d381
[appobject] kill VObject class, move base selector classes to appobject
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2654
diff
changeset
|
345 |
elif isinstance(obj, AppObject): |
0 | 346 |
acount += 1 |
347 |
else: |
|
348 |
try: |
|
1132 | 349 |
ocount[obj.__class__] += 1 |
0 | 350 |
except KeyError: |
351 |
ocount[obj.__class__] = 1 |
|
352 |
except AttributeError: |
|
353 |
pass |
|
354 |
print 'IN MEM REQUESTS', count |
|
355 |
print 'IN MEM APPOBJECTS', acount |
|
356 |
ocount = sorted(ocount.items(), key=lambda x: x[1], reverse=True)[:20] |
|
357 |
pprint(ocount) |
|
358 |
print 'UNREACHABLE', gc.garbage |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
359 |
|
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
360 |
def run(config, debug): |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
361 |
# create the site |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
362 |
root_resource = CubicWebRootResource(config, debug) |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
363 |
website = server.Site(root_resource) |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
364 |
# serve it via standard HTTP on port set in the configuration |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
365 |
port = config['port'] or 8080 |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
366 |
reactor.listenTCP(port, channel.HTTPFactory(website)) |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
367 |
baseurl = config['base-url'] or config.default_base_url() |
2694
4303ff921a9b
[twisted] use cubicweb.twisted.logger instead of print to notify when the instance is started
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2685
diff
changeset
|
368 |
logger = getLogger('cubicweb.twisted') |
4303ff921a9b
[twisted] use cubicweb.twisted.logger instead of print to notify when the instance is started
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2685
diff
changeset
|
369 |
logger.info('instance started on %s', baseurl) |
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
370 |
if not debug: |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
371 |
daemonize() |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
372 |
if config['pid-file']: |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
373 |
# ensure the directory where the pid-file should be set exists (for |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
374 |
# instance /var/run/cubicweb may be deleted on computer restart) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
375 |
piddir = os.path.dirname(config['pid-file']) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
376 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
377 |
os.makedirs(piddir) |
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
378 |
file(config['pid-file'], 'w').write(str(os.getpid())) |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
379 |
if config['profile']: |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
380 |
prof = hotshot.Profile(config['profile']) |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
381 |
prof.runcall(reactor.run) |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
382 |
else: |
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
383 |
reactor.run() |