changeset 1038:6b906b1f419a

pages: fixed XMPP URIs handling
author Goffi <goffi@goffi.org>
date Wed, 24 Jan 2018 09:57:57 +0100
parents dc6c8c4d8ff6
children 6bc7768faa5d
files src/pages/common/blog/page_meta.py src/server/pages.py src/server/server.py
diffstat 3 files changed, 30 insertions(+), 30 deletions(-) [+]
line wrap: on
line diff
--- a/src/pages/common/blog/page_meta.py	Wed Jan 24 09:57:38 2018 +0100
+++ b/src/pages/common/blog/page_meta.py	Wed Jan 24 09:57:57 2018 +0100
@@ -10,7 +10,6 @@
 from sat.tools.common.template import safe
 from server import utils
 import re
-import urllib
 import cgi
 log = getLogger('pages/common/blog')
 
@@ -23,10 +22,10 @@
 TEXT_MAX_LEN = 60
 
 def microblog_uri(self, uri_data):
-    service = urllib.quote_plus(uri_data[u'path'])
-    node = urllib.quote_plus(uri_data[u'node'])
-    return service + u'/' + node
-
+    args = [uri_data[u'path'], uri_data[u'node']]
+    if u'item' in uri_data:
+        args.extend([u'id', uri_data[u'item']])
+    return self.getURL(*args)
 
 def parse_url(self, request):
     """URL is /[service]/[node]/[filter_keyword]/[item]|[other]
--- a/src/server/pages.py	Wed Jan 24 09:57:38 2018 +0100
+++ b/src/server/pages.py	Wed Jan 24 09:57:57 2018 +0100
@@ -282,7 +282,7 @@
                                     name = cb_name, *uri_tuple))
                                 continue
                             else:
-                                cls.registerURI(uri_tuple, cb, new_path)
+                                resource.registerURI(uri_tuple, cb)
 
                 LiberviaPage.importPages(host, resource, new_path)
 
@@ -310,20 +310,19 @@
             main_menu.append((page_name, url))
         cls.main_menu = main_menu
 
-    @classmethod
-    def registerURI(cls, uri_tuple, get_uri_cb, pre_path):
+    def registerURI(self, uri_tuple, get_uri_cb):
         """register a URI handler
 
         @param uri_tuple(tuple[unicode, unicode]): type or URIs handler
             type/subtype as returned by tools/common/parseXMPPUri
+            or type/None to handle all subtypes
         @param get_uri_cb(callable): method which take uri_data dict as only argument
-            and return path with correct arguments relative to page itself
-        @param pre_path(list[unicode]): prefix path to reference the handler page
+            and return absolute path with correct arguments or None if the page
+            can't handle this URL
         """
-        if uri_tuple in cls.uri_callbacks:
+        if uri_tuple in self.uri_callbacks:
             log.info(_(u"{}/{} URIs are already handled, replacing by the new handler").format(*uri_tuple))
-        cls.uri_callbacks[uri_tuple] = {u'callback': get_uri_cb,
-                                        u'pre_path': pre_path}
+        self.uri_callbacks[uri_tuple] = (self, get_uri_cb)
 
     def registerSignal(self, request, signal, check_profile=True):
         r"""register a signal handler
@@ -348,20 +347,30 @@
         LiberviaPage.signals_handlers.setdefault(signal, {})[id(request)] = (self, request, check_profile)
         request._signals_registered.append(signal)
 
-    def getPagePathFromURI(self, uri):
+    @classmethod
+    def getPagePathFromURI(cls, uri):
         """Retrieve page URL from xmpp: URI
 
         @param uri(unicode): URI with a xmpp: scheme
         @return (unicode,None): absolute path (starting from root "/") to page handling the URI
-            None is returned if not page has been registered for this URI
+            None is returned if no page has been registered for this URI
         """
         uri_data = common_uri.parseXMPPUri(uri)
         try:
-            callback_data = self.uri_callbacks[uri_data['type'], uri_data.get('sub_type')]
+            page, cb = cls.uri_callbacks[uri_data['type'], uri_data['sub_type']]
         except KeyError:
-            return
+            url = None
         else:
-            url = os.path.join(u'/', u'/'.join(callback_data['pre_path']), callback_data['callback'](self, uri_data))
+            url = cb(page, uri_data)
+        if url is None:
+            # no handler found
+            # we try to find a more generic one
+            try:
+                page, cb = cls.uri_callbacks[uri_data['type'], None]
+            except KeyError:
+                pass
+            else:
+                url = cb(page, uri_data)
         return url
 
     @classmethod
--- a/src/server/server.py	Wed Jan 24 09:57:38 2018 +0100
+++ b/src/server/server.py	Wed Jan 24 09:57:57 2018 +0100
@@ -190,18 +190,10 @@
 
             # we handle the known URL schemes
             if new_url.scheme == 'xmpp':
-                # XMPP URI
-                parsed_qs = urlparse.parse_qs(new_url.geturl())
-                try:
-                    item = parsed_qs['item'][0]
-                    if not item:
-                        raise KeyError
-                except (IndexError, KeyError):
-                    raise NotImplementedError(u"only item for PubSub URI is handled for the moment for url_redirections_dict")
-                location = "/blog/{profile}/{item}".format(
-                    profile=quote(options['url_redirections_profile']),
-                    item = urllib.quote_plus(item),
-                    ).decode('utf-8')
+                location = LiberviaPage.getPagePathFromURI(new)
+                if location is None:
+                    log.warning(_(u"ignoring redirection, no page found to handle this URI: {uri}").format(uri=new))
+                    continue
                 request_data = self._getRequestData(location)
                 if old:
                     self.inv_redirections[location] = old