-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHTTP.cpp
162 lines (146 loc) · 4.14 KB
/
HTTP.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
#include "HTTP.hpp"
namespace HTTP
{
std::string contentType = "application/x-www-form-urlencoded";
}
#define SETCONTENTTYPE(curl, contentType) \
{ \
struct curl_slist* __SETCONTENTTYPE_hs = NULL; \
__SETCONTENTTYPE_hs = curl_slist_append(__SETCONTENTTYPE_hs, ("Content-Type: " + std::string(contentType)).c_str()); \
curl_easy_setopt(curl, CURLOPT_HTTPHEADER, __SETCONTENTTYPE_hs); \
}
struct httpWriteData
{
char* output;
size_t outputSize;
size_t outputBytesUsed;
};
size_t httpWriteCallback(void* contents, size_t __one, size_t nmemb, void* userp)
{
httpWriteData* d = (httpWriteData*)userp;
if (!d->output) return 0;
// resize output
if (d->outputBytesUsed + nmemb > d->outputSize)
{
void* re = realloc(d->output, d->outputBytesUsed + nmemb);
if (!re)
{
free(d->output);
return 0;
}
d->output = (char*)re;
}
// write to output
memcpy(d->output + d->outputBytesUsed, contents, nmemb);
d->outputBytesUsed += nmemb;
return nmemb;
}
bool HTTP::init()
{
return curl_global_init(CURL_GLOBAL_ALL) == CURLE_OK;
}
void HTTP::cleanup()
{
curl_global_cleanup();
}
char* HTTP::GET(std::string URL, size_t* bytesRead)
{
CURL* c = curl_easy_init();
if (!c) return nullptr;
CURLcode err;
httpWriteData d{};
if (!(d.output = (char*)malloc(64))) return nullptr;
d.outputSize = 64;
d.outputBytesUsed = 0;
SETCONTENTTYPE(c, HTTP::contentType);
curl_easy_setopt(c, CURLOPT_URL, URL.c_str());
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, httpWriteCallback);
curl_easy_setopt(c, CURLOPT_WRITEDATA, &d);
err = curl_easy_perform(c);
curl_easy_cleanup(c);
if (err != CURLE_OK)
{
free(d.output);
if (bytesRead) *bytesRead = 0;
return nullptr;
}
if (d.output)
{
if (d.outputBytesUsed == 0)
{
// transferred empty file
ZeroMemory(d.output, d.outputSize);
if (bytesRead) *bytesRead = 0;
return nullptr;
}
else if (d.outputBytesUsed < d.outputSize)
{
void* re = realloc(d.output, d.outputBytesUsed);
if (!re)
{
free(d.output);
if (bytesRead) *bytesRead = 0;
return nullptr;
}
d.output = (char*)re;
d.outputSize = d.outputBytesUsed;
}
if (bytesRead && d.output) *bytesRead = d.outputBytesUsed;
return d.output;
}
if (bytesRead) *bytesRead = 0;
return nullptr;
}
char* HTTP::POST(std::string URL, size_t* bytesRead, char* postData, size_t postDataLength)
{
CURL* c = curl_easy_init();
if (!c) return nullptr;
CURLcode err;
httpWriteData d{};
if (!(d.output = (char*)malloc(64))) return nullptr;
d.outputSize = 64;
d.outputBytesUsed = 0;
SETCONTENTTYPE(c, HTTP::contentType);
curl_easy_setopt(c, CURLOPT_URL, URL.c_str());
if (postData)
{
curl_easy_setopt(c, CURLOPT_POSTFIELDS, postData);
curl_easy_setopt(c, CURLOPT_POSTFIELDSIZE, postDataLength);
}
curl_easy_setopt(c, CURLOPT_WRITEFUNCTION, httpWriteCallback);
curl_easy_setopt(c, CURLOPT_WRITEDATA, &d);
err = curl_easy_perform(c);
curl_easy_cleanup(c);
if (err != CURLE_OK)
{
free(d.output);
if (bytesRead) *bytesRead = 0;
return nullptr;
}
if (d.output)
{
if (d.outputBytesUsed == 0)
{
// transferred empty file
ZeroMemory(d.output, d.outputSize);
if (bytesRead) *bytesRead = 0;
return nullptr;
}
else if (d.outputBytesUsed < d.outputSize)
{
void* re = realloc(d.output, d.outputBytesUsed);
if (!re)
{
free(d.output);
if (bytesRead) *bytesRead = 0;
return nullptr;
}
d.output = (char*)re;
d.outputSize = d.outputBytesUsed;
}
if (bytesRead && d.output) *bytesRead = d.outputBytesUsed;
return d.output;
}
if (bytesRead) *bytesRead = 0;
return nullptr;
}