annotate libervia/server/server.py @ 1237:987595a254b0

dates update
author Goffi <goffi@goffi.org>
date Wed, 29 Jan 2020 09:53:31 +0100
parents 352e189ab7f8
children 14c0f666a93d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1233
5bd801bd36aa server: fixed shebang
Goffi <goffi@goffi.org>
parents: 1228
diff changeset
1 #!/usr/bin/env python3
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
2
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
3 # Libervia: a Salut à Toi frontend
1237
987595a254b0 dates update
Goffi <goffi@goffi.org>
parents: 1236
diff changeset
4 # Copyright (C) 2011-2020 Jérôme Poisson <goffi@goffi.org>
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
5
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
6 # This program is free software: you can redistribute it and/or modify
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
7 # it under the terms of the GNU Affero General Public License as published by
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
8 # the Free Software Foundation, either version 3 of the License, or
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
9 # (at your option) any later version.
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
10
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
11 # This program is distributed in the hope that it will be useful,
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
14 # GNU Affero General Public License for more details.
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
15
339
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
16 # You should have received a copy of the GNU Affero General Public License
2067d6241927 fixed docstrings wrong usage for licence informations
Goffi <goffi@goffi.org>
parents: 336
diff changeset
17 # along with this program. If not, see <http://www.gnu.org/licenses/>.
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
18
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
19 import re
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
20 import os.path
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
21 import sys
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
22 import urllib.parse
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
23 import urllib.request, urllib.error
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
24 import time
1203
251eba911d4d server (websockets): fixed websocket handling on HTTPS connections:
Goffi <goffi@goffi.org>
parents: 1202
diff changeset
25 import copy
444
b64e528fb524 server side: use of bridge's getReady to be sure that backend is initialised at launch
Goffi <goffi@goffi.org>
parents: 438
diff changeset
26 from twisted.application import service
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
27 from twisted.internet import reactor, defer, inotify
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
28 from twisted.web import server
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
29 from twisted.web import static
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
30 from twisted.web import resource as web_resource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
31 from twisted.web import util as web_util
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
32 from twisted.web import vhost
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
33 from twisted.python.components import registerAdapter
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
34 from twisted.python import failure
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
35 from twisted.python import filepath
750
8ac862f6e5b3 browser and server sides: allow to connect with a JID that is not registered on the local server
souliane <souliane@mailoo.org>
parents: 746
diff changeset
36 from twisted.words.protocols.jabber import jid
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
37
438
582c435dab6b server side: new log system is used
Goffi <goffi@goffi.org>
parents: 435
diff changeset
38 from sat.core.log import getLogger
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
39
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
40 from sat_frontends.bridge.dbus_bridge import (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
41 Bridge,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
42 BridgeExceptionNoService,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
43 const_TIMEOUT as BRIDGE_TIMEOUT,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
44 )
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
45 from sat.core.i18n import _, D_
605
917e271975d9 server + browser side: implementation of new getEntitiesData bridge method + added a security check: only a white list of entities can be gotten
Goffi <goffi@goffi.org>
parents: 589
diff changeset
46 from sat.core import exceptions
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
47 from sat.tools import utils
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
48 from sat.tools import config
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
49 from sat.tools.common import regex
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
50 from sat.tools.common import template
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
51 from sat.tools.common import uri as common_uri
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
52 import libervia
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
53 from libervia.server import websockets
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
54 from libervia.server.pages import LiberviaPage
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
55 from libervia.server.utils import quote, ProgressHandler
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
56 from libervia.server.tasks import TasksManager
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
57 from functools import partial
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
58
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
59 try:
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
60 import OpenSSL
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
61 from twisted.internet import ssl
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
62 except ImportError:
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
63 ssl = None
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
64
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
65 from libervia.server.constants import Const as C
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents: 917
diff changeset
66 from libervia.server import session_iface
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
67
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
68 log = getLogger(__name__)
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
69
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
70
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
71 # following value are set from twisted.plugins.libervia_server initialise
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
72 # (see the comment there)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
73 DATA_DIR_DEFAULT = OPT_PARAMETERS_BOTH = OPT_PARAMETERS_CFG = None
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
74 DEFAULT_MASK = (inotify.IN_CREATE | inotify.IN_MODIFY | inotify.IN_MOVE_SELF
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
75 | inotify.IN_MOVED_TO)
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
76
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
77
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
78 class FilesWatcher(object):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
79 """Class to check files modifications using iNotify"""
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
80 _notifier = None
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
81
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
82 def __init__(self, host):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
83 self.host = host
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
84
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
85 @property
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
86 def notifier(self):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
87 if self._notifier == None:
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
88 notifier = self.__class__._notifier = inotify.INotify()
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
89 notifier.startReading()
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
90 return self._notifier
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
91
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
92 def watchDir(self, dir_path, callback, mask=DEFAULT_MASK, auto_add=False,
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
93 recursive=False, **kwargs):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
94 log.info(_("Watching directory {dir_path}").format(dir_path=dir_path))
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
95 callbacks = [lambda __, filepath, mask: callback(self.host, filepath,
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
96 inotify.humanReadableMask(mask), **kwargs)]
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
97 self.notifier.watch(
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
98 filepath.FilePath(dir_path), mask=mask, autoAdd=auto_add, recursive=recursive,
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
99 callbacks=callbacks)
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
100
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
101
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
102 class LiberviaSession(server.Session):
555
db1b330c1eb1 server_side: set the connection attempt timeout to the bridge timeout (2 minutes)
souliane <souliane@mailoo.org>
parents: 554
diff changeset
103 sessionTimeout = C.SESSION_TIMEOUT
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
104
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
105 def __init__(self, *args, **kwargs):
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
106 self.__lock = False
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
107 server.Session.__init__(self, *args, **kwargs)
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
108
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
109 def lock(self):
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
110 """Prevent session from expiring"""
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
111 self.__lock = True
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
112 self._expireCall.reset(sys.maxsize)
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
113
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
114 def unlock(self):
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
115 """Allow session to expire again, and touch it"""
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
116 self.__lock = False
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
117 self.touch()
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
118
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
119 def touch(self):
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
120 if not self.__lock:
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
121 server.Session.touch(self)
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
122
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
123
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
124 class ProtectedFile(static.File):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
125 """A static.File class which doesn't show directory listing"""
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
126
1196
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
127 def __init__(self, *args, **kwargs):
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
128 if "defaultType" not in kwargs and len(args) < 2:
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
129 # defaultType is second positional argument, and Twisted uses it
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
130 # in File.createSimilarFile, so we set kwargs only if it is missing
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
131 # in kwargs and it is not in a positional argument
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
132 kwargs["defaultType"] = "application/octet-stream"
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
133 super(ProtectedFile, self).__init__(*args, **kwargs)
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
134
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
135 def directoryListing(self):
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
136 return web_resource.NoResource()
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
137
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
138
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
139 def getChild(self, path, request):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
140 return super().getChild(path, request)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
141
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
142 def getChildWithDefault(self, path, request):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
143 return super().getChildWithDefault(path, request)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
144
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
145 def getChildForRequest(self, request):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
146 return super().getChildForRequest(request)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
147
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
148 class LiberviaRootResource(ProtectedFile):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
149 """Specialized resource for Libervia root
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
150
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
151 handle redirections declared in sat.conf
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
152 """
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
153
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
154 def __init__(self, host, host_name, site_name, site_path, *args, **kwargs):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
155 ProtectedFile.__init__(self, *args, **kwargs)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
156 self.host = host
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
157 self.host_name = host_name
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
158 self.site_name = site_name
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
159 self.site_path = site_path
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
160 self.named_pages = {}
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
161 self.uri_callbacks = {}
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
162 self.pages_redirects = {}
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
163 self.cached_urls = {}
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
164 self.main_menu = None
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
165
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
166 def __str__(self):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
167 return ("Root resource for {host_name} using {site_name} at {site_path} and "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
168 "deserving files at {path}".format(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
169 host_name=self.host_name, site_name=self.site_name,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
170 site_path=self.site_path, path=self.path))
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
171
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
172 def _initRedirections(self, options):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
173 url_redirections = options["url_redirections_dict"]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
174
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
175 url_redirections = url_redirections.get(self.site_name, {})
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
176
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
177 ## redirections
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
178 self.redirections = {}
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
179 self.inv_redirections = {} # new URL to old URL map
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
180
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
181 for old, new_data in url_redirections.items():
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
182 # new_data can be a dictionary or a unicode url
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
183 if isinstance(new_data, dict):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
184 # new_data dict must contain either "url", "page" or "path" key
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
185 # (exclusive)
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
186 # if "path" is used, a file url is constructed with it
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
187 if len({"path", "url", "page"}.intersection(list(new_data.keys()))) != 1:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
188 raise ValueError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
189 'You must have one and only one of "url", "page" or "path" key '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
190 'in your url_redirections_dict data')
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
191 if "url" in new_data:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
192 new = new_data["url"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
193 elif "page" in new_data:
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
194 new = new_data
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
195 new["type"] = "page"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
196 new.setdefault("path_args", [])
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
197 if not isinstance(new["path_args"], list):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
198 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
199 _('"path_args" in redirection of {old} must be a list. '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
200 'Ignoring the redirection'.format(old=old)))
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
201 continue
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
202 new.setdefault("query_args", {})
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
203 if not isinstance(new["query_args"], dict):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
204 log.error(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
205 _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
206 '"query_args" in redirection of {old} must be a '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
207 'dictionary. Ignoring the redirection'.format(old=old)))
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
208 continue
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
209 new["path_args"] = [quote(a) for a in new["path_args"]]
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
210 # we keep an inversed dict of page redirection
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
211 # (page/path_args => redirecting URL)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
212 # so getURL can return the redirecting URL if the same arguments
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
213 # are used # making the URL consistent
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
214 args_hash = tuple(new["path_args"])
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
215 self.pages_redirects.setdefault(new_data["page"], {})[
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
216 args_hash
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
217 ] = old
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
218
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
219 # we need lists in query_args because it will be used
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
220 # as it in request.path_args
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
221 for k, v in new["query_args"].items():
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
222 if isinstance(v, str):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
223 new["query_args"][k] = [v]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
224 elif "path" in new_data:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
225 new = "file:{}".format(urllib.parse.quote(new_data["path"]))
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
226 elif isinstance(new_data, str):
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
227 new = new_data
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
228 new_data = {}
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
229 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
230 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
231 _("ignoring invalid redirection value: {new_data}").format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
232 new_data=new_data
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
233 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
234 )
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
235 continue
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
236
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
237 # some normalization
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
238 if not old.strip():
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
239 # root URL special case
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
240 old = ""
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
241 elif not old.startswith("/"):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
242 log.error(_("redirected url must start with '/', got {value}. Ignoring")
1129
e6fe914c3eaf server: move first level dict where the key has "/" to default site in _moveFirstLevelToDict
Goffi <goffi@goffi.org>
parents: 1128
diff changeset
243 .format(value=old))
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
244 continue
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
245 else:
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
246 old = self._normalizeURL(old)
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
247
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
248 if isinstance(new, dict):
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
249 # dict are handled differently, they contain data
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
250 # which ared use dynamically when the request is done
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
251 self.redirections[old] = new
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
252 if not old:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
253 if new["type"] == "page":
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
254 log.info(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
255 _("Root URL redirected to page {name}").format(
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
256 name=new["page"]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
257 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
258 )
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
259 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
260 if new["type"] == "page":
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
261 page = self.getPageByName(new["page"])
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
262 url = page.getURL(*new.get("path_args", []))
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
263 self.inv_redirections[url] = old
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
264 continue
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
265
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
266 # at this point we have a redirection URL in new, we can parse it
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
267 new_url = urllib.parse.urlsplit(new)
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
268
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
269 # we handle the known URL schemes
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
270 if new_url.scheme == "xmpp":
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
271 location = self.getPagePathFromURI(new)
1038
6b906b1f419a pages: fixed XMPP URIs handling
Goffi <goffi@goffi.org>
parents: 1037
diff changeset
272 if location is None:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
273 log.warning(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
274 _("ignoring redirection, no page found to handle this URI: "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
275 "{uri}").format(uri=new))
1038
6b906b1f419a pages: fixed XMPP URIs handling
Goffi <goffi@goffi.org>
parents: 1037
diff changeset
276 continue
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
277 request_data = self._getRequestData(location)
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
278 if old:
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
279 self.inv_redirections[location] = old
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
280
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
281 elif new_url.scheme in ("", "http", "https"):
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
282 # direct redirection
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
283 if new_url.netloc:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
284 raise NotImplementedError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
285 "netloc ({netloc}) is not implemented yet for "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
286 "url_redirections_dict, it is not possible to redirect to an "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
287 "external website".format(netloc=new_url.netloc))
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
288 location = urllib.parse.urlunsplit(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
289 ("", "", new_url.path, new_url.query, new_url.fragment)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
290 )
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
291 request_data = self._getRequestData(location)
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
292 if old:
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
293 self.inv_redirections[location] = old
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
294
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
295 elif new_url.scheme == "file":
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
296 # file or directory
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
297 if new_url.netloc:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
298 raise NotImplementedError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
299 "netloc ({netloc}) is not implemented for url redirection to "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
300 "file system, it is not possible to redirect to an external "
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
301 "host".format(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
302 netloc=new_url.netloc))
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
303 path = urllib.parse.unquote(new_url.path)
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
304 if not os.path.isabs(path):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
305 raise ValueError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
306 "file redirection must have an absolute path: e.g. "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
307 "file:/path/to/my/file")
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
308 # for file redirection, we directly put child here
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
309 segments, __, last_segment = old.rpartition("/")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
310 url_segments = segments.split("/") if segments else []
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
311 current = self
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
312 for segment in url_segments:
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
313 resource = web_resource.NoResource()
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
314 current.putChild(segment, resource)
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
315 current = resource
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
316 resource_class = (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
317 ProtectedFile if new_data.get("protected", True) else static.File
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
318 )
1196
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
319 current.putChild(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
320 last_segment.encode('utf-8'),
1196
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
321 resource_class(path, defaultType="application/octet-stream")
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
322 )
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
323 log.info("[{host_name}] Added redirection from /{old} to file system "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
324 "path {path}".format(host_name=self.host_name,
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
325 old=old,
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
326 path=path))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
327 continue # we don't want to use redirection system, so we continue here
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
328
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
329 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
330 raise NotImplementedError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
331 "{scheme}: scheme is not managed for url_redirections_dict".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
332 scheme=new_url.scheme
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
333 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
334 )
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
335
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
336 self.redirections[old] = request_data
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
337 if not old:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
338 log.info(_("[{host_name}] Root URL redirected to {uri}")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
339 .format(host_name=self.host_name,
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
340 uri=request_data[1]))
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
341
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
342 # the default root URL, if not redirected
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
343 if not "" in self.redirections:
1202
3f791fbc1643 server: if "redirections" is not set, use "/login" page as default
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
344 self.redirections[""] = self._getRequestData(C.LIBERVIA_PAGE_START)
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
345
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
346 def _setMenu(self, menus):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
347 menus = menus.get(self.site_name, [])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
348 main_menu = []
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
349 for menu in menus:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
350 if not menu:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
351 msg = _("menu item can't be empty")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
352 log.error(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
353 raise ValueError(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
354 elif isinstance(menu, list):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
355 if len(menu) != 2:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
356 msg = _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
357 "menu item as list must be in the form [page_name, absolue URL]"
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
358 )
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
359 log.error(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
360 raise ValueError(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
361 page_name, url = menu
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
362 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
363 page_name = menu
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
364 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
365 url = self.getPageByName(page_name).url
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
366 except KeyError as e:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
367 log_msg = _("Can'find a named page ({msg}), please check "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
368 "menu_json in configuration.").format(msg=e.args[0])
1131
9cf592d1e6aa server: better error handling when a named page mentioned in menu_json is missing
Goffi <goffi@goffi.org>
parents: 1130
diff changeset
369 log.error(log_msg)
9cf592d1e6aa server: better error handling when a named page mentioned in menu_json is missing
Goffi <goffi@goffi.org>
parents: 1130
diff changeset
370 raise exceptions.ConfigError(log_msg)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
371 main_menu.append((page_name, url))
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
372 self.main_menu = main_menu
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
373
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
374 def _normalizeURL(self, url, lower=True):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
375 """Return URL normalized for self.redirections dict
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
376
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
377 @param url(unicode): URL to normalize
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
378 @param lower(bool): lower case of url if True
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
379 @return (str): normalized URL
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
380 """
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
381 if lower:
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
382 url = url.lower()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
383 return "/".join((p for p in url.split("/") if p))
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
384
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
385 def _getRequestData(self, uri):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
386 """Return data needed to redirect request
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
387
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
388 @param url(unicode): destination url
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
389 @return (tuple(list[str], str, str, dict): tuple with
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
390 splitted path as in Request.postpath
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
391 uri as in Request.uri
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
392 path as in Request.path
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
393 args as in Request.args
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
394 """
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
395 uri = uri
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
396 # XXX: we reuse code from twisted.web.http.py here
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
397 # as we need to have the same behaviour
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
398 x = uri.split("?", 1)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
399
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
400 if len(x) == 1:
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
401 path = uri
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
402 args = {}
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
403 else:
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
404 path, argstring = x
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
405 args = urllib.parse.parse_qs(argstring, True)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
406
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
407 # XXX: splitted path case must not be changed, as it may be significant
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
408 # (e.g. for blog items)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
409 return (
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
410 self._normalizeURL(path, lower=False).split("/"),
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
411 uri,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
412 path,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
413 args,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
414 )
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
415
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
416 def _redirect(self, request, request_data):
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
417 """Redirect an URL by rewritting request
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
418
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
419 this is *NOT* a HTTP redirection, but equivalent to URL rewritting
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
420 @param request(web.http.request): original request
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
421 @param request_data(tuple): data returned by self._getRequestData
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
422 @return (web_resource.Resource): resource to use
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
423 """
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
424 # recursion check
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
425 try:
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
426 request._redirected
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
427 except AttributeError:
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
428 pass
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
429 else:
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
430 try:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
431 __, uri, __, __ = request_data
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
432 except ValueError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
433 uri = ""
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
434 log.error(D_( "recursive redirection, please fix this URL:\n"
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
435 "{old} ==> {new}").format(
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
436 old=request.uri.decode("utf-8"), new=uri))
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
437 return web_resource.NoResource()
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
438
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
439 request._redirected = True # here to avoid recursive redirections
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
440
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
441 if isinstance(request_data, dict):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
442 if request_data["type"] == "page":
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
443 try:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
444 page = self.getPageByName(request_data["page"])
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
445 except KeyError:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
446 log.error(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
447 _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
448 'Can\'t find page named "{name}" requested in redirection'
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
449 ).format(name=request_data["page"])
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
450 )
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
451 return web_resource.NoResource()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
452 path_args = [pa.encode('utf-8') for pa in request_data["path_args"]]
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
453 request.postpath = path_args + request.postpath
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
454
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
455 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
456 request.args.update(request_data["query_args"])
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
457 except (TypeError, ValueError):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
458 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
459 _("Invalid args in redirection: {query_args}").format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
460 query_args=request_data["query_args"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
461 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
462 )
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
463 return web_resource.NoResource()
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
464 return page
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
465 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
466 raise exceptions.InternalError("unknown request_data type")
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
467 else:
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
468 path_list, uri, path, args = request_data
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
469 path_list = [p.encode('utf-8') for p in path_list]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
470 log.debug(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
471 "Redirecting URL {old} to {new}".format(
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
472 old=request.uri.decode('utf-8'), new=uri
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
473 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
474 )
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
475 # we change the request to reflect the new url
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
476 request.postpath = path_list[1:] + request.postpath
1158
fb4b4da61132 backend: fixed args transmission on page redirection.
Goffi <goffi@goffi.org>
parents: 1153
diff changeset
477 request.args.update(args)
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
478
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
479 # we start again to look for a child with the new url
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
480 return self.getChildWithDefault(path_list[0], request)
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
481
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
482 def getPageByName(self, name):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
483 """Retrieve page instance from its name
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
484
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
485 @param name(unicode): name of the page
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
486 @return (LiberviaPage): page instance
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
487 @raise KeyError: the page doesn't exist
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
488 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
489 return self.named_pages[name]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
490
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
491 def getPagePathFromURI(self, uri):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
492 """Retrieve page URL from xmpp: URI
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
493
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
494 @param uri(unicode): URI with a xmpp: scheme
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
495 @return (unicode,None): absolute path (starting from root "/") to page handling
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
496 the URI.
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
497 None is returned if no page has been registered for this URI
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
498 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
499 uri_data = common_uri.parseXMPPUri(uri)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
500 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
501 page, cb = self.uri_callbacks[uri_data["type"], uri_data["sub_type"]]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
502 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
503 url = None
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
504 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
505 url = cb(page, uri_data)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
506 if url is None:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
507 # no handler found
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
508 # we try to find a more generic one
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
509 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
510 page, cb = self.uri_callbacks[uri_data["type"], None]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
511 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
512 pass
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
513 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
514 url = cb(page, uri_data)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
515 return url
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
516
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
517 def getChildWithDefault(self, name, request):
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
518 # XXX: this method is overriden only for root url
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
519 # which is the only ones who need to be handled before other children
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
520 if name == b"" and not request.postpath:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
521 return self._redirect(request, self.redirections[""])
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
522 return super(LiberviaRootResource, self).getChildWithDefault(name, request)
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
523
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
524 def getChild(self, name, request):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
525 resource = super(LiberviaRootResource, self).getChild(name, request)
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
526
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
527 if isinstance(resource, web_resource.NoResource):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
528 # if nothing was found, we try our luck with redirections
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
529 # XXX: we want redirections to happen only if everything else failed
1047
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
530 path_elt = request.prepath + request.postpath
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
531 for idx in range(len(path_elt), 0, -1):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
532 test_url = b"/".join(path_elt[:idx]).decode('utf-8').lower()
1047
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
533 if test_url in self.redirections:
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
534 request_data = self.redirections[test_url]
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
535 request.postpath = path_elt[idx:]
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
536 return self._redirect(request, request_data)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
537
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
538 return resource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
539
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
540 def putChild(self, path, resource):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
541 """Add a child to the root resource"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
542 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
543 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
544 if not isinstance(resource, web_resource.EncodingResourceWrapper):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
545 # FIXME: check that no information is leaked (c.f. https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#request-encoders)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
546 resource = web_resource.EncodingResourceWrapper(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
547 resource, [server.GzipEncoderFactory()])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
548
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
549 super(LiberviaRootResource, self).putChild(path, resource)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
550
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
551 def createSimilarFile(self, path):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
552 # XXX: this method need to be overriden to avoid recreating a LiberviaRootResource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
553
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
554 f = LiberviaRootResource.__base__(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
555 path, self.defaultType, self.ignoredExts, self.registry
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
556 )
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
557 # refactoring by steps, here - constructor should almost certainly take these
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
558 f.processors = self.processors
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
559 f.indexNames = self.indexNames[:]
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
560 f.childNotFound = self.childNotFound
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
561 return f
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
562
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
563
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
564 class WaitingRequests(dict):
796
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
565 def setRequest(self, request, profile, register_with_ext_jid=False):
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
566 """Add the given profile to the waiting list.
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
567
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
568 @param request (server.Request): the connection request
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
569 @param profile (str): %(doc_profile)s
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
570 @param register_with_ext_jid (bool): True if we will try to register the
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
571 profile with an external XMPP account credentials
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
572 """
555
db1b330c1eb1 server_side: set the connection attempt timeout to the bridge timeout (2 minutes)
souliane <souliane@mailoo.org>
parents: 554
diff changeset
573 dc = reactor.callLater(BRIDGE_TIMEOUT, self.purgeRequest, profile)
796
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
574 self[profile] = (request, dc, register_with_ext_jid)
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
575
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
576 def purgeRequest(self, profile):
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
577 """Remove the given profile from the waiting list.
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
578
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
579 @param profile (str): %(doc_profile)s
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
580 """
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
581 try:
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
582 dc = self[profile][1]
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
583 except KeyError:
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
584 return
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
585 if dc.active():
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
586 dc.cancel()
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
587 del self[profile]
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
588
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
589 def getRequest(self, profile):
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
590 """Get the waiting request for the given profile.
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
591
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
592 @param profile (str): %(doc_profile)s
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
593 @return: the waiting request or None
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
594 """
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
595 return self[profile][0] if profile in self else None
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
596
796
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
597 def getRegisterWithExtJid(self, profile):
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
598 """Get the value of the register_with_ext_jid parameter.
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
599
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
600 @param profile (str): %(doc_profile)s
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
601 @return: bool or None
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
602 """
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
603 return self[profile][2] if profile in self else None
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
604
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
605
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
606 class Libervia(service.Service):
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
607 debug = defer.Deferred.debug # True if twistd/Libervia is launched in debug mode
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
608
810
3905bc24eb17 server: proper options handling
Goffi <goffi@goffi.org>
parents: 801
diff changeset
609 def __init__(self, options):
3905bc24eb17 server: proper options handling
Goffi <goffi@goffi.org>
parents: 801
diff changeset
610 self.options = options
1236
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
611
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
612 def _init(self):
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
613 # we do init here and not in __init__ to avoid doule initialisation with twistd
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
614 # this _init is called in startService
444
b64e528fb524 server side: use of bridge's getReady to be sure that backend is initialised at launch
Goffi <goffi@goffi.org>
parents: 438
diff changeset
615 self.initialised = defer.Deferred()
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
616 self.waiting_profiles = WaitingRequests() # FIXME: should be removed
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
617 self._main_conf = None
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
618 self.files_watcher = FilesWatcher(self)
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
619
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
620 if self.options["base_url_ext"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
621 self.base_url_ext = self.options.pop("base_url_ext")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
622 if self.base_url_ext[-1] != "/":
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
623 self.base_url_ext += "/"
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
624 self.base_url_ext_data = urllib.parse.urlsplit(self.base_url_ext)
881
6bdee34fa2f4 server: added base_url_ext option to handle different external URLs (e.g.: if there is a proxy)
Goffi <goffi@goffi.org>
parents: 875
diff changeset
625 else:
6bdee34fa2f4 server: added base_url_ext option to handle different external URLs (e.g.: if there is a proxy)
Goffi <goffi@goffi.org>
parents: 875
diff changeset
626 self.base_url_ext = None
884
763da94ba28b blog (atom): base_url_ext can now only specify scheme, or netloc, or path, it will be used to complete data coming from request's path
Goffi <goffi@goffi.org>
parents: 881
diff changeset
627 # we split empty string anyway so we can do things like
763da94ba28b blog (atom): base_url_ext can now only specify scheme, or netloc, or path, it will be used to complete data coming from request's path
Goffi <goffi@goffi.org>
parents: 881
diff changeset
628 # scheme = self.base_url_ext_data.scheme or 'https'
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
629 self.base_url_ext_data = urllib.parse.urlsplit("")
884
763da94ba28b blog (atom): base_url_ext can now only specify scheme, or netloc, or path, it will be used to complete data coming from request's path
Goffi <goffi@goffi.org>
parents: 881
diff changeset
630
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
631 if not self.options["port_https_ext"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
632 self.options["port_https_ext"] = self.options["port_https"]
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
633
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
634 self._cleanup = []
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
635
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
636 self.sessions = {} # key = session value = user
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
637 self.prof_connected = set() # Profiles connected
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
638 self.ns_map = {} # map of short name to namespaces
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
639
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
640 ## bridge ##
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
641 self.bridge = Bridge()
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
642 self.bridge.bridgeConnect(callback=self._bridgeCb, errback=self._bridgeEb)
553
8492c2bb463b server_side: enable HTTP gzip compression
souliane <souliane@mailoo.org>
parents: 522
diff changeset
643
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
644 @property
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
645 def roots(self):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
646 """Return available virtual host roots
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
647
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
648 Root resources are only returned once, even if they are present for multiple
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
649 named vhosts. Order is not relevant, except for default vhost which is always
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
650 returned first.
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
651 @return (list[web_resource.Resource]): all vhost root resources
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
652 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
653 roots = list(set(self.vhost_root.hosts.values()))
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
654 default = self.vhost_root.default
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
655 if default is not None and default not in roots:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
656 roots.insert(0, default)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
657 return roots
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
658
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
659 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
660 def main_conf(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
661 """SafeConfigParser instance opened on configuration file (sat.conf)"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
662 if self._main_conf is None:
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
663 self._main_conf = config.parseMainConf()
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
664 return self._main_conf
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
665
1147
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
666 def getConfig(self, site_root_res, key, default=None, value_type=None):
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
667 """Retrieve configuration associated to a site
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
668
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
669 Section is automatically set to site name
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
670 @param site_root_res(LiberviaRootResource): resource of the site in use
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
671 @param key(unicode): key to use
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
672 @param default: value to use if not found (see [config.getConfig])
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
673 @param value_type(unicode, None): filter to use on value
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
674 Note that filters are already automatically used when the key finish
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
675 by a well known suffix ("_path", "_list", "_dict", or "_json")
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
676 None to use no filter, else can be:
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
677 - "path": a path is expected, will be normalized and expanded
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
678
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
679 """
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
680 section = site_root_res.site_name.lower().strip()
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
681 value = config.getConfig(self.main_conf, section, key, default=default)
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
682 if value_type is not None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
683 if value_type == 'path':
1147
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
684 v_filter = lambda v: os.path.abspath(os.path.expanduser(v))
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
685 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
686 raise ValueError("unknown value type {value_type}".format(
1147
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
687 value_type = value_type))
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
688 if isinstance(value, list):
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
689 value = [v_filter(v) for v in value]
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
690 elif isinstance(value, dict):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
691 value = {k:v_filter(v) for k,v in list(value.items())}
1147
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
692 elif value is not None:
1217
fe9782391f63 server: fixed value filtering in getConfig
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
693 value = v_filter(value)
1147
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
694 return value
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
695
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
696 def _namespacesGetCb(self, ns_map):
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
697 self.ns_map = ns_map
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
698
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
699 def _namespacesGetEb(self, failure_):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
700 log.error(_("Can't get namespaces map: {msg}").format(msg=failure_))
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
701
1130
8a7d75c18d40 server: fixed front_url_filter in templates renderer
Goffi <goffi@goffi.org>
parents: 1129
diff changeset
702 @template.contextfilter
1127
9234f29053b0 server, pages: update to handle multi sites themes, first draft:
Goffi <goffi@goffi.org>
parents: 1126
diff changeset
703 def _front_url_filter(self, ctx, relative_url):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
704 template_data = ctx['template_data']
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
705 return os.path.join('/', C.TPL_RESOURCE, template_data.site or 'sat',
1127
9234f29053b0 server, pages: update to handle multi sites themes, first draft:
Goffi <goffi@goffi.org>
parents: 1126
diff changeset
706 C.TEMPLATE_TPL_DIR, template_data.theme, relative_url)
9234f29053b0 server, pages: update to handle multi sites themes, first draft:
Goffi <goffi@goffi.org>
parents: 1126
diff changeset
707
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
708 def _moveFirstLevelToDict(self, options, key, keys_to_keep):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
709 """Read a config option and put value at first level into u'' dict
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
710
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
711 This is useful to put values for Libervia official site directly in dictionary,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
712 and to use site_name as keys when external sites are used.
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
713 options will be modified in place
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
714 @param options(dict): options to modify
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
715 @param key(unicode): setting key to modify
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
716 @param keys_to_keep(list(unicode)): keys allowed in first level
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
717 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
718 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
719 conf = options[key]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
720 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
721 return
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
722 if not isinstance(conf, dict):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
723 options[key] = {'': conf}
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
724 return
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
725 default_dict = conf.get('', {})
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
726 to_delete = []
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
727 for key, value in conf.items():
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
728 if key not in keys_to_keep:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
729 default_dict[key] = value
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
730 to_delete.append(key)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
731 for key in to_delete:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
732 del conf[key]
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
733 if default_dict:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
734 conf[''] = default_dict
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
735
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
736 @defer.inlineCallbacks
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
737 def backendReady(self, __):
1235
40c13ba6ba94 server: remove "launching" while logging version
Goffi <goffi@goffi.org>
parents: 1234
diff changeset
738 log.info(f"Libervia v{self.full_version}")
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
739 if self.options['dev_mode']:
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
740 log.info(_("Developer mode activated"))
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
741 self.media_dir = self.bridge.getConfig("", "media_dir")
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
742 self.local_dir = self.bridge.getConfig("", "local_dir")
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
743 self.cache_root_dir = os.path.join(self.local_dir, C.CACHE_DIR)
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
744 self.renderer = template.Renderer(self, self._front_url_filter)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
745 sites_names = list(self.renderer.sites_paths.keys())
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
746
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
747 self._moveFirstLevelToDict(self.options, "url_redirections_dict", sites_names)
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
748 self._moveFirstLevelToDict(self.options, "menu_json", sites_names)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
749 if not '' in self.options["menu_json"]:
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
750 self.options["menu_json"][''] = C.DEFAULT_MENU
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
751
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
752 # we create virtual hosts and import Libervia pages into them
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
753 self.vhost_root = vhost.NameVirtualHost()
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
754 default_site_path = os.path.abspath(os.path.dirname(libervia.__file__))
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
755 # self.sat_root is official Libervia site
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
756 root_path = os.path.join(default_site_path, C.TEMPLATE_STATIC_DIR)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
757 self.sat_root = default_root = LiberviaRootResource(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
758 host=self, host_name='', site_name='', site_path=default_site_path,
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
759 path=root_path)
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
760 if self.options['dev_mode']:
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
761 self.files_watcher.watchDir(
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
762 default_site_path, auto_add=True, recursive=True,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
763 callback=LiberviaPage.onFileChange, site_root=self.sat_root,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
764 site_path=default_site_path)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
765 tasks_manager = TasksManager(self, self.sat_root)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
766 yield tasks_manager.runTasks()
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
767 LiberviaPage.importPages(self, self.sat_root)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
768 # FIXME: handle _setMenu in a more generic way, taking care of external sites
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
769 self.sat_root._setMenu(self.options["menu_json"])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
770 self.vhost_root.default = default_root
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
771 existing_vhosts = {b'': default_root}
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
772
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
773 for host_name, site_name in self.options["vhosts_dict"].items():
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
774 encoded_site_name = site_name.encode('utf-8')
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
775 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
776 site_path = self.renderer.sites_paths[site_name]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
777 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
778 log.warning(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
779 "host {host_name} link to non existing site {site_name}, ignoring "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
780 "it").format(host_name=host_name, site_name=site_name))
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
781 continue
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
782 if encoded_site_name in existing_vhosts:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
783 # we have an alias host, we re-use existing resource
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
784 res = existing_vhosts[encoded_site_name]
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
785 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
786 # for root path we first check if there is a global static dir
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
787 # if not, we use default template's static dic
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
788 root_path = os.path.join(site_path, C.TEMPLATE_STATIC_DIR)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
789 if not os.path.isdir(root_path):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
790 root_path = os.path.join(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
791 site_path, C.TEMPLATE_TPL_DIR, C.TEMPLATE_THEME_DEFAULT,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
792 C.TEMPLATE_STATIC_DIR)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
793 res = LiberviaRootResource(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
794 host=self,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
795 host_name=host_name,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
796 site_name=site_name,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
797 site_path=site_path,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
798 path=root_path)
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
799
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
800 existing_vhosts[encoded_site_name] = res
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
801
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
802 if self.options['dev_mode']:
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
803 self.files_watcher.watchDir(
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
804 site_path, auto_add=True, recursive=True,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
805 callback=LiberviaPage.onFileChange, site_root=res,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
806 site_path=site_path)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
807 tasks_manager = TasksManager(self, res)
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
808 yield tasks_manager.runTasks()
1196
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
809 res.putChild(
1218
c9fc839d304c server: fixed BUILD_DIR page encoding following python 3 port
Goffi <goffi@goffi.org>
parents: 1217
diff changeset
810 C.BUILD_DIR.encode('utf-8'),
1228
f3edbca73713 server: user ProtectedFile for build dir
Goffi <goffi@goffi.org>
parents: 1224
diff changeset
811 ProtectedFile(
f3edbca73713 server: user ProtectedFile for build dir
Goffi <goffi@goffi.org>
parents: 1224
diff changeset
812 self.getBuildPath(site_name),
f3edbca73713 server: user ProtectedFile for build dir
Goffi <goffi@goffi.org>
parents: 1224
diff changeset
813 defaultType="application/octet-stream"),
1196
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
814 )
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
815
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
816 LiberviaPage.importPages(self, res)
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
817 # FIXME: default pages are accessible if not overriden by external website
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
818 # while necessary for login or re-using existing pages
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
819 # we may want to disable access to the page by direct URL
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
820 # (e.g. /blog disabled except if called by external site)
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
821 LiberviaPage.importPages(self, res, root_path=default_site_path)
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
822 res._setMenu(self.options["menu_json"])
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
823
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
824 self.vhost_root.addHost(host_name.encode('utf-8'), res)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
825
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
826 templates_res = web_resource.Resource()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
827 self.putChildAll(C.TPL_RESOURCE.encode('utf-8'), templates_res)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
828 for site_name, site_path in self.renderer.sites_paths.items():
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
829 templates_res.putChild(site_name.encode('utf-8') or b'sat',
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
830 ProtectedFile(site_path))
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
831
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
832 d = self.bridgeCall("namespacesGet")
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
833 d.addCallback(self._namespacesGetCb)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
834 d.addErrback(self._namespacesGetEb)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
835
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
836 # websocket
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
837 if self.options["connection_type"] in ("https", "both"):
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
838 wss = websockets.LiberviaPageWSProtocol.getResource(self, secure=True)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
839 self.putChildAll(b'wss', wss)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
840 if self.options["connection_type"] in ("http", "both"):
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
841 ws = websockets.LiberviaPageWSProtocol.getResource(self, secure=False)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
842 self.putChildAll(b'ws', ws)
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
843
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
844 ## following signal is needed for cache handling in Libervia pages
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
845 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
846 "psEventRaw", partial(LiberviaPage.onNodeEvent, self), "plugin"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
847 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
848 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
849 "messageNew", partial(LiberviaPage.onSignal, self, "messageNew")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
850 )
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
851
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
852 #  Progress handling
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
853 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
854 "progressStarted", partial(ProgressHandler._signal, "started")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
855 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
856 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
857 "progressFinished", partial(ProgressHandler._signal, "finished")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
858 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
859 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
860 "progressError", partial(ProgressHandler._signal, "error")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
861 )
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
862
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
863 # media dirs
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
864 # FIXME: get rid of dirname and "/" in C.XXX_DIR
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
865 self.putChildAll(os.path.dirname(C.MEDIA_DIR).encode('utf-8'),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
866 ProtectedFile(self.media_dir))
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
867 self.cache_resource = web_resource.NoResource()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
868 self.putChildAll(C.CACHE_DIR.encode('utf-8'), self.cache_resource)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
869
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
870 # redirections
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
871 for root in self.roots:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
872 root._initRedirections(self.options)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
873
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
874 # no need to keep url_redirections_dict, it will not be used anymore
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
875 del self.options["url_redirections_dict"]
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
876
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
877 server.Request.defaultContentType = "text/html; charset=utf-8"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
878 wrapped = web_resource.EncodingResourceWrapper(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
879 self.vhost_root, [server.GzipEncoderFactory()]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
880 )
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
881 self.site = server.Site(wrapped)
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
882 self.site.sessionFactory = LiberviaSession
444
b64e528fb524 server side: use of bridge's getReady to be sure that backend is initialised at launch
Goffi <goffi@goffi.org>
parents: 438
diff changeset
883
1003
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 995
diff changeset
884 def initEb(self, failure):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
885 log.error(_("Init error: {msg}").format(msg=failure))
1003
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 995
diff changeset
886 reactor.stop()
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 995
diff changeset
887 return failure
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
888
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
889 def _bridgeCb(self):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
890 self.bridge.getReady(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
891 lambda: self.initialised.callback(None),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
892 lambda failure: self.initialised.errback(Exception(failure)),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
893 )
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
894 self.initialised.addCallback(self.backendReady)
1003
05cc33d8e328 server: better error message on init issue / menu_json trouble + stop reactor in this case.
Goffi <goffi@goffi.org>
parents: 995
diff changeset
895 self.initialised.addErrback(self.initEb)
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
896
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
897 def _bridgeEb(self, failure_):
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
898 if isinstance(failure_, BridgeExceptionNoService):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
899 print("Can't connect to SàT backend, are you sure it's launched ?")
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
900 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
901 log.error("Can't connect to bridge: {}".format(failure))
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
902 sys.exit(1)
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
903
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
904 @property
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
905 def version(self):
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
906 """Return the short version of Libervia"""
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
907 return C.APP_VERSION
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
908
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
909 @property
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
910 def full_version(self):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
911 """Return the full version of Libervia (with extra data when in dev mode)"""
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
912 version = self.version
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
913 if version[-1] == "D":
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
914 # we are in debug version, we add extra data
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
915 try:
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
916 return self._version_cache
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
917 except AttributeError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
918 self._version_cache = "{} ({})".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
919 version, utils.getRepositoryData(libervia)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
920 )
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
921 return self._version_cache
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
922 else:
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
923 return version
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
924
935
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
925 def bridgeCall(self, method_name, *args, **kwargs):
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
926 """Call an asynchronous bridge method and return a deferred
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
927
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
928 @param method_name: name of the method as a unicode
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
929 @return: a deferred which trigger the result
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
930
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
931 """
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
932 d = defer.Deferred()
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
933
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
934 def _callback(*args):
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
935 if not args:
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
936 d.callback(None)
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
937 else:
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
938 if len(args) != 1:
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
939 Exception("Multiple return arguments not supported")
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
940 d.callback(args[0])
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
941
1138
ef565839dada server: don't convert failure in errback to jsonrpclib.Fault anymore:
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
942 def _errback(failure_):
ef565839dada server: don't convert failure in errback to jsonrpclib.Fault anymore:
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
943 d.errback(failure.Failure(failure_))
935
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
944
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
945 kwargs["callback"] = _callback
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
946 kwargs["errback"] = _errback
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
947 getattr(self.bridge, method_name)(*args, **kwargs)
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
948 return d
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
949
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
950 @defer.inlineCallbacks
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
951 def _logged(self, profile, request):
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
952 """Set everything when a user just logged in
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
953
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
954 @param profile
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
955 @param request
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
956 @return: a constant indicating the state:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
957 - C.PROFILE_LOGGED
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
958 - C.PROFILE_LOGGED_EXT_JID
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
959 @raise exceptions.ConflictError: session is already active
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
960 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
961 register_with_ext_jid = self.waiting_profiles.getRegisterWithExtJid(profile)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
962 self.waiting_profiles.purgeRequest(profile)
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
963 session = request.getSession()
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
964 sat_session = session_iface.ISATSession(session)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
965 if sat_session.profile:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
966 log.error(_("/!\\ Session has already a profile, this should NEVER happen!"))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
967 raise failure.Failure(exceptions.ConflictError("Already active"))
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
968
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
969 sat_session.profile = profile
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
970 self.prof_connected.add(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
971 cache_dir = os.path.join(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
972 self.cache_root_dir, "profiles", regex.pathEscape(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
973 )
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
974 # FIXME: would be better to have a global /cache URL which redirect to
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
975 # profile's cache directory, without uuid
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
976 self.cache_resource.putChild(sat_session.uuid.encode('utf-8'),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
977 ProtectedFile(cache_dir))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
978 log.debug(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
979 _("profile cache resource added from {uuid} to {path}").format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
980 uuid=sat_session.uuid, path=cache_dir
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
981 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
982 )
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
983
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
984 def onExpire():
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
985 log.info("Session expired (profile={profile})".format(profile=profile))
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
986 self.cache_resource.delEntity(sat_session.uuid.encode('utf-8'))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
987 log.debug(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
988 _("profile cache resource {uuid} deleted").format(uuid=sat_session.uuid)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
989 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
990 # and now we disconnect the profile
1098
01e95ec9df9e server, pages: fixed blocking calls to bridge by using bridgeCall instead
Goffi <goffi@goffi.org>
parents: 1093
diff changeset
991 self.bridgeCall("disconnect", profile)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
992
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
993 session.notifyOnExpire(onExpire)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
994
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
995 # FIXME: those session infos should be returned by connect or isConnected
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
996 infos = yield self.bridgeCall("sessionInfosGet", profile)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
997 sat_session.jid = jid.JID(infos["jid"])
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
998 sat_session.backend_started = int(infos["started"])
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
999
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1000 state = C.PROFILE_LOGGED_EXT_JID if register_with_ext_jid else C.PROFILE_LOGGED
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1001 defer.returnValue(state)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1002
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1003 @defer.inlineCallbacks
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1004 def connect(self, request, login, password):
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1005 """log user in
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1006
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1007 If an other user was already logged, it will be unlogged first
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1008 @param request(server.Request): request linked to the session
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1009 @param login(unicode): user login
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1010 can be profile name
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1011 can be profile@[libervia_domain.ext]
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1012 can be a jid (a new profile will be created with this jid if needed)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1013 @param password(unicode): user password
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1014 @return (unicode, None): C.SESSION_ACTIVE: if session was aleady active else
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1015 self._logged value
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1016 @raise exceptions.DataError: invalid login
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1017 @raise exceptions.ProfileUnknownError: this login doesn't exist
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1018 @raise exceptions.PermissionError: a login is not accepted (e.g. empty password
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1019 not allowed)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1020 @raise exceptions.NotReady: a profile connection is already waiting
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1021 @raise exceptions.TimeoutError: didn't received and answer from Bridge
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1022 @raise exceptions.InternalError: unknown error
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1023 @raise ValueError(C.PROFILE_AUTH_ERROR): invalid login and/or password
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1024 @raise ValueError(C.XMPP_AUTH_ERROR): invalid XMPP account password
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1025 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1026
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1027 # XXX: all security checks must be done here, even if present in javascript
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1028 if login.startswith("@"):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1029 raise failure.Failure(exceptions.DataError("No profile_key allowed"))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1030
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1031 if login.startswith("guest@@") and login.count("@") == 2:
1100
5976dcd42591 pages (g): use server's connect method to connect instead of bridge, this way normal Libervia workflow is used, and session attributes are initialised.
Goffi <goffi@goffi.org>
parents: 1099
diff changeset
1032 log.debug("logging a guest account")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1033 elif "@" in login:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1034 if login.count("@") != 1:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1035 raise failure.Failure(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1036 exceptions.DataError("Invalid login: {login}".format(login=login))
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1037 )
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1038 try:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1039 login_jid = jid.JID(login)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1040 except (RuntimeError, jid.InvalidFormat, AttributeError):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1041 raise failure.Failure(exceptions.DataError("No profile_key allowed"))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1042
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1043 # FIXME: should it be cached?
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1044 new_account_domain = yield self.bridgeCall("getNewAccountDomain")
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1045
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1046 if login_jid.host == new_account_domain:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1047 # redirect "user@libervia.org" to the "user" profile
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1048 login = login_jid.user
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1049 login_jid = None
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1050 else:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1051 login_jid = None
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1052
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1053 try:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1054 profile = yield self.bridgeCall("profileNameGet", login)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1055 except Exception: # XXX: ProfileUnknownError wouldn't work, it's encapsulated
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1056 # FIXME: find a better way to handle bridge errors
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1057 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1058 login_jid is not None and login_jid.user
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1059 ): # try to create a new sat profile using the XMPP credentials
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1060 if not self.options["allow_registration"]:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1061 log.warning(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1062 "Trying to register JID account while registration is not "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1063 "allowed")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1064 raise failure.Failure(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1065 exceptions.DataError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1066 "JID login while registration is not allowed"
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1067 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1068 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1069 profile = login # FIXME: what if there is a resource?
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1070 connect_method = "asyncConnectWithXMPPCredentials"
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1071 register_with_ext_jid = True
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1072 else: # non existing username
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1073 raise failure.Failure(exceptions.ProfileUnknownError())
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1074 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1075 if profile != login or (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1076 not password
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1077 and profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1078 not in self.options["empty_password_allowed_warning_dangerous_list"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1079 ):
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1080 # profiles with empty passwords are restricted to local frontends
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1081 raise exceptions.PermissionError
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1082 register_with_ext_jid = False
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1083
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1084 connect_method = "connect"
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1085
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1086 # we check if there is not already an active session
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1087 sat_session = session_iface.ISATSession(request.getSession())
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1088 if sat_session.profile:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1089 # yes, there is
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1090 if sat_session.profile != profile:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1091 # it's a different profile, we need to disconnect it
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1092 log.warning(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1093 "{new_profile} requested login, but {old_profile} was already "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1094 "connected, disconnecting {old_profile}").format(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1095 old_profile=sat_session.profile, new_profile=profile))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1096 self.purgeSession(request)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1097
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1098 if self.waiting_profiles.getRequest(profile):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1099 #  FIXME: check if and when this can happen
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1100 raise failure.Failure(exceptions.NotReady("Already waiting"))
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1101
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1102 self.waiting_profiles.setRequest(request, profile, register_with_ext_jid)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1103 try:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1104 connected = yield self.bridgeCall(connect_method, profile, password)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1105 except Exception as failure_:
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
1106 fault = getattr(failure_, 'classname', None)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1107 self.waiting_profiles.purgeRequest(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1108 if fault in ("PasswordError", "ProfileUnknownError"):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1109 log.info("Profile {profile} doesn't exist or the submitted password is "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1110 "wrong".format( profile=profile))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1111 raise failure.Failure(ValueError(C.PROFILE_AUTH_ERROR))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1112 elif fault == "SASLAuthError":
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1113 log.info("The XMPP password of profile {profile} is wrong"
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1114 .format(profile=profile))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1115 raise failure.Failure(ValueError(C.XMPP_AUTH_ERROR))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1116 elif fault == "NoReply":
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1117 log.info(_("Did not receive a reply (the timeout expired or the "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1118 "connection is broken)"))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1119 raise exceptions.TimeOutError
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
1120 elif fault is None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1121 log.info(_("Unexepected failure: {failure_}").format(failure_=failure))
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
1122 raise failure_
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1123 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1124 log.error('Unmanaged fault class "{fault}" in errback for the '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1125 'connection of profile {profile}'.format(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1126 fault=fault, profile=profile))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1127 raise failure.Failure(exceptions.InternalError(fault))
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1128
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1129 if connected:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1130 #  profile is already connected in backend
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1131 # do we have a corresponding session in Libervia?
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1132 sat_session = session_iface.ISATSession(request.getSession())
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1133 if sat_session.profile:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1134 # yes, session is active
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1135 if sat_session.profile != profile:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1136 # existing session should have been ended above
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1137 # so this line should never be reached
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1138 log.error(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1139 "session profile [{session_profile}] differs from login "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1140 "profile [{profile}], this should not happen!")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1141 .format(session_profile=sat_session.profile, profile=profile))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1142 raise exceptions.InternalError("profile mismatch")
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1143 defer.returnValue(C.SESSION_ACTIVE)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1144 log.info(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1145 _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1146 "profile {profile} was already connected in backend".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1147 profile=profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1148 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1149 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1150 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1151 #  no, we have to create it
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1152
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1153 state = yield self._logged(profile, request)
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1154 defer.returnValue(state)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1155
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1156 def registerNewAccount(self, request, login, password, email):
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1157 """Create a new account, or return error
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1158 @param request(server.Request): request linked to the session
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1159 @param login(unicode): new account requested login
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1160 @param email(unicode): new account email
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1161 @param password(unicode): new account password
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1162 @return(unicode): a constant indicating the state:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1163 - C.BAD_REQUEST: something is wrong in the request (bad arguments)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1164 - C.INVALID_INPUT: one of the data is not valid
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1165 - C.REGISTRATION_SUCCEED: new account has been successfully registered
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1166 - C.ALREADY_EXISTS: the given profile already exists
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1167 - C.INTERNAL_ERROR or any unmanaged fault string
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1168 @raise PermissionError: registration is now allowed in server configuration
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1169 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1170 if not self.options["allow_registration"]:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1171 log.warning(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1172 _("Registration received while it is not allowed, hack attempt?")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1173 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1174 raise failure.Failure(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1175 exceptions.PermissionError("Registration is not allowed on this server")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1176 )
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1177
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1178 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1179 not re.match(C.REG_LOGIN_RE, login)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1180 or not re.match(C.REG_EMAIL_RE, email, re.IGNORECASE)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1181 or len(password) < C.PASSWORD_MIN_LENGTH
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1182 ):
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1183 return C.INVALID_INPUT
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1184
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1185 def registered(result):
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1186 return C.REGISTRATION_SUCCEED
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1187
1184
eee7a896cf0e server: show BridgeException traceback on unknown registering status
Goffi <goffi@goffi.org>
parents: 1182
diff changeset
1188 def registeringError(failure_):
1164
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1189 # FIXME: better error handling for bridge error is needed
1184
eee7a896cf0e server: show BridgeException traceback on unknown registering status
Goffi <goffi@goffi.org>
parents: 1182
diff changeset
1190 status = failure_.value.fullname.split('.')[-1]
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1191 if status == "ConflictError":
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1192 return C.ALREADY_EXISTS
1164
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1193 elif status == "InvalidCertificate":
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1194 return C.INVALID_CERTIFICATE
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1195 elif status == "InternalError":
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1196 return C.INTERNAL_ERROR
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1197 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1198 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1199 _("Unknown registering error status: {status}\n{traceback}").format(
1184
eee7a896cf0e server: show BridgeException traceback on unknown registering status
Goffi <goffi@goffi.org>
parents: 1182
diff changeset
1200 status=status, traceback=failure_.value.message
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1201 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1202 )
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1203 return status
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1204
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1205 d = self.bridgeCall("registerSatAccount", email, password, login)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1206 d.addCallback(registered)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1207 d.addErrback(registeringError)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1208 return d
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1209
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1210 def addCleanup(self, callback, *args, **kwargs):
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1211 """Add cleaning method to call when service is stopped
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1212
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1213 cleaning method will be called in reverse order of they insertion
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1214 @param callback: callable to call on service stop
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1215 @param *args: list of arguments of the callback
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1216 @param **kwargs: list of keyword arguments of the callback"""
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1217 self._cleanup.insert(0, (callback, args, kwargs))
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1218
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1219 def startService(self):
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1220 """Connect the profile for Libervia and start the HTTP(S) server(s)"""
1236
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
1221 self._init()
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1222
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1223 def eb(e):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1224 log.error(_("Connection failed: %s") % e)
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1225 self.stop()
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1226
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1227 def initOk(__):
859
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1228 try:
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1229 connected = self.bridge.isConnected(C.SERVICE_PROFILE)
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1230 except Exception as e:
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1231 # we don't want the traceback
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1232 msg = [l for l in str(e).split("\n") if l][-1]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1233 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1234 "Can't check service profile ({profile}), are you sure it exists ?"
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1235 "\n{error}".format(profile=C.SERVICE_PROFILE, error=msg))
859
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1236 self.stop()
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1237 return
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1238 if not connected:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1239 self.bridge.connect(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1240 C.SERVICE_PROFILE,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1241 self.options["passphrase"],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1242 {},
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1243 callback=self._startService,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1244 errback=eb,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1245 )
472
42c8e91af048 server side: fixed start sequence when SERVICE_PROFILE is already connected
Goffi <goffi@goffi.org>
parents: 471
diff changeset
1246 else:
42c8e91af048 server side: fixed start sequence when SERVICE_PROFILE is already connected
Goffi <goffi@goffi.org>
parents: 471
diff changeset
1247 self._startService()
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1248
444
b64e528fb524 server side: use of bridge's getReady to be sure that backend is initialised at launch
Goffi <goffi@goffi.org>
parents: 438
diff changeset
1249 self.initialised.addCallback(initOk)
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1250
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1251 ## URLs ##
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents: 917
diff changeset
1252
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1253 def putChildSAT(self, path, resource):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1254 """Add a child to the sat resource"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1255 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1256 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1257 self.sat_root.putChild(path, resource)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1258
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1259 def putChildAll(self, path, resource):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1260 """Add a child to all vhost root resources"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1261 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1262 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1263 # we wrap before calling putChild, to avoid having useless multiple instances
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1264 # of the resource
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1265 # FIXME: check that no information is leaked (c.f. https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#request-encoders)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1266 wrapped_res = web_resource.EncodingResourceWrapper(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1267 resource, [server.GzipEncoderFactory()])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1268 for root in self.roots:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1269 root.putChild(path, wrapped_res)
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1270
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1271 def getBuildPath(self, site_name):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1272 """Generate build path for a given site name
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1273
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1274 @param site_name(unicode): name of the site
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1275 @return (unicode): path to the build directory
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1276 """
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1277 build_path_elts = [
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1278 config.getConfig(self.main_conf, "", "local_dir"),
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1279 C.CACHE_DIR,
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1280 C.LIBERVIA_CACHE,
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1281 regex.pathEscape(site_name)]
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1282 build_path = "/".join(build_path_elts)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1283 return os.path.abspath(os.path.expanduser(build_path))
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1284
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1285 def getExtBaseURLData(self, request):
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1286 """Retrieve external base URL Data
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1287
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1288 this method tried to retrieve the base URL found by external user
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1289 It does by checking in this order:
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1290 - base_url_ext option from configuration
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1291 - proxy x-forwarder-host headers
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1292 - URL of the request
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1293 @return (urlparse.SplitResult): SplitResult instance with only scheme and
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1294 netloc filled
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1295 """
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1296 ext_data = self.base_url_ext_data
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1297 url_path = request.URLPath()
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1298 if not ext_data.scheme or not ext_data.netloc:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1299 #  ext_data is not specified, we check headers
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1300 if request.requestHeaders.hasHeader("x-forwarded-host"):
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1301 # we are behing a proxy
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1302 # we fill proxy_scheme and proxy_netloc value
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1303 proxy_host = request.requestHeaders.getRawHeaders("x-forwarded-host")[0]
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1304 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1305 proxy_server = request.requestHeaders.getRawHeaders(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1306 "x-forwarded-server"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1307 )[0]
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1308 except TypeError:
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1309 # no x-forwarded-server found, we use proxy_host
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1310 proxy_netloc = proxy_host
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1311 else:
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1312 # if the proxy host has a port, we use it with server name
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1313 proxy_port = urllib.parse.urlsplit("//{}".format(proxy_host)).port
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1314 proxy_netloc = (
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1315 "{}:{}".format(proxy_server, proxy_port)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1316 if proxy_port is not None
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1317 else proxy_server
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1318 )
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1319 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1320 proxy_scheme = request.requestHeaders.getRawHeaders(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1321 "x-forwarded-proto"
1224
62bf4f87c249 server: some encoding fixes following python 3 port
Goffi <goffi@goffi.org>
parents: 1218
diff changeset
1322 )[0]
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1323 except TypeError:
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1324 proxy_scheme = None
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1325 else:
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1326 proxy_scheme, proxy_netloc = None, None
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1327 else:
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1328 proxy_scheme, proxy_netloc = None, None
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1329
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1330 return urllib.parse.SplitResult(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1331 ext_data.scheme or proxy_scheme or url_path.scheme.decode("utf-8"),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1332 ext_data.netloc or proxy_netloc or url_path.netloc.decode("utf-8"),
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1333 ext_data.path or "/",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1334 "",
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1335 "",
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1336 )
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1337
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1338 def getExtBaseURL(self, request, path="", query="", fragment="", scheme=None):
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1339 """Get external URL according to given elements
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1340
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1341 external URL is the URL seen by external user
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1342 @param path(unicode): same as for urlsplit.urlsplit
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1343 path will be prefixed to follow found external URL if suitable
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1344 @param params(unicode): same as for urlsplit.urlsplit
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1345 @param query(unicode): same as for urlsplit.urlsplit
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1346 @param fragment(unicode): same as for urlsplit.urlsplit
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1347 @param scheme(unicode, None): if not None, will override scheme from base URL
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1348 @return (unicode): external URL
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1349 """
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1350 split_result = self.getExtBaseURLData(request)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1351 return urllib.parse.urlunsplit(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1352 (
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1353 split_result.scheme if scheme is None else scheme,
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1354 split_result.netloc,
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1355 os.path.join(split_result.path, path),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1356 query,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1357 fragment,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1358 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1359 )
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1360
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1361 def checkRedirection(self, vhost_root, url):
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1362 """check is a part of the URL prefix is redirected then replace it
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1363
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1364 @param vhost_root(web_resource.Resource): root of this virtual host
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1365 @param url(unicode): url to check
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1366 @return (unicode): possibly redirected URL which should link to the same location
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1367 """
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1368 inv_redirections = vhost_root.inv_redirections
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1369 url_parts = url.strip("/").split("/")
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1370 for idx in range(len(url), 0, -1):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1371 test_url = "/" + "/".join(url_parts[:idx])
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1372 if test_url in inv_redirections:
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1373 rem_url = url_parts[idx:]
1099
ea0d41ef3719 server: return absolute page in checkRedirection
Goffi <goffi@goffi.org>
parents: 1098
diff changeset
1374 return os.path.join(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1375 "/", "/".join([inv_redirections[test_url]] + rem_url)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1376 )
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1377 return url
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1378
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1379 ## Sessions ##
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1380
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1381 def purgeSession(self, request):
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1382 """helper method to purge a session during request handling"""
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1383 session = request.session
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1384 if session is not None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1385 log.debug(_("session purge"))
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1386 session.expire()
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1387 # FIXME: not clean but it seems that it's the best way to reset
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1388 # session during request handling
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1389 request._secureSession = request._insecureSession = None
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1390
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1391 def getSessionData(self, request, *args):
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1392 """helper method to retrieve session data
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1393
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1394 @param request(server.Request): request linked to the session
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1395 @param *args(zope.interface.Interface): interface of the session to get
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1396 @return (iterator(data)): requested session data
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1397 """
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1398 session = request.getSession()
924
94f88277c2e7 libervia (pages): getSessionData return one session if used with one argument, else an iterator
Goffi <goffi@goffi.org>
parents: 923
diff changeset
1399 if len(args) == 1:
94f88277c2e7 libervia (pages): getSessionData return one session if used with one argument, else an iterator
Goffi <goffi@goffi.org>
parents: 923
diff changeset
1400 return args[0](session)
94f88277c2e7 libervia (pages): getSessionData return one session if used with one argument, else an iterator
Goffi <goffi@goffi.org>
parents: 923
diff changeset
1401 else:
94f88277c2e7 libervia (pages): getSessionData return one session if used with one argument, else an iterator
Goffi <goffi@goffi.org>
parents: 923
diff changeset
1402 return (iface(session) for iface in args)
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1403
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1404 @defer.inlineCallbacks
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1405 def getAffiliation(self, request, service, node):
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1406 """retrieve pubsub node affiliation for current user
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1407
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1408 use cache first, and request pubsub service if not cache is found
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1409 @param request(server.Request): request linked to the session
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1410 @param service(jid.JID): pubsub service
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1411 @param node(unicode): pubsub node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1412 @return (unicode): affiliation
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1413 """
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1414 sat_session = self.getSessionData(request, session_iface.ISATSession)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1415 if sat_session.profile is None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1416 raise exceptions.InternalError("profile must be set to use this method")
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1417 affiliation = sat_session.getAffiliation(service, node)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1418 if affiliation is not None:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1419 defer.returnValue(affiliation)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1420 else:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1421 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1422 affiliations = yield self.bridgeCall(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1423 "psAffiliationsGet", service.full(), node, sat_session.profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1424 )
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1425 except Exception as e:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1426 log.warning(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1427 "Can't retrieve affiliation for {service}/{node}: {reason}".format(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1428 service=service, node=node, reason=e
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1429 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1430 )
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1431 affiliation = ""
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1432 else:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1433 try:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1434 affiliation = affiliations[node]
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1435 except KeyError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1436 affiliation = ""
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1437 sat_session.setAffiliation(service, node, affiliation)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1438 defer.returnValue(affiliation)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1439
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1440 ## Websocket (dynamic pages) ##
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1441
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1442 def getWebsocketURL(self, request):
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1443 base_url_split = self.getExtBaseURLData(request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1444 if base_url_split.scheme.endswith("s"):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1445 scheme = "wss"
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1446 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1447 scheme = "ws"
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1448
1006
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1449 return self.getExtBaseURL(request, path=scheme, scheme=scheme)
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1450
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1451 def registerWSToken(self, token, page, request):
1203
251eba911d4d server (websockets): fixed websocket handling on HTTPS connections:
Goffi <goffi@goffi.org>
parents: 1202
diff changeset
1452 # we make a shallow copy of request to avoid losing request.channel when
251eba911d4d server (websockets): fixed websocket handling on HTTPS connections:
Goffi <goffi@goffi.org>
parents: 1202
diff changeset
1453 # connection is lost (which would result as request.isSecure() being always
251eba911d4d server (websockets): fixed websocket handling on HTTPS connections:
Goffi <goffi@goffi.org>
parents: 1202
diff changeset
1454 # False). See #327
251eba911d4d server (websockets): fixed websocket handling on HTTPS connections:
Goffi <goffi@goffi.org>
parents: 1202
diff changeset
1455 request._signal_id = id(request)
251eba911d4d server (websockets): fixed websocket handling on HTTPS connections:
Goffi <goffi@goffi.org>
parents: 1202
diff changeset
1456 websockets.LiberviaPageWSProtocol.registerToken(token, page, copy.copy(request))
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1457
1019
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1458 ## Various utils ##
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1459
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1460 def getHTTPDate(self, timestamp=None):
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1461 now = time.gmtime(timestamp)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1462 fmt_date = "{day_name}, %d {month_name} %Y %H:%M:%S GMT".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1463 day_name=C.HTTP_DAYS[now.tm_wday], month_name=C.HTTP_MONTH[now.tm_mon - 1]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1464 )
1019
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1465 return time.strftime(fmt_date, now)
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1466
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1467 ## TLS related methods ##
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1468
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1469 def _TLSOptionsCheck(self):
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1470 """Check options coherence if TLS is activated, and update missing values
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1471
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1472 Must be called only if TLS is activated
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1473 """
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1474 if not self.options["tls_certificate"]:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1475 log.error("a TLS certificate is needed to activate HTTPS connection")
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1476 self.quit(1)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1477 if not self.options["tls_private_key"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1478 self.options["tls_private_key"] = self.options["tls_certificate"]
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1479
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1480 if not self.options["tls_private_key"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1481 self.options["tls_private_key"] = self.options["tls_certificate"]
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1482
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1483 def _loadCertificates(self, f):
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1484 """Read a .pem file with a list of certificates
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1485
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1486 @param f (file): file obj (opened .pem file)
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1487 @return (list[OpenSSL.crypto.X509]): list of certificates
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1488 @raise OpenSSL.crypto.Error: error while parsing the file
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1489 """
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1490 # XXX: didn't found any method to load a .pem file with several certificates
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1491 # so the certificates split is done here
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1492 certificates = []
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1493 buf = []
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1494 while True:
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1495 line = f.readline()
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1496 buf.append(line)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1497 if "-----END CERTIFICATE-----" in line:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1498 certificates.append(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1499 OpenSSL.crypto.load_certificate(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1500 OpenSSL.crypto.FILETYPE_PEM, "".join(buf)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1501 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1502 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1503 buf = []
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1504 elif not line:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1505 log.debug("{} certificate(s) found".format(len(certificates)))
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1506 return certificates
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1507
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1508 def _loadPKey(self, f):
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1509 """Read a private key from a .pem file
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1510
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1511 @param f (file): file obj (opened .pem file)
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1512 @return (list[OpenSSL.crypto.PKey]): private key object
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1513 @raise OpenSSL.crypto.Error: error while parsing the file
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1514 """
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1515 return OpenSSL.crypto.load_privatekey(OpenSSL.crypto.FILETYPE_PEM, f.read())
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1516
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1517 def _loadCertificate(self, f):
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1518 """Read a public certificate from a .pem file
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1519
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1520 @param f (file): file obj (opened .pem file)
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1521 @return (list[OpenSSL.crypto.X509]): public certificate
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1522 @raise OpenSSL.crypto.Error: error while parsing the file
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1523 """
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1524 return OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, f.read())
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1525
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1526 def _getTLSContextFactory(self):
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1527 """Load TLS certificate and build the context factory needed for listenSSL"""
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1528 if ssl is None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1529 raise ImportError("Python module pyOpenSSL is not installed!")
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1530
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1531 cert_options = {}
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1532
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1533 for name, option, method in [
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1534 ("privateKey", "tls_private_key", self._loadPKey),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1535 ("certificate", "tls_certificate", self._loadCertificate),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1536 ("extraCertChain", "tls_chain", self._loadCertificates),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1537 ]:
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1538 path = self.options[option]
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1539 if not path:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1540 assert option == "tls_chain"
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1541 continue
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1542 log.debug("loading {option} from {path}".format(option=option, path=path))
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1543 try:
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1544 with open(path) as f:
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1545 cert_options[name] = method(f)
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1546 except IOError as e:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1547 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1548 "Error while reading file {path} for option {option}: {error}".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1549 path=path, option=option, error=e
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1550 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1551 )
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1552 self.quit(2)
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1553 except OpenSSL.crypto.Error:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1554 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1555 "Error while parsing file {path} for option {option}, are you sure "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1556 "it is a valid .pem file?".format( path=path, option=option))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1557 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1558 option == "tls_private_key"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1559 and self.options["tls_certificate"] == path
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1560 ):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1561 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1562 "You are using the same file for private key and public "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1563 "certificate, make sure that both a in {path} or use "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1564 "--tls_private_key option".format(path=path))
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1565 self.quit(2)
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1566
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1567 return ssl.CertificateOptions(**cert_options)
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1568
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1569 ## service management ##
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1570
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1571 def _startService(self, __=None):
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
1572 """Actually start the HTTP(S) server(s) after the profile for Libervia is connected.
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1573
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1574 @raise ImportError: OpenSSL is not available
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
1575 @raise IOError: the certificate file doesn't exist
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
1576 @raise OpenSSL.crypto.Error: the certificate file is invalid
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
1577 """
950
67a59552f3e3 server (blog): fixed avatars handling, there is now a well-known URL to SERVICE_PROFILE cache
Goffi <goffi@goffi.org>
parents: 937
diff changeset
1578 # now that we have service profile connected, we add resource for its cache
67a59552f3e3 server (blog): fixed avatars handling, there is now a well-known URL to SERVICE_PROFILE cache
Goffi <goffi@goffi.org>
parents: 937
diff changeset
1579 service_path = regex.pathEscape(C.SERVICE_PROFILE)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1580 cache_dir = os.path.join(self.cache_root_dir, "profiles", service_path)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1581 self.cache_resource.putChild(service_path.encode('utf-8'),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1582 ProtectedFile(cache_dir))
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1583 self.service_cache_url = "/" + os.path.join(C.CACHE_DIR, service_path)
1055
c2037b44f84e server: fixed cache_dir for service profile in session interface
Goffi <goffi@goffi.org>
parents: 1054
diff changeset
1584 session_iface.SATSession.service_cache_url = self.service_cache_url
950
67a59552f3e3 server (blog): fixed avatars handling, there is now a well-known URL to SERVICE_PROFILE cache
Goffi <goffi@goffi.org>
parents: 937
diff changeset
1585
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1586 if self.options["connection_type"] in ("https", "both"):
813
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1587 self._TLSOptionsCheck()
6e27604ec95a server: added --tls_private_key and --tls_chain options. --tls_certificate .pem file will be used for private_key if --tls_private_key is not specified.
Goffi <goffi@goffi.org>
parents: 812
diff changeset
1588 context_factory = self._getTLSContextFactory()
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1589 reactor.listenSSL(self.options["port_https"], self.site, context_factory)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1590 if self.options["connection_type"] in ("http", "both"):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1591 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1592 self.options["connection_type"] == "both"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1593 and self.options["redirect_to_https"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1594 ):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1595 reactor.listenTCP(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1596 self.options["port"],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1597 server.Site(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1598 RedirectToHTTPS(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1599 self.options["port"], self.options["port_https_ext"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1600 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1601 ),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1602 )
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1603 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1604 reactor.listenTCP(self.options["port"], self.site)
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1605
1108
7ec1ba86d38d server: replaced blocking calls to bridge by bridgeCall which is not blocking
Goffi <goffi@goffi.org>
parents: 1106
diff changeset
1606 @defer.inlineCallbacks
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1607 def stopService(self):
473
d1565906f228 server_side: fixed some forgotten print statement
Goffi <goffi@goffi.org>
parents: 472
diff changeset
1608 log.info(_("launching cleaning methods"))
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1609 for callback, args, kwargs in self._cleanup:
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1610 callback(*args, **kwargs)
859
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1611 try:
1108
7ec1ba86d38d server: replaced blocking calls to bridge by bridgeCall which is not blocking
Goffi <goffi@goffi.org>
parents: 1106
diff changeset
1612 yield self.bridgeCall("disconnect", C.SERVICE_PROFILE)
859
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1613 except Exception:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1614 log.warning("Can't disconnect service profile")
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1615
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1616 def run(self):
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1617 reactor.run()
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1618
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1619 def stop(self):
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1620 reactor.stop()
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1621
812
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1622 def quit(self, exit_code=None):
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1623 """Exit app when reactor is running
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1624
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1625 @param exit_code(None, int): exit code
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1626 """
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1627 self.stop()
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1628 sys.exit(exit_code or 0)
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1629
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1630
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1631 class RedirectToHTTPS(web_resource.Resource):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1632 def __init__(self, old_port, new_port):
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1633 web_resource.Resource.__init__(self)
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1634 self.isLeaf = True
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1635 self.old_port = old_port
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1636 self.new_port = new_port
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1637
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1638 def render(self, request):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1639 netloc = request.URLPath().netloc.replace(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1640 ":%s" % self.old_port, ":%s" % self.new_port
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1641 )
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1642 url = "https://" + netloc + request.uri
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1643 return web_util.redirectTo(url, request)
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1644
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1645
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents: 917
diff changeset
1646 registerAdapter(session_iface.SATSession, server.Session, session_iface.ISATSession)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1647 registerAdapter(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1648 session_iface.SATGuestSession, server.Session, session_iface.ISATGuestSession
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1649 )