Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid url causing a crash #94

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 37 additions & 21 deletions src/signalrclient/default_http_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,35 +86,51 @@ namespace signalr
cts.cancel();
});

web::http::client::http_client client(utility::conversions::to_string_t(url), m_config.get_http_client_config());
client.request(http_request, cts.get_token())
.then([context, callback](pplx::task<web::http::http_response> response_task)
try
{
try
{
auto http_response = response_task.get();
auto status_code = http_response.status_code();
http_response.extract_utf8string()
.then([callback, status_code](std::string response_body)
{
signalr::http_response response;
response.content = response_body;
response.status_code = status_code;
callback(std::move(response), nullptr);
});
}
catch (...)
{
callback(http_response(), std::current_exception());
}
web::http::client::http_client client(utility::conversions::to_string_t(url), m_config.get_http_client_config());
client.request(http_request, cts.get_token())
.then([context, callback](pplx::task<web::http::http_response> response_task)
{
try
{
auto http_response = response_task.get();
auto status_code = http_response.status_code();
http_response.extract_utf8string()
.then([callback, status_code](std::string response_body)
{
signalr::http_response response;
response.content = response_body;
response.status_code = status_code;
callback(std::move(response), nullptr);
});
}
catch (...)
{
callback(http_response(), std::current_exception());
}

if (context != nullptr)
{
std::unique_lock<std::mutex> lck(context->mtx);
context->complete = true;
context->cv.notify_all();
}
});
}
// Url validation could throw from the client ctor
catch (...)
{
// unblock timeout task if it was created
if (context != nullptr)
{
std::unique_lock<std::mutex> lck(context->mtx);
context->complete = true;
context->cv.notify_all();
}
});
callback({}, std::current_exception());
return;
}
}
}
#endif
28 changes: 28 additions & 0 deletions test/signalrclienttests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ if(USE_MSGPACK)
)
endif()

if(USE_CPPRESTSDK)
list (APPEND SOURCES
cpprestsdk_tests.cpp
)
endif()

include_directories(
../../src/signalrclient
)
Expand Down Expand Up @@ -85,6 +91,28 @@ list (APPEND libraries ${JSONCPP_LIB})

list (APPEND libraries gtest)

if(NOT USE_CPPRESTSDK)
target_link_libraries(signalrclienttests)
else()
set_target_properties(signalrclienttests PROPERTIES COMPILE_FLAGS "-DUSE_CPPRESTSDK" )

if(APPLE)
list (APPEND libraries
${CPPREST_LIB}
OpenSSL::SSL Boost::boost Boost::system Boost::chrono Boost::thread
)
elseif(NOT WIN32)
list (APPEND libraries
${CPPREST_LIB} Boost::system
OpenSSL::SSL
)
else()
list (APPEND libraries
${CPPREST_LIB}
)
endif()
endif()

target_link_libraries(signalrclienttests ${libraries})

add_test(NAME signalrclienttests COMMAND signalrclienttests)
31 changes: 31 additions & 0 deletions test/signalrclienttests/cpprestsdk_tests.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

#include "stdafx.h"
#include "connection_impl.h"
#include "test_utils.h"
#include "memory_log_writer.h"

using namespace signalr;

TEST(cpprestsdk, invalid_url_errors)
{
auto connection = connection_impl::create("/path", trace_level::debug, std::make_shared<memory_log_writer>(), nullptr, nullptr, false);

manual_reset_event<void> mre;
connection->start([&mre](std::exception_ptr ex)
{
mre.set(ex);
});

try
{
mre.get();
ASSERT_TRUE(false);
}
catch (const std::exception& ex)
{
ASSERT_STREQ("URI must contain a hostname.", ex.what());
}
}