/
test-http-payload.c
1821 lines (1543 loc) · 45.2 KB
/
test-http-payload.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
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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Copyright (c) 2013-2018 Dovecot authors, see the included COPYING file */
#include "lib.h"
#include "str.h"
#include "llist.h"
#include "path-util.h"
#include "hostpid.h"
#include "ioloop.h"
#include "istream.h"
#include "ostream.h"
#include "iostream-temp.h"
#include "connection.h"
#include "test-common.h"
#include "http-url.h"
#include "http-request.h"
#include "http-server.h"
#include "http-client.h"
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <dirent.h>
#define CLIENT_PROGRESS_TIMEOUT 10
enum payload_handling {
PAYLOAD_HANDLING_LOW_LEVEL,
PAYLOAD_HANDLING_FORWARD,
PAYLOAD_HANDLING_HANDLER,
};
static bool debug = FALSE;
static const char *failure = NULL;
static bool blocking = FALSE;
static enum payload_handling server_payload_handling =
PAYLOAD_HANDLING_LOW_LEVEL;
static unsigned int parallel_clients = 1;
static bool parallel_clients_global = FALSE;
static bool request_100_continue = FALSE;
static size_t read_server_partial = 0;
static size_t read_client_partial = 0;
static unsigned int test_max_pending = 200;
static unsigned int client_ioloop_nesting = 0;
static struct ip_addr bind_ip;
static in_port_t bind_port = 0;
static int fd_listen = -1;
static pid_t server_pid = (pid_t)-1;
static struct ioloop *ioloop_nested = NULL;
static unsigned ioloop_nested_first = 0;
static unsigned ioloop_nested_last = 0;
static unsigned ioloop_nested_depth = 0;
/*
* Test files
*/
static ARRAY_TYPE(const_string) files;
static pool_t files_pool;
static void test_files_read_dir(const char *path)
{
DIR *dirp;
/* open the directory */
if ((dirp = opendir(path)) == NULL) {
if (errno == ENOENT || errno == EACCES)
return;
i_fatal("test files: "
"failed to open directory %s: %m", path);
}
/* read entries */
for (;;) {
const char *file;
struct dirent *dp;
struct stat st;
errno = 0;
if ((dp=readdir(dirp)) == NULL)
break;
if (*dp->d_name == '.')
continue;
file = t_abspath_to(dp->d_name, path);
if (stat(file, &st) == 0) {
if (S_ISREG(st.st_mode)) {
file += 2; /* skip "./" */
file = p_strdup(files_pool, file);
array_append(&files, &file, 1);
} else {
test_files_read_dir(file);
}
}
}
if (errno != 0)
i_fatal("test files: "
"failed to read directory %s: %m", path);
/* Close the directory */
if (closedir(dirp) < 0)
i_error("test files: "
"failed to close directory %s: %m", path);
}
static void test_files_init(void)
{
/* initialize file array */
files_pool = pool_alloconly_create
(MEMPOOL_GROWING"http_server_request", 4096);
p_array_init(&files, files_pool, 512);
/* obtain all filenames */
test_files_read_dir(".");
}
static void test_files_deinit(void)
{
pool_unref(&files_pool);
}
static struct istream *
test_file_open(const char *path,
unsigned int *status_r, const char **reason_r)
ATTR_NULL(2, 3)
{
int fd;
if (status_r != NULL)
*status_r = 200;
if (reason_r != NULL)
*reason_r = "OK";
fd = open(path, O_RDONLY);
if (fd < 0) {
if (debug) {
i_debug("test files: "
"open(%s) failed: %m", path);
}
switch (errno) {
case EFAULT:
case ENOENT:
if (status_r != NULL)
*status_r = 404;
if (reason_r != NULL)
*reason_r = "Not Found";
break;
case EISDIR:
case EACCES:
if (status_r != NULL)
*status_r = 403;
if (reason_r != NULL)
*reason_r = "Forbidden";
break;
default:
if (status_r != NULL)
*status_r = 500;
if (reason_r != NULL)
*reason_r = "Internal Server Error";
}
return NULL;
}
return i_stream_create_fd_autoclose(&fd, 40960);
}
/*
* Test server
*/
struct client {
pool_t pool;
struct client *prev, *next;
struct http_server_connection *http_conn;
};
struct client_request {
struct client *client;
struct http_server_request *server_req;
const char *path;
struct istream *payload_input;
struct ostream *payload_output;
struct io *io;
};
static const struct http_server_callbacks http_callbacks;
static struct http_server *http_server;
static struct io *io_listen;
static struct client *clients;
/* location: /succes */
static void
client_handle_success_request(struct client_request *creq)
{
struct http_server_request *req = creq->server_req;
const struct http_request *hreq =
http_server_request_get(req);
struct http_server_response *resp;
if (strcmp(hreq->method, "GET") != 0) {
http_server_request_fail(req,
405, "Method Not Allowed");
return;
}
resp = http_server_response_create(req, 200, "OK");
http_server_response_submit(resp);
}
/* location: /download/... */
static void
client_handle_download_request(
struct client_request *creq,
const char *path)
{
struct http_server_request *req = creq->server_req;
const struct http_request *hreq =
http_server_request_get(req);
struct http_server_response *resp;
const char *fpath, *reason;
struct istream *fstream;
struct ostream *output;
unsigned int status;
if (strcmp(hreq->method, "GET") != 0) {
http_server_request_fail(req,
405, "Method Not Allowed");
return;
}
fpath = t_strconcat(".", path, NULL);
if (debug) {
i_debug("test server: download: "
"sending payload for %s", fpath);
}
fstream = test_file_open(fpath, &status, &reason);
if (fstream == NULL) {
http_server_request_fail(req, status, reason);
return;
}
resp = http_server_response_create(req, 200, "OK");
http_server_response_add_header(resp, "Content-Type", "text/plain");
if (blocking) {
output = http_server_response_get_payload_output(resp, TRUE);
if (o_stream_send_istream(output, fstream) != OSTREAM_SEND_ISTREAM_RESULT_FINISHED) {
i_fatal("test server: download: "
"failed to send blocking file payload");
}
if (debug) {
i_debug("test server: download: "
"finished sending blocking payload for %s"
"(%"PRIuUOFF_T":%"PRIuUOFF_T")",
fpath, fstream->v_offset, output->offset);
}
o_stream_close(output);
o_stream_unref(&output);
} else {
http_server_response_set_payload(resp, fstream);
http_server_response_submit(resp);
}
i_stream_unref(&fstream);
}
/* location: /echo */
static void
client_request_finish_payload_in(struct client_request *creq)
{
struct http_server_response *resp;
struct istream *payload_input;
payload_input = iostream_temp_finish(&creq->payload_output, 4096);
resp = http_server_response_create
(creq->server_req, 200, "OK");
http_server_response_add_header(resp, "Content-Type", "text/plain");
http_server_response_set_payload(resp, payload_input);
http_server_response_submit(resp);
i_stream_unref(&payload_input);
}
static void
client_request_read_echo(struct client_request *creq)
{
enum ostream_send_istream_result res;
o_stream_set_max_buffer_size(creq->payload_output, IO_BLOCK_SIZE);
res = o_stream_send_istream(creq->payload_output, creq->payload_input);
o_stream_set_max_buffer_size(creq->payload_output, (size_t)-1);
switch (res) {
case OSTREAM_SEND_ISTREAM_RESULT_FINISHED:
break;
case OSTREAM_SEND_ISTREAM_RESULT_WAIT_INPUT:
case OSTREAM_SEND_ISTREAM_RESULT_WAIT_OUTPUT:
return;
case OSTREAM_SEND_ISTREAM_RESULT_ERROR_INPUT:
i_fatal("test server: echo: "
"Failed to read all echo payload [%s]", creq->path);
case OSTREAM_SEND_ISTREAM_RESULT_ERROR_OUTPUT:
i_fatal("test server: echo: "
"Failed to write all echo payload [%s]", creq->path);
}
client_request_finish_payload_in(creq);
i_stream_unref(&creq->payload_input);
}
static void
client_request_read_echo_more(struct client_request *creq)
{
client_request_read_echo(creq);
if (creq->payload_input != NULL)
return;
io_remove(&creq->io);
if (debug) {
i_debug("test server: echo: "
"finished receiving payload for %s", creq->path);
}
}
static void
client_handle_echo_request(struct client_request *creq,
const char *path)
{
struct http_server_request *req = creq->server_req;
const struct http_request *hreq =
http_server_request_get(req);
struct http_server_response *resp;
struct ostream *payload_output;
uoff_t size;
creq->path = p_strdup
(http_server_request_get_pool(req), path);
if (strcmp(hreq->method, "PUT") != 0) {
http_server_request_fail(req,
405, "Method Not Allowed");
return;
}
size = 0;
(void)http_request_get_payload_size(hreq, &size);
if (size == 0) {
resp = http_server_response_create
(creq->server_req, 200, "OK");
http_server_response_add_header(resp, "Content-Type", "text/plain");
http_server_response_submit(resp);
return;
}
payload_output = iostream_temp_create
("/tmp/test-http-server", 0);
if (blocking) {
struct istream *payload_input;
payload_input =
http_server_request_get_payload_input(req, TRUE);
if (read_server_partial > 0) {
struct istream *partial =
i_stream_create_limit(payload_input, read_server_partial);
i_stream_unref(&payload_input);
payload_input = partial;
}
if (o_stream_send_istream(payload_output, payload_input) != OSTREAM_SEND_ISTREAM_RESULT_FINISHED) {
i_fatal("test server: echo: "
"failed to receive blocking echo payload");
}
i_stream_unref(&payload_input);
payload_input = iostream_temp_finish(&payload_output, 4096);
if (debug) {
i_debug("test server: echo: "
"finished receiving blocking payload for %s", path);
}
resp = http_server_response_create(req, 200, "OK");
http_server_response_add_header(resp, "Content-Type", "text/plain");
payload_output = http_server_response_get_payload_output(resp, TRUE);
if (o_stream_send_istream(payload_output, payload_input) != OSTREAM_SEND_ISTREAM_RESULT_FINISHED) {
i_fatal("test server: echo: "
"failed to send blocking echo payload");
}
if (debug) {
i_debug("test server: echo: "
"finished sending blocking payload for %s", path);
}
i_stream_unref(&payload_input);
o_stream_close(payload_output);
o_stream_unref(&payload_output);
} else {
creq->payload_output = payload_output;
switch (server_payload_handling) {
case PAYLOAD_HANDLING_LOW_LEVEL:
creq->payload_input =
http_server_request_get_payload_input(req, FALSE);
if (read_server_partial > 0) {
struct istream *partial =
i_stream_create_limit(creq->payload_input, read_server_partial);
i_stream_unref(&creq->payload_input);
creq->payload_input = partial;
}
creq->io = io_add_istream(creq->payload_input,
client_request_read_echo_more, creq);
client_request_read_echo_more(creq);
break;
case PAYLOAD_HANDLING_FORWARD:
http_server_request_forward_payload(req,
payload_output, (size_t)-1,
client_request_finish_payload_in, creq);
break;
case PAYLOAD_HANDLING_HANDLER:
creq->payload_input =
http_server_request_get_payload_input(req, FALSE);
http_server_request_handle_payload(req,
client_request_read_echo, creq);
break;
}
}
}
/* request */
static void
http_server_request_destroyed(struct client_request *creq);
static struct client_request *
client_request_init(struct client *client,
struct http_server_request *req)
{
struct client_request *creq;
pool_t pool = http_server_request_get_pool(req);
http_server_request_ref(req);
creq = p_new(pool, struct client_request, 1);
creq->client = client;
creq->server_req = req;
http_server_request_set_destroy_callback(req,
http_server_request_destroyed, creq);
return creq;
}
static void client_request_deinit(struct client_request **_creq)
{
struct client_request *creq = *_creq;
struct http_server_request *req = creq->server_req;
*_creq = NULL;
if (creq->io != NULL) {
i_stream_unref(&creq->payload_input);
io_remove(&creq->io);
}
http_server_request_unref(&req);
}
static void
http_server_request_destroyed(struct client_request *creq)
{
client_request_deinit(&creq);
}
static void
client_handle_request(void *context,
struct http_server_request *req)
{
const struct http_request *hreq =
http_server_request_get(req);
const char *path = hreq->target.url->path, *p;
struct client *client = (struct client *)context;
struct client_request *creq;
if (debug) {
i_debug("test server: "
"request method=`%s' path=`%s'", hreq->method, path);
}
creq = client_request_init(client, req);
if (strcmp(path, "/success") == 0) {
client_handle_success_request(creq);
return;
}
if ((p=strchr(path+1, '/')) == NULL) {
http_server_request_fail(req, 404, "Not found");
return;
}
if (strncmp(path, "/download", p-path) == 0) {
client_handle_download_request(creq, p);
return;
}
if (strncmp(path, "/echo", p-path) == 0) {
client_handle_echo_request(creq, p);
return;
}
http_server_request_fail(req, 404, "Not found");
return;
}
/* client connection */
static void
client_connection_destroy(void *context, const char *reason);
static const struct http_server_callbacks http_callbacks = {
.connection_destroy = client_connection_destroy,
.handle_request = client_handle_request
};
static void client_init(int fd)
{
struct client *client;
pool_t pool;
net_set_nonblock(fd, TRUE);
pool = pool_alloconly_create("client", 256);
client = p_new(pool, struct client, 1);
client->pool = pool;
client->http_conn = http_server_connection_create(http_server,
fd, fd, FALSE, &http_callbacks, client);
DLLIST_PREPEND(&clients, client);
}
static void client_deinit(struct client **_client)
{
struct client *client = *_client;
*_client = NULL;
DLLIST_REMOVE(&clients, client);
if (client->http_conn != NULL)
http_server_connection_close(&client->http_conn, "deinit");
pool_unref(&client->pool);
}
static void
client_connection_destroy(void *context, const char *reason ATTR_UNUSED)
{
struct client *client = context;
client->http_conn = NULL;
client_deinit(&client);
}
static void client_accept(void *context ATTR_UNUSED)
{
int fd;
/* accept new client */
fd = net_accept(fd_listen, NULL, NULL);
if (fd == -1)
return;
if (fd == -2) {
i_fatal("test server: accept() failed: %m");
}
client_init(fd);
}
/* */
static void
test_server_init(const struct http_server_settings *server_set)
{
/* open server socket */
io_listen = io_add(fd_listen,
IO_READ, client_accept, (void *)NULL);
http_server = http_server_init(server_set);
}
static void test_server_deinit(void)
{
/* close server socket */
io_remove(&io_listen);
/* deinitialize */
http_server_deinit(&http_server);
}
/*
* Test client
*/
struct test_client_request {
struct test_client_request *prev, *next;
struct http_client_request *hreq;
struct io *io;
struct istream *payload;
struct istream *file;
unsigned int files_idx;
};
static struct http_client **http_clients;
static struct test_client_request *client_requests;
static unsigned int client_files_first, client_files_last;
struct timeout *to_client_progress = NULL;
static struct test_client_request *
test_client_request_new(void)
{
struct test_client_request *tcreq;
tcreq = i_new(struct test_client_request, 1);
DLLIST_PREPEND(&client_requests, tcreq);
return tcreq;
}
static void
test_client_request_destroy(struct test_client_request *tcreq)
{
io_remove(&tcreq->io);
i_stream_unref(&tcreq->payload);
i_stream_unref(&tcreq->file);
DLLIST_REMOVE(&client_requests, tcreq);
i_free(tcreq);
}
static void
test_client_switch_ioloop(void)
{
struct test_client_request *tcreq;
to_client_progress = io_loop_move_timeout(&to_client_progress);
for (tcreq = client_requests; tcreq != NULL;
tcreq = tcreq->next) {
if (tcreq->io != NULL)
tcreq->io = io_loop_move_io(&tcreq->io);
if (tcreq->payload != NULL)
i_stream_switch_ioloop(tcreq->payload);
}
}
static void
test_client_progress_timeout(void *context ATTR_UNUSED)
{
/* Terminate test due to lack of progress */
failure = "Test is hanging";
timeout_remove(&to_client_progress);
io_loop_stop(current_ioloop);
}
static void
test_client_create_clients(const struct http_client_settings *client_set)
{
struct http_client_context *http_context;
unsigned int i;
to_client_progress = timeout_add(CLIENT_PROGRESS_TIMEOUT*1000,
test_client_progress_timeout, NULL);
if (!parallel_clients_global)
http_context = http_client_context_create(client_set);
if (parallel_clients < 1)
parallel_clients = 1;
http_clients = i_new(struct http_client *, parallel_clients);
for (i = 0; i < parallel_clients; i++) {
http_clients[i] = (parallel_clients_global ?
http_client_init(client_set) :
http_client_init_shared(http_context, NULL));
}
if (!parallel_clients_global)
http_client_context_unref(&http_context);
}
/* download */
static void test_client_download_continue(void);
static void
test_client_download_finished(unsigned int files_idx)
{
const char **paths;
unsigned int count;
paths = array_get_modifiable(&files, &count);
i_assert(files_idx < count);
i_assert(client_files_first < count);
i_assert(paths[files_idx] != NULL);
paths[files_idx] = NULL;
test_client_download_continue();
}
static void
test_client_download_payload_input(struct test_client_request *tcreq)
{
struct istream *payload = tcreq->payload;
const unsigned char *pdata, *fdata;
size_t psize, fsize, pleft;
unsigned int files_idx = tcreq->files_idx;
off_t ret;
timeout_reset(to_client_progress);
/* read payload */
while ((ret=i_stream_read_more(payload, &pdata, &psize)) > 0) {
if (debug) {
i_debug("test client: download: "
"got data for [%u] (size=%d)",
tcreq->files_idx, (int)psize);
}
/* compare with file on disk */
pleft = psize;
while ((ret=i_stream_read_more
(tcreq->file, &fdata, &fsize)) > 0 && pleft > 0) {
fsize = (fsize > pleft ? pleft : fsize);
if (memcmp(pdata, fdata, fsize) != 0) {
i_fatal("test client: download: "
"received data does not match file "
"(%"PRIuUOFF_T":%"PRIuUOFF_T")",
payload->v_offset, tcreq->file->v_offset);
}
i_stream_skip(tcreq->file, fsize);
pleft -= fsize;
pdata += fsize;
}
if (ret < 0 && tcreq->file->stream_errno != 0) {
i_fatal("test client: download: "
"failed to read file: %s", i_stream_get_error(tcreq->file));
}
i_stream_skip(payload, psize);
}
if (ret == 0) {
if (debug) {
i_debug("test client: download: "
"need more data for [%u]",
tcreq->files_idx);
}
/* we will be called again for this request */
} else {
(void)i_stream_read(tcreq->file);
if (payload->stream_errno != 0) {
i_fatal("test client: download: "
"failed to read request payload: %s",
i_stream_get_error(payload));
} if (i_stream_have_bytes_left(tcreq->file)) {
if (i_stream_read_more(tcreq->file, &fdata, &fsize) <= 0)
fsize = 0;
i_fatal("test client: download: "
"payload ended prematurely "
"(at least %"PRIuSIZE_T" bytes left)", fsize);
} else if (debug) {
i_debug("test client: download: "
"finished request for [%u]",
tcreq->files_idx);
}
/* dereference payload stream; finishes the request */
tcreq->payload = NULL;
io_remove(&tcreq->io); /* holds a reference too */
i_stream_unref(&payload);
/* finished */
test_client_download_finished(files_idx);
}
}
static void
test_client_download_response(const struct http_response *resp,
struct test_client_request *tcreq)
{
const char **paths;
const char *path;
unsigned int count, status;
struct istream *fstream;
const char *reason;
if (debug) {
i_debug("test client: download: "
"got response for [%u]",
tcreq->files_idx);
}
timeout_reset(to_client_progress);
paths = array_get_modifiable(&files, &count);
i_assert(tcreq->files_idx < count);
i_assert(client_files_first < count);
path = paths[tcreq->files_idx];
i_assert(path != NULL);
if (debug) {
i_debug("test client: download: "
"path for [%u]: %s",
tcreq->files_idx, path);
}
fstream = test_file_open(path, &status, &reason);
i_assert(fstream != NULL);
if (status != resp->status) {
i_fatal("test client: download: "
"got wrong response for %s: %u %s (expected: %u %s)",
path, resp->status, resp->reason, status, reason);
}
if (resp->status / 100 != 2) {
if (debug) {
i_debug("test client: download: "
"HTTP request for %s failed: %u %s",
path, resp->status, resp->reason);
}
i_stream_unref(&fstream);
test_client_download_finished(tcreq->files_idx);
return;
}
if (resp->payload == NULL) {
if (debug) {
i_debug("test client: download: "
"no payload for %s [%u]",
path, tcreq->files_idx);
}
i_stream_unref(&fstream);
test_client_download_finished(tcreq->files_idx);
return;
}
i_assert(fstream != NULL);
if (read_client_partial == 0) {
i_stream_ref(resp->payload);
tcreq->payload = resp->payload;
tcreq->file = fstream;
} else {
struct istream *payload = resp->payload;
tcreq->payload = i_stream_create_limit
(payload, read_client_partial);
tcreq->file = i_stream_create_limit
(fstream, read_client_partial);
i_stream_unref(&fstream);
}
tcreq->io = io_add_istream(tcreq->payload,
test_client_download_payload_input, tcreq);
test_client_download_payload_input(tcreq);
}
static void test_client_download_continue(void)
{
struct test_client_request *tcreq;
struct http_client_request *hreq;
const char *const *paths;
unsigned int count;
paths = array_get(&files, &count);
i_assert(client_files_first <= count);
i_assert(client_files_last <= count);
i_assert(client_files_first <= client_files_last);
for (; client_files_first < client_files_last &&
paths[client_files_first] == NULL; client_files_first++)
if (debug) {
i_debug("test client: download: "
"received until [%u]",
client_files_first-1);
}
if (client_files_first >= count) {
io_loop_stop(current_ioloop);
return;
}
for (; client_files_last < count &&
(client_files_last - client_files_first) < test_max_pending;
client_files_last++) {
struct http_client *http_client =
http_clients[client_files_last % parallel_clients];
const char *path = paths[client_files_last];
tcreq = test_client_request_new();
tcreq->files_idx = client_files_last;
if (debug) {
i_debug("test client: download: "
"retrieving %s [%u]",
path, tcreq->files_idx);
}
hreq = tcreq->hreq = http_client_request(http_client,
"GET", net_ip2addr(&bind_ip),
t_strconcat("/download/", path, NULL),
test_client_download_response, tcreq);
http_client_request_set_port(hreq, bind_port);
http_client_request_set_destroy_callback(hreq,
test_client_request_destroy, tcreq);
http_client_request_submit(hreq);
}
}
static void
test_client_download(const struct http_client_settings *client_set)
{
/* create client(s) */
test_client_create_clients(client_set);
/* start querying server */
client_files_first = client_files_last = 0;
test_client_download_continue();
}
/* echo */
static void test_client_echo_continue(void);
static void
test_client_echo_finished(unsigned int files_idx)
{
const char **paths;
unsigned int count;
paths = array_get_modifiable(&files, &count);
i_assert(files_idx < count);
i_assert(client_files_first < count);
i_assert(paths[files_idx] != NULL);
paths[files_idx] = NULL;
test_client_echo_continue();
}
static void
test_client_echo_payload_input(struct test_client_request *tcreq)
{
struct istream *payload = tcreq->payload;
const unsigned char *pdata, *fdata;
size_t psize, fsize, pleft;
unsigned int files_idx = tcreq->files_idx;
off_t ret;
timeout_reset(to_client_progress);
/* read payload */
while ((ret=i_stream_read_more(payload, &pdata, &psize)) > 0) {
if (debug) {
i_debug("test client: echo: "
"got data for [%u] (size=%d)",
tcreq->files_idx, (int)psize);
}
/* compare with file on disk */
pleft = psize;
while ((ret=i_stream_read_more
(tcreq->file, &fdata, &fsize)) > 0 && pleft > 0) {
fsize = (fsize > pleft ? pleft : fsize);
if (memcmp(pdata, fdata, fsize) != 0) {
i_fatal("test client: echo: "
"received data does not match file "
"(%"PRIuUOFF_T":%"PRIuUOFF_T")",
payload->v_offset, tcreq->file->v_offset);
}
i_stream_skip(tcreq->file, fsize);
pleft -= fsize;