mirror of
https://github.com/jellyfin/jellyfin-kodi.git
synced 2024-12-27 03:06:10 +00:00
[websocket] Remove deprecated base64.encodestring
Clean up _validate_header a little bit
This commit is contained in:
parent
b2aa57aafa
commit
9971940cac
1 changed files with 9 additions and 22 deletions
|
@ -45,7 +45,7 @@ import threading
|
||||||
import time
|
import time
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from six import text_type, string_types, iteritems, int2byte, indexbytes
|
from six import text_type, string_types, int2byte, indexbytes
|
||||||
from six.moves import range
|
from six.moves import range
|
||||||
from six.moves.urllib.parse import urlparse
|
from six.moves.urllib.parse import urlparse
|
||||||
|
|
||||||
|
@ -239,13 +239,7 @@ _MAX_CHAR_BYTE = (1 << 8) - 1
|
||||||
|
|
||||||
def _create_sec_websocket_key():
|
def _create_sec_websocket_key():
|
||||||
uid = uuid.uuid4()
|
uid = uuid.uuid4()
|
||||||
return base64.encodestring(uid.bytes).strip()
|
return base64.b64encode(uid.bytes).strip()
|
||||||
|
|
||||||
|
|
||||||
_HEADERS_TO_CHECK = {
|
|
||||||
"upgrade": "websocket",
|
|
||||||
"connection": "upgrade",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class ABNF(object):
|
class ABNF(object):
|
||||||
|
@ -536,24 +530,17 @@ class WebSocket(object):
|
||||||
self.connected = True
|
self.connected = True
|
||||||
|
|
||||||
def _validate_header(self, headers, key):
|
def _validate_header(self, headers, key):
|
||||||
for k, v in iteritems(_HEADERS_TO_CHECK):
|
if headers.get("connection", "").lower() != "upgrade" or headers.get("upgrade", "").lower() != "websocket":
|
||||||
r = headers.get(k, None)
|
return False
|
||||||
if not r:
|
|
||||||
return False
|
|
||||||
r = r.lower()
|
|
||||||
if v != r:
|
|
||||||
return False
|
|
||||||
|
|
||||||
result = headers.get("sec-websocket-accept", None)
|
result = headers.get("sec-websocket-accept")
|
||||||
if not result:
|
if not result:
|
||||||
return False
|
return False
|
||||||
result = result.lower()
|
|
||||||
|
|
||||||
# https://tools.ietf.org/html/rfc6455#page-6
|
# https://tools.ietf.org/html/rfc6455#page-6
|
||||||
magic_string = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11".encode()
|
magic_string = b"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
|
||||||
value = key + magic_string
|
hashed = base64.b64encode(hashlib.sha1(key + magic_string).digest())
|
||||||
hashed = base64.encodestring(hashlib.sha1(value).digest()).strip().lower().decode()
|
return hashed.decode() == result
|
||||||
return hashed == result
|
|
||||||
|
|
||||||
def _read_headers(self):
|
def _read_headers(self):
|
||||||
status = None
|
status = None
|
||||||
|
@ -575,7 +562,7 @@ class WebSocket(object):
|
||||||
kv = line.split(":", 1)
|
kv = line.split(":", 1)
|
||||||
if len(kv) == 2:
|
if len(kv) == 2:
|
||||||
key, value = kv
|
key, value = kv
|
||||||
headers[key.lower()] = value.strip().lower()
|
headers[key.lower()] = value.strip()
|
||||||
else:
|
else:
|
||||||
raise WebSocketException("Invalid header")
|
raise WebSocketException("Invalid header")
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue