forked from mtrlt/Reaper_prime
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathCurl.cpp
158 lines (138 loc) · 3.79 KB
/
Curl.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
#include "Global.h"
#include "Curl.h"
#include "curl/curl.h"
#include "Util.h"
void Curl::GlobalInit()
{
curl_global_init(CURL_GLOBAL_ALL);
}
void Curl::GlobalQuit()
{
curl_global_cleanup();
}
void* Curl::Init()
{
void* curl = curl_easy_init();
if (curl == NULL)
{
throw string("libcurl initialization failure");
}
return curl;
}
void Curl::Quit(void* curl)
{
if (curl != NULL)
{
curl_easy_cleanup(curl);
}
}
size_t ResponseCallback(void *ptr, size_t size, size_t nmemb, void *data)
{
string* getworksentdata = (string*)data;
try
{
for(uint i=0; i<size*nmemb; ++i)
{
if(ptr!=NULL && data!=NULL)
{
char c = ((char*)ptr)[i];
getworksentdata->push_back(c);
}
}
}
catch(std::exception s)
{
cout << "(1) Error: " << s.what() << endl;
}
return size*nmemb;
}
string longpoll_url;
bool longpoll_active=false;
size_t HeaderCallback( void *ptr, size_t size, size_t nmemb, void *userdata)
{
string hdr;
for(uint i=0; i<size*nmemb; ++i)
{
char c = ((char*)ptr)[i];
hdr.push_back(c);
}
if (!longpoll_active && hdr.length() >= 0x10 && hdr.substr(0,0xF) == "X-Long-Polling:")
{
longpoll_url = hdr.substr(0x10);
longpoll_url = longpoll_url.substr(0, longpoll_url.length()-2);
cout << "Longpoll url -->" << longpoll_url << "<-- " << endl;
longpoll_active = true;
}
return size*nmemb;
}
string Curl::GetWork_LP(ServerSettings& s, string path, uint timeout)
{
return Execute(s,GETWORK_LP, "", path, timeout);
}
string Curl::GetWork(ServerSettings& s, string path, uint timeout)
{
return Execute(s,GETWORK, "", path, timeout);
}
string Curl::TestWork(ServerSettings& s, string work)
{
return Execute(s,TESTWORK, work, "", 30);
}
string Curl::Execute(ServerSettings& s, Curl::EXEC_TYPE type, string work, string path, uint timeout)
{
void* curl = Init();
string responsedata;
curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(curl, CURLOPT_URL, ("http://" + s.host + path).c_str());
curl_easy_setopt(curl, CURLOPT_USERPWD, (s.user + ":" + s.pass).c_str());
curl_easy_setopt(curl, CURLOPT_PORT, s.port);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ResponseCallback);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &responsedata);
if (s.proxy != "")
curl_easy_setopt(curl, CURLOPT_PROXY, s.proxy.c_str());
curl_easy_setopt(curl, CURLOPT_HEADERFUNCTION, HeaderCallback);
Execute_XPM(curl,type,work,path,timeout);
Quit(curl);
return responsedata;
}
void Curl::Execute_XPM(void* curl, Curl::EXEC_TYPE type, string work, string path, uint timeout)
{
curl_slist* headerlist = NULL;
headerlist = curl_slist_append(headerlist, "Accept: */*");
headerlist = curl_slist_append(headerlist, "Content-Type: application/json");
headerlist = curl_slist_append(headerlist, "User-Agent: reaper/" REAPER_VERSION);
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, headerlist);
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, timeout);
if (type == GETWORK_LP)
{
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, NULL);
curl_easy_setopt(curl, CURLOPT_POST, 0);
}
else if (type == GETWORK)
{
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, "{\"method\":\"getblocktemplate\",\"params\":[],\"id\":\"1\"}");
}
else if (type == TESTWORK)
{
curl_easy_setopt(curl, CURLOPT_POST, 1);
string str = work;
curl_easy_setopt(curl, CURLOPT_COPYPOSTFIELDS, str.c_str());
}
else
{
cout << "Unknown case " << (int)type << " in Curl::Execute()" << endl;
}
CURLcode code = curl_easy_perform(curl);
if(code != CURLE_OK && type != TESTWORK)
{
if (code == CURLE_COULDNT_CONNECT)
{
cout << "Could not connect. Server down?" << endl;
}
else
{
cout << "Error " << code << " getting work. See http://curl.haxx.se/libcurl/c/libcurl-errors.html for error code explanations." << endl;
}
}
curl_slist_free_all(headerlist);
}