Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.py 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ###############################################################################
  2. #
  3. # The MIT License (MIT)
  4. #
  5. # Copyright (c) typedef int GmbH
  6. #
  7. # Permission is hereby granted, free of charge, to any person obtaining a copy
  8. # of this software and associated documentation files (the "Software"), to deal
  9. # in the Software without restriction, including without limitation the rights
  10. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. # copies of the Software, and to permit persons to whom the Software is
  12. # furnished to do so, subject to the following conditions:
  13. #
  14. # The above copyright notice and this permission notice shall be included in
  15. # all copies or substantial portions of the Software.
  16. #
  17. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. # THE SOFTWARE.
  24. #
  25. ###############################################################################
  26. from autobahn.util import public
  27. from urllib import parse as urlparse
  28. # The Python urlparse module currently does not contain the ws/wss
  29. # schemes, so we add those dynamically (which is a hack of course).
  30. #
  31. # Important: if you change this stuff (you shouldn't), make sure
  32. # _all_ our unit tests for WS URLs succeed
  33. #
  34. wsschemes = ["ws", "wss"]
  35. urlparse.uses_relative.extend(wsschemes)
  36. urlparse.uses_netloc.extend(wsschemes)
  37. urlparse.uses_params.extend(wsschemes)
  38. urlparse.uses_query.extend(wsschemes)
  39. urlparse.uses_fragment.extend(wsschemes)
  40. __all__ = (
  41. "create_url",
  42. "parse_url",
  43. )
  44. @public
  45. def create_url(hostname, port=None, isSecure=False, path=None, params=None):
  46. """
  47. Create a WebSocket URL from components.
  48. :param hostname: WebSocket server hostname (for TCP/IP sockets) or
  49. filesystem path (for Unix domain sockets).
  50. :type hostname: str
  51. :param port: For TCP/IP sockets, WebSocket service port or ``None`` (to select default
  52. ports ``80`` or ``443`` depending on ``isSecure``. When ``hostname=="unix"``,
  53. this defines the path to the Unix domain socket instead of a TCP/IP network socket.
  54. :type port: int or str
  55. :param isSecure: Set ``True`` for secure WebSocket (``wss`` scheme).
  56. :type isSecure: bool
  57. :param path: WebSocket URL path of addressed resource (will be
  58. properly URL escaped). Ignored for RawSocket.
  59. :type path: str
  60. :param params: A dictionary of key-values to construct the query
  61. component of the addressed WebSocket resource (will be properly URL
  62. escaped). Ignored for RawSocket.
  63. :type params: dict
  64. :returns: Constructed WebSocket URL.
  65. :rtype: str
  66. """
  67. # assert type(hostname) == str
  68. assert type(isSecure) == bool
  69. if hostname == 'unix':
  70. netloc = "unix:%s" % port
  71. else:
  72. assert port is None or (type(port) == int and port in range(0, 65535))
  73. if port is not None:
  74. netloc = "%s:%d" % (hostname, port)
  75. else:
  76. if isSecure:
  77. netloc = "%s:443" % hostname
  78. else:
  79. netloc = "%s:80" % hostname
  80. if isSecure:
  81. scheme = "wss"
  82. else:
  83. scheme = "ws"
  84. if path is not None:
  85. ppath = urlparse.quote(path)
  86. else:
  87. ppath = "/"
  88. if params is not None:
  89. query = urlparse.urlencode(params)
  90. else:
  91. query = None
  92. return urlparse.urlunparse((scheme, netloc, ppath, None, query, None))
  93. @public
  94. def parse_url(url):
  95. """
  96. Parses as WebSocket URL into it's components and returns a tuple:
  97. - ``isSecure`` is a flag which is ``True`` for ``wss`` URLs.
  98. - ``host`` is the hostname or IP from the URL.
  99. and for TCP/IP sockets:
  100. - ``tcp_port`` is the port from the URL or standard port derived from
  101. scheme (``rs`` => ``80``, ``rss`` => ``443``).
  102. or for Unix domain sockets:
  103. - ``uds_path`` is the path on the local host filesystem.
  104. :param url: A valid WebSocket URL, i.e. ``ws://localhost:9000`` for TCP/IP sockets or
  105. ``ws://unix:/tmp/file.sock`` for Unix domain sockets (UDS).
  106. :type url: str
  107. :returns: A 6-tuple ``(isSecure, host, tcp_port, resource, path, params)`` (TCP/IP) or
  108. ``(isSecure, host, uds_path, resource, path, params)`` (UDS).
  109. :rtype: tuple
  110. """
  111. parsed = urlparse.urlparse(url)
  112. if parsed.scheme not in ["ws", "wss"]:
  113. raise ValueError("invalid WebSocket URL: protocol scheme '{}' is not for WebSocket".format(parsed.scheme))
  114. if not parsed.hostname or parsed.hostname == "":
  115. raise ValueError("invalid WebSocket URL: missing hostname")
  116. if parsed.fragment is not None and parsed.fragment != "":
  117. raise ValueError("invalid WebSocket URL: non-empty fragment '%s" % parsed.fragment)
  118. if parsed.path is not None and parsed.path != "":
  119. ppath = parsed.path
  120. path = urlparse.unquote(ppath)
  121. else:
  122. ppath = "/"
  123. path = ppath
  124. if parsed.query is not None and parsed.query != "":
  125. resource = ppath + "?" + parsed.query
  126. params = urlparse.parse_qs(parsed.query)
  127. else:
  128. resource = ppath
  129. params = {}
  130. if parsed.hostname == "unix":
  131. # Unix domain sockets sockets
  132. # ws://unix:/tmp/file.sock => unix:/tmp/file.sock => /tmp/file.sock
  133. fp = parsed.netloc + parsed.path
  134. uds_path = fp.split(':')[1]
  135. # note: we don't interpret "path" in any further way: it needs to be
  136. # a path on the local host with a listening Unix domain sockets at the other end ..
  137. return parsed.scheme == "wss", parsed.hostname, uds_path, resource, path, params
  138. else:
  139. # TCP/IP sockets
  140. if parsed.port is None or parsed.port == "":
  141. if parsed.scheme == "ws":
  142. tcp_port = 80
  143. else:
  144. tcp_port = 443
  145. else:
  146. tcp_port = int(parsed.port)
  147. if tcp_port < 1 or tcp_port > 65535:
  148. raise ValueError("invalid port {}".format(tcp_port))
  149. return parsed.scheme == "wss", parsed.hostname, tcp_port, resource, path, params