Commit d232216d authored by Taddeüs Kroes's avatar Taddeüs Kroes

Fixed handshake error (base64 encoding bug)

parent 85101b27
...@@ -55,7 +55,7 @@ class Frame(object): ...@@ -55,7 +55,7 @@ class Frame(object):
if mask: if mask:
masking_key = urandom(4) masking_key = urandom(4)
if len(masking_key) not in (0, 4): if len(masking_key) not in (0, 4):
raise ValueError('invalid masking key "%s"' % masking_key) raise ValueError('invalid masking key "%s"' % masking_key)
self.final = final self.final = final
...@@ -87,9 +87,9 @@ class Frame(object): ...@@ -87,9 +87,9 @@ class Frame(object):
| Payload Data continued ... | | Payload Data continued ... |
+---------------------------------------------------------------+ +---------------------------------------------------------------+
""" """
header = struct.pack('!B', (self.final << 7) | (self.rsv1 << 6) | header = struct.pack('!B', (self.final << 7) | (self.rsv1 << 6)
(self.rsv2 << 5) | (self.rsv3 << 4) | self.opcode) | (self.rsv2 << 5) | (self.rsv3 << 4)
| (self.opcode & 0xf))
mask = bool(self.masking_key) << 7 mask = bool(self.masking_key) << 7
payload_len = len(self.payload) payload_len = len(self.payload)
......
import re import re
import socket import socket
from hashlib import sha1 from hashlib import sha1
from base64 import b64encode
from frame import receive_frame from frame import receive_frame
from errors import HandshakeError from errors import HandshakeError
...@@ -81,6 +82,7 @@ class websocket(object): ...@@ -81,6 +82,7 @@ class websocket(object):
Send a number of frames. Send a number of frames.
""" """
for frame in args: for frame in args:
#print 'send frame:', frame, 'to %s:%d' % self.sock.getpeername()
self.sock.sendall(frame.pack()) self.sock.sendall(frame.pack())
def recv(self): def recv(self):
...@@ -89,6 +91,7 @@ class websocket(object): ...@@ -89,6 +91,7 @@ class websocket(object):
frame. frame.
""" """
frame = receive_frame(self.sock) frame = receive_frame(self.sock)
#print 'receive frame:', frame, 'from %s:%d' % self.sock.getpeername()
return frame return frame
def recvn(self, n): def recvn(self, n):
...@@ -123,7 +126,7 @@ class websocket(object): ...@@ -123,7 +126,7 @@ class websocket(object):
# request must be HTTP (at least 1.1) GET request, find the location # request must be HTTP (at least 1.1) GET request, find the location
location = re.search(r'^GET (.*) HTTP/1.1\r\n', raw_headers).group(1) location = re.search(r'^GET (.*) HTTP/1.1\r\n', raw_headers).group(1)
headers = re.findall(r'(.*?): (.*?)\r\n', raw_headers) headers = re.findall(r'(.*?): ?(.*?)\r\n', raw_headers)
header_names = [name for name, value in headers] header_names = [name for name, value in headers]
def header(name): def header(name):
...@@ -153,8 +156,8 @@ class websocket(object): ...@@ -153,8 +156,8 @@ class websocket(object):
extensions = [e for e in extensions if e in self.extensions] extensions = [e for e in extensions if e in self.extensions]
# Encode acceptation key using the WebSocket GUID # Encode acceptation key using the WebSocket GUID
key = header('Sec-WebSocket-Key') key = header('Sec-WebSocket-Key').strip()
accept = sha1(key + WS_GUID).digest().encode('base64') accept = b64encode(sha1(key + WS_GUID).digest())
# Construct HTTP response header # Construct HTTP response header
shake = 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n' shake = 'HTTP/1.1 101 Web Socket Protocol Handshake\r\n'
...@@ -171,7 +174,7 @@ class websocket(object): ...@@ -171,7 +174,7 @@ class websocket(object):
if extensions: if extensions:
shake += 'Sec-WebSocket-Extensions: %s\r\n' % ', '.join(extensions) shake += 'Sec-WebSocket-Extensions: %s\r\n' % ', '.join(extensions)
self.sock.send(shake + '\r\n') self.sock.sendall(shake + '\r\n')
def client_handshake(self): def client_handshake(self):
""" """
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment