2

i have the following code for my python server to create and send back the response to an handshake

    def HandShake(self, request):
    specificationGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
    websocketkey = ""
    protocol = ""
    for line in request.split("\n"):
        if "Sec-WebSocket-Key:" in line:
            websocketkey = line.split(" ")[1]
        elif "Sec-WebSocket-Protocol" in line:
            protocol = line.split(":")[1].strip()

    print("websocketkey: " + websocketkey + "\n")
    fullKey = hashlib.sha1(websocketkey.encode("utf-8") + specificationGUID.encode("utf-8")).digest()
    acceptKey = base64.b64encode(fullKey)
    print("acceptKey: " + str(acceptKey, "utf-8") + "\n")
    if protocol != "":
        handshake = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + str(acceptKey, "utf-8") + "\r\nSec-WebSocket-Protocol: " + protocol + "\r\n\r\n"
    else:
        handshake = "HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: " + str(acceptKey, "utf-8") + "\r\n\r\n"
    print(handshake)
    self.request.send(bytes(handshake, "utf-8"))

I have tested my method of calculating the key with the example on wikipedia, so i know that is correct. However everytime when i try to connect to my server i get the following the error:

Error during WebSocket handshake: Sec-WebSocket-Accept mismatch

I dont understand what im doing wrong here. does someone see what is going wrong?

EDIT: an example output from the prints, this prints the original message and the fabricated handshake

server started, waiting for connections...
request:
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:600
Origin: http://localhost
Pragma: no-cache
Cache-Control: no-cache
Sec-WebSocket-Key: yLffHPqMU4gIW2WnKq+4BQ==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36


websocketkey: yLffHPqMU4gIW2WnKq+4BQ==

acceptKey: A0eCd19URtkji0OPV162okWsCns=

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: A0eCd19URtkji0OPV162okWsCns=
2
  • Post the output of your print() calls in your question. Commented Dec 22, 2013 at 3:42
  • i edited the op with the print output Commented Dec 22, 2013 at 10:45

1 Answer 1

2

You have a bug in your code.

for line in request.split("\n"):
    if "Sec-WebSocket-Key:" in line:
        websocketkey = line.split(" ")[1]

That is returning the "\r" in your Sec-WebSocket-Key.

Evidence:

Normal RFC Behavior

Client Key: "yLffHPqMU4gIW2WnKq+4BQ=="
Server Key: YVjKqlMRxlzzM70LScN9VoCsboI=

Bad Behavior

Client Key: "yLffHPqMU4gIW2WnKq+4BQ==\r"
Server Key: A0eCd19URtkji0OPV162okWsCns=

Links to AutobahnPython Sec-WebSocket-Key server side validation, and AutobahnPython hash calculation.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.