| | varnish-cache/bin/varnishtest/vtest2/src/vtc_http.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2008-2019 Varnish Software AS |
| 2 |
|
* All rights reserved. |
| 3 |
|
* |
| 4 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 5 |
|
* |
| 6 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 7 |
|
* |
| 8 |
|
* Redistribution and use in source and binary forms, with or without |
| 9 |
|
* modification, are permitted provided that the following conditions |
| 10 |
|
* are met: |
| 11 |
|
* 1. Redistributions of source code must retain the above copyright |
| 12 |
|
* notice, this list of conditions and the following disclaimer. |
| 13 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 14 |
|
* notice, this list of conditions and the following disclaimer in the |
| 15 |
|
* documentation and/or other materials provided with the distribution. |
| 16 |
|
* |
| 17 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 18 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 21 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 23 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 25 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 26 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 27 |
|
* SUCH DAMAGE. |
| 28 |
|
*/ |
| 29 |
|
|
| 30 |
|
#include "config.h" |
| 31 |
|
|
| 32 |
|
#include <sys/socket.h> |
| 33 |
|
|
| 34 |
|
#include <math.h> |
| 35 |
|
#include <poll.h> |
| 36 |
|
#include <stdio.h> |
| 37 |
|
#include <stdlib.h> |
| 38 |
|
#include <unistd.h> |
| 39 |
|
#include <string.h> |
| 40 |
|
|
| 41 |
|
#include "vtc.h" |
| 42 |
|
#include "vtc_http.h" |
| 43 |
|
|
| 44 |
|
#include "vct.h" |
| 45 |
|
#include "vfil.h" |
| 46 |
|
#include "vnum.h" |
| 47 |
|
#include "vrnd.h" |
| 48 |
|
#include "vtcp.h" |
| 49 |
|
#include "vtim.h" |
| 50 |
|
#include "hpack.h" |
| 51 |
|
|
| 52 |
|
extern const struct cmds http_cmds[]; |
| 53 |
|
|
| 54 |
|
/* SECTION: client-server client/server |
| 55 |
|
* |
| 56 |
|
* Client and server threads are fake HTTP entities used to test your Varnish |
| 57 |
|
* and VCL. They take any number of arguments, and the one that are not |
| 58 |
|
* recognized, assuming they don't start with '-', are treated as |
| 59 |
|
* specifications, laying out the actions to undertake:: |
| 60 |
|
* |
| 61 |
|
* client cNAME [...] |
| 62 |
|
* server sNAME [...] |
| 63 |
|
* |
| 64 |
|
* Clients and server are identified by a string that's the first argument, |
| 65 |
|
* clients' names start with 'c' and servers' names start with 's'. |
| 66 |
|
* |
| 67 |
|
* As the client and server commands share a good deal of arguments and |
| 68 |
|
* specification actions, they are grouped in this single section, specific |
| 69 |
|
* items will be explicitly marked as such. |
| 70 |
|
* |
| 71 |
|
* SECTION: client-server.macros Macros and automatic behaviour |
| 72 |
|
* |
| 73 |
|
* To make things easier in the general case, clients will connect by default |
| 74 |
|
* to a Varnish server called v1. To connect to a different Varnish server, use |
| 75 |
|
* '-connect ${vNAME_sock}'. |
| 76 |
|
* |
| 77 |
|
* The -vcl+backend switch of the ``varnish`` command will add all the declared |
| 78 |
|
* servers as backends. Be careful though, servers will by default listen to |
| 79 |
|
* the 127.0.0.1 IP and will pick a random port, and publish 3 macros: |
| 80 |
|
* sNAME_addr, sNAME_port and sNAME_sock, but only once they are started. For |
| 81 |
|
* 'varnish -vcl+backend' to create the vcl with the correct values, the server |
| 82 |
|
* must be started first. |
| 83 |
|
* |
| 84 |
|
* SECTION: client-server.args Arguments |
| 85 |
|
* |
| 86 |
|
* \-start |
| 87 |
|
* Start the thread in background, processing the last given |
| 88 |
|
* specification. |
| 89 |
|
* |
| 90 |
|
* \-wait |
| 91 |
|
* Block until the thread finishes. |
| 92 |
|
* |
| 93 |
|
* \-run (client only) |
| 94 |
|
* Equivalent to "-start -wait". |
| 95 |
|
* |
| 96 |
|
* \-repeat NUMBER |
| 97 |
|
* Instead of processing the specification only once, do it NUMBER times. |
| 98 |
|
* |
| 99 |
|
* \-keepalive |
| 100 |
|
* For repeat, do not open new connections but rather run all |
| 101 |
|
* iterations in the same connection |
| 102 |
|
* |
| 103 |
|
* \-break (server only) |
| 104 |
|
* Stop the server. |
| 105 |
|
* |
| 106 |
|
* \-listen STRING (server only) |
| 107 |
|
* Dictate the listening socket for the server. STRING is of the form |
| 108 |
|
* "IP PORT", or "/PATH/TO/SOCKET" for a Unix domain socket. In the |
| 109 |
|
* latter case, the path must begin with '/', and the server must be |
| 110 |
|
* able to create it. |
| 111 |
|
* |
| 112 |
|
* \-connect STRING (client only) |
| 113 |
|
* Indicate the server to connect to. STRING is also of the form |
| 114 |
|
* "IP PORT", or "/PATH/TO/SOCKET". As with "server -listen", a |
| 115 |
|
* Unix domain socket is recognized when STRING begins with a '/'. |
| 116 |
|
* |
| 117 |
|
* \-dispatch (server only, s0 only) |
| 118 |
|
* Normally, to keep things simple, server threads only handle one |
| 119 |
|
* connection at a time, but the -dispatch switch allows to accept |
| 120 |
|
* any number of connection and handle them following the given spec. |
| 121 |
|
* |
| 122 |
|
* However, -dispatch is only allowed for the server name "s0". |
| 123 |
|
* |
| 124 |
|
* \-proxy1 STRING (client only) |
| 125 |
|
* Use the PROXY protocol version 1 for this connection. STRING |
| 126 |
|
* is of the form "CLIENTIP:PORT SERVERIP:PORT". |
| 127 |
|
* |
| 128 |
|
* \-proxy2 STRING (client only) |
| 129 |
|
* Use the PROXY protocol version 2 for this connection. STRING |
| 130 |
|
* is of the form "CLIENTIP:PORT SERVERIP:PORT [TLV [TLV ... ]]". |
| 131 |
|
* |
| 132 |
|
* TLV is in the form name=val |
| 133 |
|
* |
| 134 |
|
* name: 0xID or alpn, authority, crc32c, noop, unique_id, netns |
| 135 |
|
* val: 0x... or string |
| 136 |
|
* |
| 137 |
|
* ssl is currently not implemented (can be sent as hex) |
| 138 |
|
* |
| 139 |
|
* SECTION: client-server.spec Specification |
| 140 |
|
* |
| 141 |
|
* It's a string, either double-quoted "like this", but most of the time |
| 142 |
|
* enclosed in curly brackets, allowing multilining. Write a command per line in |
| 143 |
|
* it, empty line are ignored, and long line can be wrapped by using a |
| 144 |
|
* backslash. For example:: |
| 145 |
|
* |
| 146 |
|
* client c1 { |
| 147 |
|
* txreq -url /foo \ |
| 148 |
|
* -hdr "bar: baz" |
| 149 |
|
* |
| 150 |
|
* rxresp |
| 151 |
|
* } -run |
| 152 |
|
*/ |
| 153 |
|
|
| 154 |
|
#define ONLY_CLIENT(hp, av) \ |
| 155 |
|
do { \ |
| 156 |
|
if (hp->h2) \ |
| 157 |
|
vtc_fatal(hp->vl, \ |
| 158 |
|
"\"%s\" only possible before H/2 upgrade", \ |
| 159 |
|
av[0]); \ |
| 160 |
|
if (hp->sfd != NULL) \ |
| 161 |
|
vtc_fatal(hp->vl, \ |
| 162 |
|
"\"%s\" only possible in client", av[0]); \ |
| 163 |
|
} while (0) |
| 164 |
|
|
| 165 |
|
#define ONLY_SERVER(hp, av) \ |
| 166 |
|
do { \ |
| 167 |
|
if (hp->h2) \ |
| 168 |
|
vtc_fatal(hp->vl, \ |
| 169 |
|
"\"%s\" only possible before H/2 upgrade", \ |
| 170 |
|
av[0]); \ |
| 171 |
|
if (hp->sfd == NULL) \ |
| 172 |
|
vtc_fatal(hp->vl, \ |
| 173 |
|
"\"%s\" only possible in server", av[0]); \ |
| 174 |
|
} while (0) |
| 175 |
|
|
| 176 |
|
|
| 177 |
|
/* XXX: we may want to vary this */ |
| 178 |
|
static const char * const nl = "\r\n"; |
| 179 |
|
|
| 180 |
|
/********************************************************************** |
| 181 |
|
* Generate a synthetic body |
| 182 |
|
*/ |
| 183 |
|
|
| 184 |
|
char * |
| 185 |
21600 |
synth_body(const char *len, int rnd) |
| 186 |
|
{ |
| 187 |
|
int i, j, k, l; |
| 188 |
|
char *b; |
| 189 |
|
|
| 190 |
|
|
| 191 |
21600 |
AN(len); |
| 192 |
21600 |
i = strtoul(len, NULL, 0); |
| 193 |
21600 |
assert(i > 0); |
| 194 |
21600 |
b = malloc(i + 1L); |
| 195 |
21600 |
AN(b); |
| 196 |
21600 |
l = k = '!'; |
| 197 |
1443873310 |
for (j = 0; j < i; j++) { |
| 198 |
1443851710 |
if ((j % 64) == 63) { |
| 199 |
22547120 |
b[j] = '\n'; |
| 200 |
22547120 |
k++; |
| 201 |
22547120 |
if (k == '~') |
| 202 |
240320 |
k = '!'; |
| 203 |
22547120 |
l = k; |
| 204 |
1443851710 |
} else if (rnd) { |
| 205 |
681240 |
b[j] = (VRND_RandomTestable() % 95) + ' '; |
| 206 |
681240 |
} else { |
| 207 |
1420623350 |
b[j] = (char)l; |
| 208 |
1420623350 |
if (++l == '~') |
| 209 |
15214600 |
l = '!'; |
| 210 |
|
} |
| 211 |
1443851710 |
} |
| 212 |
21600 |
b[i - 1] = '\n'; |
| 213 |
21600 |
b[i] = '\0'; |
| 214 |
21600 |
return (b); |
| 215 |
|
} |
| 216 |
|
|
| 217 |
|
/********************************************************************** |
| 218 |
|
* Finish and write the vsb to the fd |
| 219 |
|
*/ |
| 220 |
|
|
| 221 |
|
static void |
| 222 |
229641 |
http_write(const struct http *hp, int lvl, const char *pfx) |
| 223 |
|
{ |
| 224 |
|
|
| 225 |
229641 |
AZ(VSB_finish(hp->vsb)); |
| 226 |
229641 |
vtc_dump(hp->vl, lvl, pfx, VSB_data(hp->vsb), VSB_len(hp->vsb)); |
| 227 |
229641 |
if (VSB_tofile(hp->vsb, hp->sess->fd)) |
| 228 |
532 |
vtc_log(hp->vl, hp->fatal, "Write failed: %s", |
| 229 |
266 |
strerror(errno)); |
| 230 |
229641 |
} |
| 231 |
|
|
| 232 |
|
/********************************************************************** |
| 233 |
|
* find header |
| 234 |
|
*/ |
| 235 |
|
|
| 236 |
|
static char * |
| 237 |
581806 |
http_find_header(char * const *hh, const char *hdr) |
| 238 |
|
{ |
| 239 |
|
int n, l; |
| 240 |
|
char *r; |
| 241 |
|
|
| 242 |
581806 |
l = strlen(hdr); |
| 243 |
|
|
| 244 |
4265305 |
for (n = 3; hh[n] != NULL; n++) { |
| 245 |
3937689 |
if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':') |
| 246 |
3683499 |
continue; |
| 247 |
508774 |
for (r = hh[n] + l + 1; vct_issp(*r); r++) |
| 248 |
254584 |
continue; |
| 249 |
254190 |
return (r); |
| 250 |
|
} |
| 251 |
327616 |
return (NULL); |
| 252 |
581806 |
} |
| 253 |
|
|
| 254 |
|
/********************************************************************** |
| 255 |
|
* count header |
| 256 |
|
*/ |
| 257 |
|
|
| 258 |
|
static int |
| 259 |
227669 |
http_count_header(char * const *hh, const char *hdr) |
| 260 |
|
{ |
| 261 |
227669 |
int n, l, r = 0; |
| 262 |
|
|
| 263 |
227669 |
l = strlen(hdr); |
| 264 |
|
|
| 265 |
1835563 |
for (n = 3; hh[n] != NULL; n++) { |
| 266 |
1607894 |
if (strncasecmp(hdr, hh[n], l) || hh[n][l] != ':') |
| 267 |
1494445 |
continue; |
| 268 |
113449 |
r++; |
| 269 |
113449 |
} |
| 270 |
227669 |
return (r); |
| 271 |
|
} |
| 272 |
|
|
| 273 |
|
/* SECTION: client-server.spec.expect |
| 274 |
|
* |
| 275 |
|
* expect STRING1 OP STRING2 |
| 276 |
|
* Test if "STRING1 OP STRING2" is true, and if not, fails the test. |
| 277 |
|
* OP can be ==, <, <=, >, >= when STRING1 and STRING2 represent numbers |
| 278 |
|
* in which case it's an order operator. If STRING1 and STRING2 are |
| 279 |
|
* meant as strings OP is a matching operator, either == (exact match) |
| 280 |
|
* or ~ (regex match). |
| 281 |
|
* |
| 282 |
|
* varnishtest will first try to resolve STRING1 and STRING2 by looking |
| 283 |
|
* if they have special meanings, in which case, the resolved value is |
| 284 |
|
* use for the test. Note that this value can be a string representing a |
| 285 |
|
* number, allowing for tests such as:: |
| 286 |
|
* |
| 287 |
|
* expect req.http.x-num > 2 |
| 288 |
|
* |
| 289 |
|
* Here's the list of recognized strings, most should be obvious as they |
| 290 |
|
* either match VCL logic, or the txreq/txresp options: |
| 291 |
|
* |
| 292 |
|
* - remote.ip |
| 293 |
|
* - remote.port |
| 294 |
|
* - remote.path |
| 295 |
|
* - req.method |
| 296 |
|
* - req.url |
| 297 |
|
* - req.proto |
| 298 |
|
* - resp.proto |
| 299 |
|
* - resp.status |
| 300 |
|
* - resp.reason |
| 301 |
|
* - resp.chunklen |
| 302 |
|
* - req.bodylen |
| 303 |
|
* - req.body |
| 304 |
|
* - resp.bodylen |
| 305 |
|
* - resp.body |
| 306 |
|
* - req.http.NAME |
| 307 |
|
* - resp.http.NAME |
| 308 |
|
*/ |
| 309 |
|
|
| 310 |
|
static const char * |
| 311 |
549190 |
cmd_var_resolve(struct http *hp, char *spec) |
| 312 |
|
{ |
| 313 |
|
char **hh, *hdr; |
| 314 |
549190 |
if (!strcmp(spec, "remote.ip")) |
| 315 |
160 |
return (hp->rem_ip); |
| 316 |
549030 |
if (!strcmp(spec, "remote.port")) |
| 317 |
160 |
return (hp->rem_port); |
| 318 |
548870 |
if (!strcmp(spec, "remote.path")) |
| 319 |
160 |
return (hp->rem_path); |
| 320 |
548710 |
if (!strcmp(spec, "req.method")) |
| 321 |
1560 |
return (hp->req[0]); |
| 322 |
547150 |
if (!strcmp(spec, "req.url")) |
| 323 |
24840 |
return (hp->req[1]); |
| 324 |
522310 |
if (!strcmp(spec, "req.proto")) |
| 325 |
1160 |
return (hp->req[2]); |
| 326 |
521150 |
if (!strcmp(spec, "resp.proto")) |
| 327 |
719 |
return (hp->resp[0]); |
| 328 |
520431 |
if (!strcmp(spec, "resp.status")) |
| 329 |
65840 |
return (hp->resp[1]); |
| 330 |
454591 |
if (!strcmp(spec, "resp.reason")) |
| 331 |
8560 |
return (hp->resp[2]); |
| 332 |
446031 |
if (!strcmp(spec, "resp.chunklen")) |
| 333 |
0 |
return (hp->chunklen); |
| 334 |
446031 |
if (!strcmp(spec, "req.bodylen")) |
| 335 |
1160 |
return (hp->bodylen); |
| 336 |
444871 |
if (!strcmp(spec, "req.body")) |
| 337 |
1160 |
return (hp->body != NULL ? hp->body : spec); |
| 338 |
443711 |
if (!strcmp(spec, "resp.bodylen")) |
| 339 |
27475 |
return (hp->bodylen); |
| 340 |
416236 |
if (!strcmp(spec, "resp.body")) |
| 341 |
8798 |
return (hp->body != NULL ? hp->body : spec); |
| 342 |
407438 |
if (!strncmp(spec, "req.http.", 9)) { |
| 343 |
16240 |
hh = hp->req; |
| 344 |
16240 |
hdr = spec + 9; |
| 345 |
407438 |
} else if (!strncmp(spec, "resp.http.", 10)) { |
| 346 |
123992 |
hh = hp->resp; |
| 347 |
123992 |
hdr = spec + 10; |
| 348 |
391198 |
} else if (!strcmp(spec, "h2.state")) { |
| 349 |
295 |
if (hp->h2) |
| 350 |
135 |
return ("true"); |
| 351 |
|
else |
| 352 |
160 |
return ("false"); |
| 353 |
|
} else |
| 354 |
266911 |
return (spec); |
| 355 |
140232 |
hdr = http_find_header(hh, hdr); |
| 356 |
140232 |
return (hdr); |
| 357 |
549190 |
} |
| 358 |
|
|
| 359 |
|
static void |
| 360 |
274599 |
cmd_http_expect(CMD_ARGS) |
| 361 |
|
{ |
| 362 |
|
struct http *hp; |
| 363 |
|
const char *lhs; |
| 364 |
|
char *cmp; |
| 365 |
|
const char *rhs; |
| 366 |
|
|
| 367 |
274599 |
(void)vl; |
| 368 |
274599 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 369 |
274599 |
AZ(strcmp(av[0], "expect")); |
| 370 |
274599 |
av++; |
| 371 |
|
|
| 372 |
274599 |
AN(av[0]); |
| 373 |
274599 |
AN(av[1]); |
| 374 |
274599 |
AN(av[2]); |
| 375 |
274599 |
AZ(av[3]); |
| 376 |
274599 |
lhs = cmd_var_resolve(hp, av[0]); |
| 377 |
274599 |
cmp = av[1]; |
| 378 |
274599 |
rhs = cmd_var_resolve(hp, av[2]); |
| 379 |
|
|
| 380 |
274599 |
vtc_expect(vl, av[0], lhs, cmp, av[2], rhs); |
| 381 |
274599 |
} |
| 382 |
|
|
| 383 |
|
/* SECTION: client-server.spec.expect_pattern |
| 384 |
|
* |
| 385 |
|
* expect_pattern |
| 386 |
|
* |
| 387 |
|
* Expect as the http body the test pattern generated by chunkedlen ('0'..'7' |
| 388 |
|
* repeating). |
| 389 |
|
*/ |
| 390 |
|
static void |
| 391 |
160 |
cmd_http_expect_pattern(CMD_ARGS) |
| 392 |
|
{ |
| 393 |
|
char *p; |
| 394 |
|
struct http *hp; |
| 395 |
160 |
char t = '0'; |
| 396 |
|
|
| 397 |
160 |
(void)vl; |
| 398 |
160 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 399 |
160 |
AZ(strcmp(av[0], "expect_pattern")); |
| 400 |
160 |
av++; |
| 401 |
160 |
AZ(av[0]); |
| 402 |
10485920 |
for (p = hp->body; *p != '\0'; p++) { |
| 403 |
10485760 |
if (*p != t) |
| 404 |
0 |
vtc_fatal(hp->vl, |
| 405 |
|
"EXPECT PATTERN FAIL @%zd should 0x%02x is 0x%02x", |
| 406 |
0 |
(ssize_t) (p - hp->body), t, *p); |
| 407 |
10485760 |
t += 1; |
| 408 |
10485760 |
t &= ~0x08; |
| 409 |
10485760 |
} |
| 410 |
160 |
vtc_log(hp->vl, 4, "EXPECT PATTERN SUCCESS"); |
| 411 |
160 |
} |
| 412 |
|
|
| 413 |
|
/********************************************************************** |
| 414 |
|
* Split a HTTP protocol header |
| 415 |
|
*/ |
| 416 |
|
|
| 417 |
|
static void |
| 418 |
227668 |
http_splitheader(struct http *hp, int req) |
| 419 |
|
{ |
| 420 |
|
char *p, *q, **hh; |
| 421 |
|
int n; |
| 422 |
|
char buf[20]; |
| 423 |
|
|
| 424 |
227668 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 425 |
227668 |
if (req) { |
| 426 |
94468 |
memset(hp->req, 0, sizeof hp->req); |
| 427 |
94468 |
hh = hp->req; |
| 428 |
94468 |
} else { |
| 429 |
133200 |
memset(hp->resp, 0, sizeof hp->resp); |
| 430 |
133200 |
hh = hp->resp; |
| 431 |
|
} |
| 432 |
|
|
| 433 |
227668 |
n = 0; |
| 434 |
227668 |
p = hp->rx_b; |
| 435 |
227668 |
if (*p == '\0') { |
| 436 |
3200 |
vtc_log(hp->vl, 4, "No headers"); |
| 437 |
3200 |
return; |
| 438 |
|
} |
| 439 |
|
|
| 440 |
|
/* REQ/PROTO */ |
| 441 |
224508 |
while (vct_islws(*p)) |
| 442 |
40 |
p++; |
| 443 |
224468 |
hh[n++] = p; |
| 444 |
1556037 |
while (!vct_islws(*p)) |
| 445 |
1331569 |
p++; |
| 446 |
224468 |
AZ(vct_iscrlf(p, hp->rx_e)); |
| 447 |
224468 |
*p++ = '\0'; |
| 448 |
|
|
| 449 |
|
/* URL/STATUS */ |
| 450 |
224468 |
while (vct_issp(*p)) /* XXX: H space only */ |
| 451 |
0 |
p++; |
| 452 |
224468 |
AZ(vct_iscrlf(p, hp->rx_e)); |
| 453 |
224468 |
hh[n++] = p; |
| 454 |
1097504 |
while (!vct_islws(*p)) |
| 455 |
873036 |
p++; |
| 456 |
224468 |
if (vct_iscrlf(p, hp->rx_e)) { |
| 457 |
40 |
hh[n++] = NULL; |
| 458 |
40 |
q = p; |
| 459 |
40 |
p = vct_skipcrlf(p, hp->rx_e); |
| 460 |
40 |
*q = '\0'; |
| 461 |
40 |
} else { |
| 462 |
224428 |
*p++ = '\0'; |
| 463 |
|
/* PROTO/MSG */ |
| 464 |
224428 |
while (vct_issp(*p)) /* XXX: H space only */ |
| 465 |
0 |
p++; |
| 466 |
224428 |
hh[n++] = p; |
| 467 |
1738290 |
while (!vct_iscrlf(p, hp->rx_e)) |
| 468 |
1513862 |
p++; |
| 469 |
224428 |
q = p; |
| 470 |
224428 |
p = vct_skipcrlf(p, hp->rx_e); |
| 471 |
224428 |
*q = '\0'; |
| 472 |
|
} |
| 473 |
224468 |
assert(n == 3); |
| 474 |
|
|
| 475 |
1832212 |
while (*p != '\0') { |
| 476 |
1832224 |
assert(n < MAX_HDR); |
| 477 |
1832224 |
if (vct_iscrlf(p, hp->rx_e)) |
| 478 |
224464 |
break; |
| 479 |
1607760 |
hh[n++] = p++; |
| 480 |
35069631 |
while (*p != '\0' && !vct_iscrlf(p, hp->rx_e)) |
| 481 |
33461871 |
p++; |
| 482 |
1607744 |
if (*p == '\0') { |
| 483 |
0 |
break; |
| 484 |
|
} |
| 485 |
1607744 |
q = p; |
| 486 |
1607744 |
p = vct_skipcrlf(p, hp->rx_e); |
| 487 |
1607744 |
*q = '\0'; |
| 488 |
|
} |
| 489 |
224488 |
p = vct_skipcrlf(p, hp->rx_e); |
| 490 |
224488 |
assert(*p == '\0'); |
| 491 |
|
|
| 492 |
2505822 |
for (n = 0; n < 3 || hh[n] != NULL; n++) { |
| 493 |
2281334 |
bprintf(buf, "http[%2d] ", n); |
| 494 |
2281334 |
vtc_dump(hp->vl, 4, buf, hh[n], -1); |
| 495 |
2281334 |
} |
| 496 |
227688 |
} |
| 497 |
|
|
| 498 |
|
|
| 499 |
|
/********************************************************************** |
| 500 |
|
* Receive another character |
| 501 |
|
*/ |
| 502 |
|
|
| 503 |
|
static int |
| 504 |
51713457 |
http_rxchar(struct http *hp, int n, int eof) |
| 505 |
|
{ |
| 506 |
|
int i; |
| 507 |
|
struct pollfd pfd[1]; |
| 508 |
|
|
| 509 |
103677593 |
while (n > 0) { |
| 510 |
51968337 |
pfd[0].fd = hp->sess->fd; |
| 511 |
51968337 |
pfd[0].events = POLLIN; |
| 512 |
51968337 |
pfd[0].revents = 0; |
| 513 |
51968337 |
i = poll(pfd, 1, (int)(hp->timeout * 1000)); |
| 514 |
51968337 |
if (i < 0 && errno == EINTR) |
| 515 |
0 |
continue; |
| 516 |
51968337 |
if (i == 0) { |
| 517 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 518 |
|
"HTTP rx timeout (fd:%d %.3fs)", |
| 519 |
0 |
hp->sess->fd, hp->timeout); |
| 520 |
0 |
continue; |
| 521 |
|
} |
| 522 |
51968337 |
if (i < 0) { |
| 523 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 524 |
|
"HTTP rx failed (fd:%d poll: %s)", |
| 525 |
0 |
hp->sess->fd, strerror(errno)); |
| 526 |
0 |
continue; |
| 527 |
|
} |
| 528 |
51968337 |
assert(i > 0); |
| 529 |
51968337 |
assert(hp->rx_p + n < hp->rx_e); |
| 530 |
51968337 |
i = read(hp->sess->fd, hp->rx_p, n); |
| 531 |
51968337 |
if (!(pfd[0].revents & POLLIN)) |
| 532 |
0 |
vtc_log(hp->vl, 4, |
| 533 |
|
"HTTP rx poll (fd:%d revents: %x n=%d, i=%d)", |
| 534 |
0 |
hp->sess->fd, pfd[0].revents, n, i); |
| 535 |
51968337 |
if (i == 0 && eof) |
| 536 |
3400 |
return (i); |
| 537 |
51964937 |
if (i == 0) { |
| 538 |
1602 |
vtc_log(hp->vl, hp->fatal, |
| 539 |
|
"HTTP rx EOF (fd:%d read: %s) %d", |
| 540 |
801 |
hp->sess->fd, strerror(errno), n); |
| 541 |
801 |
return (-1); |
| 542 |
|
} |
| 543 |
51964136 |
if (i < 0) { |
| 544 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 545 |
|
"HTTP rx failed (fd:%d read: %s)", |
| 546 |
0 |
hp->sess->fd, strerror(errno)); |
| 547 |
0 |
return (-1); |
| 548 |
|
} |
| 549 |
51964136 |
hp->rx_p += i; |
| 550 |
51964136 |
*hp->rx_p = '\0'; |
| 551 |
51964136 |
n -= i; |
| 552 |
|
} |
| 553 |
51709256 |
return (1); |
| 554 |
51713457 |
} |
| 555 |
|
|
| 556 |
|
static int |
| 557 |
55053 |
http_rxchunk(struct http *hp) |
| 558 |
|
{ |
| 559 |
|
char *q, *old; |
| 560 |
|
int i; |
| 561 |
|
|
| 562 |
55053 |
old = hp->rx_p; |
| 563 |
55053 |
do { |
| 564 |
277010 |
if (http_rxchar(hp, 1, 0) < 0) |
| 565 |
410 |
return (-1); |
| 566 |
276600 |
} while (hp->rx_p[-1] != '\n'); |
| 567 |
54643 |
vtc_dump(hp->vl, 4, "len", old, -1); |
| 568 |
54643 |
i = strtoul(old, &q, 16); |
| 569 |
54643 |
bprintf(hp->chunklen, "%d", i); |
| 570 |
54643 |
if ((q == old) || (q == hp->rx_p) || (*q != '\0' && !vct_islws(*q))) { |
| 571 |
4 |
vtc_log(hp->vl, hp->fatal, "Chunklen fail (%02x @ %td)", |
| 572 |
2 |
(*q & 0xff), q - old); |
| 573 |
2 |
return (-1); |
| 574 |
|
} |
| 575 |
54643 |
assert(*q == '\0' || vct_islws(*q)); |
| 576 |
54643 |
hp->rx_p = old; |
| 577 |
54643 |
if (i > 0) { |
| 578 |
43572 |
if (http_rxchar(hp, i, 0) < 0) |
| 579 |
70 |
return (-1); |
| 580 |
43502 |
vtc_dump(hp->vl, 4, "chunk", old, i); |
| 581 |
43502 |
} |
| 582 |
54573 |
old = hp->rx_p; |
| 583 |
54573 |
if (http_rxchar(hp, 2, 0) < 0) |
| 584 |
0 |
return (-1); |
| 585 |
54573 |
if (!vct_iscrlf(old, hp->rx_e)) { |
| 586 |
0 |
vtc_log(hp->vl, hp->fatal, "Chunklen without CRLF"); |
| 587 |
0 |
return (-1); |
| 588 |
|
} |
| 589 |
54573 |
hp->rx_p = old; |
| 590 |
54573 |
*hp->rx_p = '\0'; |
| 591 |
54573 |
return (i); |
| 592 |
55053 |
} |
| 593 |
|
|
| 594 |
|
/********************************************************************** |
| 595 |
|
* Swallow a HTTP message body |
| 596 |
|
* |
| 597 |
|
* max: 0 is all |
| 598 |
|
*/ |
| 599 |
|
|
| 600 |
|
static void |
| 601 |
220666 |
http_swallow_body(struct http *hp, char * const *hh, int body, int max) |
| 602 |
|
{ |
| 603 |
|
const char *p, *q; |
| 604 |
|
int i, l, ll; |
| 605 |
|
|
| 606 |
220666 |
l = hp->rx_p - hp->body; |
| 607 |
|
|
| 608 |
220666 |
p = http_find_header(hh, "transfer-encoding"); |
| 609 |
220666 |
q = http_find_header(hh, "content-length"); |
| 610 |
220666 |
if (p != NULL && !strcasecmp(p, "chunked")) { |
| 611 |
11669 |
if (q != NULL) { |
| 612 |
0 |
vtc_log(hp->vl, hp->fatal, "Both C-E: Chunked and C-L"); |
| 613 |
0 |
return; |
| 614 |
|
} |
| 615 |
11669 |
ll = 0; |
| 616 |
54531 |
while (http_rxchunk(hp) > 0) { |
| 617 |
43102 |
ll = (hp->rx_p - hp->body) - l; |
| 618 |
43102 |
if (max && ll >= max) |
| 619 |
240 |
break; |
| 620 |
|
} |
| 621 |
11669 |
p = "chunked"; |
| 622 |
220666 |
} else if (q != NULL) { |
| 623 |
111327 |
ll = strtoul(q, NULL, 10); |
| 624 |
111327 |
if (max && ll > l + max) |
| 625 |
160 |
ll = max; |
| 626 |
|
else |
| 627 |
111167 |
ll -= l; |
| 628 |
111327 |
i = http_rxchar(hp, ll, 0); |
| 629 |
111327 |
if (i < 0) |
| 630 |
321 |
return; |
| 631 |
111006 |
p = "c-l"; |
| 632 |
208676 |
} else if (body) { |
| 633 |
280 |
ll = 0; |
| 634 |
280 |
do { |
| 635 |
7869000 |
i = http_rxchar(hp, 1, 1); |
| 636 |
7869000 |
if (i < 0) |
| 637 |
0 |
return; |
| 638 |
7869000 |
ll += i; |
| 639 |
7869000 |
if (max && ll >= max) |
| 640 |
80 |
break; |
| 641 |
7868920 |
} while (i > 0); |
| 642 |
280 |
p = "eof"; |
| 643 |
280 |
} else { |
| 644 |
97390 |
p = "none"; |
| 645 |
97390 |
ll = l = 0; |
| 646 |
|
} |
| 647 |
220345 |
vtc_dump(hp->vl, 4, p, hp->body + l, ll); |
| 648 |
220345 |
l += ll; |
| 649 |
220345 |
hp->bodyl = l; |
| 650 |
220345 |
bprintf(hp->bodylen, "%d", l); |
| 651 |
220666 |
} |
| 652 |
|
|
| 653 |
|
/********************************************************************** |
| 654 |
|
* Receive a HTTP protocol header |
| 655 |
|
*/ |
| 656 |
|
|
| 657 |
|
static void |
| 658 |
227669 |
http_rxhdr(struct http *hp) |
| 659 |
|
{ |
| 660 |
227669 |
int i, s = 0; |
| 661 |
|
char *p; |
| 662 |
|
ssize_t l; |
| 663 |
|
|
| 664 |
227669 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 665 |
227669 |
hp->rx_p = hp->rx_b; |
| 666 |
227669 |
*hp->rx_p = '\0'; |
| 667 |
227669 |
hp->body = NULL; |
| 668 |
227669 |
bprintf(hp->bodylen, "%s", "<undef>"); |
| 669 |
43342936 |
while (1) { |
| 670 |
43342936 |
p = hp->rx_p; |
| 671 |
43342936 |
i = http_rxchar(hp, 1, 1); |
| 672 |
43342936 |
if (i < 1) |
| 673 |
3200 |
break; |
| 674 |
43339736 |
if (s == 0 && *p == '\r') |
| 675 |
1832246 |
s = 1; |
| 676 |
41507490 |
else if ((s == 0 || s == 1) && *p == '\n') |
| 677 |
1832409 |
s = 2; |
| 678 |
39675081 |
else if (s == 2 && *p == '\r') |
| 679 |
224428 |
s = 3; |
| 680 |
39450653 |
else if ((s == 2 || s == 3) && *p == '\n') |
| 681 |
224469 |
break; |
| 682 |
|
else |
| 683 |
39226184 |
s = 0; |
| 684 |
|
} |
| 685 |
227669 |
l = hp->rx_p - hp->rx_b; |
| 686 |
227669 |
vtc_dump(hp->vl, 4, "rxhdr", hp->rx_b, l); |
| 687 |
227669 |
vtc_log(hp->vl, 4, "rxhdrlen = %zd", l); |
| 688 |
227669 |
if (i < 1) |
| 689 |
3200 |
vtc_log(hp->vl, hp->fatal, "HTTP header is incomplete"); |
| 690 |
227669 |
*hp->rx_p = '\0'; |
| 691 |
227669 |
hp->body = hp->rx_p; |
| 692 |
227669 |
} |
| 693 |
|
|
| 694 |
|
/* SECTION: client-server.spec.rxresp |
| 695 |
|
* |
| 696 |
|
* rxresp [-no_obj] (client only) |
| 697 |
|
* Receive and parse a response's headers and body. If -no_obj is |
| 698 |
|
* present, only get the headers. |
| 699 |
|
*/ |
| 700 |
|
|
| 701 |
|
static void |
| 702 |
131183 |
cmd_http_rxresp(CMD_ARGS) |
| 703 |
|
{ |
| 704 |
|
struct http *hp; |
| 705 |
131183 |
int has_obj = 1; |
| 706 |
|
|
| 707 |
131183 |
(void)vl; |
| 708 |
131183 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 709 |
131183 |
ONLY_CLIENT(hp, av); |
| 710 |
131183 |
AZ(strcmp(av[0], "rxresp")); |
| 711 |
131183 |
av++; |
| 712 |
|
|
| 713 |
132223 |
for (; *av != NULL; av++) |
| 714 |
2080 |
if (!strcmp(*av, "-no_obj")) |
| 715 |
1040 |
has_obj = 0; |
| 716 |
|
else |
| 717 |
0 |
vtc_fatal(hp->vl, |
| 718 |
0 |
"Unknown http rxresp spec: %s\n", *av); |
| 719 |
131183 |
http_rxhdr(hp); |
| 720 |
131183 |
http_splitheader(hp, 0); |
| 721 |
131183 |
if (http_count_header(hp->resp, "Content-Length") > 1) |
| 722 |
0 |
vtc_fatal(hp->vl, |
| 723 |
|
"Multiple Content-Length headers.\n"); |
| 724 |
131183 |
if (!has_obj) |
| 725 |
1040 |
return; |
| 726 |
130143 |
if (!hp->resp[0] || !hp->resp[1]) |
| 727 |
2520 |
return; |
| 728 |
127639 |
if (hp->head_method) |
| 729 |
2800 |
return; |
| 730 |
124839 |
if (!strcmp(hp->resp[1], "200")) |
| 731 |
94319 |
http_swallow_body(hp, hp->resp, 1, 0); |
| 732 |
|
else |
| 733 |
30520 |
http_swallow_body(hp, hp->resp, 0, 0); |
| 734 |
124839 |
vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen); |
| 735 |
131199 |
} |
| 736 |
|
|
| 737 |
|
/* SECTION: client-server.spec.rxresphdrs |
| 738 |
|
* |
| 739 |
|
* rxresphdrs (client only) |
| 740 |
|
* Receive and parse a response's headers. |
| 741 |
|
*/ |
| 742 |
|
|
| 743 |
|
static void |
| 744 |
2000 |
cmd_http_rxresphdrs(CMD_ARGS) |
| 745 |
|
{ |
| 746 |
|
struct http *hp; |
| 747 |
|
|
| 748 |
2000 |
(void)vl; |
| 749 |
2000 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 750 |
2000 |
ONLY_CLIENT(hp, av); |
| 751 |
2000 |
AZ(strcmp(av[0], "rxresphdrs")); |
| 752 |
2000 |
av++; |
| 753 |
|
|
| 754 |
2000 |
for (; *av != NULL; av++) |
| 755 |
0 |
vtc_fatal(hp->vl, "Unknown http rxresp spec: %s\n", *av); |
| 756 |
2000 |
http_rxhdr(hp); |
| 757 |
2000 |
http_splitheader(hp, 0); |
| 758 |
2000 |
if (http_count_header(hp->resp, "Content-Length") > 1) |
| 759 |
0 |
vtc_fatal(hp->vl, |
| 760 |
|
"Multiple Content-Length headers.\n"); |
| 761 |
2000 |
} |
| 762 |
|
|
| 763 |
|
/* SECTION: client-server.spec.gunzip |
| 764 |
|
* |
| 765 |
|
* gunzip |
| 766 |
|
* Gunzip the body in place. |
| 767 |
|
*/ |
| 768 |
|
static void |
| 769 |
1480 |
cmd_http_gunzip(CMD_ARGS) |
| 770 |
|
{ |
| 771 |
|
struct http *hp; |
| 772 |
|
|
| 773 |
1480 |
(void)av; |
| 774 |
1480 |
(void)vl; |
| 775 |
|
|
| 776 |
1480 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 777 |
1480 |
vtc_gunzip(hp, hp->body, &hp->bodyl); |
| 778 |
1480 |
} |
| 779 |
|
|
| 780 |
|
/********************************************************************** |
| 781 |
|
* Handle common arguments of a transmitted request or response |
| 782 |
|
*/ |
| 783 |
|
|
| 784 |
|
static char* const * |
| 785 |
219608 |
http_tx_parse_args(char * const *av, struct vtclog *vl, struct http *hp, |
| 786 |
|
char *body, unsigned nohost, unsigned nodate, unsigned noserver, unsigned nouseragent) |
| 787 |
|
{ |
| 788 |
219608 |
long bodylen = 0; |
| 789 |
|
char *b, *c; |
| 790 |
|
char *nullbody; |
| 791 |
|
ssize_t len; |
| 792 |
219608 |
int nolen = 0; |
| 793 |
|
int l; |
| 794 |
|
|
| 795 |
219608 |
nullbody = body; |
| 796 |
|
|
| 797 |
312258 |
for (; *av != NULL; av++) { |
| 798 |
138649 |
if (!strcmp(*av, "-nolen")) { |
| 799 |
5480 |
nolen = 1; |
| 800 |
138649 |
} else if (!strcmp(*av, "-nohost")) { |
| 801 |
80 |
nohost = 1; |
| 802 |
133169 |
} else if (!strcmp(*av, "-nodate")) { |
| 803 |
320 |
nodate = 1; |
| 804 |
133089 |
} else if (!strcmp(*av, "-hdr")) { |
| 805 |
86610 |
if (!strncasecmp(av[1], "Content-Length:", 15) || |
| 806 |
83970 |
!strncasecmp(av[1], "Transfer-Encoding:", 18)) |
| 807 |
6320 |
nolen = 1; |
| 808 |
86610 |
if (!strncasecmp(av[1], "Host:", 5)) |
| 809 |
1440 |
nohost = 1; |
| 810 |
86610 |
if (!strncasecmp(av[1], "Date:", 5)) |
| 811 |
320 |
nodate = 1; |
| 812 |
86610 |
if (!strncasecmp(av[1], "Server:", 7)) |
| 813 |
600 |
noserver = 1; |
| 814 |
86610 |
if (!strncasecmp(av[1], "User-Agent:", 11)) |
| 815 |
80 |
nouseragent = 1; |
| 816 |
86610 |
VSB_printf(hp->vsb, "%s%s", av[1], nl); |
| 817 |
86610 |
av++; |
| 818 |
132769 |
} else if (!strcmp(*av, "-hdrlen")) { |
| 819 |
160 |
VSB_printf(hp->vsb, "%s: ", av[1]); |
| 820 |
160 |
l = atoi(av[2]); |
| 821 |
30160 |
while (l-- > 0) |
| 822 |
30000 |
VSB_putc(hp->vsb, '0' + (l % 10)); |
| 823 |
160 |
VSB_printf(hp->vsb, "%s", nl); |
| 824 |
160 |
av+=2; |
| 825 |
160 |
} else |
| 826 |
45999 |
break; |
| 827 |
92650 |
} |
| 828 |
266288 |
for (; *av != NULL; av++) { |
| 829 |
46680 |
if (!strcmp(*av, "-body")) { |
| 830 |
30240 |
assert(body == nullbody); |
| 831 |
30240 |
REPLACE(body, av[1]); |
| 832 |
|
|
| 833 |
30240 |
AN(body); |
| 834 |
30240 |
av++; |
| 835 |
30240 |
bodylen = strlen(body); |
| 836 |
928752 |
for (b = body; *b != '\0'; b++) { |
| 837 |
898512 |
if (*b == '\\' && b[1] == '0') { |
| 838 |
160 |
*b = '\0'; |
| 839 |
1640 |
for (c = b+1; *c != '\0'; c++) { |
| 840 |
1480 |
*c = c[1]; |
| 841 |
1480 |
} |
| 842 |
160 |
b++; |
| 843 |
160 |
bodylen--; |
| 844 |
160 |
} |
| 845 |
898512 |
} |
| 846 |
46680 |
} else if (!strcmp(*av, "-bodyfrom")) { |
| 847 |
80 |
assert(body == nullbody); |
| 848 |
80 |
free(body); |
| 849 |
80 |
body = VFIL_readfile(NULL, av[1], &len); |
| 850 |
80 |
AN(body); |
| 851 |
80 |
assert(len < INT_MAX); |
| 852 |
80 |
bodylen = len; |
| 853 |
80 |
av++; |
| 854 |
16440 |
} else if (!strcmp(*av, "-bodylen")) { |
| 855 |
9120 |
assert(body == nullbody); |
| 856 |
9120 |
free(body); |
| 857 |
9120 |
body = synth_body(av[1], 0); |
| 858 |
9120 |
bodylen = strlen(body); |
| 859 |
9120 |
av++; |
| 860 |
16360 |
} else if (!strncmp(*av, "-gzip", 5)) { |
| 861 |
7240 |
l = vtc_gzip_cmd(hp, av, &body, &bodylen); |
| 862 |
7240 |
if (l == 0) |
| 863 |
0 |
break; |
| 864 |
7240 |
av++; |
| 865 |
7240 |
if (l > 1) |
| 866 |
6560 |
VSB_printf(hp->vsb, "Content-Encoding: gzip%s", nl); |
| 867 |
7240 |
} else |
| 868 |
0 |
break; |
| 869 |
46680 |
} |
| 870 |
219608 |
if (!nohost) { |
| 871 |
128835 |
VSB_cat(hp->vsb, "Host: "); |
| 872 |
128835 |
macro_cat(vl, hp->vsb, "localhost", NULL); |
| 873 |
128835 |
VSB_cat(hp->vsb, nl); |
| 874 |
128835 |
} |
| 875 |
219608 |
if (!nodate) { |
| 876 |
87411 |
VSB_cat(hp->vsb, "Date: "); |
| 877 |
87411 |
macro_cat(vl, hp->vsb, "date", NULL); |
| 878 |
87411 |
VSB_cat(hp->vsb, nl); |
| 879 |
87411 |
} |
| 880 |
219608 |
if (!noserver) |
| 881 |
86894 |
VSB_printf(hp->vsb, "Server: %s%s", hp->sess->name, nl); |
| 882 |
219608 |
if (!nouseragent) |
| 883 |
131314 |
VSB_printf(hp->vsb, "User-Agent: %s%s", hp->sess->name, nl); |
| 884 |
219608 |
if (body != NULL && !nolen) |
| 885 |
85214 |
VSB_printf(hp->vsb, "Content-Length: %ld%s", bodylen, nl); |
| 886 |
219608 |
VSB_cat(hp->vsb, nl); |
| 887 |
219608 |
if (body != NULL) { |
| 888 |
90573 |
VSB_bcat(hp->vsb, body, bodylen); |
| 889 |
90573 |
free(body); |
| 890 |
90573 |
} |
| 891 |
219608 |
return (av); |
| 892 |
|
} |
| 893 |
|
|
| 894 |
|
/* SECTION: client-server.spec.txreq |
| 895 |
|
* |
| 896 |
|
* txreq|txresp [...] |
| 897 |
|
* Send a minimal request or response, but overload it if necessary. |
| 898 |
|
* |
| 899 |
|
* txreq is client-specific and txresp is server-specific. |
| 900 |
|
* |
| 901 |
|
* The only thing different between a request and a response, apart |
| 902 |
|
* from who can send them is that the first line (request line vs |
| 903 |
|
* status line), so all the options are pretty much the same. |
| 904 |
|
* |
| 905 |
|
* \-method STRING (txreq only) |
| 906 |
|
* What method to use (default: "GET"). |
| 907 |
|
* |
| 908 |
|
* \-req STRING (txreq only) |
| 909 |
|
* Alias for -method. |
| 910 |
|
* |
| 911 |
|
* \-url STRING (txreq only) |
| 912 |
|
* What location to use (default "/"). |
| 913 |
|
* |
| 914 |
|
* \-proto STRING |
| 915 |
|
* What protocol use in the status line. |
| 916 |
|
* (default: "HTTP/1.1"). |
| 917 |
|
* |
| 918 |
|
* \-status NUMBER (txresp only) |
| 919 |
|
* What status code to return (default 200). |
| 920 |
|
* |
| 921 |
|
* \-reason STRING (txresp only) |
| 922 |
|
* What message to put in the status line (default: "OK"). |
| 923 |
|
* |
| 924 |
|
* \-noserver (txresp only) |
| 925 |
|
* Don't include a Server header with the id of the server. |
| 926 |
|
* |
| 927 |
|
* \-nouseragent (txreq only) |
| 928 |
|
* Don't include a User-Agent header with the id of the client. |
| 929 |
|
* |
| 930 |
|
* These three switches can appear in any order but must come before the |
| 931 |
|
* following ones. |
| 932 |
|
* |
| 933 |
|
* \-nohost |
| 934 |
|
* Don't include a Host header in the request. Also Implied |
| 935 |
|
* by the addition of a Host header with ``-hdr``. |
| 936 |
|
* |
| 937 |
|
* \-nolen |
| 938 |
|
* Don't include a Content-Length header. Also implied by the |
| 939 |
|
* addition of a Content-Length or Transfer-Encoding header |
| 940 |
|
* with ``-hdr``. |
| 941 |
|
* |
| 942 |
|
* \-nodate |
| 943 |
|
* Don't include a Date header in the response. Also implied |
| 944 |
|
* by the addition of a Date header with ``-hdr``. |
| 945 |
|
* |
| 946 |
|
* \-hdr STRING |
| 947 |
|
* Add STRING as a header, it must follow this format: |
| 948 |
|
* "name: value". It can be called multiple times. |
| 949 |
|
* |
| 950 |
|
* \-hdrlen STRING NUMBER |
| 951 |
|
* Add STRING as a header with NUMBER bytes of content. |
| 952 |
|
* |
| 953 |
|
* You can then use the arguments related to the body: |
| 954 |
|
* |
| 955 |
|
* \-body STRING |
| 956 |
|
* Input STRING as body. |
| 957 |
|
* |
| 958 |
|
* \-bodyfrom FILE |
| 959 |
|
* Same as -body but content is read from FILE. |
| 960 |
|
* |
| 961 |
|
* \-bodylen NUMBER |
| 962 |
|
* Generate and input a body that is NUMBER bytes-long. |
| 963 |
|
* |
| 964 |
|
* \-gziplevel NUMBER |
| 965 |
|
* Set the gzip level (call it before any of the other gzip |
| 966 |
|
* switches). |
| 967 |
|
* |
| 968 |
|
* \-gzipresidual NUMBER |
| 969 |
|
* Add extra gzip bits. You should never need it. |
| 970 |
|
* |
| 971 |
|
* \-gzipbody STRING |
| 972 |
|
* Gzip STRING and send it as body. |
| 973 |
|
* |
| 974 |
|
* \-gziplen NUMBER |
| 975 |
|
* Combine -bodylen and -gzipbody: generate a string of length |
| 976 |
|
* NUMBER, gzip it and send as body. |
| 977 |
|
*/ |
| 978 |
|
|
| 979 |
|
/********************************************************************** |
| 980 |
|
* Transmit a response |
| 981 |
|
*/ |
| 982 |
|
|
| 983 |
|
static void |
| 984 |
88051 |
cmd_http_txresp(CMD_ARGS) |
| 985 |
|
{ |
| 986 |
|
struct http *hp; |
| 987 |
88051 |
const char *proto = "HTTP/1.1"; |
| 988 |
88051 |
const char *status = "200"; |
| 989 |
88051 |
const char *reason = "OK"; |
| 990 |
88051 |
char* body = NULL; |
| 991 |
88051 |
unsigned noserver = 0; |
| 992 |
|
|
| 993 |
88051 |
(void)vl; |
| 994 |
88051 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 995 |
88051 |
ONLY_SERVER(hp, av); |
| 996 |
88051 |
AZ(strcmp(av[0], "txresp")); |
| 997 |
88051 |
av++; |
| 998 |
|
|
| 999 |
88051 |
VSB_clear(hp->vsb); |
| 1000 |
|
|
| 1001 |
96891 |
for (; *av != NULL; av++) { |
| 1002 |
66880 |
if (!strcmp(*av, "-proto")) { |
| 1003 |
1160 |
proto = av[1]; |
| 1004 |
1160 |
av++; |
| 1005 |
66880 |
} else if (!strcmp(*av, "-status")) { |
| 1006 |
6480 |
status = av[1]; |
| 1007 |
6480 |
av++; |
| 1008 |
65720 |
} else if (!strcmp(*av, "-reason")) { |
| 1009 |
640 |
reason = av[1]; |
| 1010 |
640 |
av++; |
| 1011 |
640 |
continue; |
| 1012 |
58600 |
} else if (!strcmp(*av, "-noserver")) { |
| 1013 |
560 |
noserver = 1; |
| 1014 |
560 |
continue; |
| 1015 |
|
} else |
| 1016 |
58040 |
break; |
| 1017 |
7640 |
} |
| 1018 |
|
|
| 1019 |
88051 |
VSB_printf(hp->vsb, "%s %s %s%s", proto, status, reason, nl); |
| 1020 |
|
|
| 1021 |
|
/* send a "Content-Length: 0" header unless something else happens */ |
| 1022 |
88051 |
REPLACE(body, ""); |
| 1023 |
|
|
| 1024 |
88051 |
av = http_tx_parse_args(av, vl, hp, body, 1, 0, noserver, 1); |
| 1025 |
88051 |
if (*av != NULL) |
| 1026 |
0 |
vtc_fatal(hp->vl, "Unknown http txresp spec: %s\n", *av); |
| 1027 |
|
|
| 1028 |
88051 |
http_write(hp, 4, "txresp"); |
| 1029 |
88051 |
} |
| 1030 |
|
|
| 1031 |
|
static void |
| 1032 |
80 |
cmd_http_upgrade(CMD_ARGS) |
| 1033 |
|
{ |
| 1034 |
|
char *h; |
| 1035 |
|
struct http *hp; |
| 1036 |
|
|
| 1037 |
80 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1038 |
80 |
ONLY_SERVER(hp, av); |
| 1039 |
80 |
AN(hp->sfd); |
| 1040 |
|
|
| 1041 |
80 |
h = http_find_header(hp->req, "Upgrade"); |
| 1042 |
80 |
if (!h || strcmp(h, "h2c")) |
| 1043 |
0 |
vtc_fatal(vl, "Req misses \"Upgrade: h2c\" header"); |
| 1044 |
|
|
| 1045 |
80 |
h = http_find_header(hp->req, "Connection"); |
| 1046 |
80 |
if (!h || strcmp(h, "Upgrade, HTTP2-Settings")) |
| 1047 |
0 |
vtc_fatal(vl, "Req misses \"Connection: " |
| 1048 |
|
"Upgrade, HTTP2-Settings\" header"); |
| 1049 |
|
|
| 1050 |
80 |
h = http_find_header(hp->req, "HTTP2-Settings"); |
| 1051 |
80 |
if (!h) |
| 1052 |
0 |
vtc_fatal(vl, "Req misses \"HTTP2-Settings\" header"); |
| 1053 |
|
|
| 1054 |
80 |
parse_string(vl, hp, |
| 1055 |
|
"txresp -status 101" |
| 1056 |
|
" -hdr \"Connection: Upgrade\"" |
| 1057 |
|
" -hdr \"Upgrade: h2c\"\n" |
| 1058 |
|
); |
| 1059 |
|
|
| 1060 |
80 |
parse_string(vl, hp, |
| 1061 |
|
"rxpri\n" |
| 1062 |
|
"stream 0 {\n" |
| 1063 |
|
" txsettings\n" |
| 1064 |
|
" rxsettings\n" |
| 1065 |
|
" txsettings -ack\n" |
| 1066 |
|
" rxsettings\n" |
| 1067 |
|
" expect settings.ack == true\n" |
| 1068 |
|
"} -start\n" |
| 1069 |
|
); |
| 1070 |
|
|
| 1071 |
80 |
b64_settings(hp, h); |
| 1072 |
|
|
| 1073 |
80 |
} |
| 1074 |
|
|
| 1075 |
|
/********************************************************************** |
| 1076 |
|
* Receive a request |
| 1077 |
|
*/ |
| 1078 |
|
|
| 1079 |
|
/* SECTION: client-server.spec.rxreq |
| 1080 |
|
* |
| 1081 |
|
* rxreq (server only) |
| 1082 |
|
* Receive and parse a request's headers and body. |
| 1083 |
|
*/ |
| 1084 |
|
static void |
| 1085 |
94984 |
cmd_http_rxreq(CMD_ARGS) |
| 1086 |
|
{ |
| 1087 |
|
struct http *hp; |
| 1088 |
|
|
| 1089 |
94984 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1090 |
94984 |
ONLY_SERVER(hp, av); |
| 1091 |
94984 |
AZ(strcmp(av[0], "rxreq")); |
| 1092 |
94984 |
av++; |
| 1093 |
|
|
| 1094 |
94984 |
for (; *av != NULL; av++) |
| 1095 |
0 |
vtc_fatal(vl, "Unknown http rxreq spec: %s\n", *av); |
| 1096 |
94984 |
http_rxhdr(hp); |
| 1097 |
94984 |
http_splitheader(hp, 1); |
| 1098 |
94984 |
if (http_count_header(hp->req, "Content-Length") > 1) |
| 1099 |
0 |
vtc_fatal(vl, "Multiple Content-Length headers.\n"); |
| 1100 |
94984 |
http_swallow_body(hp, hp->req, 0, 0); |
| 1101 |
94984 |
vtc_log(vl, 4, "bodylen = %s", hp->bodylen); |
| 1102 |
94984 |
} |
| 1103 |
|
|
| 1104 |
|
/* SECTION: client-server.spec.rxreqhdrs |
| 1105 |
|
* |
| 1106 |
|
* rxreqhdrs (server only) |
| 1107 |
|
* Receive and parse a request's headers (but not the body). |
| 1108 |
|
*/ |
| 1109 |
|
|
| 1110 |
|
static void |
| 1111 |
200 |
cmd_http_rxreqhdrs(CMD_ARGS) |
| 1112 |
|
{ |
| 1113 |
|
struct http *hp; |
| 1114 |
|
|
| 1115 |
200 |
(void)vl; |
| 1116 |
200 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1117 |
200 |
AZ(strcmp(av[0], "rxreqhdrs")); |
| 1118 |
200 |
av++; |
| 1119 |
|
|
| 1120 |
200 |
for (; *av != NULL; av++) |
| 1121 |
0 |
vtc_fatal(hp->vl, "Unknown http rxreq spec: %s\n", *av); |
| 1122 |
200 |
http_rxhdr(hp); |
| 1123 |
200 |
http_splitheader(hp, 1); |
| 1124 |
200 |
if (http_count_header(hp->req, "Content-Length") > 1) |
| 1125 |
0 |
vtc_fatal(hp->vl, "Multiple Content-Length headers.\n"); |
| 1126 |
200 |
} |
| 1127 |
|
|
| 1128 |
|
/* SECTION: client-server.spec.rxreqbody |
| 1129 |
|
* |
| 1130 |
|
* rxreqbody (server only) |
| 1131 |
|
* Receive a request's body. |
| 1132 |
|
*/ |
| 1133 |
|
|
| 1134 |
|
static void |
| 1135 |
160 |
cmd_http_rxreqbody(CMD_ARGS) |
| 1136 |
|
{ |
| 1137 |
|
struct http *hp; |
| 1138 |
|
|
| 1139 |
160 |
(void)vl; |
| 1140 |
160 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1141 |
160 |
ONLY_SERVER(hp, av); |
| 1142 |
160 |
AZ(strcmp(av[0], "rxreqbody")); |
| 1143 |
160 |
av++; |
| 1144 |
|
|
| 1145 |
160 |
for (; *av != NULL; av++) |
| 1146 |
0 |
vtc_fatal(hp->vl, "Unknown http rxreq spec: %s\n", *av); |
| 1147 |
160 |
http_swallow_body(hp, hp->req, 0, 0); |
| 1148 |
160 |
vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen); |
| 1149 |
160 |
} |
| 1150 |
|
|
| 1151 |
|
/* SECTION: client-server.spec.rxrespbody |
| 1152 |
|
* |
| 1153 |
|
* rxrespbody (client only) |
| 1154 |
|
* Receive (part of) a response's body. |
| 1155 |
|
* |
| 1156 |
|
* -max : max length of this receive, 0 for all |
| 1157 |
|
*/ |
| 1158 |
|
|
| 1159 |
|
static void |
| 1160 |
1398 |
cmd_http_rxrespbody(CMD_ARGS) |
| 1161 |
|
{ |
| 1162 |
|
struct http *hp; |
| 1163 |
1398 |
int max = 0; |
| 1164 |
|
|
| 1165 |
1398 |
(void)vl; |
| 1166 |
1398 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1167 |
1398 |
ONLY_CLIENT(hp, av); |
| 1168 |
1398 |
AZ(strcmp(av[0], "rxrespbody")); |
| 1169 |
1398 |
av++; |
| 1170 |
|
|
| 1171 |
1878 |
for (; *av != NULL; av++) |
| 1172 |
960 |
if (!strcmp(*av, "-max")) { |
| 1173 |
480 |
max = atoi(av[1]); |
| 1174 |
480 |
av++; |
| 1175 |
480 |
} else |
| 1176 |
0 |
vtc_fatal(hp->vl, |
| 1177 |
0 |
"Unknown http rxrespbody spec: %s\n", *av); |
| 1178 |
|
|
| 1179 |
1398 |
http_swallow_body(hp, hp->resp, 1, max); |
| 1180 |
1398 |
vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen); |
| 1181 |
1398 |
} |
| 1182 |
|
|
| 1183 |
|
/* SECTION: client-server.spec.rxchunk |
| 1184 |
|
* |
| 1185 |
|
* rxchunk |
| 1186 |
|
* Receive an HTTP chunk. |
| 1187 |
|
*/ |
| 1188 |
|
|
| 1189 |
|
static void |
| 1190 |
520 |
cmd_http_rxchunk(CMD_ARGS) |
| 1191 |
|
{ |
| 1192 |
|
struct http *hp; |
| 1193 |
|
int ll, i; |
| 1194 |
|
|
| 1195 |
520 |
(void)vl; |
| 1196 |
520 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1197 |
520 |
ONLY_CLIENT(hp, av); |
| 1198 |
|
|
| 1199 |
520 |
i = http_rxchunk(hp); |
| 1200 |
520 |
if (i == 0) { |
| 1201 |
0 |
ll = hp->rx_p - hp->body; |
| 1202 |
0 |
hp->bodyl = ll; |
| 1203 |
0 |
bprintf(hp->bodylen, "%d", ll); |
| 1204 |
0 |
vtc_log(hp->vl, 4, "bodylen = %s", hp->bodylen); |
| 1205 |
0 |
} |
| 1206 |
520 |
} |
| 1207 |
|
|
| 1208 |
|
/********************************************************************** |
| 1209 |
|
* Transmit a request |
| 1210 |
|
*/ |
| 1211 |
|
|
| 1212 |
|
static void |
| 1213 |
131551 |
cmd_http_txreq(CMD_ARGS) |
| 1214 |
|
{ |
| 1215 |
|
struct http *hp; |
| 1216 |
131551 |
const char *req = "GET"; |
| 1217 |
131551 |
const char *url = "/"; |
| 1218 |
131551 |
const char *proto = "HTTP/1.1"; |
| 1219 |
131551 |
const char *up = NULL; |
| 1220 |
|
unsigned nohost; |
| 1221 |
131551 |
unsigned nouseragent = 0; |
| 1222 |
|
|
| 1223 |
131551 |
(void)vl; |
| 1224 |
131551 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1225 |
131551 |
ONLY_CLIENT(hp, av); |
| 1226 |
131551 |
AZ(strcmp(av[0], "txreq")); |
| 1227 |
131551 |
av++; |
| 1228 |
|
|
| 1229 |
131551 |
VSB_clear(hp->vsb); |
| 1230 |
|
|
| 1231 |
131551 |
hp->head_method = 0; |
| 1232 |
211723 |
for (; *av != NULL; av++) { |
| 1233 |
117891 |
if (!strcmp(*av, "-url")) { |
| 1234 |
67853 |
url = av[1]; |
| 1235 |
67853 |
av++; |
| 1236 |
117891 |
} else if (!strcmp(*av, "-proto")) { |
| 1237 |
1520 |
proto = av[1]; |
| 1238 |
1520 |
av++; |
| 1239 |
50038 |
} else if (!strcmp(*av, "-method") || |
| 1240 |
43560 |
!strcmp(*av, "-req")) { |
| 1241 |
10599 |
req = av[1]; |
| 1242 |
10599 |
hp->head_method = !strcmp(av[1], "HEAD") ; |
| 1243 |
10599 |
av++; |
| 1244 |
48518 |
} else if (!hp->sfd && !strcmp(*av, "-up")) { |
| 1245 |
40 |
up = av[1]; |
| 1246 |
40 |
av++; |
| 1247 |
37919 |
} else if (!strcmp(*av, "-nouseragent")) { |
| 1248 |
160 |
nouseragent = 1; |
| 1249 |
160 |
} else |
| 1250 |
37719 |
break; |
| 1251 |
80172 |
} |
| 1252 |
131551 |
VSB_printf(hp->vsb, "%s %s %s%s", req, url, proto, nl); |
| 1253 |
|
|
| 1254 |
131551 |
if (up) |
| 1255 |
80 |
VSB_printf(hp->vsb, "Connection: Upgrade, HTTP2-Settings%s" |
| 1256 |
|
"Upgrade: h2c%s" |
| 1257 |
40 |
"HTTP2-Settings: %s%s", nl, nl, up, nl); |
| 1258 |
|
|
| 1259 |
131551 |
nohost = strcmp(proto, "HTTP/1.1") != 0; |
| 1260 |
131551 |
av = http_tx_parse_args(av, vl, hp, NULL, nohost, 1, 1, nouseragent); |
| 1261 |
131551 |
if (*av != NULL) |
| 1262 |
0 |
vtc_fatal(hp->vl, "Unknown http txreq spec: %s\n", *av); |
| 1263 |
131551 |
http_write(hp, 4, "txreq"); |
| 1264 |
|
|
| 1265 |
131551 |
if (up) { |
| 1266 |
40 |
parse_string(vl, hp, |
| 1267 |
|
"rxresp\n" |
| 1268 |
|
"expect resp.status == 101\n" |
| 1269 |
|
"expect resp.http.connection == Upgrade\n" |
| 1270 |
|
"expect resp.http.upgrade == h2c\n" |
| 1271 |
|
"txpri\n" |
| 1272 |
|
); |
| 1273 |
40 |
b64_settings(hp, up); |
| 1274 |
40 |
parse_string(vl, hp, |
| 1275 |
|
"stream 0 {\n" |
| 1276 |
|
" txsettings\n" |
| 1277 |
|
" rxsettings\n" |
| 1278 |
|
" txsettings -ack\n" |
| 1279 |
|
" rxsettings\n" |
| 1280 |
|
" expect settings.ack == true" |
| 1281 |
|
"} -start\n" |
| 1282 |
|
); |
| 1283 |
40 |
} |
| 1284 |
131551 |
} |
| 1285 |
|
|
| 1286 |
|
/* SECTION: client-server.spec.recv |
| 1287 |
|
* |
| 1288 |
|
* recv NUMBER |
| 1289 |
|
* Read NUMBER bytes from the connection. |
| 1290 |
|
*/ |
| 1291 |
|
|
| 1292 |
|
static void |
| 1293 |
360 |
cmd_http_recv(CMD_ARGS) |
| 1294 |
|
{ |
| 1295 |
|
struct http *hp; |
| 1296 |
|
int i, n; |
| 1297 |
|
char u[32]; |
| 1298 |
|
|
| 1299 |
360 |
(void)vl; |
| 1300 |
360 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1301 |
360 |
AN(av[1]); |
| 1302 |
360 |
AZ(av[2]); |
| 1303 |
360 |
n = strtoul(av[1], NULL, 0); |
| 1304 |
1160 |
while (n > 0) { |
| 1305 |
800 |
i = read(hp->sess->fd, u, n > 32 ? 32 : n); |
| 1306 |
800 |
if (i > 0) |
| 1307 |
800 |
vtc_dump(hp->vl, 4, "recv", u, i); |
| 1308 |
|
else |
| 1309 |
0 |
vtc_log(hp->vl, hp->fatal, "recv() got %d (%s)", i, |
| 1310 |
0 |
strerror(errno)); |
| 1311 |
800 |
n -= i; |
| 1312 |
|
} |
| 1313 |
360 |
} |
| 1314 |
|
|
| 1315 |
|
/* SECTION: client-server.spec.send |
| 1316 |
|
* |
| 1317 |
|
* send STRING |
| 1318 |
|
* Push STRING on the connection. |
| 1319 |
|
*/ |
| 1320 |
|
|
| 1321 |
|
static void |
| 1322 |
36247 |
cmd_http_send(CMD_ARGS) |
| 1323 |
|
{ |
| 1324 |
|
struct http *hp; |
| 1325 |
|
int i; |
| 1326 |
|
|
| 1327 |
36247 |
(void)vl; |
| 1328 |
36247 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1329 |
36247 |
AN(av[1]); |
| 1330 |
36247 |
AZ(av[2]); |
| 1331 |
36247 |
vtc_dump(hp->vl, 4, "send", av[1], -1); |
| 1332 |
36247 |
i = write(hp->sess->fd, av[1], strlen(av[1])); |
| 1333 |
36247 |
if (i != strlen(av[1])) |
| 1334 |
10 |
vtc_log(hp->vl, hp->fatal, "Write error in http_send(): %s", |
| 1335 |
5 |
strerror(errno)); |
| 1336 |
36247 |
} |
| 1337 |
|
|
| 1338 |
|
/* SECTION: client-server.spec.send_n |
| 1339 |
|
* |
| 1340 |
|
* send_n NUMBER STRING |
| 1341 |
|
* Write STRING on the socket NUMBER times. |
| 1342 |
|
*/ |
| 1343 |
|
|
| 1344 |
|
static void |
| 1345 |
160 |
cmd_http_send_n(CMD_ARGS) |
| 1346 |
|
{ |
| 1347 |
|
struct http *hp; |
| 1348 |
|
int i, n, l; |
| 1349 |
|
|
| 1350 |
160 |
(void)vl; |
| 1351 |
160 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1352 |
160 |
AN(av[1]); |
| 1353 |
160 |
AN(av[2]); |
| 1354 |
160 |
AZ(av[3]); |
| 1355 |
160 |
n = strtoul(av[1], NULL, 0); |
| 1356 |
160 |
vtc_dump(hp->vl, 4, "send_n", av[2], -1); |
| 1357 |
160 |
l = strlen(av[2]); |
| 1358 |
83760 |
while (n--) { |
| 1359 |
83600 |
i = write(hp->sess->fd, av[2], l); |
| 1360 |
83600 |
if (i != l) |
| 1361 |
8986 |
vtc_log(hp->vl, hp->fatal, |
| 1362 |
|
"Write error in http_send(): %s", |
| 1363 |
4493 |
strerror(errno)); |
| 1364 |
|
} |
| 1365 |
160 |
} |
| 1366 |
|
|
| 1367 |
|
/* SECTION: client-server.spec.send_urgent |
| 1368 |
|
* |
| 1369 |
|
* send_urgent STRING |
| 1370 |
|
* Send string as TCP OOB urgent data. You will never need this. |
| 1371 |
|
*/ |
| 1372 |
|
|
| 1373 |
|
static void |
| 1374 |
400 |
cmd_http_send_urgent(CMD_ARGS) |
| 1375 |
|
{ |
| 1376 |
|
struct http *hp; |
| 1377 |
|
int i; |
| 1378 |
|
|
| 1379 |
400 |
(void)vl; |
| 1380 |
400 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1381 |
400 |
AN(av[1]); |
| 1382 |
400 |
AZ(av[2]); |
| 1383 |
400 |
vtc_dump(hp->vl, 4, "send_urgent", av[1], -1); |
| 1384 |
400 |
i = send(hp->sess->fd, av[1], strlen(av[1]), MSG_OOB); |
| 1385 |
400 |
if (i != strlen(av[1])) |
| 1386 |
0 |
vtc_log(hp->vl, hp->fatal, |
| 1387 |
0 |
"Write error in http_send_urgent(): %s", strerror(errno)); |
| 1388 |
400 |
} |
| 1389 |
|
|
| 1390 |
|
/* SECTION: client-server.spec.sendhex |
| 1391 |
|
* |
| 1392 |
|
* sendhex STRING |
| 1393 |
|
* Send bytes as described by STRING. STRING should consist of hex pairs |
| 1394 |
|
* possibly separated by whitespace or newlines. For example: |
| 1395 |
|
* "0F EE a5 3df2". |
| 1396 |
|
*/ |
| 1397 |
|
|
| 1398 |
|
static void |
| 1399 |
7160 |
cmd_http_sendhex(CMD_ARGS) |
| 1400 |
|
{ |
| 1401 |
|
struct vsb *vsb; |
| 1402 |
|
struct http *hp; |
| 1403 |
|
|
| 1404 |
7160 |
(void)vl; |
| 1405 |
7160 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1406 |
7160 |
AN(av[1]); |
| 1407 |
7160 |
AZ(av[2]); |
| 1408 |
7160 |
vsb = vtc_hex_to_bin(hp->vl, av[1]); |
| 1409 |
7160 |
assert(VSB_len(vsb) >= 0); |
| 1410 |
7160 |
vtc_hexdump(hp->vl, 4, "sendhex", VSB_data(vsb), VSB_len(vsb)); |
| 1411 |
7160 |
if (VSB_tofile(vsb, hp->sess->fd)) |
| 1412 |
0 |
vtc_log(hp->vl, hp->fatal, "Write failed: %s", |
| 1413 |
0 |
strerror(errno)); |
| 1414 |
7160 |
VSB_destroy(&vsb); |
| 1415 |
7160 |
} |
| 1416 |
|
|
| 1417 |
|
/* SECTION: client-server.spec.chunked |
| 1418 |
|
* |
| 1419 |
|
* chunked STRING |
| 1420 |
|
* Send STRING as chunked encoding. |
| 1421 |
|
*/ |
| 1422 |
|
|
| 1423 |
|
static void |
| 1424 |
2480 |
cmd_http_chunked(CMD_ARGS) |
| 1425 |
|
{ |
| 1426 |
|
struct http *hp; |
| 1427 |
|
|
| 1428 |
2480 |
(void)vl; |
| 1429 |
2480 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1430 |
2480 |
AN(av[1]); |
| 1431 |
2480 |
AZ(av[2]); |
| 1432 |
2480 |
VSB_clear(hp->vsb); |
| 1433 |
4960 |
VSB_printf(hp->vsb, "%jx%s%s%s", |
| 1434 |
2480 |
(uintmax_t)strlen(av[1]), nl, av[1], nl); |
| 1435 |
2480 |
http_write(hp, 4, "chunked"); |
| 1436 |
2480 |
} |
| 1437 |
|
|
| 1438 |
|
/* SECTION: client-server.spec.chunkedlen |
| 1439 |
|
* |
| 1440 |
|
* chunkedlen NUMBER |
| 1441 |
|
* Do as ``chunked`` except that the string will be generated |
| 1442 |
|
* for you, with a length of NUMBER characters. |
| 1443 |
|
*/ |
| 1444 |
|
|
| 1445 |
|
static void |
| 1446 |
7556 |
cmd_http_chunkedlen(CMD_ARGS) |
| 1447 |
|
{ |
| 1448 |
|
unsigned len; |
| 1449 |
|
unsigned u, v; |
| 1450 |
|
char buf[16384]; |
| 1451 |
|
struct http *hp; |
| 1452 |
|
|
| 1453 |
7556 |
(void)vl; |
| 1454 |
7556 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1455 |
7556 |
AN(av[1]); |
| 1456 |
7556 |
AZ(av[2]); |
| 1457 |
7556 |
VSB_clear(hp->vsb); |
| 1458 |
|
|
| 1459 |
7556 |
len = atoi(av[1]); |
| 1460 |
|
|
| 1461 |
7556 |
if (len == 0) { |
| 1462 |
2838 |
VSB_printf(hp->vsb, "0%s%s", nl, nl); |
| 1463 |
2838 |
} else { |
| 1464 |
69418077 |
for (u = 0; u < sizeof buf; u++) |
| 1465 |
69413359 |
buf[u] = (u & 7) + '0'; |
| 1466 |
|
|
| 1467 |
4718 |
VSB_printf(hp->vsb, "%x%s", len, nl); |
| 1468 |
16397 |
for (u = 0; u < len; u += v) { |
| 1469 |
11679 |
v = vmin_t(unsigned, len - u, sizeof buf); |
| 1470 |
11679 |
VSB_bcat(hp->vsb, buf, v); |
| 1471 |
11679 |
} |
| 1472 |
4718 |
VSB_printf(hp->vsb, "%s", nl); |
| 1473 |
|
} |
| 1474 |
7556 |
http_write(hp, 4, "chunked"); |
| 1475 |
7556 |
} |
| 1476 |
|
|
| 1477 |
|
|
| 1478 |
|
/* SECTION: client-server.spec.timeout |
| 1479 |
|
* |
| 1480 |
|
* timeout NUMBER |
| 1481 |
|
* Set the TCP timeout for this entity. |
| 1482 |
|
*/ |
| 1483 |
|
|
| 1484 |
|
static void |
| 1485 |
1360 |
cmd_http_timeout(CMD_ARGS) |
| 1486 |
|
{ |
| 1487 |
|
struct http *hp; |
| 1488 |
|
double d; |
| 1489 |
|
|
| 1490 |
1360 |
(void)vl; |
| 1491 |
1360 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1492 |
1360 |
AN(av[1]); |
| 1493 |
1360 |
AZ(av[2]); |
| 1494 |
1360 |
d = VNUM(av[1]); |
| 1495 |
1360 |
if (isnan(d)) |
| 1496 |
0 |
vtc_fatal(vl, "timeout is not a number (%s)", av[1]); |
| 1497 |
1360 |
hp->timeout = d; |
| 1498 |
1360 |
} |
| 1499 |
|
|
| 1500 |
|
/* SECTION: client-server.spec.expect_close |
| 1501 |
|
* |
| 1502 |
|
* expect_close |
| 1503 |
|
* Reads from the connection, expecting nothing to read but an EOF. |
| 1504 |
|
*/ |
| 1505 |
|
static void |
| 1506 |
7027 |
cmd_http_expect_close(CMD_ARGS) |
| 1507 |
|
{ |
| 1508 |
|
struct http *hp; |
| 1509 |
|
struct pollfd fds[1]; |
| 1510 |
|
char c; |
| 1511 |
|
int i; |
| 1512 |
|
|
| 1513 |
7027 |
(void)vl; |
| 1514 |
7027 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1515 |
7027 |
AZ(av[1]); |
| 1516 |
|
|
| 1517 |
7027 |
vtc_log(vl, 4, "Expecting close (fd = %d)", hp->sess->fd); |
| 1518 |
7027 |
if (hp->h2) |
| 1519 |
560 |
stop_h2(hp); |
| 1520 |
7027 |
while (1) { |
| 1521 |
7027 |
fds[0].fd = hp->sess->fd; |
| 1522 |
7027 |
fds[0].events = POLLIN; |
| 1523 |
7027 |
fds[0].revents = 0; |
| 1524 |
7027 |
i = poll(fds, 1, (int)(hp->timeout * 1000)); |
| 1525 |
7027 |
if (i < 0 && errno == EINTR) |
| 1526 |
0 |
continue; |
| 1527 |
7027 |
if (i == 0) |
| 1528 |
0 |
vtc_log(vl, hp->fatal, "Expected close: timeout"); |
| 1529 |
7027 |
if (i != 1 || !(fds[0].revents & (POLLIN|POLLERR|POLLHUP))) |
| 1530 |
412 |
vtc_log(vl, hp->fatal, |
| 1531 |
|
"Expected close: poll = %d, revents = 0x%x", |
| 1532 |
206 |
i, fds[0].revents); |
| 1533 |
6821 |
i = read(hp->sess->fd, &c, 1); |
| 1534 |
6821 |
if (i <= 0 && VTCP_Check(i)) |
| 1535 |
6821 |
break; |
| 1536 |
206 |
if (i == 1 && vct_islws(c)) |
| 1537 |
0 |
continue; |
| 1538 |
0 |
vtc_log(vl, hp->fatal, |
| 1539 |
0 |
"Expecting close: read = %d, c = 0x%02x", i, c); |
| 1540 |
|
} |
| 1541 |
6821 |
vtc_log(vl, 4, "fd=%d EOF, as expected", hp->sess->fd); |
| 1542 |
6821 |
} |
| 1543 |
|
|
| 1544 |
|
/* SECTION: client-server.spec.close |
| 1545 |
|
* |
| 1546 |
|
* close (server only) |
| 1547 |
|
* Close the connection. Note that if operating in HTTP/2 mode no |
| 1548 |
|
* extra (GOAWAY) frame is sent, it's simply a TCP close. |
| 1549 |
|
*/ |
| 1550 |
|
static void |
| 1551 |
800 |
cmd_http_close(CMD_ARGS) |
| 1552 |
|
{ |
| 1553 |
|
struct http *hp; |
| 1554 |
|
|
| 1555 |
800 |
(void)vl; |
| 1556 |
800 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1557 |
800 |
ONLY_SERVER(hp, av); |
| 1558 |
800 |
AZ(av[1]); |
| 1559 |
800 |
assert(hp->sfd != NULL); |
| 1560 |
800 |
assert(*hp->sfd >= 0); |
| 1561 |
800 |
if (hp->h2) |
| 1562 |
0 |
stop_h2(hp); |
| 1563 |
800 |
VTCP_close(&hp->sess->fd); |
| 1564 |
800 |
vtc_log(vl, 4, "Closed"); |
| 1565 |
800 |
} |
| 1566 |
|
|
| 1567 |
|
/* SECTION: client-server.spec.accept |
| 1568 |
|
* |
| 1569 |
|
* accept (server only) |
| 1570 |
|
* Close the current connection, if any, and accept a new one. Note |
| 1571 |
|
* that this new connection is HTTP/1.x. |
| 1572 |
|
*/ |
| 1573 |
|
static void |
| 1574 |
6548 |
cmd_http_accept(CMD_ARGS) |
| 1575 |
|
{ |
| 1576 |
|
struct http *hp; |
| 1577 |
|
|
| 1578 |
6548 |
(void)vl; |
| 1579 |
6548 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1580 |
6548 |
ONLY_SERVER(hp, av); |
| 1581 |
6548 |
AZ(av[1]); |
| 1582 |
6548 |
assert(hp->sfd != NULL); |
| 1583 |
6548 |
assert(*hp->sfd >= 0); |
| 1584 |
6548 |
if (hp->h2) |
| 1585 |
0 |
stop_h2(hp); |
| 1586 |
6548 |
if (hp->sess->fd >= 0) |
| 1587 |
5788 |
VTCP_close(&hp->sess->fd); |
| 1588 |
6548 |
vtc_log(vl, 4, "Accepting"); |
| 1589 |
6548 |
hp->sess->fd = accept(*hp->sfd, NULL, NULL); |
| 1590 |
6548 |
if (hp->sess->fd < 0) |
| 1591 |
0 |
vtc_log(vl, hp->fatal, "Accepted failed: %s", strerror(errno)); |
| 1592 |
6548 |
vtc_log(vl, 3, "Accepted socket fd is %d", hp->sess->fd); |
| 1593 |
6548 |
} |
| 1594 |
|
|
| 1595 |
|
/* SECTION: client-server.spec.shutdown |
| 1596 |
|
* |
| 1597 |
|
* shutdown |
| 1598 |
|
* Initiate shutdown. |
| 1599 |
|
* |
| 1600 |
|
* \-read |
| 1601 |
|
* Shutdown the read direction. |
| 1602 |
|
* \-write |
| 1603 |
|
* Shutdown the write direction. |
| 1604 |
|
* |
| 1605 |
|
* The default is both direction. |
| 1606 |
|
*/ |
| 1607 |
|
static void |
| 1608 |
|
cmd_http_shutdown(CMD_ARGS) |
| 1609 |
|
{ |
| 1610 |
400 |
struct http *hp; |
| 1611 |
|
int how = SHUT_RDWR; |
| 1612 |
|
const char *str[] = { |
| 1613 |
400 |
[SHUT_RD] = "RD", |
| 1614 |
400 |
[SHUT_WR] = "WR", |
| 1615 |
|
[SHUT_RDWR] = "RDWR", |
| 1616 |
400 |
}; |
| 1617 |
|
|
| 1618 |
|
(void)vl; |
| 1619 |
|
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1620 |
|
AZ(strcmp(av[0], "shutdown")); |
| 1621 |
|
av++; |
| 1622 |
400 |
|
| 1623 |
400 |
if (*av != NULL) { |
| 1624 |
400 |
if (!strcmp(*av, "-read")) { |
| 1625 |
400 |
how = SHUT_RD; |
| 1626 |
|
av++; |
| 1627 |
800 |
} else if (!strcmp(*av, "-write")) { |
| 1628 |
400 |
how = SHUT_WR; |
| 1629 |
80 |
av++; |
| 1630 |
80 |
} |
| 1631 |
400 |
} |
| 1632 |
240 |
|
| 1633 |
240 |
if (*av != NULL) |
| 1634 |
320 |
vtc_fatal(hp->vl, "Unknown http shutdown spec: %s\n", *av); |
| 1635 |
80 |
|
| 1636 |
80 |
vtc_log(vl, 4, "Shutting down fd (%s): %d", str[how], hp->sess->fd); |
| 1637 |
80 |
if (shutdown(hp->sess->fd, how) < 0) |
| 1638 |
0 |
vtc_log(vl, hp->fatal, "Shutdown failed: %s", strerror(errno)); |
| 1639 |
|
vtc_log(vl, 3, "Shutdown socket fd (%d): %d", how, hp->sess->fd); |
| 1640 |
|
} |
| 1641 |
|
|
| 1642 |
400 |
/* SECTION: client-server.spec.fatal |
| 1643 |
0 |
* |
| 1644 |
|
* fatal|non_fatal |
| 1645 |
400 |
* Control whether a failure of this entity should stop the test. |
| 1646 |
400 |
*/ |
| 1647 |
400 |
|
| 1648 |
400 |
static void |
| 1649 |
0 |
cmd_http_fatal(CMD_ARGS) |
| 1650 |
|
{ |
| 1651 |
0 |
struct http *hp; |
| 1652 |
|
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1653 |
400 |
|
| 1654 |
0 |
(void)vl; |
| 1655 |
|
AZ(av[1]); |
| 1656 |
0 |
if (!strcmp(av[0], "fatal")) { |
| 1657 |
|
hp->fatal = 0; |
| 1658 |
|
} else { |
| 1659 |
800 |
assert(!strcmp(av[0], "non_fatal")); |
| 1660 |
400 |
hp->fatal = -1; |
| 1661 |
|
} |
| 1662 |
400 |
} |
| 1663 |
|
|
| 1664 |
|
#define cmd_http_non_fatal cmd_http_fatal |
| 1665 |
|
|
| 1666 |
|
static const char PREFACE[24] = { |
| 1667 |
|
0x50, 0x52, 0x49, 0x20, 0x2a, 0x20, 0x48, 0x54, |
| 1668 |
|
0x54, 0x50, 0x2f, 0x32, 0x2e, 0x30, 0x0d, 0x0a, |
| 1669 |
|
0x0d, 0x0a, 0x53, 0x4d, 0x0d, 0x0a, 0x0d, 0x0a |
| 1670 |
|
}; |
| 1671 |
16808 |
|
| 1672 |
|
/* SECTION: client-server.spec.txpri |
| 1673 |
|
* |
| 1674 |
16808 |
* txpri (client only) |
| 1675 |
|
* Send an HTTP/2 preface ("PRI * HTTP/2.0\\r\\n\\r\\nSM\\r\\n\\r\\n") |
| 1676 |
16808 |
* and set client to HTTP/2. |
| 1677 |
16808 |
*/ |
| 1678 |
16808 |
static void |
| 1679 |
880 |
cmd_http_txpri(CMD_ARGS) |
| 1680 |
880 |
{ |
| 1681 |
15928 |
size_t l; |
| 1682 |
15928 |
struct http *hp; |
| 1683 |
|
|
| 1684 |
16808 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1685 |
|
ONLY_CLIENT(hp, av); |
| 1686 |
|
|
| 1687 |
|
vtc_dump(hp->vl, 4, "txpri", PREFACE, sizeof(PREFACE)); |
| 1688 |
|
/* Dribble out the preface */ |
| 1689 |
|
l = write(hp->sess->fd, PREFACE, 18); |
| 1690 |
|
if (l != 18) |
| 1691 |
|
vtc_log(vl, hp->fatal, "Write failed: (%zd vs %zd) %s", |
| 1692 |
|
l, sizeof(PREFACE), strerror(errno)); |
| 1693 |
|
VTIM_sleep(0.01); |
| 1694 |
|
l = write(hp->sess->fd, PREFACE + 18, sizeof(PREFACE) - 18); |
| 1695 |
|
if (l != sizeof(PREFACE) - 18) |
| 1696 |
|
vtc_log(vl, hp->fatal, "Write failed: (%zd vs %zd) %s", |
| 1697 |
|
l, sizeof(PREFACE), strerror(errno)); |
| 1698 |
|
|
| 1699 |
|
start_h2(hp); |
| 1700 |
|
AN(hp->h2); |
| 1701 |
7280 |
} |
| 1702 |
|
|
| 1703 |
|
/* SECTION: client-server.spec.rxpri |
| 1704 |
|
* |
| 1705 |
|
* rxpri (server only) |
| 1706 |
7280 |
* Receive a preface. If valid set the server to HTTP/2, abort |
| 1707 |
7280 |
* otherwise. |
| 1708 |
|
*/ |
| 1709 |
7280 |
static void |
| 1710 |
|
cmd_http_rxpri(CMD_ARGS) |
| 1711 |
7280 |
{ |
| 1712 |
7280 |
struct http *hp; |
| 1713 |
0 |
|
| 1714 |
0 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1715 |
7280 |
ONLY_SERVER(hp, av); |
| 1716 |
7280 |
|
| 1717 |
7280 |
hp->rx_p = hp->rx_b; |
| 1718 |
0 |
if (!http_rxchar(hp, sizeof(PREFACE), 0)) |
| 1719 |
0 |
vtc_fatal(vl, "Couldn't retrieve connection preface"); |
| 1720 |
|
if (memcmp(hp->rx_b, PREFACE, sizeof(PREFACE))) |
| 1721 |
7280 |
vtc_fatal(vl, "Received invalid preface\n"); |
| 1722 |
7280 |
start_h2(hp); |
| 1723 |
7280 |
AN(hp->h2); |
| 1724 |
|
} |
| 1725 |
|
|
| 1726 |
|
/* SECTION: client-server.spec.settings |
| 1727 |
|
* |
| 1728 |
|
* settings -dectbl INT |
| 1729 |
|
* Force internal HTTP/2 settings to certain values. Currently only |
| 1730 |
|
* support setting the decoding table size. |
| 1731 |
|
*/ |
| 1732 |
1200 |
static void |
| 1733 |
|
cmd_http_settings(CMD_ARGS) |
| 1734 |
|
{ |
| 1735 |
|
uint32_t n; |
| 1736 |
1200 |
char *p; |
| 1737 |
1200 |
struct http *hp; |
| 1738 |
|
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1739 |
1200 |
|
| 1740 |
1200 |
if (!hp->h2) |
| 1741 |
0 |
vtc_fatal(hp->vl, "Only possible in H/2 mode"); |
| 1742 |
1200 |
|
| 1743 |
0 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1744 |
1200 |
|
| 1745 |
1200 |
for (; *av != NULL; av++) { |
| 1746 |
1200 |
if (!strcmp(*av, "-dectbl")) { |
| 1747 |
|
n = strtoul(av[1], &p, 0); |
| 1748 |
|
if (*p != '\0') |
| 1749 |
|
vtc_fatal(hp->vl, "-dectbl takes an integer as " |
| 1750 |
|
"argument (found %s)", av[1]); |
| 1751 |
|
assert(HPK_ResizeTbl(hp->decctx, n) != hpk_err); |
| 1752 |
|
av++; |
| 1753 |
|
} else |
| 1754 |
|
vtc_fatal(vl, "Unknown settings spec: %s\n", *av); |
| 1755 |
0 |
} |
| 1756 |
|
} |
| 1757 |
|
|
| 1758 |
|
static void |
| 1759 |
|
cmd_http_stream(CMD_ARGS) |
| 1760 |
0 |
{ |
| 1761 |
|
struct http *hp; |
| 1762 |
0 |
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1763 |
0 |
if (!hp->h2) { |
| 1764 |
|
vtc_log(hp->vl, 4, "Not in H/2 mode, do what's needed"); |
| 1765 |
0 |
if (hp->sfd) |
| 1766 |
|
parse_string(vl, hp, "rxpri"); |
| 1767 |
0 |
else |
| 1768 |
0 |
parse_string(vl, hp, "txpri"); |
| 1769 |
0 |
parse_string(vl, hp, |
| 1770 |
0 |
"stream 0 {\n" |
| 1771 |
0 |
" txsettings\n" |
| 1772 |
0 |
" rxsettings\n" |
| 1773 |
0 |
" txsettings -ack\n" |
| 1774 |
0 |
" rxsettings\n" |
| 1775 |
0 |
" expect settings.ack == true" |
| 1776 |
0 |
"} -run\n" |
| 1777 |
0 |
); |
| 1778 |
0 |
} |
| 1779 |
|
cmd_stream(av, hp, vl); |
| 1780 |
|
} |
| 1781 |
24360 |
|
| 1782 |
|
/* SECTION: client-server.spec.write_body |
| 1783 |
|
* |
| 1784 |
24360 |
* write_body STRING |
| 1785 |
24360 |
* Write the body of a request or a response to a file. By using the |
| 1786 |
7520 |
* shell command, higher-level checks on the body can be performed |
| 1787 |
7520 |
* (eg. XML, JSON, ...) provided that such checks can be delegated |
| 1788 |
1040 |
* to an external program. |
| 1789 |
|
*/ |
| 1790 |
6480 |
static void |
| 1791 |
7520 |
cmd_http_write_body(CMD_ARGS) |
| 1792 |
|
{ |
| 1793 |
|
struct http *hp; |
| 1794 |
|
|
| 1795 |
|
(void)vl; |
| 1796 |
|
CAST_OBJ_NOTNULL(hp, priv, HTTP_MAGIC); |
| 1797 |
|
AN(av[0]); |
| 1798 |
|
AN(av[1]); |
| 1799 |
|
AZ(av[2]); |
| 1800 |
7520 |
AZ(strcmp(av[0], "write_body")); |
| 1801 |
24360 |
if (VFIL_writefile(NULL, av[1], hp->body, hp->bodyl) != 0) |
| 1802 |
24360 |
vtc_fatal(hp->vl, "failed to write body: %s (%d)", |
| 1803 |
|
strerror(errno), errno); |
| 1804 |
|
} |
| 1805 |
|
|
| 1806 |
|
/********************************************************************** |
| 1807 |
|
* Execute HTTP specifications |
| 1808 |
|
*/ |
| 1809 |
|
|
| 1810 |
|
const struct cmds http_cmds[] = { |
| 1811 |
|
#define CMD_HTTP(n) { #n, cmd_http_##n }, |
| 1812 |
|
/* session */ |
| 1813 |
80 |
CMD_HTTP(accept) |
| 1814 |
|
CMD_HTTP(close) |
| 1815 |
|
CMD_HTTP(recv) |
| 1816 |
|
CMD_HTTP(send) |
| 1817 |
80 |
CMD_HTTP(send_n) |
| 1818 |
80 |
CMD_HTTP(send_urgent) |
| 1819 |
80 |
CMD_HTTP(sendhex) |
| 1820 |
80 |
CMD_HTTP(shutdown) |
| 1821 |
80 |
CMD_HTTP(timeout) |
| 1822 |
80 |
|
| 1823 |
80 |
/* spec */ |
| 1824 |
0 |
CMD_HTTP(fatal) |
| 1825 |
0 |
CMD_HTTP(non_fatal) |
| 1826 |
80 |
|
| 1827 |
|
/* body */ |
| 1828 |
|
CMD_HTTP(gunzip) |
| 1829 |
|
CMD_HTTP(write_body) |
| 1830 |
|
|
| 1831 |
|
/* HTTP/1.x */ |
| 1832 |
|
CMD_HTTP(chunked) |
| 1833 |
|
CMD_HTTP(chunkedlen) |
| 1834 |
|
CMD_HTTP(rxchunk) |
| 1835 |
|
|
| 1836 |
|
/* HTTP/2 */ |
| 1837 |
|
CMD_HTTP(stream) |
| 1838 |
|
CMD_HTTP(settings) |
| 1839 |
|
|
| 1840 |
|
/* client */ |
| 1841 |
|
CMD_HTTP(rxresp) |
| 1842 |
|
CMD_HTTP(rxrespbody) |
| 1843 |
|
CMD_HTTP(rxresphdrs) |
| 1844 |
|
CMD_HTTP(txpri) |
| 1845 |
|
CMD_HTTP(txreq) |
| 1846 |
|
|
| 1847 |
|
/* server */ |
| 1848 |
|
CMD_HTTP(rxpri) |
| 1849 |
|
CMD_HTTP(rxreq) |
| 1850 |
|
CMD_HTTP(rxreqbody) |
| 1851 |
|
CMD_HTTP(rxreqhdrs) |
| 1852 |
|
CMD_HTTP(txresp) |
| 1853 |
|
CMD_HTTP(upgrade) |
| 1854 |
|
|
| 1855 |
|
/* expect */ |
| 1856 |
|
CMD_HTTP(expect) |
| 1857 |
|
CMD_HTTP(expect_close) |
| 1858 |
|
CMD_HTTP(expect_pattern) |
| 1859 |
|
#undef CMD_HTTP |
| 1860 |
|
{ NULL, NULL } |
| 1861 |
|
}; |
| 1862 |
|
|
| 1863 |
|
static void |
| 1864 |
|
http_process_cleanup(void *arg) |
| 1865 |
|
{ |
| 1866 |
|
struct http *hp; |
| 1867 |
|
|
| 1868 |
|
CAST_OBJ_NOTNULL(hp, arg, HTTP_MAGIC); |
| 1869 |
|
|
| 1870 |
|
if (hp->h2) |
| 1871 |
|
stop_h2(hp); |
| 1872 |
|
VSB_destroy(&hp->vsb); |
| 1873 |
|
free(hp->rx_b); |
| 1874 |
|
free(hp->rem_ip); |
| 1875 |
|
free(hp->rem_port); |
| 1876 |
|
free(hp->rem_path); |
| 1877 |
|
FREE_OBJ(hp); |
| 1878 |
|
} |
| 1879 |
|
|
| 1880 |
|
int |
| 1881 |
|
http_process(struct vtclog *vl, struct vtc_sess *vsp, const char *spec, |
| 1882 |
|
int sock, int *sfd, const char *addr, int rcvbuf) |
| 1883 |
|
{ |
| 1884 |
|
struct http *hp; |
| 1885 |
|
int retval, oldbuf; |
| 1886 |
160032 |
socklen_t intlen = sizeof(int); |
| 1887 |
|
|
| 1888 |
|
(void)sfd; |
| 1889 |
|
ALLOC_OBJ(hp, HTTP_MAGIC); |
| 1890 |
160032 |
AN(hp); |
| 1891 |
|
hp->sess = vsp; |
| 1892 |
160032 |
hp->sess->fd = sock; |
| 1893 |
7920 |
hp->timeout = vtc_maxdur * .5; |
| 1894 |
160032 |
|
| 1895 |
160032 |
if (rcvbuf) { |
| 1896 |
160032 |
// XXX setsockopt() too late on SunOS |
| 1897 |
160032 |
// https://github.com/varnishcache/varnish-cache/pull/2980#issuecomment-486214661 |
| 1898 |
160032 |
hp->rcvbuf = rcvbuf; |
| 1899 |
160032 |
|
| 1900 |
160032 |
oldbuf = 0; |
| 1901 |
|
AZ(getsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &oldbuf, &intlen)); |
| 1902 |
|
AZ(setsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, intlen)); |
| 1903 |
160047 |
AZ(getsockopt(hp->sess->fd, SOL_SOCKET, SO_RCVBUF, &rcvbuf, &intlen)); |
| 1904 |
|
|
| 1905 |
|
vtc_log(vl, 3, "-rcvbuf fd=%d old=%d new=%d actual=%d", |
| 1906 |
|
hp->sess->fd, oldbuf, hp->rcvbuf, rcvbuf); |
| 1907 |
|
} |
| 1908 |
160047 |
|
| 1909 |
|
hp->nrxbuf = 2048*1024; |
| 1910 |
160047 |
hp->rx_b = malloc(hp->nrxbuf); |
| 1911 |
160047 |
AN(hp->rx_b); |
| 1912 |
160047 |
hp->rx_e = hp->rx_b + hp->nrxbuf; |
| 1913 |
160047 |
hp->rx_p = hp->rx_b; |
| 1914 |
160047 |
*hp->rx_p = '\0'; |
| 1915 |
160047 |
|
| 1916 |
|
hp->vsb = VSB_new_auto(); |
| 1917 |
160047 |
AN(hp->vsb); |
| 1918 |
|
|
| 1919 |
|
hp->sfd = sfd; |
| 1920 |
360 |
|
| 1921 |
|
hp->rem_ip = malloc(VTCP_ADDRBUFSIZE); |
| 1922 |
360 |
AN(hp->rem_ip); |
| 1923 |
360 |
|
| 1924 |
360 |
hp->rem_port = malloc(VTCP_PORTBUFSIZE); |
| 1925 |
360 |
AN(hp->rem_port); |
| 1926 |
|
|
| 1927 |
720 |
hp->vl = vl; |
| 1928 |
360 |
vtc_log_set_cmd(hp->vl, http_cmds); |
| 1929 |
360 |
hp->gziplevel = 0; |
| 1930 |
|
hp->gzipresidual = -1; |
| 1931 |
160047 |
|
| 1932 |
160047 |
if (*addr != '/') { |
| 1933 |
160047 |
VTCP_hisname(sock, hp->rem_ip, VTCP_ADDRBUFSIZE, hp->rem_port, |
| 1934 |
160047 |
VTCP_PORTBUFSIZE); |
| 1935 |
160047 |
hp->rem_path = NULL; |
| 1936 |
160047 |
} else { |
| 1937 |
|
strcpy(hp->rem_ip, "0.0.0.0"); |
| 1938 |
160047 |
strcpy(hp->rem_port, "0"); |
| 1939 |
160047 |
hp->rem_path = strdup(addr); |
| 1940 |
|
} |
| 1941 |
160047 |
/* XXX: After an upgrade to HTTP/2 the cleanup of a server that is |
| 1942 |
|
* not -wait'ed before the test resets is subject to a race where the |
| 1943 |
160047 |
* cleanup does not happen, so ASAN reports leaks despite the push |
| 1944 |
160047 |
* of a cleanup handler. To easily reproduce, remove the server wait |
| 1945 |
|
* from a02022.vtc and run with ASAN enabled. |
| 1946 |
160047 |
*/ |
| 1947 |
160047 |
pthread_cleanup_push(http_process_cleanup, hp); |
| 1948 |
|
parse_string(vl, hp, spec); |
| 1949 |
160047 |
retval = hp->sess->fd; |
| 1950 |
160047 |
pthread_cleanup_pop(0); |
| 1951 |
160047 |
http_process_cleanup(hp); |
| 1952 |
160047 |
return (retval); |
| 1953 |
|
} |
| 1954 |
160047 |
|
| 1955 |
147646 |
/********************************************************************** |
| 1956 |
|
* Magic test routine |
| 1957 |
147646 |
* |
| 1958 |
147646 |
* This function brute-forces some short strings through gzip(9) to |
| 1959 |
12401 |
* find candidates for all possible 8 bit positions of the stopbit. |
| 1960 |
12401 |
* |
| 1961 |
12401 |
* Here is some good short output strings: |
| 1962 |
|
* |
| 1963 |
|
* 0 184 <e04c8d0fd604c> |
| 1964 |
|
* 1 257 <1ea86e6cf31bf4ec3d7a86> |
| 1965 |
|
* 2 106 <10> |
| 1966 |
|
* 3 163 <a5e2e2e1c2e2> |
| 1967 |
|
* 4 180 <71c5d18ec5d5d1> |
| 1968 |
|
* 5 189 <39886d28a6d2988> |
| 1969 |
160047 |
* 6 118 <80000> |
| 1970 |
160047 |
* 7 151 <386811868> |
| 1971 |
160047 |
* |
| 1972 |
160047 |
*/ |
| 1973 |
160047 |
|
| 1974 |
160047 |
#if 0 |
| 1975 |
|
void xxx(void); |
| 1976 |
|
|
| 1977 |
|
void |
| 1978 |
|
xxx(void) |
| 1979 |
|
{ |
| 1980 |
|
z_stream vz; |
| 1981 |
|
int n; |
| 1982 |
|
char ibuf[200]; |
| 1983 |
|
char obuf[200]; |
| 1984 |
|
int fl[8]; |
| 1985 |
|
int i, j; |
| 1986 |
|
|
| 1987 |
|
for (n = 0; n < 8; n++) |
| 1988 |
|
fl[n] = 9999; |
| 1989 |
|
|
| 1990 |
|
memset(&vz, 0, sizeof vz); |
| 1991 |
|
|
| 1992 |
|
for (n = 0; n < 999999999; n++) { |
| 1993 |
|
*ibuf = 0; |
| 1994 |
|
for (j = 0; j < 7; j++) { |
| 1995 |
|
snprintf(strchr(ibuf, 0), 5, "%x", |
| 1996 |
|
(unsigned)VRND_RandomTestable() & 0xffff); |
| 1997 |
|
vz.next_in = TRUST_ME(ibuf); |
| 1998 |
|
vz.avail_in = strlen(ibuf); |
| 1999 |
|
vz.next_out = TRUST_ME(obuf); |
| 2000 |
|
vz.avail_out = sizeof obuf; |
| 2001 |
|
assert(Z_OK == deflateInit2(&vz, |
| 2002 |
|
9, Z_DEFLATED, 31, 9, Z_DEFAULT_STRATEGY)); |
| 2003 |
|
assert(Z_STREAM_END == deflate(&vz, Z_FINISH)); |
| 2004 |
|
i = vz.stop_bit & 7; |
| 2005 |
|
if (fl[i] > strlen(ibuf)) { |
| 2006 |
|
printf("%d %jd <%s>\n", i, vz.stop_bit, ibuf); |
| 2007 |
|
fl[i] = strlen(ibuf); |
| 2008 |
|
} |
| 2009 |
|
assert(Z_OK == deflateEnd(&vz)); |
| 2010 |
|
} |
| 2011 |
|
} |
| 2012 |
|
|
| 2013 |
|
printf("FOO\n"); |
| 2014 |
|
} |
| 2015 |
|
#endif |