author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Wed, 12 May 2010 12:20:47 +0200 | |
branch | stable |
changeset 5520 | 2d7f135846ee |
parent 5491 | 0a6e6fccfd30 |
child 5508 | 6718ba5db0eb |
child 5553 | 646737f80c88 |
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
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:
5325
diff
changeset
|
17 |
# with CubicWeb. If not, see <http://www.gnu.org/licenses/>. |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2181
diff
changeset
|
18 |
"""twisted server for CubicWeb web instances |
0 | 19 |
|
20 |
""" |
|
21 |
__docformat__ = "restructuredtext en" |
|
22 |
||
23 |
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
|
24 |
import os |
0 | 25 |
import select |
4398
4e7e74918631
add missing errno import in etwist/server.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4221
diff
changeset
|
26 |
import errno |
5243
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
27 |
import traceback |
5216
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
28 |
from os.path import join |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
29 |
from time import mktime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
30 |
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
|
31 |
from urlparse import urlsplit, urlunsplit |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
32 |
from cgi import FieldStorage, parse_header |
5243
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
33 |
from cStringIO import StringIO |
0 | 34 |
|
35 |
from twisted.internet import reactor, task, threads |
|
36 |
from twisted.internet.defer import maybeDeferred |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
37 |
from twisted.web import http, server |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
38 |
from twisted.web import static, resource |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
39 |
from twisted.web.server import NOT_DONE_YET |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
40 |
|
5491
0a6e6fccfd30
[python2.6] do not import simplejson directly
Alexandre Fayolle <alexandre.fayolle@logilab.fr>
parents:
5426
diff
changeset
|
41 |
from cubicweb.web import dumps |
0 | 42 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
43 |
from logilab.common.decorators import monkeypatch |
0 | 44 |
|
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5216
diff
changeset
|
45 |
from cubicweb import AuthenticationError, ConfigurationError, CW_EVENT_MANAGER |
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5216
diff
changeset
|
46 |
from cubicweb.web import Redirect, DirectResponse, StatusResponse, LogOut |
0 | 47 |
from cubicweb.web.application import CubicWebPublisher |
5216
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
48 |
from cubicweb.web.http_headers import generateDateTime |
0 | 49 |
from cubicweb.etwist.request import CubicWebTwistedRequestAdapter |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
50 |
from cubicweb.etwist.http import HTTPResponse |
0 | 51 |
|
3193
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
52 |
def daemonize(): |
3181
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
53 |
# XXX unix specific |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
54 |
# XXX factorize w/ code in cw.server.server and cw.server.serverctl |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
55 |
# (start-repository command) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
56 |
# See http://www.erlenstar.demon.co.uk/unix/faq_toc.html#TOC16 |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
57 |
if os.fork(): # launch child and... |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
58 |
return 1 |
3181
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
59 |
os.setsid() |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
60 |
if os.fork(): # launch child again. |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
61 |
return 1 |
3181
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
62 |
# move to the root to avoit mount pb |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
63 |
os.chdir('/') |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
64 |
# set paranoid umask |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
65 |
os.umask(077) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
66 |
null = os.open('/dev/null', os.O_RDWR) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
67 |
for i in range(3): |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
68 |
try: |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
69 |
os.dup2(null, i) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
70 |
except OSError, e: |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
71 |
if e.errno != errno.EBADF: |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
72 |
raise |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
73 |
os.close(null) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
74 |
return None |
0 | 75 |
|
76 |
def start_task(interval, func): |
|
77 |
lc = task.LoopingCall(func) |
|
3493
3e4603678d4e
use default repo looping task so they are started in thread, else we get blocking task
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3198
diff
changeset
|
78 |
# wait until interval has expired to actually start the task, else we have |
3e4603678d4e
use default repo looping task so they are started in thread, else we get blocking task
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3198
diff
changeset
|
79 |
# to wait all task to be finished for the server to be actually started |
3e4603678d4e
use default repo looping task so they are started in thread, else we get blocking task
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3198
diff
changeset
|
80 |
lc.start(interval, now=False) |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
81 |
|
1543
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
82 |
def host_prefixed_baseurl(baseurl, host): |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
83 |
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
|
84 |
netloc_domain = '.' + '.'.join(netloc.split('.')[-2:]) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
85 |
if host.endswith(netloc_domain): |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
86 |
netloc = host |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
87 |
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
|
88 |
return baseurl |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
89 |
|
0 | 90 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
91 |
class ForbiddenDirectoryLister(resource.Resource): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
92 |
def render(self, request): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
93 |
return HTTPResponse(twisted_request=request, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
94 |
code=http.FORBIDDEN, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
95 |
stream='Access forbidden') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
96 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
97 |
class File(static.File): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
98 |
"""Prevent from listing directories""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
99 |
def directoryListing(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
100 |
return ForbiddenDirectoryLister() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
101 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
102 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
103 |
class LongTimeExpiringFile(File): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
104 |
"""overrides static.File and sets a far future ``Expires`` date |
0 | 105 |
on the resouce. |
106 |
||
107 |
versions handling is done by serving static files by different |
|
108 |
URLs for each version. For instance:: |
|
109 |
||
110 |
http://localhost:8080/data-2.48.2/cubicweb.css |
|
111 |
http://localhost:8080/data-2.49.0/cubicweb.css |
|
112 |
etc. |
|
113 |
||
114 |
""" |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
115 |
def render(self, request): |
5216
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
116 |
# XXX: Don't provide additional resource information to error responses |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
117 |
# |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
118 |
# the HTTP RFC recommands not going further than 1 year ahead |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
119 |
expires = date.today() + timedelta(days=6*30) |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
120 |
request.setHeader('Expires', generateDateTime(mktime(expires.timetuple()))) |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
121 |
return File.render(self, request) |
0 | 122 |
|
123 |
||
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
124 |
class CubicWebRootResource(resource.Resource): |
0 | 125 |
def __init__(self, config, debug=None): |
126 |
self.debugmode = debug |
|
127 |
self.config = config |
|
3828
335e40fdb5a5
when instance is started normally (eg not in debug mode), check versions before daemonization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3791
diff
changeset
|
128 |
# instantiate publisher here and not in init_publisher to get some |
335e40fdb5a5
when instance is started normally (eg not in debug mode), check versions before daemonization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3791
diff
changeset
|
129 |
# checks done before daemonization (eg versions consistency) |
335e40fdb5a5
when instance is started normally (eg not in debug mode), check versions before daemonization
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3791
diff
changeset
|
130 |
self.appli = CubicWebPublisher(config, debug=self.debugmode) |
4909
a13c1d1ecc5b
[web server]Â simplify base-url handling on startup. Ensure config['base-url'] is correctly set once started
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
131 |
self.base_url = config['base-url'] |
a13c1d1ecc5b
[web server]Â simplify base-url handling on startup. Ensure config['base-url'] is correctly set once started
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4436
diff
changeset
|
132 |
self.https_url = config['https-url'] |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
133 |
self.children = {} |
5216
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
134 |
self.static_directories = set(('data%s' % config.instance_md5_version(), |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
135 |
'data', 'static', 'fckeditor')) |
5270
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
136 |
global MAX_POST_LENGTH |
5330
19bc44f5f9d1
[web] no more needed with the bytes option type
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5328
diff
changeset
|
137 |
MAX_POST_LENGTH = config['max-post-length'] |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
138 |
|
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
139 |
def init_publisher(self): |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
140 |
config = self.config |
0 | 141 |
# when we have an in-memory repository, clean unused sessions every XX |
142 |
# seconds and properly shutdown the server |
|
143 |
if config.repo_method == 'inmemory': |
|
144 |
reactor.addSystemEventTrigger('before', 'shutdown', |
|
145 |
self.shutdown_event) |
|
146 |
if config.pyro_enabled(): |
|
147 |
# if pyro is enabled, we have to register to the pyro name |
|
148 |
# server, create a pyro daemon, and create a task to handle pyro |
|
149 |
# requests |
|
150 |
self.pyro_daemon = self.appli.repo.pyro_register() |
|
151 |
self.pyro_listen_timeout = 0.02 |
|
3502 | 152 |
self.appli.repo.looping_task(1, self.pyro_loop_event) |
0 | 153 |
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
|
154 |
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
|
155 |
CW_EVENT_MANAGER.bind('after-registry-reload', self.set_url_rewriter) |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
156 |
|
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
157 |
def start_service(self): |
5325
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5279
diff
changeset
|
158 |
start_task(self.appli.session_handler.clean_sessions_interval, |
f1c660e1169e
[web] consistent cleanup session interval time
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5279
diff
changeset
|
159 |
self.appli.session_handler.clean_sessions) |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
160 |
|
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
161 |
def set_url_rewriter(self): |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
162 |
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
|
163 |
|
0 | 164 |
def shutdown_event(self): |
165 |
"""callback fired when the server is shutting down to properly |
|
166 |
clean opened sessions |
|
167 |
""" |
|
168 |
self.appli.repo.shutdown() |
|
169 |
||
170 |
def pyro_loop_event(self): |
|
171 |
"""listen for pyro events""" |
|
172 |
try: |
|
173 |
self.pyro_daemon.handleRequests(self.pyro_listen_timeout) |
|
174 |
except select.error: |
|
175 |
return |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
176 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
177 |
def getChild(self, path, request): |
0 | 178 |
"""Indicate which resource to use to process down the URL's path""" |
5216
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
179 |
pre_path = request.path.split('/')[1:] |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
180 |
if pre_path[0] == 'https': |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
181 |
pre_path.pop(0) |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
182 |
directory = pre_path[0] |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
183 |
# Anything in data/, static/, fckeditor/ and the generated versioned |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
184 |
# data directory is treated as static files |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
185 |
if directory in self.static_directories: |
5337
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
186 |
# take care fckeditor may appears as root directory or as a data |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
187 |
# subdirectory |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
188 |
if directory == 'static': |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
189 |
return File(self.config.static_directory) |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
190 |
if directory == 'fckeditor': |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
191 |
return File(self.config.ext_resources['FCKEDITOR_PATH']) |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
192 |
if directory != 'data': |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
193 |
# versioned directory, use specific file with http cache |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
194 |
# headers so their are cached for a very long time |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
195 |
cls = LongTimeExpiringFile |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
196 |
else: |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
197 |
cls = File |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
198 |
if path == 'fckeditor': |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
199 |
return cls(self.config.ext_resources['FCKEDITOR_PATH']) |
5216
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
200 |
if path == directory: # recurse |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
201 |
return self |
5337
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
202 |
datadir = self.config.locate_resource(path) |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
203 |
if datadir is None: |
c3a037550e1f
[etwist] proper getChild implementation (eventually): fix fckeditor resources locating bugs by properly using file resources
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5335
diff
changeset
|
204 |
return self # recurse |
5216
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
205 |
self.debug('static file %s from %s', path, datadir) |
4f4369e63f5e
[twisted] fix LongTimeExpiringFile and getChild implementation
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5155
diff
changeset
|
206 |
return cls(join(datadir, path)) |
0 | 207 |
# Otherwise we use this single resource |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
208 |
return self |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
209 |
|
0 | 210 |
def render(self, request): |
211 |
"""Render a page from the root resource""" |
|
3638
648d6dbec630
system/user modes + CWDEV instead of installed/dev mixed modes. Fix behaviour when setting CW_MODE explicitly
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3618
diff
changeset
|
212 |
# reload modified files in debug mode |
3641
cf30e4498674
fix debug attribute conflict on configuration
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3638
diff
changeset
|
213 |
if self.debugmode: |
5273
c4caef6f09c9
[vreg] refactor the [re]loading process so things get clearer and fix spurious errors on edge cases
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5252
diff
changeset
|
214 |
self.appli.vreg.reload_if_needed() |
0 | 215 |
if self.config['profile']: # default profiler don't trace threads |
216 |
return self.render_request(request) |
|
217 |
else: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
218 |
deferred = threads.deferToThread(self.render_request, request) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
219 |
return NOT_DONE_YET |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
220 |
|
0 | 221 |
def render_request(self, request): |
5243
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
222 |
try: |
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
223 |
return self._render_request(request) |
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
224 |
except: |
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
225 |
errorstream = StringIO() |
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
226 |
traceback.print_exc(file=errorstream) |
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
227 |
return HTTPResponse(stream='<pre>%s</pre>' % errorstream.getvalue(), |
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
228 |
code=500, twisted_request=request) |
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
229 |
|
1ab7acb9abe9
[etwist] we must call all error not handled in _render_request, else the thread fail silently and nothing ends up on the ui
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5223
diff
changeset
|
230 |
def _render_request(self, request): |
0 | 231 |
origpath = request.path |
232 |
host = request.host |
|
233 |
# dual http/https access handling: expect a rewrite rule to prepend |
|
234 |
# 'https' to the path to detect https access |
|
235 |
if origpath.split('/', 2)[1] == 'https': |
|
236 |
origpath = origpath[6:] |
|
237 |
request.uri = request.uri[6:] |
|
238 |
https = True |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
239 |
baseurl = self.https_url or self.base_url |
0 | 240 |
else: |
241 |
https = False |
|
242 |
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
|
243 |
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
|
244 |
baseurl = host_prefixed_baseurl(baseurl, host) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
245 |
self.warning('used baseurl is %s for this request', baseurl) |
0 | 246 |
req = CubicWebTwistedRequestAdapter(request, self.appli.vreg, https, baseurl) |
247 |
if req.authmode == 'http': |
|
248 |
# activate realm-based auth |
|
249 |
realm = self.config['realm'] |
|
250 |
req.set_header('WWW-Authenticate', [('Basic', {'realm' : realm })], raw=False) |
|
251 |
try: |
|
252 |
self.appli.connect(req) |
|
253 |
except Redirect, ex: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
254 |
return self.redirect(request=req, location=ex.location) |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5216
diff
changeset
|
255 |
if https and req.session.anonymous_session: |
0 | 256 |
# don't allow anonymous on https connection |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
257 |
return self.request_auth(request=req) |
0 | 258 |
if self.url_rewriter is not None: |
1115 | 259 |
# XXX should occur before authentication? |
0 | 260 |
try: |
1936
c5af2fbda5b6
pass request to ApacheRewriter rewrite method
Florent <florent@secondweb.fr>
parents:
1543
diff
changeset
|
261 |
path = self.url_rewriter.rewrite(host, origpath, req) |
0 | 262 |
except Redirect, ex: |
263 |
return self.redirect(req, ex.location) |
|
264 |
request.uri.replace(origpath, path, 1) |
|
265 |
else: |
|
266 |
path = origpath |
|
267 |
if not path or path == "/": |
|
268 |
path = 'view' |
|
269 |
try: |
|
270 |
result = self.appli.publish(path, req) |
|
271 |
except DirectResponse, ex: |
|
272 |
return ex.response |
|
273 |
except StatusResponse, ex: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
274 |
return HTTPResponse(stream=ex.content, code=ex.status, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
275 |
twisted_request=req._twreq, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
276 |
headers=req.headers_out) |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5216
diff
changeset
|
277 |
except AuthenticationError: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
278 |
return self.request_auth(request=req) |
5223
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5216
diff
changeset
|
279 |
except LogOut, ex: |
6abd6e3599f4
#773448: refactor session and 'no connection' handling, by introducing proper web session. We should now be able to see page even when no anon is configured, and be redirected to the login form as soon as one tries to do a query.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5216
diff
changeset
|
280 |
if self.config['auth-mode'] == 'cookie' and ex.url: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
281 |
return self.redirect(request=req, location=ex.url) |
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4909
diff
changeset
|
282 |
# in http we have to request auth to flush current http auth |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4909
diff
changeset
|
283 |
# information |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
284 |
return self.request_auth(request=req, loggedout=True) |
0 | 285 |
except Redirect, ex: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
286 |
return self.redirect(request=req, location=ex.location) |
0 | 287 |
# request may be referenced by "onetime callback", so clear its entity |
288 |
# cache to avoid memory usage |
|
289 |
req.drop_entity_cache() |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
290 |
return HTTPResponse(twisted_request=req._twreq, code=http.OK, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
291 |
stream=result, headers=req.headers_out) |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
292 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
293 |
def redirect(self, request, location): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
294 |
self.debug('redirecting to %s', str(location)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
295 |
request.headers_out.setHeader('location', str(location)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
296 |
# 303 See other |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
297 |
return HTTPResponse(twisted_request=request._twreq, code=303, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
298 |
headers=request.headers_out) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
299 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
300 |
def request_auth(self, request, loggedout=False): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
301 |
if self.https_url and request.base_url() != self.https_url: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
302 |
return self.redirect(request, self.https_url + 'login') |
0 | 303 |
if self.config['auth-mode'] == 'http': |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
304 |
code = http.UNAUTHORIZED |
0 | 305 |
else: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
306 |
code = http.FORBIDDEN |
0 | 307 |
if loggedout: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
308 |
if request.https: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
309 |
request._base_url = self.base_url |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
310 |
request.https = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
311 |
content = self.appli.loggedout_content(request) |
0 | 312 |
else: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
313 |
content = self.appli.need_login_content(request) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
314 |
return HTTPResponse(twisted_request=request._twreq, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
315 |
stream=content, code=code, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
316 |
headers=request.headers_out) |
0 | 317 |
|
318 |
||
5335
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
319 |
JSON_PATHS = set(('json',)) |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
320 |
FRAME_POST_PATHS = set(('validateform',)) |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
321 |
|
5270
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
322 |
orig_gotLength = http.Request.gotLength |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
323 |
@monkeypatch(http.Request) |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
324 |
def gotLength(self, length): |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
325 |
orig_gotLength(self, length) |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
326 |
if length > MAX_POST_LENGTH: # length is 0 on GET |
5335
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
327 |
path = self.channel._path.split('?', 1)[0].rstrip('/').rsplit('/', 1)[-1] |
5270
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
328 |
self.clientproto = 'HTTP/1.1' # not yet initialized |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
329 |
self.channel.persistent = 0 # force connection close on cleanup |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
330 |
self.setResponseCode(http.BAD_REQUEST) |
5335
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
331 |
if path in JSON_PATHS: # XXX better json path detection |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
332 |
self.setHeader('content-type',"application/json") |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
333 |
body = dumps({'reason': 'request max size exceeded'}) |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
334 |
elif path in FRAME_POST_PATHS: # XXX better frame post path detection |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
335 |
self.setHeader('content-type',"text/html") |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
336 |
body = ('<script type="text/javascript">' |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
337 |
'window.parent.handleFormValidationResponse(null, null, null, %s, null);' |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
338 |
'</script>' % dumps( (False, 'request max size exceeded', None) )) |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
339 |
else: |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
340 |
self.setHeader('content-type',"text/html") |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
341 |
body = ("<html><head><title>Processing Failed</title></head><body>" |
9aed0b607130
[etwist] nicer, thouhg really not perferct) request max size exceeded support of json/frame posts
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5330
diff
changeset
|
342 |
"<b>request max size exceeded</b></body></html>") |
5270
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
343 |
self.setHeader('content-length', str(len(body))) |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
344 |
self.write(body) |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
345 |
# see request.finish(). Done here since we get error due to not full |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
346 |
# initialized request |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
347 |
self.finished = 1 |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
348 |
if not self.queued: |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
349 |
self._cleanup() |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
350 |
for d in self.notifications: |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
351 |
d.callback(None) |
6297d5265572
[etwist] allow to specify max size for POST request in the configuration. We should deal with this nicely on the js form validation code.
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
5243
diff
changeset
|
352 |
self.notifications = [] |
0 | 353 |
|
354 |
||
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
355 |
@monkeypatch(http.Request) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
356 |
def requestReceived(self, command, path, version): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
357 |
"""Called by channel when all data has been received. |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
358 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
359 |
This method is not intended for users. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
360 |
""" |
5272 | 361 |
self.content.seek(0, 0) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
362 |
self.args = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
363 |
self.files = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
364 |
self.stack = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
365 |
self.method, self.uri = command, path |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
366 |
self.clientproto = version |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
367 |
x = self.uri.split('?', 1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
368 |
if len(x) == 1: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
369 |
self.path = self.uri |
0 | 370 |
else: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
371 |
self.path, argstring = x |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
372 |
self.args = http.parse_qs(argstring, 1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
373 |
# cache the client and server information, we'll need this later to be |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
374 |
# serialized and sent with the request so CGIs will work remotely |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
375 |
self.client = self.channel.transport.getPeer() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
376 |
self.host = self.channel.transport.getHost() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
377 |
# Argument processing |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
378 |
ctype = self.getHeader('content-type') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
379 |
if self.method == "POST" and ctype: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
380 |
key, pdict = parse_header(ctype) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
381 |
if key == 'application/x-www-form-urlencoded': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
382 |
self.args.update(http.parse_qs(self.content.read(), 1)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
383 |
elif key == 'multipart/form-data': |
5272 | 384 |
self.content.seek(0, 0) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
385 |
form = FieldStorage(self.content, self.received_headers, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
386 |
environ={'REQUEST_METHOD': 'POST'}, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
387 |
keep_blank_values=1, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
388 |
strict_parsing=1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
389 |
for key in form: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
390 |
value = form[key] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
391 |
if isinstance(value, list): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
392 |
self.args[key] = [v.value for v in value] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
393 |
elif value.filename: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
394 |
if value.done != -1: # -1 is transfer has been interrupted |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
395 |
self.files[key] = (value.filename, value.file) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
396 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
397 |
self.files[key] = (None, None) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
398 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
399 |
self.args[key] = value.value |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
400 |
self.process() |
0 | 401 |
|
402 |
||
403 |
from logging import getLogger |
|
404 |
from cubicweb import set_log_methods |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
405 |
LOGGER = getLogger('cubicweb.twisted') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
406 |
set_log_methods(CubicWebRootResource, LOGGER) |
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
|
407 |
|
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
|
408 |
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
|
409 |
# 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
|
410 |
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
|
411 |
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
|
412 |
# 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
|
413 |
port = config['port'] or 8080 |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
414 |
reactor.listenTCP(port, website) |
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
|
415 |
logger = getLogger('cubicweb.twisted') |
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
|
416 |
if not debug: |
4219
87203a0a3a81
do not attempt to daemonize under windows
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
417 |
if sys.platform == 'win32': |
4221
da84ca26896d
raising ConfigurationError is cleaner; remove some unused imports
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4219
diff
changeset
|
418 |
raise ConfigurationError("Under windows, you must use the service management " |
da84ca26896d
raising ConfigurationError is cleaner; remove some unused imports
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4219
diff
changeset
|
419 |
"commands (e.g : 'net start my_instance)'") |
3558
0e478957a9d4
smallest possible change to fix #472806
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3502
diff
changeset
|
420 |
print 'instance starting in the background' |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
421 |
if daemonize(): |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
422 |
return # child process |
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
|
423 |
if config['pid-file']: |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
424 |
# ensure the directory where the pid-file should be set exists (for |
3791
62d6ab877694
put back url info started at the end of initialisations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3641
diff
changeset
|
425 |
# instance /var/run/cubicweb may be deleted on computer restart) |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
426 |
piddir = os.path.dirname(config['pid-file']) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
427 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
428 |
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
|
429 |
file(config['pid-file'], 'w').write(str(os.getpid())) |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
430 |
root_resource.init_publisher() # before changing uid |
3193
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
431 |
if config['uid'] is not None: |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
432 |
try: |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
433 |
uid = int(config['uid']) |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
434 |
except ValueError: |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
435 |
from pwd import getpwnam |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
436 |
uid = getpwnam(config['uid']).pw_uid |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
437 |
os.setuid(uid) |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
438 |
root_resource.start_service() |
3791
62d6ab877694
put back url info started at the end of initialisations
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
3641
diff
changeset
|
439 |
logger.info('instance started on %s', root_resource.base_url) |
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
|
440 |
if config['profile']: |
4960
26b2468a1e73
[etwist] cleanup, use cProfile instead of hotshot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
441 |
import cProfile |
26b2468a1e73
[etwist] cleanup, use cProfile instead of hotshot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
442 |
cProfile.runctx('reactor.run()', globals(), locals(), config['profile']) |
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
|
443 |
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
|
444 |
reactor.run() |