tools (common/uri): accept URIs without type:

URIs without type are used to link JIDs.

rel 369
This commit is contained in:
Goffi 2022-07-09 16:30:37 +02:00
commit 70cfacfe1a

View file

@ -19,6 +19,7 @@
""" XMPP uri parsing tools """ """ XMPP uri parsing tools """
from typing import Optional
import sys import sys
import urllib.parse import urllib.parse
import urllib.request, urllib.parse, urllib.error import urllib.request, urllib.parse, urllib.error
@ -50,9 +51,13 @@ def parseXMPPUri(uri):
data = {"path": urllib.parse.unquote(uri_split.path)} data = {"path": urllib.parse.unquote(uri_split.path)}
query_end = uri_split.query.find(";") query_end = uri_split.query.find(";")
query_type = uri_split.query[:query_end] if query_end == -1:
if query_end == -1 or "=" in query_type: # we just have a JID
raise ValueError("no query type, invalid XMPP URI") query_type = None
else:
query_type = uri_split.query[:query_end]
if "=" in query_type:
raise ValueError("no query type, invalid XMPP URI")
if sys.version_info >= (3, 9): if sys.version_info >= (3, 9):
# parse_qs behaviour has been modified in Python 3.9, ";" is not understood as a # parse_qs behaviour has been modified in Python 3.9, ";" is not understood as a
@ -92,13 +97,19 @@ def addPairs(uri, pairs):
) )
def buildXMPPUri(type_, **kwargs): def buildXMPPUri(type_: Optional[str] = None, **kwargs: str) -> str:
uri = ["xmpp:"] uri = ["xmpp:"]
subtype = kwargs.pop("subtype", None) subtype = kwargs.pop("subtype", None)
path = kwargs.pop("path") path = kwargs.pop("path")
uri.append(urllib.parse.quote_plus(path.encode("utf-8")).replace("%40", "@")) uri.append(urllib.parse.quote_plus(path.encode("utf-8")).replace("%40", "@"))
if type_ == "pubsub": if type_ is None:
# we have an URI to a JID
if kwargs:
raise NotImplementedError(
"keyword arguments are not supported for URI without type"
)
elif type_ == "pubsub":
if subtype == "microblog" and not kwargs.get("node"): if subtype == "microblog" and not kwargs.get("node"):
kwargs["node"] = "urn:xmpp:microblog:0" kwargs["node"] = "urn:xmpp:microblog:0"
if kwargs: if kwargs: