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