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
37
#include "vtcp.h"
38
39
/*--------------------------------------------------------------------*/
40
41
static void
42 4120
v1d_error(struct req *req, struct boc *boc, const char *msg)
43
{
44
        static const char r_500[] =
45
            "HTTP/1.1 500 Internal Server Error\r\n"
46
            "Server: Varnish\r\n"
47
            "Connection: close\r\n\r\n";
48
49 4120
        AZ(req->wrk->v1l);
50
51 4120
        VSLbs(req->vsl, SLT_Error, TOSTRAND(msg));
52 4120
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1");
53 4120
        VSLb(req->vsl, SLT_RespStatus, "500");
54 4120
        VSLb(req->vsl, SLT_RespReason, "Internal Server Error");
55
56 4120
        req->wrk->stats->client_resp_500++;
57 4120
        VTCP_Assert(write(req->sp->fd, r_500, sizeof r_500 - 1));
58 4120
        req->doclose = SC_TX_EOF;
59
60 4120
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, boc);
61 4120
}
62
63
/*--------------------------------------------------------------------
64
 */
65
66
void v_matchproto_(vtr_deliver_f)
67 119119
V1D_Deliver(struct req *req, struct boc *boc, int sendbody)
68
{
69
        struct vrt_ctx ctx[1];
70 119119
        int err = 0, chunked = 0;
71
        stream_close_t sc;
72
        uint64_t hdrbytes, bytes;
73
74 119119
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
75 119119
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
76 119119
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
77
78 119119
        if (req->doclose == SC_NULL &&
79 110343
            http_HdrIs(req->resp, H_Connection, "close")) {
80 280
                req->doclose = SC_RESP_CLOSE;
81 119119
        } else if (req->doclose != SC_NULL) {
82 8761
                if (!http_HdrIs(req->resp, H_Connection, "close")) {
83 8621
                        http_Unset(req->resp, H_Connection);
84 8621
                        http_SetHeader(req->resp, "Connection: close");
85 8621
                }
86 118839
        } else if (!http_GetHdr(req->resp, H_Connection, NULL))
87 110062
                http_SetHeader(req->resp, "Connection: keep-alive");
88
89 119097
        if (sendbody) {
90 78141
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
91 10239
                        if (req->http->protover == 11) {
92 10079
                                chunked = 1;
93 10079
                                http_SetHeader(req->resp,
94
                                    "Transfer-Encoding: chunked");
95 10079
                        } else {
96 160
                                req->doclose = SC_TX_EOF;
97
                        }
98 10239
                }
99 78141
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
100 78141
                VCL_Req2Ctx(ctx, req);
101 78141
                if (VDP_Push(ctx, req->vdc, req->ws, VDP_v1l, NULL)) {
102 2280
                        v1d_error(req, boc, "Failure to push v1d processor");
103 2280
                        return;
104
                }
105 75861
        }
106
107 116817
        if (WS_Overflowed(req->ws)) {
108 1800
                v1d_error(req, boc, "workspace_client overflow");
109 1800
                return;
110
        }
111
112 115017
        if (WS_Overflowed(req->sp->ws)) {
113 40
                v1d_error(req, boc, "workspace_session overflow");
114 40
                return;
115
        }
116
117 229954
        V1L_Open(req->wrk, req->wrk->aws, &req->sp->fd, req->vsl,
118 114977
            req->t_prev + SESS_TMO(req->sp, send_timeout),
119 114977
            cache_param->http1_iovs);
120
121 114977
        if (WS_Overflowed(req->wrk->aws)) {
122 0
                v1d_error(req, boc, "workspace_thread overflow");
123 0
                return;
124
        }
125
126 114977
        hdrbytes = HTTP1_Write(req->wrk, req->resp, HTTP1_Resp);
127
128 114977
        if (sendbody) {
129 75666
                if (DO_DEBUG(DBG_FLUSH_HEAD))
130 439
                        (void)V1L_Flush(req->wrk);
131 75666
                if (chunked)
132 10039
                        V1L_Chunked(req->wrk);
133 75666
                err = VDP_DeliverObj(req->vdc, req->objcore);
134 75666
                if (!err && chunked)
135 9196
                        V1L_EndChunk(req->wrk);
136 75666
        }
137
138 114977
        sc = V1L_Close(req->wrk, &bytes);
139 114977
        AZ(req->wrk->v1l);
140
141 114977
        req->acct.resp_hdrbytes += hdrbytes;
142 114977
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, boc);
143
144 114977
        if (sc == SC_NULL && err && req->sp->fd >= 0)
145 600
                sc = SC_REM_CLOSE;
146 114977
        if (sc != SC_NULL)
147 1080
                Req_Fail(req, sc);
148 119097
}