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
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
/*
* test82: test HTTP with a remote server (is $USENETWORK="yes")
*/
#define DEBUG 0
#if DEBUG
#define dbgprintf(...) do { \
fprintf(stderr, "[%s:%s:%d %d] ", \
__FILE__, __FUNCTION__, \
__LINE__, getpid()); \
fprintf(stderr, __VA_ARGS__); \
fflush(stderr); \
} while (0)
#else
#define dbgprintf(...)
#endif
#include <arpa/inet.h>
#include <assert.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include "common.h"
#define CLOSE(fd) do { assert(fd >= 0); if (close((fd)) != 0) efmt("close failed"); } while (0);
#define REALLOC(p, size) do { p = realloc(p, size); if (!p) efmt("realloc of %zu bytes failed", size); } while (0);
#define HOST "test82.minix3.org"
#define PORT 80
#define PATH1 "/test1.txt"
#define PATH1_DATA "Hello world\n"
#define PATH2 "/test2.bin"
static void callback_verify_path1(const void *data, size_t size);
static void callback_verify_path2(const void *data, size_t size);
#define URL_COUNT 2
struct url {
const char *host;
int port;
const char *path;
void (* callback_verify)(const void *data, size_t size);
};
static const struct url urls[URL_COUNT] = {
{ HOST, PORT, PATH1, callback_verify_path1 },
{ HOST, PORT, PATH2, callback_verify_path2 },
};
static int http_connect(const char *host, int port) {
struct addrinfo *addr = NULL;
int fd = -1;
struct addrinfo hints = {
.ai_family = PF_INET,
.ai_socktype = SOCK_STREAM,
};
char serv[12];
assert(host);
snprintf(serv, sizeof(serv), "%d", port);
errno = 0;
if (getaddrinfo(host, serv, &hints, &addr) != 0 || !addr) {
efmt("host %s not found", host);
goto failure;
}
fd = socket(AF_INET, SOCK_STREAM, 0);
if (fd < 0) {
efmt("cannot create socket");
goto failure;
}
if (connect(fd, addr->ai_addr, addr->ai_addrlen) != 0) {
efmt("cannot connect to %s:%d", host, port);
goto failure;
}
freeaddrinfo(addr);
return fd;
failure:
if (fd >= 0) CLOSE(fd);
if (addr) freeaddrinfo(addr);
return -1;
}
static void write_chunked(
int fd,
const char *data,
size_t size,
size_t chunksize) {
ssize_t r;
size_t s;
assert(fd >= 0);
assert(data);
assert(chunksize > 0);
while (size > 0) {
s = chunksize;
if (s > size) s = size;
errno = 0;
r = write(fd, data, s);
if (r <= 0 || (size_t) r > s) {
errno = 0;
efmt("write of %zu bytes failed with result %zd", s, r);
break;
}
data += r;
size -= r;
}
}
static void http_send_request(
int fd,
const char *host,
const char *path,
size_t chunksize,
int bigrequest) {
char buf[8192];
size_t len;
int lineno;
assert(fd >= 0);
assert(host);
assert(path);
assert(chunksize > 0);
/* http://tools.ietf.org/html/rfc2616#section-5 */
len = snprintf(buf, sizeof(buf),
"GET %s HTTP/1.1\r\n"
"Host: %s\r\n",
path, host);
if (bigrequest) {
lineno = 0;
while (len + 24 < sizeof(buf)) {
len += snprintf(buf + len, sizeof(buf) - len,
"X-Padding%d: %d\r\n",
lineno, lineno);
lineno++;
}
}
len += snprintf(buf + len, sizeof(buf) - len, "\r\n");
dbgprintf("sending request:\n%.*s", (int) len, buf);
write_chunked(fd, buf, len, chunksize);
}
static int is_whitespace(char c) {
return c == ' ' || c == '\t';
}
static int is_whitespace_or_linebreak(char c) {
return is_whitespace(c) || c == '\r' || c == '\n';
}
static int is_numeric(char c) {
return c >= '0' && c <= '9';
}
static int http_get_header_line(
const char *data,
size_t len,
size_t *index_p,
size_t *linelen_p) {
int has_cr;
size_t index;
size_t linelen;
assert(data);
assert(index_p);
assert(*index_p <= len);
assert(linelen_p);
/* starting the next line with whitespace means the line is continued */
index = *index_p;
do {
while (index < len && data[index] != '\n') index++;
if (index >= len) goto notfound;
index++;
} while (index < len && is_whitespace(data[index]));
/* exclude LF or CR+LF from line length */
assert(index - 1 >= *index_p && data[index - 1] == '\n');
has_cr = (index - 2 >= *index_p) && data[index - 2] == '\r';
linelen = index - *index_p - (has_cr ? 2 : 1);
/* if LF is the last character in the buffer, the line may be continued
* when more data is retrieved unless we reached the end of the headers
*/
if (index >= len && linelen > 0) goto notfound;
*linelen_p = linelen;
*index_p = index;
return 1;
notfound:
*linelen_p = 0;
*index_p = index;
return 0;
}
static int http_get_status_line(
const char *data,
size_t len,
size_t *index_p,
int *error_p,
int *code_p) {
int code, i;
size_t index;
assert(data);
assert(index_p);
assert(*index_p <= len);
assert(error_p);
assert(*error_p == 0);
assert(code_p);
/* skip leading whitespace/blank lines */
index = *index_p;
while (index < len && is_whitespace_or_linebreak(data[index])) index++;
/* parse version */
while (index < len && !is_whitespace(data[index])) index++;
/* skip separator */
while (index < len && is_whitespace(data[index])) index++;
/* parse status code */
code = 0;
for (i = 0; i < 3; i++) {
if (index >= len) goto notfound;
if (!is_numeric(data[index])) {
errno = 0;
efmt("HTTP error: bad status line: \"%.*s\"",
(int) (index - *index_p), data + *index_p);
*error_p = 1;
goto notfound;
}
code = code * 10 + (data[index++] - '0');
}
/* skip separator */
while (index < len && is_whitespace(data[index])) index++;
/* parse reason phrase */
while (index < len && data[index] != '\n') index++;
if (index >= len) goto notfound;
index++;
*code_p = code;
*index_p = index;
return 1;
notfound:
*code_p = 0;
*index_p = index;
return 0;
}
static int http_header_is(
const char *data,
size_t len,
size_t index,
const char *name,
size_t *index_value_p) {
size_t namelen;
assert(data);
assert(index <= len);
assert(name);
assert(index_value_p);
namelen = strlen(name);
if (index + namelen > len) goto notfound;
if (strncasecmp(data + index, name, namelen) != 0) goto notfound;
index += namelen;
while (index < len && is_whitespace(data[index])) index++;
if (index >= len || data[index] != ':') goto notfound;
index++;
while (index < len && is_whitespace(data[index])) index++;
*index_value_p = index;
return 1;
notfound:
*index_value_p = 0;
return 0;
}
static int http_parse_int_header(
const char *data,
size_t index,
size_t index_end,
int *value_p,
int *error_p) {
int value = 0;
assert(data);
assert(index <= index_end);
assert(value_p);
assert(error_p);
assert(!*error_p);
while (index < index_end && is_numeric(data[index])) {
value = value * 10 + (data[index++] - '0');
}
while (index < index_end && is_whitespace_or_linebreak(data[index])) {
index++;
}
if (index < index_end) {
errno = 0;
efmt("HTTP error: bad numeric header value: \"%.*s\"",
(int) (index_end - index), data + index);
*error_p = 1;
return 0;
}
*value_p = value;
return 1;
}
static int http_response_complete(
const char *data,
size_t len,
int *error_p,
int *code_p,
size_t *index_body_p) {
int content_length = -1;
size_t index = 0, index_line;
size_t index_value;
size_t linelen;
assert(data);
assert(error_p);
assert(!*error_p);
assert(code_p);
assert(index_body_p);
/* parse status line */
if (!http_get_status_line(data, len, &index, error_p, code_p)) {
return 0;
}
/* parse headers */
for (;;) {
index_line = index;
if (!http_get_header_line(data, len, &index, &linelen)) {
return 0;
}
if (linelen == 0) break;
if (http_header_is(data, len, index_line,
"Content-Length", &index_value)) {
if (!http_parse_int_header(data, index_value,
index_line + linelen, &content_length,
error_p)) {
return 0;
}
}
}
/* do we know how long the response will be? */
if (content_length < 0) {
errno = 0;
efmt("HTTP error: missing Content-Length header "
"(maybe Transfer-Encoding is specified instead "
"but this is currently unsupported)");
goto error;
}
/* check whether the amount of data is correct */
if (len > index + content_length) {
errno = 0;
efmt("HTTP error: more data received than expected");
goto error;
}
*index_body_p = index;
return len == index + content_length;
error:
*error_p = 1;
*code_p = 0;
*index_body_p = 0;
return 0;
}
static void http_recv_response(
int fd,
void (* callback_verify)(const void *data, size_t size),
size_t chunksize) {
int code;
char *data;
size_t datalen = 0, datasize = 0;
int error = 0;
size_t index_body;
ssize_t r;
assert(fd >= 0);
assert(callback_verify);
assert(chunksize > 0);
data = NULL;
for (;;) {
/* make room for another chunk in the buffer if needed */
if (datasize < datalen + chunksize) {
datasize = (datalen + chunksize) * 2;
REALLOC(data, datasize);
}
/* read a chunk of data */
errno = 0;
r = read(fd, data + datalen, chunksize);
if (r < 0 || (size_t) r > chunksize) {
efmt("read of %zu bytes failed with result %zd",
chunksize, r);
goto cleanup;
}
datalen += r;
/* if we received all headers+data, we are done */
if (http_response_complete(data, datalen, &error, &code,
&index_body)) {
break;
}
if (error) goto cleanup;
/* check for premature disconnection */
if (r == 0) {
errno = 0;
efmt("server disconnected even though the response "
"seems to be incomplete");
goto cleanup;
}
}
dbgprintf("received response:\n%.*s", (int) datalen, data);
assert(index_body <= datalen);
if (code == 200) {
callback_verify(data + index_body, datalen - index_body);
} else {
errno = 0;
efmt("unexpected HTTP status code %d", code);
}
cleanup:
if (data) free(data);
}
static void http_test(
const struct url *url,
size_t chunksize,
int bigrequest,
int delay,
int withshutdown) {
int fd;
assert(url);
assert(chunksize > 0);
dbgprintf("attempting download from http://%s:%d%s, "
"chunksize=%zu, bigrequest=%d, delay=%d, withshutdown=%d\n",
url->host, url->port, url->path, chunksize, bigrequest,
delay, withshutdown);
fd = http_connect(url->host, url->port);
if (fd < 0) return;
http_send_request(fd, url->host, url->path, chunksize, bigrequest);
errno = 0;
if (withshutdown && shutdown(fd, SHUT_WR) != 0) {
efmt("shutdown failed");
}
if (delay) sleep(1);
http_recv_response(fd, url->callback_verify, chunksize);
CLOSE(fd);
dbgprintf("download attempt completed\n");
}
static int child_count;
static void http_test_fork(
const struct url *url,
size_t chunksize,
int bigrequest,
int delay,
int withshutdown) {
int errctold;
pid_t pid;
assert(url);
assert(chunksize > 0);
errno = 0;
pid = fork();
if (pid < 0) {
efmt("fork failed");
return;
}
if (pid > 0) {
child_count++;
return;
}
errctold = errct;
http_test(
url,
chunksize,
bigrequest,
delay,
withshutdown);
assert(errct >= errctold);
exit(errct - errctold);
}
static void wait_all(void) {
int exitcode, status;
pid_t pid;
while (child_count > 0) {
errno = 0;
pid = waitpid(-1, &status, 0);
if (pid <= 0) {
efmt("waitpid failed");
return;
}
if (WIFEXITED(status)) {
exitcode = WEXITSTATUS(status);
dbgprintf("child %d completed with exit code %d\n",
(int) pid, exitcode);
if (exitcode >= 0) {
errct += exitcode;
} else {
efmt("child has negative exit code %d",
exitcode);
}
} else if (WIFSIGNALED(status)) {
dbgprintf("child %d killed by signal %d\n",
(int) pid, WTERMSIG(status));
efmt("child killed by signal %d", WTERMSIG(status));
} else {
dbgprintf("child %d gone with status 0x%x\n",
(int) pid, status);
efmt("child gone, but neither exit nor signal");
}
child_count--;
}
errno = 0;
if (waitpid(-1, &status, 0) != -1 || errno != ECHILD) {
efmt("waitpid should have returned ECHILD");
}
}
#define OPTION_BIGREQUEST (1 << 0)
#define OPTION_DELAY (1 << 1)
#define OPTION_SHUTDOWN (1 << 2)
static void http_test_all(int multiproc) {
static const size_t chunksizes[] = { 1, 1024, 65536 };
static const int optionsets[] = {
0,
OPTION_BIGREQUEST,
OPTION_DELAY,
OPTION_SHUTDOWN,
OPTION_BIGREQUEST | OPTION_DELAY | OPTION_SHUTDOWN,
};
int chunksizeindex;
int options;
int optionindex;
int urlindex;
for (urlindex = 0; urlindex < URL_COUNT; urlindex++) {
for (chunksizeindex = 0; chunksizeindex < 3; chunksizeindex++) {
for (optionindex = 0; optionindex < 3; optionindex++) {
options = optionsets[optionindex];
(multiproc ? http_test_fork : http_test)(
&urls[urlindex],
chunksizes[chunksizeindex],
options & OPTION_BIGREQUEST,
options & OPTION_DELAY,
options & OPTION_SHUTDOWN);
}
}
}
wait_all();
}
static void verify_data(
const void *httpdata, size_t httpsize,
const void *refdata, size_t refsize,
const char *path) {
assert(httpdata);
assert(refdata);
assert(path);
if (httpsize != refsize) {
errno = 0;
efmt("download from http://%s:%d%s returned wrong number "
"of bytes: %zd (expected %zd)",
HOST, PORT, path, httpsize, refsize);
} else if (memcmp(httpdata, refdata, refsize) != 0) {
errno = 0;
efmt("download from http://%s:%d%s returned wrong data",
HOST, PORT, path);
}
}
static void callback_verify_path1(const void *data, size_t size) {
verify_data(data, size, PATH1_DATA, strlen(PATH1_DATA), PATH1);
}
static void callback_verify_path2(const void *data, size_t size) {
unsigned short buf[65536];
int i;
for (i = 0; i < 65536; i++) buf[i] = htons(i);
verify_data(data, size, buf, sizeof(buf), PATH2);
}
int main(int argc, char **argv)
{
int use_network;
start(82);
use_network = get_setting_use_network();
if (use_network) {
http_test_all(0 /* multiproc */);
http_test_all(1 /* multiproc */);
} else {
dbgprintf("test disabled, set USENETWORK=yes to enable\n");
}
quit();
return 0;
}
|