Skip to content

Commit 34cc7ef

Browse files
committed
Merge pull request mrtazz#14 from 69736c616d/master
timeout for whole rest operations
2 parents 67a0ff4 + fa129a8 commit 34cc7ef

6 files changed

Lines changed: 92 additions & 8 deletions

File tree

include/restclient-cpp/restclient.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,15 @@ class RestClient
4141
static void clearAuth();
4242
static void setAuth(const std::string& user,const std::string& password);
4343
// HTTP GET
44-
static response get(const std::string& url);
44+
static response get(const std::string& url, const size_t timeout = 0);
4545
// HTTP POST
4646
static response post(const std::string& url, const std::string& ctype,
47-
const std::string& data);
47+
const std::string& data, const size_t timeout = 0);
4848
// HTTP PUT
4949
static response put(const std::string& url, const std::string& ctype,
50-
const std::string& data);
50+
const std::string& data, const size_t timeout = 0);
5151
// HTTP DELETE
52-
static response del(const std::string& url);
52+
static response del(const std::string& url, const size_t timeout = 0);
5353

5454
private:
5555
// writedata callback function

source/restclient.cpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*/
94107
RestClient::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
*/
164191
RestClient::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;

test/test_restclient_delete.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,11 @@ TEST_F(RestClientDeleteTest, TestRestClientDeleteHeaders)
6161
RestClient::response res = RestClient::del(url);
6262
EXPECT_EQ("keep-alive", res.headers["Connection"]);
6363
}
64+
65+
/*TEST_F(RestClientDeleteTest, TestRestClientDeleteTimeout)
66+
{
67+
std::string u = "http://httpbin.org/delay/10";
68+
RestClient::response res = RestClient::del(u, 5);
69+
EXPECT_EQ(28, res.code);
70+
}*/
71+

test/test_restclient_get.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,11 @@ TEST_F(RestClientGetTest, TestRestClientGETHeaders)
6363
EXPECT_EQ("keep-alive", res.headers["Connection"]);
6464
}
6565

66+
TEST_F(RestClientGetTest, TestRestClientGETTimeout)
67+
{
68+
std::string u = "http://httpbin.org/delay/10";
69+
RestClient::response res = RestClient::get(u, 5);
70+
EXPECT_EQ(28, res.code);
71+
}
72+
73+

test/test_restclient_post.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,11 @@ TEST_F(RestClientPostTest, TestRestClientPOSTHeaders)
6161
RestClient::response res = RestClient::post(url, ctype, data);
6262
EXPECT_EQ("keep-alive", res.headers["Connection"]);
6363
}
64+
65+
/*TEST_F(RestClientPostTest, TestRestClientPOSTTimeout)
66+
{
67+
std::string u = "http://httpbin.org/delay/10";
68+
RestClient::response res = RestClient::post(u, ctype, data, 5);
69+
EXPECT_EQ(28, res.code);
70+
}*/
71+

test/test_restclient_put.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,10 @@ TEST_F(RestClientPutTest, TestRestClientPUTHeaders)
6161
RestClient::response res = RestClient::put(url, ctype, data);
6262
EXPECT_EQ("keep-alive", res.headers["Connection"]);
6363
}
64+
65+
/*TEST_F(RestClientPutTest, TestRestClientPUTTimeout)
66+
{
67+
std::string u = "http://httpbin.org/delay/10";
68+
RestClient::response res = RestClient::put(u, ctype, data, 5);
69+
EXPECT_EQ(28, res.code);
70+
}*/

0 commit comments

Comments
 (0)