Skip to content

Commit 0adcc6b

Browse files
authored
Fix socket wait with OpenSSL 3.0.14 (#605)
IB-8121 Signed-off-by: Raul Metsma <[email protected]>
1 parent c43bd94 commit 0adcc6b

File tree

1 file changed

+21
-16
lines changed

1 file changed

+21
-16
lines changed

src/crypto/Connect.cpp

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,17 +56,6 @@ using namespace std;
5656
throw ex; \
5757
}
5858

59-
#if OPENSSL_VERSION_NUMBER < 0x30000000L
60-
int BIO_socket_wait(int fd, bool read, time_t timeout)
61-
{
62-
fd_set confds;
63-
FD_ZERO(&confds);
64-
FD_SET(fd, &confds);
65-
timeval tv { timeout, 0 };
66-
return select(fd + 1, read ? &confds : nullptr, read ? nullptr : &confds, nullptr, &tv);
67-
}
68-
#endif
69-
7059

7160

7261
Connect::Connect(const string &_url, string _method, int _timeout, const vector<X509Cert> &certs)
@@ -111,6 +100,7 @@ Connect::Connect(const string &_url, string _method, int _timeout, const vector<
111100

112101
BIO_set_nbio(d, _timeout > 0);
113102
auto start = chrono::high_resolution_clock::now();
103+
#if OPENSSL_VERSION_NUMBER < 0x30000000L
114104
while(BIO_do_connect(d) != 1)
115105
{
116106
if(_timeout == 0)
@@ -122,6 +112,10 @@ Connect::Connect(const string &_url, string _method, int _timeout, const vector<
122112
THROW_NETWORKEXCEPTION("Failed to create connection with host timeout: '%s'", hostname.c_str())
123113
this_thread::sleep_for(chrono::milliseconds(50));
124114
}
115+
#else
116+
if(BIO_do_connect_retry(d, timeout, -1) < 1)
117+
THROW_NETWORKEXCEPTION("Failed to create connection with host timeout: '%s'", hostname.c_str())
118+
#endif
125119

126120
if(usessl > 0)
127121
{
@@ -137,7 +131,7 @@ Connect::Connect(const string &_url, string _method, int _timeout, const vector<
137131
doProxyConnect = false;
138132
}
139133

140-
ssl.reset(SSL_CTX_new(SSLv23_client_method()), SSL_CTX_free);
134+
ssl.reset(SSL_CTX_new(TLS_client_method()), SSL_CTX_free);
141135
if(!ssl)
142136
THROW_NETWORKEXCEPTION("Failed to create ssl connection with host: '%s'", hostname.c_str())
143137
SSL_CTX_set_mode(ssl.get(), SSL_MODE_AUTO_RETRY);
@@ -179,9 +173,19 @@ Connect::Connect(const string &_url, string _method, int _timeout, const vector<
179173
}
180174
}
181175

182-
if(int fd = BIO_get_fd(d, nullptr);
183-
_timeout > 0 && BIO_socket_wait(fd, BIO_should_read(d), _timeout) == -1)
184-
DEBUG("select failed");
176+
#if OPENSSL_VERSION_NUMBER > 0x30000000L
177+
if(_timeout > 0)
178+
{
179+
int fd = BIO_get_fd(d, nullptr);
180+
fd_set confds;
181+
FD_ZERO(&confds);
182+
FD_SET(fd, &confds);
183+
timeval tv { timeout, 0 };
184+
int read = BIO_should_read(d);
185+
if(select(fd + 1, read ? &confds : nullptr, read ? nullptr : &confds, nullptr, &tv) == -1)
186+
DEBUG("select failed");
187+
}
188+
#endif
185189

186190
BIO_printf(d, "%s %s HTTP/1.1\r\n", method.c_str(), path.c_str());
187191
addHeader("Connection", "close");
@@ -333,7 +337,8 @@ Connect::Result Connect::exec(initializer_list<pair<string_view,string_view>> he
333337

334338
if(!r.isRedirect() || recursive > 3)
335339
return r;
336-
string url = r.headers["Location"].find("://") != string::npos ? r.headers["Location"] : baseurl + r.headers["Location"];
340+
string location = r.headers.find("Location") == r.headers.cend() ? r.headers["location"] : r.headers["Location"];
341+
string url = location.find("://") != string::npos ? location : baseurl + location;
337342
Connect c(url, method, timeout);
338343
c.recursive = recursive + 1;
339344
return c.exec(headers);

0 commit comments

Comments
 (0)