Browse Source

Code cleanup

eric.gressman 2 years ago
parent
commit
7b23b32c87

+ 1 - 43
src/impl/tcpproxytransport.cpp

@@ -12,28 +12,6 @@
 
 
 #if RTC_ENABLE_WEBSOCKET
 #if RTC_ENABLE_WEBSOCKET
 
 
-// #include <algorithm>
-// #include <chrono>
-// #include <iostream>
-// #include <numeric>
-// #include <random>
-// #include <regex>
-// #include <sstream>
-
-// #ifdef _WIN32
-// #include <winsock2.h>
-// #else
-// #include <arpa/inet.h>
-// #endif
-
-#ifndef htonll
-#define htonll(x)                                                                                  \
-	((uint64_t)(((uint64_t)htonl((uint32_t)(x))) << 32) | (uint64_t)htonl((uint32_t)((x) >> 32)))
-#endif
-#ifndef ntohll
-#define ntohll(x) htonll(x)
-#endif
-
 namespace rtc::impl {
 namespace rtc::impl {
 
 
 using std::to_integer;
 using std::to_integer;
@@ -124,30 +102,10 @@ std::string TcpProxyTransport::generateHttpRequest()
 	return out;
 	return out;
 }
 }
 
 
-//TODO move to utils?
-size_t parseHttpLines(const byte *buffer, size_t size, std::list<string> &lines) {
-	lines.clear();
-	auto begin = reinterpret_cast<const char *>(buffer);
-	auto end = begin + size;
-	auto cur = begin;
-	while (true) {
-		auto last = cur;
-		cur = std::find(cur, end, '\n');
-		if (cur == end)
-			return 0;
-		string line(last, cur != begin && *std::prev(cur) == '\r' ? std::prev(cur++) : cur++);
-		if (line.empty())
-			break;
-		lines.emplace_back(std::move(line));
-	}
-
-	return cur - begin;
-}
-
 size_t TcpProxyTransport::parseHttpResponse( std::byte* buffer, size_t size )
 size_t TcpProxyTransport::parseHttpResponse( std::byte* buffer, size_t size )
 {
 {
 	std::list<string> lines;
 	std::list<string> lines;
-	size_t length = parseHttpLines(buffer, size, lines);
+	size_t length = utils::parseHttpLines(buffer, size, lines);
 	if (length == 0)
 	if (length == 0)
 		return 0;
 		return 0;
 
 

+ 40 - 0
src/impl/utils.cpp

@@ -128,4 +128,44 @@ std::seed_seq random_seed() {
 	return std::seed_seq(seed.begin(), seed.end());
 	return std::seed_seq(seed.begin(), seed.end());
 }
 }
 
 
+size_t parseHttpLines(const byte *buffer, size_t size, std::list<string> &lines) {
+	lines.clear();
+	auto begin = reinterpret_cast<const char *>(buffer);
+	auto end = begin + size;
+	auto cur = begin;
+	while (true) {
+		auto last = cur;
+		cur = std::find(cur, end, '\n');
+		if (cur == end)
+			return 0;
+		string line(last, cur != begin && *std::prev(cur) == '\r' ? std::prev(cur++) : cur++);
+		if (line.empty())
+			break;
+		lines.emplace_back(std::move(line));
+	}
+
+	return cur - begin;
+}
+
+std::multimap<string, string> parseHttpHeaders(const std::list<string> &lines) {
+	std::multimap<string, string> headers;
+	for (const auto &line : lines) {
+		if (size_t pos = line.find_first_of(':'); pos != string::npos) {
+			string key = line.substr(0, pos);
+			string value = "";
+			if (size_t subPos = line.find_first_not_of(' ', pos + 1); subPos != string::npos )
+			{
+				value = line.substr(subPos);
+			}
+			std::transform(key.begin(), key.end(), key.begin(),
+			               [](char c) { return std::tolower(c); });
+			headers.emplace(std::move(key), std::move(value));
+		} else {
+			headers.emplace(line, "");
+		}
+	}
+
+	return headers;
+}
+
 } // namespace rtc::impl::utils
 } // namespace rtc::impl::utils

