-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttpmessaging.cpp
More file actions
274 lines (234 loc) · 7.61 KB
/
httpmessaging.cpp
File metadata and controls
274 lines (234 loc) · 7.61 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
#include "httpmessaging.h"
#include "AnsiString.h"
#include <stdlib.h>
#define USER_AGENT "concept://1.2"
struct MemoryStruct {
char *memory;
size_t size;
size_t offset;
PROGRESS_API notify_parent;
bool idle_call;
bool shown;
};
static size_t WriteCallback(void *ptr, size_t size, size_t nmemb, void *data) {
size_t realsize = size * nmemb;
struct MemoryStruct *mem = (struct MemoryStruct *)data;
mem->memory = (char *)realloc(mem->memory, mem->size + realsize + 1);
if (mem->memory == NULL) {
printf("not enough memory (realloc returned NULL)\n");
return -1;
}
memcpy(&(mem->memory[mem->size]), ptr, realsize);
mem->size += realsize;
mem->memory[mem->size] = 0;
if ((mem->notify_parent) && (mem->size > BIG_MESSAGE) && (!mem->shown)) {
mem->notify_parent(-1, 5, mem->idle_call);
mem->shown = true;
}
return realsize;
}
int GetResponse(struct MemoryStruct *resp) {
int response = -1;
if (resp->memory) {
char *resp_start = strchr(resp->memory, ' ');
if (resp_start) {
resp_start++;
char *resp_end = strchr(resp_start, ' ');
if (resp_end) {
resp_end[0] = 0;
response = atoi(resp_start);
}
}
free(resp->memory);
resp->memory = 0;
resp->size = 0;
}
return response;
}
void HTTPInit() {
//curl_global_init(CURL_GLOBAL_ALL);
}
int HTTPPostSend(char *host, char *data, int len, int max_packet, PROGRESS_API notify_parent, bool idle_call) {
int res = len;
if ((!len) || (!data))
return 0;
/*
int pack_size = len;
if (max_packet > 0)
pack_size = len > max_packet ? max_packet : len;
do {
struct MemoryStruct chunk;
struct MemoryStruct chunk_resp;
chunk.memory = 0;
chunk.size = 0;
chunk.offset = 0;
chunk.notify_parent = notify_parent;
chunk.idle_call = idle_call;
chunk.shown = false;
chunk_resp.memory = 0;
chunk_resp.size = 0;
chunk_resp.offset = 0;
chunk_resp.notify_parent = 0;
chunk_resp.idle_call = 0;
chunk_resp.shown = false;
CURL *handle = curl_easy_init();
if (!handle)
return(-1);
struct curl_httppost *formpost = NULL;
struct curl_httppost *lastptr = NULL;
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(handle, CURLOPT_WRITEHEADER, (void *)&chunk_resp);
curl_formadd(&formpost, &lastptr,
CURLFORM_PTRNAME, "b",
CURLFORM_BUFFER, "x",
CURLFORM_BUFFERPTR, data,
CURLFORM_BUFFERLENGTH, (long)pack_size,
CURLFORM_END);
curl_easy_setopt(handle, CURLOPT_URL, host);
curl_easy_setopt(handle, CURLOPT_HTTPPOST, formpost);
curl_easy_setopt(handle, CURLOPT_USERAGENT, USER_AGENT);
if (curl_easy_perform(handle))
res = -2;
curl_easy_cleanup(handle);
curl_formfree(formpost);
int response = GetResponse(&chunk_resp);
if (((response < 200) && (response != 100)) || ((response >= 300) && (response != 304)))
res = -3;
else {
len -= max_packet;
if (len > 0)
data += max_packet;
}
len -= max_packet;
if (len > 0)
data += max_packet;
if (len < pack_size)
pack_size = len;
free(chunk.memory);
} while ((len > 0) && (res > 0));*/
return res;
}
int HTTPGetRecv(char *host, char **data, int *len, PROGRESS_API notify_parent, bool idle_call) {
int res = 0;
/* *data = 0;
* len = 0;
int response = -1;
int out;
do {
out = 1;
CURL *handle = curl_easy_init();
if (!handle)
return(-1);
struct MemoryStruct chunk;
struct MemoryStruct chunk_resp;
chunk.memory = 0;
chunk.size = 0;
chunk.offset = 0;
chunk.notify_parent = notify_parent;
chunk.idle_call = idle_call;
chunk.shown = false;
chunk_resp.memory = 0;
chunk_resp.size = 0;
chunk_resp.offset = 0;
chunk_resp.notify_parent = notify_parent;
chunk_resp.idle_call = idle_call;
curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, WriteCallback);
curl_easy_setopt(handle, CURLOPT_WRITEDATA, (void *)&chunk);
curl_easy_setopt(handle, CURLOPT_WRITEHEADER, (void *)&chunk_resp);
curl_easy_setopt(handle, CURLOPT_URL, host);
curl_easy_setopt(handle, CURLOPT_USERAGENT, USER_AGENT);
struct curl_slist *headers = NULL;
headers = curl_slist_append(headers, "Content-Type: application/octet-stream");
headers = curl_slist_append(headers, "Connection: close");
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
if (curl_easy_perform(handle))
res = -2;
if ((chunk.shown) && (notify_parent))
notify_parent(101, 0, idle_call);
curl_slist_free_all(headers);
response = GetResponse(&chunk_resp);
if ((response < 200) || ((response >= 300) && (response != 304)))
if (response != -1)
res = -3;
curl_easy_cleanup(handle);
if ((response == 200) && (chunk.size == 0)) {
if (chunk.memory) {
free(chunk.memory);
chunk.memory = 0;
}
out = 0;
continue;
}
if (res >= 0) {
* data = chunk.memory;
* len = chunk.size;
res = chunk.size;
} else {
if (chunk.memory)
free(chunk.memory);
return(res);
}
} while (!out);*/
return res;
}
char *piece = 0;
int piece_size = 0;
int piece_offset = 0;
int HTTPRecv(char *host, char *data, int len, int flags, PROGRESS_API notify_parent, bool idle_call) {
int res = -1;
/*if ((piece_size) && (piece_offset >= piece_size)) {
if (piece)
free(piece);
piece = 0;
piece_size = 0;
piece_offset = 0;
}
if (!piece)
res = HTTPGetRecv(host, &piece, &piece_size, notify_parent);
if (piece) {
int size = len;
char *start = piece + piece_offset;
int piece_vsize = piece_size - piece_offset;
if (size > piece_vsize) {
size = piece_size;
memcpy(data, start, size);
res = size;
free(piece);
piece = 0;
piece_size = 0;
piece_offset = 0;
} else {
memcpy(data, start, size);
res = size;
piece_offset += size;
}
}*/
return res;
}
int HTTPHaveMessage(char *host) {
/*if (piece) {
if ((piece_size) && (piece_offset < piece_size))
return(1);
free(piece);
piece = 0;
piece_size = 0;
piece_offset = 0;
return(0);
}
char *buf = 0;
int len = 0;
HTTPGetRecv(host, &buf, &len);
if (buf)
if ((len == 1) && (buf[0] == '1')) {
HTTPFreeData(buf);
return(1);
}
HTTPFreeData(buf);
*/
return 0;
}
void HTTPFreeData(char *buffer) {
if (buffer)
free(buffer);
}