|  |  | varnish-cache/bin/varnishd/http1/cache_http1_deliver.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 |  | #include "cache_http1.h" | 
| 36 |  | #include "cache/cache_transport.h" | 
| 37 |  |  | 
| 38 |  | #include "vtcp.h" | 
| 39 |  |  | 
| 40 |  | /*--------------------------------------------------------------------*/ | 
| 41 |  |  | 
| 42 |  | static void | 
| 43 | 1339 | v1d_error(struct req *req, struct v1l **v1lp, const char *msg) | 
| 44 |  | { | 
| 45 |  |         static const char r_500[] = | 
| 46 |  |             "HTTP/1.1 500 Internal Server Error\r\n" | 
| 47 |  |             "Server: Varnish\r\n" | 
| 48 |  |             "Connection: close\r\n\r\n"; | 
| 49 |  |         uint64_t bytes; | 
| 50 |  |  | 
| 51 | 1339 |         AN(v1lp); | 
| 52 | 1339 |         if (*v1lp != NULL) | 
| 53 | 1339 |                 (void) V1L_Close(v1lp, &bytes); | 
| 54 |  |  | 
| 55 | 1339 |         VSLbs(req->vsl, SLT_Error, TOSTRAND(msg)); | 
| 56 | 1339 |         VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1"); | 
| 57 | 1339 |         VSLb(req->vsl, SLT_RespStatus, "500"); | 
| 58 | 1339 |         VSLb(req->vsl, SLT_RespReason, "Internal Server Error"); | 
| 59 |  |  | 
| 60 | 1339 |         req->wrk->stats->client_resp_500++; | 
| 61 | 1339 |         VTCP_Assert(write(req->sp->fd, r_500, sizeof r_500 - 1)); | 
| 62 | 1339 |         req->doclose = SC_TX_EOF; | 
| 63 |  |  | 
| 64 | 1339 |         req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc); | 
| 65 | 1339 | } | 
| 66 |  |  | 
| 67 |  | /*-------------------------------------------------------------------- | 
| 68 |  |  */ | 
| 69 |  |  | 
| 70 |  | enum vtr_deliver_e v_matchproto_(vtr_deliver_f) | 
| 71 | 40071 | V1D_Deliver(struct req *req, int sendbody) | 
| 72 |  | { | 
| 73 |  |         struct vrt_ctx ctx[1]; | 
| 74 | 40071 |         int err = 0, chunked = 0; | 
| 75 |  |         stream_close_t sc; | 
| 76 |  |         uint64_t bytes; | 
| 77 |  |         struct v1l *v1l; | 
| 78 |  |  | 
| 79 | 40071 |         CHECK_OBJ_NOTNULL(req, REQ_MAGIC); | 
| 80 | 40071 |         CHECK_OBJ_ORNULL(req->boc, BOC_MAGIC); | 
| 81 | 40071 |         CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC); | 
| 82 |  |  | 
| 83 | 40071 |         if (req->doclose == SC_NULL && | 
| 84 | 37151 |             http_HdrIs(req->resp, H_Connection, "close")) { | 
| 85 | 91 |                 req->doclose = SC_RESP_CLOSE; | 
| 86 | 40071 |         } else if (req->doclose != SC_NULL) { | 
| 87 | 2914 |                 if (!http_HdrIs(req->resp, H_Connection, "close")) { | 
| 88 | 2880 |                         http_Unset(req->resp, H_Connection); | 
| 89 | 2880 |                         http_SetHeader(req->resp, "Connection: close"); | 
| 90 | 2880 |                 } | 
| 91 | 39980 |         } else if (!http_GetHdr(req->resp, H_Connection, NULL)) | 
| 92 | 37057 |                 http_SetHeader(req->resp, "Connection: keep-alive"); | 
| 93 |  |  | 
| 94 | 40059 |         CHECK_OBJ_NOTNULL(req->wrk, WORKER_MAGIC); | 
| 95 |  |  | 
| 96 | 80118 |         v1l = V1L_Open(req->wrk->aws, &req->sp->fd, req->vsl, | 
| 97 | 40059 |             req->t_prev + SESS_TMO(req->sp, send_timeout), | 
| 98 | 40059 |             cache_param->http1_iovs); | 
| 99 |  |  | 
| 100 | 40059 |         if (v1l == NULL) { | 
| 101 | 0 |                 v1d_error(req, &v1l, "Failure to init v1d (workspace_thread overflow)"); | 
| 102 | 0 |                 return (VTR_D_DONE); | 
| 103 |  |         } | 
| 104 |  |  | 
| 105 | 40059 |         if (sendbody) { | 
| 106 | 26243 |                 if (!http_GetHdr(req->resp, H_Content_Length, NULL)) { | 
| 107 | 3359 |                         if (req->http->protover == 11) { | 
| 108 | 3307 |                                 chunked = 1; | 
| 109 | 3307 |                                 http_SetHeader(req->resp, | 
| 110 |  |                                     "Transfer-Encoding: chunked"); | 
| 111 | 3307 |                         } else { | 
| 112 | 52 |                                 req->doclose = SC_TX_EOF; | 
| 113 |  |                         } | 
| 114 | 3359 |                 } | 
| 115 | 26243 |                 INIT_OBJ(ctx, VRT_CTX_MAGIC); | 
| 116 | 26243 |                 VCL_Req2Ctx(ctx, req); | 
| 117 | 26243 |                 if (VDP_Push(ctx, req->vdc, req->ws, VDP_v1l, v1l)) { | 
| 118 | 741 |                         v1d_error(req, &v1l, "Failure to push v1d processor"); | 
| 119 | 741 |                         return (VTR_D_DONE); | 
| 120 |  |                 } | 
| 121 | 25502 |         } | 
| 122 |  |  | 
| 123 | 39318 |         if (WS_Overflowed(req->ws)) { | 
| 124 | 585 |                 v1d_error(req, &v1l, "workspace_client overflow"); | 
| 125 | 585 |                 return (VTR_D_DONE); | 
| 126 |  |         } | 
| 127 |  |  | 
| 128 | 38733 |         if (WS_Overflowed(req->sp->ws)) { | 
| 129 | 13 |                 v1d_error(req, &v1l, "workspace_session overflow"); | 
| 130 | 13 |                 return (VTR_D_DONE); | 
| 131 |  |         } | 
| 132 |  |  | 
| 133 | 38720 |         if (WS_Overflowed(req->wrk->aws)) { | 
| 134 | 0 |                 v1d_error(req, &v1l, "workspace_thread overflow"); | 
| 135 | 0 |                 return (VTR_D_DONE); | 
| 136 |  |         } | 
| 137 |  |  | 
| 138 | 38720 |         req->acct.resp_hdrbytes += HTTP1_Write(v1l, req->resp, HTTP1_Resp); | 
| 139 |  |  | 
| 140 | 38720 |         if (sendbody) { | 
| 141 | 25433 |                 if (DO_DEBUG(DBG_FLUSH_HEAD)) | 
| 142 | 142 |                         (void)V1L_Flush(v1l); | 
| 143 | 25433 |                 if (chunked) | 
| 144 | 3294 |                         V1L_Chunked(v1l); | 
| 145 | 25433 |                 err = VDP_DeliverObj(req->vdc, req->objcore); | 
| 146 | 25433 |                 if (!err && chunked) | 
| 147 | 3022 |                         V1L_EndChunk(v1l); | 
| 148 | 25433 |         } | 
| 149 |  |  | 
| 150 | 38720 |         sc = V1L_Close(&v1l, &bytes); | 
| 151 |  |  | 
| 152 | 38720 |         req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc); | 
| 153 |  |  | 
| 154 | 38720 |         if (sc == SC_NULL && err && req->sp->fd >= 0) | 
| 155 | 221 |                 sc = SC_REM_CLOSE; | 
| 156 | 38720 |         if (sc != SC_NULL) | 
| 157 | 377 |                 Req_Fail(req, sc); | 
| 158 | 38720 |         return (VTR_D_DONE); | 
| 159 | 40059 | } |