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 21825
v1d_bytes(struct req *req, enum vdp_action act, void **priv,
40
    const void *ptr, ssize_t len)
41
{
42 21825
        ssize_t wl = 0;
43
44 21825
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
45 21825
        (void)priv;
46
47 21825
        AZ(req->vdc->nxt);              /* always at the bottom of the pile */
48
49 21825
        if (len > 0)
50 17433
                wl = V1L_Write(req->wrk, ptr, len);
51 21825
        if (act > VDP_NULL && V1L_Flush(req->wrk))
52 56
                return (-1);
53 21769
        if (len != wl)
54 0
                return (-1);
55 21769
        return (0);
56 21825
}
57
58
static const struct vdp v1d_vdp = {
59
        .name =         "V1B",
60
        .bytes =        v1d_bytes,
61
};
62
63
static void
64 200
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 200
        VSLb(req->vsl, SLT_Error, "%s", msg);
72 200
        VSLb(req->vsl, SLT_RespProtocol, "HTTP/1.1");
73 200
        VSLb(req->vsl, SLT_RespStatus, "500");
74 200
        VSLb(req->vsl, SLT_RespReason, "Internal Server Error");
75
76 200
        req->wrk->stats->client_resp_500++;
77 200
        (void)write(req->sp->fd, r_500, sizeof r_500 - 1);
78 200
        req->doclose = SC_TX_EOF;
79 200
}
80
81
/*--------------------------------------------------------------------
82
 */
83
84
void v_matchproto_(vtr_deliver_f)
85 18614
V1D_Deliver(struct req *req, struct boc *boc, int sendbody)
86
{
87 18614
        int err = 0, chunked = 0;
88
        unsigned u;
89
        uint64_t hdrbytes, bytes;
90
91 18614
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
92 18624
        CHECK_OBJ_ORNULL(boc, BOC_MAGIC);
93 18623
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
94
95 18624
        if (!req->doclose && http_HdrIs(req->resp, H_Connection, "close")) {
96 56
                req->doclose = SC_RESP_CLOSE;
97 18624
        } else if (req->doclose) {
98 1336
                if (!http_HdrIs(req->resp, H_Connection, "close")) {
99 1312
                        http_Unset(req->resp, H_Connection);
100 1312
                        http_SetHeader(req->resp, "Connection: close");
101 1312
                }
102 18568
        } else if (!http_GetHdr(req->resp, H_Connection, NULL))
103 17232
                http_SetHeader(req->resp, "Connection: keep-alive");
104
105 18624
        if (sendbody) {
106 12240
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
107 1821
                        if (req->http->protover == 11) {
108 1789
                                chunked = 1;
109 1789
                                http_SetHeader(req->resp,
110
                                    "Transfer-Encoding: chunked");
111 1789
                        } else {
112 32
                                req->doclose = SC_TX_EOF;
113
                        }
114 1821
                }
115 12240
                if (VDP_Push(req, &v1d_vdp, NULL)) {
116 184
                        v1d_error(req, "workspace_thread overflow");
117 184
                        AZ(req->wrk->v1l);
118 184
                        return;
119
                }
120 12056
        }
121
122 18440
        if (WS_Overflowed(req->ws)) {
123 16
                v1d_error(req, "workspace_client overflow");
124 16
                return;
125
        }
126
127 18424
        if (WS_Overflowed(req->sp->ws)) {
128 0
                v1d_error(req, "workspace_session overflow");
129 0
                return;
130
        }
131
132 18424
        AZ(req->wrk->v1l);
133 36848
        V1L_Open(req->wrk, req->wrk->aws,
134 18424
            &req->sp->fd, req->vsl, req->t_prev + cache_param->send_timeout,
135 18424
            cache_param->http1_iovs);
136
137 18424
        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 18423
        hdrbytes = HTTP1_Write(req->wrk, req->resp, HTTP1_Resp);
144
145 18423
        if (sendbody) {
146 12056
                if (DO_DEBUG(DBG_FLUSH_HEAD))
147 0
                        (void)V1L_Flush(req->wrk);
148 12056
                if (chunked)
149 1773
                        V1L_Chunked(req->wrk);
150 12056
                err = VDP_DeliverObj(req);
151 12056
                if (!err && chunked)
152 1685
                        V1L_EndChunk(req->wrk);
153 12056
        }
154
155 18424
        u = V1L_Close(req->wrk, &bytes);
156 18424
        AZ(req->wrk->v1l);
157
158
        /* Bytes accounting */
159 18424
        if (bytes < hdrbytes)
160 7
                req->acct.resp_hdrbytes += bytes;
161
        else {
162 18417
                req->acct.resp_hdrbytes += hdrbytes;
163 18417
                req->acct.resp_bodybytes += bytes - hdrbytes;
164
        }
165
166 18424
        if ((u || err) && req->sp->fd >= 0)
167 128
                Req_Fail(req, SC_REM_CLOSE);
168 18424
        VDP_close(req);
169 18624
}