Beispiel #1
0
    /**
     * @param [out] req  set of headers to fill in
     * @param [in] uri the uri being connected to
     * @param [in] subprotocols the list of subprotocols to request
     */
    lib::error_code client_handshake_request(request_type & req, uri_ptr
        uri, std::vector<std::string> const & subprotocols) const
    {
        req.set_method("get");
        req.set_uri(uri->get_resource());
        req.set_version("http/1.1");

        req.append_header("upgrade","websocket");
        req.append_header("connection","upgrade");
        req.replace_header("sec-websocket-version","13");
        req.replace_header("host",uri->get_host_port());

        if (!subprotocols.empty()) {
            std::ostringstream result;
            std::vector<std::string>::const_iterator it = subprotocols.begin();
            result << *it++;
            while (it != subprotocols.end()) {
                result << ", " << *it++;
            }

            req.replace_header("sec-websocket-protocol",result.str());
        }

        // generate handshake key
        frame::uint32_converter conv;
        unsigned char raw_key[16];

        for (int i = 0; i < 4; i++) {
            conv.i = m_rng();
            std::copy(conv.c,conv.c+4,&raw_key[i*4]);
        }

        req.replace_header("sec-websocket-key",base64_encode(raw_key, 16));

        return lib::error_code();
    }