mirror of
https://github.com/Tautulli/Tautulli.git
synced 2025-07-06 05:01:14 -07:00
Update websocket-client-1.2.1
This commit is contained in:
parent
c67f18d65c
commit
aa127ecbda
21 changed files with 1756 additions and 1045 deletions
|
@ -1,57 +1,34 @@
|
|||
"""
|
||||
_handshake.py
|
||||
websocket - WebSocket client library for Python
|
||||
|
||||
Copyright (C) 2010 Hiroki Ohtani(liris)
|
||||
Copyright 2021 engn33r
|
||||
|
||||
This library is free software; you can redistribute it and/or
|
||||
modify it under the terms of the GNU Lesser General Public
|
||||
License as published by the Free Software Foundation; either
|
||||
version 2.1 of the License, or (at your option) any later version.
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
This library is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
Lesser General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU Lesser General Public
|
||||
License along with this library; if not, write to the Free Software
|
||||
Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
||||
Boston, MA 02110-1335 USA
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
"""
|
||||
import hashlib
|
||||
import hmac
|
||||
import os
|
||||
|
||||
import six
|
||||
|
||||
from base64 import encodebytes as base64encode
|
||||
from http import client as HTTPStatus
|
||||
from ._cookiejar import SimpleCookieJar
|
||||
from ._exceptions import *
|
||||
from ._http import *
|
||||
from ._logging import *
|
||||
from ._socket import *
|
||||
|
||||
if hasattr(six, 'PY3') and six.PY3:
|
||||
from base64 import encodebytes as base64encode
|
||||
else:
|
||||
from base64 import encodestring as base64encode
|
||||
|
||||
if hasattr(six, 'PY3') and six.PY3:
|
||||
if hasattr(six, 'PY34') and six.PY34:
|
||||
from http import client as HTTPStatus
|
||||
else:
|
||||
from http import HTTPStatus
|
||||
else:
|
||||
import httplib as HTTPStatus
|
||||
|
||||
__all__ = ["handshake_response", "handshake", "SUPPORTED_REDIRECT_STATUSES"]
|
||||
|
||||
if hasattr(hmac, "compare_digest"):
|
||||
compare_digest = hmac.compare_digest
|
||||
else:
|
||||
def compare_digest(s1, s2):
|
||||
return s1 == s2
|
||||
|
||||
# websocket supported version.
|
||||
VERSION = 13
|
||||
|
||||
|
@ -94,6 +71,7 @@ def _pack_hostname(hostname):
|
|||
|
||||
return hostname
|
||||
|
||||
|
||||
def _get_handshake_headers(resource, host, port, options):
|
||||
headers = [
|
||||
"GET %s HTTP/1.1" % resource,
|
||||
|
@ -115,19 +93,19 @@ def _get_handshake_headers(resource, host, port, options):
|
|||
headers.append("Origin: http://%s" % hostport)
|
||||
|
||||
key = _create_sec_websocket_key()
|
||||
|
||||
|
||||
# Append Sec-WebSocket-Key & Sec-WebSocket-Version if not manually specified
|
||||
if not 'header' in options or 'Sec-WebSocket-Key' not in options['header']:
|
||||
if 'header' not in options or 'Sec-WebSocket-Key' not in options['header']:
|
||||
key = _create_sec_websocket_key()
|
||||
headers.append("Sec-WebSocket-Key: %s" % key)
|
||||
else:
|
||||
key = options['header']['Sec-WebSocket-Key']
|
||||
|
||||
if not 'header' in options or 'Sec-WebSocket-Version' not in options['header']:
|
||||
if 'header' not in options or 'Sec-WebSocket-Version' not in options['header']:
|
||||
headers.append("Sec-WebSocket-Version: %s" % VERSION)
|
||||
|
||||
if not 'connection' in options or options['connection'] is None:
|
||||
headers.append('Connection: upgrade')
|
||||
if 'connection' not in options or options['connection'] is None:
|
||||
headers.append('Connection: Upgrade')
|
||||
else:
|
||||
headers.append(options['connection'])
|
||||
|
||||
|
@ -178,27 +156,28 @@ def _validate(headers, key, subprotocols):
|
|||
r = headers.get(k, None)
|
||||
if not r:
|
||||
return False, None
|
||||
r = r.lower()
|
||||
if v != r:
|
||||
r = [x.strip().lower() for x in r.split(',')]
|
||||
if v not in r:
|
||||
return False, None
|
||||
|
||||
if subprotocols:
|
||||
subproto = headers.get("sec-websocket-protocol", None).lower()
|
||||
if not subproto or subproto not in [s.lower() for s in subprotocols]:
|
||||
subproto = headers.get("sec-websocket-protocol", None)
|
||||
if not subproto or subproto.lower() not in [s.lower() for s in subprotocols]:
|
||||
error("Invalid subprotocol: " + str(subprotocols))
|
||||
return False, None
|
||||
subproto = subproto.lower()
|
||||
|
||||
result = headers.get("sec-websocket-accept", None)
|
||||
if not result:
|
||||
return False, None
|
||||
result = result.lower()
|
||||
|
||||
if isinstance(result, six.text_type):
|
||||
if isinstance(result, str):
|
||||
result = result.encode('utf-8')
|
||||
|
||||
value = (key + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11").encode('utf-8')
|
||||
hashed = base64encode(hashlib.sha1(value).digest()).strip().lower()
|
||||
success = compare_digest(hashed, result)
|
||||
success = hmac.compare_digest(hashed, result)
|
||||
|
||||
if success:
|
||||
return True, subproto
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue