Skip to content

Commit

Permalink
[0.65] Allow propagating WinRTWebSocketResource constructor exceptions (
Browse files Browse the repository at this point in the history
#7896)

* Allow propagating WinRTWebSocketResource constructor exceptions (#7892)

* Correctly format message on hresult_error

* Remove noexcept from public constructors

* Change files

* Make private constructor certExceptions r-value
  • Loading branch information
JunielKatarn committed Jun 1, 2021
1 parent 0e11f00 commit 405b65d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 22 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"type": "prerelease",
"comment": "Allow propagating WinRTWebSocketResource constructor exceptions (#7892)",
"packageName": "react-native-windows",
"email": "julio.rocha@microsoft.com",
"dependentChangeType": "patch"
}
12 changes: 12 additions & 0 deletions vnext/Desktop.UnitTests/WebSocketModuleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ TEST_CLASS (WebSocketModuleTest) {
Assert::AreEqual(static_cast<size_t>(0), module->getConstants().size());
}

TEST_METHOD(ConnectEmptyUriFails) {
auto module = make_unique<WebSocketModule>();

module->getMethods()
.at(WebSocketModule::MethodId::Connect)
.func(
dynamic::array("" /*url*/, dynamic(), dynamic(), /*id*/ 0), [](vector<dynamic>) {}, [](vector<dynamic>) {});

// Test passes by not crashing due to an unhandled exception.
Assert::IsTrue(true);
}

TEST_METHOD(ConnectSendsEvent) {
string eventName;
string moduleName;
Expand Down
8 changes: 7 additions & 1 deletion vnext/Shared/Modules/WebSocketModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
#include <cxxreact/JsArgumentHelpers.h>
#include "Unicode.h"

// Standard Libriary
#include <iomanip>

using namespace facebook::xplat;
using namespace folly;

Expand Down Expand Up @@ -161,7 +164,10 @@ shared_ptr<IWebSocketResource> WebSocketModule::GetOrCreateWebSocket(int64_t id,
}
catch (const winrt::hresult_error& e)
{
string message = string{"[" + e.code()} + "] " + Utf16ToUtf8(e.message());
std::wstringstream ss;
ss << L"[" << std::hex << std::showbase << std::setw(8) << static_cast<uint32_t>(e.code()) << L"] " << e.message().c_str();
string message{winrt::to_string(ss.str()).c_str()};

SendEvent("webSocketFailed", dynamic::object("id", id)("message", std::move(message)));

return nullptr;
Expand Down
31 changes: 15 additions & 16 deletions vnext/Shared/WinRTWebSocketResource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,33 +87,32 @@ string HResultToString(hresult &&result) {

namespace Microsoft::React {

// private
WinRTWebSocketResource::WinRTWebSocketResource(
IMessageWebSocket &&socket,
Uri &&uri,
vector<ChainValidationResult> &&certExceptions)
: WinRTWebSocketResource(
std::move(socket),
DataWriter{socket.OutputStream()},
std::move(uri),
std::move(certExceptions)) {}

WinRTWebSocketResource::WinRTWebSocketResource(
IMessageWebSocket &&socket,
IDataWriter &&writer,
Uri &&uri,
vector<ChainValidationResult> &&certExeptions) noexcept
vector<ChainValidationResult> &&certExceptions)
: m_uri{std::move(uri)}, m_socket{std::move(socket)}, m_writer{std::move(writer)} {
m_socket.MessageReceived({this, &WinRTWebSocketResource::OnMessageReceived});

for (const auto &certException : certExeptions) {
for (const auto &certException : certExceptions) {
m_socket.Control().IgnorableServerCertificateErrors().Append(certException);
}
}

WinRTWebSocketResource::WinRTWebSocketResource(
IMessageWebSocket &&socket,
Uri &&uri,
vector<ChainValidationResult> certExeptions) noexcept
: WinRTWebSocketResource(
std::move(socket),
DataWriter{socket.OutputStream()},
std::move(uri),
std::move(certExeptions)) {}

WinRTWebSocketResource::WinRTWebSocketResource(
const string &urlString,
vector<ChainValidationResult> &&certExeptions) noexcept
: WinRTWebSocketResource(MessageWebSocket{}, Uri{winrt::to_hstring(urlString)}, std::move(certExeptions)) {}
WinRTWebSocketResource::WinRTWebSocketResource(const string &urlString, vector<ChainValidationResult> &&certExceptions)
: WinRTWebSocketResource(MessageWebSocket{}, Uri{winrt::to_hstring(urlString)}, std::move(certExceptions)) {}

WinRTWebSocketResource::~WinRTWebSocketResource() noexcept /*override*/
{
Expand Down
8 changes: 3 additions & 5 deletions vnext/Shared/WinRTWebSocketResource.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class WinRTWebSocketResource : public IWebSocketResource, public std::enable_sha
WinRTWebSocketResource(
winrt::Windows::Networking::Sockets::IMessageWebSocket &&socket,
winrt::Windows::Foundation::Uri &&uri,
std::vector<winrt::Windows::Security::Cryptography::Certificates::ChainValidationResult> certExeptions) noexcept;
std::vector<winrt::Windows::Security::Cryptography::Certificates::ChainValidationResult> &&certExceptions);

winrt::Windows::Foundation::IAsyncAction PerformConnect() noexcept;
winrt::fire_and_forget PerformPing() noexcept;
Expand All @@ -63,13 +63,11 @@ class WinRTWebSocketResource : public IWebSocketResource, public std::enable_sha
winrt::Windows::Networking::Sockets::IMessageWebSocket &&socket,
winrt::Windows::Storage::Streams::IDataWriter &&writer,
winrt::Windows::Foundation::Uri &&uri,
std::vector<winrt::Windows::Security::Cryptography::Certificates::ChainValidationResult>
&&certExeptions) noexcept;
std::vector<winrt::Windows::Security::Cryptography::Certificates::ChainValidationResult> &&certExceptions);

WinRTWebSocketResource(
const std::string &urlString,
std::vector<winrt::Windows::Security::Cryptography::Certificates::ChainValidationResult>
&&certExeptions) noexcept;
std::vector<winrt::Windows::Security::Cryptography::Certificates::ChainValidationResult> &&certExceptions);

~WinRTWebSocketResource() noexcept override;

Expand Down

0 comments on commit 405b65d

Please sign in to comment.