Commit e8f56d91 authored by Dridi Boukelmoune's avatar Dridi Boukelmoune

range: Move more parsing logic to http_GetRange()

parent 3e0717e5
...@@ -640,7 +640,8 @@ int http_GetHdrField(const struct http *hp, hdr_t, ...@@ -640,7 +640,8 @@ int http_GetHdrField(const struct http *hp, hdr_t,
double http_GetHdrQ(const struct http *hp, hdr_t, const char *field); double http_GetHdrQ(const struct http *hp, hdr_t, const char *field);
ssize_t http_GetContentLength(const struct http *hp); ssize_t http_GetContentLength(const struct http *hp);
ssize_t http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi); ssize_t http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi);
const char * http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi); const char * http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi,
ssize_t len);
uint16_t http_GetStatus(const struct http *hp); uint16_t http_GetStatus(const struct http *hp);
int http_IsStatus(const struct http *hp, int); int http_IsStatus(const struct http *hp, int);
void http_SetStatus(struct http *to, uint16_t status, const char *reason); void http_SetStatus(struct http *to, uint16_t status, const char *reason);
......
...@@ -958,7 +958,7 @@ http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi) ...@@ -958,7 +958,7 @@ http_GetContentRange(const struct http *hp, ssize_t *lo, ssize_t *hi)
} }
const char * const char *
http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi) http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi, ssize_t len)
{ {
ssize_t tmp_lo, tmp_hi; ssize_t tmp_lo, tmp_hi;
const char *b, *t; const char *b, *t;
...@@ -1003,6 +1003,26 @@ http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi) ...@@ -1003,6 +1003,26 @@ http_GetRange(const struct http *hp, ssize_t *lo, ssize_t *hi)
b++; b++;
if (*b != '\0') if (*b != '\0')
return ("Trailing stuff"); return ("Trailing stuff");
assert(*lo >= -1);
assert(*hi >= -1);
if (len <= 0)
return (NULL); // Allow 200 response
if (*lo < 0) {
assert(*hi > 0);
*lo = len - *hi;
if (*lo < 0)
*lo = 0;
*hi = len - 1;
} else if (len >= 0 && (*hi >= len || *hi < 0)) {
*hi = len - 1;
}
if (*lo >= len)
return ("low range beyond object");
return (NULL); return (NULL);
} }
......
...@@ -108,25 +108,13 @@ vrg_dorange(struct req *req, void **priv) ...@@ -108,25 +108,13 @@ vrg_dorange(struct req *req, void **priv)
struct vrg_priv *vrg_priv; struct vrg_priv *vrg_priv;
const char *err; const char *err;
err = http_GetRange(req->http, &low, &high); err = http_GetRange(req->http, &low, &high, req->resp_len);
if (err != NULL) if (err != NULL)
return (err); return (err);
assert(low >= -1); if (low < 0 || high < 0)
assert(high >= -1); return (NULL); // Allow 200 response
if (low < 0) {
if (req->resp_len < 0 || high < 0)
return (NULL); // Allow 200 response
assert(high > 0);
low = req->resp_len - high;
if (low < 0)
low = 0;
high = req->resp_len - 1;
} else if (req->resp_len >= 0 && (high >= req->resp_len || high < 0))
high = req->resp_len - 1;
else if (high < 0)
return (NULL); // Allow 200 response
/* /*
* else (bo != NULL) { * else (bo != NULL) {
* We assume that the client knows what it's doing and trust * We assume that the client knows what it's doing and trust
...@@ -134,9 +122,6 @@ vrg_dorange(struct req *req, void **priv) ...@@ -134,9 +122,6 @@ vrg_dorange(struct req *req, void **priv)
* } * }
*/ */
if (req->resp_len >= 0 && low >= req->resp_len)
return ("low range beyond object");
if (req->resp_len >= 0) { if (req->resp_len >= 0) {
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)req->resp_len); (intmax_t)low, (intmax_t)high, (intmax_t)req->resp_len);
...@@ -268,7 +253,7 @@ VRG_CheckBo(struct busyobj *bo) ...@@ -268,7 +253,7 @@ VRG_CheckBo(struct busyobj *bo)
if (!cache_param->http_range_support) if (!cache_param->http_range_support)
return (0); return (0);
err = http_GetRange(bo->bereq0, &rlo, &rhi); err = http_GetRange(bo->bereq0, &rlo, &rhi, -1);
clen = http_GetContentLength(bo->beresp); clen = http_GetContentLength(bo->beresp);
crlen = http_GetContentRange(bo->beresp, &crlo, &crhi); crlen = http_GetContentRange(bo->beresp, &crlo, &crhi);
......
...@@ -58,6 +58,7 @@ ...@@ -58,6 +58,7 @@
* binary/load-time compatible, increment MAJOR version * binary/load-time compatible, increment MAJOR version
* *
* NEXT (2023-03-15) * NEXT (2023-03-15)
* [cache.h] http_GetRange() changed
* 16.0 (2022-09-15) * 16.0 (2022-09-15)
* VMOD C-prototypes moved into JSON * VMOD C-prototypes moved into JSON
* VRT_AddVDP() deprecated * VRT_AddVDP() deprecated
......
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