forked from laurentluce/twilio-cplusplus
/
Rest.cpp
295 lines (253 loc) · 6.83 KB
/
Rest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
/*
* Twilio REST C++
* Author: Laurent Luce
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
using namespace std;
#include "Utils.h"
#include "Rest.h"
using namespace twilio;
/**
* HTTP request
* @param path URL path
* @param method HTTP method: "GET", "POST", "PUT", "DELETE"
* @param vars URL attributes or POST attributes
* @return HTTP response
*/
string Rest::request(const string& path, const string& method, const vector<Var>& vars)
{
string response, m;
if (path.length() <= 0)
throw "Path is empty";
m = method;
transform(m.begin(), m.end(), m.begin(), ::toupper);
if ((m != "GET" && m != "POST"
&& m != "PUT" && m != "DELETE"))
{
throw "Invalid m parameter";
}
string url = build_uri(path);
if(m == "GET")
{
response = get(url, vars);
}
else if(m == "POST")
{
response = post(url, vars);
}
else if(m == "PUT")
{
response = put(url, vars[0].value);
}
else if(m == "DELETE")
{
response = tdelete(url);
}
return response;
}
/**
* Curl write function
* @param data data received from server
* @param size data size is size * nmemb
* @param nmemb data size is size * nmemb
* @param buffer
*/
static int writer(char *data, size_t size, size_t nmemb, string *buffer)
{
int result = 0;
if (buffer != NULL)
{
buffer->append(data, size * nmemb);
result = size * nmemb;
}
return result;
}
/**
* Curl read callback function
* @param ptr pointer to storage
* @param size data size is size * nmemb
* @param nmemb data size is size * nmemb
* @param userdata read from stream
*/
static size_t read_callback(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t retcode;
retcode = fread((FILE*)ptr, size, nmemb, (FILE*)stream);
return retcode;
}
/**
* HTTP GET request
* @param url HTTP request URL
* @param vars URL attributes or POST attributes
* @return HTTP response
*/
string Rest::get(const string& url, const vector<Var>& vars)
{
string query = "";
string u;
for(unsigned int i = 0; i < vars.size(); i++)
{
query += "&" + vars[i].key + "=" + vars[i].value;
}
if (query.length() > 0)
u = url + "?" + query.substr(1);
CURL *curl;
CURLcode res;
tbuffer = "HTTP get error";
curl = curl_easy_init();
if(curl) {
//url = curl_easy_escape(curl, url.c_str(), url.length());
string sAuth = tid + ":" + ttoken;
curl_easy_setopt(curl, CURLOPT_URL, u.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, sAuth.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
tbuffer = "";
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tbuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK)
return tbuffer;
}
return tbuffer;
}
/**
* HTTP POST request
* @param url HTTP request URL
* @param vars POST attributes
* @return HTTP response
*/
string Rest::post(const string& url, const vector<Var>& vars)
{
CURL *curl;
CURLcode res;
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
struct curl_slist *headerlist = NULL;
static const char buf[] = "Expect:";
curl_global_init(CURL_GLOBAL_ALL);
// Post data
for(unsigned int i = 0; i < vars.size(); i++)
{
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, vars[i].key.c_str(),
CURLFORM_COPYCONTENTS, vars[i].value.c_str(),
CURLFORM_END);
}
tbuffer = "HTTP post error";
curl = curl_easy_init();
headerlist = curl_slist_append(headerlist, buf);
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
string sAuth = tid + ":" + ttoken;
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, sAuth.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
tbuffer = "";
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tbuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_formfree(formpost);
curl_slist_free_all (headerlist);
curl_global_cleanup();
if (res == CURLE_OK)
return tbuffer;
}
return tbuffer;
}
/**
* HTTP PUT request
* @param url HTTP request URL
* @param filename File to read data from
* @return HTTP response
*/
string Rest::put(const string& url, const string& filename)
{
CURL *curl;
CURLcode res;
FILE * hd_src ;
int hd ;
struct stat file_info;
// get local file size
hd = open(filename.c_str(), O_RDONLY);
fstat(hd, &file_info);
close(hd);
hd_src = fopen(filename.c_str(), "rb");
curl_global_init(CURL_GLOBAL_ALL);
tbuffer = "HTTP put error";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
string sAuth = tid + ":" + ttoken;
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
curl_easy_setopt(curl, CURLOPT_USERPWD, sAuth.c_str());
curl_easy_setopt(curl, CURLOPT_READFUNCTION, read_callback);
curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
curl_easy_setopt(curl, CURLOPT_READDATA, hd_src);
curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE,
(curl_off_t)file_info.st_size);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
tbuffer = "";
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tbuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
curl_global_cleanup();
fclose(hd_src);
if (res == CURLE_OK)
return tbuffer;
}
fclose(hd_src);
return tbuffer;
}
/**
* HTTP DELETE request
* @param url HTTP request URL
* @return HTTP response
*/
string Rest::tdelete(const string& url)
{
CURL *curl;
CURLcode res;
tbuffer = "HTTP delete error";
curl = curl_easy_init();
if(curl) {
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
string sAuth = tid + ":" + ttoken;
curl_easy_setopt(curl, CURLOPT_USERPWD, sAuth.c_str());
curl_easy_setopt(curl, CURLOPT_CUSTOMREQUEST, "DELETE");
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
tbuffer = "";
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &tbuffer);
res = curl_easy_perform(curl);
curl_easy_cleanup(curl);
if (res == CURLE_OK)
return tbuffer;
}
return tbuffer;
}
/**
* Prefix path with Twilio URL
* @param path URL path
* @return full URL
*/
string Rest::build_uri(const string& path) const
{
if (path[0] == '/')
return TWILIO_API_URL + path;
else
return TWILIO_API_URL + "/" + path;
}
const string Rest::TWILIO_API_URL = "https://api.twilio.com";