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