Marky Ross
Marky Ross

Reputation: 41

Websockets Sec-WebSocket-Accept invalid in Vala

I have got to the point where I have no where else to search and been at this for 2 days.

I am trying to implement server side for Websockets in Vala. I have been following the RFC here as well as tried to convert examples from other languages.

string res = "HTTP/1.1 101 Switching Protocols" + "\r\n"
            + "Connection: Upgrade" + "\r\n"
            + "Upgrade: websocket" + "\r\n"
            + "Sec-WebSocket-Accept: " + Base64.encode(Checksum.compute_for_string(ChecksumType.SHA1,
                Regex.split_simple("Sec-WebSocket-Key: (.*)", request)[1].strip() + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
            ).data;)
            + "\r\n\r\n";

The output from my Vala code is different from both C# and PHP outputs. The key sent to my browser is:

y/WXsXKM98if/+AUaqF6iQ==

PHP and C# give me the following result:

ZGZhMTEyNjkxNDBkNGQ4YzlmOGFmNjZmYzEzN2UxOThlOGM0ZDRlYg==

Vala gives me the following result:

ZmRjODM1ODQwNDNmOTM5ODAzY2Q5MzJhMjE4NzQyYmQ2YmRkOWQ1

I don't think it is anything to do with encoding of strings as C# and Vala both use the same default encoding. I assumed it was something to do with a null byte at the end of the Data array but, I have checked and there is not a one.

Any advice would be great

Edit

Finally got the output in vala to match that of PHP and C#. I forgot to append the GUID but, this doesn't explain why I am getting this in Chrome

failed: Error during WebSocket handshake: Incorrect 'Sec-WebSocket-Accept' header value

Upvotes: 1

Views: 666

Answers (1)

Marky Ross
Marky Ross

Reputation: 41

The whole problem was that the SHA1 output I was using was the escaped HEX string rather than the raw binary output.

Checksum cs = new Checksum(ChecksumType.SHA1);
cs.update(_base.data, -1);
cs.update("258EAFA5-E914-47DA-95CA-C5AB0DC85B11".data, -1);

size_t len = 20;
uint8[] digest = new uint8[len];
cs.get_digest(digest, ref len);
string key = Base64.encode(digest);

Found the answer by looking through Libsoup.

Upvotes: 1

Related Questions