Exemplo n.º 1
0
void
websocket_info::parse_sec_keys(request_impl const &req) {

	std::string const &key1 = req.header(WS_STR_SEC_WEBSOCKET_KEY1);
	if (key1.empty()) {
		return;
	}
	std::string const &key2 = req.header(WS_STR_SEC_WEBSOCKET_KEY2);
	if (key2.empty()) {
		return;
	}

	if (req.body().size() < 8) {
		throw std::runtime_error("can not find data in websocket request");
	}
	boost::int32_t v1 = get_websocket_key_value(key1.c_str(), WS_STR_SEC_WEBSOCKET_KEY1);
	boost::int32_t v2 = get_websocket_key_value(key2.c_str(), WS_STR_SEC_WEBSOCKET_KEY2);

	MD5_CTX md5handler;
	MD5_Init(&md5handler);
	MD5_Update(&md5handler, &v1, sizeof(v1));
	MD5_Update(&md5handler, &v2, sizeof(v2));
	MD5_Update(&md5handler, req.body().data(), 8);

	std::string str;
	str.resize(16);
	MD5_Final((unsigned char*)&str[0], &md5handler);

	str.swap(sec_data_);
}
Exemplo n.º 2
0
bool
websocket_info::parse(request_impl const &req, bool secure) {

	if (req.header(WS_STR_CONNECTION) != WS_STR_UPGRADE || req.header(WS_STR_UPGRADE) != WS_STR_WEBSOCKET) {
		return false;
	}
	empty_ = false;

	std::string const &host = req.header(WS_STR_HOST);
	if (host.empty()) {
		throw std::runtime_error("can not find Host in websocket request");
	}

	std::string const &key1 = req.header(WS_STR_SEC_WEBSOCKET_KEY1);
	std::string const &key2 = req.header(WS_STR_SEC_WEBSOCKET_KEY2);
	if (!key1.empty() && !key2.empty()) {
		if (req.body().size() < 8) {
			throw std::runtime_error("can not find data in websocket request");
		}
		boost::int32_t v1 = get_websocket_key_value(key1.c_str(), WS_STR_SEC_WEBSOCKET_KEY1);
		boost::int32_t v2 = get_websocket_key_value(key2.c_str(), WS_STR_SEC_WEBSOCKET_KEY2);

		MD5_CTX md5handler;
		MD5_Init(&md5handler);
		MD5_Update(&md5handler, &v1, sizeof(v1));
		MD5_Update(&md5handler, &v2, sizeof(v2));
		MD5_Update(&md5handler, req.body().data(), 8);

		std::string str;
		str.resize(16);
		MD5_Final((unsigned char*)&str[0], &md5handler);

		str.swap(sec_data_);
	}

	std::string const &uri = req.uri();
	if (secure) {
		location_.reserve(WS_STR_SECURE_SCHEME_DELIMITER.size() + host.size() + uri.size() + 1);
		location_.assign(WS_STR_SECURE_SCHEME_DELIMITER).append(host).append(uri);
	}
	else {
		location_.reserve(WS_STR_SCHEME_DELIMITER.size() + host.size() + uri.size() + 1);
		location_.assign(WS_STR_SCHEME_DELIMITER).append(host).append(uri);
	}

	origin_ = req.header(WS_STR_ORIGIN);
	protocol_ = req.header(WS_STR_WEBSOCKET_PROTOCOL);
	return true;
}