Skip to content

Commit 5f19a1f

Browse files
committed
tcp endpoint created before server creation, invalid address error is logged now, server runs into timeout in that case
added testcase for invalid address
1 parent 551bcda commit 5f19a1f

File tree

3 files changed

+43
-16
lines changed

3 files changed

+43
-16
lines changed

include/crow/app.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ namespace crow
365365
}
366366

367367
/// \brief Get the number of threads that server is using
368-
std::uint16_t concurrency()
368+
std::uint16_t concurrency() const
369369
{
370370
return concurrency_;
371371
}
@@ -506,11 +506,18 @@ namespace crow
506506
#endif
507507
validate();
508508

509+
error_code ec;
510+
asio::ip::address addr = asio::ip::make_address(bindaddr_,ec);
511+
if (ec){
512+
CROW_LOG_ERROR << ec.message() << " - Can not create valid ip address from string: \"" << bindaddr_ << "\"";
513+
return;
514+
}
515+
tcp::endpoint endpoint(addr, port_);
509516
#ifdef CROW_ENABLE_SSL
510517
if (ssl_used_)
511518
{
512519
router_.using_ssl = true;
513-
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
520+
ssl_server_ = std::move(std::unique_ptr<ssl_server_t>(new ssl_server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, &ssl_context_)));
514521
ssl_server_->set_tick_function(tick_interval_, tick_function_);
515522
ssl_server_->signal_clear();
516523
for (auto snum : signals_)
@@ -523,7 +530,7 @@ namespace crow
523530
else
524531
#endif
525532
{
526-
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, bindaddr_, port_, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
533+
server_ = std::move(std::unique_ptr<server_t>(new server_t(this, endpoint, server_name_, &middlewares_, concurrency_, timeout_, nullptr)));
527534
server_->set_tick_function(tick_interval_, tick_function_);
528535
for (auto snum : signals_)
529536
{

include/crow/http_server.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,20 @@ namespace crow // NOTE: Already documented in "crow/app.h"
4242
class Server
4343
{
4444
public:
45-
Server(Handler* handler, std::string bindaddr, uint16_t port, std::string server_name = std::string("Crow/") + VERSION, std::tuple<Middlewares...>* middlewares = nullptr, uint16_t concurrency = 1, uint8_t timeout = 5, typename Adaptor::context* adaptor_ctx = nullptr):
46-
acceptor_(io_context_, tcp::endpoint(asio::ip::make_address(bindaddr), port)),
45+
Server(Handler* handler,
46+
const tcp::endpoint& endpoint,
47+
std::string server_name = std::string("Crow/") + VERSION,
48+
std::tuple<Middlewares...>* middlewares = nullptr,
49+
uint16_t concurrency = 1,
50+
uint8_t timeout = 5,
51+
typename Adaptor::context* adaptor_ctx = nullptr):
52+
acceptor_(io_context_,endpoint),
4753
signals_(io_context_),
4854
tick_timer_(io_context_),
4955
handler_(handler),
5056
concurrency_(concurrency),
5157
timeout_(timeout),
5258
server_name_(server_name),
53-
port_(port),
54-
bindaddr_(bindaddr),
5559
task_queue_length_pool_(concurrency_ - 1),
5660
middlewares_(middlewares),
5761
adaptor_ctx_(adaptor_ctx)
@@ -150,11 +154,12 @@ namespace crow // NOTE: Already documented in "crow/app.h"
150154
});
151155
}
152156

153-
port_ = acceptor_.local_endpoint().port();
154-
handler_->port(port_);
157+
handler_->port(acceptor_.local_endpoint().port());
155158

156159

157-
CROW_LOG_INFO << server_name_ << " server is running at " << (handler_->ssl_used() ? "https://" : "http://") << bindaddr_ << ":" << acceptor_.local_endpoint().port() << " using " << concurrency_ << " threads";
160+
CROW_LOG_INFO << server_name_
161+
<< " server is running at " << (handler_->ssl_used() ? "https://" : "http://")
162+
<< acceptor_.local_endpoint().address() << ":" << acceptor_.local_endpoint().port() << " using " << concurrency_ << " threads";
158163
CROW_LOG_INFO << "Call `app.loglevel(crow::LogLevel::Warning)` to hide Info level logs.";
159164

160165
signals_.async_wait(
@@ -192,7 +197,7 @@ namespace crow // NOTE: Already documented in "crow/app.h"
192197
io_context_.stop(); // Close main io_service
193198
}
194199

195-
uint16_t port(){
200+
uint16_t port() const {
196201
return acceptor_.local_endpoint().port();
197202
}
198203

@@ -293,8 +298,6 @@ namespace crow // NOTE: Already documented in "crow/app.h"
293298
uint16_t concurrency_{2};
294299
std::uint8_t timeout_;
295300
std::string server_name_;
296-
uint16_t port_;
297-
std::string bindaddr_;
298301
std::vector<std::atomic<unsigned int>> task_queue_length_pool_;
299302

300303
std::chrono::milliseconds tick_interval_;

tests/unittest.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,22 @@ TEST_CASE("server_handling_error_request")
643643
app.stop();
644644
} // server_handling_error_request
645645

646-
TEST_CASE("server_dynamic_port_allication")
646+
TEST_CASE("server_invalid_ip_address")
647+
{
648+
SimpleApp app;
649+
CROW_ROUTE(app, "/")
650+
([] {
651+
return "A";
652+
});
653+
auto _ = app.bindaddr("192.").port(45451).run_async();
654+
auto state = app.wait_for_server_start();
655+
656+
// we should run into a timeout as the server will not started
657+
CHECK(state==cv_status::timeout);
658+
} // server_invalid_ip_address
659+
660+
661+
TEST_CASE("server_dynamic_port_allocation")
647662
{
648663
SimpleApp app;
649664
CROW_ROUTE(app, "/")
@@ -659,7 +674,7 @@ TEST_CASE("server_dynamic_port_allication")
659674
asio::ip::make_address(LOCALHOST_ADDRESS), app.port()));
660675
}
661676
app.stop();
662-
} // server_dynamic_port_allication
677+
} // server_dynamic_port_allocation
663678

664679
TEST_CASE("server_handling_error_request_http_version")
665680
{
@@ -1520,7 +1535,9 @@ struct NullSimpleMiddleware
15201535
TEST_CASE("middleware_simple")
15211536
{
15221537
App<NullMiddleware, NullSimpleMiddleware> app;
1523-
decltype(app)::server_t server(&app, LOCALHOST_ADDRESS, 45451);
1538+
asio::ip::address adr = asio::ip::make_address(LOCALHOST_ADDRESS);
1539+
tcp::endpoint ep(adr,45451);
1540+
decltype(app)::server_t server(&app, ep);
15241541
CROW_ROUTE(app, "/")
15251542
([&](const crow::request& req) {
15261543
app.get_context<NullMiddleware>(req);

0 commit comments

Comments
 (0)