Commit a4d271de authored by Lasse Karstensen's avatar Lasse Karstensen

Add Range error handling with HTTP 416.

A set of robustness fixes for Range handling backported from master.

Patch by Federico G. Schwindt.
parent 13d7ff6f
......@@ -101,14 +101,12 @@ v1d_dorange(struct req *req, struct busyobj *bo, const char *r)
else
len = req->obj->len;
if (strncmp(r, "bytes=", 6))
if (strncasecmp(r, "bytes=", 6))
return;
r += 6;
/* The low end of range */
has_low = low = 0;
if (!vct_isdigit(*r) && *r != '-')
return;
while (vct_isdigit(*r)) {
has_low = 1;
low *= 10;
......@@ -116,9 +114,6 @@ v1d_dorange(struct req *req, struct busyobj *bo, const char *r)
r++;
}
if (low >= len)
return;
if (*r != '-')
return;
r++;
......@@ -131,22 +126,31 @@ v1d_dorange(struct req *req, struct busyobj *bo, const char *r)
high += *r - '0';
r++;
}
if (high < low)
return;
if (!has_low) {
low = len - high;
if (low < 0)
low = 0;
high = len - 1;
}
} else
} else if (high >= len)
high = len - 1;
} else if (has_low)
high = len - 1;
if (*r != '\0')
else
return;
if (high >= len)
high = len - 1;
if (*r != '\0')
return;
if (low > high)
if (low >= len) {
http_PrintfHeader(req->resp, "Content-Range: bytes */%jd",
(intmax_t)len);
http_Unset(req->resp, H_Content_Length);
http_PutResponse(req->resp, "HTTP/1.1", 416, NULL);
req->wantbody = 0;
return;
}
http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd",
(intmax_t)low, (intmax_t)high, (intmax_t)len);
......
......@@ -5,13 +5,11 @@ server s1 {
txresp -bodylen 100
} -start
varnish v1 -vcl+backend {
varnish v1 -arg "-p http_range_support=off" -vcl+backend {
sub vcl_backend_response {
set beresp.do_stream = false;
}
} -start
varnish v1 -cliok "param.set http_range_support off"
client c1 {
txreq -hdr "Range: bytes=0-9"
......@@ -45,61 +43,71 @@ client c1 {
expect resp.status == 200
expect resp.bodylen == 100
txreq -hdr "Range: bytes=-"
rxresp
expect resp.status == 200
expect resp.bodylen == 100
txreq -hdr "Range: bytes=5-2"
rxresp
expect resp.status == 200
expect resp.bodylen == 100
} -run
varnish v1 -expect s_resp_bodybytes == 500
varnish v1 -expect s_resp_bodybytes == 700
client c1 {
txreq -hdr "Range: bytes=0-9"
txreq -hdr "Range: bytes=-0"
rxresp
expect resp.status == 206
expect resp.bodylen == 10
expect resp.status == 416
expect resp.bodylen == 0
expect resp.http.content-range == "bytes */100"
txreq -hdr "Range: bytes=10-19"
txreq -hdr "Range: bytes=100-"
rxresp
expect resp.status == 206
expect resp.bodylen == 10
expect resp.status == 416
expect resp.bodylen == 0
expect resp.http.content-range == "bytes */100"
} -run
txreq -hdr "Range: bytes=90-"
rxresp
expect resp.status == 206
expect resp.bodylen == 10
varnish v1 -expect s_resp_bodybytes == 700
txreq -hdr "Range: bytes=-9"
client c1 {
txreq -hdr "Range: bytes=0-49"
rxresp
expect resp.status == 206
expect resp.bodylen == 9
expect resp.bodylen == 50
expect resp.http.content-range == "bytes 0-49/100"
txreq -hdr "Range: bytes=-"
txreq -hdr "Range: bytes=50-99"
rxresp
expect resp.status == 206
expect resp.bodylen == 100
txreq -hdr "Range: bytes=102-102"
rxresp
expect resp.status == 200
expect resp.bodylen == 100
expect resp.bodylen == 50
expect resp.http.content-range == "bytes 50-99/100"
txreq -hdr "Range: bytes=99-"
txreq -hdr "Range: bytes=-50"
rxresp
expect resp.status == 206
expect resp.bodylen == 1
expect resp.bodylen == 50
expect resp.http.content-range == "bytes 50-99/100"
txreq -hdr "Range: bytes=99-70"
txreq -hdr "Range: bytes=50-"
rxresp
expect resp.status == 200
expect resp.bodylen == 100
expect resp.status == 206
expect resp.bodylen == 50
expect resp.http.content-range == "bytes 50-99/100"
txreq -hdr "Range: bytes=-"
txreq -hdr "Range: bytes=0-0"
rxresp
expect resp.status == 206
expect resp.bodylen == 100
expect resp.bodylen == 1
expect resp.http.content-range == "bytes 0-0/100"
txreq -hdr "Range: bytes=-101"
txreq -hdr "Range: bytes=-2000"
rxresp
expect resp.status == 206
expect resp.bodylen == 100
expect resp.http.content-range == "bytes 0-99/100"
} -run
varnish v1 -expect s_resp_bodybytes == 1040
varnish v1 -expect s_resp_bodybytes == 1001
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment