[master] 6503425 Move Range: processing to its own source file
Poul-Henning Kamp
phk at FreeBSD.org
Mon Oct 27 21:21:28 CET 2014
commit 65034251f59181ac35f40ef06926e98951b3cd5b
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date: Mon Oct 27 20:21:11 2014 +0000
Move Range: processing to its own source file
diff --git a/bin/varnishd/Makefile.am b/bin/varnishd/Makefile.am
index a347119..a71e2a6 100644
--- a/bin/varnishd/Makefile.am
+++ b/bin/varnishd/Makefile.am
@@ -36,6 +36,7 @@ varnishd_SOURCES = \
cache/cache_req_body.c \
cache/cache_req_fsm.c \
cache/cache_rfc2616.c \
+ cache/cache_range.c \
cache/cache_session.c \
cache/cache_shmlog.c \
cache/cache_vary.c \
diff --git a/bin/varnishd/cache/cache.h b/bin/varnishd/cache/cache.h
index eaea802..1e0cec2 100644
--- a/bin/varnishd/cache/cache.h
+++ b/bin/varnishd/cache/cache.h
@@ -957,6 +957,9 @@ unsigned V1L_Flush(const struct worker *w);
unsigned V1L_FlushRelease(struct worker *w);
unsigned V1L_Write(const struct worker *w, const void *ptr, int len);
+/* cache_range.c [VRG] */
+void VRG_dorange(struct req *req, struct busyobj *bo, const char *r);
+
/* cache_session.c [SES] */
void SES_Close(struct sess *sp, enum sess_close reason);
void SES_Delete(struct sess *sp, enum sess_close reason, double now);
diff --git a/bin/varnishd/cache/cache_range.c b/bin/varnishd/cache/cache_range.c
new file mode 100644
index 0000000..ed08bf5
--- /dev/null
+++ b/bin/varnishd/cache/cache_range.c
@@ -0,0 +1,163 @@
+/*-
+ * Copyright (c) 2006 Verdens Gang AS
+ * Copyright (c) 2006-2014 Varnish Software AS
+ * All rights reserved.
+ *
+ * Author: Poul-Henning Kamp <phk at phk.freebsd.dk>
+ *
+ * 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/cache.h"
+#include "cache/cache_filter.h"
+
+#include "vct.h"
+
+/*--------------------------------------------------------------------*/
+
+struct vrg_priv {
+ unsigned magic;
+#define VRG_PRIV_MAGIC 0xb886e711
+ ssize_t range_low;
+ ssize_t range_high;
+ ssize_t range_off;
+};
+
+static int __match_proto__(vdp_bytes)
+vrg_range_bytes(struct req *req, enum vdp_action act, void **priv,
+ const void *ptr, ssize_t len)
+{
+ int retval = 0;
+ ssize_t l;
+ const char *p = ptr;
+ struct vrg_priv *vrg_priv;
+
+ CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
+ if (act == VDP_INIT)
+ return (0);
+ if (act == VDP_FINI) {
+ *priv = NULL;
+ return (0);
+ }
+ CAST_OBJ_NOTNULL(vrg_priv, *priv, VRG_PRIV_MAGIC);
+ 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 = vrg_priv->range_high - vrg_priv->range_off;
+ if (l > len)
+ l = len;
+ if (l > 0)
+ retval = VDP_bytes(req, act, p, l);
+ else if (act > VDP_NULL)
+ retval = VDP_bytes(req, act, p, 0);
+ vrg_priv->range_off += len;
+ return (retval);
+}
+
+/*--------------------------------------------------------------------*/
+
+void
+VRG_dorange(struct req *req, struct busyobj *bo, const char *r)
+{
+ ssize_t len, low, high, has_low;
+ struct vrg_priv *vrg_priv;
+
+ CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
+ CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
+ assert(http_IsStatus(req->resp, 200));
+
+ /* We must snapshot the length if we're streaming from the backend */
+ if (bo != NULL)
+ len = VBO_waitlen(req->wrk, bo, -1);
+ else
+ len = ObjGetLen(req->wrk, req->objcore);
+
+ if (strncmp(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;
+ low += *r - '0';
+ r++;
+ }
+
+ if (low >= len)
+ return;
+
+ if (*r != '-')
+ return;
+ r++;
+
+ /* The high end of range */
+ if (vct_isdigit(*r)) {
+ high = 0;
+ while (vct_isdigit(*r)) {
+ high *= 10;
+ high += *r - '0';
+ r++;
+ }
+ if (!has_low) {
+ low = len - high;
+ if (low < 0)
+ low = 0;
+ high = len - 1;
+ }
+ } else
+ high = len - 1;
+ if (*r != '\0')
+ return;
+
+ if (high >= len)
+ high = len - 1;
+
+ if (low > high)
+ return;
+
+ http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd",
+ (intmax_t)low, (intmax_t)high, (intmax_t)len);
+ http_Unset(req->resp, H_Content_Length);
+ if (req->res_mode & RES_LEN)
+ http_PrintfHeader(req->resp, "Content-Length: %jd",
+ (intmax_t)(1 + high - low));
+ http_PutResponse(req->resp, "HTTP/1.1", 206, NULL);
+
+ vrg_priv = WS_Alloc(req->ws, sizeof *vrg_priv);
+ XXXAN(vrg_priv);
+ INIT_OBJ(vrg_priv, VRG_PRIV_MAGIC);
+ vrg_priv->range_off = 0;
+ vrg_priv->range_low = low;
+ vrg_priv->range_high = high + 1;
+ VDP_push(req, vrg_range_bytes, vrg_priv, 0);
+}
diff --git a/bin/varnishd/http1/cache_http1_deliver.c b/bin/varnishd/http1/cache_http1_deliver.c
index f887a63..4b09178 100644
--- a/bin/varnishd/http1/cache_http1_deliver.c
+++ b/bin/varnishd/http1/cache_http1_deliver.c
@@ -32,8 +32,6 @@
#include "cache/cache.h"
#include "cache/cache_filter.h"
-#include "vct.h"
-
/*--------------------------------------------------------------------*/
static int __match_proto__(vdp_bytes)
@@ -59,135 +57,6 @@ v1d_bytes(struct req *req, enum vdp_action act, void **priv,
return (0);
}
-/*--------------------------------------------------------------------*/
-
-struct v1rp {
- unsigned magic;
-#define V1RP_MAGIC 0xb886e711
- ssize_t range_low;
- ssize_t range_high;
- ssize_t range_off;
-};
-
-static int __match_proto__(vdp_bytes)
-v1d_range_bytes(struct req *req, enum vdp_action act, void **priv,
- const void *ptr, ssize_t len)
-{
- int retval = 0;
- ssize_t l;
- const char *p = ptr;
- struct v1rp *v1rp;
-
- CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
- if (act == VDP_INIT)
- return (0);
- if (act == VDP_FINI) {
- *priv = NULL;
- return (0);
- }
- CAST_OBJ_NOTNULL(v1rp, *priv, V1RP_MAGIC);
- l = v1rp->range_low - v1rp->range_off;
- if (l > 0) {
- if (l > len)
- l = len;
- v1rp->range_off += l;
- p += l;
- len -= l;
- }
- l = v1rp->range_high - v1rp->range_off;
- if (l > len)
- l = len;
- if (l > 0)
- retval = VDP_bytes(req, act, p, l);
- else if (act > VDP_NULL)
- retval = VDP_bytes(req, act, p, 0);
- v1rp->range_off += len;
- return (retval);
-}
-
-/*--------------------------------------------------------------------*/
-
-
-static void
-v1d_dorange(struct req *req, struct busyobj *bo, const char *r)
-{
- ssize_t len, low, high, has_low;
- struct v1rp *v1rp;
-
- CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
- CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
- assert(http_IsStatus(req->resp, 200));
-
- /* We must snapshot the length if we're streaming from the backend */
- if (bo != NULL)
- len = VBO_waitlen(req->wrk, bo, -1);
- else
- len = ObjGetLen(req->wrk, req->objcore);
-
- if (strncmp(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;
- low += *r - '0';
- r++;
- }
-
- if (low >= len)
- return;
-
- if (*r != '-')
- return;
- r++;
-
- /* The high end of range */
- if (vct_isdigit(*r)) {
- high = 0;
- while (vct_isdigit(*r)) {
- high *= 10;
- high += *r - '0';
- r++;
- }
- if (!has_low) {
- low = len - high;
- if (low < 0)
- low = 0;
- high = len - 1;
- }
- } else
- high = len - 1;
- if (*r != '\0')
- return;
-
- if (high >= len)
- high = len - 1;
-
- if (low > high)
- return;
-
- http_PrintfHeader(req->resp, "Content-Range: bytes %jd-%jd/%jd",
- (intmax_t)low, (intmax_t)high, (intmax_t)len);
- http_Unset(req->resp, H_Content_Length);
- if (req->res_mode & RES_LEN)
- http_PrintfHeader(req->resp, "Content-Length: %jd",
- (intmax_t)(1 + high - low));
- http_PutResponse(req->resp, "HTTP/1.1", 206, NULL);
-
- v1rp = WS_Alloc(req->ws, sizeof *v1rp);
- XXXAN(v1rp);
- INIT_OBJ(v1rp, V1RP_MAGIC);
- v1rp->range_off = 0;
- v1rp->range_low = low;
- v1rp->range_high = high + 1;
- VDP_push(req, v1d_range_bytes, v1rp, 0);
-}
-
/*--------------------------------------------------------------------
*/
@@ -234,7 +103,7 @@ V1D_Deliver(struct req *req, struct busyobj *bo)
if (cache_param->http_range_support && http_IsStatus(req->resp, 200)) {
http_SetHeader(req->resp, "Accept-Ranges: bytes");
if (req->wantbody && http_GetHdr(req->http, H_Range, &r))
- v1d_dorange(req, bo, r);
+ VRG_dorange(req, bo, r);
}
if (cache_param->http_gzip_support &&
More information about the varnish-commit
mailing list