1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321
|
/*-
* Copyright (c) 2006 Verdens Gang AS
* Copyright (c) 2006-2015 Varnish Software AS
* All rights reserved.
*
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include "config.h"
#include "cache_varnishd.h"
#include "cache_filter.h"
#include "vct.h"
#include <vtim.h>
/*--------------------------------------------------------------------*/
struct vrg_priv {
unsigned magic;
#define VRG_PRIV_MAGIC 0xb886e711
struct req *req;
ssize_t range_low;
ssize_t range_high;
ssize_t range_off;
};
static int v_matchproto_(vdp_fini_f)
vrg_range_fini(struct vdp_ctx *vdc, void **priv)
{
struct vrg_priv *vrg_priv;
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
TAKE_OBJ_NOTNULL(vrg_priv, priv, VRG_PRIV_MAGIC);
if (vrg_priv->req->resp_len >= 0 &&
vrg_priv->range_off < vrg_priv->range_high) {
Req_Fail(vrg_priv->req, SC_RANGE_SHORT);
vrg_priv->req->vdc->retval = -1;
}
/* struct on ws, no need to free */
return (0);
}
static int v_matchproto_(vdp_bytes_f)
vrg_range_bytes(struct vdp_ctx *vdc, enum vdp_action act, void **priv,
const void *ptr, ssize_t len)
{
int retval = 0;
ssize_t l = 0;
const char *p = ptr;
struct vrg_priv *vrg_priv;
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
AN(priv);
CAST_OBJ_NOTNULL(vrg_priv, *priv, VRG_PRIV_MAGIC);
if (ptr != NULL) {
l = vrg_priv->range_low - vrg_priv->range_off;
if (l > 0) {
if (l > len)
l = len;
vrg_priv->range_off += l;
p += l;
len -= l;
}
l = vmin(vrg_priv->range_high - vrg_priv->range_off, len);
vrg_priv->range_off += len;
if (vrg_priv->range_off >= vrg_priv->range_high)
act = VDP_END;
}
if (l > 0)
retval = VDP_bytes(vdc, act, p, l);
else if (l == 0 && act > VDP_NULL)
retval = VDP_bytes(vdc, act, p, 0);
return (retval || act == VDP_END ? 1 : 0);
}
/*--------------------------------------------------------------------*/
static const char *
vrg_dorange(struct req *req, void **priv)
{
ssize_t low, high;
struct vrg_priv *vrg_priv;
const char *err;
err = http_GetRange(req->http, &low, &high, req->resp_len);
if (err != NULL)
return (err);
if (low < 0 || high < 0)
return (NULL); // Allow 200 response
if (req->resp_len >= 0) {
http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd",
(intmax_t)low, (intmax_t)high, (intmax_t)req->resp_len);
req->resp_len = (intmax_t)(1 + high - low);
} else
http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/*",
(intmax_t)low, (intmax_t)high);
vrg_priv = WS_Alloc(req->ws, sizeof *vrg_priv);
if (vrg_priv == NULL)
return ("WS too small");
INIT_OBJ(vrg_priv, VRG_PRIV_MAGIC);
vrg_priv->req = req;
vrg_priv->range_off = 0;
vrg_priv->range_low = low;
vrg_priv->range_high = high + 1;
*priv = vrg_priv;
http_PutResponse(req->resp, "HTTP/1.1", 206, NULL);
return (NULL);
}
/*
* return 1 if range should be observed, based on if-range value
* if-range can either be a date or an ETag [RFC7233 3.2 p8]
*/
static int
vrg_ifrange(struct req *req)
{
const char *p, *e;
vtim_real ims, lm, d;
if (!http_GetHdr(req->http, H_If_Range, &p)) // rfc7233,l,455,456
return (1);
/* strong validation needed */
if (p[0] == 'W' && p[1] == '/') // rfc7233,l,500,501
return (0);
/* ETag */
if (p[0] == '"') { // rfc7233,l,512,514
if (!http_GetHdr(req->resp, H_ETag, &e))
return (0);
if ((e[0] == 'W' && e[1] == '/')) // rfc7232,l,547,548
return (0);
/* XXX: should we also have http_etag_cmp() ? */
return (strcmp(p, e) == 0); // rfc7232,l,548,548
}
/* assume date, strong check [RFC7232 2.2.2 p7] */
ims = VTIM_parse(p);
if (!ims) // rfc7233,l,502,512
return (0);
/* the response needs a Date */
// rfc7232,l,439,440
if (!http_GetHdr(req->resp, H_Date, &p))
return (0);
d = VTIM_parse(p);
if (!d)
return (0);
/* grab the Last Modified value */
if (!http_GetHdr(req->resp, H_Last_Modified, &p))
return (0);
lm = VTIM_parse(p);
if (!lm)
return (0);
/* Last Modified must be 60 seconds older than Date */
if (lm > d + 60) // rfc7232,l,442,443
return (0);
if (lm != ims) // rfc7233,l,455,456
return (0);
return (1);
}
static int v_matchproto_(vdp_init_f)
vrg_range_init(VRT_CTX, struct vdp_ctx *vdc, void **priv)
{
const char *err;
CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
CHECK_OBJ_ORNULL(ctx->req, REQ_MAGIC);
CHECK_OBJ_NOTNULL(vdc, VDP_CTX_MAGIC);
AN(priv);
if (ctx->req == NULL) {
VSLb(vdc->vsl, SLT_Error,
"range can only be used on the client side");
return (1);
}
// not using vdc->{hd,cl}, because range needs req anyway for Req_Fail()
if (!vrg_ifrange(ctx->req)) // rfc7233,l,455,456
return (1);
err = vrg_dorange(ctx->req, priv);
if (err == NULL)
return (*priv == NULL ? 1 : 0);
VSLb(vdc->vsl, SLT_Debug, "RANGE_FAIL %s", err);
if (ctx->req->resp_len >= 0)
http_PrintfHeader(ctx->req->resp,
"Content-Range: bytes */%jd",
(intmax_t)ctx->req->resp_len);
http_PutResponse(ctx->req->resp, "HTTP/1.1", 416, NULL);
/*
* XXX: We ought to produce a body explaining things.
* XXX: That really calls for us to hit vcl_synth{}
*/
ctx->req->resp_len = 0;
return (1);
}
const struct vdp VDP_range = {
.name = "range",
.init = vrg_range_init,
.bytes = vrg_range_bytes,
.fini = vrg_range_fini,
};
/*--------------------------------------------------------------------*/
int
VRG_CheckBo(struct busyobj *bo)
{
ssize_t rlo, rhi, crlo, crhi, crlen, clen;
const char *err;
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC);
if (!cache_param->http_range_support)
return (0);
err = http_GetRange(bo->bereq0, &rlo, &rhi, -1);
clen = http_GetContentLength(bo->beresp);
crlen = http_GetContentRange(bo->beresp, &crlo, &crhi);
if (err != NULL) {
VSLb(bo->vsl, SLT_Error, "Invalid range header (%s)", err);
return (-1);
}
if (crlen < -1) {
VSLb(bo->vsl, SLT_Error, "Invalid content-range header");
return (-1);
}
if (clen < -1) {
VSLb(bo->vsl, SLT_Error, "Invalid content-length header");
return (-1);
}
if (crlo < 0 && crhi < 0 && crlen < 0) {
VSLb(bo->vsl, SLT_Debug,
"Missing content-range header or unknown range unit");
return (0);
}
if (rlo < 0 && rhi < 0) {
VSLb(bo->vsl, SLT_Error, "Unexpected content-range header");
return (-1);
}
if (crlo < 0) { // Content-Range: bytes */123
assert(crhi < 0);
assert(crlen > 0);
if (http_GetStatus(bo->beresp) == 416)
return (0);
crlo = 0;
crhi = crlen - 1;
}
#define RANGE_CHECK(val, op, crval, what) \
do { \
if (val >= 0 && !(val op crval)) { \
VSLb(bo->vsl, SLT_Error, \
"Expected " what " %zd, got %zd", \
crval, val); \
return (-1); \
} \
} while (0)
crlen = (crhi - crlo) + 1;
RANGE_CHECK(clen, ==, crlen, "content length");
/* NB: if the client didn't specify a low range the high range
* was adjusted based on the resource length, and a high range
* is allowed to be out of bounds so at this point there is
* nothing left to check.
*/
if (rlo < 0)
return (0);
RANGE_CHECK(rlo, ==, crlo, "low range");
RANGE_CHECK(rhi, >=, crhi, "minimum high range");
#undef RANGE_CHECK
return (0);
}
|