@@ -35,7 +35,7 @@ void RestClient::setAuth(const std::string& user,const std::string& password){
3535 *
3636 * @return response struct
3737 */
38- RestClient::response RestClient::get (const std::string& url)
38+ RestClient::response RestClient::get (const std::string& url, const size_t timeout )
3939{
4040 /* * create return struct */
4141 RestClient::response ret = {};
@@ -65,9 +65,22 @@ RestClient::response RestClient::get(const std::string& url)
6565 /* * callback object for headers */
6666 curl_easy_setopt (curl, CURLOPT_HEADERDATA, &ret);
6767 /* * perform the actual query */
68+
69+ // set timeout
70+ if (timeout) {
71+ curl_easy_setopt (curl, CURLOPT_TIMEOUT, timeout);
72+ curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1 ); // dont want to get a sig alarm on timeout
73+ }
74+
6875 res = curl_easy_perform (curl);
6976 if (res != CURLE_OK)
7077 {
78+ if (res == CURLE_OPERATION_TIMEDOUT) {
79+ ret.code = res;
80+ ret.body = " Operation Timeout." ;
81+ return ret;
82+ }
83+
7184 ret.body = " Failed to query." ;
7285 ret.code = -1 ;
7386 return ret;
@@ -93,7 +106,8 @@ RestClient::response RestClient::get(const std::string& url)
93106 */
94107RestClient::response RestClient::post (const std::string& url,
95108 const std::string& ctype,
96- const std::string& data)
109+ const std::string& data,
110+ const size_t timeout)
97111{
98112 /* * create return struct */
99113 RestClient::response ret = {};
@@ -133,10 +147,23 @@ RestClient::response RestClient::post(const std::string& url,
133147 curl_slist* header = NULL ;
134148 header = curl_slist_append (header, ctype_header.c_str ());
135149 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, header);
150+
151+ // set timeout
152+ if (timeout) {
153+ curl_easy_setopt (curl, CURLOPT_TIMEOUT, timeout);
154+ curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1 ); // dont want to get a sig alarm on timeout
155+ }
156+
136157 /* * perform the actual query */
137158 res = curl_easy_perform (curl);
138159 if (res != CURLE_OK)
139160 {
161+ if (res == CURLE_OPERATION_TIMEDOUT) {
162+ ret.code = res;
163+ ret.body = " Operation Timeout." ;
164+ return ret;
165+ }
166+
140167 ret.body = " Failed to query." ;
141168 ret.code = -1 ;
142169 return ret;
@@ -163,7 +190,7 @@ RestClient::response RestClient::post(const std::string& url,
163190 */
164191RestClient::response RestClient::put (const std::string& url,
165192 const std::string& ctype,
166- const std::string& data)
193+ const std::string& data, const size_t timeout )
167194{
168195 /* * create return struct */
169196 RestClient::response ret = {};
@@ -214,10 +241,23 @@ RestClient::response RestClient::put(const std::string& url,
214241 curl_slist* header = NULL ;
215242 header = curl_slist_append (header, ctype_header.c_str ());
216243 curl_easy_setopt (curl, CURLOPT_HTTPHEADER, header);
244+
245+ // set timeout
246+ if (timeout) {
247+ curl_easy_setopt (curl, CURLOPT_TIMEOUT, timeout);
248+ curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1 ); // dont want to get a sig alarm on timeout
249+ }
250+
217251 /* * perform the actual query */
218252 res = curl_easy_perform (curl);
219253 if (res != CURLE_OK)
220254 {
255+ if (res == CURLE_OPERATION_TIMEDOUT) {
256+ ret.code = res;
257+ ret.body = " Operation Timeout." ;
258+ return ret;
259+ }
260+
221261 ret.body = " Failed to query." ;
222262 ret.code = -1 ;
223263 return ret;
@@ -240,7 +280,7 @@ RestClient::response RestClient::put(const std::string& url,
240280 *
241281 * @return response struct
242282 */
243- RestClient::response RestClient::del (const std::string& url)
283+ RestClient::response RestClient::del (const std::string& url, const size_t timeout )
244284{
245285 /* * create return struct */
246286 RestClient::response ret = {};
@@ -274,10 +314,23 @@ RestClient::response RestClient::del(const std::string& url)
274314 curl_easy_setopt (curl, CURLOPT_HEADERFUNCTION, RestClient::header_callback);
275315 /* * callback object for headers */
276316 curl_easy_setopt (curl, CURLOPT_HEADERDATA, &ret);
317+
318+ // set timeout
319+ if (timeout) {
320+ curl_easy_setopt (curl, CURLOPT_TIMEOUT, timeout);
321+ curl_easy_setopt (curl, CURLOPT_NOSIGNAL, 1 ); // dont want to get a sig alarm on timeout
322+ }
323+
277324 /* * perform the actual query */
278325 res = curl_easy_perform (curl);
279326 if (res != CURLE_OK)
280327 {
328+ if (res == CURLE_OPERATION_TIMEDOUT) {
329+ ret.code = res;
330+ ret.body = " Operation Timeout." ;
331+ return ret;
332+ }
333+
281334 ret.body = " Failed to query." ;
282335 ret.code = -1 ;
283336 return ret;
0 commit comments