Commit 6066a2f3 authored by Federico G. Schwindt's avatar Federico G. Schwindt

Improve Range handling

Add support for 416 and fix some corner cases.
parent 165b4caf
...@@ -98,14 +98,12 @@ VRG_dorange(struct req *req, struct busyobj *bo, const char *r) ...@@ -98,14 +98,12 @@ VRG_dorange(struct req *req, struct busyobj *bo, const char *r)
else else
len = ObjGetLen(req->wrk, req->objcore); len = ObjGetLen(req->wrk, req->objcore);
if (strncmp(r, "bytes=", 6)) if (strncasecmp(r, "bytes=", 6))
return; return;
r += 6; r += 6;
/* The low end of range */ /* The low end of range */
has_low = low = 0; has_low = low = 0;
if (!vct_isdigit(*r) && *r != '-')
return;
while (vct_isdigit(*r)) { while (vct_isdigit(*r)) {
has_low = 1; has_low = 1;
low *= 10; low *= 10;
...@@ -113,9 +111,6 @@ VRG_dorange(struct req *req, struct busyobj *bo, const char *r) ...@@ -113,9 +111,6 @@ VRG_dorange(struct req *req, struct busyobj *bo, const char *r)
r++; r++;
} }
if (low >= len)
return;
if (*r != '-') if (*r != '-')
return; return;
r++; r++;
...@@ -128,22 +123,31 @@ VRG_dorange(struct req *req, struct busyobj *bo, const char *r) ...@@ -128,22 +123,31 @@ VRG_dorange(struct req *req, struct busyobj *bo, const char *r)
high += *r - '0'; high += *r - '0';
r++; r++;
} }
if (high < low)
return;
if (!has_low) { if (!has_low) {
low = len - high; low = len - high;
if (low < 0) if (low < 0)
low = 0; low = 0;
high = len - 1; high = len - 1;
} } else if (high >= len)
} else high = len - 1;
} else if (has_low)
high = len - 1; high = len - 1;
if (*r != '\0') else
return; return;
if (high >= len) if (*r != '\0')
high = len - 1; 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; return;
}
http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd", http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd",
(intmax_t)low, (intmax_t)high, (intmax_t)len); (intmax_t)low, (intmax_t)high, (intmax_t)len);
......
...@@ -5,13 +5,11 @@ server s1 { ...@@ -5,13 +5,11 @@ server s1 {
txresp -bodylen 100 txresp -bodylen 100
} -start } -start
varnish v1 -vcl+backend { varnish v1 -arg "-p http_range_support=off" -vcl+backend {
sub vcl_backend_response { sub vcl_backend_response {
set beresp.do_stream = false; set beresp.do_stream = false;
} }
} -start } -start
varnish v1 -cliok "param.set http_range_support off"
client c1 { client c1 {
txreq -hdr "Range: bytes=0-9" txreq -hdr "Range: bytes=0-9"
...@@ -45,61 +43,71 @@ client c1 { ...@@ -45,61 +43,71 @@ client c1 {
expect resp.status == 200 expect resp.status == 200
expect resp.bodylen == 100 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 } -run
varnish v1 -expect s_resp_bodybytes == 500 varnish v1 -expect s_resp_bodybytes == 700
client c1 { client c1 {
txreq -hdr "Range: bytes=-0"
txreq -hdr "Range: bytes=0-9"
rxresp rxresp
expect resp.status == 206 expect resp.status == 416
expect resp.bodylen == 10 expect resp.bodylen == 0
expect resp.http.content-range == "bytes */100"
txreq -hdr "Range: bytes=10-19" txreq -hdr "Range: bytes=100-"
rxresp rxresp
expect resp.status == 206 expect resp.status == 416
expect resp.bodylen == 10 expect resp.bodylen == 0
expect resp.http.content-range == "bytes */100"
} -run
txreq -hdr "Range: bytes=90-" varnish v1 -expect s_resp_bodybytes == 700
rxresp
expect resp.status == 206
expect resp.bodylen == 10
txreq -hdr "Range: bytes=-9" client c1 {
txreq -hdr "Range: bytes=0-49"
rxresp rxresp
expect resp.status == 206 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 rxresp
expect resp.status == 206 expect resp.status == 206
expect resp.bodylen == 100 expect resp.bodylen == 50
expect resp.http.content-range == "bytes 50-99/100"
txreq -hdr "Range: bytes=102-102"
rxresp
expect resp.status == 200
expect resp.bodylen == 100
txreq -hdr "Range: bytes=99-" txreq -hdr "Range: bytes=-50"
rxresp rxresp
expect resp.status == 206 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 rxresp
expect resp.status == 200 expect resp.status == 206
expect resp.bodylen == 100 expect resp.bodylen == 50
expect resp.http.content-range == "bytes 50-99/100"
txreq -hdr "Range: bytes=-" txreq -hdr "Range: bytes=0-0"
rxresp rxresp
expect resp.status == 206 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 rxresp
expect resp.status == 206 expect resp.status == 206
expect resp.bodylen == 100 expect resp.bodylen == 100
expect resp.http.content-range == "bytes 0-99/100"
} -run } -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