varnish-cache/bin/varnishd/http1/cache_http1_deliver.c
1
/*-
2
 * Copyright (c) 2006 Verdens Gang AS
3
 * Copyright (c) 2006-2015 Varnish Software AS
4
 * All rights reserved.
5
 *
6
 * Author: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *
8
 * Redistribution and use in source and binary forms, with or without
9
 * modification, are permitted provided that the following conditions
10
 * are met:
11
 * 1. Redistributions of source code must retain the above copyright
12
 *    notice, this list of conditions and the following disclaimer.
13
 * 2. Redistributions in binary form must reproduce the above copyright
14
 *    notice, this list of conditions and the following disclaimer in the
15
 *    documentation and/or other materials provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
21
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27
 * SUCH DAMAGE.
28
 */
29
30
#include "config.h"
31
32
#include "cache/cache_varnishd.h"
33
#include "cache/cache_filter.h"
34
#include "cache_http1.h"
35
36
/*--------------------------------------------------------------------*/
37
38
static int v_matchproto_(vdp_bytes)
39 5332
v1d_bytes(struct req *req, enum vdp_action act, void **priv,
40
    const void *ptr, ssize_t len)
41
{
42 5332
        ssize_t wl = 0;
43
44 5332
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
45
        (void)priv;
46 5332
        if (act == VDP_INIT || act == VDP_FINI)
47 2752
                return (0);
48
49 2580
        AZ(req->vdc->nxt);              /* always at the bottom of the pile */
50
51 2580
        if (len > 0)
52 2053
                wl = V1L_Write(req->wrk, ptr, len);
53 2580
        if (act > VDP_NULL && V1L_Flush(req->wrk))
54 3
                return (-1);
55 2577
        if (len != wl)
56 0
                return (-1);
57 2577
        return (0);
58
}
59
60
static const struct vdp v1d_vdp = {
61
        .name =         "V1B",
62
        .func =         v1d_bytes,
63
};
64
65
static void
66 22
v1d_error(struct req *req, const char *msg)
67
{
68
        static const char r_500[] =
69
            "HTTP/1.1 500 Internal Server Error\r\n"
70
            "Server: Varnish\r\n"
71
            "Connection: close\r\n\r\n";
72
73 22
        VSLb(req->vsl, SLT_Error, "%s", msg);
74 22
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1");
75 22
        VSLb(req->vsl, SLT_RespStatus, "500");
76 22
        VSLb(req->vsl, SLT_RespReason, "Internal Server Error");
77
78 22
        (void)write(req->sp->fd, r_500, sizeof r_500 - 1);
79 22
        req->doclose = SC_TX_EOF;
80 22
}
81
82
/*--------------------------------------------------------------------
83
 */
