-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathhttp.c
533 lines (435 loc) · 9.79 KB
/
http.c
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "http.h"
#ifndef HTTP_USER_AGENT
#define HTTP_USER_AGENT "Mozilla/4.0 (Linux)"
#endif
#ifndef HTTP_TIME_OUT
#define HTTP_TIME_OUT 360
#endif
/**
* Parse URL into protocol, hostname and query part; the returned
* structure needs to be freed after use
*
* @param url - URL
*/
struct http_url *http_parse_url(const char *url) {
struct http_url *hu;
size_t len;
char *buf;
char *p;
if (!url ||
(len = strlen(url)) < 1 ||
!(hu = calloc(1, sizeof(struct http_url) + len + 1))) {
return NULL;
}
buf = (char *) hu + sizeof(struct http_url);
memcpy(buf, url, len);
if ((p = strstr(buf, "://"))) {
*p = 0;
hu->protocol = buf;
buf = p + 3;
}
hu->host = buf;
if ((p = strchr(buf, '/'))) {
*p = 0;
hu->query = ++p;
if ((p = strchr(p, '#'))) {
*p = 0;
}
} else {
hu->query = "";
}
if ((p = strchr(hu->host, ':'))) {
hu->protocol = ++p;
} else {
hu->protocol = "http";
}
return hu;
}
/**
* Resolve URL and try to connect
*
* @param hu - URL structure
*/
int http_connect(struct http_url *hu) {
struct addrinfo hints, *si, *p;
int sd = -1;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
/* cut off optional colon */
{
char buf[256];
char *host = hu->host;
char *p;
if ((p = strchr(host, ':'))) {
size_t l = p - host;
if (l > sizeof(buf) - 1) {
return -1;
}
*buf = 0;
host = strncat(buf, host, l);
}
if (getaddrinfo(host, hu->protocol, &hints, &si)) {
return -1;
}
}
/* loop through all results until connect is successful */
for (p = si; p; p = p->ai_next) {
if ((sd = socket(
p->ai_family,
p->ai_socktype,
p->ai_protocol)) < 0) {
continue;
}
if (connect(sd, p->ai_addr, p->ai_addrlen) < 0) {
#ifdef WIN32
closesocket(sd);
#else
close(sd);
#endif
continue;
}
break;
}
if (!p && sd > -1) {
#ifdef WIN32
closesocket(sd);
#else
close(sd);
#endif
sd = -1;
}
freeaddrinfo(si);
return sd;
}
/**
* Send HTTP request
*
* @param sd - socket
* @param rq - request
*/
int http_send(int sd, const char *rq) {
size_t l;
if (!rq) {
return -1;
}
for (l = strlen(rq); l > 0;) {
int bytes = send(sd, rq, l, 0);
if (bytes < 0) {
return -1;
}
rq += bytes;
l -= bytes;
}
return 0;
}
/**
* Cut off trailing CRLF
*
* @param msg - message struct
*/
static void http_cut_trailing_crlf(struct http_message *msg) {
if (msg->state.chunk == 1 && msg->length > 0) {
/* cut of just CR */
msg->content[--msg->length] = 0;
} else if (!msg->state.chunk && msg->length > 1) {
/* cut of CR/LF */
msg->length -= 2;
msg->content[msg->length] = 0;
}
}
/**
* Parse message content
*
* @param msg - message struct
* @param bod - begin of data, points to the first character
* @param eod - end of data, points to the terminating NULL character
*/
static char *http_parse_content(
struct http_message *msg,
char *bod,
char *eod) {
int len = eod - bod;
/* check if message is chunked */
if (msg->state.chunk > -1) {
/* check if chunk stops in this segment */
if (msg->state.chunk < len) {
char *eoc;
if (msg->state.chunk > 0) {
if (msg->state.chunk == 1) {
/* skip trailing LF from previous
* chunk header */
++bod;
msg->length = 0;
msg->state.chunk = 0;
} else {
/* return data before next chunk
* header first */
msg->length = msg->state.chunk;
msg->content = bod;
bod += msg->state.chunk;
msg->state.chunk = 0;
http_cut_trailing_crlf(msg);
}
return bod;
}
/* chunk header is always at the begin of
* data here */
if (!(eoc = strchr(bod, '\n'))) {
/* chunk header is incomplete */
return bod;
}
*eoc = 0;
sscanf(bod, "%x", &msg->state.chunk);
/* add trailing CR/LF to chunk size */
msg->state.chunk += 2;
msg->content = ++eoc;
len = eod - msg->content;
if (msg->state.chunk < len) {
/* chunk ends in this segment and a new
* one starts */
msg->length = msg->state.chunk;
bod = msg->content + msg->state.chunk;
/* CR/LF must be together here */
msg->state.chunk = 0;
http_cut_trailing_crlf(msg);
return bod;
} else {
/* next chunk header is beyond this segment */
msg->length = len;
msg->state.chunk -= len;
http_cut_trailing_crlf(msg);
return eod;
}
} else {
/* next chunk header is beyond this segment;
* fall through to default behaviour */
msg->state.chunk -= len;
}
}
msg->content = bod;
msg->length = len;
http_cut_trailing_crlf(msg);
return eod;
}
/**
* Parse HTTP message
*
* @param msg - message struct
* @param bod - begin of data, points to the first character
* @param eod - end of data, points to the terminating NULL character
*/
static char *http_parse_message(
struct http_message *msg,
char *bod,
char *eod) {
char *lf;
if (!bod) {
return bod;
}
if (msg->state.in_content) {
return http_parse_content(msg, bod, eod);
}
/* header line is incomplete so fetch more data */
if (!(lf = strchr(bod, '\n'))) {
return bod;
}
/* parse status code */
if (!msg->header.code) {
/* accept HTTP/1.[01] only */
if (strncmp(bod, "HTTP/1.", 7) ||
!strchr("01", *(bod + 7))) {
return NULL;
}
bod += 8;
msg->header.code = atoi(bod);
return http_parse_message(msg, ++lf, eod);
}
/* parse header line by line */
for (; lf; bod = ++lf, lf = strchr(bod, '\n')) {
#define WHITESPACE " \t\r\n"
char *value;
*lf = 0;
/* check for end of header */
if (!*bod || !strcmp(bod, "\r")) {
if (!*bod) {
++bod;
} else {
bod += 2;
}
msg->state.in_content = 1;
return http_parse_message(msg, bod, eod);
}
if (!(value = strchr(bod, ':'))) {
continue;
}
*value = 0;
++value;
bod += strspn(bod, WHITESPACE);
strtok(bod, WHITESPACE);
value += strspn(value, WHITESPACE);
strtok(value, WHITESPACE);
if (!strcasecmp(bod, "Transfer-Encoding") &&
!strcasecmp(value, "chunked")) {
/* means 0 bytes until next chunk header */
msg->state.chunk = 0;
} else if (!strcasecmp(bod, "Content-Length")) {
msg->header.length = atoi(value);
}
}
return bod;
}
/**
* Read next part of the response; returns 0 when message is complete;
* this function blocks until data is available
*
* @param sd - socket
* @param msg - message struct that gets filled with data, must be
* all 0 for the very first call
*/
int http_read(int sd, struct http_message *msg) {
if (!msg) {
return -1;
}
/* first-time initialization */
if (!msg->state.offset) {
/* initialize size of read buffer;
* reserve a byte for the terminating NULL
* character */
msg->state.size = sizeof(msg->state.buf) - 1;
msg->state.offset = msg->state.buf;
/* initialize remaining length of chunk;
* -1 means content is not chunked */
msg->state.chunk = -1;
/* initialize content length;
* -1 means not given */
msg->header.length = -1;
}
if (msg->state.total == msg->header.length) {
/* return 0 for keep-alive connections */
return 0;
}
for (;;) {
/* try to parse buffer first if there's still data in it */
if (msg->state.left > 0) {
char *parsed_until;
msg->length = 0;
parsed_until = http_parse_message(
msg,
msg->state.offset,
msg->state.offset + msg->state.left);
if (parsed_until > msg->state.offset) {
msg->state.left -= parsed_until - msg->state.offset;
msg->state.offset = parsed_until;
msg->state.total += msg->length;
return 1;
}
}
/* make offset begin at the front of the buffer */
if (msg->state.offset > msg->state.buf) {
if (msg->state.left > 0)
memmove(
msg->state.buf,
msg->state.offset,
msg->state.left);
msg->state.offset = msg->state.buf;
}
/* wait for and read new data from the network */
{
char *append = msg->state.offset + msg->state.left;
int bytes,
size = (msg->state.buf + msg->state.size)-append;
/* drop half of the buffer if there's no space left */
if (size < 1) {
int half = msg->state.size >> 1;
memcpy(
msg->state.buf,
msg->state.buf + half,
half);
msg->state.offset = msg->state.buf;
msg->state.left = half;
append = msg->state.offset + msg->state.left;
size = msg->state.size - msg->state.left;
}
if ((bytes = recv(
sd,
append,
size,
0)) < 1) {
/* bytes == 0 means remote socket was closed */
return 0;
}
append[bytes] = 0;
msg->state.left += bytes;
}
}
return 0;
}
/**
* Send HTTP request
*
* @param url - URL
*/
int http_request(const char *url) {
struct http_url *hu;
int sd;
if (!(hu = http_parse_url(url)) ||
(sd = http_connect(hu)) < 0) {
/* it's save to free NULL */
free(hu);
return -1;
}
/* this way even very very long query strings won't be a problem */
if (http_send(sd, "GET /") ||
http_send(sd, hu->query) ||
http_send(sd, " HTTP/1.1\r\n\
User-Agent: "HTTP_USER_AGENT"\r\n\
Host: ") ||
http_send(sd, hu->host) ||
http_send(sd, "\r\n\
Accept: */*\r\n\
Connection: close\r\n\
\r\n")) {
#ifdef WIN32
closesocket(sd);
#else
close(sd);
#endif
return -1;
}
free(hu);
return sd;
}
/**
* Read next part of the response; returns 0 when message is complete;
* this function blocks until data is available
*
* @param sd - socket
* @param msg - message struct that gets filled with data, must be
* all 0 for the very first call
*/
int http_response(int sd, struct http_message *msg) {
fd_set r;
struct timeval tv;
tv.tv_sec = HTTP_TIME_OUT;
tv.tv_usec = 0;
FD_ZERO(&r);
FD_SET(sd, &r);
if (select(sd + 1, &r, NULL, NULL, &tv) < 1 ||
!FD_ISSET(sd, &r)) {
return -1;
}
return http_read(sd, msg);
}