@@ -169,6 +169,8 @@ class Server {
169
169
Server ();
170
170
virtual ~Server ();
171
171
172
+ virtual bool is_valid () const ;
173
+
172
174
Server& get (const char * pattern, Handler handler);
173
175
Server& post (const char * pattern, Handler handler);
174
176
@@ -208,6 +210,8 @@ class Client {
208
210
Client (const char * host, int port, HttpVersion http_version = HttpVersion::v1_0);
209
211
virtual ~Client ();
210
212
213
+ virtual bool is_valid () const ;
214
+
211
215
std::shared_ptr<Response> get (const char * path, Progress progress = nullptr );
212
216
std::shared_ptr<Response> get (const char * path, const Headers& headers, Progress progress = nullptr );
213
217
@@ -256,6 +260,8 @@ class SSLServer : public Server {
256
260
SSLServer (const char * cert_path, const char * private_key_path);
257
261
virtual ~SSLServer ();
258
262
263
+ virtual bool is_valid () const ;
264
+
259
265
private:
260
266
virtual bool read_and_close_socket (socket_t sock);
261
267
@@ -267,6 +273,8 @@ class SSLClient : public Client {
267
273
SSLClient (const char * host, int port, HttpVersion http_version = HttpVersion::v1_0);
268
274
virtual ~SSLClient ();
269
275
276
+ virtual bool is_valid () const ;
277
+
270
278
private:
271
279
virtual bool read_and_close_socket (socket_t sock, const Request& req, Response& res);
272
280
@@ -1216,6 +1224,10 @@ inline void Server::set_logger(Logger logger)
1216
1224
1217
1225
inline bool Server::listen (const char * host, int port, int socket_flags)
1218
1226
{
1227
+ if (!is_valid ()) {
1228
+ return false ;
1229
+ }
1230
+
1219
1231
svr_sock_ = detail::create_server_socket (host, port, socket_flags);
1220
1232
if (svr_sock_ == -1 ) {
1221
1233
return false ;
@@ -1405,6 +1417,11 @@ inline void Server::process_request(Stream& strm)
1405
1417
write_response (strm, req, res);
1406
1418
}
1407
1419
1420
+ inline bool Server::is_valid () const
1421
+ {
1422
+ return true ;
1423
+ }
1424
+
1408
1425
inline bool Server::read_and_close_socket (socket_t sock)
1409
1426
{
1410
1427
return detail::read_and_close_socket (sock, [this ](Stream& strm) {
@@ -1426,6 +1443,11 @@ inline Client::~Client()
1426
1443
{
1427
1444
}
1428
1445
1446
+ inline bool Client::is_valid () const
1447
+ {
1448
+ return true ;
1449
+ }
1450
+
1429
1451
inline bool Client::read_response_line (Stream& strm, Response& res)
1430
1452
{
1431
1453
const auto bufsiz = 2048 ;
@@ -1610,6 +1632,9 @@ template <typename U, typename V, typename T>
1610
1632
inline bool read_and_close_socket_ssl (socket_t sock, SSL_CTX* ctx, U SSL_connect_or_accept, V setup, T callback)
1611
1633
{
1612
1634
auto ssl = SSL_new (ctx);
1635
+ if (!ssl) {
1636
+ return false ;
1637
+ }
1613
1638
1614
1639
auto bio = BIO_new_socket (sock, BIO_NOCLOSE);
1615
1640
SSL_set_bio (ssl, bio, bio);
@@ -1693,6 +1718,11 @@ inline SSLServer::~SSLServer()
1693
1718
}
1694
1719
}
1695
1720
1721
+ inline bool SSLServer::is_valid () const
1722
+ {
1723
+ return ctx_;
1724
+ }
1725
+
1696
1726
inline bool SSLServer::read_and_close_socket (socket_t sock)
1697
1727
{
1698
1728
return detail::read_and_close_socket_ssl (
@@ -1719,9 +1749,14 @@ inline SSLClient::~SSLClient()
1719
1749
}
1720
1750
}
1721
1751
1752
+ inline bool SSLClient::is_valid () const
1753
+ {
1754
+ return ctx_;
1755
+ }
1756
+
1722
1757
inline bool SSLClient::read_and_close_socket (socket_t sock, const Request& req, Response& res)
1723
1758
{
1724
- return detail::read_and_close_socket_ssl (
1759
+ return is_valid () && detail::read_and_close_socket_ssl (
1725
1760
sock, ctx_,
1726
1761
SSL_connect,
1727
1762
[&](SSL* ssl) {
0 commit comments