Forráskód Böngészése

support WebSocket Protocol RFC6455

홍학규 4 éve
szülő
commit
99bae7f830
4 módosított fájl, 23 hozzáadás és 5 törlés
  1. 1 0
      include/rtc/websocket.hpp
  2. 1 0
      src/websocket.cpp
  3. 14 4
      src/wstransport.cpp
  4. 7 1
      src/wstransport.hpp

+ 1 - 0
include/rtc/websocket.hpp

@@ -49,6 +49,7 @@ public:
 
 	struct Configuration {
 		bool disableTlsVerification = false; // if true, don't verify the TLS certificate
+		std::vector<string> protocols;
 	};
 
 	WebSocket(std::optional<Configuration> config = nullopt);

+ 1 - 0
src/websocket.cpp

@@ -288,6 +288,7 @@ shared_ptr<WsTransport> WebSocket::initWsTransport() {
 		if (!lower)
 			lower = std::atomic_load(&mTcpTransport);
 		auto transport = std::make_shared<WsTransport>(
+			WsTransport::Configuration{{.protocols = mConfig.protocols}},
 		    lower, mHost, mPath, weak_bind(&WebSocket::incoming, this, _1),
 		    [this, weak_this = weak_from_this()](State state) {
 			    auto shared_this = weak_this.lock();

+ 14 - 4
src/wstransport.cpp

@@ -53,9 +53,9 @@ using std::to_string;
 using random_bytes_engine =
     std::independent_bits_engine<std::default_random_engine, CHAR_BIT, unsigned short>;
 
-WsTransport::WsTransport(std::shared_ptr<Transport> lower, string host, string path,
+WsTransport::WsTransport(std::optional<Configuration> config, std::shared_ptr<Transport> lower, string host, string path,
                          message_callback recvCallback, state_callback stateCallback)
-    : Transport(lower, std::move(stateCallback)), mHost(std::move(host)), mPath(std::move(path)) {
+    : Transport(lower, std::move(stateCallback)), mHost(std::move(host)), mPath(std::move(path)), mConfig(config ? std::move(*config) : Configuration()) {
 	onRecv(recvCallback);
 
 	PLOG_DEBUG << "Initializing WebSocket transport";
@@ -164,6 +164,15 @@ bool WsTransport::sendHttpRequest() {
 	auto k = reinterpret_cast<uint8_t *>(key.data());
 	std::generate(k, k + key.size(), [&]() { return uint8_t(generator()); });
 
+	string appendHeader = "";
+	if(mConfig.protocols.size() > 0) {
+		appendHeader += "Sec-WebSocket-Protocol: " +
+						std::accumulate(mConfig.protocols.begin(), mConfig.protocols.end(), string(), [](const string& a, const string& b) -> string { 
+							return a + (a.length() > 0 ? "," : "") + b; 
+						}) +
+						"\r\n";
+	}
+	
 	const string request = "GET " + mPath +
 	                       " HTTP/1.1\r\n"
 	                       "Host: " +
@@ -174,8 +183,9 @@ bool WsTransport::sendHttpRequest() {
 	                       "Sec-WebSocket-Version: 13\r\n"
 	                       "Sec-WebSocket-Key: " +
 	                       to_base64(key) +
-	                       "\r\n"
-	                       "\r\n";
+	                       "\r\n" +
+						   std::move(appendHeader) +
+						   "\r\n";
 
 	auto data = reinterpret_cast<const byte *>(request.data());
 	auto size = request.size();

+ 7 - 1
src/wstransport.hpp

@@ -31,7 +31,11 @@ class TlsTransport;
 
 class WsTransport : public Transport {
 public:
-	WsTransport(std::shared_ptr<Transport> lower, string host, string path,
+	struct Configuration {
+		std::vector<string> protocols;
+	};
+
+	WsTransport(std::optional<Configuration> config, std::shared_ptr<Transport> lower, string host, string path,
 	            message_callback recvCallback, state_callback stateCallback);
 	~WsTransport();
 
@@ -74,6 +78,8 @@ private:
 	binary mBuffer;
 	binary mPartial;
 	Opcode mPartialOpcode;
+
+	const Configuration mConfig;
 };
 
 } // namespace rtc