diff --git a/iOS/FlipperKit/FlipperWebSocket.mm b/iOS/FlipperKit/FlipperWebSocket.mm index d3bcec289..e394c156b 100644 --- a/iOS/FlipperKit/FlipperWebSocket.mm +++ b/iOS/FlipperKit/FlipperWebSocket.mm @@ -9,6 +9,7 @@ #import "FlipperWebSocket.h" #import +#import #import #import #import @@ -71,11 +72,7 @@ class WebSocketSerializer : public FlipperPayloadSerializer { query += key; query += "="; if (key == "csr") { - NSString* csr = [NSString stringWithUTF8String:value.c_str()]; - NSData* data = [csr dataUsingEncoding:NSUTF8StringEncoding]; - NSString* base64 = [data base64EncodedStringWithOptions:0]; - - query += base64.UTF8String; + query += Base64::encode(value); } else { query += url_encode(value); } diff --git a/xplat/Flipper/FlipperBase64.cpp b/xplat/Flipper/FlipperBase64.cpp new file mode 100644 index 000000000..c61f6b338 --- /dev/null +++ b/xplat/Flipper/FlipperBase64.cpp @@ -0,0 +1,37 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#include "FlipperBase64.h" +#include +#include +#include + +namespace facebook { +namespace flipper { + +std::string Base64::encode(const std::string& input) { + const auto base64_mem = BIO_new(BIO_s_mem()); + auto base64 = BIO_new(BIO_f_base64()); + base64 = BIO_push(base64, base64_mem); + + BIO_set_flags(base64, BIO_FLAGS_BASE64_NO_NL); + + BIO_write(base64, input.c_str(), static_cast(input.length())); + BIO_flush(base64); + + BUF_MEM* buffer_memory{}; + BIO_get_mem_ptr(base64, &buffer_memory); + auto base64_encoded = + std::string(buffer_memory->data, buffer_memory->length - 1); + + BIO_free_all(base64); + + return base64_encoded; +} + +} // namespace flipper +} // namespace facebook diff --git a/xplat/Flipper/FlipperBase64.h b/xplat/Flipper/FlipperBase64.h new file mode 100644 index 000000000..5715d7014 --- /dev/null +++ b/xplat/Flipper/FlipperBase64.h @@ -0,0 +1,21 @@ +/* + * Copyright (c) Facebook, Inc. and its affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +#pragma once + +#include + +namespace facebook { +namespace flipper { + +class Base64 { + public: + static std::string encode(const std::string& input); +}; + +} // namespace flipper +} // namespace facebook