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_f)
39 5206
v1d_bytes(struct req *req, enum vdp_action act, void **priv,
40
    const void *ptr, ssize_t len)
41
{
42 5206
        ssize_t wl = 0;
43
44 5206
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
45
        (void)priv;
46
47 5206
        AZ(req->vdc->nxt);              /* always at the bottom of the pile */
48
49 5206
        if (len > 0)
50 4151
                wl = V1L_Write(req->wrk, ptr, len);
51 5206
        if (act > VDP_NULL && V1L_Flush(req->wrk))
52 6
                return (-1);
53 5199
        if (len != wl)
54 0
                return (-1);
55 5199
        return (0);
56
}
57
58
static const struct vdp v1d_vdp = {
59
        .name =         "V1B",
60
        .bytes =        v1d_bytes,
61
};
62
63
static void
64 46
v1d_error(struct req *req, const char *msg)
65
{
66
        static const char r_500[] =
67
            "HTTP/1.1 500 Internal Server Error\r\n"
68
            "Server: Varnish\r\n"
69
            "Connection: close\r\n\r\n";
70
71 46
        VSLb(req->vsl, SLT_Error, "%s", msg);
72 46
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1");
73 46
        VSLb(req->vsl, SLT_RespStatus, "500");
74 46
        VSLb(req->vsl, SLT_RespReason, "Internal Server Error");
75
76 46
        req->wrk->stats->client_resp_500++;
77 46
        (void)write(req->sp->fd, r_500, sizeof r_500 - 1);
78 46
        req->doclose = SC_TX_EOF;
79 46
}
80
81
/*--------------------------------------------------------------------
82
 */
83
84
void v_matchproto_(vtr_deliver_f)
85 4218
V1D_Deliver(struct req *req, struct boc *boc, int sendbody)
86
{
87 4218
        int err = 0, chunked = 0;
88
        unsigned u;
89
        uint64_t hdrbytes, bytes;
90
91 4218
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
92 4218
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
93 4218
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
94
95 4218
        if (!req->doclose && http_HdrIs(req->resp, H_Connection, "close")) {
96 14
                req->doclose = SC_RESP_CLOSE;
97 4204
        } else if (req->doclose) {
98 278
                if (!http_HdrIs(req->resp, H_Connection, "close")) {
99 272
                        http_Unset(req->resp, H_Connection);
100 272
                        http_SetHeader(req->resp, "Connection: close");
101
                }
102 3926
        } else if (!http_GetHdr(req->resp, H_Connection, NULL))
103 3926
                http_SetHeader(req->resp, "Connection: keep-alive");
104
105 4218
        if (sendbody) {
106 2832
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
107 384
                        if (req->http->protover == 11) {
108 376
                                chunked = 1;
109 376
                                http_SetHeader(req->resp,
110
                                    "Transfer-Encoding: chunked");
111
                        } else {
112 8
                                req->doclose = SC_TX_EOF;
113
                        }
114
                }
115 2832
                if (VDP_Push(req, &v1d_vdp, NULL)) {
116 30
                        v1d_error(req, "workspace_thread overflow");
117 30
                        AZ(req->wrk->v1l);
118 76
                        return;
119
                }
120
        }
121
122 4188
        if (WS_Overflowed(req->ws)) {
123 16
                v1d_error(req, "workspace_client overflow");
124 16
                return;
125
        }
126
127 4172
        if (WS_Overflowed(req->sp->ws)) {
128 0
                v1d_error(req, "workspace_session overflow");
129 0
                return;
130
        }
131
132 4172
        AZ(req->wrk->v1l);
133 8344
        V1L_Open(req->wrk, req->wrk->aws,
134 8344
                 &req->sp->fd, req->vsl, req->t_prev, cache_param->http1_iovs);
135
136 4172
        if (WS_Overflowed(req->wrk->aws)) {
137 0
                v1d_error(req, "workspace_thread overflow");
138 0
                AZ(req->wrk->v1l);
139 0
                return;
140
        }
141
142 4172
        hdrbytes = HTTP1_Write(req->wrk, req->resp, HTTP1_Resp);
143
144 4172
        if (sendbody) {
145 2802
                if (DO_DEBUG(DBG_FLUSH_HEAD))
146 0
                        (void)V1L_Flush(req->wrk);
147 2802
                if (chunked)
148 376
                        V1L_Chunked(req->wrk);
149 2802
                err = VDP_DeliverObj(req);
150 2802
                if (!err && chunked)
151 354
                        V1L_EndChunk(req->wrk);
152
        }
153
154 4172
        u = V1L_Close(req->wrk, &bytes);
155 4172
        AZ(req->wrk->v1l);
156
157
        /* Bytes accounting */
158 4172
        if (bytes < hdrbytes)
159 0
                req->acct.resp_hdrbytes += bytes;
160
        else {
161 4172
                req->acct.resp_hdrbytes += hdrbytes;
162 4172
                req->acct.resp_bodybytes += bytes - hdrbytes;
163
        }
164
165 4172
        if ((u || err) && req->sp->fd >= 0)
166 24
                Req_Fail(req, SC_REM_CLOSE);
167 4172
        VDP_close(req);
168
}