Exemple #1
0
    lib::error_code process_handshake(request_type const & req,
        std::string const & subprotocol, response_type & res) const
    {
        std::array<char, 16> key_final;

        // copy key1 into final key
        decode_client_key(req.get_header("Sec-WebSocket-Key1"), &key_final[0]);

        // copy key2 into final key
        decode_client_key(req.get_header("Sec-WebSocket-Key2"), &key_final[4]);

        // copy key3 into final key
        // key3 should be exactly 8 bytes. If it is more it will be truncated
        // if it is less the final key will almost certainly be wrong.
        // TODO: decide if it is best to silently fail here or produce some sort
        //       of warning or exception.
        const std::string& key3 = req.get_header("Sec-WebSocket-Key3");
        auto sz = std::min(size_t(8), key3.size());
        std::copy(key3.begin(),
                  key3.begin() + sz,
                  key_final.begin() + 8);

        res.append_header(
            "Sec-WebSocket-Key3",
            md5::md5_hash_string(std::string(key_final.begin(), key_final.end()))
        );

        res.append_header("Upgrade","WebSocket");
        res.append_header("Connection","Upgrade");

        // Echo back client's origin unless our local application set a
        // more restrictive one.
        if (res.get_header("Sec-WebSocket-Origin") == "") {
            res.append_header("Sec-WebSocket-Origin",req.get_header("Origin"));
        }

        // Echo back the client's request host unless our local application
        // set a different one.
        if (res.get_header("Sec-WebSocket-Location") == "") {
            uri_ptr uri = get_uri(req);
            res.append_header("Sec-WebSocket-Location",uri->str());
        }

        if (subprotocol != "") {
            res.replace_header("Sec-WebSocket-Protocol",subprotocol);
        }

        return lib::error_code();
    }
Exemple #2
0
    /* TODO: the 'subprotocol' parameter may need to be expanded into a more
     * generic struct if other user input parameters to the processed handshake
     * are found.
     */
    lib::error_code process_handshake(request_type const & request, 
        std::string const & subprotocol, response_type & response) const
    {
        std::string server_key = request.get_header("Sec-WebSocket-Key");

        lib::error_code ec = process_handshake_key(server_key);

        if (ec) {
            return ec;
        }

        response.replace_header("Sec-WebSocket-Accept",server_key);
        response.append_header("Upgrade",constants::upgrade_token);
        response.append_header("Connection",constants::connection_token);

        if (!subprotocol.empty()) {
            response.replace_header("Sec-WebSocket-Protocol",subprotocol);
        }

        return lib::error_code();
    }