Skip to content

Commit 3f65c13

Browse files
Merge pull request #1177 from paullouisageneau/websocket-certificate-from-string
Add support for loading WebSocket certificate from PEM string
2 parents 3a11fec + d3c94b7 commit 3f65c13

File tree

2 files changed

+23
-17
lines changed

2 files changed

+23
-17
lines changed

src/impl/websocket.cpp

+22-16
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,28 @@ using namespace std::placeholders;
3434
using namespace std::chrono_literals;
3535
using std::chrono::milliseconds;
3636

37+
const string PemBeginCertificateTag = "-----BEGIN CERTIFICATE-----";
38+
3739
WebSocket::WebSocket(optional<Configuration> optConfig, certificate_ptr certificate)
3840
: config(optConfig ? std::move(*optConfig) : Configuration()),
39-
mCertificate(certificate ? std::move(certificate) : std::move(loadCertificate(config))),
40-
mIsSecure(mCertificate != nullptr), mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {
41+
mRecvQueue(RECV_QUEUE_LIMIT, message_size_func) {
4142
PLOG_VERBOSE << "Creating WebSocket";
43+
44+
if (certificate) {
45+
mCertificate = std::move(certificate);
46+
} else if (config.certificatePemFile && config.keyPemFile) {
47+
mCertificate = std::make_shared<Certificate>(
48+
config.certificatePemFile->find(PemBeginCertificateTag) != string::npos
49+
? Certificate::FromString(*config.certificatePemFile, *config.keyPemFile)
50+
: Certificate::FromFile(*config.certificatePemFile, *config.keyPemFile,
51+
config.keyPemPass.value_or("")));
52+
} else if (config.certificatePemFile || config.keyPemFile) {
53+
throw std::invalid_argument(
54+
"Either none or both certificate and key PEM files must be specified");
55+
}
56+
57+
mIsSecure = mCertificate != nullptr;
58+
4259
if (config.proxyServer) {
4360
if (config.proxyServer->type == ProxyServer::Type::Socks5)
4461
throw std::invalid_argument(
@@ -49,19 +66,6 @@ WebSocket::WebSocket(optional<Configuration> optConfig, certificate_ptr certific
4966
}
5067
}
5168

52-
certificate_ptr WebSocket::loadCertificate(const Configuration& config) {
53-
if (!config.certificatePemFile)
54-
return nullptr;
55-
56-
if (config.keyPemFile)
57-
return std::make_shared<Certificate>(
58-
Certificate::FromFile(*config.certificatePemFile, *config.keyPemFile,
59-
config.keyPemPass.value_or("")));
60-
61-
throw std::invalid_argument(
62-
"Either none or both certificate and key PEM files must be specified");
63-
}
64-
6569
WebSocket::~WebSocket() { PLOG_VERBOSE << "Destroying WebSocket"; }
6670

6771
void WebSocket::open(const string &url) {
@@ -156,7 +160,9 @@ bool WebSocket::isOpen() const { return state == State::Open; }
156160

157161
bool WebSocket::isClosed() const { return state == State::Closed; }
158162

159-
size_t WebSocket::maxMessageSize() const { return config.maxMessageSize.value_or(DEFAULT_WS_MAX_MESSAGE_SIZE); }
163+
size_t WebSocket::maxMessageSize() const {
164+
return config.maxMessageSize.value_or(DEFAULT_WS_MAX_MESSAGE_SIZE);
165+
}
160166

161167
optional<message_variant> WebSocket::receive() {
162168
auto next = mRecvQueue.pop();

src/impl/websocket.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ struct WebSocket final : public Channel, public std::enable_shared_from_this<Web
7373

7474
const init_token mInitToken = Init::Instance().token();
7575

76-
const certificate_ptr mCertificate;
76+
certificate_ptr mCertificate;
7777
bool mIsSecure;
7878

7979
optional<string> mHostname; // for TLS SNI and Proxy

0 commit comments

Comments
 (0)