84
85
void v_matchproto_(vtr_deliver_f)
86 2084
V1D_Deliver(struct req *req, struct boc *boc, int sendbody)
87
{
88
        int err;
89
        unsigned u;
90
        uint64_t hdrbytes, bytes;
91
92 2084
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
93 2084
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
94 2084
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
95
96 2084
        if (sendbody) {
97 2039
                if (http_GetHdr(req->resp, H_Content_Length, NULL))
98 1847
                        req->res_mode |= RES_LEN;
99 192
                else if (req->http->protover == 11) {
100 188
                        req->res_mode |= RES_CHUNKED;
101 188
                        http_SetHeader(req->resp, "Transfer-Encoding: chunked");
102
                } else {
103 4
                        req->res_mode |= RES_EOF;
104 4
                        req->doclose = SC_TX_EOF;
105
                }
106
        }
107
108 2084
        if (!req->doclose && http_HdrIs(req->resp, H_Connection, "close")) {
109 7
                req->doclose = SC_RESP_CLOSE;
110 2077
        } else if (req->doclose) {
111 135
                if (!http_HdrIs(req->resp, H_Connection, "close")) {
112 132
                        http_Unset(req->resp, H_Connection);
113 132
                        http_SetHeader(req->resp, "Connection: close");
114
                }
115 1942
        } else if (!http_GetHdr(req->resp, H_Connection, NULL))
116 1942
                http_SetHeader(req->resp, "Connection: keep-alive");
117
118 2084
        if (WS_Overflowed(req->ws)) {
119 12
                v1d_error(req, "workspace_client overflow");
120 12
                return;
121
        }
122
123 2072
        if (WS_Overflowed(req->sp->ws)) {
124 0
                v1d_error(req, "workspace_session overflow");
125 0
                return;
126
        }
127
128 2072
        if (req->resp_len == 0)
129 644
                sendbody = 0;
130
131 2072
        if (sendbody && VDP_push(req, &v1d_vdp, NULL, 1)) {
132 10
                v1d_error(req, "workspace_thread overflow");
133 10
                AZ(req->wrk->v1l);
134 10
                return;
135
        }
136
137 2062
        AZ(req->wrk->v1l);
138 4124
        V1L_Open(req->wrk, req->wrk->aws,
139 2062
                 &req->sp->fd, req->vsl, req->t_prev, 0);
140
141 2062
        if (WS_Overflowed(req->wrk->aws)) {
142 0
                v1d_error(req, "workspace_thread overflow");
143 0
                AZ(req->wrk->v1l);
144 0
                return;
145
        }
146
147 2062
        hdrbytes = HTTP1_Write(req->wrk, req->resp, HTTP1_Resp);
148
149 2062
        if (DO_DEBUG(DBG_FLUSH_HEAD))
150 0
                (void)V1L_Flush(req->wrk);
151
152 2062
        if (!sendbody || req->res_mode & RES_ESI) {
153 775
                if (V1L_Close(req->wrk, &bytes) && req->sp->fd >= 0) {
154 0
                        Req_Fail(req, SC_REM_CLOSE);
155 0
                        sendbody = 0;
156
                }
157
158
                /* Charge bytes sent as reported from V1L_Close. Only
159
                 * header-bytes have been attempted sent. */
160 775
                req->acct.resp_hdrbytes += bytes;
161 775
                hdrbytes = 0;
162
        }
163
164 2062
        if (!sendbody) {
165 686
                AZ(req->wrk->v1l);
166 686
                VDP_close(req);
167 686
                return;
168
        }
169
170 1376
        AN(sendbody);
171 1376
        if (req->res_mode & RES_ESI) {
172 89
                AZ(req->wrk->v1l);
173
174 178
                V1L_Open(req->wrk, req->wrk->aws,
175 89
                         &req->sp->fd, req->vsl, req->t_prev,
176 89
                         cache_param->esi_iovs);
177
178 89
                if (WS_Overflowed(req->wrk->aws)) {
179 0
                        v1d_error(req, "workspace_thread overflow");
180 0
                        AZ(req->wrk->v1l);
181 0
                        return;
182
                }
183
        }
184
185 1376
        if (req->res_mode & RES_CHUNKED)
186 188
                V1L_Chunked(req->wrk);
187 1376
        err = VDP_DeliverObj(req);
188 1376
        if (!err && (req->res_mode & RES_CHUNKED))
189 177
                V1L_EndChunk(req->wrk);
190
191 1376
        u = V1L_Close(req->wrk, &bytes);
192
193
        /* Bytes accounting */
194 1376
        if (bytes < hdrbytes)
195 0
                req->acct.resp_hdrbytes += bytes;
196
        else {
197 1376
                req->acct.resp_hdrbytes += hdrbytes;
198 1376
                req->acct.resp_bodybytes += bytes - hdrbytes;
199
        }
200
201 1376
        if ((u || err) && req->sp->fd >= 0)
202 12
                Req_Fail(req, SC_REM_CLOSE);
203 1376
        AZ(req->wrk->v1l);
204 1376
        VDP_close(req);
205
}