annotate libervia/web/server/server.py @ 1618:5d9889f14012 default tip @

server: start major redesign - Add icons to menu items - Switch menu items representation from tuple to dictionary for future extensibility: - Include icon information - Prepare for additional data - Remove "login" from main menu, add login page URL to template data, as it is now a separate right-aligned item
author Goffi <goffi@goffi.org>
date Sat, 26 Oct 2024 23:07:01 +0200
parents ebd538cb26cb
children
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
1397
ed037818d6de core (constants): renaming following global project renaming
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
3 # Libervia Web
1396
822bd0139769 date update
Goffi <goffi@goffi.org>
parents: 1383
diff changeset
4 # Copyright (C) 2011-2021 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
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
19 from functools import partial
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
20 import os.path
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
21 from pathlib import Path
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
22 import re
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
23 import sys
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
24 import time
1512
65e063657597 server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
25 from typing import Callable, Dict, Optional
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
26 import urllib.error
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
27 import urllib.parse
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
28 import urllib.request
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
29
1594
93abef9a3548 server: catch `DBusException` and retry to connect in this case.
Goffi <goffi@goffi.org>
parents: 1592
diff changeset
30 from dbus.exceptions import DBusException
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
31 from twisted.application import service
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
32 from twisted.internet import defer, inotify, reactor
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
33 from twisted.python import failure
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
34 from twisted.python import filepath
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
35 from twisted.python.components import registerAdapter
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
36 from twisted.web import server
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
37 from twisted.web import static
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
38 from twisted.web import resource as web_resource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
39 from twisted.web import util as web_util
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
40 from twisted.web import vhost
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
41 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
42
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
43 import libervia.web
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
44 from libervia.web.server import websockets
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
45 from libervia.web.server import session_iface
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
46 from libervia.web.server.constants import Const as C
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
47 from libervia.web.server.pages import LiberviaPage
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
48 from libervia.web.server.tasks.manager import TasksManager
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
49 from libervia.web.server.utils import ProgressHandler
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
50 from libervia.backend.core import exceptions
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
51 from libervia.backend.core.i18n import _
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
52 from libervia.backend.core.log import getLogger
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
53 from libervia.backend.tools import utils
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
54 from libervia.backend.tools import config
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
55 from libervia.backend.tools.common import regex
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
56 from libervia.backend.tools.common import template
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
57 from libervia.backend.tools.common import data_format
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
58 from libervia.backend.tools.common import tls
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
59 from libervia.frontends.bridge.bridge_frontend import BridgeException
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
60 from libervia.frontends.bridge.dbus_bridge import BridgeExceptionNoService, bridge
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
61 from libervia.frontends.bridge.dbus_bridge import const_TIMEOUT as BRIDGE_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
62
1512
65e063657597 server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
63 from .resources import LiberviaRootResource, ProtectedFile
1287
1f26d8c2afc1 server: restricted_bridge first draft:
Goffi <goffi@goffi.org>
parents: 1282
diff changeset
64 from .restricted_bridge import RestrictedBridge
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
65
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
66 log = getLogger(__name__)
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
67
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
68
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
69 DEFAULT_MASK = (inotify.IN_CREATE | inotify.IN_MODIFY | inotify.IN_MOVE_SELF
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
70 | inotify.IN_MOVED_TO)
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
71
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
72
1361
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
73 class SysExit(Exception):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
74
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
75 def __init__(self, exit_code, message=""):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
76 self.exit_code = exit_code
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
77 self.message = message
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
78
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
79 def __str__(self):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
80 return f"System Exit({self.exit_code}): {self.message}"
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
81
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
82
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
83 class FilesWatcher(object):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
84 """Class to check files modifications using iNotify"""
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
85 _notifier = None
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
86
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
87 def __init__(self, host):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
88 self.host = host
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
89
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
90 @property
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
91 def notifier(self):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
92 if self._notifier == None:
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
93 notifier = self.__class__._notifier = inotify.INotify()
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
94 notifier.startReading()
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
95 return self._notifier
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
96
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
97 def _check_callback(self, dir_path, callback, recursive):
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
98 # Twisted doesn't add callback if a watcher was already set on a path
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
99 # but in dev mode Libervia watches whole sites + internal path can be watched
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
100 # by tasks, so several callbacks must be called on some paths.
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
101 # This method check that the new callback is indeed present in the desired path
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
102 # and add it otherwise.
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
103 # FIXME: this should probably be fixed upstream
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
104 if recursive:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
105 for child in dir_path.walk():
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
106 if child.isdir():
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
107 self._check_callback(child, callback, recursive=False)
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
108 else:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
109 watch_id = self.notifier._isWatched(dir_path)
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
110 if watch_id is None:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
111 log.warning(
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
112 f"There is no watch ID for path {dir_path}, this should not happen"
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
113 )
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
114 else:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
115 watch_point = self.notifier._watchpoints[watch_id]
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
116 if callback not in watch_point.callbacks:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
117 watch_point.callbacks.append(callback)
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
118
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
119 def watch_dir(self, dir_path, callback, mask=DEFAULT_MASK, auto_add=False,
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
120 recursive=False, **kwargs):
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
121 dir_path = str(dir_path)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
122 log.info(_("Watching directory {dir_path}").format(dir_path=dir_path))
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
123 wrapped_callback = lambda __, filepath, mask: callback(
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
124 self.host, filepath, inotify.humanReadableMask(mask), **kwargs)
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
125 callbacks = [wrapped_callback]
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
126 dir_path = filepath.FilePath(dir_path)
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
127 self.notifier.watch(
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
128 dir_path, mask=mask, autoAdd=auto_add, recursive=recursive,
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
129 callbacks=callbacks)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
130 self._check_callback(dir_path, wrapped_callback, recursive)
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
131
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
132
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
133 class WebSession(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
134 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
135
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
136 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
137 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
138 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
139
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
140 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
141 """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
142 self.__lock = True
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
143 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
144
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
145 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
146 """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
147 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
148 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
149
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
150 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
151 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
152 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
153
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
154
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
155 class WaitingRequests(dict):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
156 def set_request(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
157 """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
158
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
159 @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
160 @param profile (str): %(doc_profile)s
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
161 @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
162 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
163 """
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
164 dc = reactor.callLater(BRIDGE_TIMEOUT, self.purge_request, 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
165 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
166
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
167 def purge_request(self, profile):
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
168 """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
169
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
170 @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
171 """
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
172 try:
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
173 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
174 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
175 return
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
176 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
177 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
178 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
179
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
180 def get_request(self, profile):
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
181 """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
182
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
183 @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
184 @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
185 """
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
186 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
187
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
188 def get_register_with_ext_jid(self, 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
189 """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
190
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
191 @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
192 @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
193 """
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
194 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
195
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
196
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
197 class LiberviaWeb(service.Service):
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
198 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
199
810
3905bc24eb17 server: proper options handling
Goffi <goffi@goffi.org>
parents: 801
diff changeset
200 def __init__(self, options):
3905bc24eb17 server: proper options handling
Goffi <goffi@goffi.org>
parents: 801
diff changeset
201 self.options = options
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
202 websockets.host = self
1236
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
203
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
204 def _init(self):
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
205 # 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
206 # 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
207 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
208 self.waiting_profiles = WaitingRequests() # FIXME: should be removed
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
209 self._main_conf = None
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
210 self.files_watcher = FilesWatcher(self)
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
211
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
212 if self.options["base_url_ext"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
213 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
214 if self.base_url_ext[-1] != "/":
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
215 self.base_url_ext += "/"
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
216 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
217 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
218 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
219 # 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
220 # scheme = self.base_url_ext_data.scheme or 'https'
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
221 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
222
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
223 if not self.options["port_https_ext"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
224 self.options["port_https_ext"] = self.options["port_https"]
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
225
1612
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
226 self.local_shared_path: Path|None = None
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
227
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
228 self._cleanup = []
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
229
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
230 self.sessions = {} # key = session value = user
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
231 self.prof_connected = set() # Profiles connected
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
232 self.ns_map = {} # map of short name to namespaces
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
233
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
234 ## bridge ##
1367
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
235 self._bridge_retry = self.options['bridge-retries']
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
236 self.bridge = bridge()
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
237 self.bridge.bridge_connect(callback=self._bridge_cb, errback=self._bridge_eb)
553
8492c2bb463b server_side: enable HTTP gzip compression
souliane <souliane@mailoo.org>
parents: 522
diff changeset
238
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
239 ## libervia app callbacks ##
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
240 # mapping instance id to the callback to call on "started" signal
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
241 self.apps_cb: Dict[str, Callable] = {}
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
242
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
243 @property
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
244 def roots(self):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
245 """Return available virtual host roots
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
246
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
247 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
248 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
249 returned first.
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
250 @return (list[web_resource.Resource]): all vhost root resources
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
251 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
252 roots = list(set(self.vhost_root.hosts.values()))
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
253 default = self.vhost_root.default
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
254 if default is not None and default not in roots:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
255 roots.insert(0, default)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
256 return roots
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
257
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
258 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
259 def main_conf(self):
1601
d07838fc9d99 minor updates
Goffi <goffi@goffi.org>
parents: 1598
diff changeset
260 """ConfigParser instance opened on configuration file (libervia.conf)"""
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
261 if self._main_conf is None:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
262 self._main_conf = config.parse_main_conf(log_filenames=True)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
263 return self._main_conf
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
264
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
265 def config_get(self, site_root_res, key, default=None, value_type=None):
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
266 """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
267
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
268 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
269 @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
270 @param key(unicode): key to use
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
271 @param default: value to use if not found (see [config.config_get])
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
272 @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
273 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
274 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
275 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
276 - "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
277
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
278 """
1275
334d044f2713 server: default theme can now be specified in site section of `sat.conf` with `theme` key
Goffi <goffi@goffi.org>
parents: 1274
diff changeset
279 section = site_root_res.site_name.lower().strip() or C.CONFIG_SECTION
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
280 value = config.config_get(self.main_conf, section, key, default=default)
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
281 if value_type is not None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
282 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
283 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
284 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
285 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
286 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
287 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
288 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
289 elif isinstance(value, dict):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
290 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
291 elif value is not None:
1217
fe9782391f63 server: fixed value filtering in getConfig
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
292 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
293 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
294
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
295 def _namespaces_get_cb(self, ns_map):
1383
81b472bcf0a1 server: be sure to have strings for ns_map
Goffi <goffi@goffi.org>
parents: 1370
diff changeset
296 self.ns_map = {str(k): str(v) for k,v in ns_map.items()}
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
297
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
298 def _namespaces_get_eb(self, failure_):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
299 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
300
1522
a44f77559279 installation: moved from `setup.py` to `pyproject.toml`:
Goffi <goffi@goffi.org>
parents: 1519
diff changeset
301 @template.pass_context
1127
9234f29053b0 server, pages: update to handle multi sites themes, first draft:
Goffi <goffi@goffi.org>
parents: 1126
diff changeset
302 def _front_url_filter(self, ctx, relative_url):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
303 template_data = ctx['template_data']
1292
e30e96958fb7 server: use C.SITE_NAME_DEFAULT instead of "sat" when suitable
Goffi <goffi@goffi.org>
parents: 1287
diff changeset
304 return os.path.join(
e30e96958fb7 server: use C.SITE_NAME_DEFAULT instead of "sat" when suitable
Goffi <goffi@goffi.org>
parents: 1287
diff changeset
305 '/', C.TPL_RESOURCE, template_data.site or C.SITE_NAME_DEFAULT,
1127
9234f29053b0 server, pages: update to handle multi sites themes, first draft:
Goffi <goffi@goffi.org>
parents: 1126
diff changeset
306 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
307
1618
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
308 def _move_first_level_to_dict(
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
309 self, options: dict, key: str, keys_to_keep: list[str]
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
310 ) -> None:
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
311 """Read a config option and put value at first level into '' dict
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
312
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
313 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
314 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
315 options will be modified in place
1618
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
316 @param options: Dictionary of options to modify in place.
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
317 @param key: The key in the options dictionary to process.
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
318 @param keys_to_keep: List of keys that are allowed to remain at the first level.
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
319 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
320 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
321 conf = options[key]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
322 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
323 return
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
324 if not isinstance(conf, dict):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
325 options[key] = {'': conf}
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
326 return
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
327 default_dict = conf.get('', {})
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
328 to_delete = []
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
329 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
330 if key not in keys_to_keep:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
331 default_dict[key] = value
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
332 to_delete.append(key)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
333 for key in to_delete:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
334 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
335 if default_dict:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
336 conf[''] = default_dict
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
337
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
338 async def check_and_connect_service_profile(self):
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
339 passphrase = self.options["passphrase"]
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
340 if not passphrase:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
341 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
342 C.EXIT_BAD_ARG,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
343 _("No passphrase set for service profile, please check installation "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
344 "documentation.")
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
345 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
346 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
347 s_prof_connected = await self.bridge_call("is_connected", C.SERVICE_PROFILE)
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
348 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
349 if e.classname == "ProfileUnknownError":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
350 log.info("Service profile doesn't exist, creating it.")
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
351 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
352 xmpp_domain = await self.bridge_call("config_get", "", "xmpp_domain")
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
353 xmpp_domain = xmpp_domain.strip()
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
354 if not xmpp_domain:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
355 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
356 C.EXIT_BAD_ARG,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
357 _('"xmpp_domain" must be set to create new accounts, please '
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
358 'check documentation')
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
359 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
360 service_profile_jid_s = f"{C.SERVICE_PROFILE}@{xmpp_domain}"
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
361 await self.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
362 "in_band_account_new",
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
363 service_profile_jid_s,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
364 passphrase,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
365 "",
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
366 xmpp_domain,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
367 0,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
368 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
369 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
370 if e.condition == "conflict":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
371 log.info(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
372 _("Service's profile JID {profile_jid} already exists")
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
373 .format(profile_jid=service_profile_jid_s)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
374 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
375 elif e.classname == "UnknownMethod":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
376 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
377 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
378 _("Can't create service profile XMPP account, In-Band "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
379 "Registration plugin is not activated, you'll have to "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
380 "create the {profile!r} profile with {profile_jid!r} JID "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
381 "manually.").format(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
382 profile=C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
383 profile_jid=service_profile_jid_s)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
384 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
385 elif e.condition == "service-unavailable":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
386 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
387 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
388 _("Can't create service profile XMPP account, In-Band "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
389 "Registration is not activated on your server, you'll have "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
390 "to create the {profile!r} profile with {profile_jid!r} JID "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
391 "manually.\nNote that you'll need to activate In-Band "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
392 "Registation on your server if you want users to be able "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
393 "to create new account from {app_name}, please check "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
394 "documentation.").format(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
395 profile=C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
396 profile_jid=service_profile_jid_s,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
397 app_name=C.APP_NAME)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
398 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
399 elif e.condition == "not-acceptable":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
400 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
401 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
402 _("Can't create service profile XMPP account, your XMPP "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
403 "server doesn't allow us to create new accounts with "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
404 "In-Band Registration please check XMPP server "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
405 "configuration: {reason}"
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
406 ).format(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
407 profile=C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
408 profile_jid=service_profile_jid_s,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
409 reason=e.message)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
410 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
411
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
412 else:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
413 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
414 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
415 _("Can't create service profile XMPP account, you'll have "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
416 "do to it manually: {reason}").format(reason=e.message)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
417 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
418 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
419 await self.bridge_call("profile_create", C.SERVICE_PROFILE, passphrase)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
420 await self.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
421 "profile_start_session", passphrase, C.SERVICE_PROFILE)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
422 await self.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
423 "param_set", "JabberID", service_profile_jid_s, "Connection", -1,
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
424 C.SERVICE_PROFILE)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
425 await self.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
426 "param_set", "Password", passphrase, "Connection", -1,
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
427 C.SERVICE_PROFILE)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
428 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
429 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
430 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
431 _("Can't create service profile XMPP account, you'll have "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
432 "do to it manually: {reason}").format(reason=e.message)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
433 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
434 log.info(_("Service profile has been successfully created"))
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
435 s_prof_connected = False
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
436 else:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
437 raise SysExit(C.EXIT_BRIDGE_ERROR, e.message)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
438
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
439 if not s_prof_connected:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
440 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
441 await self.bridge_call(
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
442 "connect",
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
443 C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
444 passphrase,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
445 {},
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
446 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
447 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
448 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
449 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
450 _("Connection of service profile failed: {reason}").format(reason=e)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
451 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
452
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
453 async def backend_ready(self):
1397
ed037818d6de core (constants): renaming following global project renaming
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
454 log.info(f"Libervia Web v{self.full_version}")
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
455
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
456 # settings
1364
df40708c4c76 server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents: 1362
diff changeset
457 if self.options['dev-mode']:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
458 log.info(_("Developer mode activated"))
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
459 self.media_dir = await self.bridge_call("config_get", "", "media_dir")
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
460 self.local_dir = await self.bridge_call("config_get", "", "local_dir")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
461 self.cache_root_dir = os.path.join(self.local_dir, C.CACHE_DIR)
1612
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
462
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
463 use_local_shared_tmp = C.bool(config.config_get(
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
464 self.main_conf, "", "use_local_shared_tmp", C.BOOL_FALSE
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
465 ))
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
466 if use_local_shared_tmp:
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
467 C.LOCAL_SHARED_DIR = "_SHARED_TMP"
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
468 self.local_shared_path = self.cache_root_dir / Path(C.LOCAL_SHARED_DIR)
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
469 self.local_shared_path.mkdir(0o700, parents=True, exist_ok=True)
ebd538cb26cb server, pages (files/view): use new `use_local_shared_tmp` option.
Goffi <goffi@goffi.org>
parents: 1604
diff changeset
470
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
471 self.renderer = template.Renderer(self, self._front_url_filter)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
472 sites_names = list(self.renderer.sites_paths.keys())
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
473
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
474 self._move_first_level_to_dict(self.options, "url_redirections_dict", sites_names)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
475 self._move_first_level_to_dict(self.options, "menu_json", sites_names)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
476 self._move_first_level_to_dict(self.options, "menu_extra_json", sites_names)
1359
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
477 menu = self.options["menu_json"]
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
478 if not '' in menu:
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
479 menu[''] = C.DEFAULT_MENU
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
480 for site, value in self.options["menu_extra_json"].items():
1618
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
481 try:
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
482 menu[site].extend(value)
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
483 except KeyError:
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
484 log.warning(
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
485 "Configuration error: the key {site!r} is missing from \"menu_json\"."
5d9889f14012 server: start major redesign
Goffi <goffi@goffi.org>
parents: 1612
diff changeset
486 )
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
487
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
488 # service profile
1370
2d6cfce0df6c server: typo
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
489 if not self.options['build-only']:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
490 await self.check_and_connect_service_profile()
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
491
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
492 # restricted bridge, the one used by browser code
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
493 self.restricted_bridge = RestrictedBridge(self)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
494
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
495 # we create virtual hosts and import Libervia pages into them
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
496 self.vhost_root = vhost.NameVirtualHost()
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
497 default_site_path = Path(libervia.web.__file__).parent.resolve()
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
498 # self.sat_root is official Libervia site
1253
6d49fae517ba pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents: 1251
diff changeset
499 root_path = default_site_path / C.TEMPLATE_STATIC_DIR
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
500 self.sat_root = default_root = LiberviaRootResource(
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
501 host=self, host_name='', site_name='',
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
502 site_path=default_site_path, path=root_path)
1364
df40708c4c76 server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents: 1362
diff changeset
503 if self.options['dev-mode']:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
504 self.files_watcher.watch_dir(
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
505 default_site_path, auto_add=True, recursive=True,
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
506 callback=LiberviaPage.on_file_change, site_root=self.sat_root,
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
507 site_path=default_site_path)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
508 LiberviaPage.import_pages(self, self.sat_root)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
509 tasks_manager = TasksManager(self, self.sat_root)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
510 await tasks_manager.parse_tasks()
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
511 await tasks_manager.run_tasks()
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
512 # FIXME: handle _set_menu in a more generic way, taking care of external sites
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
513 await self.sat_root._set_menu(self.options["menu_json"])
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
514 self.vhost_root.default = default_root
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
515 existing_vhosts = {b'': default_root}
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
516
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
517 for host_name, site_name in self.options["vhosts_dict"].items():
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
518 if site_name == C.SITE_NAME_DEFAULT:
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
519 raise ValueError(
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
520 f"{C.DEFAULT_SITE_NAME} is reserved and can't be used in vhosts_dict")
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
521 encoded_site_name = site_name.encode('utf-8')
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
522 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
523 site_path = self.renderer.sites_paths[site_name]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
524 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
525 log.warning(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
526 "host {host_name} link to non existing site {site_name}, ignoring "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
527 "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
528 continue
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
529 if encoded_site_name in existing_vhosts:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
530 # we have an alias host, we re-use existing resource
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
531 res = existing_vhosts[encoded_site_name]
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
532 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
533 # for root path we first check if there is a global static dir
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
534 # if not, we use default template's static dir
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
535 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
536 if not os.path.isdir(root_path):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
537 root_path = os.path.join(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
538 site_path, C.TEMPLATE_TPL_DIR, C.TEMPLATE_THEME_DEFAULT,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
539 C.TEMPLATE_STATIC_DIR)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
540 res = LiberviaRootResource(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
541 host=self,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
542 host_name=host_name,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
543 site_name=site_name,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
544 site_path=site_path,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
545 path=root_path)
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
546
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
547 existing_vhosts[encoded_site_name] = res
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
548
1364
df40708c4c76 server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents: 1362
diff changeset
549 if self.options['dev-mode']:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
550 self.files_watcher.watch_dir(
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
551 site_path, auto_add=True, recursive=True,
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
552 callback=LiberviaPage.on_file_change, site_root=res,
1514
16228994ca3b server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents: 1512
diff changeset
553 # FIXME: site_path should always be a Path, check code above and
16228994ca3b server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents: 1512
diff changeset
554 # in template module
16228994ca3b server: fix hot reloading of modules in dev mode
Goffi <goffi@goffi.org>
parents: 1512
diff changeset
555 site_path=Path(site_path))
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
556
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
557 LiberviaPage.import_pages(self, res)
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
558 # FIXME: default pages are accessible if not overriden by external website
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
559 # while necessary for login or re-using existing pages
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
560 # 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
561 # (e.g. /blog disabled except if called by external site)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
562 LiberviaPage.import_pages(self, res, root_path=default_site_path)
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
563 tasks_manager = TasksManager(self, res)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
564 await tasks_manager.parse_tasks()
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
565 await tasks_manager.run_tasks()
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
566 await res._set_menu(self.options["menu_json"])
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
567
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
568 self.vhost_root.addHost(host_name.encode('utf-8'), res)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
569
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
570 templates_res = web_resource.Resource()
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
571 self.put_child_all(C.TPL_RESOURCE.encode('utf-8'), templates_res)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
572 for site_name, site_path in self.renderer.sites_paths.items():
1292
e30e96958fb7 server: use C.SITE_NAME_DEFAULT instead of "sat" when suitable
Goffi <goffi@goffi.org>
parents: 1287
diff changeset
573 templates_res.putChild(site_name.encode() or C.SITE_NAME_DEFAULT.encode(),
e30e96958fb7 server: use C.SITE_NAME_DEFAULT instead of "sat" when suitable
Goffi <goffi@goffi.org>
parents: 1287
diff changeset
574 static.File(site_path))
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
575
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
576 d = self.bridge_call("namespaces_get")
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
577 d.addCallback(self._namespaces_get_cb)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
578 d.addErrback(self._namespaces_get_eb)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
579
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
580 # websocket
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
581 if self.options["connection_type"] in ("https", "both"):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
582 wss = websockets.LiberviaPageWSProtocol.get_resource(secure=True)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
583 self.put_child_all(b'wss', wss)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
584 if self.options["connection_type"] in ("http", "both"):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
585 ws = websockets.LiberviaPageWSProtocol.get_resource(secure=False)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
586 self.put_child_all(b'ws', ws)
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
587
1512
65e063657597 server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents: 1509
diff changeset
588 # 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
589 self.bridge.register_signal(
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
590 "ps_event_raw", partial(LiberviaPage.on_node_event, self), "plugin"
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
591 )
1559
410064b31dca browser (calls): add some logs useful for debugging
Goffi <goffi@goffi.org>
parents: 1549
diff changeset
592
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
593 self.bridge.register_signal(
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
594 "message_new", partial(self.on_signal, "message_new")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
595 )
1516
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
596 self.bridge.register_signal(
1575
9e3f7bf55749 server: listen to `message_update` signal.
Goffi <goffi@goffi.org>
parents: 1560
diff changeset
597 "message_update", partial(self.on_signal, "message_update")
9e3f7bf55749 server: listen to `message_update` signal.
Goffi <goffi@goffi.org>
parents: 1560
diff changeset
598 )
9e3f7bf55749 server: listen to `message_update` signal.
Goffi <goffi@goffi.org>
parents: 1560
diff changeset
599 self.bridge.register_signal(
1549
e47c24204449 browser (calls): update call to handle search, control buttons, and better UI/UX:
Goffi <goffi@goffi.org>
parents: 1522
diff changeset
600 "call_setup", partial(self.on_signal, "call_setup"), "plugin"
1516
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
601 )
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
602 self.bridge.register_signal(
1560
84f312be53b4 browser (calls): handle "ringing" info message and update status when received
Goffi <goffi@goffi.org>
parents: 1559
diff changeset
603 "call_info", partial(self.on_signal, "call_info"), "plugin"
84f312be53b4 browser (calls): handle "ringing" info message and update status when received
Goffi <goffi@goffi.org>
parents: 1559
diff changeset
604 )
84f312be53b4 browser (calls): handle "ringing" info message and update status when received
Goffi <goffi@goffi.org>
parents: 1559
diff changeset
605 self.bridge.register_signal(
1516
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
606 "call_ended", partial(self.on_signal, "call_ended"), "plugin"
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
607 )
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
608 self.bridge.register_signal(
1604
4a9679369856 browser (call): implements group calls:
Goffi <goffi@goffi.org>
parents: 1601
diff changeset
609 "call_group_setup", partial(self.on_signal, "call_group_setup"), "plugin"
4a9679369856 browser (call): implements group calls:
Goffi <goffi@goffi.org>
parents: 1601
diff changeset
610 )
4a9679369856 browser (call): implements group calls:
Goffi <goffi@goffi.org>
parents: 1601
diff changeset
611 self.bridge.register_signal(
1516
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
612 "ice_candidates_new", partial(self.on_signal, "ice_candidates_new"), "plugin"
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
613 )
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
614 self.bridge.register_signal(
1559
410064b31dca browser (calls): add some logs useful for debugging
Goffi <goffi@goffi.org>
parents: 1549
diff changeset
615 "ice_restart", partial(self.on_signal, "ice_restart"), "plugin"
410064b31dca browser (calls): add some logs useful for debugging
Goffi <goffi@goffi.org>
parents: 1549
diff changeset
616 )
410064b31dca browser (calls): add some logs useful for debugging
Goffi <goffi@goffi.org>
parents: 1549
diff changeset
617 self.bridge.register_signal(
1516
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
618 "action_new", self.action_new_handler,
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
619 )
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
620
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
621 # libervia applications handling
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
622 self.bridge.register_signal(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
623 "application_started", self.application_started_handler, "plugin"
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
624 )
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
625 self.bridge.register_signal(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
626 "application_error", self.application_error_handler, "plugin"
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
627 )
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
628
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
629 #  Progress handling
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
630 self.bridge.register_signal(
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
631 "progress_started", partial(ProgressHandler._signal, "started")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
632 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
633 self.bridge.register_signal(
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
634 "progress_finished", partial(ProgressHandler._signal, "finished")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
635 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
636 self.bridge.register_signal(
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
637 "progress_error", partial(ProgressHandler._signal, "error")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
638 )
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
639
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
640 # media dirs
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
641 # FIXME: get rid of dirname and "/" in C.XXX_DIR
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
642 self.put_child_all(os.path.dirname(C.MEDIA_DIR).encode('utf-8'),
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
643 ProtectedFile(self.media_dir))
1242
8f39d98416c5 server: added a child for common cache
Goffi <goffi@goffi.org>
parents: 1240
diff changeset
644
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
645 self.cache_resource = web_resource.NoResource()
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
646 self.put_child_all(C.CACHE_DIR.encode('utf-8'), self.cache_resource)
1242
8f39d98416c5 server: added a child for common cache
Goffi <goffi@goffi.org>
parents: 1240
diff changeset
647 self.cache_resource.putChild(
8f39d98416c5 server: added a child for common cache
Goffi <goffi@goffi.org>
parents: 1240
diff changeset
648 b"common", ProtectedFile(str(self.cache_root_dir / Path("common"))))
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
649
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
650 # redirections
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
651 for root in self.roots:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
652 await root._init_redirections(self.options)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
653
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
654 # 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
655 del self.options["url_redirections_dict"]
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
656
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
657 server.Request.defaultContentType = "text/html; charset=utf-8"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
658 wrapped = web_resource.EncodingResourceWrapper(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
659 self.vhost_root, [server.GzipEncoderFactory()]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
660 )
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
661 self.site = server.Site(wrapped)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
662 self.site.sessionFactory = WebSession
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
663
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
664 def _bridge_cb(self):
1367
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
665 del self._bridge_retry
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
666 self.bridge.ready_get(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
667 lambda: self.initialised.callback(None),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
668 lambda failure: self.initialised.errback(Exception(failure)),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
669 )
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
670 self.initialised.addCallback(lambda __: defer.ensureDeferred(self.backend_ready()))
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
671
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
672 def _bridge_eb(self, failure_):
1594
93abef9a3548 server: catch `DBusException` and retry to connect in this case.
Goffi <goffi@goffi.org>
parents: 1592
diff changeset
673 if (
93abef9a3548 server: catch `DBusException` and retry to connect in this case.
Goffi <goffi@goffi.org>
parents: 1592
diff changeset
674 isinstance(failure_, BridgeExceptionNoService)
93abef9a3548 server: catch `DBusException` and retry to connect in this case.
Goffi <goffi@goffi.org>
parents: 1592
diff changeset
675 or isinstance(failure_, DBusException)
93abef9a3548 server: catch `DBusException` and retry to connect in this case.
Goffi <goffi@goffi.org>
parents: 1592
diff changeset
676 ):
1367
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
677 if self._bridge_retry:
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
678 if self._bridge_retry < 0:
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
679 print(_("Can't connect to bridge, will retry indefinitely. "
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
680 "Next try in 1s."))
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
681 else:
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
682 self._bridge_retry -= 1
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
683 print(
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
684 _(
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
685 "Can't connect to bridge, will retry in 1 s ({retries_left} "
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
686 "trie(s) left)."
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
687 ).format(retries_left=self._bridge_retry)
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
688 )
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
689 time.sleep(1)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
690 self.bridge.bridge_connect(callback=self._bridge_cb, errback=self._bridge_eb)
1367
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
691 return
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
692
1594
93abef9a3548 server: catch `DBusException` and retry to connect in this case.
Goffi <goffi@goffi.org>
parents: 1592
diff changeset
693 print("Can't connect to Libervia backend, are you sure it's launched?")
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
694 else:
1594
93abef9a3548 server: catch `DBusException` and retry to connect in this case.
Goffi <goffi@goffi.org>
parents: 1592
diff changeset
695 log.exception("Can't connect to bridge")
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
696 sys.exit(1)
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
697
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
698 @property
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
699 def version(self):
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
700 """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
701 return C.APP_VERSION
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
702
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
703 @property
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
704 def full_version(self):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
705 """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
706 version = self.version
1519
01b8d68edd70 version: use standard Python's `dev0` instead of Libervia specific `D` for dev version
Goffi <goffi@goffi.org>
parents: 1518
diff changeset
707 if version.split(".")[-1] == "dev0":
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
708 # 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
709 try:
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
710 return self._version_cache
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
711 except AttributeError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
712 self._version_cache = "{} ({})".format(
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
713 version, utils.get_repository_data(libervia.web)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
714 )
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
715 return self._version_cache
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
716 else:
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
717 return version
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
718
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
719 def bridge_call(self, method_name, *args, **kwargs):
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
720 """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
721
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
722 @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
723 @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
724
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
725 """
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
726 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
727
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
728 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
729 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
730 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
731 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
732 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
733 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
734 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
735
1138
ef565839dada server: don't convert failure in errback to jsonrpclib.Fault anymore:
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
736 def _errback(failure_):
ef565839dada server: don't convert failure in errback to jsonrpclib.Fault anymore:
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
737 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
738
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
739 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
740 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
741 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
742 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
743
1516
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
744 def action_new_handler(
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
745 self,
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
746 action_data_s: str,
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
747 action_id: str,
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
748 security_limit: int,
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
749 profile: str
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
750 ) -> None:
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
751 if security_limit > C.SECURITY_LIMIT:
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
752 log.debug(
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
753 f"ignoring action {action_id} due to security limit"
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
754 )
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
755 else:
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
756 self.on_signal(
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
757 "action_new", action_data_s, action_id, security_limit, profile
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
758 )
a3ca1bab6eb1 server, restricted bridge: add new methods and signals to prepare calls implementation:
Goffi <goffi@goffi.org>
parents: 1514
diff changeset
759
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
760 def on_signal(self, signal_name, *args):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
761 profile = args[-1]
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
762 if not profile:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
763 log.error(f"got signal without profile: {signal_name}, {args}")
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
764 return
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
765 session_iface.WebSession.send(
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
766 profile,
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
767 "bridge",
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
768 {"signal": signal_name, "args": args}
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
769 )
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
770
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
771 def application_started_handler(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
772 self,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
773 name: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
774 instance_id: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
775 extra_s: str
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
776 ) -> None:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
777 callback = self.apps_cb.pop(instance_id, None)
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
778 if callback is not None:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
779 defer.ensureDeferred(callback(str(name), str(instance_id)))
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
780
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
781 def application_error_handler(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
782 self,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
783 name: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
784 instance_id: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
785 extra_s: str
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
786 ) -> None:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
787 callback = self.apps_cb.pop(instance_id, None)
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
788 if callback is not None:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
789 extra = data_format.deserialise(extra_s)
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
790 log.error(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
791 f"Can't start application {name}: {extra['class']}\n{extra['msg']}"
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
792 )
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
793
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1258
diff changeset
794 async def _logged(self, profile, request):
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
795 """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
796
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
797 @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
798 @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
799 @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
800 - 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
801 - 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
802 @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
803 """
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
804 register_with_ext_jid = self.waiting_profiles.get_register_with_ext_jid(profile)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
805 self.waiting_profiles.purge_request(profile)
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
806 session = request.getSession()
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
807 web_session = session_iface.IWebSession(session)
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
808 if web_session.profile:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
809 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
810 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
811
1305
db9ea167c409 pages: `profile` is now exposed to scripts
Goffi <goffi@goffi.org>
parents: 1293
diff changeset
812 # XXX: we force string because python D-Bus has its own string type (dbus.String)
db9ea167c409 pages: `profile` is now exposed to scripts
Goffi <goffi@goffi.org>
parents: 1293
diff changeset
813 # which may cause trouble when exposing it to scripts
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
814 web_session.profile = str(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
815 self.prof_connected.add(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
816 cache_dir = os.path.join(
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
817 self.cache_root_dir, "profiles", regex.path_escape(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
818 )
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
819 # 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
820 # profile's cache directory, without uuid
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
821 self.cache_resource.putChild(web_session.uuid.encode('utf-8'),
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
822 ProtectedFile(cache_dir))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
823 log.debug(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
824 _("profile cache resource added from {uuid} to {path}").format(
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
825 uuid=web_session.uuid, path=cache_dir
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
826 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
827 )
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
828
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
829 def on_expire():
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
830 log.info("Session expired (profile={profile})".format(profile=profile))
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
831 self.cache_resource.delEntity(web_session.uuid.encode('utf-8'))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
832 log.debug(
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
833 _("profile cache resource {uuid} deleted").format(uuid=web_session.uuid)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
834 )
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
835 web_session.on_expire()
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
836 if web_session.ws_socket is not None:
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
837 web_session.ws_socket.close()
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
838 # and now we disconnect the profile
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
839 self.bridge_call("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
840
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
841 session.notifyOnExpire(on_expire)
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
842
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
843 # FIXME: those session infos should be returned by connect or is_connected
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
844 infos = await self.bridge_call("session_infos_get", profile)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
845 web_session.jid = jid.JID(infos["jid"])
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
846 own_bare_jid_s = web_session.jid.userhost()
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
847 own_id_raw = await self.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
848 "identity_get", own_bare_jid_s, [], True, profile)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
849 web_session.identities[own_bare_jid_s] = data_format.deserialise(own_id_raw)
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
850 web_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
851
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
852 state = C.PROFILE_LOGGED_EXT_JID if register_with_ext_jid else C.PROFILE_LOGGED
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1258
diff changeset
853 return 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
854
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
855 async def connect(self, request, login, password):
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
856 """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
857
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
858 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
859 @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
860 @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
861 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
862 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
863 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
864 @param password(unicode): user password
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
865 @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
866 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
867 @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
868 @raise exceptions.ProfileUnknownError: this login doesn't exist
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
869 @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
870 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
871 @raise exceptions.NotReady: a profile connection is already waiting
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
872 @raise exceptions.TimeoutError: didn't received and answer from bridge
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
873 @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
874 @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
875 @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
876 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
877 # 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
878 if login.startswith("@"):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
879 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
880
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
881 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
882 log.debug("logging a guest account")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
883 elif "@" in login:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
884 if login.count("@") != 1:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
885 raise failure.Failure(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
886 exceptions.DataError("Invalid login: {login}".format(login=login))
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
887 )
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
888 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
889 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
890 except (RuntimeError, jid.InvalidFormat, AttributeError):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
891 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
892
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
893 # FIXME: should it be cached?
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
894 new_account_domain = await self.bridge_call("account_domain_new_get")
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
895
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
896 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
897 # 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
898 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
899 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
900 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
901 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
902
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
903 try:
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
904 profile = await self.bridge_call("profile_name_get", login)
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
905 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
906 # 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
907 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
908 login_jid is not None and login_jid.user
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
909 ): # try to create a new libervia.backend 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
910 if not self.options["allow_registration"]:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
911 log.warning(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
912 "Trying to register JID account while registration is not "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
913 "allowed")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
914 raise failure.Failure(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
915 exceptions.DataError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
916 "JID login while registration is not allowed"
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
917 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
918 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
919 profile = login # FIXME: what if there is a resource?
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
920 connect_method = "credentials_xmpp_connect"
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
921 register_with_ext_jid = True
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
922 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
923 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
924 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
925 if profile != login or (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
926 not password
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
927 and profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
928 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
929 ):
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
930 # 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
931 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
932 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
933
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
934 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
935
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
936 # we check the active session
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
937 web_session = session_iface.IWebSession(request.getSession())
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
938 if web_session.profile != profile:
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
939 # It's a different profile, we need to disconnect it.
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
940 # We always purge session even if no user was logged, to avoid the re-use of
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
941 # cookie (i.e. to avoid session fixation).
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
942 if web_session.profile:
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
943 # no need to log a warning if the previous profile was the service profile
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
944 log.warning(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
945 "{new_profile} requested login, but {old_profile} was already "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
946 "connected, disconnecting {old_profile}").format(
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
947 old_profile=web_session.profile, new_profile=profile))
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
948 self.purge_session(request)
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
949
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
950 if self.waiting_profiles.get_request(profile):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
951 #  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
952 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
953
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
954 self.waiting_profiles.set_request(request, profile, register_with_ext_jid)
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
955 try:
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
956 connected = await self.bridge_call(connect_method, profile, password)
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
957 except Exception as failure_:
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
958 fault = getattr(failure_, 'classname', None)
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
959 self.waiting_profiles.purge_request(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
960 if fault in ("PasswordError", "ProfileUnknownError"):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
961 log.info("Profile {profile} doesn't exist or the submitted password is "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
962 "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
963 raise failure.Failure(ValueError(C.PROFILE_AUTH_ERROR))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
964 elif fault == "SASLAuthError":
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
965 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
966 .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
967 raise failure.Failure(ValueError(C.XMPP_AUTH_ERROR))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
968 elif fault == "NoReply":
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
969 log.info(_("Did not receive a reply (the timeout expired or the "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
970 "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
971 raise exceptions.TimeOutError
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
972 elif fault is None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
973 log.info(_("Unexepected failure: {failure_}").format(failure_=failure))
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
974 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
975 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
976 log.error('Unmanaged fault class "{fault}" in errback for the '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
977 'connection of profile {profile}'.format(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
978 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
979 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
980
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
981 if connected:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
982 #  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
983 # do we have a corresponding session in Libervia?
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
984 web_session = session_iface.IWebSession(request.getSession())
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
985 if web_session.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
986 # yes, session is active
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
987 if web_session.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
988 # 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
989 # so this line should never be reached
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
990 log.error(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
991 "session profile [{session_profile}] differs from login "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
992 "profile [{profile}], this should not happen!")
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
993 .format(session_profile=web_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
994 raise exceptions.InternalError("profile mismatch")
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
995 return C.SESSION_ACTIVE
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
996 log.info(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
997 _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
998 "profile {profile} was already connected in backend".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
999 profile=profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1000 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1001 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1002 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1003 #  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
1004
1598
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
1005 state = await self._logged(profile, request)
86c7a3a625d5 server: always start a new session on connection:
Goffi <goffi@goffi.org>
parents: 1594
diff changeset
1006 return 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
1007
1592
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1008 async def check_registration_id(self, request: server.Request) -> None:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1009 """Check if a valid registration ID is found in request data
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1010
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1011 @param request: request used for account registration
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1012
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1013 @raise exceptions.NotFound: not valid registration ID has been found
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1014 """
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1015 try:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1016 r_data = request.data
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1017 registration_id = r_data["registration_id"]
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1018 except (AttributeError, KeyError):
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1019 log.warning("Can't find registration ID")
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1020 raise exceptions.NotFound
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1021 if registration_id is None:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1022 raise exceptions.NotFound
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1023 try:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1024 registration_link_data = data_format.deserialise(
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1025 await self.bridge_call(
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1026 "registration_link_get",
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1027 registration_id
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1028 )
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1029 )
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1030 except BridgeException as e:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1031 if e.classname == "NotFound":
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1032 log.warning(f"Invalid registration ID: {registration_id!r}")
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1033 else:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1034 log.exception(f"Can't get registration ID data: {e}")
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1035 raise exceptions.NotFound
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1036 else:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1037 log.info(
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1038 f"Using registration ID {registration_id!r} ("
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1039 f"{registration_link_data['recipient']})."
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1040 )
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1041
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1042
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1043 async def register_new_account(
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1044 self,
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1045 request: server.Request,
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1046 login: str,
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1047 password: str,
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1048 email: str
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1049 ) -> str:
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
1050 """Create a new account, or return error
1592
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1051
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1052 @param request: request linked to the session
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1053 @param login: new account requested login
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1054 @param email: new account email
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1055 @param password: new account password
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1056 @return: a constant indicating the 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
1057 - 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
1058 - 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
1059 - 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
1060 - 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
1061 - 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
1062 @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
1063 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1064 if not self.options["allow_registration"]:
1592
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1065 try:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1066 await self.check_registration_id(request)
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1067 except exceptions.NotFound:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1068 log.warning(
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1069 _("Registration received while it is not allowed, hack attempt?")
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1070 )
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1071 raise failure.Failure(
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1072 exceptions.PermissionError("Registration is not allowed on this server")
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1073 )
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
1074
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1075 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1076 not re.match(C.REG_LOGIN_RE, login)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1077 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
1078 or len(password) < C.PASSWORD_MIN_LENGTH
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 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
1081
1592
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1082 try:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1083 await self.bridge_call("libervia_account_register", email, password, login)
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1084 except BridgeException as e:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1085 status = e.classname
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
1086 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
1087 return C.ALREADY_EXISTS
1164
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1088 elif status == "InvalidCertificate":
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1089 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
1090 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
1091 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
1092 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1093 log.error(
1592
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1094 _("Unknown registering error status: {status}\n{e}").format(
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1095 status=status, e=e
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1096 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1097 )
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
1098 return status
1592
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1099 else:
291a7026cb2b server: handle new registration link feature, following backend implementation
Goffi <goffi@goffi.org>
parents: 1575
diff changeset
1100 return C.REGISTRATION_SUCCEED
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
1101
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
1102 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
1103 """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
1104
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
1105 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
1106 @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
1107 @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
1108 @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
1109 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
1110
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1111 def init_eb(self, failure):
1361
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1112 from twisted.application import app
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1113 if failure.check(SysExit):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1114 if failure.value.message:
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1115 log.error(failure.value.message)
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1116 app._exitCode = failure.value.exit_code
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1117 reactor.stop()
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1118 else:
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1119 log.error(_("Init error: {msg}").format(msg=failure))
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1120 app._exitCode = C.EXIT_INTERNAL_ERROR
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1121 reactor.stop()
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1122 return failure
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1123
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1124 def _build_only_cb(self, __):
1368
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1125 log.info(_("Stopping here due to --build-only flag"))
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1126 self.stop()
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1127
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
1128 def startService(self):
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1129 """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
1130 self._init()
1368
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1131 if self.options['build-only']:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1132 self.initialised.addCallback(self._build_only_cb)
1368
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1133 else:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1134 self.initialised.addCallback(self._start_service)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1135 self.initialised.addErrback(self.init_eb)
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
1136
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1137 ## URLs ##
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents: 917
diff changeset
1138
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1139 def put_child_sat(self, path, resource):
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
1140 """Add a child to the libervia.backend resource"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1141 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1142 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1143 self.sat_root.putChild(path, resource)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1144
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1145 def put_child_all(self, path, resource):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1146 """Add a child to all vhost root resources"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1147 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1148 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1149 # 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
1150 # of the resource
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1151 # 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
1152 wrapped_res = web_resource.EncodingResourceWrapper(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1153 resource, [server.GzipEncoderFactory()])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1154 for root in self.roots:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1155 root.putChild(path, wrapped_res)
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1156
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1157 def get_build_path(self, site_name: str, dev: bool=False) -> Path:
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1158 """Generate build path for a given site name
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1159
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1160 @param site_name: name of the site
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1161 @param dev: return dev build dir if True, production one otherwise
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1162 dev build dir is used for installing dependencies needed temporarily (e.g.
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1163 to compile files), while production build path is the one served by the
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1164 HTTP server, where final files are downloaded.
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1165 @return: path to the build directory
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1166 """
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1167 sub_dir = C.DEV_BUILD_DIR if dev else C.PRODUCTION_BUILD_DIR
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1168 build_path_elts = [
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1169 config.config_get(self.main_conf, "", "local_dir"),
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1170 C.CACHE_DIR,
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1171 C.LIBERVIA_CACHE,
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1172 sub_dir,
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1173 regex.path_escape(site_name or C.SITE_NAME_DEFAULT)]
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1174 build_path = Path("/".join(build_path_elts))
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1175 return build_path.expanduser().resolve()
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1176
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1177 def get_ext_base_url_data(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
1178 """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
1179
1435
396d5606477f server (proxy): add "Forwarded" and "X-Forwarded-xxx" headers to reverse proxy
Goffi <goffi@goffi.org>
parents: 1397
diff changeset
1180 this method try to retrieve the base URL found by external user
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
1181 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
1182 - 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
1183 - 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
1184 - URL of the request
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1185 @return (urlparse.SplitResult): SplitResult instance with only scheme and
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1186 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
1187 """
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
1188 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
1189 url_path = request.URLPath()
1467
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1190
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1191 try:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1192 forwarded = request.requestHeaders.getRawHeaders(
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1193 "forwarded"
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1194 )[0]
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1195 except TypeError:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1196 # we try deprecated headers
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1197 try:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1198 proxy_netloc = request.requestHeaders.getRawHeaders(
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1199 "x-forwarded-host"
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1200 )[0]
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1201 except TypeError:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1202 proxy_netloc = None
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1203 try:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1204 proxy_scheme = request.requestHeaders.getRawHeaders(
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1205 "x-forwarded-proto"
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1206 )[0]
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1207 except TypeError:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1208 proxy_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
1209 else:
1467
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1210 fwd_data = {
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1211 k.strip(): v.strip()
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1212 for k,v in (d.split("=") for d in forwarded.split(";"))
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1213 }
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1214 proxy_netloc = fwd_data.get("host")
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1215 proxy_scheme = fwd_data.get("proto")
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
1216
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1217 return urllib.parse.SplitResult(
1467
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1218 ext_data.scheme or proxy_scheme or url_path.scheme.decode(),
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1219 ext_data.netloc or proxy_netloc or url_path.netloc.decode(),
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1220 ext_data.path or "/",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1221 "",
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1222 "",
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1223 )
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
1224
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1225 def get_ext_base_url(
1459
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1226 self,
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1227 request: server.Request,
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1228 path: str = "",
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1229 query: str = "",
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1230 fragment: str = "",
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1231 scheme: Optional[str] = None,
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1232 ) -> str:
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
1233 """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
1234
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
1235 external URL is the URL seen by external user
1459
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1236 @param path: same as for urlsplit.urlsplit
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
1237 path will be prefixed to follow found external URL if suitable
1459
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1238 @param params: same as for urlsplit.urlsplit
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1239 @param query: same as for urlsplit.urlsplit
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1240 @param fragment: same as for urlsplit.urlsplit
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1241 @param scheme: if not None, will override scheme from base URL
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1242 @return: external 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
1243 """
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1244 split_result = self.get_ext_base_url_data(request)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1245 return urllib.parse.urlunsplit(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1246 (
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1247 split_result.scheme if scheme is None else scheme,
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1248 split_result.netloc,
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1249 os.path.join(split_result.path, path),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1250 query,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1251 fragment,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1252 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1253 )
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
1254
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1255 def check_redirection(self, vhost_root: LiberviaRootResource, url_path: str) -> str:
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1256 """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
1257
1458
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1258 @param vhost_root: root of this virtual host
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1259 @param url_path: path of the url to check
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1260 @return: possibly redirected URL which should link to the same location
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1261 """
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1262 inv_redirections = vhost_root.inv_redirections
1458
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1263 url_parts = url_path.strip("/").split("/")
1475
83cd4862b134 server: fix args range in checkRedirection
Goffi <goffi@goffi.org>
parents: 1467
diff changeset
1264 for idx in range(len(url_parts), -1, -1):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1265 test_url = "/" + "/".join(url_parts[:idx])
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1266 if test_url in inv_redirections:
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1267 rem_url = url_parts[idx:]
1099
ea0d41ef3719 server: return absolute page in checkRedirection
Goffi <goffi@goffi.org>
parents: 1098
diff changeset
1268 return os.path.join(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1269 "/", "/".join([inv_redirections[test_url]] + rem_url)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1270 )
1458
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1271 return url_path
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
1272
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1273 ## Sessions ##
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1274
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1275 def purge_session(self, request):
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1276 """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
1277 session = request.session
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1278 if session is not None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1279 log.debug(_("session purge"))
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1280 web_session = self.get_session_data(request, session_iface.IWebSession)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1281 socket = web_session.ws_socket
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1282 if socket is not None:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1283 socket.close()
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1284 session.ws_socket = None
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1285 session.expire()
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1286 # 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
1287 # session during request handling
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1288 request._secureSession = request._insecureSession = None
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1289
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1290 def get_session_data(self, request, *args):
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1291 """helper method to retrieve session data
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1292
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1293 @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
1294 @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
1295 @return (iterator(data)): requested session data
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1296 """
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1297 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
1298 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
1299 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
1300 else:
94f88277c2e7 libervia (pages): getSessionData return one session if used with one argument, else an iterator
Goffi <goffi@goffi.org>
parents: 923
diff changeset
1301 return (iface(session) for iface in args)
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1302
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1303 @defer.inlineCallbacks
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1304 def get_affiliation(self, request, service, node):
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1305 """retrieve pubsub node affiliation for current user
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1306
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1307 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
1308 @param request(server.Request): request linked to the session
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1309 @param service(jid.JID): pubsub service
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1310 @param node(unicode): pubsub node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1311 @return (unicode): affiliation
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1312 """
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1313 web_session = self.get_session_data(request, session_iface.IWebSession)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1314 if web_session.profile is None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1315 raise exceptions.InternalError("profile must be set to use this method")
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1316 affiliation = web_session.get_affiliation(service, node)
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1317 if affiliation is not None:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1318 defer.returnValue(affiliation)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1319 else:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1320 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1321 affiliations = yield self.bridge_call(
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1322 "ps_affiliations_get", service.full(), node, web_session.profile
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1323 )
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1324 except Exception as e:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1325 log.warning(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1326 "Can't retrieve affiliation for {service}/{node}: {reason}".format(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1327 service=service, node=node, reason=e
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1328 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1329 )
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1330 affiliation = ""
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1331 else:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1332 try:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1333 affiliation = affiliations[node]
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1334 except KeyError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1335 affiliation = ""
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1336 web_session.set_affiliation(service, node, affiliation)
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1337 defer.returnValue(affiliation)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1338
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1339 ## Websocket (dynamic pages) ##
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1340
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1341 def get_websocket_url(self, request):
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1342 base_url_split = self.get_ext_base_url_data(request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1343 if base_url_split.scheme.endswith("s"):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1344 scheme = "wss"
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1345 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1346 scheme = "ws"
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1347
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1348 return self.get_ext_base_url(request, path=scheme, scheme=scheme)
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1349
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1350
1019
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1351 ## Various utils ##
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1352
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1353 def get_http_date(self, timestamp=None):
1019
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1354 now = time.gmtime(timestamp)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1355 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
1356 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
1357 )
1019
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1358 return time.strftime(fmt_date, now)
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1359
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
1360 ## 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
1361
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1362 def _start_service(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
1363 """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
1364
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
1365 @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
1366 @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
1367 @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
1368 """
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
1369 # now that we have service profile connected, we add resource for its cache
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1370 service_path = regex.path_escape(C.SERVICE_PROFILE)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1371 cache_dir = os.path.join(self.cache_root_dir, "profiles", service_path)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1372 self.cache_resource.putChild(service_path.encode('utf-8'),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1373 ProtectedFile(cache_dir))
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1374 self.service_cache_url = "/" + os.path.join(C.CACHE_DIR, service_path)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1375 session_iface.WebSession.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
1376
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1377 if self.options["connection_type"] in ("https", "both"):
1274
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1378 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1379 tls.tls_options_check(self.options)
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1380 context_factory = tls.get_tls_context_factory(self.options)
1274
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1381 except exceptions.ConfigError as e:
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1382 log.warning(
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1383 f"There is a problem in TLS settings in your configuration file: {e}")
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1384 self.quit(2)
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1385 except exceptions.DataError as e:
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1386 log.warning(
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1387 f"Can't set TLS: {e}")
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1388 self.quit(1)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1389 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
1390 if self.options["connection_type"] in ("http", "both"):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1391 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1392 self.options["connection_type"] == "both"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1393 and self.options["redirect_to_https"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1394 ):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1395 reactor.listenTCP(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1396 self.options["port"],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1397 server.Site(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1398 RedirectToHTTPS(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1399 self.options["port"], self.options["port_https_ext"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1400 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1401 ),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1402 )
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1403 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1404 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
1405
1108
7ec1ba86d38d server: replaced blocking calls to bridge by bridgeCall which is not blocking
Goffi <goffi@goffi.org>
parents: 1106
diff changeset
1406 @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
1407 def stopService(self):
473
d1565906f228 server_side: fixed some forgotten print statement
Goffi <goffi@goffi.org>
parents: 472
diff changeset
1408 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
1409 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
1410 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
1411 try:
1509
106bae41f5c8 massive refactoring from camelCase -> snake_case. See backend commit log for more details
Goffi <goffi@goffi.org>
parents: 1506
diff changeset
1412 yield self.bridge_call("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
1413 except Exception:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1414 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
1415
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
1416 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
1417 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
1418
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
1419 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
1420 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
1421
812
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1422 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
1423 """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
1424
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1425 @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
1426 """
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1427 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
1428 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
1429
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1430
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1431 class RedirectToHTTPS(web_resource.Resource):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1432 def __init__(self, old_port, new_port):
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1433 web_resource.Resource.__init__(self)
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1434 self.isLeaf = True
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1435 self.old_port = old_port
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1436 self.new_port = new_port
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1437
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1438 def render(self, request):
1354
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
1439 netloc = request.URLPath().netloc.decode().replace(
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
1440 f":{self.old_port}", f":{self.new_port}"
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1441 )
1354
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
1442 url = f"https://{netloc}{request.uri.decode()}"
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
1443 return web_util.redirectTo(url.encode(), request)
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1444
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1445
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1446 registerAdapter(session_iface.WebSession, server.Session, session_iface.IWebSession)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1447 registerAdapter(
1518
eb00d593801d refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents: 1516
diff changeset
1448 session_iface.WebGuestSession, server.Session, session_iface.IWebGuestSession
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1449 )