forked from varnishcache/varnish-cache
/
vtc_http.c
1986 lines (1774 loc) · 44.9 KB
/
vtc_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
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) 2008-2015 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include <sys/socket.h>
#include <errno.h>
#include <math.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include "vtc.h"
#include "vtc_http.h"
#include "vct.h"
#include "vfil.h"
#include "vgz.h"
#include "vnum.h"
#include "vrnd.h"
#include "vtcp.h"
#include "hpack.h"
extern const struct cmds http_cmds[];
/* SECTION: client-server client/server
*
* Client and server threads are fake HTTP entities used to test your Varnish
* and VCL. They take any number of arguments, and the one that are not
* recognized, assuming they don't start with '-', are treated as
* specifications, laying out the actions to undertake::
*
* client cNAME [...]
* server sNAME [...]
*
* Clients and server are identified by a string that's the first argument,
* clients' names start with 'c' and servers' names start with 's'.
*
* As the client and server commands share a good deal of arguments and
* specification actions, they are grouped in this single section, specific
* items will be explicitly marked as such.
*
* SECTION: client-server.macros Macros and automatic behaviour
*
* To make things easier in the general case, clients will connect by default
* to the first Varnish server declared and the -vcl+backend switch of the
* ``varnish`` command will add all the declared servers as backends.
*
* Be careful though, servers will by default listen to the 127.0.0.1 IP and
* will pick a random port, and publish 3 macros: sNAME_addr, sNAME_port and
* sNAME_sock, but only once they are started.
* For 'varnish -vcl+backend' to create the vcl with the correct values, the
* server must be started first.
*
* SECTION: client-server.args Arguments
*
* \-start
* Start the thread in background, processing the last given
* specification.
*
* \-wait
* Block until the thread finishes.
*
* \-run (client only)
* Equivalent to "-start -wait".
*
* \-repeat NUMBER
* Instead of processing the specification only once, do it NUMBER times.
*
* \-keepalive
* For repeat, do not open new connections but rather run all
* iterations in the same connection
*
* \-break (server only)
* Stop the server.
*
* \-listen STRING (server only)
* Dictate the listening socket for the server. STRING is of the form
* "IP PORT", or "/PATH/TO/SOCKET" for a Unix domain socket. In the
* latter case, the path must begin with '/', and the server must be
* able to create it.
*
* \-connect STRING (client only)
* Indicate the server to connect to. STRING is also of the form
* "IP PORT", or "/PATH/TO/SOCKET". As with "server -listen", a
* Unix domain socket is recognized when STRING begins with a '/'.
*
* \-dispatch (server only, s0 only)
* Normally, to keep things simple, server threads only handle one
* connection at a time, but the -dispatch switch allows to accept
* any number of connection and handle them following the given spec.
*
* However, -dispatch is only allowed for the server name "s0".
*
* \-proxy1 STRING (client only)
* Use the PROXY protocol version 1 for this connection. STRING
* is of the form "CLIENTIP:PORT SERVERIP:PORT".
*
* \-proxy2 STRING (client only)
* Use the PROXY protocol version 2 for this connection. STRING
* is of the form "CLIENTIP:PORT SERVERIP:PORT".
*
* SECTION: client-server.spec Specification
*
* It's a string, either double-quoted "like this", but most of the time
* enclosed in curly brackets, allowing multilining. Write a command per line in
* it, empty line are ignored, and long line can be wrapped by using a
* backslash. For example::
*
* client c1 {
* txreq -url /foo \
* -hdr "bar: baz"
*
* rxresp
* } -run
*/
#define ONLY_CLIENT(hp, av) \
do { \
if (hp->h2) \
vtc_fatal(hp->vl, \
"\"%s\" only possible before H/2 upgrade", \
av[0]); \
if (hp->sfd != NULL) \
vtc_fatal(hp->vl, \
"\"%s\" only possible in client", av[0]); \
} while (0)
#define ONLY_SERVER(hp, av) \
do { \
if (hp->h2) \
vtc_fatal(hp->vl, \
"\"%s\" only possible before H/2 upgrade", \
av[0]); \
if (hp->sfd == NULL) \
vtc_fatal(hp->vl, \
"\"%s\" only possible in server", av[0]); \
} while (0)
/* XXX: we may want to vary this */
static const char * const nl = "\r\n";
/**********************************************************************
* Generate a synthetic body
*/
char *
synth_body(const char *len, int rnd)
{
int i, j, k, l;
char *b;
AN(len);
i = strtoul(len, NULL, 0);
assert(i > 0);
b = malloc(i + 1L);
AN(b);
l = k = '!';
for (j = 0; j < i; j++) {
if ((j % 64) == 63) {
b[j] = '\n';
k++;
if (k == '~')
k = '!';
l = k;
} else if (rnd) {
b[j] = (VRND_RandomTestable() % 95) + ' ';
} else {
b[j] = (char)l;
if (++l == '~')
l = '!';
}
}
b[i - 1] = '\n';
b[i] = '\0';
return (b);
}
/**********************************************************************
* Finish and write the vsb to the fd
*/
static void
http_write(const struct http *hp, int lvl, const char *pfx)
{
AZ(VSB_finish(hp->vsb));
vtc_dump(hp->vl, lvl, pfx, VSB_data(hp->vsb), VSB_len(hp->vsb));
if (VSB_tofile(hp->vsb, hp->fd))
vtc_log(hp->vl, hp->fatal, "Write failed: %s",
strerror(errno));
}
/**********************************************************************
* find header
*/
static char *
http_find_header(char * const *hh, const char *hdr)
{
int n, l;
char *r;
l = strlen(hdr);
for (n = 3; hh[n] != NULL; n++) {
if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':')
continue;
for (r = hh[n] + l + 1; vct_issp(*r); r++)
continue;
return (r);
}
return (NULL);
}
/**********************************************************************
* count header
*/
static int
http_count_header(char * const *hh, const char *hdr)
{
int n, l, r = 0;
l = strlen(hdr);
for (n = 3; hh[n] != NULL; n++) {
if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':')
continue;
r++;
}
return (r);
}
/* SECTION: client-server.spec.expect
*
* expect STRING1 OP STRING2
* Test if "STRING1 OP STRING2" is true, and if not, fails the test.
* OP can be ==, <, <=, >, >= when STRING1 and STRING2 represent numbers
* in which case it's an order operator. If STRING1 and STRING2 are
* meant as strings OP is a matching operator, either == (exact match)
* or ~ (regex match).
*
* varnishtet will first try to resolve STRING1 and STRING2 by looking
* if they have special meanings, in which case, the resolved value is
* use for the test. Note that this value can be a string representing a
* number, allowing for tests such as::
*
* expect req.http.x-num > 2
*
* Here's the list of recognized strings, most should be obvious as they
* either match VCL logic, or the txreq/txresp options:
*
* - remote.ip
* - remote.port
* - remote.path
* - req.method
* - req.url
* - req.proto
* - resp.proto
* - resp.status
* - resp.reason
* - resp.chunklen
* - req.bodylen
* - req.body
* - resp.bodylen
* - resp.body
* - req.http.NAME
* - resp.http.NAME
*/
static const char *
cmd_var_resolve(struct http *hp, char *spec)
{
char **hh, *hdr;
if (!strcmp(spec, "remote.ip"))
return(hp->rem_ip);
if (!strcmp(spec, "remote.port"))
return(hp->rem_port);
if (!strcmp(spec, "remote.path"))
return(hp->rem_path);
if (!strcmp(spec, "req.method"))
return(hp->req[0]);
if (!strcmp(spec, "req.url"))
return(hp->req[1]);
if (!strcmp(spec, "req.proto"))
return(hp->req[2]);
if (!strcmp(spec, "resp.proto"))
return(hp->resp[0]);
if (!strcmp(spec, "resp.status"))
return(hp->resp[1]);
if (!strcmp(spec, "resp.reason"))
return(hp->resp[2]);
if (!strcmp(spec, "resp.chunklen"))
return(hp->chunklen);
if (!strcmp(spec, "req.bodylen"))
return(hp->bodylen);
if (!strcmp(spec, "req.body"))
return(hp->body != NULL ? hp->body : spec);
if (!strcmp(spec, "resp.bodylen"))
return(hp->bodylen);
if (!strcmp(spec, "resp.body"))
return(hp->body != NULL ? hp->body : spec);
if (!strncmp(spec, "req.http.", 9)) {
hh = hp->req;
hdr = spec + 9;
} else if (!strncmp(spec, "resp.http.", 10)) {
hh = hp->resp;
hdr = spec + 10;
} else if (!strcmp(spec, "h2.state")) {
if (hp->h2)
return ("true");
else
return ("false");
} else
return (spec);
hdr = http_find_header(hh, hdr);
return (hdr);
}
static void
cmd_http_expect(CMD_ARGS)
{
struct http *hp;
const char *lhs;
char *cmp;
const char *rhs;
(void)cmd;
(void)vl;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
AZ(strcmp(av[0], "expect"));
av++;
AN(av[0]);
AN(av[1]);
AN(av[2]);
AZ(av[3]);
lhs = cmd_var_resolve(hp, av[0]);
cmp = av[1];
rhs = cmd_var_resolve(hp, av[2]);
vtc_expect(vl, av[0], lhs, cmp, av[2], rhs);
}
static void
cmd_http_expect_pattern(CMD_ARGS)
{
char *p;
struct http *hp;
char t = '0';
(void)cmd;
(void)vl;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
AZ(strcmp(av[0], "expect_pattern"));
av++;
AZ(av[0]);
for (p = hp->body; *p != '\0'; p++) {
if (*p != t)
vtc_fatal(hp->vl,
"EXPECT PATTERN FAIL @%zd should 0x%02x is 0x%02x",
p - hp->body, t, *p);
t += 1;
t &= ~0x08;
}
vtc_log(hp->vl, 4, "EXPECT PATTERN SUCCESS");
}
/**********************************************************************
* Split a HTTP protocol header
*/
static void
http_splitheader(struct http *hp, int req)
{
char *p, *q, **hh;
int n;
char buf[20];
const char* rxbuf_e = &hp->rxbuf[hp->prxbuf];
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
if (req) {
memset(hp->req, 0, sizeof hp->req);
hh = hp->req;
} else {
memset(hp->resp, 0, sizeof hp->resp);
hh = hp->resp;
}
n = 0;
p = hp->rxbuf;
/* REQ/PROTO */
while (vct_islws(*p))
p++;
hh[n++] = p;
while (!vct_islws(*p))
p++;
AZ(vct_iscrlf(p, rxbuf_e));
*p++ = '\0';
/* URL/STATUS */
while (vct_issp(*p)) /* XXX: H space only */
p++;
AZ(vct_iscrlf(p, rxbuf_e));
hh[n++] = p;
while (!vct_islws(*p))
p++;
if (vct_iscrlf(p, rxbuf_e)) {
hh[n++] = NULL;
q = p;
p = vct_skipcrlf(p, rxbuf_e);
*q = '\0';
} else {
*p++ = '\0';
/* PROTO/MSG */
while (vct_issp(*p)) /* XXX: H space only */
p++;
hh[n++] = p;
while (!vct_iscrlf(p, rxbuf_e))
p++;
q = p;
p = vct_skipcrlf(p, rxbuf_e);
*q = '\0';
}
assert(n == 3);
while (*p != '\0') {
assert(n < MAX_HDR);
if (vct_iscrlf(p, rxbuf_e))
break;
hh[n++] = p++;
while (*p != '\0' && !vct_iscrlf(p, rxbuf_e))
p++;
q = p;
p = vct_skipcrlf(p, rxbuf_e);
*q = '\0';
}
p = vct_skipcrlf(p, rxbuf_e);
assert(*p == '\0');
for (n = 0; n < 3 || hh[n] != NULL; n++) {
bprintf(buf, "http[%2d] ", n);
vtc_dump(hp->vl, 4, buf, hh[n], -1);
}
}
/**********************************************************************
* Receive another character
*/
static int
http_rxchar(struct http *hp, int n, int eof)
{
int i;
struct pollfd pfd[1];
while (n > 0) {
pfd[0].fd = hp->fd;
pfd[0].events = POLLIN;
pfd[0].revents = 0;
i = poll(pfd, 1, hp->timeout);
if (i < 0 && errno == EINTR)
continue;
if (i == 0) {
vtc_log(hp->vl, hp->fatal,
"HTTP rx timeout (fd:%d %u ms)",
hp->fd, hp->timeout);
continue;
}
if (i < 0) {
vtc_log(hp->vl, hp->fatal,
"HTTP rx failed (fd:%d poll: %s)",
hp->fd, strerror(errno));
continue;
}
assert(i > 0);
assert(hp->prxbuf + n < hp->nrxbuf);
i = read(hp->fd, hp->rxbuf + hp->prxbuf, n);
if (!(pfd[0].revents & POLLIN))
vtc_log(hp->vl, 4,
"HTTP rx poll (fd:%d revents: %x n=%d, i=%d)",
hp->fd, pfd[0].revents, n, i);
if (i == 0 && eof)
return (i);
if (i == 0) {
vtc_log(hp->vl, hp->fatal,
"HTTP rx EOF (fd:%d read: %s) %d",
hp->fd, strerror(errno), n);
return (-1);
}
if (i < 0) {
vtc_log(hp->vl, hp->fatal,
"HTTP rx failed (fd:%d read: %s)",
hp->fd, strerror(errno));
return (-1);
}
hp->prxbuf += i;
hp->rxbuf[hp->prxbuf] = '\0';
n -= i;
}
return (1);
}
static int
http_rxchunk(struct http *hp)
{
char *q;
int l, i;
l = hp->prxbuf;
do {
if (http_rxchar(hp, 1, 0) < 0)
return (-1);
} while (hp->rxbuf[hp->prxbuf - 1] != '\n');
vtc_dump(hp->vl, 4, "len", hp->rxbuf + l, -1);
i = strtoul(hp->rxbuf + l, &q, 16);
bprintf(hp->chunklen, "%d", i);
if ((q == hp->rxbuf + l) ||
(*q != '\0' && !vct_islws(*q))) {
vtc_log(hp->vl, hp->fatal, "chunked fail %02x @ %td",
*q, q - (hp->rxbuf + l));
return (-1);
}
assert(q != hp->rxbuf + l);
assert(*q == '\0' || vct_islws(*q));
hp->prxbuf = l;
if (i > 0) {
if (http_rxchar(hp, i, 0) < 0)
return (-1);
vtc_dump(hp->vl, 4, "chunk", hp->rxbuf + l, i);
}
l = hp->prxbuf;
if (http_rxchar(hp, 2, 0) < 0)
return (-1);
if (!vct_iscrlf(&hp->rxbuf[l], &hp->rxbuf[hp->prxbuf])) {
vtc_log(hp->vl, hp->fatal,
"Wrong chunk tail[0] = %02x",
hp->rxbuf[l] & 0xff);
return (-1);
}
if (!vct_iscrlf(&hp->rxbuf[l + 1], &hp->rxbuf[hp->prxbuf])) {
vtc_log(hp->vl, hp->fatal,
"Wrong chunk tail[1] = %02x",
hp->rxbuf[l + 1] & 0xff);
return (-1);
}
hp->prxbuf = l;
hp->rxbuf[l] = '\0';
return (i);
}
/**********************************************************************
* Swallow a HTTP message body
*/
static void
http_swallow_body(struct http *hp, char * const *hh, int body)
{
char *p;
int i, l, ll;
ll = 0;
p = http_find_header(hh, "transfer-encoding");
if (p != NULL && !strcasecmp(p, "chunked")) {
while (http_rxchunk(hp) > 0)
continue;
vtc_dump(hp->vl, 4, "body", hp->body, ll);
ll = hp->rxbuf + hp->prxbuf - hp->body;
hp->bodyl = ll;
bprintf(hp->bodylen, "%d", ll);
return;
}
p = http_find_header(hh, "content-length");
if (p != NULL) {
l = strtoul(p, NULL, 10);
if (http_rxchar(hp, l, 0) < 0)
return;
vtc_dump(hp->vl, 4, "body", hp->body, l);
hp->bodyl = l;
bprintf(hp->bodylen, "%d", l);
return;
}
if (body) {
do {
i = http_rxchar(hp, 1, 1);
if (i < 0)
return;
ll += i;
} while (i > 0);
vtc_dump(hp->vl, 4, "rxeof", hp->body, ll);
}
hp->bodyl = ll;
bprintf(hp->bodylen, "%d", ll);
}
/**********************************************************************
* Receive a HTTP protocol header
*/
static void
http_rxhdr(struct http *hp)
{
int i;
char *p;
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC);
hp->prxbuf = 0;
hp->body = NULL;
bprintf(hp->bodylen, "%s", "<undef>");
while (1) {
(void)http_rxchar(hp, 1, 0);
p = hp->rxbuf + hp->prxbuf - 1;
for (i = 0; p > hp->rxbuf; p--) {
if (*p != '\n')
break;
if (p - 1 > hp->rxbuf && p[-1] == '\r')
p--;
if (++i == 2)
break;
}
if (i == 2)
break;
}
vtc_dump(hp->vl, 4, "rxhdr", hp->rxbuf, -1);
vtc_log(hp->vl, 4, "rxhdrlen = %zd", strlen(hp->rxbuf));
hp->body = hp->rxbuf + hp->prxbuf;
}
/* SECTION: client-server.spec.rxresp
*
* rxresp [-no_obj] (client only)
* Receive and parse a response's headers and body. If -no_obj is present, only get
* the headers.
*/
static void
cmd_http_rxresp(CMD_ARGS)
{
struct http *hp;
int has_obj = 1;
(void)cmd;
(void)vl;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
ONLY_CLIENT(hp, av);
AZ(strcmp(av[0], "rxresp"));
av++;
for (; *av != NULL; av++)
if (!strcmp(*av, "-no_obj"))
has_obj = 0;
else
vtc_fatal(hp->vl,
"Unknown http rxresp spec: %s\n", *av);
http_rxhdr(hp);
http_splitheader(hp, 0);
if (http_count_header(hp->resp, "Content-Length") > 1)
vtc_fatal(hp->vl,
"Multiple Content-Length headers.\n");
if (!has_obj)
return;
else if (!strcmp(hp->resp[1], "200"))
http_swallow_body(hp, hp->resp, 1);
else
http_swallow_body(hp, hp->resp, 0);
vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen);
}
/* SECTION: client-server.spec.rxresphdrs
*
* rxresphdrs (client only)
* Receive and parse a response's headers.
*/
static void
cmd_http_rxresphdrs(CMD_ARGS)
{
struct http *hp;
(void)cmd;
(void)vl;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
ONLY_CLIENT(hp, av);
AZ(strcmp(av[0], "rxresphdrs"));
av++;
for (; *av != NULL; av++)
vtc_fatal(hp->vl, "Unknown http rxresp spec: %s\n", *av);
http_rxhdr(hp);
http_splitheader(hp, 0);
if (http_count_header(hp->resp, "Content-Length") > 1)
vtc_fatal(hp->vl,
"Multiple Content-Length headers.\n");
}
/**********************************************************************
* Ungzip rx'ed body
*/
#define OVERHEAD 64L
static void
cmd_http_gunzip(CMD_ARGS)
{
int i;
z_stream vz;
struct http *hp;
char *p;
unsigned l;
(void)av;
(void)cmd;
(void)vl;
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC);
memset(&vz, 0, sizeof vz);
AN(hp->body);
if (hp->body[0] != (char)0x1f || hp->body[1] != (char)0x8b)
vtc_log(hp->vl, hp->fatal,
"Gunzip error: Body lacks gzip magics");
vz.next_in = TRUST_ME(hp->body);
vz.avail_in = hp->bodyl;
l = hp->bodyl * 10;
p = calloc(1, l);
AN(p);
vz.next_out = TRUST_ME(p);
vz.avail_out = l;
assert(Z_OK == inflateInit2(&vz, 31));
i = inflate(&vz, Z_FINISH);
assert(vz.total_out < l);
hp->bodyl = vz.total_out;
memcpy(hp->body, p, hp->bodyl);
free(p);
vtc_log(hp->vl, 3, "new bodylen %u", hp->bodyl);
vtc_dump(hp->vl, 4, "body", hp->body, hp->bodyl);
bprintf(hp->bodylen, "%u", hp->bodyl);
#ifdef VGZ_EXTENSIONS
vtc_log(hp->vl, 4, "startbit = %ju %ju/%ju",
(uintmax_t)vz.start_bit,
(uintmax_t)vz.start_bit >> 3, (uintmax_t)vz.start_bit & 7);
vtc_log(hp->vl, 4, "lastbit = %ju %ju/%ju",
(uintmax_t)vz.last_bit,
(uintmax_t)vz.last_bit >> 3, (uintmax_t)vz.last_bit & 7);
vtc_log(hp->vl, 4, "stopbit = %ju %ju/%ju",
(uintmax_t)vz.stop_bit,
(uintmax_t)vz.stop_bit >> 3, (uintmax_t)vz.stop_bit & 7);
#endif
if (i != Z_STREAM_END)
vtc_log(hp->vl, hp->fatal,
"Gunzip error = %d (%s) in:%jd out:%jd",
i, vz.msg, (intmax_t)vz.total_in, (intmax_t)vz.total_out);
assert(Z_OK == inflateEnd(&vz));
hp->body[hp->bodyl] = '\0';
}
/**********************************************************************
* Create a gzip'ed body
*/
static void
gzip_body(const struct http *hp, const char *txt, char **body, int *bodylen)
{
int l;
z_stream vz;
#ifdef VGZ_EXTENSIONS
int i;
#endif
memset(&vz, 0, sizeof vz);
l = strlen(txt);
*body = calloc(1, l + OVERHEAD);
AN(*body);
vz.next_in = TRUST_ME(txt);
vz.avail_in = l;
vz.next_out = TRUST_ME(*body);
vz.avail_out = l + OVERHEAD;
assert(Z_OK == deflateInit2(&vz,
hp->gziplevel, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY));
assert(Z_STREAM_END == deflate(&vz, Z_FINISH));
*bodylen = vz.total_out;
#ifdef VGZ_EXTENSIONS
i = vz.stop_bit & 7;
if (hp->gzipresidual >= 0 && hp->gzipresidual != i)
vtc_log(hp->vl, hp->fatal,
"Wrong gzip residual got %d wanted %d",
i, hp->gzipresidual);
vtc_log(hp->vl, 4, "startbit = %ju %ju/%ju",
(uintmax_t)vz.start_bit,
(uintmax_t)vz.start_bit >> 3, (uintmax_t)vz.start_bit & 7);
vtc_log(hp->vl, 4, "lastbit = %ju %ju/%ju",
(uintmax_t)vz.last_bit,
(uintmax_t)vz.last_bit >> 3, (uintmax_t)vz.last_bit & 7);
vtc_log(hp->vl, 4, "stopbit = %ju %ju/%ju",
(uintmax_t)vz.stop_bit,
(uintmax_t)vz.stop_bit >> 3, (uintmax_t)vz.stop_bit & 7);
assert(Z_OK == deflateEnd(&vz));
#endif
}
/**********************************************************************
* Handle common arguments of a transmited request or response
*/
static char* const *
http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp,
char *body, unsigned nohost)
{
int bodylen = 0;
char *b, *c;
char *nullbody;
char *m;
int nolen = 0;
int l;
(void)vl;
nullbody = body;
for (; *av != NULL; av++) {
if (!strcmp(*av, "-nolen")) {
nolen = 1;
} else if (!strcmp(*av, "-nohost")) {
nohost = 1;
} else if (!strcmp(*av, "-hdr")) {
if (!strncasecmp(av[1], "Host:", 5))
nohost = 1;
VSB_printf(hp->vsb, "%s%s", av[1], nl);
av++;
} else if (!strcmp(*av, "-hdrlen")) {
VSB_printf(hp->vsb, "%s: ", av[1]);
l = atoi(av[2]);
while (l-- > 0)
VSB_putc(hp->vsb, '0' + (l % 10));
VSB_printf(hp->vsb, "%s", nl);
av+=2;
} else
break;
}
for (; *av != NULL; av++) {
if (!strcmp(*av, "-body")) {
assert(body == nullbody);
REPLACE(body, av[1]);
AN(body);
av++;
bodylen = strlen(body);
for (b = body; *b != '\0'; b++) {
if (*b == '\\' && b[1] == '0') {
*b = '\0';
for (c = b+1; *c != '\0'; c++) {
*c = c[1];
}
b++;
bodylen--;
}
}
} else if (!strcmp(*av, "-bodylen")) {
assert(body == nullbody);
free(body);
body = synth_body(av[1], 0);
bodylen = strlen(body);
av++;
} else if (!strcmp(*av, "-gzipresidual")) {
hp->gzipresidual = strtoul(av[1], NULL, 0);
av++;
} else if (!strcmp(*av, "-gziplevel")) {
hp->gziplevel = strtoul(av[1], NULL, 0);
av++;
} else if (!strcmp(*av, "-gziplen")) {
assert(body == nullbody);
free(body);
b = synth_body(av[1], 1);
gzip_body(hp, b, &body, &bodylen);
free(b);
VSB_printf(hp->vsb, "Content-Encoding: gzip%s", nl);
// vtc_hexdump(hp->vl, 4, "gzip", (void*)body, bodylen);
av++;
} else if (!strcmp(*av, "-gzipbody")) {
assert(body == nullbody);
free(body);
gzip_body(hp, av[1], &body, &bodylen);
VSB_printf(hp->vsb, "Content-Encoding: gzip%s", nl);
// vtc_hexdump(hp->vl, 4, "gzip", (void*)body, bodylen);
av++;
} else
break;
}
if (!nohost) {
m = macro_get("localhost", NULL);
AN(m);
VSB_printf(hp->vsb, "Host: %s%s", m, nl);
free(m);
}
if (body != NULL && !nolen)
VSB_printf(hp->vsb, "Content-Length: %d%s", bodylen, nl);
VSB_cat(hp->vsb, nl);
if (body != NULL) {
VSB_bcat(hp->vsb, body, bodylen);
free(body);
}
return (av);
}
/* SECTION: client-server.spec.txreq
*
* txreq|txresp [...]
* Send a minimal request or response, but overload it if necessary.
*
* txreq is client-specific and txresp is server-specific.
*
* The only thing different between a request and a response, apart
* from who can send them is that the first line (request line vs
* status line), so all the options are prety much the same.
*
* \-req STRING (txreq only)
* What method to use (default: "GET").
*
* \-url STRING (txreq only)
* What location to use (default "/").
*
* \-proto STRING
* What protocol use in the status line.
* (default: "HTTP/1.1").
*
* \-status NUMBER (txresp only)
* What status code to return (default 200).
*
* \-reason STRING (txresp only)
* What message to put in the status line (default: "OK").
*
* These three switches can appear in any order but must come before the
* following ones.
*
* \-nohost
* Don't include a Host header in the request.
*
* \-nolen
* Don't include a Content-Length header.
*
* \-hdr STRING
* Add STRING as a header, it must follow this format:
* "name: value". It can be called multiple times.
*
* \-hdrlen STRING NUMBER
* Add STRING as a header with NUMBER bytes of content.
*
* You can then use the arguments related to the body:
*
* \-body STRING
* Input STRING as body.
*
* \-bodylen NUMBER
* Generate and input a body that is NUMBER bytes-long.
*
* \-gziplevel NUMBER
* Set the gzip level (call it before any of the other gzip
* switches).
*
* \-gzipresidual NUMBER
* Add extra gzip bits. You should never need it.