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 8482
v1d_bytes(struct req *req, enum vdp_action act, void **priv,
40
    const void *ptr, ssize_t len)
41
{
42 8482
        ssize_t wl = 0;
43
44 8482
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
45
        (void)priv;
46 8482
        if (act == VDP_INIT || act == VDP_FINI)
47 4082
                return (0);
48
49 4400
        AZ(req->vdc->nxt);              /* always at the bottom of the pile */
50
51 4400
        if (len > 0)
52 3363
                wl = V1L_Write(req->wrk, ptr, len);
53 4400
        req->acct.resp_bodybytes += len;
54 4400
        if (act > VDP_NULL && V1L_Flush(req->wrk))
55 6
                return (-1);
56 4397
        if (len != wl)
57 0
                return (-1);
58 4397
        return (0);
59
}
60
61
static const struct vdp v1d_vdp = {
62
        .name =         "V1B",
63
        .func =         v1d_bytes,
64
};
65
66
static void
67 8
v1d_error(struct req *req, const char *msg)
68
{
69
        static const char r_500[] =
70
            "HTTP/1.1 500 Internal Server Error\r\n"
71
            "Server: Varnish\r\n"
72
            "Connection: close\r\n\r\n";
73
74 8
        VSLb(req->vsl, SLT_Error, "%s", msg);
75 8
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1");
76 8
        VSLb(req->vsl, SLT_RespStatus, "500");
77 8
        VSLb(req->vsl, SLT_RespReason, "Internal Server Error");
78
79 8
        (void)write(req->sp->fd, r_500, sizeof r_500 - 1);
80 8
        req->doclose = SC_TX_EOF;
81 8
}
82
83
/*--------------------------------------------------------------------
84
 */
85
86
void v_matchproto_(vtr_deliver_f)
87 3231
V1D_Deliver(struct req *req, struct boc *boc, int sendbody)
88
{
89
        int err;
90
91 3231
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
92 3231
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
93 3231
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
94
95 3231
        if (sendbody) {
96 3144
                if (http_GetHdr(req->resp, H_Content_Length, NULL))
97 2873
                        req->res_mode |= RES_LEN;
98 270
                else if (req->http->protover == 11) {
99 262
                        req->res_mode |= RES_CHUNKED;
100 262
                        http_SetHeader(req->resp, "Transfer-Encoding: chunked");
101
                } else {
102 8
                        req->res_mode |= RES_EOF;
103 8
                        req->doclose = SC_TX_EOF;
104
                }
105
        }
106
107 3232
        if (!req->doclose && http_HdrIs(req->resp, H_Connection, "close")) {
108 14
                req->doclose = SC_RESP_CLOSE;
109 3218
        } else if (req->doclose) {
110 198
                if (!http_HdrIs(req->resp, H_Connection, "close")) {
111 192
                        http_Unset(req->resp, H_Connection);
112 192
                        http_SetHeader(req->resp, "Connection: close");
113
                }
114 3020
        } else if (!http_GetHdr(req->resp, H_Connection, NULL))
115 3017
                http_SetHeader(req->resp, "Connection: keep-alive");
116
117 3232
        if (WS_Overflowed(req->ws)) {
118 8
                v1d_error(req, "workspace_client overflow");
119 8
                return;
120
        }
121
122 3222
        if (WS_Overflowed(req->sp->ws)) {
123 0
                v1d_error(req, "workspace_session overflow");
124 0
                return;
125
        }
126
127 3222
        if (req->resp_len == 0)
128 1100
                sendbody = 0;
129
130 3222
        if (sendbody)
131 2041
                VDP_push(req, &v1d_vdp, NULL, 1);
132
133 3221
        AZ(req->wrk->v1l);
134 6442
        V1L_Open(req->wrk, req->wrk->aws,
135 3221
                 &req->sp->fd, req->vsl, req->t_prev, 0);
136
137 3222
        if (WS_Overflowed(req->wrk->aws)) {
138 0
                v1d_error(req, "workspace_thread overflow");
139 0
                AZ(req->wrk->v1l);
140 0
                return;
141
        }
142
143 3222
        req->acct.resp_hdrbytes += HTTP1_Write(req->wrk, req->resp, HTTP1_Resp);
144
145 3224
        if (DO_DEBUG(DBG_FLUSH_HEAD))
146 0
                (void)V1L_Flush(req->wrk);
147
148 3220
        if (!sendbody || req->res_mode & RES_ESI)
149 1355
                if (V1L_Close(req->wrk) && req->sp->fd >= 0) {
150 0
                        Req_Fail(req, SC_REM_CLOSE);
151 0
                        sendbody = 0;
152
                }
153
154 3219
        if (!sendbody) {
155 1182
                AZ(req->wrk->v1l);
156 1182
                VDP_close(req);
157 1182
                return;
158
        }
159
160 2037
        AN(sendbody);
161 2037
        if (req->res_mode & RES_ESI) {
162 172
                AZ(req->wrk->v1l);
163
164 344
                V1L_Open(req->wrk, req->wrk->aws,
165 172
                         &req->sp->fd, req->vsl, req->t_prev,
166 172
                         cache_param->esi_iovs);
167
168 172
                if (WS_Overflowed(req->wrk->aws)) {
169 0
                        v1d_error(req, "workspace_thread overflow");
170 0
                        AZ(req->wrk->v1l);
171 0
                        return;
172
                }
173
        }
174
175 2037
        if (req->res_mode & RES_CHUNKED)
176 262
                V1L_Chunked(req->wrk);
177 2037
        err = VDP_DeliverObj(req);
178 2042
        if (!err && (req->res_mode & RES_CHUNKED))
179 242
                V1L_EndChunk(req->wrk);
180
181 2042
        if ((V1L_Close(req->wrk) || err) && req->sp->fd >= 0)
182 22
                Req_Fail(req, SC_REM_CLOSE);
183 2042
        AZ(req->wrk->v1l);
184 2042
        VDP_close(req);
185
}