Code Coverage: vinyl-cache/vmod/vmod_debug_transport_reembarking_http1.c
 
82217aa4fc7094b3610340078afaf78cd95a8ec9
 
0
/*-
1
 * Copyright (c) 2006 Verdens Gang AS
2
 * Copyright (c) 2006-2015 Varnish Software AS
3
 * Copyright 2024 UPLEX - Nils Goroll Systemoptimierung
4
 * All rights reserved.
5
 *
6
 * Authors: Poul-Henning Kamp <phk@phk.freebsd.dk>
7
 *          Nils Goroll <slink@uplex.de>
8
 *
9
 * SPDX-License-Identifier: BSD-2-Clause
10
 *
11
 * Redistribution and use in source and binary forms, with or without
12
 * modification, are permitted provided that the following conditions
13
 * are met:
14
 * 1. Redistributions of source code must retain the above copyright
15
 *    notice, this list of conditions and the following disclaimer.
16
 * 2. Redistributions in binary form must reproduce the above copyright
17
 *    notice, this list of conditions and the following disclaimer in the
18
 *    documentation and/or other materials provided with the distribution.
19
 *
20
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23
 * ARE DISCLAIMED.  IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
24
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30
 * SUCH DAMAGE.
31
 */
32
33
#include "config.h"
34
35
#include "cache/cache_int.h"
36
37
#include "cache/cache_filter.h"
38
#include "cache/cache_transport.h"
39
#include "http1/cache_http1.h"
40
41
#include "vmod_debug.h"
42
43
static void
44
dbg_error(struct req *req, struct v1l **v1lp, const char *msg)
45 0
{
46
47 0
        (void)req;
48 0
        (void)v1lp;
49 0
        (void)msg;
50 0
        INCOMPL();
51 0
}
52
53
static void dbg_deliver_finish(struct req *req, struct v1l **v1lp, int err);
54
static void dbg_sendbody(struct worker *wrk, void *arg);
55
56
static task_func_t *hack_http1_req = NULL;
57
58
// copied from cache_http_deliver.c, then split & modified
59
static enum vtr_deliver_e v_matchproto_(vtr_deliver_f)
60
dbg_deliver(struct req *req, int sendbody)
61 32
{
62 32
        struct vrt_ctx ctx[1];
63 32
        struct v1l *v1l;
64
65 32
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
66 32
        CHECK_OBJ_ORNULL(req->boc, BOC_MAGIC);
67 32
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
68
69 32
        req->doclose = http_EnsureConnection(req->resp, req->doclose);
70
71 32
        CHECK_OBJ_NOTNULL(req->wrk, WORKER_MAGIC);
72
73 32
        v1l = V1L_Open(req->ws, &req->sp->fd, req->vsl,
74 32
            req->t_prev + SESS_TMO(req->sp, send_timeout),
75 32
            cache_param->http1_iovs);
76
77 32
        if (v1l == NULL) {
78 0
                dbg_error(req, &v1l, "Failure to init v1d (workspace_thread overflow)");
79 0
                return (VTR_D_DONE);
80 0
        }
81
82
        // Do not roll back req->ws upon V1L_Close()
83 32
        V1L_NoRollback(v1l);
84
85 32
        if (sendbody) {
86 32
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
87 16
                        if (req->http->protover == 11) {
88 16
                                http_SetHeader(req->resp,
89 16
                                    "Transfer-Encoding: chunked");
90 16
                        } else {
91 0
                                req->doclose = SC_TX_EOF;
92 0
                        }
93 16
                }
94 32
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
95 32
                VCL_Req2Ctx(ctx, req);
96 32
                if (VDP_Push(ctx, req->vdc, req->ws, VDP_v1l, v1l)) {
97 0
                        dbg_error(req, &v1l, "Failure to push v1d processor");
98 0
                        return (VTR_D_DONE);
99 0
                }
100 32
        }
101
102 32
        if (WS_Overflowed(req->ws)) {
103 0
                dbg_error(req, &v1l, "workspace_client overflow");
104 0
                return (VTR_D_DONE);
105 0
        }
106
107 32
        if (WS_Overflowed(req->sp->ws)) {
108 0
                dbg_error(req, &v1l, "workspace_session overflow");
109 0
                return (VTR_D_DONE);
110 0
        }
