| | varnish-cache/bin/varnishd/http1/cache_http1_fetch.c |
| 0 |
|
/*- |
| 1 |
|
* Copyright (c) 2006 Verdens Gang AS |
| 2 |
|
* Copyright (c) 2006-2015 Varnish Software AS |
| 3 |
|
* All rights reserved. |
| 4 |
|
* |
| 5 |
|
* Author: Poul-Henning Kamp <phk@phk.freebsd.dk> |
| 6 |
|
* |
| 7 |
|
* SPDX-License-Identifier: BSD-2-Clause |
| 8 |
|
* |
| 9 |
|
* Redistribution and use in source and binary forms, with or without |
| 10 |
|
* modification, are permitted provided that the following conditions |
| 11 |
|
* are met: |
| 12 |
|
* 1. Redistributions of source code must retain the above copyright |
| 13 |
|
* notice, this list of conditions and the following disclaimer. |
| 14 |
|
* 2. Redistributions in binary form must reproduce the above copyright |
| 15 |
|
* notice, this list of conditions and the following disclaimer in the |
| 16 |
|
* documentation and/or other materials provided with the distribution. |
| 17 |
|
* |
| 18 |
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 |
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 |
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 |
|
* ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 |
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 |
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 |
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 |
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 |
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 |
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 |
|
* SUCH DAMAGE. |
| 29 |
|
*/ |
| 30 |
|
|
| 31 |
|
#include "config.h" |
| 32 |
|
|
| 33 |
|
#include "cache/cache_varnishd.h" |
| 34 |
|
#include "cache/cache_filter.h" |
| 35 |
|
|
| 36 |
|
#include <stdio.h> |
| 37 |
|
#include <stdlib.h> |
| 38 |
|
|
| 39 |
|
#include "vtcp.h" |
| 40 |
|
#include "vtim.h" |
| 41 |
|
|
| 42 |
|
#include "cache_http1.h" |
| 43 |
|
|
| 44 |
|
static int |
| 45 |
86519 |
v1f_stackv1l(struct vdp_ctx *vdc, struct busyobj *bo, struct v1l *v1l) |
| 46 |
|
{ |
| 47 |
|
struct vrt_ctx ctx[1]; |
| 48 |
|
|
| 49 |
86519 |
INIT_OBJ(ctx, VRT_CTX_MAGIC); |
| 50 |
86519 |
VCL_Bo2Ctx(ctx, bo); |
| 51 |
86519 |
return (VDP_Push(ctx, vdc, ctx->ws, VDP_v1l, v1l)); |
| 52 |
|
} |
| 53 |
|
|
| 54 |
|
/*-------------------------------------------------------------------- |
| 55 |
|
* Send request to backend, including any (cached) req.body |
| 56 |
|
* |
| 57 |
|
* Return value: |
| 58 |
|
* 0 success |
| 59 |
|
* 1 failure |
| 60 |
|
*/ |
| 61 |
|
|
| 62 |
|
int |
| 63 |
86599 |
V1F_SendReq(struct worker *wrk, struct busyobj *bo, uint64_t *ctr_hdrbytes, |
| 64 |
|
uint64_t *ctr_bodybytes) |
| 65 |
|
{ |
| 66 |
|
struct http *hp; |
| 67 |
|
stream_close_t sc; |
| 68 |
|
ssize_t i; |
| 69 |
|
uint64_t bytes, hdrbytes; |
| 70 |
|
struct http_conn *htc; |
| 71 |
86599 |
struct vdp_ctx vdc[1] = {{ 0 }}; |
| 72 |
|
intmax_t cl; |
| 73 |
86599 |
const char *err = NULL; |
| 74 |
86599 |
struct v1l *v1l = NULL; |
| 75 |
|
|
| 76 |
86599 |
CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC); |
| 77 |
86599 |
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC); |
| 78 |
86599 |
CHECK_OBJ_NOTNULL(bo->htc, HTTP_CONN_MAGIC); |
| 79 |
86599 |
CHECK_OBJ_ORNULL(bo->req, REQ_MAGIC); |
| 80 |
86599 |
AN(ctr_hdrbytes); |
| 81 |
86599 |
AN(ctr_bodybytes); |
| 82 |
|
|
| 83 |
86599 |
htc = bo->htc; |
| 84 |
86599 |
assert(*htc->rfd > 0); |
| 85 |
86599 |
hp = bo->bereq; |
| 86 |
|
|
| 87 |
86599 |
if (bo->bereq_body != NULL) |
| 88 |
721 |
cl = ObjGetLen(wrk, bo->bereq_body); |
| 89 |
85878 |
else if (bo->req != NULL && !bo->req->req_body_status->length_known) |
| 90 |
560 |
cl = -1; |
| 91 |
85318 |
else if (bo->req != NULL) { |
| 92 |
2800 |
cl = http_GetContentLength(bo->req->http); |
| 93 |
2800 |
if (cl < 0) |
| 94 |
800 |
cl = 0; |
| 95 |
2800 |
} |
| 96 |
|
else |
| 97 |
82518 |
cl = 0; |
| 98 |
|
|
| 99 |
86599 |
VDP_Init(vdc, wrk, bo->vsl, NULL, bo, &cl); |
| 100 |
86599 |
if (bo->vdp_filter_list != NULL && |
| 101 |
320 |
VCL_StackVDP(vdc, bo->vcl, bo->vdp_filter_list, NULL, bo)) |
| 102 |
40 |
err = "Failure to push processors"; |
| 103 |
86559 |
else if ((v1l = V1L_Open(wrk->aws, htc->rfd, bo->vsl, nan(""), 0)) == NULL) { |
| 104 |
|
/* ^^^^^^^^ |
| 105 |
|
* XXX: need a send_timeout for the backend side |
| 106 |
|
* XXX: use cache_param->http1_iovs ? |
| 107 |
|
*/ |
| 108 |
40 |
err = "Failure to open V1L (workspace_thread overflow)"; |
| 109 |
40 |
} |
| 110 |
86519 |
else if (v1f_stackv1l(vdc, bo, v1l)) |
| 111 |
640 |
err = "Failure to push V1L"; |
| 112 |
|
|
| 113 |
86599 |
if (err != NULL) { |
| 114 |
721 |
if (v1l != NULL) |
| 115 |
640 |
(void) V1L_Close(&v1l, &bytes); |
| 116 |
721 |
if (VALID_OBJ(vdc, VDP_CTX_MAGIC)) |
| 117 |
720 |
(void) VDP_Close(vdc, NULL, NULL); |
| 118 |
723 |
VSLb(bo->vsl, SLT_FetchError, "%s", err); |
| 119 |
723 |
VSLb_ts_busyobj(bo, "Bereq", W_TIM_real(wrk)); |
| 120 |
723 |
htc->doclose = SC_OVERLOAD; |
| 121 |
723 |
return (-1); |
| 122 |
|
} |
| 123 |
|
|
| 124 |
|
|
| 125 |
85878 |
assert(cl >= -1); |
| 126 |
85878 |
if (cl < 0) |
| 127 |
600 |
http_PrintfHeader(hp, "Transfer-Encoding: chunked"); |
| 128 |
|
|
| 129 |
85878 |
VTCP_blocking(*htc->rfd); /* XXX: we should timeout instead */ |
| 130 |
85878 |
hdrbytes = HTTP1_Write(v1l, hp, HTTP1_Req); |
| 131 |
|
|
| 132 |
|
/* Deal with any message-body the request might (still) have */ |
| 133 |
85878 |
i = 0; |
| 134 |
|
|
| 135 |
85878 |
if (bo->bereq_body != NULL) { |
| 136 |
722 |
AZ(bo->req); |
| 137 |
722 |
assert(cl >= 0); |
| 138 |
1444 |
(void)ObjIterate(bo->wrk, bo->bereq_body, |
| 139 |
722 |
vdc, VDP_ObjIterate, 0); |
| 140 |
85878 |
} else if (bo->req != NULL && |
| 141 |
3280 |
bo->req->req_body_status != BS_NONE) { |
| 142 |
2400 |
if (cl < 0) |
| 143 |
600 |
V1L_Chunked(v1l); |
| 144 |
2400 |
i = VRB_Iterate(wrk, bo->vsl, bo->req, VDP_ObjIterate, vdc); |
| 145 |
|
|
| 146 |
2400 |
if (bo->req->req_body_status != BS_CACHED) |
| 147 |
2360 |
bo->no_retry = "req.body not cached"; |
| 148 |
|
|
| 149 |
2400 |
if (bo->req->req_body_status == BS_ERROR) { |
| 150 |
|
/* |
| 151 |
|
* XXX: (#2332) We should test to see if the backend |
| 152 |
|
* XXX: sent us some headers explaining why. |
| 153 |
|
* XXX: This is hard because of the mistaken API split |
| 154 |
|
* XXX: between cache_backend.c and V1F, and therefore |
| 155 |
|
* XXX: Parked in this comment, pending renovation of |
| 156 |
|
* XXX: the VDI/backend-protocol API to allow non-H1 |
| 157 |
|
* XXX: backends. |
| 158 |
|
*/ |
| 159 |
600 |
assert(i < 0); |
| 160 |
1200 |
VSLb(bo->vsl, SLT_FetchError, |
| 161 |
|
"req.body read error: %d (%s)", |
| 162 |
600 |
errno, VAS_errtxt(errno)); |
| 163 |
600 |
bo->req->doclose = SC_RX_BODY; |
| 164 |
600 |
bo->err_code = 400; |
| 165 |
600 |
} |
| 166 |
2400 |
if (cl < 0) |
| 167 |
600 |
V1L_EndChunk(v1l); |
| 168 |
2400 |
} |
| 169 |
|
|
| 170 |
85878 |
sc = V1L_Close(&v1l, &bytes); |
| 171 |
85878 |
CHECK_OBJ_NOTNULL(sc, STREAM_CLOSE_MAGIC); |
| 172 |
|
|
| 173 |
|
/* Bytes accounting */ |
| 174 |
85878 |
*ctr_hdrbytes += hdrbytes; |
| 175 |
85878 |
*ctr_bodybytes += VDP_Close(vdc, NULL, NULL); |
| 176 |
|
|
| 177 |
85878 |
if (sc == SC_NULL && i < 0) |
| 178 |
600 |
sc = SC_TX_ERROR; |
| 179 |
|
|
| 180 |
85878 |
CHECK_OBJ_NOTNULL(sc, STREAM_CLOSE_MAGIC); |
| 181 |
85878 |
if (sc != SC_NULL) { |
| 182 |
1200 |
VSLb(bo->vsl, SLT_FetchError, |
| 183 |
|
"backend write error: %d (%s) (%s)", |
| 184 |
600 |
errno, VAS_errtxt(errno), sc->desc); |
| 185 |
600 |
VSLb_ts_busyobj(bo, "Bereq", W_TIM_real(wrk)); |
| 186 |
600 |
htc->doclose = sc; |
| 187 |
600 |
return (-1); |
| 188 |
|
} |
| 189 |
85278 |
CHECK_OBJ_NOTNULL(sc, STREAM_CLOSE_MAGIC); |
| 190 |
85278 |
VSLb_ts_busyobj(bo, "Bereq", W_TIM_real(wrk)); |
| 191 |
85278 |
return (0); |
| 192 |
86601 |
} |
| 193 |
|
|
| 194 |
|
int |
| 195 |
84553 |
V1F_FetchRespHdr(struct busyobj *bo) |
| 196 |
|
{ |
| 197 |
|
|
| 198 |
|
struct http *hp; |
| 199 |
|
int i; |
| 200 |
|
double t; |
| 201 |
|
struct http_conn *htc; |
| 202 |
|
enum htc_status_e hs; |
| 203 |
|
const char *name, *desc; |
| 204 |
|
|
| 205 |
84553 |
CHECK_OBJ_NOTNULL(bo, BUSYOBJ_MAGIC); |
| 206 |
84553 |
CHECK_OBJ_NOTNULL(bo->htc, HTTP_CONN_MAGIC); |
| 207 |
84553 |
CHECK_OBJ_ORNULL(bo->req, REQ_MAGIC); |
| 208 |
|
|
| 209 |
84553 |
htc = bo->htc; |
| 210 |
84553 |
assert(*htc->rfd > 0); |
| 211 |
|
|
| 212 |
84553 |
VSC_C_main->backend_req++; |
| 213 |
|
|
| 214 |
|
/* Receive response */ |
| 215 |
|
|
| 216 |
84553 |
HTC_RxInit(htc, bo->ws); |
| 217 |
84553 |
CHECK_OBJ_NOTNULL(htc, HTTP_CONN_MAGIC); |
| 218 |
84553 |
CHECK_OBJ_NOTNULL(bo->htc, HTTP_CONN_MAGIC); |
| 219 |
|
|
| 220 |
84553 |
t = VTIM_real() + htc->first_byte_timeout; |
| 221 |
169106 |
hs = HTC_RxStuff(htc, HTTP1_Complete, NULL, NULL, |
| 222 |
84553 |
t, NAN, htc->between_bytes_timeout, cache_param->http_resp_size); |
| 223 |
84553 |
if (hs != HTC_S_COMPLETE) { |
| 224 |
1680 |
bo->acct.beresp_hdrbytes += |
| 225 |
1680 |
htc->rxbuf_e - htc->rxbuf_b; |
| 226 |
1680 |
switch (hs) { |
| 227 |
|
case HTC_S_JUNK: |
| 228 |
0 |
VSLb(bo->vsl, SLT_FetchError, "Received junk"); |
| 229 |
0 |
htc->doclose = SC_RX_JUNK; |
| 230 |
0 |
break; |
| 231 |
|
case HTC_S_CLOSE: |
| 232 |
0 |
VSLb(bo->vsl, SLT_FetchError, "backend closed"); |
| 233 |
0 |
htc->doclose = SC_RESP_CLOSE; |
| 234 |
0 |
break; |
| 235 |
|
case HTC_S_TIMEOUT: |
| 236 |
40 |
VSLb(bo->vsl, SLT_FetchError, "timeout"); |
| 237 |
40 |
htc->doclose = SC_RX_TIMEOUT; |
| 238 |
40 |
break; |
| 239 |
|
case HTC_S_OVERFLOW: |
| 240 |
800 |
VSLb(bo->vsl, SLT_FetchError, "overflow"); |
| 241 |
800 |
htc->doclose = SC_RX_OVERFLOW; |
| 242 |
800 |
break; |
| 243 |
|
case HTC_S_IDLE: |
| 244 |
200 |
VSLb(bo->vsl, SLT_FetchError, "first byte timeout"); |
| 245 |
200 |
htc->doclose = SC_RX_TIMEOUT; |
| 246 |
200 |
break; |
| 247 |
|
default: |
| 248 |
640 |
HTC_Status(hs, &name, &desc); |
| 249 |
1280 |
VSLb(bo->vsl, SLT_FetchError, "HTC %s (%s)", |
| 250 |
640 |
name, desc); |
| 251 |
640 |
htc->doclose = SC_RX_BAD; |
| 252 |
640 |
break; |
| 253 |
|
} |
| 254 |
1680 |
return (htc->rxbuf_e == htc->rxbuf_b ? 1 : -1); |
| 255 |
|
} |
| 256 |
82873 |
VTCP_set_read_timeout(*htc->rfd, htc->between_bytes_timeout); |
| 257 |
|
|
| 258 |
82873 |
hp = bo->beresp; |
| 259 |
|
|
| 260 |
82873 |
i = HTTP1_DissectResponse(htc, hp, bo->bereq); |
| 261 |
82873 |
bo->acct.beresp_hdrbytes += htc->rxbuf_e - htc->rxbuf_b; |
| 262 |
82873 |
if (i) { |
| 263 |
720 |
VSLb(bo->vsl, SLT_FetchError, "http format error"); |
| 264 |
720 |
htc->doclose = SC_RX_JUNK; |
| 265 |
720 |
return (-1); |
| 266 |
|
} |
| 267 |
|
|
| 268 |
82153 |
htc->doclose = http_DoConnection(hp, SC_RESP_CLOSE); |
| 269 |
|
|
| 270 |
|
/* |
| 271 |
|
* Figure out how the fetch is supposed to happen, before the |
| 272 |
|
* headers are adulterated by VCL |
| 273 |
|
*/ |
| 274 |
82153 |
if (http_method_eq(http_GetMethod(bo->bereq), HEAD)) { |
| 275 |
|
/* |
| 276 |
|
* A HEAD request can never have a body in the reply, |
| 277 |
|
* no matter what the headers might say. |
| 278 |
|
* [RFC7231 4.3.2 p25] |
| 279 |
|
*/ |
| 280 |
240 |
bo->wrk->stats->fetch_head++; |
| 281 |
240 |
bo->htc->body_status = BS_NONE; |
| 282 |
82153 |
} else if (http_GetStatus(bo->beresp) <= 199) { |
| 283 |
|
/* |
| 284 |
|
* 1xx responses never have a body. |
| 285 |
|
* [RFC7230 3.3.2 p31] |
| 286 |
|
* ... but we should never see them. |
| 287 |
|
*/ |
| 288 |
0 |
bo->wrk->stats->fetch_1xx++; |
| 289 |
0 |
bo->htc->body_status = BS_ERROR; |
| 290 |
81913 |
} else if (http_IsStatus(bo->beresp, 204)) { |
| 291 |
|
/* |
| 292 |
|
* 204 is "No Content", obviously don't expect a body. |
| 293 |
|
* [RFC7230 3.3.1 p29 and 3.3.2 p31] |
| 294 |
|
*/ |
| 295 |
480 |
bo->wrk->stats->fetch_204++; |
| 296 |
480 |
if ((http_GetHdr(bo->beresp, H_Content_Length, NULL) && |
| 297 |
200 |
bo->htc->content_length != 0) || |
| 298 |
280 |
http_GetHdr(bo->beresp, H_Transfer_Encoding, NULL)) |
| 299 |
320 |
bo->htc->body_status = BS_ERROR; |
| 300 |
|
else |
| 301 |
160 |
bo->htc->body_status = BS_NONE; |
| 302 |
81673 |
} else if (http_IsStatus(bo->beresp, 304)) { |
| 303 |
|
/* |
| 304 |
|
* 304 is "Not Modified" it has no body. |
| 305 |
|
* [RFC7230 3.3 p28] |
| 306 |
|
*/ |
| 307 |
1760 |
bo->wrk->stats->fetch_304++; |
| 308 |
1760 |
bo->htc->body_status = BS_NONE; |
| 309 |
81433 |
} else if (bo->htc->body_status == BS_CHUNKED) { |
| 310 |
5680 |
bo->wrk->stats->fetch_chunked++; |
| 311 |
79673 |
} else if (bo->htc->body_status == BS_LENGTH) { |
| 312 |
42073 |
assert(bo->htc->content_length > 0); |
| 313 |
42073 |
bo->wrk->stats->fetch_length++; |
| 314 |
73993 |
} else if (bo->htc->body_status == BS_EOF) { |
| 315 |
920 |
bo->wrk->stats->fetch_eof++; |
| 316 |
31920 |
} else if (bo->htc->body_status == BS_ERROR) { |
| 317 |
160 |
bo->wrk->stats->fetch_bad++; |
| 318 |
31000 |
} else if (bo->htc->body_status == BS_NONE) { |
| 319 |
30840 |
bo->wrk->stats->fetch_none++; |
| 320 |
30840 |
} else { |
| 321 |
0 |
WRONG("wrong bodystatus"); |
| 322 |
|
} |
| 323 |
|
|
| 324 |
81913 |
assert(bo->vfc->resp == bo->beresp); |
| 325 |
81913 |
if (bo->htc->body_status != BS_NONE && |
| 326 |
48913 |
bo->htc->body_status != BS_ERROR) |
| 327 |
48678 |
if (V1F_Setup_Fetch(bo->vfc, bo->htc)) { |
| 328 |
640 |
VSLb(bo->vsl, SLT_FetchError, "overflow"); |
| 329 |
640 |
htc->doclose = SC_RX_OVERFLOW; |
| 330 |
640 |
return (-1); |
| 331 |
|
} |
| 332 |
|
|
| 333 |
81273 |
return (0); |
| 334 |
84313 |
} |