| Code Coverage: vinyl-cache/bin/vinyld/cache/cache_range.c |
| |
| 82217aa4fc7094b3610340078afaf78cd95a8ec9 |
| |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions |
| 11 |
|
* are met: |
| 12 |
|
* 1. Redistributions of source code must retain the above copyright |
| 13 |
|
* notice, this list of conditions and the following disclaimer. |
| 14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
|
* notice, this list of conditions and the following disclaimer in the |
| 16 |
|
* documentation and/or other materials provided with the distribution. |
| 17 |
|
* |
| 18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 |
|
* SUCH DAMAGE. |
| 29 |
|
*/ |
| 30 |
|
|
| 31 |
|
//lint --e{801} |
| 32 |
|
|
| 33 |
|
#include "config.h" |
| 34 |
|
|
| 35 |
|
#include "cache_int.h" |
| 36 |
|
#include "cache_filter.h" |
| 37 |
|
|
| 38 |
|
#include "vct.h" |
| 39 |
|
#include <vnum.h> |
| 40 |
|
#include <vtim.h> |
| 41 |
|
|
| 42 |
|
/*--------------------------------------------------------------------*/ |
| 43 |
|
|
| 44 |
|
struct vrg_priv { |
| 45 |
|
unsigned magic; |
| 46 |
|
#define VRG_PRIV_MAGIC 0xb886e711 |
| 47 |
|
struct req *req; |
| 48 |
|
ssize_t range_low; |
| 49 |
|
ssize_t range_high; |
| 50 |
|
ssize_t range_off; |
| 51 |
|
}; |
| 52 |
|
|
| 53 |
|
static const char * http_GetRange(const struct http *hp, ssize_t *lo, |
| 54 |
|
ssize_t *hi, ssize_t len); |
| 55 |
|
|
| 56 |
|
static int v_matchproto_(vdp_fini_f) |
| 57 |
|
vrg_range_fini(struct vdp_ctx *vdc, void **priv) |
| 58 |
612 |
{ |
| 59 |
612 |
struct vrg_priv *vrg_priv; |
| 60 |
|
|
| 61 |
612 |
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC); |
| 62 |
612 |
TAKE_OBJ_NOTNULL(vrg_priv, priv, VRG_PRIV_MAGIC); |
| 63 |
612 |
if (vrg_priv->req->resp_len >= 0 && |
| 64 |
612 |
vrg_priv->range_off < vrg_priv->range_high) { |
| 65 |
482 |
Req_Fail(vrg_priv->req, SC_RANGE_SHORT); |
| 66 |
482 |
vdc->retval = -1; |
| 67 |
482 |
} |
| 68 |
|
/* struct on ws, no need to free */ |
| 69 |
612 |
return (0); |
| 70 |
612 |
} |
| 71 |
|
|
| 72 |
|
static int v_matchproto_(vdp_bytes_f) |
| 73 |
|
vrg_range_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv, |
| 74 |
|
const void *ptr, ssize_t len) |
| 75 |
67 |
{ |
| 76 |
67 |
int retval = 0; |
| 77 |
67 |
ssize_t l = 0; |
| 78 |
67 |
const char *p = ptr; |
| 79 |
67 |
struct vrg_priv *vrg_priv; |
| 80 |
|
|
| 81 |
67 |
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC); |
| 82 |
67 |
AN(priv); |
| 83 |
67 |
CAST_OBJ_NOTNULL(vrg_priv, *priv, VRG_PRIV_MAGIC); |
| 84 |
|
|
| 85 |
67 |
if (ptr != NULL) { |
| 86 |
49 |
l = vrg_priv->range_low - vrg_priv->range_off; |
| 87 |
49 |
if (l > 0) { |
| 88 |
25 |
if (l > len) |
| 89 |
2 |
l = len; |
| 90 |
25 |
vrg_priv->range_off += l; |
| 91 |
25 |
p += l; |
| 92 |
25 |
len -= l; |
| 93 |
25 |
} |
| 94 |
49 |
l = vmin(vrg_priv->range_high - vrg_priv->range_off, len); |
| 95 |
49 |
vrg_priv->range_off += len; |
| 96 |
49 |
if (vrg_priv->range_off >= vrg_priv->range_high) |
| 97 |
31 |
act = VDP_END; |
| 98 |
49 |
} |
| 99 |
|
|
| 100 |
67 |
if (l > 0) |
| 101 |
46 |
retval = VDP_bytes(vdc, act, p, l); |
| 102 |
21 |
else if (l == 0 && act > VDP_NULL) |
| 103 |
19 |
retval = VDP_bytes(vdc, act, p, 0); |
| 104 |
67 |
return (retval || act == VDP_END ? 1 : 0); |
| 105 |
67 |
} |
| 106 |
|
|
| 107 |
|
/*--------------------------------------------------------------------*/ |
| 108 |
|
|
| 109 |
|
static const char * |
| 110 |
|
vrg_dorange(struct req *req, intmax_t *clen, void **priv) |
| 111 |
626 |
{ |
| 112 |
626 |
ssize_t low = -1, high = -1; |
| 113 |
626 |
struct vrg_priv *vrg_priv; |
| 114 |
626 |
const char *err; |
| 115 |
|
|
| 116 |
626 |
AN(clen); |
| 117 |
626 |
err = http_GetRange(req->http, &low, &high, *clen); |
| 118 |
626 |
if (err != NULL && *err) |
| 119 |
9 |
return (err); |
| 120 |
|
|
| 121 |
617 |
if (low < 0 || high < 0) |
| 122 |
5 |
return (NULL); // Allow 200 response |
| 123 |
|
|
| 124 |
612 |
http_Unset(req->resp, H_Content_Range); |
| 125 |
612 |
if (*clen >= 0) { |
| 126 |
603 |
http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd", |
| 127 |
603 |
(intmax_t)low, (intmax_t)high, *clen); |
| 128 |
603 |
*clen = (intmax_t)(1 + high - low); |
| 129 |
603 |
} else |
| 130 |
9 |
http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/*", |
| 131 |
9 |
(intmax_t)low, (intmax_t)high); |
| 132 |
|
|
| 133 |
612 |
vrg_priv = WS_Alloc(req->ws, sizeof *vrg_priv); |
| 134 |
612 |
if (vrg_priv == NULL) |
| 135 |
0 |
return ("WS too small"); |
| 136 |
|
|
| 137 |
612 |
INIT_OBJ(vrg_priv, VRG_PRIV_MAGIC); |
| 138 |
612 |
vrg_priv->req = req; |
| 139 |
612 |
vrg_priv->range_off = 0; |
| 140 |
612 |
vrg_priv->range_low = low; |
| 141 |
612 |
vrg_priv->range_high = high + 1; |
| 142 |
612 |
*priv = vrg_priv; |
| 143 |
612 |
http_PutResponse(req->resp, "HTTP/1.1", 206, NULL); |
| 144 |
612 |
return (NULL); |
| 145 |
612 |
} |
| 146 |
|
|
| 147 |
|
/* |
| 148 |
|
* return 1 if range should be observed, based on if-range value |
| 149 |
|
* if-range can either be a date or an ETag [RFC7233 3.2 p8] |
| 150 |
|
*/ |
| 151 |
|
static int |
| 152 |
|
vrg_ifrange(struct req *req) |
| 153 |
633 |
{ |
| 154 |
633 |
const char *p, *e; |
| 155 |
633 |
vtim_real ims, lm, d; |
| 156 |
|
|
| 157 |
633 |
if (!http_GetHdr(req->http, H_If_Range, &p)) // rfc7233,l,455,456 |
| 158 |
624 |
return (1); |
| 159 |
|
|
| 160 |
|
/* strong validation needed */ |
| 161 |
9 |
if (p[0] == 'W' && p[1] == '/') // rfc7233,l,500,501 |
| 162 |
0 |
return (0); |
| 163 |
|
|
| 164 |
|
/* ETag */ |
| 165 |
9 |
if (p[0] == '"') { // rfc7233,l,512,514 |
| 166 |
4 |
if (!http_GetHdr(req->resp, H_ETag, &e)) |
| 167 |
2 |
return (0); |
| 168 |
2 |
if ((e[0] == 'W' && e[1] == '/')) // rfc7232,l,547,548 |
| 169 |
0 |
return (0); |
| 170 |
|
/* XXX: should we also have http_etag_cmp() ? */ |
| 171 |
2 |
return (vstrcmp(p, e) == 0); // rfc7232,l,548,548 |
| 172 |
2 |
} |
| 173 |
|
|
| 174 |
|
/* assume date, strong check [RFC7232 2.2.2 p7] */ |
| 175 |
5 |
ims = VTIM_parse(p); |
| 176 |
5 |
if (!ims) // rfc7233,l,502,512 |
| 177 |
0 |
return (0); |
| 178 |
|
|
| 179 |
|
/* the response needs a Date */ |
| 180 |
|
// rfc7232,l,439,440 |
| 181 |
5 |
if (!http_GetHdr(req->resp, H_Date, &p)) |
| 182 |
0 |
return (0); |
| 183 |
5 |
d = VTIM_parse(p); |
| 184 |
5 |
if (!d) |
| 185 |
0 |
return (0); |
| 186 |
|
|
| 187 |
|
|
| 188 |
|
/* grab the Last Modified value */ |
| 189 |
5 |
if (!http_GetHdr(req->resp, H_Last_Modified, &p)) |
| 190 |
2 |
return (0); |
| 191 |
|
|
| 192 |
3 |
lm = VTIM_parse(p); |
| 193 |
3 |
if (!lm) |
| 194 |
0 |
return (0); |
| 195 |
|
|
| 196 |
|
/* Last Modified must be 60 seconds older than Date */ |
| 197 |
3 |
if (lm > d + 60) // rfc7232,l,442,443 |
| 198 |
0 |
return (0); |
| 199 |
|
|
| 200 |
3 |
if (lm != ims) // rfc7233,l,455,456 |
| 201 |
2 |
return (0); |
| 202 |
1 |
return (1); |
| 203 |
3 |
} |
| 204 |
|
|
| 205 |
|
static int v_matchproto_(vdp_init_f) |
| 206 |
|
vrg_range_init(VRT_CTX, struct vdp_ctx *vdc, void **priv) |
| 207 |
633 |
{ |
| 208 |
633 |
const char *err; |
| 209 |
|
|
| 210 |
633 |
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC); |
| 211 |
633 |
CHECK_OBJ_ORNULL(ctx->req, REQ_MAGIC); |
| 212 |
633 |
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC); |
| 213 |
633 |
AN(priv); |
| 214 |
|
|
| 215 |
633 |
if (ctx->req == NULL) { |
| 216 |
0 |
VSLb(vdc->vsl, SLT_Error, |
| 217 |
0 |
"range can only be used on the client side"); |
| 218 |
0 |
return (1); |
| 219 |
0 |
} |
| 220 |
|
|
| 221 |
633 |
AN(vdc->clen); |
| 222 |
|
|
| 223 |
|
// not using vdc->{hd,cl}, because range needs req anyway for Req_Fail() |
| 224 |
|
|
| 225 |
633 |
if (!vrg_ifrange(ctx->req)) // rfc7233,l,455,456 |
| 226 |
7 |
return (1); |
| 227 |
626 |
err = vrg_dorange(ctx->req, vdc->clen, priv); |
| 228 |
626 |
if (err == NULL) |
| 229 |
617 |
return (*priv == NULL ? 1 : 0); |
| 230 |
|
|
| 231 |
9 |
VSLb(vdc->vsl, SLT_Debug, "RANGE_FAIL %s", err); |
| 232 |
9 |
http_Unset(ctx->req->resp, H_Content_Range); |
| 233 |
9 |
if (*vdc->clen >= 0) |
| 234 |
9 |
http_PrintfHeader(ctx->req->resp, |
| 235 |
9 |
"Content-Range: bytes */%jd", *vdc->clen); |
| 236 |
9 |
http_PutResponse(ctx->req->resp, "HTTP/1.1", 416, NULL); |
| 237 |
|
/* |
| 238 |
|
* XXX: We ought to produce a body explaining things. |
| 239 |
|
* XXX: That really calls for us to hit vcl_synth{} |
| 240 |
|
*/ |
| 241 |
9 |
*vdc->clen = 0; |
| 242 |
9 |
return (1); |
| 243 |
626 |
} |
| 244 |
|
|
| 245 |
|
static int v_matchproto_(vdpio_init_f) |
| 246 |
|
vrg_range_io_init(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity) |
| 247 |
576 |
{ |
| 248 |
576 |
int r = vrg_range_init(ctx, vdc, priv); |
| 249 |
|
|
| 250 |
576 |
if (r == 0) |
| 251 |
576 |
return (capacity); |
| 252 |
0 |
return (-1); |
| 253 |
576 |
} |
| 254 |
|
|
| 255 |
|
static int v_matchproto_(vdpio_init_f) |
| 256 |
|
vrg_range_io_upgrade(VRT_CTX, struct vdp_ctx *vdc, void **priv, int capacity) |
| 257 |
0 |
{ |
| 258 |
|
|
| 259 |
0 |
(void)ctx; |
| 260 |
0 |
(void)vdc; |
| 261 |
0 |
(void)priv; |
| 262 |
|
|
| 263 |
0 |
return (capacity); |
| 264 |
0 |
} |
| 265 |
|
|
| 266 |
|
static int v_matchproto_(vdpio_lease_f) |
| 267 |
|
vrg_range_io_lease(struct vdp_ctx *vdc, struct vdp_entry *this, struct vscarab *out) |
| 268 |
6457 |
{ |
| 269 |
6457 |
struct vrg_priv *vrg_priv; |
| 270 |
6457 |
struct viov *v; |
| 271 |
6457 |
ssize_t l, ll; |
| 272 |
6457 |
int r; |
| 273 |
|
|
| 274 |
6457 |
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC); |
| 275 |
6457 |
CHECK_OBJ_NOTNULL(this, VDP_ENTRY_MAGIC); |
| 276 |
6457 |
VSCARAB_CHECK(out); |
| 277 |
|
|
| 278 |
6457 |
CAST_OBJ_NOTNULL(vrg_priv, this->priv, VRG_PRIV_MAGIC); |
| 279 |
|
|
| 280 |
6457 |
this->calls++; |
| 281 |
|
|
| 282 |
6457 |
if (vrg_priv->range_off >= vrg_priv->range_high) { |
| 283 |
960 |
out->flags |= VSCARAB_F_END; |
| 284 |
960 |
return (0); |
| 285 |
960 |
} |
| 286 |
|
// vdpio_pull*() guarantees |
| 287 |
5497 |
assert(out->used < out->capacity); |
| 288 |
|
|
| 289 |
|
// ensure we do not pull more than we can return |
| 290 |
5497 |
VSCARAB_LOCAL(in, out->capacity - out->used); |
| 291 |
|
|
| 292 |
5720 |
while (vrg_priv->range_off < vrg_priv->range_low) { |
| 293 |
3567 |
r = vdpio_pull(vdc, this, in); |
| 294 |
3567 |
out->flags |= in->flags; |
| 295 |
3567 |
if (r <= 0) |
| 296 |
3344 |
goto out; |
| 297 |
223 |
l = vrg_priv->range_low - vrg_priv->range_off; |
| 298 |
336 |
VSCARAB_FOREACH(v, in) { |
| 299 |
336 |
ll = vmin(l, (ssize_t)v->iov.iov_len); |
| 300 |
336 |
this->bytes_in += ll; |
| 301 |
336 |
v->iov.iov_base = (char *)v->iov.iov_base + ll; |
| 302 |
336 |
v->iov.iov_len -= ll; |
| 303 |
336 |
l -= ll; |
| 304 |
336 |
if (l == 0) |
| 305 |
48 |
break; |
| 306 |
336 |
} |
| 307 |
223 |
vrg_priv->range_off = vrg_priv->range_low - l; |
| 308 |
|
|
| 309 |
223 |
vdpio_consolidate_vscarab(vdc, in); |
| 310 |
|
|
| 311 |
223 |
if (l != 0) |
| 312 |
175 |
AZ(in->used); |
| 313 |
48 |
else |
| 314 |
48 |
assert(vrg_priv->range_off == vrg_priv->range_low); |
| 315 |
223 |
} |
| 316 |
|
|
| 317 |
2153 |
assert(vrg_priv->range_off >= vrg_priv->range_low); |
| 318 |
2153 |
assert(vrg_priv->range_off <= vrg_priv->range_high); |
| 319 |
|
|
| 320 |
2153 |
if (in->used == 0) { |
| 321 |
2105 |
r = vdpio_pull(vdc, this, in); |
| 322 |
2105 |
out->flags |= in->flags; |
| 323 |
2105 |
if (r <= 0) |
| 324 |
1997 |
goto out; |
| 325 |
2105 |
} |
| 326 |
|
|
| 327 |
156 |
AN(in->used); |
| 328 |
|
|
| 329 |
156 |
r = 0; |
| 330 |
156 |
l = vrg_priv->range_high - vrg_priv->range_off; |
| 331 |
312 |
VSCARAB_FOREACH(v, in) { |
| 332 |
312 |
this->bytes_in += v->iov.iov_len; |
| 333 |
312 |
vrg_priv->range_off += (ssize_t)v->iov.iov_len; |
| 334 |
312 |
ll = vmin(l, (ssize_t)v->iov.iov_len); |
| 335 |
312 |
v->iov.iov_len = ll; |
| 336 |
312 |
if (ll == 0) |
| 337 |
141 |
vdpio_return_lease(vdc, v->lease); |
| 338 |
171 |
else { |
| 339 |
171 |
VSCARAB_ADD(out, *v); |
| 340 |
171 |
l -= ll; |
| 341 |
171 |
r++; |
| 342 |
171 |
} |
| 343 |
312 |
} |
| 344 |
156 |
if (vrg_priv->range_off >= vrg_priv->range_high) |
| 345 |
96 |
out->flags |= VSCARAB_F_END; |
| 346 |
156 |
return (r); |
| 347 |
|
|
| 348 |
5341 |
out: |
| 349 |
5341 |
assert(r <= 0); |
| 350 |
5341 |
vdpio_return_vscarab(vdc, in); |
| 351 |
5341 |
return (r); |
| 352 |
5341 |
} |
| 353 |
|
|
| 354 |
|
static void v_matchproto_(vdpio_fini_f) |
| 355 |
|
vrg_range_io_fini(struct vdp_ctx *vdc, void **priv) |
| 356 |
576 |
{ |
| 357 |
576 |
AZ(vrg_range_fini(vdc, priv)); |
| 358 |
576 |
} |
| 359 |
|
|
| 360 |
|
const struct vdp VDP_range = { |
| 361 |
|
.name = "range", |
| 362 |
|
.init = vrg_range_init, |
| 363 |
|
.bytes = vrg_range_bytes, |
| 364 |
|
.fini = vrg_range_fini, |
| 365 |
|
|
| 366 |
|
.io_init = vrg_range_io_init, |
| 367 |
|
.io_upgrade = vrg_range_io_upgrade, |
| 368 |
|
.io_lease = vrg_range_io_lease, |
| 369 |
|
.io_fini = vrg_range_io_fini, |
| 370 |
|
}; |
| 371 |
|
|
| 372 |
|
#if !defined(VDPIO_TEST_DRIVER) |
| 373 |
|
|
| 374 |
|
static const char * |
| 375 |
|
http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi, |
| 376 |
|
ssize_t *crl) |
| 377 |
2702 |
{ |
| 378 |
2702 |
const char *b, *t; |
| 379 |
|
|
| 380 |
2702 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 381 |
2702 |
AN(lo); |
| 382 |
2702 |
AN(hi); |
| 383 |
2702 |
AN(crl); |
| 384 |
|
|
| 385 |
2702 |
if (!http_GetHdr(hp, H_Content_Range, &b)) { |
| 386 |
2692 |
return (""); |
| 387 |
2692 |
} |
| 388 |
|
|
| 389 |
10 |
t = strchr(b, ' '); |
| 390 |
10 |
if (t == NULL) |
| 391 |
1 |
return ("Missing space after range unit"); |
| 392 |
|
|
| 393 |
9 |
if (!http_range_at(b, bytes, t - b)) { |
| 394 |
1 |
*crl = -1; |
| 395 |
1 |
return (NULL); // Unknown range unit, ignore |
| 396 |
1 |
} |
| 397 |
8 |
b = t + 1; |
| 398 |
|
|
| 399 |
8 |
if (*b == '*') { // Content-Range: bytes */123 |
| 400 |
3 |
*lo = *hi = -1; |
| 401 |
3 |
b++; |
| 402 |
5 |
} else { // Content-Range: bytes 1-2/3 |
| 403 |
5 |
*lo = VNUM_uint(b, NULL, &b); |
| 404 |
5 |
if (*lo < 0) |
| 405 |
0 |
return ("Bad low number"); |
| 406 |
5 |
if (*b != '-') |
| 407 |
0 |
return ("No hyphen between numbers"); |
| 408 |
5 |
*hi = VNUM_uint(b + 1, NULL, &b); |
| 409 |
5 |
if (*hi < 0) |
| 410 |
0 |
return ("Bad high number"); |
| 411 |
5 |
} |
| 412 |
8 |
if (*b != '/') |
| 413 |
0 |
return ("No slash"); |
| 414 |
8 |
if (b[1] == '*') { // Content-Range: bytes 1-2/* |
| 415 |
1 |
b += 2; |
| 416 |
7 |
} else { |
| 417 |
7 |
*crl = VNUM_uint(b + 1, NULL, &b); |
| 418 |
7 |
if (*crl <= 0) |
| 419 |
0 |
return ("Bad length number"); |
| 420 |
7 |
} |
| 421 |
8 |
while (vct_isows(*b)) |
| 422 |
0 |
b++; |
| 423 |
8 |
if (*b != '\0') |
| 424 |
0 |
return ("Trailing noise"); |
| 425 |
8 |
if (*lo > *hi) |
| 426 |
0 |
return ("low > high"); |
| 427 |
8 |
assert(*crl >= -1); |
| 428 |
8 |
if (*lo >= *crl || *hi >= *crl) |
| 429 |
1 |
return ("low or high > lengtn"); |
| 430 |
7 |
assert(*crl != 0); |
| 431 |
7 |
return (NULL); |
| 432 |
7 |
} |
| 433 |
|
|
| 434 |
|
static const char * |
| 435 |
|
http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi, ssize_t len) |
| 436 |
2754 |
{ |
| 437 |
2754 |
const char *b, *t; |
| 438 |
|
|
| 439 |
2754 |
CHECK_OBJ_NOTNULL(hp, HTTP_MAGIC); |
| 440 |
2754 |
AN(lo); |
| 441 |
2754 |
AN(hi); |
| 442 |
|
|
| 443 |
|
// rfc9110,l,6481,6484 |
| 444 |
2754 |
if (!http_method_eq(hp->wkm, WKM_GET)) |
| 445 |
224 |
return (""); |
| 446 |
|
|
| 447 |
2530 |
if (!http_GetHdr(hp, H_Range, &b)) |
| 448 |
2474 |
return (""); |
| 449 |
|
|
| 450 |
56 |
t = strchr(b, '='); |
| 451 |
56 |
if (t == NULL) |
| 452 |
2 |
return ("Missing '='"); |
| 453 |
|
|
| 454 |
54 |
if (!http_range_at(b, bytes, t - b)) |
| 455 |
4 |
return ("Not Bytes"); |
| 456 |
50 |
b = t + 1; |
| 457 |
|
|
| 458 |
50 |
*lo = VNUM_uint(b, NULL, &b); |
| 459 |
50 |
if (*lo == -2) |
| 460 |
0 |
return ("Low number too big"); |
| 461 |
50 |
if (*b++ != '-') |
| 462 |
0 |
return ("Missing hyphen"); |
| 463 |
|
|
| 464 |
50 |
*hi = VNUM_uint(b, NULL, &b); |
| 465 |
50 |
if (*hi == -2) |
| 466 |
0 |
return ("High number too big"); |
| 467 |
50 |
if (*lo == -1 && *hi == -1) |
| 468 |
1 |
return ("Neither high nor low"); |
| 469 |
49 |
if (*lo == -1 && *hi == 0) |
| 470 |
1 |
return ("No low, high is zero"); |
| 471 |
48 |
if (*hi >= 0 && *hi < *lo) |
| 472 |
1 |
return ("high smaller than low"); |
| 473 |
|
|
| 474 |
48 |
while (vct_isows(*b)) |
| 475 |
1 |
b++; |
| 476 |
47 |
if (*b != '\0') |
| 477 |
1 |
return ("Trailing stuff"); |
| 478 |
|
|
| 479 |
46 |
assert(*lo >= -1); |
| 480 |
46 |
assert(*hi >= -1); |
| 481 |
|
|
| 482 |
46 |
if (*lo < 0) { |
| 483 |
4 |
assert(*hi > 0); |
| 484 |
4 |
*lo = len - *hi; |
| 485 |
4 |
if (*lo < 0) |
| 486 |
2 |
*lo = 0; |
| 487 |
4 |
*hi = len - 1; |
| 488 |
42 |
} else if (len >= 0 && (*hi >= len || *hi < 0)) { |
| 489 |
9 |
*hi = len - 1; |
| 490 |
9 |
} |
| 491 |
|
|
| 492 |
46 |
if (len <= 0) |
| 493 |
18 |
return (NULL); // Allow 200 response |
| 494 |
|
|
| 495 |
28 |
if (*lo >= len) |
| 496 |
1 |
return ("low range beyond object"); |
| 497 |
|
|
| 498 |
27 |
return (NULL); |
| 499 |
28 |
} |
| 500 |
|
|
| 501 |
|
/*--------------------------------------------------------------------*/ |
| 502 |
|
|
| 503 |
|
int |
| 504 |
|
VRG_CheckBo(struct busyobj *bo) |
| 505 |
2708 |
{ |
| 506 |
2708 |
ssize_t rlo = -1, rhi = -1, crlo = -1 , crhi = -1, crlen = -1, clen; |
| 507 |
2708 |
const char *err_r, *err_cr; |
| 508 |
|
|
| 509 |
2708 |
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC); |
| 510 |
|
|
| 511 |
2708 |
if (!cache_param->http_range_support) |
| 512 |
4 |
return (0); |
| 513 |
|
|
| 514 |
2704 |
err_r = http_GetRange(bo->bereq, &rlo, &rhi, -1); |
| 515 |
2704 |
if (err_r != NULL && *err_r) { |
| 516 |
2 |
VSLb(bo->vsl, SLT_Error, "Invalid range header (%s)", err_r); |
| 517 |
2 |
return (-1); |
| 518 |
2 |
} |
| 519 |
|
|
| 520 |
2702 |
err_cr = http_GetContentRange(bo->beresp, &crlo, &crhi, &crlen); |
| 521 |
2702 |
if (err_cr != NULL && *err_cr) { |
| 522 |
2 |
VSLb(bo->vsl, SLT_Error, |
| 523 |
2 |
"Invalid content-range header (%s)", err_cr); |
| 524 |
2 |
return (-1); |
| 525 |
2 |
} |
| 526 |
|
|
| 527 |
2700 |
if (err_r != NULL && err_cr != NULL) // Neither header present |
| 528 |
2692 |
return (0); |
| 529 |
|
|
| 530 |
8 |
clen = http_GetContentLength(bo->beresp); |
| 531 |
8 |
if (clen < -1) { |
| 532 |
0 |
VSLb(bo->vsl, SLT_Error, "Invalid content-length header"); |
| 533 |
0 |
return (-1); |
| 534 |
0 |
} |
| 535 |
|
|
| 536 |
8 |
if (crlen < 0 && crlo < 0 && crhi < 0) { |
| 537 |
1 |
VSLb(bo->vsl, SLT_Debug, |
| 538 |
1 |
"Missing content-range header or unknown range unit"); |
| 539 |
1 |
return (0); |
| 540 |
1 |
} |
| 541 |
|
|
| 542 |
7 |
if (rlo < 0 && rhi < 0) { |
| 543 |
2 |
VSLb(bo->vsl, SLT_Error, "Unexpected content-range header"); |
| 544 |
2 |
return (-1); |
| 545 |
2 |
} |
| 546 |
|
|
| 547 |
5 |
if (crlo < 0) { // Content-Range: bytes */123 |
| 548 |
1 |
assert(crhi < 0); |
| 549 |
1 |
assert(crlen > 0); |
| 550 |
1 |
if (http_GetStatus(bo->beresp) == 416) |
| 551 |
1 |
return (0); |
| 552 |
0 |
crlo = 0; |
| 553 |
0 |
crhi = crlen - 1; |
| 554 |
0 |
} |
| 555 |
|
|
| 556 |
4 |
#define RANGE_CHECK(val, op, crval, what) \ |
| 557 |
8 |
do { \ |
| 558 |
8 |
if (val >= 0 && !(val op crval)) { \ |
| 559 |
2 |
VSLb(bo->vsl, SLT_Error, \ |
| 560 |
2 |
"Expected " what " %zd, got %zd", \ |
| 561 |
2 |
crval, val); \ |
| 562 |
2 |
return (-1); \ |
| 563 |
2 |
} \ |
| 564 |
8 |
} while (0) |
| 565 |
|
|
| 566 |
4 |
crlen = (crhi - crlo) + 1; |
| 567 |
4 |
RANGE_CHECK(clen, ==, crlen, "content length"); |
| 568 |
|
|
| 569 |
|
/* NB: if the client didn't specify a low range the high range |
| 570 |
|
* was adjusted based on the resource length, and a high range |
| 571 |
|
* is allowed to be out of bounds so at this point there is |
| 572 |
|
* nothing left to check. |
| 573 |
|
*/ |
| 574 |
2 |
if (rlo < 0) |
| 575 |
0 |
return (0); |
| 576 |
|
|
| 577 |
2 |
RANGE_CHECK(rlo, ==, crlo, "low range"); |
| 578 |
2 |
RANGE_CHECK(rhi, >=, crhi, "minimum high range"); |
| 579 |
2 |
#undef RANGE_CHECK |
| 580 |
|
|
| 581 |
2 |
return (0); |
| 582 |
2 |
} |
| 583 |
|
|
| 584 |
|
|
| 585 |
|
#elif defined(VDPIO_TEST_DRIVER) |
| 586 |
|
|
| 587 |
|
#include <stdio.h> |
| 588 |
|
#include <stdlib.h> |
| 589 |
|
#include "vdpio_test.h" |
| 590 |
|
#include "vfil.h" |
| 591 |
|
|
| 592 |
|
// the vdpio test does not care about headers, they are tested in vtcs |
| 593 |
|
void |
| 594 |
|
http_PrintfHeader(struct http *to, const char *fmt, ...) |
| 595 |
576 |
{ |
| 596 |
576 |
(void)to; |
| 597 |
576 |
(void)fmt; |
| 598 |
576 |
} |
| 599 |
|
|
| 600 |
|
int |
| 601 |
|
http_GetHdr(const struct http *hp, hdr_t t, const char **ptr) |
| 602 |
576 |
{ |
| 603 |
576 |
(void)hp; |
| 604 |
576 |
(void)t; |
| 605 |
576 |
(void)ptr; |
| 606 |
576 |
return (0); |
| 607 |
576 |
} |
| 608 |
|
|
| 609 |
|
static ssize_t t_lo, t_hi; |
| 610 |
|
const char * |
| 611 |
|
http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi, ssize_t len) |
| 612 |
576 |
{ |
| 613 |
576 |
(void)hp; |
| 614 |
576 |
AN(lo); |
| 615 |
576 |
AN(hi); |
| 616 |
576 |
(void)len; |
| 617 |
576 |
*lo = t_lo; |
| 618 |
576 |
*hi = t_hi; |
| 619 |
576 |
return (NULL); |
| 620 |
576 |
} |
| 621 |
|
|
| 622 |
|
void |
| 623 |
|
Req_Fail(struct req *req, stream_close_t reason) |
| 624 |
480 |
{ |
| 625 |
480 |
(void)req; |
| 626 |
480 |
(void)reason; |
| 627 |
480 |
} |
| 628 |
|
|
| 629 |
|
const struct stream_close SC_RANGE_SHORT[1]; |
| 630 |
|
|
| 631 |
|
void |
| 632 |
|
http_PutResponse(struct http *to, const char *proto, uint16_t status, |
| 633 |
|
const char *response) |
| 634 |
576 |
{ |
| 635 |
576 |
(void)to; |
| 636 |
576 |
(void)proto; |
| 637 |
576 |
(void)status; |
| 638 |
576 |
(void)response; |
| 639 |
576 |
} |
| 640 |
|
|
| 641 |
|
int |
| 642 |
|
main(int argc, char *const *argv) |
| 643 |
1 |
{ |
| 644 |
1 |
char *data; |
| 645 |
|
|
| 646 |
1 |
VFLA_LOCAL(vdpio_test, t, VDPIO_TEST_MAGIC, tc, 2); |
| 647 |
1 |
t->vdp = &VDP_range; |
| 648 |
|
|
| 649 |
1 |
AN(argc); |
| 650 |
|
|
| 651 |
1 |
data = VFIL_readfile(NULL, argv[0], &t->datasz); |
| 652 |
1 |
AN(data); |
| 653 |
1 |
t->data = data; |
| 654 |
|
|
| 655 |
4 |
#define RGT(name, lo, hi) do { \ |
| 656 |
4 |
printf("%s:\n", name); \ |
| 657 |
4 |
t_lo = (lo); \ |
| 658 |
4 |
t_hi = (hi); \ |
| 659 |
4 |
t->expect = data + t_lo; \ |
| 660 |
4 |
t->expectsz = (t_hi - t_lo) + 1; \ |
| 661 |
4 |
AZ(vdpio_mock(t)); \ |
| 662 |
4 |
} while(0) |
| 663 |
|
|
| 664 |
1 |
RGT("1st quarter", 0, t->datasz / 4 - 1); |
| 665 |
1 |
RGT("2nd quarter", t->datasz / 4, t->datasz / 2 - 1); |
| 666 |
1 |
RGT("last byte", t->datasz - 1, t->datasz -1); |
| 667 |
|
|
| 668 |
|
// injection is in first quarter, so can not test |
| 669 |
1 |
t->no_injection = 1; |
| 670 |
1 |
RGT("first byte", 0, 0); |
| 671 |
|
|
| 672 |
1 |
free(data); |
| 673 |
1 |
} |
| 674 |
|
|
| 675 |
|
#endif |