-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcppurl.hpp
More file actions
163 lines (140 loc) · 2.87 KB
/
Copy pathcppurl.hpp
File metadata and controls
163 lines (140 loc) · 2.87 KB
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
#ifndef _CPPURL_
#define _CPPURL_
#include <curl/curl.h>
#include <string>
class CppurlInitException : public std::exception
{
virtual const char* what() const throw()
{
return "cppurl: libcurl initialization failed!";
}
};
class Cppurl
{
public:
enum ContentType
{
json,
text
};
Cppurl()
{
curl_global_init(CURL_GLOBAL_ALL);
curl = curl_easy_init();
if(!curl)
{
CppurlInitException ex;
throw ex;
}
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, callback);
userpwd = "";
}
~Cppurl()
{
curl_easy_cleanup(curl);
curl_global_cleanup();
}
void setAvoidSSL(bool value)
{
if (value)
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
}
else
{
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 1L);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 1L);
}
}
void setVerbose(bool value)
{
if (value)
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
}
else
{
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L);
}
}
void setUserAndPwd(std::string value)
{
if (value.empty() && !userpwd.empty())
{
userpwd = "";
curl_easy_setopt(curl, CURLOPT_USERPWD, NULL);
}
else
{
userpwd = value;
curl_easy_setopt(curl, CURLOPT_USERPWD, userpwd.c_str());
}
}
void setContentType(ContentType ct)
{
struct curl_slist *headers=NULL;
std::string type;
switch (ct)
{
case json:
type = "Content-Type: application/json";
break;
case text:
type = "Content-Type: application/text";
break;
}
headers = curl_slist_append(headers, type.c_str());
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headers);
}
int post(std::string url, std::string data, std::string& response)
{
return perform(url, data, response);
}
int get(std::string url, std::string& response)
{
return perform(url, "", response);
}
private:
int perform(std::string url, std::string data, std::string& response)
{
CURLcode res;
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
if (!data.empty())
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
}
std::string s;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s);
long http_code = 0;
curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &http_code);
res = curl_easy_perform(curl);
if(res != CURLE_OK)
{
response = curl_easy_strerror(res);
}
else
{
response = s;
}
return http_code;
}
static int callback(void *contents, int size, int nmemb, std::string *s)
{
int newLength = size*nmemb;
int oldLength = s->size();
try
{
s->resize(oldLength + newLength);
}
catch(std::bad_alloc &e)
{
return 0;
}
copy((char*)contents, (char*)contents+newLength, s->begin()+oldLength);
return size*nmemb;
}
CURL *curl;
std::string userpwd;
};
#endif