string ~ ubyte

Bob Cowdery bob at bobcowdery.plus.com
Sun Oct 31 04:36:33 PDT 2010


Hi
I'm implementing a web sockets server as part of the UI for my
application. It's almost working but the connection closes on me just
after the handshake. I'm pretty sure the handshake response I'm sending
is incorrect.

This is the last bit of the code (I've missed ot the bit that creates
the parts as I think that is ok):

        // Create the challenge response
        // The response is formed by a concatenation of
        // part_1 + part_2 + key (where parts are 32 bit integers and
key is 8 bytes)
        // The parts must be in big endian order and the whole forms a
128 bit value
        // The response is then the MD5 hash of this value
        writeln("Creating response");
        auto response = new ubyte[16];
        response[0] = part_1 & 0xFF;
        response[1] = (part_1 >> 8) & 0xFF;
        response[2] = (part_1 >> 16) & 0xFF;
        response[3] = (part_1 >> 24) & 0xFF;
        response[4] = part_2 & 0xFF;
        response[5] = (part_2 >> 8) & 0xFF;
        response[6] = (part_2 >> 16) & 0xFF;
        response[7] = (part_2 >> 24) & 0xFF;
        foreach(i, value; key) {
            response[i+8] = value;
        }

        // Create an MD5 hash of the result.
        ubyte digest[16];
        sum(digest, response);

        // Send the handshake
        writeln("Handshake sending: ", handshake ~ digestToString(digest));
        this.sock.send(handshake ~ digestToString(digest));

The result of running gives me a handshake respose of:

        Creating response
        Handshake sending: HTTP/1.1 101 Web Socket Protocol Handshake
        Upgrade: WebSocket
        Connection: Upgrade
        WebSocket-Origin: http://localhost:9000
        WebSocket-Location: ws://localhost:9999/
        Sec-Websocket-Origin: http://localhost:9000
        Sec-Websocket-Location: ws://localhost:9999/
       
        5C368EF0F874025639911E2BC5F01412

This all looks good except the string at the end (the digest) which has
32 characters whereas I think the client is expecting 16. I'm guessing
the string is created as UTF16. Is there a way to get this to be 16
characters?

bob


More information about the Digitalmars-d-learn mailing list