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 975
v1d_error(struct req *req, 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 975
        AZ(req->wrk->v1l);
50
51 975
        VSLbs(req->vsl, SLT_Error, TOSTRAND(msg));
52 975
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1");
53 975
        VSLb(req->vsl, SLT_RespStatus, "500");
54 975
        VSLb(req->vsl, SLT_RespReason, "Internal Server Error");
55
56 975
        req->wrk->stats->client_resp_500++;
57 975
        VTCP_Assert(write(req->sp->fd, r_500, sizeof r_500 - 1));
58 975
        req->doclose = SC_TX_EOF;
59 975
}
60
61
/*--------------------------------------------------------------------
62
 */
63
64
void v_matchproto_(vtr_deliver_f)
65 69798
V1D_Deliver(struct req *req, struct boc *boc, int sendbody)
66
{
67
        struct vrt_ctx ctx[1];
68 69798
        int err = 0, chunked = 0;
69
        stream_close_t sc;
70
        uint64_t hdrbytes, bytes;
71
72 69798
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
73 69798
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
74 69798
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
75
76 69798
        if (req->doclose == SC_NULL &&
77 65440
            http_HdrIs(req->resp, H_Connection, "close")) {
78 175
                req->doclose = SC_RESP_CLOSE;
79 69798
        } else if (req->doclose != SC_NULL) {
80 4346
                if (!http_HdrIs(req->resp, H_Connection, "close")) {
81 4265
                        http_Unset(req->resp, H_Connection);
82 4265
                        http_SetHeader(req->resp, "Connection: close");
83 4265
                }
84 69623
        } else if (!http_GetHdr(req->resp, H_Connection, NULL))
85 65267
                http_SetHeader(req->resp, "Connection: keep-alive");
86
87 69788
        if (sendbody) {
88 45560
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
89 6480
                        if (req->http->protover == 11) {
90 6380
                                chunked = 1;
91 6380
                                http_SetHeader(req->resp,
92
                                    "Transfer-Encoding: chunked");
93 6380
                        } else {
94 100
                                req->doclose = SC_TX_EOF;
95
                        }
96 6480
                }
97 45560
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
98 45560
                VCL_Req2Ctx(ctx, req);
99 45560
                if (VDP_Push(ctx, req->vdc, req->ws, VDP_v1l, NULL)) {
100 725
                        v1d_error(req, "Failure to push v1d processor");
101 725
                        return;
102
                }
103 44835
        }
104
105 69063
        if (WS_Overflowed(req->ws)) {
106 225
                v1d_error(req, "workspace_client overflow");
107 225
                return;
108
        }
109
110 68838
        if (WS_Overflowed(req->sp->ws)) {
111 25
                v1d_error(req, "workspace_session overflow");
112 25
                return;
113
        }
114
115 137626
        V1L_Open(req->wrk, req->wrk->aws, &req->sp->fd, req->vsl,
116 68813
            req->t_prev + SESS_TMO(req->sp, send_timeout),
117 68813
            cache_param->http1_iovs);
118
119 68813
        if (WS_Overflowed(req->wrk->aws)) {
120 0
                v1d_error(req, "workspace_thread overflow");
121 0
                return;
122
        }
123
124 68813
        hdrbytes = HTTP1_Write(req->wrk, req->resp, HTTP1_Resp);
125
126 68813
        if (sendbody) {
127 44818
                if (DO_DEBUG(DBG_FLUSH_HEAD))
128 275
                        (void)V1L_Flush(req->wrk);
129 44818
                if (chunked)
130 6355
                        V1L_Chunked(req->wrk);
131 44818
                err = VDP_DeliverObj(req->vdc, req->objcore);
132 44818
                if (!err && chunked)
133 5829
                        V1L_EndChunk(req->wrk);
134 44818
        }
135
136 68813
        sc = V1L_Close(req->wrk, &bytes);
137 68813
        AZ(req->wrk->v1l);
138
139 68813
        req->acct.resp_hdrbytes += hdrbytes;
140 68813
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, boc);
141
142 68813
        if (sc == SC_NULL && err && req->sp->fd >= 0)
143 375
                sc = SC_REM_CLOSE;
144 68813
        if (sc != SC_NULL)
145 675
                Req_Fail(req, sc);
146 69788
}