111
112 32
        if (WS_Overflowed(req->wrk->aws)) {
113 0
                dbg_error(req, &v1l, "workspace_thread overflow");
114 0
                return (VTR_D_DONE);
115 0
        }
116
117 32
        req->acct.resp_hdrbytes += HTTP1_Write(v1l, req->resp, HTTP1_Resp);
118
119 32
        if (! sendbody) {
120 0
                dbg_deliver_finish(req, &v1l, 0);
121 0
                return (VTR_D_DONE);
122 0
        }
123
124 32
        (void)V1L_Flush(v1l);
125
126 32
        if (hack_http1_req == NULL)
127 1
                hack_http1_req = req->task->func;
128 32
        AN(hack_http1_req);
129
130 32
        VSLb(req->vsl, SLT_Debug, "w=%p scheduling dbg_sendbody", req->wrk);
131
132 32
        req->task->func = dbg_sendbody;
133 32
        req->task->priv = req;
134
135 32
        req->wrk = NULL;
136 32
        req->vdc->wrk = NULL;
137 32
        req->transport_priv = v1l;
138
139 32
        AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH));
140 32
        return (VTR_D_DISEMBARK);
141 32
}
142
143
static void v_matchproto_(task_func_t)
144
dbg_sendbody(struct worker *wrk, void *arg)
145 32
{
146 32
        struct req *req;
147 32
        struct v1l *v1l;
148 32
        const char *p;
149 32
        int err, chunked;
150
151 32
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
152 32
        CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC);
153 32
        v1l = req->transport_priv;
154 32
        req->transport_priv = NULL;
155 32
        AN(v1l);
156
157 32
        THR_SetRequest(req);
158 32
        VSLb(req->vsl, SLT_Debug, "w=%p enter dbg_sendbody", wrk);
159 32
        AZ(req->wrk);
160 32
        CNT_Embark(wrk, req);
161 32
        req->vdc->wrk = wrk;    // move to CNT_Embark?
162
163 32
        chunked = http_GetHdr(req->resp, H_Transfer_Encoding, &p) &&
164 32
            vstrcmp(p, "chunked") == 0;
165 32
        if (chunked)
166 16
                V1L_Chunked(v1l);
167 32
        err = VDP_DeliverObj(req->vdc, req->objcore);
168 32
        if (!err && chunked)
169 16
                V1L_EndChunk(v1l);
170 32
        dbg_deliver_finish(req, &v1l, err);
171
172 32
        VSLb(req->vsl, SLT_Debug, "w=%p resuming http1_req", wrk);
173 32
        wrk->task->func = hack_http1_req;
174 32
        wrk->task->priv = req;
175 32
}
176
177
static void
178
dbg_deliver_finish(struct req *req, struct v1l **v1lp, int err)
179 32
{
180 32
        stream_close_t sc;
181 32
        uint64_t bytes;
182
183 32
        sc = V1L_Close(v1lp, &bytes);
184
185 32
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc);
186
187 32
        if (sc == SC_NULL && err && req->sp->fd >= 0)
188 0
                sc = SC_REM_CLOSE;
189 32
        if (sc != SC_NULL)
190 0
                Req_Fail(req, sc);
191 32
}
192
193
static struct transport DBG_transport;
194
195
void
196
debug_transport_reembarking_http1_init(void)
197 114
{
198 114
        DBG_transport = HTTP1_transport;
199 114
        DBG_transport.name = "DBG";
200 114
        DBG_transport.deliver = dbg_deliver;
201 114
}
202
203
void
204
debug_transport_reembarking_http1_use(VRT_CTX)
205 32
{
206 32
        struct req *req;
207
208 32
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
209 32
        req = ctx->req;
210 32
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
211
212 32
        if (req->transport == &DBG_transport) {
213 0
                return;
214 0
        }
215 32
        if (req->transport != &HTTP1_transport) {
216 0
                VRT_fail(ctx, "%s: %s transport not supported",
217 0
                    DBG_transport.name, req->transport->name);
218 0
                return;
219 0
        }
220 32
        AZ(req->transport_priv);
221 32
        req->transport = &DBG_transport;
222 32
}