author | Sylvain Thénault <sylvain.thenault@logilab.fr> |
Thu, 08 Apr 2010 13:38:36 +0200 | |
changeset 5195 | e8235b2789fc |
parent 5155 | 1dea6e0fdfc1 |
child 5216 | 4f4369e63f5e |
permissions | -rw-r--r-- |
2476
1294a6bdf3bf
application -> instance where it makes sense
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2181
diff
changeset
|
1 |
"""twisted server for CubicWeb web instances |
0 | 2 |
|
3 |
:organization: Logilab |
|
4212
ab6573088b4a
update copyright: welcome 2010
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4194
diff
changeset
|
4 |
:copyright: 2001-2010 LOGILAB S.A. (Paris, FRANCE), license is LGPL v2. |
0 | 5 |
:contact: http://www.logilab.fr/ -- mailto:contact@logilab.fr |
1977
606923dff11b
big bunch of copyright / docstring update
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
1936
diff
changeset
|
6 |
:license: GNU Lesser General Public License, v2.1 - http://www.gnu.org/licenses |
0 | 7 |
""" |
8 |
__docformat__ = "restructuredtext en" |
|
9 |
||
10 |
import sys |
|
2654
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
11 |
import os |
0 | 12 |
import select |
4398
4e7e74918631
add missing errno import in etwist/server.py
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
4221
diff
changeset
|
13 |
import errno |
1016
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
14 |
from time import mktime |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
15 |
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
|
16 |
from urlparse import urlsplit, urlunsplit |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
17 |
from cgi import FieldStorage, parse_header |
0 | 18 |
|
19 |
from twisted.internet import reactor, task, threads |
|
20 |
from twisted.internet.defer import maybeDeferred |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
21 |
from twisted.web import http, server |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
22 |
from twisted.web import static, resource |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
23 |
from twisted.web.server import NOT_DONE_YET |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
24 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
25 |
from logilab.common.decorators import monkeypatch |
0 | 26 |
|
4221
da84ca26896d
raising ConfigurationError is cleaner; remove some unused imports
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4219
diff
changeset
|
27 |
from cubicweb import ConfigurationError, CW_EVENT_MANAGER |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
28 |
from cubicweb.web import (AuthenticationError, NotFound, Redirect, |
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
29 |
RemoteCallFailed, DirectResponse, StatusResponse, |
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
30 |
ExplicitLogin) |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
31 |
|
0 | 32 |
from cubicweb.web.application import CubicWebPublisher |
33 |
||
34 |
from cubicweb.etwist.request import CubicWebTwistedRequestAdapter |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
35 |
from cubicweb.etwist.http import HTTPResponse |
0 | 36 |
|
3193
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
37 |
def daemonize(): |
3181
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
38 |
# XXX unix specific |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
39 |
# 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
|
40 |
# (start-repository command) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
41 |
# 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
|
42 |
if os.fork(): # launch child and... |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
43 |
return 1 |
3181
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
44 |
os.setsid() |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
45 |
if os.fork(): # launch child again. |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
46 |
return 1 |
3181
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
47 |
# 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
|
48 |
os.chdir('/') |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
49 |
# set paranoid umask |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
50 |
os.umask(077) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
51 |
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
|
52 |
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
|
53 |
try: |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
54 |
os.dup2(null, i) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
55 |
except OSError, e: |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
56 |
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
|
57 |
raise |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
58 |
os.close(null) |
735c5f9fcded
fix daemonize: set umask, move to /, consider uid
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2711
diff
changeset
|
59 |
return None |
0 | 60 |
|
61 |
def start_task(interval, func): |
|
62 |
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
|
63 |
# 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
|
64 |
# 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
|
65 |
lc.start(interval, now=False) |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
66 |
|
1543
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
67 |
def host_prefixed_baseurl(baseurl, host): |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
68 |
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
|
69 |
netloc_domain = '.' + '.'.join(netloc.split('.')[-2:]) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
70 |
if host.endswith(netloc_domain): |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
71 |
netloc = host |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
72 |
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
|
73 |
return baseurl |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
74 |
|
0 | 75 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
76 |
class ForbiddenDirectoryLister(resource.Resource): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
77 |
def render(self, request): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
78 |
return HTTPResponse(twisted_request=request, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
79 |
code=http.FORBIDDEN, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
80 |
stream='Access forbidden') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
81 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
82 |
class File(static.File): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
83 |
"""Prevent from listing directories""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
84 |
def directoryListing(self): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
85 |
return ForbiddenDirectoryLister() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
86 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
87 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
88 |
class LongTimeExpiringFile(File): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
89 |
"""overrides static.File and sets a far future ``Expires`` date |
0 | 90 |
on the resouce. |
91 |
||
92 |
versions handling is done by serving static files by different |
|
93 |
URLs for each version. For instance:: |
|
94 |
||
95 |
http://localhost:8080/data-2.48.2/cubicweb.css |
|
96 |
http://localhost:8080/data-2.49.0/cubicweb.css |
|
97 |
etc. |
|
98 |
||
99 |
""" |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
100 |
def render(self, request): |
0 | 101 |
def setExpireHeader(response): |
102 |
# Don't provide additional resource information to error responses |
|
103 |
if response.code < 400: |
|
104 |
# 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
|
105 |
expires = date.today() + timedelta(days=6*30) |
26387b836099
use datetime instead of mx.DateTime
sylvain.thenault@logilab.fr
parents:
151
diff
changeset
|
106 |
response.headers.setHeader('Expires', mktime(expires.timetuple())) |
0 | 107 |
return response |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
108 |
d = maybeDeferred(super(LongTimeExpiringFile, self).render, request) |
0 | 109 |
return d.addCallback(setExpireHeader) |
110 |
||
111 |
||
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
112 |
class CubicWebRootResource(resource.Resource): |
0 | 113 |
def __init__(self, config, debug=None): |
114 |
self.debugmode = debug |
|
115 |
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
|
116 |
# 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
|
117 |
# 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
|
118 |
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
|
119 |
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
|
120 |
self.https_url = config['https-url'] |
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
|
121 |
self.versioned_datadir = 'data%s' % config.instance_md5_version() |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
122 |
self.children = {} |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
123 |
|
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
124 |
def init_publisher(self): |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
125 |
config = self.config |
0 | 126 |
# when we have an in-memory repository, clean unused sessions every XX |
127 |
# seconds and properly shutdown the server |
|
128 |
if config.repo_method == 'inmemory': |
|
129 |
reactor.addSystemEventTrigger('before', 'shutdown', |
|
130 |
self.shutdown_event) |
|
131 |
if config.pyro_enabled(): |
|
132 |
# if pyro is enabled, we have to register to the pyro name |
|
133 |
# server, create a pyro daemon, and create a task to handle pyro |
|
134 |
# requests |
|
135 |
self.pyro_daemon = self.appli.repo.pyro_register() |
|
136 |
self.pyro_listen_timeout = 0.02 |
|
3502 | 137 |
self.appli.repo.looping_task(1, self.pyro_loop_event) |
0 | 138 |
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
|
139 |
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
|
140 |
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
|
141 |
|
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
142 |
def start_service(self): |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
143 |
config = self.config |
0 | 144 |
interval = min(config['cleanup-session-time'] or 120, |
145 |
config['cleanup-anonymous-session-time'] or 720) / 2. |
|
146 |
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
|
147 |
|
2685
0518ca8f63e3
[autoreload] recompute urlresolver / urlrewriter after autoreload
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2670
diff
changeset
|
148 |
def set_url_rewriter(self): |
2770
356e9d7c356d
R propagate registry API changes
Nicolas Chauvat <nicolas.chauvat@logilab.fr>
parents:
2711
diff
changeset
|
149 |
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
|
150 |
|
0 | 151 |
def shutdown_event(self): |
152 |
"""callback fired when the server is shutting down to properly |
|
153 |
clean opened sessions |
|
154 |
""" |
|
155 |
self.appli.repo.shutdown() |
|
156 |
||
157 |
def pyro_loop_event(self): |
|
158 |
"""listen for pyro events""" |
|
159 |
try: |
|
160 |
self.pyro_daemon.handleRequests(self.pyro_listen_timeout) |
|
161 |
except select.error: |
|
162 |
return |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
163 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
164 |
def getChild(self, path, request): |
0 | 165 |
"""Indicate which resource to use to process down the URL's path""" |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
166 |
pre_path = request.prePathURL() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
167 |
# XXX testing pre_path[0] not enough? |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
168 |
if any(s in pre_path |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
169 |
for s in (self.versioned_datadir, 'data', 'static')): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
170 |
# Anything in data/, static/ is treated as static files |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
171 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
172 |
if 'static' in pre_path: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
173 |
# instance static directory |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
174 |
datadir = self.config.static_directory |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
175 |
elif 'fckeditor' in pre_path: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
176 |
fckeditordir = self.config.ext_resources['FCKEDITOR_PATH'] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
177 |
return File(fckeditordir) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
178 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
179 |
# cube static data file |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
180 |
datadir = self.config.locate_resource(path) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
181 |
if datadir is None: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
182 |
return self |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
183 |
self.info('static file %s from %s', path, datadir) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
184 |
if 'data' in pre_path: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
185 |
return File(os.path.join(datadir, path)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
186 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
187 |
return LongTimeExpiringFile(datadir) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
188 |
elif path == 'fckeditor': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
189 |
fckeditordir = self.config.ext_resources['FCKEDITOR_PATH'] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
190 |
return File(fckeditordir) |
0 | 191 |
# Otherwise we use this single resource |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
192 |
return self |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
193 |
|
0 | 194 |
def render(self, request): |
195 |
"""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
|
196 |
# 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
|
197 |
if self.debugmode: |
0 | 198 |
self.appli.vreg.register_objects(self.config.vregistry_path()) |
199 |
if self.config['profile']: # default profiler don't trace threads |
|
200 |
return self.render_request(request) |
|
201 |
else: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
202 |
deferred = threads.deferToThread(self.render_request, request) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
203 |
return NOT_DONE_YET |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
204 |
|
0 | 205 |
def render_request(self, request): |
206 |
origpath = request.path |
|
207 |
host = request.host |
|
208 |
# dual http/https access handling: expect a rewrite rule to prepend |
|
209 |
# 'https' to the path to detect https access |
|
210 |
if origpath.split('/', 2)[1] == 'https': |
|
211 |
origpath = origpath[6:] |
|
212 |
request.uri = request.uri[6:] |
|
213 |
https = True |
|
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
214 |
baseurl = self.https_url or self.base_url |
0 | 215 |
else: |
216 |
https = False |
|
217 |
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
|
218 |
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
|
219 |
baseurl = host_prefixed_baseurl(baseurl, host) |
dca9817bb337
fix use-request-subdomain option behaviour and add tests
Florent <florent@secondweb.fr>
parents:
1542
diff
changeset
|
220 |
self.warning('used baseurl is %s for this request', baseurl) |
0 | 221 |
req = CubicWebTwistedRequestAdapter(request, self.appli.vreg, https, baseurl) |
222 |
if req.authmode == 'http': |
|
223 |
# activate realm-based auth |
|
224 |
realm = self.config['realm'] |
|
225 |
req.set_header('WWW-Authenticate', [('Basic', {'realm' : realm })], raw=False) |
|
226 |
try: |
|
227 |
self.appli.connect(req) |
|
228 |
except AuthenticationError: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
229 |
return self.request_auth(request=req) |
0 | 230 |
except Redirect, ex: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
231 |
return self.redirect(request=req, location=ex.location) |
0 | 232 |
if https and req.cnx.anonymous_connection: |
233 |
# don't allow anonymous on https connection |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
234 |
return self.request_auth(request=req) |
0 | 235 |
if self.url_rewriter is not None: |
1115 | 236 |
# XXX should occur before authentication? |
0 | 237 |
try: |
1936
c5af2fbda5b6
pass request to ApacheRewriter rewrite method
Florent <florent@secondweb.fr>
parents:
1543
diff
changeset
|
238 |
path = self.url_rewriter.rewrite(host, origpath, req) |
0 | 239 |
except Redirect, ex: |
240 |
return self.redirect(req, ex.location) |
|
241 |
request.uri.replace(origpath, path, 1) |
|
242 |
else: |
|
243 |
path = origpath |
|
244 |
if not path or path == "/": |
|
245 |
path = 'view' |
|
246 |
try: |
|
247 |
result = self.appli.publish(path, req) |
|
248 |
except DirectResponse, ex: |
|
249 |
return ex.response |
|
250 |
except StatusResponse, ex: |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
251 |
return HTTPResponse(stream=ex.content, code=ex.status, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
252 |
twisted_request=req._twreq, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
253 |
headers=req.headers_out) |
0 | 254 |
except RemoteCallFailed, ex: |
255 |
req.set_header('content-type', 'application/json') |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
256 |
return HTTPResponse(twisted_request=req._twreq, code=http.INTERNAL_SERVER_ERROR, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
257 |
stream=ex.dumps(), headers=req.headers_out) |
0 | 258 |
except NotFound: |
259 |
result = self.appli.notfound_content(req) |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
260 |
return HTTPResponse(twisted_request=req._twreq, code=http.NOT_FOUND, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
261 |
stream=result, headers=req.headers_out) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
262 |
|
0 | 263 |
except ExplicitLogin: # must be before AuthenticationError |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
264 |
return self.request_auth(request=req) |
4911
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4909
diff
changeset
|
265 |
except AuthenticationError, ex: |
898c35be5873
#750055: make it easier to change post logout url
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4909
diff
changeset
|
266 |
if self.config['auth-mode'] == 'cookie' and getattr(ex, 'url', None): |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
267 |
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
|
268 |
# 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
|
269 |
# information |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
270 |
return self.request_auth(request=req, loggedout=True) |
0 | 271 |
except Redirect, ex: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
272 |
return self.redirect(request=req, location=ex.location) |
0 | 273 |
# request may be referenced by "onetime callback", so clear its entity |
274 |
# cache to avoid memory usage |
|
275 |
req.drop_entity_cache() |
|
276 |
||
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
277 |
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
|
278 |
stream=result, headers=req.headers_out) |
1420
25c13e5b12bd
stop complaining about empty response, remove trailing spaces
sylvain.thenault@logilab.fr
parents:
1263
diff
changeset
|
279 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
280 |
def redirect(self, request, location): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
281 |
self.debug('redirecting to %s', str(location)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
282 |
request.headers_out.setHeader('location', str(location)) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
283 |
# 303 See other |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
284 |
return HTTPResponse(twisted_request=request._twreq, code=303, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
285 |
headers=request.headers_out) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
286 |
|
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
287 |
def request_auth(self, request, loggedout=False): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
288 |
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
|
289 |
return self.redirect(request, self.https_url + 'login') |
0 | 290 |
if self.config['auth-mode'] == 'http': |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
291 |
code = http.UNAUTHORIZED |
0 | 292 |
else: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
293 |
code = http.FORBIDDEN |
0 | 294 |
if loggedout: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
295 |
if request.https: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
296 |
request._base_url = self.base_url |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
297 |
request.https = False |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
298 |
content = self.appli.loggedout_content(request) |
0 | 299 |
else: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
300 |
content = self.appli.need_login_content(request) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
301 |
return HTTPResponse(twisted_request=request._twreq, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
302 |
stream=content, code=code, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
303 |
headers=request.headers_out) |
0 | 304 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
305 |
#TODO |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
306 |
# # XXX max upload size in the configuration |
0 | 307 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
308 |
@monkeypatch(http.Request) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
309 |
def requestReceived(self, command, path, version): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
310 |
"""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
|
311 |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
312 |
This method is not intended for users. |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
313 |
""" |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
314 |
self.content.seek(0,0) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
315 |
self.args = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
316 |
self.files = {} |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
317 |
self.stack = [] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
318 |
self.method, self.uri = command, path |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
319 |
self.clientproto = version |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
320 |
x = self.uri.split('?', 1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
321 |
if len(x) == 1: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
322 |
self.path = self.uri |
0 | 323 |
else: |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
324 |
self.path, argstring = x |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
325 |
self.args = http.parse_qs(argstring, 1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
326 |
# 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
|
327 |
# 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
|
328 |
self.client = self.channel.transport.getPeer() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
329 |
self.host = self.channel.transport.getHost() |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
330 |
# Argument processing |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
331 |
ctype = self.getHeader('content-type') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
332 |
if self.method == "POST" and ctype: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
333 |
key, pdict = parse_header(ctype) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
334 |
if key == 'application/x-www-form-urlencoded': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
335 |
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
|
336 |
elif key == 'multipart/form-data': |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
337 |
self.content.seek(0,0) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
338 |
form = FieldStorage(self.content, self.received_headers, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
339 |
environ={'REQUEST_METHOD': 'POST'}, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
340 |
keep_blank_values=1, |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
341 |
strict_parsing=1) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
342 |
for key in form: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
343 |
value = form[key] |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
344 |
if isinstance(value, list): |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
345 |
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
|
346 |
elif value.filename: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
347 |
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
|
348 |
self.files[key] = (value.filename, value.file) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
349 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
350 |
self.files[key] = (None, None) |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
351 |
else: |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
352 |
self.args[key] = value.value |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
353 |
self.process() |
0 | 354 |
|
355 |
||
356 |
from logging import getLogger |
|
357 |
from cubicweb import set_log_methods |
|
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
358 |
LOGGER = getLogger('cubicweb.twisted') |
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
359 |
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
|
360 |
|
6512522860aa
[twisted] don't use twistd anymore, all-in-one.py file is needed anymore
Adrien Di Mascio <Adrien.DiMascio@logilab.fr>
parents:
2476
diff
changeset
|
361 |
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
|
362 |
# 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
|
363 |
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
|
364 |
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
|
365 |
# 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
|
366 |
port = config['port'] or 8080 |
5155
1dea6e0fdfc1
Switched from TwistedWeb2 to TwistedWeb
Adrien Chauve <adrien.chauve@logilab.fr>
parents:
5062
diff
changeset
|
367 |
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
|
368 |
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
|
369 |
if not debug: |
4219
87203a0a3a81
do not attempt to daemonize under windows
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4212
diff
changeset
|
370 |
if sys.platform == 'win32': |
4221
da84ca26896d
raising ConfigurationError is cleaner; remove some unused imports
Aurelien Campeas <aurelien.campeas@logilab.fr>
parents:
4219
diff
changeset
|
371 |
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
|
372 |
"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
|
373 |
print 'instance starting in the background' |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
374 |
if daemonize(): |
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
375 |
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
|
376 |
if config['pid-file']: |
2711
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
377 |
# 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
|
378 |
# 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
|
379 |
piddir = os.path.dirname(config['pid-file']) |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
380 |
if not os.path.exists(piddir): |
7aee3bd7a704
ensure runtime dir exists
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
2708
diff
changeset
|
381 |
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
|
382 |
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
|
383 |
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
|
384 |
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
|
385 |
try: |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
386 |
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
|
387 |
except ValueError: |
c1a8456cb5f9
should change process'user once pid file has been written
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3181
diff
changeset
|
388 |
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
|
389 |
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
|
390 |
os.setuid(uid) |
3606
8326aceecb46
fix startup as a daemon
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
3558
diff
changeset
|
391 |
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
|
392 |
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
|
393 |
if config['profile']: |
4960
26b2468a1e73
[etwist] cleanup, use cProfile instead of hotshot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
394 |
import cProfile |
26b2468a1e73
[etwist] cleanup, use cProfile instead of hotshot
Sylvain Thénault <sylvain.thenault@logilab.fr>
parents:
4914
diff
changeset
|
395 |
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
|
396 |
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
|
397 |
reactor.run() |