annotate libervia/server/server.py @ 1506:ce879da7fcf7

server: fix `on_signal` callback
author Goffi <goffi@goffi.org>
date Thu, 23 Mar 2023 17:50:54 +0100
parents a169cbc315f0
children 106bae41f5c8
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
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
25 from typing import Dict, Optional, Callable
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
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
30 from sat.core import exceptions
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
31 from sat.core.i18n import D_, _
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
32 from sat.core.log import getLogger
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
33 from sat.tools import utils
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
34 from sat.tools import config
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
35 from sat.tools.common import regex
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
36 from sat.tools.common import template
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
37 from sat.tools.common import uri as common_uri
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
38 from sat.tools.common import data_format
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
39 from sat.tools.common import tls
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
40 from sat.tools.common.utils import OrderedSet, recursive_update
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
41 from sat_frontends.bridge.bridge_frontend import BridgeException
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
42 from sat_frontends.bridge.dbus_bridge import (
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
43 Bridge,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
44 BridgeExceptionNoService,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
45 const_TIMEOUT as BRIDGE_TIMEOUT,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
46 )
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
47 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
48 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
49 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
50 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
51 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
52 from twisted.web import server
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
53 from twisted.web import static
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
54 from twisted.web import resource as web_resource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
55 from twisted.web import util as web_util
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
56 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
57 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
58
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
59 import libervia
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
60 from libervia.server import websockets
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
61 from libervia.server import session_iface
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
62 from libervia.server.constants import Const as C
984
f0fc28b3bd1e server: moved LiberviaPage code in its own module
Goffi <goffi@goffi.org>
parents: 980
diff changeset
63 from libervia.server.pages import LiberviaPage
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
64 from libervia.server.tasks.manager import TasksManager
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
65 from libervia.server.utils import ProgressHandler, quote
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
66
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
67 from . import proxy
1287
1f26d8c2afc1 server: restricted_bridge first draft:
Goffi <goffi@goffi.org>
parents: 1282
diff changeset
68 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
69
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
70 log = getLogger(__name__)
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
71
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
72
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
73 DEFAULT_MASK = (inotify.IN_CREATE | inotify.IN_MODIFY | inotify.IN_MOVE_SELF
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
74 | inotify.IN_MOVED_TO)
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
75
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
76
1361
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
77 class SysExit(Exception):
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 __init__(self, exit_code, message=""):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
80 self.exit_code = exit_code
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
81 self.message = message
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
82
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
83 def __str__(self):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
84 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
85
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
86
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
87 class FilesWatcher(object):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
88 """Class to check files modifications using iNotify"""
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
89 _notifier = None
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
90
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
91 def __init__(self, host):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
92 self.host = host
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
93
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
94 @property
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
95 def notifier(self):
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
96 if self._notifier == None:
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
97 notifier = self.__class__._notifier = inotify.INotify()
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
98 notifier.startReading()
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
99 return self._notifier
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
100
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
101 def _checkCallback(self, dir_path, callback, recursive):
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
102 # 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
103 # 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
104 # 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
105 # 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
106 # and add it otherwise.
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
107 # 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
108 if recursive:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
109 for child in dir_path.walk():
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
110 if child.isdir():
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
111 self._checkCallback(child, callback, recursive=False)
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
112 else:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
113 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
114 if watch_id is None:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
115 log.warning(
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
116 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
117 )
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
118 else:
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
119 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
120 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
121 watch_point.callbacks.append(callback)
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
122
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
123 def watchDir(self, dir_path, callback, mask=DEFAULT_MASK, auto_add=False,
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
124 recursive=False, **kwargs):
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
125 dir_path = str(dir_path)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
126 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
127 wrapped_callback = lambda __, filepath, mask: callback(
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
128 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
129 callbacks = [wrapped_callback]
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
130 dir_path = filepath.FilePath(dir_path)
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
131 self.notifier.watch(
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
132 dir_path, mask=mask, autoAdd=auto_add, recursive=recursive,
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
133 callbacks=callbacks)
1251
a1606e2a92eb server: fixed watching a directory which is already watched:
Goffi <goffi@goffi.org>
parents: 1246
diff changeset
134 self._checkCallback(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
135
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
136
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
137 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
138 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
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 __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
141 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
142 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
143
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 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
145 """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
146 self.__lock = True
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
147 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
148
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 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
150 """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
151 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
152 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
153
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
154 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
155 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
156 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
157
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
158
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
159 class ProtectedFile(static.File):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
160 """A static.File class which doesn't show directory listing"""
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
161
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
162 def __init__(self, path, *args, **kwargs):
1196
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
163 if "defaultType" not in kwargs and len(args) < 2:
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
164 # defaultType is second positional argument, and Twisted uses it
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
165 # in File.createSimilarFile, so we set kwargs only if it is missing
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
166 # in kwargs and it is not in a positional argument
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
167 kwargs["defaultType"] = "application/octet-stream"
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
168 super(ProtectedFile, self).__init__(str(path), *args, **kwargs)
1196
a15ac511390c server: changed defaultType for File resources to "application/octet-stream" instead of "text/html"
Goffi <goffi@goffi.org>
parents: 1191
diff changeset
169
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
170 def directoryListing(self):
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
171 return web_resource.NoResource()
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
172
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
173
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
174 def getChild(self, path, request):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
175 return super().getChild(path, request)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
176
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
177 def getChildWithDefault(self, path, request):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
178 return super().getChildWithDefault(path, request)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
179
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
180 def getChildForRequest(self, request):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
181 return super().getChildForRequest(request)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
182
1460
0ea54090e414 server: renamed `sat-app` to `libervia-app` following global renaming
Goffi <goffi@goffi.org>
parents: 1459
diff changeset
183
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
184 class LiberviaRootResource(ProtectedFile):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
185 """Specialized resource for Libervia root
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
186
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
187 handle redirections declared in sat.conf
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
188 """
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
189
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
190 def __init__(self, host, host_name, site_name, site_path, *args, **kwargs):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
191 ProtectedFile.__init__(self, *args, **kwargs)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
192 self.host = host
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
193 self.host_name = host_name
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
194 self.site_name = site_name
1253
6d49fae517ba pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents: 1251
diff changeset
195 self.site_path = Path(site_path)
1321
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
196 self.default_theme = self.getConfig('theme')
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
197 if self.default_theme is None:
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
198 if not host_name:
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
199 # FIXME: we use bulma theme by default for main site for now
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
200 # as the development is focusing on this one, and default theme may
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
201 # be broken
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
202 self.default_theme = 'bulma'
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
203 else:
eb85ef26cb4a server: use `bulma` theme as default for default site:
Goffi <goffi@goffi.org>
parents: 1305
diff changeset
204 self.default_theme = C.TEMPLATE_THEME_DEFAULT
1258
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
205 self.site_themes = set()
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
206 self.named_pages = {}
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
207 self.browser_modules = {}
1256
08cd652dea14 server, pages, tasks (brython): common_scripts:
Goffi <goffi@goffi.org>
parents: 1253
diff changeset
208 # template dynamic data used in all pages
1282
0e4e413eb8db server: user new OrderedSet to handle scripts:
Goffi <goffi@goffi.org>
parents: 1275
diff changeset
209 self.dyn_data_common = {"scripts": OrderedSet()}
1258
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
210 for theme, data in host.renderer.getThemesData(site_name).items():
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
211 # we check themes for browser metadata, and merge them here if found
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
212 self.site_themes.add(theme)
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
213 browser_meta = data.get('browser_meta')
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
214 if browser_meta is not None:
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
215 log.debug(f"merging browser metadata from theme {theme}: {browser_meta}")
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
216 recursive_update(self.browser_modules, browser_meta)
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
217 browser_path = data.get('browser_path')
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
218 if browser_path is not None:
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
219 self.browser_modules.setdefault('themes_browser_paths', set()).add(
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
220 browser_path)
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
221 try:
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
222 next(browser_path.glob("*.py"))
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
223 except StopIteration:
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
224 pass
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
225 else:
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
226 log.debug(f"found brython script(s) for theme {theme}")
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
227 self.browser_modules.setdefault('brython', []).append(
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
228 {
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
229 "path": browser_path,
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
230 "url_hash": None,
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
231 "url_prefix": f"__t_{theme}"
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
232 }
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
233 )
92ff09cdd6dd server: gather themes data:
Goffi <goffi@goffi.org>
parents: 1257
diff changeset
234
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
235 self.uri_callbacks = {}
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
236 self.pages_redirects = {}
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
237 self.cached_urls = {}
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
238 self.main_menu = None
1460
0ea54090e414 server: renamed `sat-app` to `libervia-app` following global renaming
Goffi <goffi@goffi.org>
parents: 1459
diff changeset
239 # map Libervia application names => data
0ea54090e414 server: renamed `sat-app` to `libervia-app` following global renaming
Goffi <goffi@goffi.org>
parents: 1459
diff changeset
240 self.libervia_apps = {}
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
241 self.build_path = host.getBuildPath(site_name)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
242 self.build_path.mkdir(parents=True, exist_ok=True)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
243 self.dev_build_path = host.getBuildPath(site_name, dev=True)
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
244 self.dev_build_path.mkdir(parents=True, exist_ok=True)
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
245 self.putChild(
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
246 C.BUILD_DIR.encode(),
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
247 ProtectedFile(
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
248 self.build_path,
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
249 defaultType="application/octet-stream"),
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
250 )
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
251
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
252 def __str__(self):
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
253 return (
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
254 f"Root resource for {self.host_name or 'default host'} using "
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
255 f"{self.site_name or 'default site'} at {self.site_path} and deserving "
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
256 f"files at {self.path}"
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
257 )
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
258
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
259 def getConfig(self, key, default=None, value_type=None):
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
260 """Retrieve configuration for this site
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
261
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
262 params are the same as for [Libervia.getConfig]
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
263 """
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
264 return self.host.getConfig(self, key, default, value_type)
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
265
1293
de3b15d68bb6 pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
266 def getFrontURL(self, theme):
de3b15d68bb6 pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
267 return Path(
de3b15d68bb6 pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
268 '/',
de3b15d68bb6 pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
269 C.TPL_RESOURCE,
de3b15d68bb6 pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
270 self.site_name or C.SITE_NAME_DEFAULT,
de3b15d68bb6 pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
271 C.TEMPLATE_TPL_DIR,
de3b15d68bb6 pages: expose `templates_root_url` to scripts
Goffi <goffi@goffi.org>
parents: 1292
diff changeset
272 theme)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
273
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
274 def addResourceToPath(self, path: str, resource: web_resource.Resource) -> None:
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
275 """Add a resource to the given path
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
276
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
277 A "NoResource" will be used for all intermediate segments
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
278 """
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
279 segments, __, last_segment = path.rpartition("/")
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
280 url_segments = segments.split("/") if segments else []
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
281 current = self
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
282 for segment in url_segments:
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
283 resource = web_resource.NoResource()
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
284 current.putChild(segment, resource)
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
285 current = resource
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
286
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
287 current.putChild(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
288 last_segment.encode('utf-8'),
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
289 resource
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
290 )
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
291
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
292 async def _start_app(self, app_name, extra=None) -> dict:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
293 """Start a Libervia App
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
294
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
295 @param app_name: canonical application name
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
296 @param extra: extra parameter to configure app
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
297 @return: app data
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
298 app data will not include computed exposed data, at this needs to wait for the
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
299 app to be started
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
300 """
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
301 if extra is None:
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
302 extra = {}
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
303 log.info(_(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
304 "starting application {app_name}").format(app_name=app_name))
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
305 app_data = data_format.deserialise(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
306 await self.host.bridgeCall(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
307 "applicationStart", app_name, data_format.serialise(extra)
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
308 )
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
309 )
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
310 if app_data.get("started", False):
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
311 log.debug(f"application {app_name!r} is already started or starting")
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
312 # we do not await on purpose, the workflow should not be blocking at this
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
313 # point
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
314 defer.ensureDeferred(self._on_app_started(app_name, app_data["instance"]))
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
315 else:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
316 self.host.apps_cb[app_data["instance"]] = self._on_app_started
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
317 return app_data
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
318
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
319 async def _on_app_started(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
320 self,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
321 app_name: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
322 instance_id: str
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
323 ) -> None:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
324 exposed_data = self.libervia_apps[app_name] = data_format.deserialise(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
325 await self.host.bridgeCall("applicationExposedGet", app_name, "", "")
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
326 )
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
327
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
328 try:
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
329 web_port = int(exposed_data['ports']['web'].split(':')[1])
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
330 except (KeyError, ValueError):
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
331 log.warning(_(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
332 "no web port found for application {app_name!r}, can't use it "
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
333 ).format(app_name=app_name))
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
334 raise exceptions.DataError("no web port found")
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
335
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
336 try:
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
337 url_prefix = exposed_data['url_prefix'].strip().rstrip('/')
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
338 except (KeyError, AttributeError) as e:
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
339 log.warning(_(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
340 "no URL prefix specified for this application, we can't embed it: {msg}")
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
341 .format(msg=e))
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
342 raise exceptions.DataError("no URL prefix")
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
343
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
344 if not url_prefix.startswith('/'):
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
345 raise exceptions.DataError(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
346 f"invalid URL prefix, it must start with '/': {url_prefix!r}")
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
347
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
348 res = proxy.SatReverseProxyResource(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
349 "localhost",
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
350 web_port,
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
351 url_prefix.encode()
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
352 )
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
353 self.addResourceToPath(url_prefix, res)
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
354 log.info(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
355 f"Resource for app {app_name!r} (instance {instance_id!r}) has been added"
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
356 )
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
357
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
358 async def _initRedirections(self, options):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
359 url_redirections = options["url_redirections_dict"]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
360
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
361 url_redirections = url_redirections.get(self.site_name, {})
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
362
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
363 ## redirections
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
364 self.redirections = {}
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
365 self.inv_redirections = {} # new URL to old URL map
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
366
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
367 for old, new_data_list in url_redirections.items():
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
368 # several redirections can be used for one path by using a list.
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
369 # The redirection will be done using first item of the list, and all items
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
370 # will be used for inverse redirection.
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
371 # e.g. if a => [b, c], a will redirect to c, and b and c will both be
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
372 # equivalent to a
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
373 if not isinstance(new_data_list, list):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
374 new_data_list = [new_data_list]
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
375 for new_data in new_data_list:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
376 # new_data can be a dictionary or a unicode url
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
377 if isinstance(new_data, dict):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
378 # new_data dict must contain either "url", "page" or "path" key
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
379 # (exclusive)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
380 # if "path" is used, a file url is constructed with it
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
381 if ((
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
382 len(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
383 {"path", "url", "page"}.intersection(list(new_data.keys()))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
384 ) != 1
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
385 )):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
386 raise ValueError(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
387 'You must have one and only one of "url", "page" or "path" '
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
388 'key in your url_redirections_dict data'
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
389 )
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
390 if "url" in new_data:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
391 new = new_data["url"]
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
392 elif "page" in new_data:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
393 new = new_data
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
394 new["type"] = "page"
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
395 new.setdefault("path_args", [])
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
396 if not isinstance(new["path_args"], list):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
397 log.error(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
398 _('"path_args" in redirection of {old} must be a list. '
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
399 'Ignoring the redirection'.format(old=old)))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
400 continue
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
401 new.setdefault("query_args", {})
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
402 if not isinstance(new["query_args"], dict):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
403 log.error(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
404 _(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
405 '"query_args" in redirection of {old} must be a '
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
406 'dictionary. Ignoring the redirection'
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
407 ).format(old=old)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
408 )
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
409 continue
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
410 new["path_args"] = [quote(a) for a in new["path_args"]]
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
411 # we keep an inversed dict of page redirection
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
412 # (page/path_args => redirecting URL)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
413 # so getURL can return the redirecting URL if the same arguments
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
414 # are used # making the URL consistent
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
415 args_hash = tuple(new["path_args"])
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
416 self.pages_redirects.setdefault(new_data["page"], {}).setdefault(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
417 args_hash,
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
418 old
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
419 )
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
420
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
421 # we need lists in query_args because it will be used
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
422 # as it in request.path_args
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
423 for k, v in new["query_args"].items():
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
424 if isinstance(v, str):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
425 new["query_args"][k] = [v]
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
426 elif "path" in new_data:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
427 new = "file:{}".format(urllib.parse.quote(new_data["path"]))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
428 elif isinstance(new_data, str):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
429 new = new_data
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
430 new_data = {}
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
431 else:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
432 log.error(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
433 _("ignoring invalid redirection value: {new_data}").format(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
434 new_data=new_data
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
435 )
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
436 )
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
437 continue
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
438
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
439 # some normalization
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
440 if not old.strip():
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
441 # root URL special case
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
442 old = ""
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
443 elif not old.startswith("/"):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
444 log.error(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
445 _("redirected url must start with '/', got {value}. Ignoring")
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
446 .format(value=old)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
447 )
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
448 continue
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
449 else:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
450 old = self._normalizeURL(old)
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
451
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
452 if isinstance(new, dict):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
453 # dict are handled differently, they contain data
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
454 # which ared use dynamically when the request is done
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
455 self.redirections.setdefault(old, new)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
456 if not old:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
457 if new["type"] == "page":
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
458 log.info(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
459 _("Root URL redirected to page {name}").format(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
460 name=new["page"]
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
461 )
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
462 )
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
463 else:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
464 if new["type"] == "page":
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
465 page = self.getPageByName(new["page"])
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
466 url = page.getURL(*new.get("path_args", []))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
467 self.inv_redirections[url] = old
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
468 continue
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
469
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
470 # at this point we have a redirection URL in new, we can parse it
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
471 new_url = urllib.parse.urlsplit(new)
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
472
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
473 # we handle the known URL schemes
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
474 if new_url.scheme == "xmpp":
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
475 location = self.getPagePathFromURI(new)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
476 if location is None:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
477 log.warning(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
478 _("ignoring redirection, no page found to handle this URI: "
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
479 "{uri}").format(uri=new))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
480 continue
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
481 request_data = self._getRequestData(location)
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
482 self.inv_redirections[location] = old
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
483
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
484 elif new_url.scheme in ("", "http", "https"):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
485 # direct redirection
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
486 if new_url.netloc:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
487 raise NotImplementedError(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
488 "netloc ({netloc}) is not implemented yet for "
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
489 "url_redirections_dict, it is not possible to redirect to an "
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
490 "external website".format(netloc=new_url.netloc))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
491 location = urllib.parse.urlunsplit(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
492 ("", "", new_url.path, new_url.query, new_url.fragment)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
493 )
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
494 request_data = self._getRequestData(location)
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
495 self.inv_redirections[location] = old
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
496
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
497 elif new_url.scheme == "file":
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
498 # file or directory
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
499 if new_url.netloc:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
500 raise NotImplementedError(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
501 "netloc ({netloc}) is not implemented for url redirection to "
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
502 "file system, it is not possible to redirect to an external "
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
503 "host".format(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
504 netloc=new_url.netloc))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
505 path = urllib.parse.unquote(new_url.path)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
506 if not os.path.isabs(path):
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
507 raise ValueError(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
508 "file redirection must have an absolute path: e.g. "
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
509 "file:/path/to/my/file")
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
510 # for file redirection, we directly put child here
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
511 resource_class = (
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
512 ProtectedFile if new_data.get("protected", True) else static.File
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
513 )
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
514 res = resource_class(path, defaultType="application/octet-stream")
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
515 self.addResourceToPath(old, res)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
516 log.info("[{host_name}] Added redirection from /{old} to file system "
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
517 "path {path}".format(host_name=self.host_name,
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
518 old=old,
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
519 path=path))
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
520
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
521 # we don't want to use redirection system, so we continue here
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
522 continue
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
523
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
524 elif new_url.scheme == "libervia-app":
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
525 # a Libervia application
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
526
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
527 app_name = urllib.parse.unquote(new_url.path).lower().strip()
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
528 extra = {"url_prefix": f"/{old}"}
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
529 try:
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
530 await self._start_app(app_name, extra)
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
531 except Exception as e:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
532 log.warning(_(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
533 "Can't launch {app_name!r} for path /{old}: {e}").format(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
534 app_name=app_name, old=old, e=e))
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
535 continue
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
536
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
537 log.info(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
538 f"[{self.host_name}] Added redirection from /{old} to "
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
539 f"application {app_name}"
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
540 )
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
541 # normal redirection system is not used here
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
542 continue
1484
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
543 elif new_url.scheme == "proxy":
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
544 # a reverse proxy
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
545 host, port = new_url.hostname, new_url.port
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
546 if host is None or port is None:
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
547 raise ValueError(
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
548 "invalid host or port in proxy redirection, please check your "
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
549 "configuration: {new_url.geturl()}"
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
550 )
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
551 url_prefix = (new_url.path or old).rstrip('/')
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
552 res = proxy.SatReverseProxyResource(
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
553 host,
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
554 port,
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
555 url_prefix.encode(),
1454
fc91b78b71db server: handle "proxy" scheme in configuration
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
556 )
1484
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
557 self.addResourceToPath(old, res)
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
558 log.info(
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
559 f"[{self.host_name}] Added redirection from /{old} to reverse proxy "
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
560 f"{new_url.netloc} with URL prefix {url_prefix}/"
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
561 )
1454
fc91b78b71db server: handle "proxy" scheme in configuration
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
562
1484
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
563 # normal redirection system is not used here
6643855770a5 server: fix indentation following merge
Goffi <goffi@goffi.org>
parents: 1483
diff changeset
564 continue
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
565 else:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
566 raise NotImplementedError(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
567 "{scheme}: scheme is not managed for url_redirections_dict".format(
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
568 scheme=new_url.scheme
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
569 )
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
570 )
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
571
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
572 self.redirections.setdefault(old, request_data)
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
573 if not old:
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
574 log.info(_("[{host_name}] Root URL redirected to {uri}")
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
575 .format(host_name=self.host_name,
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
576 uri=request_data[1]))
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
577
875
d9b98b8a1847 server: handling of dict and "file:" urls in url_redirections_dict:
Goffi <goffi@goffi.org>
parents: 873
diff changeset
578 # the default root URL, if not redirected
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
579 if not "" in self.redirections:
1202
3f791fbc1643 server: if "redirections" is not set, use "/login" page as default
Goffi <goffi@goffi.org>
parents: 1199
diff changeset
580 self.redirections[""] = self._getRequestData(C.LIBERVIA_PAGE_START)
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
581
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
582 async def _setMenu(self, menus):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
583 menus = menus.get(self.site_name, [])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
584 main_menu = []
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
585 for menu in menus:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
586 if not menu:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
587 msg = _("menu item can't be empty")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
588 log.error(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
589 raise ValueError(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
590 elif isinstance(menu, list):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
591 if len(menu) != 2:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
592 msg = _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
593 "menu item as list must be in the form [page_name, absolue URL]"
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
594 )
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
595 log.error(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
596 raise ValueError(msg)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
597 page_name, url = menu
1460
0ea54090e414 server: renamed `sat-app` to `libervia-app` following global renaming
Goffi <goffi@goffi.org>
parents: 1459
diff changeset
598 elif menu.startswith("libervia-app:"):
1462
87e48b6a1bbd server: fix `libervia-app` name parsing following prefix change
Goffi <goffi@goffi.org>
parents: 1460
diff changeset
599 app_name = menu[13:].strip().lower()
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
600 app_data = await self._start_app(app_name)
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
601 exposed_data = app_data["expose"]
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
602 front_url = exposed_data['front_url']
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
603 options = self.host.options
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
604 url_redirections = options["url_redirections_dict"].setdefault(
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
605 self.site_name, {}
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
606 )
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
607 if front_url in url_redirections:
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
608 raise exceptions.ConflictError(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
609 f"There is already a redirection from {front_url!r}, can't add "
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
610 f"{app_name!r}")
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
611
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
612 url_redirections[front_url] = {
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
613 "page": 'embed_app',
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
614 "path_args": [app_name]
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
615 }
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
616
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
617 page_name = exposed_data.get('web_label', app_name).title()
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
618 url = front_url
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
619
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
620 log.debug(
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
621 f"Application {app_name} added to menu of {self.site_name}"
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
622 )
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
623 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
624 page_name = menu
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
625 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
626 url = self.getPageByName(page_name).url
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
627 except KeyError as e:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
628 log_msg = _("Can'find a named page ({msg}), please check "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
629 "menu_json in configuration.").format(msg=e.args[0])
1131
9cf592d1e6aa server: better error handling when a named page mentioned in menu_json is missing
Goffi <goffi@goffi.org>
parents: 1130
diff changeset
630 log.error(log_msg)
9cf592d1e6aa server: better error handling when a named page mentioned in menu_json is missing
Goffi <goffi@goffi.org>
parents: 1130
diff changeset
631 raise exceptions.ConfigError(log_msg)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
632 main_menu.append((page_name, url))
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
633 self.main_menu = main_menu
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
634
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
635 def _normalizeURL(self, url, lower=True):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
636 """Return URL normalized for self.redirections dict
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
637
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
638 @param url(unicode): URL to normalize
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
639 @param lower(bool): lower case of url if True
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
640 @return (str): normalized URL
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
641 """
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
642 if lower:
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
643 url = url.lower()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
644 return "/".join((p for p in url.split("/") if p))
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
645
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
646 def _getRequestData(self, uri):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
647 """Return data needed to redirect request
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
648
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
649 @param url(unicode): destination url
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
650 @return (tuple(list[str], str, str, dict): tuple with
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
651 splitted path as in Request.postpath
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
652 uri as in Request.uri
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
653 path as in Request.path
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
654 args as in Request.args
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
655 """
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
656 uri = uri
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
657 # XXX: we reuse code from twisted.web.http.py here
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
658 # as we need to have the same behaviour
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
659 x = uri.split("?", 1)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
660
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
661 if len(x) == 1:
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
662 path = uri
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
663 args = {}
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
664 else:
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
665 path, argstring = x
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
666 args = urllib.parse.parse_qs(argstring, True)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
667
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
668 # XXX: splitted path case must not be changed, as it may be significant
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
669 # (e.g. for blog items)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
670 return (
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
671 self._normalizeURL(path, lower=False).split("/"),
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
672 uri,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
673 path,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
674 args,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
675 )
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
676
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
677 def _redirect(self, request, request_data):
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
678 """Redirect an URL by rewritting request
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
679
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
680 this is *NOT* a HTTP redirection, but equivalent to URL rewritting
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
681 @param request(web.http.request): original request
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
682 @param request_data(tuple): data returned by self._getRequestData
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
683 @return (web_resource.Resource): resource to use
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
684 """
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
685 # recursion check
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
686 try:
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
687 request._redirected
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
688 except AttributeError:
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
689 pass
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
690 else:
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
691 try:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
692 __, uri, __, __ = request_data
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
693 except ValueError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
694 uri = ""
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
695 log.error(D_( "recursive redirection, please fix this URL:\n"
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
696 "{old} ==> {new}").format(
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
697 old=request.uri.decode("utf-8"), new=uri))
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
698 return web_resource.NoResource()
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
699
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
700 request._redirected = True # here to avoid recursive redirections
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
701
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
702 if isinstance(request_data, dict):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
703 if request_data["type"] == "page":
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
704 try:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
705 page = self.getPageByName(request_data["page"])
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
706 except KeyError:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
707 log.error(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
708 _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
709 'Can\'t find page named "{name}" requested in redirection'
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
710 ).format(name=request_data["page"])
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
711 )
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
712 return web_resource.NoResource()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
713 path_args = [pa.encode('utf-8') for pa in request_data["path_args"]]
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
714 request.postpath = path_args + request.postpath
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
715
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
716 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
717 request.args.update(request_data["query_args"])
980
bcacf970f970 core (pages redirection): inverted redirection + getSubPageURL:
Goffi <goffi@goffi.org>
parents: 979
diff changeset
718 except (TypeError, ValueError):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
719 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
720 _("Invalid args in redirection: {query_args}").format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
721 query_args=request_data["query_args"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
722 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
723 )
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
724 return web_resource.NoResource()
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
725 return page
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
726 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
727 raise exceptions.InternalError("unknown request_data type")
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
728 else:
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
729 path_list, uri, path, args = request_data
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
730 path_list = [p.encode('utf-8') for p in path_list]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
731 log.debug(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
732 "Redirecting URL {old} to {new}".format(
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
733 old=request.uri.decode('utf-8'), new=uri
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
734 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
735 )
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
736 # we change the request to reflect the new url
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
737 request.postpath = path_list[1:] + request.postpath
1158
fb4b4da61132 backend: fixed args transmission on page redirection.
Goffi <goffi@goffi.org>
parents: 1153
diff changeset
738 request.args.update(args)
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
739
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
740 # we start again to look for a child with the new url
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
741 return self.getChildWithDefault(path_list[0], request)
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
742
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
743 def getPageByName(self, name):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
744 """Retrieve page instance from its name
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
745
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
746 @param name(unicode): name of the page
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
747 @return (LiberviaPage): page instance
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
748 @raise KeyError: the page doesn't exist
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
749 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
750 return self.named_pages[name]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
751
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
752 def getPagePathFromURI(self, uri):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
753 """Retrieve page URL from xmpp: URI
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
754
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
755 @param uri(unicode): URI with a xmpp: scheme
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
756 @return (unicode,None): absolute path (starting from root "/") to page handling
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
757 the URI.
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
758 None is returned if no page has been registered for this URI
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
759 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
760 uri_data = common_uri.parseXMPPUri(uri)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
761 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
762 page, cb = self.uri_callbacks[uri_data["type"], uri_data["sub_type"]]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
763 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
764 url = None
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
765 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
766 url = cb(page, uri_data)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
767 if url is None:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
768 # no handler found
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
769 # we try to find a more generic one
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
770 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
771 page, cb = self.uri_callbacks[uri_data["type"], None]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
772 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
773 pass
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
774 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
775 url = cb(page, uri_data)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
776 return url
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
777
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
778 def getChildWithDefault(self, name, request):
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
779 # XXX: this method is overriden only for root url
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
780 # which is the only ones who need to be handled before other children
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
781 if name == b"" and not request.postpath:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
782 return self._redirect(request, self.redirections[""])
862
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
783 return super(LiberviaRootResource, self).getChildWithDefault(name, request)
e3e2effc9a4c server: LiberviaRootResource now manages root url redirection, and former redirection has been replaced by it:
Goffi <goffi@goffi.org>
parents: 859
diff changeset
784
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
785 def getChild(self, name, request):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
786 resource = super(LiberviaRootResource, self).getChild(name, request)
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
787
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
788 if isinstance(resource, web_resource.NoResource):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
789 # if nothing was found, we try our luck with redirections
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
790 # XXX: we want redirections to happen only if everything else failed
1047
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
791 path_elt = request.prepath + request.postpath
1457
792a2e902ee9 server: fix inverse URL redirection for root path + allow multiple inverse redirections:
Goffi <goffi@goffi.org>
parents: 1435
diff changeset
792 for idx in range(len(path_elt), -1, -1):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
793 test_url = b"/".join(path_elt[:idx]).decode('utf-8').lower()
1047
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
794 if test_url in self.redirections:
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
795 request_data = self.redirections[test_url]
3f6f4d907c30 server: better redirection (fixed issue in remaining path arguments)
Goffi <goffi@goffi.org>
parents: 1046
diff changeset
796 request.postpath = path_elt[idx:]
979
1d558dfb32ca server: pages redirection:
Goffi <goffi@goffi.org>
parents: 978
diff changeset
797 return self._redirect(request, request_data)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
798
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
799 return resource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
800
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
801 def putChild(self, path, resource):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
802 """Add a child to the root resource"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
803 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
804 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
805 if not isinstance(resource, web_resource.EncodingResourceWrapper):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
806 # FIXME: check that no information is leaked (c.f. https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#request-encoders)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
807 resource = web_resource.EncodingResourceWrapper(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
808 resource, [server.GzipEncoderFactory()])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
809
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
810 super(LiberviaRootResource, self).putChild(path, resource)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
811
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
812 def createSimilarFile(self, path):
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
813 # XXX: this method need to be overriden to avoid recreating a LiberviaRootResource
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
814
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
815 f = LiberviaRootResource.__base__(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
816 path, self.defaultType, self.ignoredExts, self.registry
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
817 )
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
818 # refactoring by steps, here - constructor should almost certainly take these
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
819 f.processors = self.processors
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
820 f.indexNames = self.indexNames[:]
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
821 f.childNotFound = self.childNotFound
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
822 return f
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
823
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
824
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
825 class WaitingRequests(dict):
796
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
826 def setRequest(self, request, profile, register_with_ext_jid=False):
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
827 """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
828
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
829 @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
830 @param profile (str): %(doc_profile)s
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
831 @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
832 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
833 """
555
db1b330c1eb1 server_side: set the connection attempt timeout to the bridge timeout (2 minutes)
souliane <souliane@mailoo.org>
parents: 554
diff changeset
834 dc = reactor.callLater(BRIDGE_TIMEOUT, self.purgeRequest, profile)
796
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
835 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
836
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
837 def purgeRequest(self, profile):
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
838 """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
839
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
840 @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
841 """
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
842 try:
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
843 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
844 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
845 return
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
846 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
847 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
848 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
849
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
850 def getRequest(self, profile):
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
851 """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
852
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
853 @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
854 @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
855 """
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
856 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
857
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
858 def getRegisterWithExtJid(self, profile):
fad9c9f82ae3 browser and server sides: alert the user after he created a new profile using his XMPP account credentials
souliane <souliane@mailoo.org>
parents: 794
diff changeset
859 """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
860
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
861 @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
862 @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
863 """
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
864 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
865
554
471b6babe960 server_side: set a timeout to reset the waiting connection requests after 5 minutes
souliane <souliane@mailoo.org>
parents: 553
diff changeset
866
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
867 class Libervia(service.Service):
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
868 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
869
810
3905bc24eb17 server: proper options handling
Goffi <goffi@goffi.org>
parents: 801
diff changeset
870 def __init__(self, options):
3905bc24eb17 server: proper options handling
Goffi <goffi@goffi.org>
parents: 801
diff changeset
871 self.options = options
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
872 websockets.host = self
1236
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
873
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
874 def _init(self):
352e189ab7f8 server: avoid double init
Goffi <goffi@goffi.org>
parents: 1235
diff changeset
875 # 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
876 # 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
877 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
878 self.waiting_profiles = WaitingRequests() # FIXME: should be removed
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
879 self._main_conf = None
1152
1c23252958ed server: iNotify support:
Goffi <goffi@goffi.org>
parents: 1151
diff changeset
880 self.files_watcher = FilesWatcher(self)
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
881
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
882 if self.options["base_url_ext"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
883 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
884 if self.base_url_ext[-1] != "/":
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
885 self.base_url_ext += "/"
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
886 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
887 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
888 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
889 # 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
890 # scheme = self.base_url_ext_data.scheme or 'https'
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
891 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
892
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
893 if not self.options["port_https_ext"]:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
894 self.options["port_https_ext"] = self.options["port_https"]
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
895
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
896 self._cleanup = []
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
897
451
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
898 self.sessions = {} # key = session value = user
1a0cec9b0f1e better PEP-8 compliance
souliane <souliane@mailoo.org>
parents: 449
diff changeset
899 self.prof_connected = set() # Profiles connected
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
900 self.ns_map = {} # map of short name to namespaces
470
34ce41e014c4 server side: options managing improvments:
Goffi <goffi@goffi.org>
parents: 465
diff changeset
901
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
902 ## bridge ##
1367
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
903 self._bridge_retry = self.options['bridge-retries']
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
904 self.bridge = Bridge()
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
905 self.bridge.bridgeConnect(callback=self._bridgeCb, errback=self._bridgeEb)
553
8492c2bb463b server_side: enable HTTP gzip compression
souliane <souliane@mailoo.org>
parents: 522
diff changeset
906
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
907 ## libervia app callbacks ##
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
908 # 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
909 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
910
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
911 @property
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
912 def roots(self):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
913 """Return available virtual host roots
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
914
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
915 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
916 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
917 returned first.
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
918 @return (list[web_resource.Resource]): all vhost root resources
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
919 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
920 roots = list(set(self.vhost_root.hosts.values()))
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
921 default = self.vhost_root.default
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
922 if default is not None and default not in roots:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
923 roots.insert(0, default)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
924 return roots
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
925
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
926 @property
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
927 def main_conf(self):
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
928 """SafeConfigParser instance opened on configuration file (sat.conf)"""
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
929 if self._main_conf is None:
1240
14c0f666a93d server: log config files names
Goffi <goffi@goffi.org>
parents: 1237
diff changeset
930 self._main_conf = config.parseMainConf(log_filenames=True)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
931 return self._main_conf
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
932
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
933 def getConfig(self, site_root_res, key, default=None, value_type=None):
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
934 """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
935
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
936 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
937 @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
938 @param key(unicode): key to use
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
939 @param default: value to use if not found (see [config.getConfig])
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
940 @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
941 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
942 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
943 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
944 - "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
945
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
946 """
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
947 section = site_root_res.site_name.lower().strip() or C.CONFIG_SECTION
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
948 value = config.getConfig(self.main_conf, section, key, default=default)
02afab1b15c5 server, pages, tasks: moved getConfig to backend, and added shorcut version in LiberviaPage and TasksManager
Goffi <goffi@goffi.org>
parents: 1146
diff changeset
949 if value_type is not None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
950 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
951 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
952 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
953 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
954 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
955 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
956 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
957 elif isinstance(value, dict):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
958 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
959 elif value is not None:
1217
fe9782391f63 server: fixed value filtering in getConfig
Goffi <goffi@goffi.org>
parents: 1216
diff changeset
960 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
961 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
962
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
963 def _namespacesGetCb(self, ns_map):
1383
81b472bcf0a1 server: be sure to have strings for ns_map
Goffi <goffi@goffi.org>
parents: 1370
diff changeset
964 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
965
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
966 def _namespacesGetEb(self, failure_):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
967 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
968
1130
8a7d75c18d40 server: fixed front_url_filter in templates renderer
Goffi <goffi@goffi.org>
parents: 1129
diff changeset
969 @template.contextfilter
1127
9234f29053b0 server, pages: update to handle multi sites themes, first draft:
Goffi <goffi@goffi.org>
parents: 1126
diff changeset
970 def _front_url_filter(self, ctx, relative_url):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
971 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
972 return os.path.join(
e30e96958fb7 server: use C.SITE_NAME_DEFAULT instead of "sat" when suitable
Goffi <goffi@goffi.org>
parents: 1287
diff changeset
973 '/', 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
974 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
975
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
976 def _moveFirstLevelToDict(self, options, key, keys_to_keep):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
977 """Read a config option and put value at first level into u'' dict
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
978
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
979 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
980 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
981 options will be modified in place
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
982 @param options(dict): options to modify
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
983 @param key(unicode): setting key to modify
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
984 @param keys_to_keep(list(unicode)): keys allowed in first level
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
985 """
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
986 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
987 conf = options[key]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
988 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
989 return
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
990 if not isinstance(conf, dict):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
991 options[key] = {'': conf}
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
992 return
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
993 default_dict = conf.get('', {})
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
994 to_delete = []
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
995 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
996 if key not in keys_to_keep:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
997 default_dict[key] = value
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
998 to_delete.append(key)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
999 for key in to_delete:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1000 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
1001 if default_dict:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1002 conf[''] = default_dict
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1003
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1004 async def checkAndConnectServiceProfile(self):
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1005 passphrase = self.options["passphrase"]
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1006 if not passphrase:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1007 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1008 C.EXIT_BAD_ARG,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1009 _("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
1010 "documentation.")
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1011 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1012 try:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1013 s_prof_connected = await self.bridgeCall("isConnected", C.SERVICE_PROFILE)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1014 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1015 if e.classname == "ProfileUnknownError":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1016 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
1017 try:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1018 xmpp_domain = await self.bridgeCall("getConfig", "", "xmpp_domain")
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1019 xmpp_domain = xmpp_domain.strip()
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1020 if not xmpp_domain:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1021 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1022 C.EXIT_BAD_ARG,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1023 _('"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
1024 'check documentation')
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1025 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1026 service_profile_jid_s = f"{C.SERVICE_PROFILE}@{xmpp_domain}"
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1027 await self.bridgeCall(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1028 "inBandAccountNew",
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1029 service_profile_jid_s,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1030 passphrase,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1031 "",
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1032 xmpp_domain,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1033 0,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1034 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1035 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1036 if e.condition == "conflict":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1037 log.info(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1038 _("Service's profile JID {profile_jid} already exists")
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1039 .format(profile_jid=service_profile_jid_s)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1040 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1041 elif e.classname == "UnknownMethod":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1042 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1043 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1044 _("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
1045 "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
1046 "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
1047 "manually.").format(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1048 profile=C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1049 profile_jid=service_profile_jid_s)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1050 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1051 elif e.condition == "service-unavailable":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1052 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1053 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1054 _("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
1055 "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
1056 "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
1057 "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
1058 "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
1059 "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
1060 "documentation.").format(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1061 profile=C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1062 profile_jid=service_profile_jid_s,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1063 app_name=C.APP_NAME)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1064 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1065 elif e.condition == "not-acceptable":
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1066 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1067 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1068 _("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
1069 "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
1070 "In-Band Registration please check XMPP server "
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1071 "configuration: {reason}"
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1072 ).format(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1073 profile=C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1074 profile_jid=service_profile_jid_s,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1075 reason=e.message)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1076 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1077
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1078 else:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1079 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1080 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1081 _("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
1082 "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
1083 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1084 try:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1085 await self.bridgeCall("profileCreate", C.SERVICE_PROFILE, passphrase)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1086 await self.bridgeCall(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1087 "profileStartSession", passphrase, C.SERVICE_PROFILE)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1088 await self.bridgeCall(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1089 "setParam", "JabberID", service_profile_jid_s, "Connection", -1,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1090 C.SERVICE_PROFILE)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1091 await self.bridgeCall(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1092 "setParam", "Password", passphrase, "Connection", -1,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1093 C.SERVICE_PROFILE)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1094 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1095 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1096 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1097 _("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
1098 "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
1099 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1100 log.info(_("Service profile has been successfully created"))
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1101 s_prof_connected = False
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1102 else:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1103 raise SysExit(C.EXIT_BRIDGE_ERROR, e.message)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1104
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1105 if not s_prof_connected:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1106 try:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1107 await self.bridgeCall(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1108 "connect",
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1109 C.SERVICE_PROFILE,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1110 passphrase,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1111 {},
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1112 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1113 except BridgeException as e:
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1114 raise SysExit(
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1115 C.EXIT_BRIDGE_ERROR,
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1116 _("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
1117 )
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1118
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1119 async def backendReady(self):
1397
ed037818d6de core (constants): renaming following global project renaming
Goffi <goffi@goffi.org>
parents: 1396
diff changeset
1120 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
1121
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1122 # settings
1364
df40708c4c76 server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents: 1362
diff changeset
1123 if self.options['dev-mode']:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1124 log.info(_("Developer mode activated"))
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1125 self.media_dir = await self.bridgeCall("getConfig", "", "media_dir")
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1126 self.local_dir = await self.bridgeCall("getConfig", "", "local_dir")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1127 self.cache_root_dir = os.path.join(self.local_dir, C.CACHE_DIR)
1132
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
1128 self.renderer = template.Renderer(self, self._front_url_filter)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1129 sites_names = list(self.renderer.sites_paths.keys())
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1130
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
1131 self._moveFirstLevelToDict(self.options, "url_redirections_dict", sites_names)
0cafb79ced6d server: use site names in _moveFirstLevelToDict to better distinguish values for default site at first level + better handling of default menu
Goffi <goffi@goffi.org>
parents: 1131
diff changeset
1132 self._moveFirstLevelToDict(self.options, "menu_json", sites_names)
1359
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
1133 self._moveFirstLevelToDict(self.options, "menu_extra_json", sites_names)
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
1134 menu = self.options["menu_json"]
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
1135 if not '' in menu:
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
1136 menu[''] = C.DEFAULT_MENU
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
1137 for site, value in self.options["menu_extra_json"].items():
2da573bf3f8b server: new `menu_extra_json` configuration option:
Goffi <goffi@goffi.org>
parents: 1354
diff changeset
1138 menu[site].extend(value)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1139
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1140 # service profile
1370
2d6cfce0df6c server: typo
Goffi <goffi@goffi.org>
parents: 1369
diff changeset
1141 if not self.options['build-only']:
1369
b5f2565c4bac server: don't check or connect service profile when `--build-only` is set
Goffi <goffi@goffi.org>
parents: 1368
diff changeset
1142 await self.checkAndConnectServiceProfile()
1362
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1143
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1144 # restricted bridge, the one used by browser code
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1145 self.restricted_bridge = RestrictedBridge(self)
45ebeea1bacd server: improved service profile check + auto creation:
Goffi <goffi@goffi.org>
parents: 1361
diff changeset
1146
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1147 # we create virtual hosts and import Libervia pages into them
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1148 self.vhost_root = vhost.NameVirtualHost()
1253
6d49fae517ba pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents: 1251
diff changeset
1149 default_site_path = Path(libervia.__file__).parent.resolve()
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1150 # self.sat_root is official Libervia site
1253
6d49fae517ba pages: browser metadata + root `_browser`:
Goffi <goffi@goffi.org>
parents: 1251
diff changeset
1151 root_path = default_site_path / C.TEMPLATE_STATIC_DIR
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1152 self.sat_root = default_root = LiberviaRootResource(
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1153 host=self, host_name='', site_name='',
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1154 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
1155 if self.options['dev-mode']:
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1156 self.files_watcher.watchDir(
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1157 default_site_path, auto_add=True, recursive=True,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1158 callback=LiberviaPage.onFileChange, site_root=self.sat_root,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1159 site_path=default_site_path)
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1160 LiberviaPage.importPages(self, self.sat_root)
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1161 tasks_manager = TasksManager(self, self.sat_root)
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1162 await tasks_manager.parseTasks()
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1163 await tasks_manager.runTasks()
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1164 # FIXME: handle _setMenu in a more generic way, taking care of external sites
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
1165 await self.sat_root._setMenu(self.options["menu_json"])
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1166 self.vhost_root.default = default_root
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1167 existing_vhosts = {b'': default_root}
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1168
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1169 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
1170 if site_name == C.SITE_NAME_DEFAULT:
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1171 raise ValueError(
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1172 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
1173 encoded_site_name = site_name.encode('utf-8')
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1174 try:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1175 site_path = self.renderer.sites_paths[site_name]
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1176 except KeyError:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1177 log.warning(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1178 "host {host_name} link to non existing site {site_name}, ignoring "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1179 "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
1180 continue
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1181 if encoded_site_name in existing_vhosts:
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1182 # we have an alias host, we re-use existing resource
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1183 res = existing_vhosts[encoded_site_name]
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1184 else:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1185 # 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
1186 # if not, we use default template's static dir
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1187 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
1188 if not os.path.isdir(root_path):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1189 root_path = os.path.join(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1190 site_path, C.TEMPLATE_TPL_DIR, C.TEMPLATE_THEME_DEFAULT,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1191 C.TEMPLATE_STATIC_DIR)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1192 res = LiberviaRootResource(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1193 host=self,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1194 host_name=host_name,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1195 site_name=site_name,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1196 site_path=site_path,
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1197 path=root_path)
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1198
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1199 existing_vhosts[encoded_site_name] = res
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1200
1364
df40708c4c76 server: renamed `--dev_mode` to `--dev-mode` and set it as a flag:
Goffi <goffi@goffi.org>
parents: 1362
diff changeset
1201 if self.options['dev-mode']:
1153
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1202 self.files_watcher.watchDir(
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1203 site_path, auto_add=True, recursive=True,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1204 callback=LiberviaPage.onFileChange, site_root=res,
94f9d81a475e pages: auto reloading
Goffi <goffi@goffi.org>
parents: 1152
diff changeset
1205 site_path=site_path)
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1206
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1207 LiberviaPage.importPages(self, res)
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1208 # FIXME: default pages are accessible if not overriden by external website
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1209 # while necessary for login or re-using existing pages
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1210 # 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
1211 # (e.g. /blog disabled except if called by external site)
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1212 LiberviaPage.importPages(self, res, root_path=default_site_path)
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1213 tasks_manager = TasksManager(self, res)
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1214 await tasks_manager.parseTasks()
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1215 await tasks_manager.runTasks()
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
1216 await res._setMenu(self.options["menu_json"])
1191
ba45dcdbeeaf server: fixed sites alias:
Goffi <goffi@goffi.org>
parents: 1184
diff changeset
1217
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1218 self.vhost_root.addHost(host_name.encode('utf-8'), res)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1219
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1220 templates_res = web_resource.Resource()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1221 self.putChildAll(C.TPL_RESOURCE.encode('utf-8'), templates_res)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1222 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
1223 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
1224 static.File(site_path))
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1225
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1226 d = self.bridgeCall("namespacesGet")
985
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
1227 d.addCallback(self._namespacesGetCb)
64826e69f365 pages: cache mechanism, first draft:
Goffi <goffi@goffi.org>
parents: 984
diff changeset
1228 d.addErrback(self._namespacesGetEb)
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1229
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1230 # websocket
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1231 if self.options["connection_type"] in ("https", "both"):
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1232 wss = websockets.LiberviaPageWSProtocol.getResource(secure=True)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1233 self.putChildAll(b'wss', wss)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1234 if self.options["connection_type"] in ("http", "both"):
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1235 ws = websockets.LiberviaPageWSProtocol.getResource(secure=False)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1236 self.putChildAll(b'ws', ws)
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1237
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1238 ## 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
1239 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1240 "psEventRaw", partial(LiberviaPage.onNodeEvent, self), "plugin"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1241 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1242 self.bridge.register_signal(
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1243 "messageNew", partial(self.on_signal, "messageNew")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1244 )
917
86563d6c83b0 server: Libervia pages:
Goffi <goffi@goffi.org>
parents: 915
diff changeset
1245
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1246 # libervia applications handling
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1247 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
1248 "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
1249 )
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1250 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
1251 "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
1252 )
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1253
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1254 #  Progress handling
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1255 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1256 "progressStarted", partial(ProgressHandler._signal, "started")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1257 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1258 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1259 "progressFinished", partial(ProgressHandler._signal, "finished")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1260 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1261 self.bridge.register_signal(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1262 "progressError", partial(ProgressHandler._signal, "error")
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1263 )
1063
4b69f69c6ffd server: new ProgressHandler helper class, to handle progressing actions
Goffi <goffi@goffi.org>
parents: 1055
diff changeset
1264
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1265 # media dirs
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1266 # FIXME: get rid of dirname and "/" in C.XXX_DIR
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1267 self.putChildAll(os.path.dirname(C.MEDIA_DIR).encode('utf-8'),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1268 ProtectedFile(self.media_dir))
1242
8f39d98416c5 server: added a child for common cache
Goffi <goffi@goffi.org>
parents: 1240
diff changeset
1269
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1270 self.cache_resource = web_resource.NoResource()
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1271 self.putChildAll(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
1272 self.cache_resource.putChild(
8f39d98416c5 server: added a child for common cache
Goffi <goffi@goffi.org>
parents: 1240
diff changeset
1273 b"common", ProtectedFile(str(self.cache_root_dir / Path("common"))))
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1274
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1275 # redirections
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1276 for root in self.roots:
1360
389a83eefe62 server: SàT applications integration:
Goffi <goffi@goffi.org>
parents: 1359
diff changeset
1277 await root._initRedirections(self.options)
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1278
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1279 # 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
1280 del self.options["url_redirections_dict"]
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1281
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1282 server.Request.defaultContentType = "text/html; charset=utf-8"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1283 wrapped = web_resource.EncodingResourceWrapper(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1284 self.vhost_root, [server.GzipEncoderFactory()]
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1285 )
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1286 self.site = server.Site(wrapped)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1287 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
1288
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1289 def _bridgeCb(self):
1367
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1290 del self._bridge_retry
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1291 self.bridge.getReady(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1292 lambda: self.initialised.callback(None),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1293 lambda failure: self.initialised.errback(Exception(failure)),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1294 )
1245
079e8eb6e327 server (tasks): refactoring:
Goffi <goffi@goffi.org>
parents: 1242
diff changeset
1295 self.initialised.addCallback(lambda __: defer.ensureDeferred(self.backendReady()))
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
1296
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
1297 def _bridgeEb(self, failure_):
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
1298 if isinstance(failure_, BridgeExceptionNoService):
1367
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1299 if self._bridge_retry:
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1300 if self._bridge_retry < 0:
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1301 print(_("Can't connect to bridge, will retry indefinitely. "
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1302 "Next try in 1s."))
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1303 else:
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1304 self._bridge_retry -= 1
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1305 print(
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1306 _(
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1307 "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
1308 "trie(s) left)."
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1309 ).format(retries_left=self._bridge_retry)
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1310 )
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1311 time.sleep(1)
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1312 self.bridge.bridgeConnect(callback=self._bridgeCb, errback=self._bridgeEb)
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1313 return
ce5cfe004d4e server: new `--bridge-retries` option:
Goffi <goffi@goffi.org>
parents: 1365
diff changeset
1314
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1315 print("Can't connect to SàT backend, are you sure it's launched ?")
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
1316 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1317 log.error("Can't connect to bridge: {}".format(failure))
1126
7cd89277a129 server: fixed missing backend detection
Goffi <goffi@goffi.org>
parents: 1124
diff changeset
1318 sys.exit(1)
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1319
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1320 @property
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1321 def version(self):
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1322 """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
1323 return C.APP_VERSION
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1324
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1325 @property
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1326 def full_version(self):
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1327 """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
1328 version = self.version
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1329 if version[-1] == "D":
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1330 # 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
1331 try:
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1332 return self._version_cache
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1333 except AttributeError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1334 self._version_cache = "{} ({})".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1335 version, utils.getRepositoryData(libervia)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1336 )
682
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1337 return self._version_cache
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1338 else:
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1339 return version
e6bb64bd6b4d server side: implemented methods to get SàT and Libervia versions
Goffi <goffi@goffi.org>
parents: 679
diff changeset
1340
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
1341 def bridgeCall(self, method_name, *args, **kwargs):
c3ae008f7a0f server: move asyncBridgeCall to Libervia.bridgeCall, this way async bridge calls can be used easily everywhere.
Goffi <goffi@goffi.org>
parents: 931
diff changeset
1342 """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
1343
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
1344 @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
1345 @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
1346
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
1347 """
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
1348 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
1349
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
1350 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
1351 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
1352 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
1353 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
1354 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
1355 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
1356 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
1357
1138
ef565839dada server: don't convert failure in errback to jsonrpclib.Fault anymore:
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
1358 def _errback(failure_):
ef565839dada server: don't convert failure in errback to jsonrpclib.Fault anymore:
Goffi <goffi@goffi.org>
parents: 1132
diff changeset
1359 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
1360
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
1361 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
1362 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
1363 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
1364 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
1365
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1366 def on_signal(self, signal_name, *args):
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1367 profile = args[-1]
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1368 if not profile:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1369 log.error(f"got signal without profile: {signal_name}, {args}")
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1370 return
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1371 session_iface.WebSession.send(
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1372 profile,
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1373 "bridge",
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1374 {"signal": signal_name, "args": args}
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1375 )
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1376
1505
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1377 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
1378 self,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1379 name: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1380 instance_id: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1381 extra_s: str
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1382 ) -> None:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1383 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
1384 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
1385 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
1386
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1387 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
1388 self,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1389 name: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1390 instance_id: str,
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1391 extra_s: str
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1392 ) -> None:
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1393 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
1394 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
1395 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
1396 log.error(
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1397 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
1398 )
a169cbc315f0 server: don't wait anymore for libervia app to be fully started:
Goffi <goffi@goffi.org>
parents: 1504
diff changeset
1399
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1258
diff changeset
1400 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
1401 """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
1402
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1403 @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
1404 @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
1405 @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
1406 - 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
1407 - 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
1408 @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
1409 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1410 register_with_ext_jid = self.waiting_profiles.getRegisterWithExtJid(profile)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1411 self.waiting_profiles.purgeRequest(profile)
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1412 session = request.getSession()
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1413 web_session = session_iface.IWebSession(session)
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1414 if web_session.profile:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1415 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
1416 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
1417
1305
db9ea167c409 pages: `profile` is now exposed to scripts
Goffi <goffi@goffi.org>
parents: 1293
diff changeset
1418 # 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
1419 # which may cause trouble when exposing it to scripts
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1420 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
1421 self.prof_connected.add(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1422 cache_dir = os.path.join(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1423 self.cache_root_dir, "profiles", regex.pathEscape(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1424 )
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1425 # 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
1426 # profile's cache directory, without uuid
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1427 self.cache_resource.putChild(web_session.uuid.encode('utf-8'),
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1428 ProtectedFile(cache_dir))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1429 log.debug(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1430 _("profile cache resource added from {uuid} to {path}").format(
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1431 uuid=web_session.uuid, path=cache_dir
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1432 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1433 )
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
1434
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1435 def on_expire():
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1436 log.info("Session expired (profile={profile})".format(profile=profile))
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1437 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
1438 log.debug(
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1439 _("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
1440 )
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1441 web_session.on_expire()
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1442 if web_session.ws_socket is not None:
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1443 web_session.ws_socket.close()
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1444 # and now we disconnect the profile
1098
01e95ec9df9e server, pages: fixed blocking calls to bridge by using bridgeCall instead
Goffi <goffi@goffi.org>
parents: 1093
diff changeset
1445 self.bridgeCall("disconnect", profile)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1446
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1447 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
1448
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1449 # FIXME: those session infos should be returned by connect or isConnected
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1258
diff changeset
1450 infos = await self.bridgeCall("sessionInfosGet", profile)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1451 web_session.jid = jid.JID(infos["jid"])
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1452 own_bare_jid_s = web_session.jid.userhost()
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1258
diff changeset
1453 own_id_raw = await self.bridgeCall(
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1258
diff changeset
1454 "identityGet", own_bare_jid_s, [], True, profile)
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1455 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
1456 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
1457
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1458 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
1459 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
1460
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1461 @defer.inlineCallbacks
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1462 def connect(self, request, login, password):
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1463 """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
1464
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1465 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
1466 @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
1467 @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
1468 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
1469 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
1470 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
1471 @param password(unicode): user password
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1472 @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
1473 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
1474 @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
1475 @raise exceptions.ProfileUnknownError: this login doesn't exist
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1476 @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
1477 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
1478 @raise exceptions.NotReady: a profile connection is already waiting
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1479 @raise exceptions.TimeoutError: didn't received and answer from Bridge
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1480 @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
1481 @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
1482 @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
1483 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1484
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1485 # 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
1486 if login.startswith("@"):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1487 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
1488
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1489 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
1490 log.debug("logging a guest account")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1491 elif "@" in login:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1492 if login.count("@") != 1:
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1493 raise failure.Failure(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1494 exceptions.DataError("Invalid login: {login}".format(login=login))
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1495 )
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
1496 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
1497 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
1498 except (RuntimeError, jid.InvalidFormat, AttributeError):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1499 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
1500
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1501 # FIXME: should it be cached?
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1502 new_account_domain = yield self.bridgeCall("getNewAccountDomain")
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1503
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1504 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
1505 # 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
1506 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
1507 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
1508 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
1509 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
1510
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1511 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
1512 profile = yield self.bridgeCall("profileNameGet", login)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1513 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
1514 # 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
1515 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1516 login_jid is not None and login_jid.user
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1517 ): # try to create a new sat profile using the XMPP credentials
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1518 if not self.options["allow_registration"]:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1519 log.warning(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1520 "Trying to register JID account while registration is not "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1521 "allowed")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1522 raise failure.Failure(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1523 exceptions.DataError(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1524 "JID login while registration is not allowed"
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1525 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1526 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1527 profile = login # FIXME: what if there is a resource?
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1528 connect_method = "asyncConnectWithXMPPCredentials"
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1529 register_with_ext_jid = True
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1530 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
1531 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
1532 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1533 if profile != login or (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1534 not password
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1535 and profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1536 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
1537 ):
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
1538 # 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
1539 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
1540 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
1541
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1542 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
1543
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1544 # we check if there is not already an active session
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1545 web_session = session_iface.IWebSession(request.getSession())
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1546 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
1547 # yes, there is
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1548 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
1549 # it's a different profile, we need to disconnect it
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1550 log.warning(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1551 "{new_profile} requested login, but {old_profile} was already "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1552 "connected, disconnecting {old_profile}").format(
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1553 old_profile=web_session.profile, new_profile=profile))
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1554 self.purgeSession(request)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1555
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1556 if self.waiting_profiles.getRequest(profile):
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1557 #  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
1558 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
1559
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1560 self.waiting_profiles.setRequest(request, profile, register_with_ext_jid)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1561 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
1562 connected = yield self.bridgeCall(connect_method, profile, password)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1563 except Exception as failure_:
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
1564 fault = getattr(failure_, 'classname', None)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1565 self.waiting_profiles.purgeRequest(profile)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1566 if fault in ("PasswordError", "ProfileUnknownError"):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1567 log.info("Profile {profile} doesn't exist or the submitted password is "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1568 "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
1569 raise failure.Failure(ValueError(C.PROFILE_AUTH_ERROR))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1570 elif fault == "SASLAuthError":
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1571 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
1572 .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
1573 raise failure.Failure(ValueError(C.XMPP_AUTH_ERROR))
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1574 elif fault == "NoReply":
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1575 log.info(_("Did not receive a reply (the timeout expired or the "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1576 "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
1577 raise exceptions.TimeOutError
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
1578 elif fault is None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1579 log.info(_("Unexepected failure: {failure_}").format(failure_=failure))
1204
a2df53dfbf46 server: handle unexpected exceptions on connect:
Goffi <goffi@goffi.org>
parents: 1203
diff changeset
1580 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
1581 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1582 log.error('Unmanaged fault class "{fault}" in errback for the '
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1583 'connection of profile {profile}'.format(
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1584 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
1585 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
1586
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1587 if connected:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1588 #  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
1589 # do we have a corresponding session in Libervia?
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1590 web_session = session_iface.IWebSession(request.getSession())
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1591 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
1592 # yes, session is active
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1593 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
1594 # 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
1595 # so this line should never be reached
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1596 log.error(_(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1597 "session profile [{session_profile}] differs from login "
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1598 "profile [{profile}], this should not happen!")
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1599 .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
1600 raise exceptions.InternalError("profile mismatch")
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1601 defer.returnValue(C.SESSION_ACTIVE)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1602 log.info(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1603 _(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1604 "profile {profile} was already connected in backend".format(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1605 profile=profile
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1606 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1607 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1608 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1609 #  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
1610
1266
6b7f9c3558cc server, pages: better identities handling:
Goffi <goffi@goffi.org>
parents: 1258
diff changeset
1611 state = yield defer.ensureDeferred(self._logged(profile, request))
1090
9c41b7e91172 server: set jid and started time in session on log-in:
Goffi <goffi@goffi.org>
parents: 1066
diff changeset
1612 defer.returnValue(state)
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1613
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1614 def registerNewAccount(self, request, login, password, email):
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1615 """Create a new account, or return error
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1616 @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
1617 @param login(unicode): new account requested login
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1618 @param email(unicode): new account email
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1619 @param password(unicode): new account password
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1620 @return(unicode): a constant indicating the state:
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1621 - 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
1622 - 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
1623 - 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
1624 - 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
1625 - 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
1626 @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
1627 """
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1628 if not self.options["allow_registration"]:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1629 log.warning(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1630 _("Registration received while it is not allowed, hack attempt?")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1631 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1632 raise failure.Failure(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1633 exceptions.PermissionError("Registration is not allowed on this server")
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1634 )
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
1635
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1636 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1637 not re.match(C.REG_LOGIN_RE, login)
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1638 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
1639 or len(password) < C.PASSWORD_MIN_LENGTH
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1640 ):
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
1641 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
1642
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1643 def registered(result):
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1644 return C.REGISTRATION_SUCCEED
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1645
1184
eee7a896cf0e server: show BridgeException traceback on unknown registering status
Goffi <goffi@goffi.org>
parents: 1182
diff changeset
1646 def registeringError(failure_):
1164
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1647 # FIXME: better error handling for bridge error is needed
1184
eee7a896cf0e server: show BridgeException traceback on unknown registering status
Goffi <goffi@goffi.org>
parents: 1182
diff changeset
1648 status = failure_.value.fullname.split('.')[-1]
961
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1649 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
1650 return C.ALREADY_EXISTS
1164
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1651 elif status == "InvalidCertificate":
ef3adc3f2078 handle invalid certificate on registration + fixed error handling on registration
Goffi <goffi@goffi.org>
parents: 1158
diff changeset
1652 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
1653 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
1654 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
1655 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1656 log.error(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1657 _("Unknown registering error status: {status}\n{traceback}").format(
1184
eee7a896cf0e server: show BridgeException traceback on unknown registering status
Goffi <goffi@goffi.org>
parents: 1182
diff changeset
1658 status=status, traceback=failure_.value.message
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1659 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1660 )
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
1661 return status
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1662
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1663 d = self.bridgeCall("registerSatAccount", email, password, login)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1664 d.addCallback(registered)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1665 d.addErrback(registeringError)
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1666 return d
22fe06569b1a server: moved logging worflow in separated method, so it can be used by Libervia Pages
Goffi <goffi@goffi.org>
parents: 959
diff changeset
1667
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
1668 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
1669 """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
1670
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
1671 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
1672 @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
1673 @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
1674 @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
1675 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
1676
1361
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1677 def initEb(self, failure):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1678 from twisted.application import app
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1679 if failure.check(SysExit):
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1680 if failure.value.message:
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1681 log.error(failure.value.message)
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1682 app._exitCode = failure.value.exit_code
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1683 reactor.stop()
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1684 else:
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1685 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
1686 app._exitCode = C.EXIT_INTERNAL_ERROR
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1687 reactor.stop()
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1688 return failure
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1689
1368
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1690 def _buildOnlyCb(self, __):
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1691 log.info(_("Stopping here due to --build-only flag"))
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1692 self.stop()
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1693
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
1694 def startService(self):
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1695 """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
1696 self._init()
1368
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1697 if self.options['build-only']:
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1698 self.initialised.addCallback(self._buildOnlyCb)
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1699 else:
9624c2a89a92 server: new `--build-only` flag:
Goffi <goffi@goffi.org>
parents: 1367
diff changeset
1700 self.initialised.addCallback(self._startService)
1361
626b7bbb7f90 server: mechanism to exit Libervia with an exit code:
Goffi <goffi@goffi.org>
parents: 1360
diff changeset
1701 self.initialised.addErrback(self.initEb)
331
06a48d805547 server side: make Libervia a Twisted plugin, and add it the --port argument + add a config file for the port.
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents: 330
diff changeset
1702
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1703 ## URLs ##
919
7b267496da1d server: moved session interfaces to session_iface module + added SATGuestSession
Goffi <goffi@goffi.org>
parents: 917
diff changeset
1704
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1705 def putChildSAT(self, path, resource):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1706 """Add a child to the sat resource"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1707 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1708 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1709 self.sat_root.putChild(path, resource)
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1710
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1711 def putChildAll(self, path, resource):
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1712 """Add a child to all vhost root resources"""
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1713 if not isinstance(path, bytes):
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1714 raise ValueError("path must be specified in bytes")
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1715 # 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
1716 # of the resource
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1717 # 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
1718 wrapped_res = web_resource.EncodingResourceWrapper(
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1719 resource, [server.GzipEncoderFactory()])
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1720 for root in self.roots:
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1721 root.putChild(path, wrapped_res)
914
0c0551967bdf server, browser: partial Libervia fix
Goffi <goffi@goffi.org>
parents: 913
diff changeset
1722
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1723 def getBuildPath(self, site_name: str, dev: bool=False) -> Path:
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1724 """Generate build path for a given site name
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1725
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1726 @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
1727 @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
1728 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
1729 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
1730 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
1731 @return: path to the build directory
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1732 """
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1733 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
1734 build_path_elts = [
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1735 config.getConfig(self.main_conf, "", "local_dir"),
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1736 C.CACHE_DIR,
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1737 C.LIBERVIA_CACHE,
1257
1ec41ac1e7cf server: seperation between production build dir and dev build dir:
Goffi <goffi@goffi.org>
parents: 1256
diff changeset
1738 sub_dir,
1246
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1739 regex.pathEscape(site_name or C.SITE_NAME_DEFAULT)]
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1740 build_path = Path("/".join(build_path_elts))
aaf28d45ae67 pages: browser code, first draft:
Goffi <goffi@goffi.org>
parents: 1245
diff changeset
1741 return build_path.expanduser().resolve()
1146
76d75423ef53 server: tasks manager first draft:
Goffi <goffi@goffi.org>
parents: 1144
diff changeset
1742
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
1743 def getExtBaseURLData(self, request):
d0b27d1e2d50 server: moved code to retrieve external server from legacy blog to server.py, and use it to find websocket URL
Goffi <goffi@goffi.org>
parents: 1003
diff changeset
1744 """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
1745
1435
396d5606477f server (proxy): add "Forwarded" and "X-Forwarded-xxx" headers to reverse proxy
Goffi <goffi@goffi.org>
parents: 1397
diff changeset
1746 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
1747 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
1748 - 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
1749 - 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
1750 - URL of the request
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1751 @return (urlparse.SplitResult): SplitResult instance with only scheme and
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1752 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
1753 """
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
1754 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
1755 url_path = request.URLPath()
1467
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1756
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1757 try:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1758 forwarded = request.requestHeaders.getRawHeaders(
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1759 "forwarded"
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1760 )[0]
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1761 except TypeError:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1762 # we try deprecated headers
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1763 try:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1764 proxy_netloc = request.requestHeaders.getRawHeaders(
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1765 "x-forwarded-host"
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1766 )[0]
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1767 except TypeError:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1768 proxy_netloc = None
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1769 try:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1770 proxy_scheme = request.requestHeaders.getRawHeaders(
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1771 "x-forwarded-proto"
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1772 )[0]
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1773 except TypeError:
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1774 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
1775 else:
1467
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1776 fwd_data = {
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1777 k.strip(): v.strip()
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1778 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
1779 }
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1780 proxy_netloc = fwd_data.get("host")
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1781 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
1782
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1783 return urllib.parse.SplitResult(
1467
d6062cccd4c0 server: better reverse proxy headers handling:
Goffi <goffi@goffi.org>
parents: 1462
diff changeset
1784 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
1785 ext_data.netloc or proxy_netloc or url_path.netloc.decode(),
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1786 ext_data.path or "/",
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1787 "",
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1788 "",
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1789 )
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
1790
1459
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1791 def getExtBaseURL(
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1792 self,
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1793 request: server.Request,
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1794 path: str = "",
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1795 query: str = "",
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1796 fragment: str = "",
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1797 scheme: Optional[str] = None,
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1798 ) -> 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
1799 """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
1800
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
1801 external URL is the URL seen by external user
1459
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1802 @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
1803 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
1804 @param params: same as for urlsplit.urlsplit
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1805 @param query: same as for urlsplit.urlsplit
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1806 @param fragment: same as for urlsplit.urlsplit
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1807 @param scheme: if not None, will override scheme from base URL
47db314e60ca server: `getExtBaseURL` type hints
Goffi <goffi@goffi.org>
parents: 1458
diff changeset
1808 @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
1809 """
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
1810 split_result = self.getExtBaseURLData(request)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1811 return urllib.parse.urlunsplit(
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1812 (
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1813 split_result.scheme if scheme is None else scheme,
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1814 split_result.netloc,
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1815 os.path.join(split_result.path, path),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1816 query,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1817 fragment,
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1818 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1819 )
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
1820
1458
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1821 def checkRedirection(self, vhost_root: LiberviaRootResource, url_path: str) -> str:
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1822 """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
1823
1458
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1824 @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
1825 @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
1826 @return: possibly redirected URL which should link to the same location
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1827 """
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1828 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
1829 url_parts = url_path.strip("/").split("/")
1475
83cd4862b134 server: fix args range in checkRedirection
Goffi <goffi@goffi.org>
parents: 1467
diff changeset
1830 for idx in range(len(url_parts), -1, -1):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1831 test_url = "/" + "/".join(url_parts[:idx])
1037
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1832 if test_url in inv_redirections:
dc6c8c4d8ff6 server: redirection rework:
Goffi <goffi@goffi.org>
parents: 1019
diff changeset
1833 rem_url = url_parts[idx:]
1099
ea0d41ef3719 server: return absolute page in checkRedirection
Goffi <goffi@goffi.org>
parents: 1098
diff changeset
1834 return os.path.join(
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1835 "/", "/".join([inv_redirections[test_url]] + rem_url)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1836 )
1458
db13f5c768a0 server: `checkRedirection`: fix range variable + type hints + arg name:
Goffi <goffi@goffi.org>
parents: 1457
diff changeset
1837 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
1838
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1839 ## Sessions ##
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1840
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1841 def purgeSession(self, request):
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1842 """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
1843 session = request.session
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1844 if session is not None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1845 log.debug(_("session purge"))
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1846 web_session = self.getSessionData(request, session_iface.IWebSession)
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1847 socket = web_session.ws_socket
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1848 if socket is not None:
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1849 socket.close()
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1850 session.ws_socket = None
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1851 session.expire()
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1852 # 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
1853 # session during request handling
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1854 request._secureSession = request._insecureSession = None
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1855
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1856 def getSessionData(self, request, *args):
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1857 """helper method to retrieve session data
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1858
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1859 @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
1860 @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
1861 @return (iterator(data)): requested session data
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1862 """
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1863 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
1864 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
1865 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
1866 else:
94f88277c2e7 libervia (pages): getSessionData return one session if used with one argument, else an iterator
Goffi <goffi@goffi.org>
parents: 923
diff changeset
1867 return (iface(session) for iface in args)
920
8cea8bf41b03 server: new purgeSession and getSessionData helper methods
Goffi <goffi@goffi.org>
parents: 919
diff changeset
1868
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1869 @defer.inlineCallbacks
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1870 def getAffiliation(self, request, service, node):
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1871 """retrieve pubsub node affiliation for current user
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1872
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1873 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
1874 @param request(server.Request): request linked to the session
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1875 @param service(jid.JID): pubsub service
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1876 @param node(unicode): pubsub node
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1877 @return (unicode): affiliation
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1878 """
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1879 web_session = self.getSessionData(request, session_iface.IWebSession)
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1880 if web_session.profile is None:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1881 raise exceptions.InternalError("profile must be set to use this method")
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1882 affiliation = web_session.getAffiliation(service, node)
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1883 if affiliation is not None:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1884 defer.returnValue(affiliation)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1885 else:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1886 try:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1887 affiliations = yield self.bridgeCall(
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1888 "psAffiliationsGet", service.full(), node, web_session.profile
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1889 )
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1890 except Exception as e:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1891 log.warning(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1892 "Can't retrieve affiliation for {service}/{node}: {reason}".format(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1893 service=service, node=node, reason=e
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1894 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1895 )
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1896 affiliation = ""
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1897 else:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1898 try:
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1899 affiliation = affiliations[node]
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1900 except KeyError:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1901 affiliation = ""
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
1902 web_session.setAffiliation(service, node, affiliation)
1093
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1903 defer.returnValue(affiliation)
eda7a1c6532a server: new getAffiliation method:
Goffi <goffi@goffi.org>
parents: 1090
diff changeset
1904
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1905 ## Websocket (dynamic pages) ##
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1906
1504
409d10211b20 server, browser: dynamic pages refactoring:
Goffi <goffi@goffi.org>
parents: 1484
diff changeset
1907 def get_websocket_url(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
1908 base_url_split = self.getExtBaseURLData(request)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1909 if base_url_split.scheme.endswith("s"):
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1910 scheme = "wss"
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1911 else:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1912 scheme = "ws"
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1913
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
1914 return self.getExtBaseURL(request, path=scheme, scheme=scheme)
995
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1915
f88325b56a6a server: dynamic pages first draft:
Goffi <goffi@goffi.org>
parents: 990
diff changeset
1916
1019
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1917 ## Various utils ##
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1918
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1919 def getHTTPDate(self, timestamp=None):
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1920 now = time.gmtime(timestamp)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1921 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
1922 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
1923 )
1019
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1924 return time.strftime(fmt_date, now)
34240d08f682 pages: HTTP cache headers handling:
Goffi <goffi@goffi.org>
parents: 1012
diff changeset
1925
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
1926 ## 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
1927
1128
6414fd795df4 server, pages: multi-sites refactoring:
Goffi <goffi@goffi.org>
parents: 1127
diff changeset
1928 def _startService(self, __=None):
449
981ed669d3b3 /!\ reorganize all the file hierarchy, move the code and launching script to src:
souliane <souliane@mailoo.org>
parents: 448
diff changeset
1929 """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
1930
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
1931 @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
1932 @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
1933 @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
1934 """
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
1935 # now that we have service profile connected, we add resource for its cache
67a59552f3e3 server (blog): fixed avatars handling, there is now a well-known URL to SERVICE_PROFILE cache
Goffi <goffi@goffi.org>
parents: 937
diff changeset
1936 service_path = regex.pathEscape(C.SERVICE_PROFILE)
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1937 cache_dir = os.path.join(self.cache_root_dir, "profiles", service_path)
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1938 self.cache_resource.putChild(service_path.encode('utf-8'),
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1939 ProtectedFile(cache_dir))
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1940 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
1941 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
1942
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1943 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
1944 try:
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1945 tls.TLSOptionsCheck(self.options)
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1946 context_factory = tls.getTLSContextFactory(self.options)
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1947 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
1948 log.warning(
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1949 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
1950 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
1951 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
1952 log.warning(
eb4f03da0d7d server: re-usable Twisted TLS code has been moved to SàT backend
Goffi <goffi@goffi.org>
parents: 1268
diff changeset
1953 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
1954 self.quit(1)
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1955 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
1956 if self.options["connection_type"] in ("http", "both"):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1957 if (
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1958 self.options["connection_type"] == "both"
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1959 and self.options["redirect_to_https"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1960 ):
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1961 reactor.listenTCP(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1962 self.options["port"],
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1963 server.Site(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1964 RedirectToHTTPS(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1965 self.options["port"], self.options["port_https_ext"]
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1966 )
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1967 ),
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1968 )
446
c406e46fe9c0 server_side: update the connection mechanism to handle profile passwords
souliane <souliane@mailoo.org>
parents: 445
diff changeset
1969 else:
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
1970 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
1971
1108
7ec1ba86d38d server: replaced blocking calls to bridge by bridgeCall which is not blocking
Goffi <goffi@goffi.org>
parents: 1106
diff changeset
1972 @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
1973 def stopService(self):
473
d1565906f228 server_side: fixed some forgotten print statement
Goffi <goffi@goffi.org>
parents: 472
diff changeset
1974 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
1975 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
1976 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
1977 try:
1108
7ec1ba86d38d server: replaced blocking calls to bridge by bridgeCall which is not blocking
Goffi <goffi@goffi.org>
parents: 1106
diff changeset
1978 yield self.bridgeCall("disconnect", C.SERVICE_PROFILE)
859
0e9341e537d6 server: catch exceptions on service profile connection check, and show an helping message if an error is caught
Goffi <goffi@goffi.org>
parents: 858
diff changeset
1979 except Exception:
1216
b2d067339de3 python 3 port:
Goffi <goffi@goffi.org>
parents: 1204
diff changeset
1980 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
1981
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
1982 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
1983 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
1984
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
1985 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
1986 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
1987
812
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1988 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
1989 """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
1990
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1991 @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
1992 """
fd6965c16e7e server: added "quit" method to exit with an exit code when reactor is running
Goffi <goffi@goffi.org>
parents: 810
diff changeset
1993 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
1994 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
1995
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1996
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1997 class RedirectToHTTPS(web_resource.Resource):
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
1998 def __init__(self, old_port, new_port):
858
7dde76708892 server: URL redirections + misc:
Goffi <goffi@goffi.org>
parents: 856
diff changeset
1999 web_resource.Resource.__init__(self)
415
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
2000 self.isLeaf = True
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
2001 self.old_port = old_port
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
2002 self.new_port = new_port
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
2003
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
2004 def render(self, request):
1354
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
2005 netloc = request.URLPath().netloc.decode().replace(
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
2006 f":{self.old_port}", f":{self.new_port}"
1113
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
2007 )
1354
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
2008 url = f"https://{netloc}{request.uri.decode()}"
4751e9febbbf server: fixed HTTPS redirection
Goffi <goffi@goffi.org>
parents: 1321
diff changeset
2009 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
2010
fadbba1d793f server_side: added support for SSL and related parameters:
souliane <souliane@mailoo.org>
parents: 413
diff changeset
2011
1506
ce879da7fcf7 server: fix `on_signal` callback
Goffi <goffi@goffi.org>
parents: 1505
diff changeset
2012 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
2013 registerAdapter(
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
2014 session_iface.SATGuestSession, server.Session, session_iface.ISATGuestSession
cdd389ef97bc server: code style reformatting using black
Goffi <goffi@goffi.org>
parents: 1108
diff changeset
2015 )