+ 8 - 0
src/impl/utils.hpp

@@ -13,6 +13,8 @@
 
 
 #include <climits>
 #include <climits>
 #include <limits>
 #include <limits>
+#include <list>
+#include <map>
 #include <random>
 #include <random>
 #include <vector>
 #include <vector>
 
 
@@ -32,6 +34,12 @@ string base64_encode(const binary &data);
 // Return a random seed sequence
 // Return a random seed sequence
 std::seed_seq random_seed();
 std::seed_seq random_seed();
 
 
+// Parse an http packet into lines
+size_t parseHttpLines(const byte *buffer, size_t size, std::list<string> &lines);
+
+// Parse headers of a http message
+std::multimap<string, string> parseHttpHeaders(const std::list<string> &lines);
+
 template <typename Generator, typename Result = typename Generator::result_type>
 template <typename Generator, typename Result = typename Generator::result_type>
 struct random_engine_wrapper {
 struct random_engine_wrapper {
 	Generator &engine;
 	Generator &engine;

+ 1 - 3
src/impl/websocket.cpp

@@ -103,7 +103,7 @@ void WebSocket::open(const string &url) {
 	if (string query = m[15]; !query.empty())
 	if (string query = m[15]; !query.empty())
 		path += "?" + query;
 		path += "?" + query;
 
 
-	mHostname = hostname; // for TLS SNI
+	mHostname = hostname; // for TLS SNI and Proxy
 	mService = service; //For proxy
 	mService = service; //For proxy
 	std::atomic_store(&mWsHandshake, std::make_shared<WsHandshake>(host, path, config.protocols));
 	std::atomic_store(&mWsHandshake, std::make_shared<WsHandshake>(host, path, config.protocols));
 
 
@@ -111,7 +111,6 @@ void WebSocket::open(const string &url) {
 
 
 	if (mIsProxied)
 	if (mIsProxied)
 	{
 	{
-		//TODO catch bad convert
 		setTcpTransport(std::make_shared<TcpTransport>(mProxy.value().hostname, std::to_string(mProxy.value().port), nullptr));
 		setTcpTransport(std::make_shared<TcpTransport>(mProxy.value().hostname, std::to_string(mProxy.value().port), nullptr));
 	}
 	}
 	else
 	else
@@ -299,7 +298,6 @@ shared_ptr<TcpProxyTransport> WebSocket::initProxyTransport() {
 			}
 			}
 		};
 		};
 
 
-		//TODO check optionals?
 		auto transport = std::make_shared<TcpProxyTransport>( lower, mHostname.value(), mService.value(), stateChangeCallback );
 		auto transport = std::make_shared<TcpProxyTransport>( lower, mHostname.value(), mService.value(), stateChangeCallback );
 
 
 		return emplaceTransport(this, &mProxyTransport, std::move(transport));
 		return emplaceTransport(this, &mProxyTransport, std::move(transport));

+ 1 - 1
src/impl/websocket.hpp

@@ -74,7 +74,7 @@ private:
 	bool mIsProxied{false};
 	bool mIsProxied{false};
 
 
 	optional<ProxyServer> mProxy;
 	optional<ProxyServer> mProxy;
-	optional<string> mHostname; // for TLS SNI
+	optional<string> mHostname; // for TLS SNI and Proxy
 	optional<string> mService; // for Proxy
 	optional<string> mService; // for Proxy
 
 
 	shared_ptr<TcpTransport> mTcpTransport;
 	shared_ptr<TcpTransport> mTcpTransport;

+ 4 - 44
src/impl/wshandshake.cpp

@@ -133,7 +133,7 @@ string WsHandshake::generateHttpError(int responseCode) {
 size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
 size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
 	std::unique_lock lock(mMutex);
 	std::unique_lock lock(mMutex);
 	std::list<string> lines;
 	std::list<string> lines;
-	size_t length = parseHttpLines(buffer, size, lines);
+	size_t length = utils::parseHttpLines(buffer, size, lines);
 	if (length == 0)
 	if (length == 0)
 		return 0;
 		return 0;
 
 
@@ -151,7 +151,7 @@ size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
 
 
 	mPath = std::move(path);
 	mPath = std::move(path);
 
 
-	auto headers = parseHttpHeaders(lines);
+	auto headers = utils::parseHttpHeaders(lines);
 
 
 	auto h = headers.find("host");
 	auto h = headers.find("host");
 	if (h == headers.end())
 	if (h == headers.end())
@@ -185,7 +185,7 @@ size_t WsHandshake::parseHttpRequest(const byte *buffer, size_t size) {
 size_t WsHandshake::parseHttpResponse(const byte *buffer, size_t size) {
 size_t WsHandshake::parseHttpResponse(const byte *buffer, size_t size) {
 	std::unique_lock lock(mMutex);
 	std::unique_lock lock(mMutex);
 	std::list<string> lines;
 	std::list<string> lines;
-	size_t length = parseHttpLines(buffer, size, lines);
+	size_t length = utils::parseHttpLines(buffer, size, lines);
 	if (length == 0)
 	if (length == 0)
 		return 0;
 		return 0;
 
 
@@ -202,7 +202,7 @@ size_t WsHandshake::parseHttpResponse(const byte *buffer, size_t size) {
 	if (code != 101)
 	if (code != 101)
 		throw std::runtime_error("Unexpected response code " + to_string(code) + " for WebSocket");
 		throw std::runtime_error("Unexpected response code " + to_string(code) + " for WebSocket");
 
 
-	auto headers = parseHttpHeaders(lines);
+	auto headers = utils::parseHttpHeaders(lines);
 
 
 	auto h = headers.find("upgrade");
 	auto h = headers.find("upgrade");
 	if (h == headers.end())
 	if (h == headers.end())
@@ -238,46 +238,6 @@ string WsHandshake::computeAcceptKey(const string &key) {
 	return utils::base64_encode(Sha1(string(key) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
 	return utils::base64_encode(Sha1(string(key) + "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"));
 }
 }
 
 
-size_t WsHandshake::parseHttpLines(const byte *buffer, size_t size, std::list<string> &lines) {
-	lines.clear();
-	auto begin = reinterpret_cast<const char *>(buffer);
-	auto end = begin + size;
-	auto cur = begin;
-	while (true) {
-		auto last = cur;
-		cur = std::find(cur, end, '\n');
-		if (cur == end)
-			return 0;
-		string line(last, cur != begin && *std::prev(cur) == '\r' ? std::prev(cur++) : cur++);
-		if (line.empty())
-			break;
-		lines.emplace_back(std::move(line));
-	}
-
-	return cur - begin;
-}
-
-std::multimap<string, string> WsHandshake::parseHttpHeaders(const std::list<string> &lines) {
-	std::multimap<string, string> headers;
-	for (const auto &line : lines) {
-		if (size_t pos = line.find_first_of(':'); pos != string::npos) {
-			string key = line.substr(0, pos);
-			string value = "";
-			if (size_t subPos = line.find_first_not_of(' ', pos + 1); subPos != string::npos )
-			{
-				value = line.substr(subPos);
-			}
-			std::transform(key.begin(), key.end(), key.begin(),
-			               [](char c) { return std::tolower(c); });
-			headers.emplace(std::move(key), std::move(value));
-		} else {
-			headers.emplace(line, "");
-		}
-	}
-
-	return headers;
-}
-
 WsHandshake::Error::Error(const string &w) : std::runtime_error(w) {}
 WsHandshake::Error::Error(const string &w) : std::runtime_error(w) {}
 
 
 WsHandshake::RequestError::RequestError(const string &w, int responseCode)
 WsHandshake::RequestError::RequestError(const string &w, int responseCode)

+ 0 - 2
src/impl/wshandshake.hpp

@@ -52,8 +52,6 @@ public:
 private:
 private:
 	static string generateKey();
 	static string generateKey();
 	static string computeAcceptKey(const string &key);
 	static string computeAcceptKey(const string &key);
-	static size_t parseHttpLines(const byte *buffer, size_t size, std::list<string> &lines);
-	static std::multimap<string, string> parseHttpHeaders(const std::list<string> &lines);
 
 
 	string mHost;
 	string mHost;
 	string mPath;
 	string mPath;