Mercurial > libervia-web
annotate libervia/web/server/resources.py @ 1552:c62027660ec1
doc (installation): update `pipx` instruction and remove `requirements.txt` mention
author | Goffi <goffi@goffi.org> |
---|---|
date | Wed, 09 Aug 2023 00:48:21 +0200 |
parents | eb00d593801d |
children | f3305832f3f6 |
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 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
30 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
31 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
|
32 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
|
33 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
|
34 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
|
35 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
|
36 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
|
37 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
|
38 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
|
39 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
40 from . import proxy |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
41 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
42 log = getLogger(__name__) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
43 |
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 class ProtectedFile(static.File): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
46 """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
|
47 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
48 def __init__(self, path, *args, **kwargs): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
49 if "defaultType" not in kwargs and len(args) < 2: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
50 # defaultType is second positional argument, and Twisted uses it |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
51 # 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
|
52 # 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
|
53 kwargs["defaultType"] = "application/octet-stream" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
54 super(ProtectedFile, self).__init__(str(path), *args, **kwargs) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
55 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
56 def directoryListing(self): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
57 return web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
58 |
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 def getChild(self, path, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
61 return super().getChild(path, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
62 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
63 def getChildWithDefault(self, path, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
64 return super().getChildWithDefault(path, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
65 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
66 def getChildForRequest(self, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
67 return super().getChildForRequest(request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
68 |
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 class LiberviaRootResource(ProtectedFile): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
71 """Specialized resource for Libervia root |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
72 |
1518
eb00d593801d
refactoring: rename `libervia` to `libervia.web` + update imports following backend changes
Goffi <goffi@goffi.org>
parents:
1512
diff
changeset
|
73 handle redirections declared in libervia.conf |
1512
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
74 """ |
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 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
|
77 ProtectedFile.__init__(self, *args, **kwargs) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
78 self.host = host |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
79 self.host_name = host_name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
80 self.site_name = site_name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
81 self.site_path = Path(site_path) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
82 self.default_theme = self.config_get('theme') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
83 if self.default_theme is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
84 if not host_name: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
85 # 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
|
86 # 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
|
87 # be broken |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
88 self.default_theme = 'bulma' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
89 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
90 self.default_theme = C.TEMPLATE_THEME_DEFAULT |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
91 self.site_themes = set() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
92 self.named_pages = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
93 self.browser_modules = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
94 # template dynamic data used in all pages |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
95 self.dyn_data_common = {"scripts": OrderedSet()} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
96 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
|
97 # 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
|
98 self.site_themes.add(theme) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
99 browser_meta = data.get('browser_meta') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
100 if browser_meta is not None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
101 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
|
102 recursive_update(self.browser_modules, browser_meta) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
103 browser_path = data.get('browser_path') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
104 if browser_path is not None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
105 self.browser_modules.setdefault('themes_browser_paths', set()).add( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
106 browser_path) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
107 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
108 next(browser_path.glob("*.py")) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
109 except StopIteration: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
110 pass |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
111 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
112 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
|
113 self.browser_modules.setdefault('brython', []).append( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
114 { |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
115 "path": browser_path, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
116 "url_hash": None, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
117 "url_prefix": f"__t_{theme}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
118 } |
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 self.uri_callbacks = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
122 self.pages_redirects = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
123 self.cached_urls = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
124 self.main_menu = None |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
125 # map Libervia application names => data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
126 self.libervia_apps = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
127 self.build_path = host.get_build_path(site_name) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
128 self.build_path.mkdir(parents=True, exist_ok=True) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
129 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
|
130 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
|
131 self.putChild( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
132 C.BUILD_DIR.encode(), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
133 ProtectedFile( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
134 self.build_path, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
135 defaultType="application/octet-stream"), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
136 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
137 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
138 def __str__(self): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
139 return ( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
140 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
|
141 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
|
142 f"files at {self.path}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
143 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
144 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
145 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
|
146 """Retrieve configuration for this site |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
147 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
148 params are the same as for [Libervia.config_get] |
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 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
|
151 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
152 def get_front_url(self, theme): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
153 return Path( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
154 '/', |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
155 C.TPL_RESOURCE, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
156 self.site_name or C.SITE_NAME_DEFAULT, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
157 C.TEMPLATE_TPL_DIR, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
158 theme) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
159 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
160 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
|
161 """Add a resource to the given path |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
162 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
163 A "NoResource" will be used for all intermediate segments |
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 segments, __, last_segment = path.rpartition("/") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
166 url_segments = segments.split("/") if segments else [] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
167 current = self |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
168 for segment in url_segments: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
169 resource = web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
170 current.putChild(segment, resource) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
171 current = resource |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
172 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
173 current.putChild( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
174 last_segment.encode('utf-8'), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
175 resource |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
176 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
177 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
178 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
|
179 """Start a Libervia App |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
180 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
181 @param app_name: canonical application name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
182 @param extra: extra parameter to configure app |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
183 @return: app data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
184 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
|
185 app to be started |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
186 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
187 if extra is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
188 extra = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
189 log.info(_( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
190 "starting application {app_name}").format(app_name=app_name)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
191 app_data = data_format.deserialise( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
192 await self.host.bridge_call( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
193 "application_start", app_name, data_format.serialise(extra) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
194 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
195 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
196 if app_data.get("started", False): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
197 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
|
198 # 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
|
199 # point |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
200 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
|
201 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
202 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
|
203 return app_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
204 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
205 async def _on_app_started( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
206 self, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
207 app_name: str, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
208 instance_id: str |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
209 ) -> None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
210 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
|
211 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
|
212 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
213 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
214 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
215 web_port = int(exposed_data['ports']['web'].split(':')[1]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
216 except (KeyError, ValueError): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
217 log.warning(_( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
218 "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
|
219 ).format(app_name=app_name)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
220 raise exceptions.DataError("no web port found") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
221 |
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 url_prefix = exposed_data['url_prefix'].strip().rstrip('/') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
224 except (KeyError, AttributeError) as e: |
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 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
|
227 .format(msg=e)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
228 raise exceptions.DataError("no URL prefix") |
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 if not url_prefix.startswith('/'): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
231 raise exceptions.DataError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
232 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
|
233 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
234 res = proxy.SatReverseProxyResource( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
235 "localhost", |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
236 web_port, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
237 url_prefix.encode() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
238 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
239 self.add_resource_to_path(url_prefix, res) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
240 log.info( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
241 f"Resource for app {app_name!r} (instance {instance_id!r}) has been added" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
242 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
243 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
244 async def _init_redirections(self, options): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
245 url_redirections = options["url_redirections_dict"] |
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 url_redirections = url_redirections.get(self.site_name, {}) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
248 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
249 ## redirections |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
250 self.redirections = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
251 self.inv_redirections = {} # new URL to old URL map |
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 for old, new_data_list in url_redirections.items(): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
254 # 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
|
255 # 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
|
256 # will be used for inverse redirection. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
257 # 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
|
258 # equivalent to a |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
259 if not isinstance(new_data_list, list): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
260 new_data_list = [new_data_list] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
261 for new_data in new_data_list: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
262 # 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
|
263 if isinstance(new_data, dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
264 # 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
|
265 # (exclusive) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
266 # 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
|
267 if (( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
268 len( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
269 {"path", "url", "page"}.intersection(list(new_data.keys())) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
270 ) != 1 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
271 )): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
272 raise ValueError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
273 '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
|
274 'key in your url_redirections_dict data' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
275 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
276 if "url" in new_data: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
277 new = new_data["url"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
278 elif "page" in new_data: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
279 new = new_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
280 new["type"] = "page" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
281 new.setdefault("path_args", []) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
282 if not isinstance(new["path_args"], list): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
283 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
284 _('"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
|
285 'Ignoring the redirection'.format(old=old))) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
286 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
287 new.setdefault("query_args", {}) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
288 if not isinstance(new["query_args"], dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
289 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
290 _( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
291 '"query_args" in redirection of {old} must be a ' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
292 'dictionary. Ignoring the redirection' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
293 ).format(old=old) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
294 ) |
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["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
|
297 # we keep an inversed dict of page redirection |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
298 # (page/path_args => redirecting URL) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
299 # 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
|
300 # are used # making the URL consistent |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
301 args_hash = tuple(new["path_args"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
302 self.pages_redirects.setdefault(new_data["page"], {}).setdefault( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
303 args_hash, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
304 old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
305 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
306 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
307 # 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
|
308 # as it in request.path_args |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
309 for k, v in new["query_args"].items(): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
310 if isinstance(v, str): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
311 new["query_args"][k] = [v] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
312 elif "path" in new_data: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
313 new = "file:{}".format(urllib.parse.quote(new_data["path"])) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
314 elif isinstance(new_data, str): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
315 new = new_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
316 new_data = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
317 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
318 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
319 _("ignoring invalid redirection value: {new_data}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
320 new_data=new_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
321 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
322 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
323 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
324 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
325 # some normalization |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
326 if not old.strip(): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
327 # root URL special case |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
328 old = "" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
329 elif not old.startswith("/"): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
330 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
331 _("redirected url must start with '/', got {value}. Ignoring") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
332 .format(value=old) |
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 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
335 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
336 old = self._normalize_url(old) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
337 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
338 if isinstance(new, dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
339 # dict are handled differently, they contain data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
340 # which ared use dynamically when the request is done |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
341 self.redirections.setdefault(old, new) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
342 if not old: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
343 if new["type"] == "page": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
344 log.info( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
345 _("Root URL redirected to page {name}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
346 name=new["page"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
347 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
348 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
349 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
350 if new["type"] == "page": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
351 page = self.get_page_by_name(new["page"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
352 url = page.get_url(*new.get("path_args", [])) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
353 self.inv_redirections[url] = old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
354 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
355 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
356 # 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
|
357 new_url = urllib.parse.urlsplit(new) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
358 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
359 # we handle the known URL schemes |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
360 if new_url.scheme == "xmpp": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
361 location = self.get_page_path_from_uri(new) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
362 if location is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
363 log.warning( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
364 _("ignoring redirection, no page found to handle this URI: " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
365 "{uri}").format(uri=new)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
366 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
367 request_data = self._get_request_data(location) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
368 self.inv_redirections[location] = old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
369 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
370 elif new_url.scheme in ("", "http", "https"): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
371 # direct redirection |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
372 if new_url.netloc: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
373 raise NotImplementedError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
374 "netloc ({netloc}) is not implemented yet for " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
375 "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
|
376 "external website".format(netloc=new_url.netloc)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
377 location = urllib.parse.urlunsplit( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
378 ("", "", new_url.path, new_url.query, new_url.fragment) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
379 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
380 request_data = self._get_request_data(location) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
381 self.inv_redirections[location] = old |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
382 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
383 elif new_url.scheme == "file": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
384 # file or directory |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
385 if new_url.netloc: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
386 raise NotImplementedError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
387 "netloc ({netloc}) is not implemented for url redirection to " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
388 "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
|
389 "host".format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
390 netloc=new_url.netloc)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
391 path = urllib.parse.unquote(new_url.path) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
392 if not os.path.isabs(path): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
393 raise ValueError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
394 "file redirection must have an absolute path: e.g. " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
395 "file:/path/to/my/file") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
396 # for file redirection, we directly put child here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
397 resource_class = ( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
398 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
|
399 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
400 res = resource_class(path, defaultType="application/octet-stream") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
401 self.add_resource_to_path(old, res) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
402 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
|
403 "path {path}".format(host_name=self.host_name, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
404 old=old, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
405 path=path)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
406 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
407 # 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
|
408 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
409 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
410 elif new_url.scheme == "libervia-app": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
411 # a Libervia application |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
412 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
413 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
|
414 extra = {"url_prefix": f"/{old}"} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
415 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
416 await self._start_app(app_name, extra) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
417 except Exception as e: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
418 log.warning(_( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
419 "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
|
420 app_name=app_name, old=old, e=e)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
421 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
422 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
423 log.info( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
424 f"[{self.host_name}] Added redirection from /{old} to " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
425 f"application {app_name}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
426 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
427 # normal redirection system is not used here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
428 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
429 elif new_url.scheme == "proxy": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
430 # a reverse proxy |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
431 host, port = new_url.hostname, new_url.port |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
432 if host is None or port is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
433 raise ValueError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
434 "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
|
435 "configuration: {new_url.geturl()}" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
436 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
437 url_prefix = (new_url.path or old).rstrip('/') |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
438 res = proxy.SatReverseProxyResource( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
439 host, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
440 port, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
441 url_prefix.encode(), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
442 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
443 self.add_resource_to_path(old, res) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
444 log.info( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
445 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
|
446 f"{new_url.netloc} with URL prefix {url_prefix}/" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
447 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
448 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
449 # normal redirection system is not used here |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
450 continue |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
451 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
452 raise NotImplementedError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
453 "{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
|
454 scheme=new_url.scheme |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
455 ) |
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 self.redirections.setdefault(old, request_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
459 if not old: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
460 log.info(_("[{host_name}] Root URL redirected to {uri}") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
461 .format(host_name=self.host_name, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
462 uri=request_data[1])) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
463 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
464 # the default root URL, if not redirected |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
465 if not "" in self.redirections: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
466 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
|
467 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
468 async def _set_menu(self, menus): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
469 menus = menus.get(self.site_name, []) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
470 main_menu = [] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
471 for menu in menus: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
472 if not menu: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
473 msg = _("menu item can't be empty") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
474 log.error(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
475 raise ValueError(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
476 elif isinstance(menu, list): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
477 if len(menu) != 2: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
478 msg = _( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
479 "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
|
480 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
481 log.error(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
482 raise ValueError(msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
483 page_name, url = menu |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
484 elif menu.startswith("libervia-app:"): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
485 app_name = menu[13:].strip().lower() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
486 app_data = await self._start_app(app_name) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
487 exposed_data = app_data["expose"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
488 front_url = exposed_data['front_url'] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
489 options = self.host.options |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
490 url_redirections = options["url_redirections_dict"].setdefault( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
491 self.site_name, {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
492 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
493 if front_url in url_redirections: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
494 raise exceptions.ConflictError( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
495 f"There is already a redirection from {front_url!r}, can't add " |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
496 f"{app_name!r}") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
497 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
498 url_redirections[front_url] = { |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
499 "page": 'embed_app', |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
500 "path_args": [app_name] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
501 } |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
502 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
503 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
|
504 url = front_url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
505 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
506 log.debug( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
507 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
|
508 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
509 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
510 page_name = menu |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
511 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
512 url = self.get_page_by_name(page_name).url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
513 except KeyError as e: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
514 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
|
515 "menu_json in configuration.").format(msg=e.args[0]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
516 log.error(log_msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
517 raise exceptions.ConfigError(log_msg) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
518 main_menu.append((page_name, url)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
519 self.main_menu = main_menu |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
520 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
521 def _normalize_url(self, url, lower=True): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
522 """Return URL normalized for self.redirections dict |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
523 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
524 @param url(unicode): URL to normalize |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
525 @param lower(bool): lower case of url if True |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
526 @return (str): normalized 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 if lower: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
529 url = url.lower() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
530 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
|
531 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
532 def _get_request_data(self, uri): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
533 """Return data needed to redirect request |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
534 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
535 @param url(unicode): destination url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
536 @return (tuple(list[str], str, str, dict): tuple with |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
537 splitted path as in Request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
538 uri as in Request.uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
539 path as in Request.path |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
540 args as in Request.args |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
541 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
542 uri = uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
543 # 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
|
544 # as we need to have the same behaviour |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
545 x = uri.split("?", 1) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
546 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
547 if len(x) == 1: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
548 path = uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
549 args = {} |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
550 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
551 path, argstring = x |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
552 args = urllib.parse.parse_qs(argstring, True) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
553 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
554 # 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
|
555 # (e.g. for blog items) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
556 return ( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
557 self._normalize_url(path, lower=False).split("/"), |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
558 uri, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
559 path, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
560 args, |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
561 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
562 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
563 def _redirect(self, request, request_data): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
564 """Redirect an URL by rewritting request |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
565 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
566 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
|
567 @param request(web.http.request): original request |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
568 @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
|
569 @return (web_resource.Resource): resource to use |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
570 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
571 # recursion check |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
572 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
573 request._redirected |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
574 except AttributeError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
575 pass |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
576 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
577 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
578 __, uri, __, __ = request_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
579 except ValueError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
580 uri = "" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
581 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
|
582 "{old} ==> {new}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
583 old=request.uri.decode("utf-8"), new=uri)) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
584 return web_resource.NoResource() |
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 request._redirected = True # here to avoid recursive redirections |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
587 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
588 if isinstance(request_data, dict): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
589 if request_data["type"] == "page": |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
590 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
591 page = self.get_page_by_name(request_data["page"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
592 except KeyError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
593 log.error( |
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 'Can\'t find page named "{name}" requested in redirection' |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
596 ).format(name=request_data["page"]) |
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 return web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
599 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
|
600 request.postpath = path_args + request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
601 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
602 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
603 request.args.update(request_data["query_args"]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
604 except (TypeError, ValueError): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
605 log.error( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
606 _("Invalid args in redirection: {query_args}").format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
607 query_args=request_data["query_args"] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
608 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
609 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
610 return web_resource.NoResource() |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
611 return page |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
612 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
613 raise exceptions.InternalError("unknown request_data type") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
614 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
615 path_list, uri, path, args = request_data |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
616 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
|
617 log.debug( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
618 "Redirecting URL {old} to {new}".format( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
619 old=request.uri.decode('utf-8'), new=uri |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
620 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
621 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
622 # we change the request to reflect the new url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
623 request.postpath = path_list[1:] + request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
624 request.args.update(args) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
625 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
626 # 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
|
627 return self.getChildWithDefault(path_list[0], request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
628 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
629 def get_page_by_name(self, name): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
630 """Retrieve page instance from its name |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
631 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
632 @param name(unicode): name of the page |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
633 @return (LiberviaPage): page instance |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
634 @raise KeyError: the page doesn't exist |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
635 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
636 return self.named_pages[name] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
637 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
638 def get_page_path_from_uri(self, uri): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
639 """Retrieve page URL from xmpp: URI |
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 @param uri(unicode): URI with a xmpp: scheme |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
642 @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
|
643 the URI. |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
644 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
|
645 """ |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
646 uri_data = common_uri.parse_xmpp_uri(uri) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
647 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
648 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
|
649 except KeyError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
650 url = None |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
651 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
652 url = cb(page, uri_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
653 if url is None: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
654 # no handler found |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
655 # we try to find a more generic one |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
656 try: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
657 page, cb = self.uri_callbacks[uri_data["type"], None] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
658 except KeyError: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
659 pass |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
660 else: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
661 url = cb(page, uri_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
662 return url |
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 def getChildWithDefault(self, name, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
665 # XXX: this method is overriden only for root url |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
666 # 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
|
667 if name == b"" and not request.postpath: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
668 return self._redirect(request, self.redirections[""]) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
669 return super(LiberviaRootResource, self).getChildWithDefault(name, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
670 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
671 def getChild(self, name, request): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
672 resource = super(LiberviaRootResource, self).getChild(name, request) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
673 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
674 if isinstance(resource, web_resource.NoResource): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
675 # 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
|
676 # 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
|
677 path_elt = request.prepath + request.postpath |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
678 for idx in range(len(path_elt), -1, -1): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
679 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
|
680 if test_url in self.redirections: |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
681 request_data = self.redirections[test_url] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
682 request.postpath = path_elt[idx:] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
683 return self._redirect(request, request_data) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
684 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
685 return resource |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
686 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
687 def putChild(self, path, resource): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
688 """Add a child to the root resource""" |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
689 if not isinstance(path, bytes): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
690 raise ValueError("path must be specified in bytes") |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
691 if not isinstance(resource, web_resource.EncodingResourceWrapper): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
692 # 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
|
693 resource = web_resource.EncodingResourceWrapper( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
694 resource, [server.GzipEncoderFactory()]) |
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 super(LiberviaRootResource, self).putChild(path, resource) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
697 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
698 def createSimilarFile(self, path): |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
699 # 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
|
700 |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
701 f = LiberviaRootResource.__base__( |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
702 path, self.defaultType, self.ignoredExts, self.registry |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
703 ) |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
704 # 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
|
705 f.processors = self.processors |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
706 f.indexNames = self.indexNames[:] |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
707 f.childNotFound = self.childNotFound |
65e063657597
server: move resources to a dedicated module
Goffi <goffi@goffi.org>
parents:
diff
changeset
|
708 return f |