-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttp.h
More file actions
118 lines (94 loc) · 2.89 KB
/
Copy pathHttp.h
File metadata and controls
118 lines (94 loc) · 2.89 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
#ifndef DATASERVICES_FEED_HTTP
#define DATASERVICES_FEED_HTTP
#include <iostream>
#include <string>
#include <curl/curl.h>
#include <list>
#include <map>
#include "HttpException.h"
namespace Http{
using paramsmap = std::map<std::string, std::string>;
using std::string;
inline size_t writeFunction(void *ptr, size_t size, size_t nmemb, string *data) {
data->append((char*) ptr, size * nmemb);
return size * nmemb;
}
inline void appendHeaders(CURL *curl, paramsmap headers){
struct curl_slist *list = NULL;
for ( auto const& elem : headers){
string item = elem.first+": "+elem.second;
list = curl_slist_append(list, item.c_str());
}
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, list);
}
inline string mapToURL(const paramsmap &map){
if (map.empty()){
return "";
}
string query;
for(const auto ¶m:map)
query+="&"+param.first+"="+param.second;
query.erase(0,1);
return query;
}
inline bool defaultConfig(CURL *curl, const string &url, const string *header, const string *body, const paramsmap &headerMap){
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if(curl){
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, Http::writeFunction);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, body);
curl_easy_setopt(curl, CURLOPT_HEADERDATA, header);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "curl/7.42.0");
Http::appendHeaders(curl, headerMap);
return true;
}else{
curl_easy_cleanup(curl);
throw Http::Exception("Curl can't init");
}
}
inline string get(const string &url, const paramsmap &headerMap={}, string* headerPtr=nullptr){
string body;
string header;
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if(defaultConfig(curl, url, &header, &body, headerMap)){
auto res = curl_easy_perform(curl);
if(res != CURLE_OK){
std::cout<<curl_easy_strerror(res);
throw Http::Exception("Curl perform failed: ");
}
else{
if(headerPtr)
*headerPtr = header;
return body;
}
}else{
curl_easy_cleanup(curl);
throw Http::Exception("Curl config can't init");
}
}
inline string post(const string &url, const paramsmap ¶ms, const paramsmap& headerMap={}, string* headerPtr=nullptr){
string body;
string header;
CURL* curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L);
if(defaultConfig(curl, url, &header, &body, headerMap)){
string data = mapToURL(params);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, data.c_str());
auto res = curl_easy_perform(curl);
if(res != CURLE_OK){
std::cout<<curl_easy_strerror(res);
throw Http::Exception("Curl perform failed: ");
}
else{
if(headerPtr)
*headerPtr = header;
return body;
}
}else{
curl_easy_cleanup(curl);
throw Http::Exception("Curl config can't init");
}
}
}
#endif //DATASERVICES_FEED_HTTP