annotate libervia/web/server/resources.py @ 1609:f3305832f3f6

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