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.

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. # The Python urlparse module currently does not contain the rs/rss
  28. # schemes, so we add those dynamically (which is a hack of course).
  29. #
  30. # Important: if you change this stuff (you shouldn't), make sure
  31. # _all_ our unit tests for WS URLs succeed
  32. #
  33. from urllib import parse as urlparse
  34. wsschemes = ["rs", "rss"]
  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):
  46. """
  47. Create a RawSocket URL from components.
  48. :param hostname: RawSocket 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, RawSocket 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 RawSocket (``rss`` scheme).
  56. :type isSecure: bool
  57. :returns: Constructed RawSocket URL.
  58. :rtype: str
  59. """
  60. # assert type(hostname) == str
  61. assert type(isSecure) == bool
  62. if hostname == 'unix':
  63. netloc = "unix:%s" % port
  64. else:
  65. assert port is None or (type(port) == int and port in range(0, 65535))
  66. if port is not None:
  67. netloc = "%s:%d" % (hostname, port)
  68. else:
  69. if isSecure:
  70. netloc = "{}:443".format(hostname)
  71. else:
  72. netloc = "{}:80".format(hostname)
  73. if isSecure:
  74. scheme = "rss"
  75. else:
  76. scheme = "rs"
  77. return "{}://{}".format(scheme, netloc)
  78. @public
  79. def parse_url(url):
  80. """
  81. Parses as RawSocket URL into it's components and returns a tuple:
  82. - ``isSecure`` is a flag which is ``True`` for ``rss`` URLs.
  83. - ``host`` is the hostname or IP from the URL.
  84. and for TCP/IP sockets:
  85. - ``tcp_port`` is the port from the URL or standard port derived from
  86. scheme (``rs`` => ``80``, ``rss`` => ``443``).
  87. or for Unix domain sockets:
  88. - ``uds_path`` is the path on the local host filesystem.
  89. :param url: A valid RawSocket URL, i.e. ``rs://localhost:9000`` for TCP/IP sockets or
  90. ``rs://unix:/tmp/file.sock`` for Unix domain sockets (UDS).
  91. :type url: str
  92. :returns: A 3-tuple ``(isSecure, host, tcp_port)`` (TCP/IP) or ``(isSecure, host, uds_path)`` (UDS).
  93. :rtype: tuple
  94. """
  95. parsed = urlparse.urlparse(url)
  96. if parsed.scheme not in ["rs", "rss"]:
  97. raise Exception("invalid RawSocket URL: protocol scheme '{}' is not for RawSocket".format(parsed.scheme))
  98. if not parsed.hostname or parsed.hostname == "":
  99. raise Exception("invalid RawSocket URL: missing hostname")
  100. if parsed.query is not None and parsed.query != "":
  101. raise Exception("invalid RawSocket URL: non-empty query '{}'".format(parsed.query))
  102. if parsed.fragment is not None and parsed.fragment != "":
  103. raise Exception("invalid RawSocket URL: non-empty fragment '{}'".format(parsed.fragment))
  104. if parsed.hostname == "unix":
  105. # Unix domain sockets sockets
  106. # rs://unix:/tmp/file.sock => unix:/tmp/file.sock => /tmp/file.sock
  107. fp = parsed.netloc + parsed.path
  108. uds_path = fp.split(':')[1]
  109. # note: we don't interpret "uds_path" in any further way: it needs to be
  110. # a path on the local host with a listening Unix domain sockets at the other end ..
  111. return parsed.scheme == "rss", parsed.hostname, uds_path
  112. else:
  113. # TCP/IP sockets
  114. if parsed.path is not None and parsed.path != "":
  115. raise Exception("invalid RawSocket URL: non-empty path '{}'".format(parsed.path))
  116. if parsed.port is None or parsed.port == "":
  117. if parsed.scheme == "rs":
  118. tcp_port = 80
  119. else:
  120. tcp_port = 443
  121. else:
  122. tcp_port = int(parsed.port)
  123. if tcp_port < 1 or tcp_port > 65535:
  124. raise Exception("invalid port {}".format(tcp_port))
  125. return parsed.scheme == "rss", parsed.hostname, tcp_port