@@ -35,12 +35,16 @@ CurlRaii myCurl;
3535class SimpleCurlGet {
3636
3737 int m_maxbytes;
38+ unsigned m_timeout;
3839 std::vector<char > m_data;
3940 size_t m_len{0 };
4041
4142public:
42- SimpleCurlGet (int maxbytes=1024 *1024 )
43- : m_maxbytes(maxbytes)
43+ static const unsigned default_timeout = 4 ;
44+ static const unsigned extended_timeout = 30 ;
45+
46+ SimpleCurlGet (int maxbytes=1024 *1024 , unsigned timeout=4 )
47+ : m_maxbytes(maxbytes), m_timeout(timeout)
4448 {}
4549
4650 int perform (const std::string &url) {
@@ -58,6 +62,8 @@ class SimpleCurlGet {
5862 }
5963 }
6064
65+ long timeout = m_timeout > 120 ? 120 : m_timeout;
66+
6167 CURLcode rv = curl_easy_setopt (curl, CURLOPT_URL, url.c_str ());
6268 if (rv != CURLE_OK) {
6369 throw CurlException (" Failed to set CURLOPT_URL." );
@@ -70,6 +76,10 @@ class SimpleCurlGet {
7076 if (rv != CURLE_OK) {
7177 throw CurlException (" Failed to set CURLOPT_WRITEDATA." );
7278 }
79+ rv = curl_easy_setopt (curl, CURLOPT_TIMEOUT, timeout);
80+ if (rv != CURLE_OK) {
81+ throw CurlException (" Failed to set CURLOPT_TIMEOUT." );
82+ }
7383
7484 auto res = curl_easy_perform (curl);
7585 if (res != CURLE_OK) {
@@ -467,12 +477,12 @@ SciToken::deserialize(const std::string &data, const std::vector<std::string> al
467477
468478
469479void
470- Validator::get_public_keys_from_web (const std::string &issuer, picojson::value &keys, int64_t &next_update, int64_t &expires)
480+ Validator::get_public_keys_from_web (const std::string &issuer, unsigned timeout, picojson::value &keys, int64_t &next_update, int64_t &expires)
471481{
472482 std::string openid_metadata, oauth_metadata;
473483 get_metadata_endpoint (issuer, openid_metadata, oauth_metadata);
474484
475- SimpleCurlGet cget;
485+ SimpleCurlGet cget ( 1024 * 1024 , timeout) ;
476486 auto status_code = cget.perform (openid_metadata);
477487
478488 if (status_code != 200 ) {
@@ -542,7 +552,7 @@ Validator::refresh_jwks(const std::string &issuer)
542552{
543553 int64_t next_update, expires;
544554 picojson::value keys;
545- get_public_keys_from_web (issuer, keys, next_update, expires);
555+ get_public_keys_from_web (issuer, SimpleCurlGet::default_timeout, keys, next_update, expires);
546556 return store_public_keys (issuer, keys, next_update, expires);
547557}
548558
@@ -571,14 +581,14 @@ Validator::get_public_key_pem(const std::string &issuer, const std::string &kid,
571581 if (get_public_keys_from_db (issuer, now, keys, next_update)) {
572582 if (now > next_update) {
573583 try {
574- get_public_keys_from_web (issuer, keys, next_update, expires);
584+ get_public_keys_from_web (issuer, SimpleCurlGet::default_timeout, keys, next_update, expires);
575585 store_public_keys (issuer, keys, next_update, expires);
576586 } catch (std::runtime_error &) {
577587 // ignore the exception: we have a valid set of keys already/
578588 }
579589 }
580590 } else {
581- get_public_keys_from_web (issuer, keys, next_update, expires);
591+ get_public_keys_from_web (issuer, SimpleCurlGet::extended_timeout, keys, next_update, expires);
582592 store_public_keys (issuer, keys, next_update, expires);
583593 }
584594
0 commit comments