You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Enso-Bot/venv/Lib/site-packages/websockets/uri.py

56 lines
1.5 KiB
Python

4 years ago
"""
The :mod:`websockets.uri` module implements parsing of WebSocket URIs
according to `section 3 of RFC 6455`_.
4 years ago
.. _section 3 of RFC 6455: http://tools.ietf.org/html/rfc6455#section-3
"""
import collections
4 years ago
import urllib.parse
from .exceptions import InvalidURI
__all__ = ['parse_uri', 'WebSocketURI']
4 years ago
WebSocketURI = collections.namedtuple(
'WebSocketURI', ('secure', 'host', 'port', 'resource_name'))
WebSocketURI.__doc__ = """WebSocket URI.
4 years ago
* ``secure`` is the secure flag
* ``host`` is the lower-case host
* ``port`` if the integer port, it's always provided even if it's the default
* ``resource_name`` is the resource name, that is, the path and optional query
4 years ago
"""
4 years ago
def parse_uri(uri):
4 years ago
"""
This function parses and validates a WebSocket URI.
4 years ago
If the URI is valid, it returns a :class:`WebSocketURI`.
4 years ago
Otherwise it raises an :exc:`~websockets.exceptions.InvalidURI` exception.
4 years ago
"""
uri = urllib.parse.urlparse(uri)
4 years ago
try:
assert uri.scheme in ('ws', 'wss')
assert uri.params == ''
assert uri.fragment == ''
assert uri.username is None
assert uri.password is None
assert uri.hostname is not None
4 years ago
except AssertionError as exc:
raise InvalidURI() from exc
secure = uri.scheme == 'wss'
host = uri.hostname
port = uri.port or (443 if secure else 80)
resource_name = uri.path or '/'
if uri.query:
resource_name += '?' + uri.query
return WebSocketURI(secure, host, port, resource_name)