vinyl-cache/vmod/vmod_debug_transport_reembarking_http1.c
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 0
dbg_error(struct req *req, struct v1l **v1lp, const char *msg)
45
{
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 672
dbg_deliver(struct req *req, int sendbody)
61
{
62
        struct vrt_ctx ctx[1];
63
        struct v1l *v1l;
64
65 672
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
66 672
        CHECK_OBJ_ORNULL(req->boc, BOC_MAGIC);
67 672
        CHECK_OBJ_NOTNULL(req->objcore, OBJCORE_MAGIC);
68
69 672
        req->doclose = http_EnsureConnection(req->resp, req->doclose);
70
71 672
        CHECK_OBJ_NOTNULL(req->wrk, WORKER_MAGIC);
72
73 1344
        v1l = V1L_Open(req->ws, &req->sp->fd, req->vsl,
74 672
            req->t_prev + SESS_TMO(req->sp, send_timeout),
75 672
            cache_param->http1_iovs);
76
77 672
        if (v1l == NULL) {
78 0
                dbg_error(req, &v1l, "Failure to init v1d (workspace_thread overflow)");
79 0
                return (VTR_D_DONE);
80
        }
81
82
        // Do not roll back req->ws upon V1L_Close()
83 672
        V1L_NoRollback(v1l);
84
85 672
        if (sendbody) {
86 672
                if (!http_GetHdr(req->resp, H_Content_Length, NULL)) {
87 336
                        if (req->http->protover == 11) {
88 336
                                http_SetHeader(req->resp,
89
                                    "Transfer-Encoding: chunked");
90 336
                        } else {
91 0
                                req->doclose = SC_TX_EOF;
92
                        }
93 336
                }
94 672
                INIT_OBJ(ctx, VRT_CTX_MAGIC);
95 672
                VCL_Req2Ctx(ctx, req);
96 672
                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
                }
100 672
        }
101
102 672
        if (WS_Overflowed(req->ws)) {
103 0
                dbg_error(req, &v1l, "workspace_client overflow");
104 0
                return (VTR_D_DONE);
105
        }
106
107 672
        if (WS_Overflowed(req->sp->ws)) {
108 0
                dbg_error(req, &v1l, "workspace_session overflow");
109 0
                return (VTR_D_DONE);
110
        }
111
112 672
        if (WS_Overflowed(req->wrk->aws)) {
113 0
                dbg_error(req, &v1l, "workspace_thread overflow");
114 0
                return (VTR_D_DONE);
115
        }
116
117 672
        req->acct.resp_hdrbytes += HTTP1_Write(v1l, req->resp, HTTP1_Resp);
118
119 672
        if (! sendbody) {
120 0
                dbg_deliver_finish(req, &v1l, 0);
121 0
                return (VTR_D_DONE);
122
        }
123
124 672
        (void)V1L_Flush(v1l);
125
126 672
        if (hack_http1_req == NULL)
127 21
                hack_http1_req = req->task->func;
128 672
        AN(hack_http1_req);
129
130 672
        VSLb(req->vsl, SLT_Debug, "w=%p scheduling dbg_sendbody", req->wrk);
131
132 672
        req->task->func = dbg_sendbody;
133 672
        req->task->priv = req;
134
135 672
        req->wrk = NULL;
136 672
        req->vdc->wrk = NULL;
137 672
        req->transport_priv = v1l;
138
139 672
        AZ(Pool_Task(req->sp->pool, req->task, TASK_QUEUE_RUSH));
140 672
        return (VTR_D_DISEMBARK);
141 672
}
142
143
static void v_matchproto_(task_func_t)
144 672
dbg_sendbody(struct worker *wrk, void *arg)
145
{
146
        struct req *req;
147
        struct v1l *v1l;
148
        const char *p;
149
        int err, chunked;
150
151 672
        CHECK_OBJ_NOTNULL(wrk, WORKER_MAGIC);
152 672
        CAST_OBJ_NOTNULL(req, arg, REQ_MAGIC);
153 672
        v1l = req->transport_priv;
154 672
        req->transport_priv = NULL;
155 672
        AN(v1l);
156
157 672
        THR_SetRequest(req);
158 672
        VSLb(req->vsl, SLT_Debug, "w=%p enter dbg_sendbody", wrk);
159 672
        AZ(req->wrk);
160 672
        CNT_Embark(wrk, req);
161 672
        req->vdc->wrk = wrk;    // move to CNT_Embark?
162
163 1008
        chunked = http_GetHdr(req->resp, H_Transfer_Encoding, &p) &&
164 336
            vstrcmp(p, "chunked") == 0;
165 672
        if (chunked)
166 336
                V1L_Chunked(v1l);
167 672
        err = VDP_DeliverObj(req->vdc, req->objcore);
168 672
        if (!err && chunked)
169 336
                V1L_EndChunk(v1l);
170 672
        dbg_deliver_finish(req, &v1l, err);
171
172 672
        VSLb(req->vsl, SLT_Debug, "w=%p resuming http1_req", wrk);
173 672
        wrk->task->func = hack_http1_req;
174 672
        wrk->task->priv = req;
175 672
}
176
177
static void
178 672
dbg_deliver_finish(struct req *req, struct v1l **v1lp, int err)
179
{
180
        stream_close_t sc;
181
        uint64_t bytes;
182
183 672
        sc = V1L_Close(v1lp, &bytes);
184
185 672
        req->acct.resp_bodybytes += VDP_Close(req->vdc, req->objcore, req->boc);
186
187 672
        if (sc == SC_NULL && err && req->sp->fd >= 0)
188 0
                sc = SC_REM_CLOSE;
189 672
        if (sc != SC_NULL)
190 0
                Req_Fail(req, sc);
191 672
}
192
193
static struct transport DBG_transport;
194
195
void
196 2205
debug_transport_reembarking_http1_init(void)
197
{
198 2205
        DBG_transport = HTTP1_transport;
199 2205
        DBG_transport.name = "DBG";
200 2205
        DBG_transport.deliver = dbg_deliver;
201 2205
}
202
203
void
204 670
debug_transport_reembarking_http1_use(VRT_CTX)
205
{
206
        struct req *req;
207
208 670
        CHECK_OBJ_NOTNULL(ctx, VRT_CTX_MAGIC);
209 670
        req = ctx->req;
210 670
        CHECK_OBJ_NOTNULL(req, REQ_MAGIC);
211
212 670
        if (req->transport == &DBG_transport) {
213 0
                return;
214
        }
215 670
        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
        }
220 670
        AZ(req->transport_priv);
221 670
        req->transport = &DBG_transport;
222 670
}