Mercurial > libervia-web
annotate libervia/web/server/resources.py @ 1618:5d9889f14012 default tip @
server: start major redesign
- Add icons to menu items
- Switch menu items representation from tuple to dictionary for future extensibility:
- Include icon information
- Prepare for additional data
- Remove "login" from main menu, add login page URL to template data, as it is now a separate right-aligned item
author | Goffi <goffi@goffi.org> |
---|---|
date | Sat, 26 Oct 2024 23:07:01 +0200 |
parents | f3305832f3f6 |
children |
rev | line source |
---|---|
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
1 #!/usr/bin/env python3 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
2 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
3 # Libervia Web |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
4 # Copyright (C) 2011-2021 Jérôme Poisson <goffi@goffi.org> |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
5 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
6 # This program is free software: you can redistribute it and/or modify |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
7 # it under the terms of the GNU Affero General Public License as published by |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
8 # the Free Software Foundation, either version 3 of the License, or |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
9 # (at your option) any later version. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
10 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
11 # This program is distributed in the hope that it will be useful, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
14 # GNU Affero General Public License for more details. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
15 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
16 # You should have received a copy of the GNU Affero General Public License |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
17 # along with this program. If not, see <http://www.gnu.org/licenses/>. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
18 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
19 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
20 import os.path |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
21 from pathlib import Path |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
22 import urllib.error |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
23 import urllib.parse |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
24 import urllib.request |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
25 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
26 from twisted.internet import defer |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
27 from twisted.web import server |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
28 from twisted.web import static |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
29 from twisted.web import resource as web_resource |
1609
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
30 from twisted.web.util import Redirect |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
31 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
32 from libervia.web.server.constants import Const as C |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
33 from libervia.web.server.utils import quote |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
34 from libervia.backend.core import exceptions |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
35 from libervia.backend.core.i18n import D_, _ |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
36 from libervia.backend.core.log import getLogger |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
37 from libervia.backend.tools.common import uri as common_uri |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
38 from libervia.backend.tools.common import data_format |
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
39 from libervia.backend.tools.common.utils import OrderedSet, recursive_update |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 from . import proxy |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 log = getLogger(__name__) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
44 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
45 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 class ProtectedFile(static.File): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
47 """A static.File class which doesn't show directory listing""" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 def __init__(self, path, *args, **kwargs): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 if "defaultType" not in kwargs and len(args) < 2: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 # defaultType is second positional argument, and Twisted uses it |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
52 # in File.createSimilarFile, so we set kwargs only if it is missing |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
53 # in kwargs and it is not in a positional argument |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 kwargs["defaultType"] = "application/octet-stream" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 super(ProtectedFile, self).__init__(str(path), *args, **kwargs) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 def directoryListing(self): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 return web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
59 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
60 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 def getChild(self, path, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 return super().getChild(path, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 def getChildWithDefault(self, path, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 return super().getChildWithDefault(path, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 def getChildForRequest(self, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 return super().getChildForRequest(request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
69 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
70 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 class LiberviaRootResource(ProtectedFile): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 """Specialized resource for Libervia root |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
73 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
74 handle redirections declared in libervia.conf |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
75 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
76 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
77 def __init__(self, host, host_name, site_name, site_path, *args, **kwargs): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 ProtectedFile.__init__(self, *args, **kwargs) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 self.host = host |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 self.host_name = host_name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 self.site_name = site_name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 self.site_path = Path(site_path) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 self.default_theme = self.config_get('theme') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 if self.default_theme is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 if not host_name: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
86 # FIXME: we use bulma theme by default for main site for now |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
87 # as the development is focusing on this one, and default theme may |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 # be broken |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 self.default_theme = 'bulma' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 self.default_theme = C.TEMPLATE_THEME_DEFAULT |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 self.site_themes = set() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 self.named_pages = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 self.browser_modules = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 # template dynamic data used in all pages |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 self.dyn_data_common = {"scripts": OrderedSet()} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
97 for theme, data in host.renderer.get_themes_data(site_name).items(): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
98 # we check themes for browser metadata, and merge them here if found |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 self.site_themes.add(theme) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 browser_meta = data.get('browser_meta') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 if browser_meta is not None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
102 log.debug(f"merging browser metadata from theme {theme}: {browser_meta}") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 recursive_update(self.browser_modules, browser_meta) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 browser_path = data.get('browser_path') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 if browser_path is not None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 self.browser_modules.setdefault('themes_browser_paths', set()).add( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 browser_path) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 next(browser_path.glob("*.py")) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 except StopIteration: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 pass |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
113 log.debug(f"found brython script(s) for theme {theme}") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 self.browser_modules.setdefault('brython', []).append( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 { |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 "path": browser_path, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 "url_hash": None, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 "url_prefix": f"__t_{theme}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
119 } |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
120 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
121 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 self.uri_callbacks = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 self.pages_redirects = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 self.cached_urls = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 self.main_menu = None |
1618 | 126 self.login_url = None |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 # map Libervia application names => data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 self.libervia_apps = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 self.build_path = host.get_build_path(site_name) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
130 self.build_path.mkdir(parents=True, exist_ok=True) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
131 self.dev_build_path = host.get_build_path(site_name, dev=True) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 self.dev_build_path.mkdir(parents=True, exist_ok=True) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 self.putChild( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 C.BUILD_DIR.encode(), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 ProtectedFile( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 self.build_path, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 defaultType="application/octet-stream"), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 def __str__(self): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
141 return ( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
142 f"Root resource for {self.host_name or 'default host'} using " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 f"{self.site_name or 'default site'} at {self.site_path} and deserving " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 f"files at {self.path}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
146 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 def config_get(self, key, default=None, value_type=None): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 """Retrieve configuration for this site |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
149 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
150 params are the same as for [Libervia.config_get] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
151 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 return self.host.config_get(self, key, default, value_type) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 def get_front_url(self, theme): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 return Path( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 '/', |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 C.TPL_RESOURCE, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 self.site_name or C.SITE_NAME_DEFAULT, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 C.TEMPLATE_TPL_DIR, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 theme) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
161 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 def add_resource_to_path(self, path: str, resource: web_resource.Resource) -> None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 """Add a resource to the given path |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
164 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
165 A "NoResource" will be used for all intermediate segments |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 segments, __, last_segment = path.rpartition("/") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 url_segments = segments.split("/") if segments else [] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 current = self |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 for segment in url_segments: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 resource = web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 current.putChild(segment, resource) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 current = resource |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 current.putChild( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 last_segment.encode('utf-8'), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 resource |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
179 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 async def _start_app(self, app_name, extra=None) -> dict: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 """Start a Libervia App |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 @param app_name: canonical application name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 @param extra: extra parameter to configure app |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
185 @return: app data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 app data will not include computed exposed data, at this needs to wait for the |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 app to be started |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 if extra is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 extra = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 log.info(_( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 "starting application {app_name}").format(app_name=app_name)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 app_data = data_format.deserialise( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 await self.host.bridge_call( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 "application_start", app_name, data_format.serialise(extra) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
198 if app_data.get("started", False): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
199 log.debug(f"application {app_name!r} is already started or starting") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 # we do not await on purpose, the workflow should not be blocking at this |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
201 # point |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 defer.ensureDeferred(self._on_app_started(app_name, app_data["instance"])) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
203 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 self.host.apps_cb[app_data["instance"]] = self._on_app_started |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 return app_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 async def _on_app_started( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 self, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 app_name: str, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 instance_id: str |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
211 ) -> None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
212 exposed_data = self.libervia_apps[app_name] = data_format.deserialise( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 await self.host.bridge_call("application_exposed_get", app_name, "", "") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 ) |
1609
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
215 external = exposed_data.get("web_external", False) |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
216 if external: |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
217 log.info( |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
218 f"App {app_name!r} (instance {instance_id!r}) has been started." |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
219 ) |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
220 # we don't add any resource of proxy for external apps. |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
221 return |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
222 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
223 web_port = int(exposed_data['ports']['web'].split(':')[1]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 except (KeyError, ValueError): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
225 log.warning(_( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
226 "no web port found for application {app_name!r}, can't use it " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
227 ).format(app_name=app_name)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 raise exceptions.DataError("no web port found") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
229 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
230 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 url_prefix = exposed_data['url_prefix'].strip().rstrip('/') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 except (KeyError, AttributeError) as e: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
233 log.warning(_( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 "no URL prefix specified for this application, we can't embed it: {msg}") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 .format(msg=e)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 raise exceptions.DataError("no URL prefix") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 if not url_prefix.startswith('/'): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 raise exceptions.DataError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 f"invalid URL prefix, it must start with '/': {url_prefix!r}") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 res = proxy.SatReverseProxyResource( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 "localhost", |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 web_port, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 url_prefix.encode() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
246 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
247 self.add_resource_to_path(url_prefix, res) |
1609
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
248 |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 log.info( |
1609
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
250 f"Resource for app {app_name!r} (instance {instance_id!r}) has been added." |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
252 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
253 async def _init_redirections(self, options): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 url_redirections = options["url_redirections_dict"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
255 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
256 url_redirections = url_redirections.get(self.site_name, {}) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
258 ## redirections |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 self.redirections = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 self.inv_redirections = {} # new URL to old URL map |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 for old, new_data_list in url_redirections.items(): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
263 # several redirections can be used for one path by using a list. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 # The redirection will be done using first item of the list, and all items |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
265 # will be used for inverse redirection. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 # e.g. if a => [b, c], a will redirect to c, and b and c will both be |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
267 # equivalent to a |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 if not isinstance(new_data_list, list): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 new_data_list = [new_data_list] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 for new_data in new_data_list: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 # new_data can be a dictionary or a unicode url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 if isinstance(new_data, dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 # new_data dict must contain either "url", "page" or "path" key |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
274 # (exclusive) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 # if "path" is used, a file url is constructed with it |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 if (( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 len( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 {"path", "url", "page"}.intersection(list(new_data.keys())) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 ) != 1 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 )): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 raise ValueError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 'You must have one and only one of "url", "page" or "path" ' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 'key in your url_redirections_dict data' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
285 if "url" in new_data: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 new = new_data["url"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 elif "page" in new_data: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 new = new_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 new["type"] = "page" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 new.setdefault("path_args", []) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 if not isinstance(new["path_args"], list): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 _('"path_args" in redirection of {old} must be a list. ' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 'Ignoring the redirection'.format(old=old))) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
295 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
296 new.setdefault("query_args", {}) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
297 if not isinstance(new["query_args"], dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 _( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
300 '"query_args" in redirection of {old} must be a ' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 'dictionary. Ignoring the redirection' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 ).format(old=old) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 new["path_args"] = [quote(a) for a in new["path_args"]] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 # we keep an inversed dict of page redirection |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 # (page/path_args => redirecting URL) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
308 # so get_url can return the redirecting URL if the same arguments |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 # are used # making the URL consistent |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 args_hash = tuple(new["path_args"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 self.pages_redirects.setdefault(new_data["page"], {}).setdefault( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 args_hash, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 # we need lists in query_args because it will be used |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 # as it in request.path_args |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 for k, v in new["query_args"].items(): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 if isinstance(v, str): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 new["query_args"][k] = [v] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 elif "path" in new_data: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 new = "file:{}".format(urllib.parse.quote(new_data["path"])) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 elif isinstance(new_data, str): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 new = new_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 new_data = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 _("ignoring invalid redirection value: {new_data}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 new_data=new_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
333 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
334 # some normalization |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 if not old.strip(): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 # root URL special case |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 old = "" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 elif not old.startswith("/"): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 _("redirected url must start with '/', got {value}. Ignoring") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 .format(value=old) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 old = self._normalize_url(old) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 if isinstance(new, dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 # dict are handled differently, they contain data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 # which ared use dynamically when the request is done |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 self.redirections.setdefault(old, new) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 if not old: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 if new["type"] == "page": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 log.info( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 _("Root URL redirected to page {name}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 name=new["page"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
357 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 if new["type"] == "page": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 page = self.get_page_by_name(new["page"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 url = page.get_url(*new.get("path_args", [])) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 self.inv_redirections[url] = old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 # at this point we have a redirection URL in new, we can parse it |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 new_url = urllib.parse.urlsplit(new) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 # we handle the known URL schemes |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 if new_url.scheme == "xmpp": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 location = self.get_page_path_from_uri(new) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 if location is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 log.warning( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 _("ignoring redirection, no page found to handle this URI: " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 "{uri}").format(uri=new)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
376 request_data = self._get_request_data(location) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 self.inv_redirections[location] = old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 elif new_url.scheme in ("", "http", "https"): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 # direct redirection |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
381 if new_url.netloc: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 raise NotImplementedError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 "netloc ({netloc}) is not implemented yet for " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 "url_redirections_dict, it is not possible to redirect to an " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
385 "external website".format(netloc=new_url.netloc)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
386 location = urllib.parse.urlunsplit( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
387 ("", "", new_url.path, new_url.query, new_url.fragment) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
388 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
389 request_data = self._get_request_data(location) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
390 self.inv_redirections[location] = old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
391 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
392 elif new_url.scheme == "file": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 # file or directory |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 if new_url.netloc: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 raise NotImplementedError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
396 "netloc ({netloc}) is not implemented for url redirection to " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 "file system, it is not possible to redirect to an external " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
398 "host".format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
399 netloc=new_url.netloc)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 path = urllib.parse.unquote(new_url.path) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 if not os.path.isabs(path): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
402 raise ValueError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
403 "file redirection must have an absolute path: e.g. " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
404 "file:/path/to/my/file") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
405 # for file redirection, we directly put child here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
406 resource_class = ( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
407 ProtectedFile if new_data.get("protected", True) else static.File |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
408 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
409 res = resource_class(path, defaultType="application/octet-stream") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 self.add_resource_to_path(old, res) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 log.info("[{host_name}] Added redirection from /{old} to file system " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
412 "path {path}".format(host_name=self.host_name, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
413 old=old, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
414 path=path)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
415 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
416 # we don't want to use redirection system, so we continue here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
417 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
418 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
419 elif new_url.scheme == "libervia-app": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
420 # a Libervia application |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
421 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
422 app_name = urllib.parse.unquote(new_url.path).lower().strip() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
423 extra = {"url_prefix": f"/{old}"} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
424 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
425 await self._start_app(app_name, extra) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
426 except Exception as e: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
427 log.warning(_( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
428 "Can't launch {app_name!r} for path /{old}: {e}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
429 app_name=app_name, old=old, e=e)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
430 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
431 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
432 log.info( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
433 f"[{self.host_name}] Added redirection from /{old} to " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
434 f"application {app_name}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
435 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
436 # normal redirection system is not used here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
437 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
438 elif new_url.scheme == "proxy": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
439 # a reverse proxy |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
440 host, port = new_url.hostname, new_url.port |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
441 if host is None or port is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
442 raise ValueError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
443 "invalid host or port in proxy redirection, please check your " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
444 "configuration: {new_url.geturl()}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
445 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
446 url_prefix = (new_url.path or old).rstrip('/') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
447 res = proxy.SatReverseProxyResource( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
448 host, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
449 port, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
450 url_prefix.encode(), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
451 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
452 self.add_resource_to_path(old, res) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
453 log.info( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
454 f"[{self.host_name}] Added redirection from /{old} to reverse proxy " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
455 f"{new_url.netloc} with URL prefix {url_prefix}/" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
456 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
457 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
458 # normal redirection system is not used here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
459 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
460 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
461 raise NotImplementedError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
462 "{scheme}: scheme is not managed for url_redirections_dict".format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
463 scheme=new_url.scheme |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
464 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
465 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
466 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
467 self.redirections.setdefault(old, request_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
468 if not old: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
469 log.info(_("[{host_name}] Root URL redirected to {uri}") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
470 .format(host_name=self.host_name, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
471 uri=request_data[1])) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
472 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
473 # the default root URL, if not redirected |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
474 if not "" in self.redirections: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
475 self.redirections[""] = self._get_request_data(C.LIBERVIA_PAGE_START) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
476 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
477 async def _set_menu(self, menus): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
478 menus = menus.get(self.site_name, []) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
479 main_menu = [] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
480 for menu in menus: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
481 if not menu: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
482 msg = _("menu item can't be empty") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
483 log.error(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
484 raise ValueError(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
485 elif isinstance(menu, list): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
486 if len(menu) != 2: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
487 msg = _( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
488 "menu item as list must be in the form [page_name, absolue URL]" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
489 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
490 log.error(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
491 raise ValueError(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 page_name, url = menu |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
493 elif menu.startswith("libervia-app:"): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
494 app_name = menu[13:].strip().lower() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
495 app_data = await self._start_app(app_name) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
496 exposed_data = app_data["expose"] |
1609
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
497 external = exposed_data.get("web_external", False) |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
498 if external: |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
499 # The app is opened separately and not embedded into the Web frontend. |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
500 try: |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
501 front_url = exposed_data["front_url"] |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
502 except KeyError: |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
503 raise exceptions.DataError( |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
504 'Missing "front_url" in app data for external app ' |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
505 f'{app_name!r}, it is mandatory when "external" is set. This ' |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
506 f'must be set in {app_name!r} app configuration file.' |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
507 |
1609
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
508 ) |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
509 else: |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
510 front_url = exposed_data['web_url_path'] |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
511 options = self.host.options |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
512 url_redirections = options["url_redirections_dict"].setdefault( |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
513 self.site_name, {} |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
514 ) |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
515 if front_url in url_redirections: |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
516 raise exceptions.ConflictError( |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
517 f"There is already a redirection from {front_url!r}, can't add " |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
518 f"{app_name!r}") |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
519 |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
520 url_redirections[front_url] = { |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
521 "page": 'embed_app', |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
522 "path_args": [app_name] |
f3305832f3f6
server (resources): handle external Libervia apps by adding them to menu without using a Proxy:
Goffi <goffi@goffi.org>
parents:
1518
diff
changeset
|
523 } |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
524 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
525 page_name = exposed_data.get('web_label', app_name).title() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
526 url = front_url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
527 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
528 log.debug( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
529 f"Application {app_name} added to menu of {self.site_name}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
530 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
531 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
532 page_name = menu |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
533 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
534 url = self.get_page_by_name(page_name).url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
535 except KeyError as e: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
536 log_msg = _("Can'find a named page ({msg}), please check " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
537 "menu_json in configuration.").format(msg=e.args[0]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
538 log.error(log_msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
539 raise exceptions.ConfigError(log_msg) |
1618 | 540 |
541 if page_name == "login": | |
542 self.login_url = url | |
543 continue | |
544 icon = C.DEFAULT_ICONS.get(page_name, "") | |
545 | |
546 main_menu.append({ | |
547 "name": page_name, | |
548 "url": url, | |
549 "icon": icon | |
550 }) | |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
551 self.main_menu = main_menu |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
552 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
553 def _normalize_url(self, url, lower=True): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
554 """Return URL normalized for self.redirections dict |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
555 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
556 @param url(unicode): URL to normalize |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
557 @param lower(bool): lower case of url if True |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
558 @return (str): normalized URL |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
559 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
560 if lower: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 url = url.lower() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
562 return "/".join((p for p in url.split("/") if p)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
563 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
564 def _get_request_data(self, uri): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
565 """Return data needed to redirect request |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
566 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
567 @param url(unicode): destination url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
568 @return (tuple(list[str], str, str, dict): tuple with |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
569 splitted path as in Request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
570 uri as in Request.uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
571 path as in Request.path |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
572 args as in Request.args |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
573 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
574 uri = uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
575 # XXX: we reuse code from twisted.web.http.py here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
576 # as we need to have the same behaviour |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
577 x = uri.split("?", 1) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
578 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
579 if len(x) == 1: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
580 path = uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
581 args = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
582 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
583 path, argstring = x |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
584 args = urllib.parse.parse_qs(argstring, True) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
585 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
586 # XXX: splitted path case must not be changed, as it may be significant |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
587 # (e.g. for blog items) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
588 return ( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
589 self._normalize_url(path, lower=False).split("/"), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
590 uri, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
591 path, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
592 args, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
593 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
594 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
595 def _redirect(self, request, request_data): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
596 """Redirect an URL by rewritting request |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
597 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
598 this is *NOT* a HTTP redirection, but equivalent to URL rewritting |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
599 @param request(web.http.request): original request |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
600 @param request_data(tuple): data returned by self._get_request_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
601 @return (web_resource.Resource): resource to use |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
602 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
603 # recursion check |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
604 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
605 request._redirected |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
606 except AttributeError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
607 pass |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
608 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
609 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
610 __, uri, __, __ = request_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
611 except ValueError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
612 uri = "" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
613 log.error(D_( "recursive redirection, please fix this URL:\n" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
614 "{old} ==> {new}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
615 old=request.uri.decode("utf-8"), new=uri)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
616 return web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
617 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
618 request._redirected = True # here to avoid recursive redirections |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
619 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
620 if isinstance(request_data, dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
621 if request_data["type"] == "page": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
622 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
623 page = self.get_page_by_name(request_data["page"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
624 except KeyError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
625 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
626 _( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
627 'Can\'t find page named "{name}" requested in redirection' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
628 ).format(name=request_data["page"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
629 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
630 return web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
631 path_args = [pa.encode('utf-8') for pa in request_data["path_args"]] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
632 request.postpath = path_args + request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
633 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
634 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
635 request.args.update(request_data["query_args"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
636 except (TypeError, ValueError): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
637 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
638 _("Invalid args in redirection: {query_args}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
639 query_args=request_data["query_args"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
640 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
641 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
642 return web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
643 return page |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
644 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
645 raise exceptions.InternalError("unknown request_data type") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
646 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
647 path_list, uri, path, args = request_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
648 path_list = [p.encode('utf-8') for p in path_list] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
649 log.debug( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
650 "Redirecting URL {old} to {new}".format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
651 old=request.uri.decode('utf-8'), new=uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
652 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
653 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
654 # we change the request to reflect the new url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
655 request.postpath = path_list[1:] + request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
656 request.args.update(args) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
657 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
658 # we start again to look for a child with the new url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
659 return self.getChildWithDefault(path_list[0], request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
660 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
661 def get_page_by_name(self, name): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
662 """Retrieve page instance from its name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
663 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
664 @param name(unicode): name of the page |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
665 @return (LiberviaPage): page instance |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
666 @raise KeyError: the page doesn't exist |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
667 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
668 return self.named_pages[name] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
669 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
670 def get_page_path_from_uri(self, uri): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
671 """Retrieve page URL from xmpp: URI |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
672 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
673 @param uri(unicode): URI with a xmpp: scheme |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
674 @return (unicode,None): absolute path (starting from root "/") to page handling |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
675 the URI. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
676 None is returned if no page has been registered for this URI |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
677 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
678 uri_data = common_uri.parse_xmpp_uri(uri) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
679 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
680 page, cb = self.uri_callbacks[uri_data["type"], uri_data["sub_type"]] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
681 except KeyError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
682 url = None |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
683 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
684 url = cb(page, uri_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
685 if url is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
686 # no handler found |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
687 # we try to find a more generic one |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
688 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
689 page, cb = self.uri_callbacks[uri_data["type"], None] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
690 except KeyError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
691 pass |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
692 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
693 url = cb(page, uri_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
694 return url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
695 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
696 def getChildWithDefault(self, name, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
697 # XXX: this method is overriden only for root url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
698 # which is the only ones who need to be handled before other children |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
699 if name == b"" and not request.postpath: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
700 return self._redirect(request, self.redirections[""]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
701 return super(LiberviaRootResource, self).getChildWithDefault(name, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
702 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
703 def getChild(self, name, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
704 resource = super(LiberviaRootResource, self).getChild(name, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
705 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
706 if isinstance(resource, web_resource.NoResource): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
707 # if nothing was found, we try our luck with redirections |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
708 # XXX: we want redirections to happen only if everything else failed |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
709 path_elt = request.prepath + request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
710 for idx in range(len(path_elt), -1, -1): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
711 test_url = b"/".join(path_elt[:idx]).decode('utf-8').lower() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
712 if test_url in self.redirections: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
713 request_data = self.redirections[test_url] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
714 request.postpath = path_elt[idx:] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
715 return self._redirect(request, request_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
716 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
717 return resource |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
718 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
719 def putChild(self, path, resource): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
720 """Add a child to the root resource""" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
721 if not isinstance(path, bytes): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
722 raise ValueError("path must be specified in bytes") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
723 if not isinstance(resource, web_resource.EncodingResourceWrapper): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
724 # FIXME: check that no information is leaked (c.f. https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html#request-encoders) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
725 resource = web_resource.EncodingResourceWrapper( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
726 resource, [server.GzipEncoderFactory()]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
727 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
728 super(LiberviaRootResource, self).putChild(path, resource) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
729 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
730 def createSimilarFile(self, path): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
731 # XXX: this method need to be overriden to avoid recreating a LiberviaRootResource |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
732 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
733 f = LiberviaRootResource.__base__( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
734 path, self.defaultType, self.ignoredExts, self.registry |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
735 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
736 # refactoring by steps, here - constructor should almost certainly take these |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
737 f.processors = self.processors |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
738 f.indexNames = self.indexNames[:] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
739 f.childNotFound = self.childNotFound |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
740 return f |