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