-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.cpp
138 lines (128 loc) · 3.5 KB
/
net.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
#include <curl/curl.h>
#include "common.h"
#include<iostream>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#define net_agent "EVGESHAd agent"
/* Init network settings */
void net::init()
{
curl_global_init(CURL_GLOBAL_ALL);
}
string net::urlEncode(string str)
{
char* esc_text = curl_easy_escape(NULL, str.c_str(), str.length());
string result = esc_text;
curl_free(esc_text);
return result;
}
string net::send(string url, table param, bool post)
{
string paramline = "";
table *params = ¶m;
for (auto iter = params->begin(); iter != params->end(); iter++) {
paramline += iter->first + "=" + urlEncode(iter->second) + "&";
}
if(post)
return net::send(url, paramline);
return net::send(url+"?"+paramline);
}
string buffer;
size_t 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;
}
string net::send(string url, string params)
{
buffer ="";
CURL *curl;
CURLcode result;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, net_agent);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
if (params != "") {
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
}
result = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
cout << endl << other::getRealTime() << ": " << url << "-" << params << endl << " " << buffer << endl;
if (result == CURLE_OK)
return buffer;
return net::send(url, params);
}
size_t write_data(void *ptr, size_t size, size_t nmemb, void *stream)
{
size_t written = fwrite(ptr, size, nmemb, (FILE *)stream);
return written;
}
string net::upload(string url, string filename, string params)
{
buffer ="";
CURL *curl;
CURLcode result;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, net_agent);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writer);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, &buffer);
if (params != "") {
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
}
struct curl_httppost *formpost=NULL;
struct curl_httppost *lastptr=NULL;
curl_formadd(&formpost,
&lastptr,
CURLFORM_COPYNAME, "file",
CURLFORM_FILENAME, filename.c_str(),
CURLFORM_FILE, filename.c_str(),
CURLFORM_CONTENTTYPE, "multipart/form-data",
CURLFORM_END);
curl_easy_setopt(curl, CURLOPT_HTTPPOST, formpost);
result = curl_easy_perform(curl);
}
curl_easy_cleanup(curl);
//cout << endl << other::getRealTime() << ": " << url << "-" << params << endl << " " << buffer << endl;
if (result == CURLE_OK)
return buffer;
return "";
}
void net::download(string url, string filename, string params)
{
CURL *curl;
FILE *file = NULL;
curl = curl_easy_init();
if (curl)
{
curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
curl_easy_setopt(curl, CURLOPT_USERAGENT, net_agent);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_data);
if (params != "") {
curl_easy_setopt(curl, CURLOPT_POST, 1);
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, params.c_str());
}
file = fopen(filename.c_str(), "wb");
if(file){
curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
curl_easy_perform(curl);
fclose(file);
}
}
curl_easy_cleanup(curl);
return;
}