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

WebSocket: move client certificate loading to impl

Salvo Passaro 1 éve
szülő
commit
9167c31d5b
4 módosított fájl, 20 hozzáadás és 23 törlés
  1. 1 4
      include/rtc/websocket.hpp
  2. 15 2
      src/impl/websocket.cpp
  3. 2 0
      src/impl/websocket.hpp
  4. 2 17
      src/websocket.cpp

+ 1 - 4
include/rtc/websocket.hpp

@@ -20,7 +20,6 @@ namespace rtc {
 namespace impl {
 
 struct WebSocket;
-class Certificate;
 
 }
 
@@ -47,7 +46,7 @@ public:
 	};
 
 	WebSocket();
-	WebSocket(const Configuration& config);
+	WebSocket(Configuration config);
 	WebSocket(impl_ptr<impl::WebSocket> impl);
 	~WebSocket() override;
 
@@ -67,8 +66,6 @@ public:
 	optional<string> path() const;
 
 private:
-	static shared_ptr<impl::Certificate> loadCertificate(const Configuration&);
-
 	using CheshireCat<impl::WebSocket>::impl;
 };
 

+ 15 - 2
src/impl/websocket.cpp

@@ -36,8 +36,8 @@ using std::chrono::milliseconds;
 
 WebSocket::WebSocket(optional<Configuration> optConfig, certificate_ptr certificate)
     : config(optConfig ? std::move(*optConfig) : Configuration()),
-      mCertificate(std::move(certificate)), mIsSecure(mCertificate != nullptr),
-      mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {
+      mCertificate(certificate ? std::move(certificate) : std::move(loadCertificate(config))),
+      mIsSecure(mCertificate != nullptr), mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {
 	PLOG_VERBOSE << "Creating WebSocket";
 	if (config.proxyServer) {
 		if (config.proxyServer->type == ProxyServer::Type::Socks5)
@@ -49,6 +49,19 @@ WebSocket::WebSocket(optional<Configuration> optConfig, certificate_ptr certific
 	}
 }
 
+certificate_ptr WebSocket::loadCertificate(const Configuration& config) {
+	if (!config.certificatePemFile)
+		return nullptr;
+
+	if (config.keyPemFile)
+		return std::make_shared<Certificate>(
+			Certificate::FromFile(*config.certificatePemFile, *config.keyPemFile,
+										config.keyPemPass.value_or("")));
+
+	throw std::invalid_argument(
+		"Either none or both certificate and key PEM files must be specified");
+}
+
 WebSocket::~WebSocket() { PLOG_VERBOSE << "Destroying WebSocket"; }
 
 void WebSocket::open(const string &url) {

+ 2 - 0
src/impl/websocket.hpp

@@ -67,6 +67,8 @@ struct WebSocket final : public Channel, public std::enable_shared_from_this<Web
 	std::atomic<State> state = State::Closed;
 
 private:
+	static certificate_ptr loadCertificate(const Configuration& config);
+
 	void scheduleConnectionTimeout();
 
 	const init_token mInitToken = Init::Instance().token();

+ 2 - 17
src/websocket.cpp

@@ -13,14 +13,13 @@
 
 #include "impl/internals.hpp"
 #include "impl/websocket.hpp"
-#include "impl/certificate.hpp"
 
 namespace rtc {
 
 WebSocket::WebSocket() : WebSocket(Configuration()) {}
 
-WebSocket::WebSocket(const Configuration& config)
-    : CheshireCat<impl::WebSocket>(config, loadCertificate(config)),
+WebSocket::WebSocket(Configuration config)
+    : CheshireCat<impl::WebSocket>(std::move(config)),
       Channel(std::dynamic_pointer_cast<impl::Channel>(CheshireCat<impl::WebSocket>::impl())) {}
 
 WebSocket::WebSocket(impl_ptr<impl::WebSocket> impl)
@@ -69,20 +68,6 @@ optional<string> WebSocket::path() const {
 	return state != State::Connecting && handshake ? make_optional(handshake->path()) : nullopt;
 }
 
-impl::certificate_ptr WebSocket::loadCertificate(const Configuration& config) {
-	if (!config.certificatePemFile && !config.keyPemFile)
-		return nullptr;
-
-	if (config.certificatePemFile && config.keyPemFile) {
-		return std::make_shared<impl::Certificate>(
-			impl::Certificate::FromFile(*config.certificatePemFile, *config.keyPemFile,
-										config.keyPemPass.value_or("")));
-	}
-
-	throw std::invalid_argument(
-		"Either none or both certificate and key PEM files must be specified");
-}
-
 } // namespace rtc
 
 #endif