Skip to content

Commit f3becb2

Browse files
committed
websocket: Extract encode_base64() function
This function will be useful on websocket client to construct Sec-WebSocket-Key header.
1 parent f5cd86e commit f3becb2

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

include/seastar/websocket/common.hh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ protected:
165165
};
166166

167167
std::string sha1_base64(std::string_view source);
168+
std::string encode_base64(std::string_view source);
168169

169170
extern logger websocket_logger;
170171

src/websocket/common.cc

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,18 +137,21 @@ std::string sha1_base64(std::string_view source) {
137137
ret != GNUTLS_E_SUCCESS) {
138138
throw websocket::exception(fmt::format("gnutls_hash_fast: {}", gnutls_strerror(ret)));
139139
}
140-
gnutls_datum_t hash_data{
141-
.data = hash,
142-
.size = sizeof(hash),
140+
return encode_base64(std::string_view(reinterpret_cast<const char*>(hash), sizeof(hash)));
141+
}
142+
143+
std::string encode_base64(std::string_view source) {
144+
gnutls_datum_t src_data{
145+
.data = reinterpret_cast<uint8_t*>(const_cast<char*>(source.data())),
146+
.size = static_cast<unsigned>(source.size())
143147
};
144-
gnutls_datum_t base64_encoded;
145-
if (int ret = gnutls_base64_encode2(&hash_data, &base64_encoded);
146-
ret != GNUTLS_E_SUCCESS) {
148+
gnutls_datum_t encoded_data;
149+
if (int ret = gnutls_base64_encode2(&src_data, &encoded_data); ret != GNUTLS_E_SUCCESS) {
147150
throw websocket::exception(fmt::format("gnutls_base64_encode2: {}", gnutls_strerror(ret)));
148151
}
149-
auto free_base64_encoded = defer([&] () noexcept { gnutls_free(base64_encoded.data); });
152+
auto free_encoded_data = defer([&] () noexcept { gnutls_free(encoded_data.data); });
150153
// base64_encoded.data is "unsigned char *"
151-
return std::string(reinterpret_cast<const char*>(base64_encoded.data), base64_encoded.size);
154+
return std::string(reinterpret_cast<const char*>(encoded_data.data), encoded_data.size);
152155
}
153156

154157
}

0 commit comments

Comments
 (0)