-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp_client.cpp
398 lines (345 loc) · 9.79 KB
/
http_client.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
//
// http_client.cpp
// http_client
//
// Created by kpraveen on 2019-03-16.
// Copyright © 2019 Praveen. All rights reserved.
//
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <stdio.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <errno.h>
#include <stdarg.h>
#include <ctype.h>
#include <stdint.h>
#include <inttypes.h>
#include <fcntl.h>
#include "http_client.h"
struct request_data {
char* path;
char* body;
size_t bodylen;
int completed;
char* header;
size_t headerlen;
};
int
HttpClient::socket_connect(char *host, in_port_t port)
{
struct hostent *hp;
struct sockaddr_in addr;
int on = 1;
if((hp = gethostbyname(host)) == NULL){
sprintf(error_message, "Could not get host from hostname, error [%d]\n", errno);
return -1;
}
bcopy(hp->h_addr, &addr.sin_addr, hp->h_length);
addr.sin_port = htons(port);
addr.sin_family = AF_INET;
fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
//setsockopt(sock, IPPROTO_TCP, TCP_NODELAY, (const char *)&on, sizeof(int));
if(fd == -1){
sprintf(error_message, "Could not create socket, error [%d]\n", errno);
return -1;
}
if(connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) == -1) {
fd = -1;
sprintf(error_message, "Connection refused, error [%d]\n", errno);
return -1;
}
return fd;
}
static int on_message_begin(http_parser* parser) {
return 0;
}
static int on_headers_complete(http_parser* parser) {
return 0;
}
static int on_message_complete(http_parser* parser) {
// printf("on_message_complete set data.completed true\n");
request_data* data = (request_data*)(parser->data);
data->completed = true;
return 0;
}
static int on_url(http_parser* parser, const char* at, size_t length) {
printf("on url\n");
return 0;
}
// strcasecmp POSIX.1-2001 and 4.4BSD
static int on_header_field(http_parser* parser, const char* at, size_t length) {
request_data* data = (request_data*)(parser->data);
data->header = const_cast<char*>(at);
data->headerlen = length;
return 0;
}
static int on_header_value(http_parser* parser, const char* at, size_t length) {
//printf("on_header_value %.*s\n", length,at);
if (strncasecmp(at, "content-type", 12) == 0) {
printf("header 1 %.*s\n", length, at);
}
return 0;
}
static int on_body(http_parser* parser, const char* at, size_t length) {
request_data* data = (request_data*)(parser->data);
if (!(data->body)) {
data->body = const_cast<char*>(at);
}
data->bodylen += length;
return 0;
}
static int setSocketNonBlocking(int fd) {
int lFlags;
lFlags = fcntl(fd, F_GETFL, 0);
if (lFlags < 0)
{
return -1;
}
lFlags |= O_NONBLOCK;
if(fcntl(fd, F_SETFL, lFlags) == 0)
{
return -1;
}
return 0;
}
static int setSocketBlocking(int fd) {
int lFlags;
lFlags = fcntl(fd, F_GETFL, 0);
if (lFlags < 0)
{
return -1;
}
lFlags &= ~O_NONBLOCK;
if(fcntl(fd, F_SETFL, lFlags) == 0)
{
return -1;
}
return 0;
}
int
HttpClient::_send_receive(char* url) {
struct http_parser_url u;
printf("url %s\n", url);
char path[300] = "/";
int has_protocol = 0;
if (strncmp(url, "http", 4) == 0) {
has_protocol = 0;
};
http_parser_url_init(&u);
int result = http_parser_parse_url(url, strlen(url), has_protocol, &u);
if ((u.field_set & (1 << UF_HOST))) {
char temp[256];
if (u.field_data[UF_HOST].len > 255) {
strcpy(error_message, "hostname too long");
return -1;
}
strncpy(temp, url+u.field_data[UF_HOST].off, u.field_data[UF_HOST].len);
temp[u.field_data[UF_HOST].len] = NULL;
if (fd >= 0) {
if (strcmp(hostname, temp) != 0) {
shutdown(fd, SHUT_RDWR);
portnum = 80;
fd = -1;
strcpy(hostname, temp);
}
}
else {
strcpy(hostname, temp);
}
}
if (hostname[0] == 0) {
strcpy(error_message, "hostname not provided");
return -1;
}
if ((u.field_set & (1 << UF_PORT))) {
char temp[6];
if (u.field_data[UF_PORT].len > 5) {
strcpy(error_message, "portnumber too long");
return -1;
}
strncpy(temp, url+u.field_data[UF_PORT].off, u.field_data[UF_PORT].len);
int newport = atoi(temp);
if (fd >= 0) {
if (newport != portnum) {
shutdown(fd, SHUT_RDWR);
}
}
portnum = newport;
}
if ((u.field_set & (1 << UF_PATH))) {
strncpy(path, url+u.field_data[UF_PATH].off, u.field_data[UF_PATH].len);
path[u.field_data[UF_PATH].len] = NULL;
}
int retry_done = 0;
char req[2056];
if (method == HTTP_GET) {
if (request_headers) {
sprintf(req, "GET %s HTTP/1.1\r\nHost: %s \r\n%s\r\n\r\n", path, hostname, request_headers);
}
else {
sprintf(req, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", path, hostname);
}
} else if (method == HTTP_POST) {
if (request_headers) {
sprintf(req, "POST %s HTTP/1.1\r\nHost: %s \r\n%s\r\n\r\n", path, hostname, request_headers);
}
else {
sprintf(req, "POST %s HTTP/1.1\r\nHost: %s\r\nContent-Length: %d\r\n\r\n", path, hostname, request_length);
}
} else {
strcpy(error_message,"inalid method");
return -1;
}
RETRY_REQUEST:
bufread = 0;
parsepos = bufstart;
response = NULL;
response_length = 0;
if (fd == -1) {
socket_connect(hostname, portnum);
setSocketBlocking(fd);
}
else {
setSocketNonBlocking(fd);
char temp;
int peeked = recv(fd, &temp, 1, MSG_PEEK);
if (peeked == 0) {
fd = -1;
}
else if (peeked < 0) {
int lasterror = errno;
if (lasterror == EWOULDBLOCK) {
// socket is alive
} else {
fd = -1;
}
}
else {
// socket is live and data is present
}
if (fd == -1) {
socket_connect(hostname, portnum);
} else {
setSocketBlocking(fd);
}
}
if (fd < 0) {
return -1;
}
struct request_data data;
data.path = path;
data.completed = false;
data.bodylen = 0;
data.body = NULL;
// data.parser = this;
parser.data = &data;
int sb = 0;
if (method == HTTP_GET) {
sb = send(fd, req, strlen(req), 0);
} else {
int flags = 0;
#ifdef __linux__
flags = flags & MSG_MORE;
#elif __APPLE__
// flags = flags & MSG_MORE;
#endif
sb = send(fd, req, strlen(req), flags);
if (sb > 0) {
flags = 0;
printf("sending body %.*s\n", request_length, request_body);
sb = send(fd, request_body, request_length, flags);
}
}
if (sb <= 0) {
printf("Could not send\n");
return sb;
}
int totalparsed = 0;
while(1) {
if (capacity-bufread < 1024) {
bufstart = (char*)realloc(bufstart, capacity+4096);
capacity += 4096;
};
int recived_now = recv(fd, bufstart+bufread, capacity-bufread, 0);
if (recived_now < 0) {
if (errno == EAGAIN || errno == EWOULDBLOCK) {
continue;
}
return recived_now;
}
if (recived_now == 0) {
if (!retry_done) {
retry_done = 1;
socket_connect(hostname, portnum);
goto RETRY_REQUEST;
}
return recived_now;
}
if (recived_now <= 0) {
return recived_now;
}
bufread += recived_now;
// printf("total read %d\n", bufread);
// printf("received buffer [%*.*s]\n", bufread, bufread, bufstart);
char* headerend = strstr(bufstart, "\r\n\r\n");
if (!headerend) {
continue;
}
int toparse = bufread - totalparsed;
int nparsed = http_parser_execute(&parser, &settings, parsepos, toparse);
if (nparsed != toparse) {
strcpy(error_message, "invalid http response");
return -1;
}
parsepos = parsepos + toparse;
totalparsed = totalparsed + toparse;
if (data.completed) {
status_code = parser.status_code;
response = data.body;
response_length = data.bodylen;
break;
}
}
return status_code;
}
int
HttpClient::get(char* url) {
method = HTTP_GET;
return _send_receive(url);
}
int
HttpClient::post(char* url, char* body, int len) {
method = HTTP_POST;
request_body = body;
request_length = len;
return _send_receive(url);
}
HttpClient::HttpClient() {
portnum = 80;
fd = -1;
memset(&settings, 0, sizeof(settings));
settings.on_message_begin = on_message_begin;
settings.on_url = on_url;
settings.on_header_field = on_header_field;
settings.on_header_value = on_header_value;
settings.on_headers_complete = on_headers_complete;
settings.on_body = on_body;
settings.on_message_complete = on_message_complete;
parser.data = NULL;
http_parser_init(&parser, HTTP_RESPONSE);
capacity = 4096;
bufread = 0;
bufstart = (char*)malloc(4096);
response = NULL;
response_length = 0;
strcpy(hostname, "");
portnum = 80;
status_code = -1;
}