]> git.ipfire.org Git - thirdparty/squid.git/blame - src/clientStream.cc
CI: Remove unnecessary test-functionality test wrappers (#1393)
[thirdparty/squid.git] / src / clientStream.cc
CommitLineData
edce4d98 1/*
b8ae064d 2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
edce4d98 3 *
bbc27441
AJ
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
edce4d98 7 */
8
bbc27441
AJ
9/* DEBUG: section 87 Client-side Stream routines. */
10
582c2af2 11#include "squid.h"
602d9612 12#include "client_side_request.h"
63be0a78 13#include "clientStream.h"
d3dddfb5 14#include "http/Stream.h"
63be0a78 15#include "HttpReply.h"
16#include "HttpRequest.h"
63be0a78 17
18/**
19 \defgroup ClientStreamInternal Client Streams Internals
20 \ingroup ClientStreamAPI
21 \par
edce4d98 22 * A client Stream is a uni directional pipe, with the usual non-blocking
23 * asynchronous approach present elsewhere in squid.
24 *
63be0a78 25 \par
edce4d98 26 * Each pipe node has a data push function, and a data request function.
2f8abb64 27 * This limits flexibility - the data flow is no longer assembled at each
26ac0430 28 * step.
edce4d98 29 *
63be0a78 30 \par
edce4d98 31 * An alternative approach is to pass each node in the pipe the call-
26ac0430
AJ
32 * back to use on each IO call. This allows the callbacks to be changed
33 * very easily by a participating node, but requires more maintenance
34 * in each node (store the callback to the most recent IO request in
35 * the nodes context.) Such an approach also prevents dynamically
edce4d98 36 * changing the pipeline from outside without an additional interface
37 * method to extract the callback and context from the next node.
38 *
63be0a78 39 \par
edce4d98 40 * One important characteristic of the stream is that the readfunc
41 * on the terminating node, and the callback on the first node
42 * will be NULL, and never used.
edce4d98 43 *
63be0a78 44 \section QuickNotes Quick Notes
45 \par
edce4d98 46 * Each node including the HEAD of the clientStream has a cbdataReference
47 * held by the stream. Freeing the stream then removes that reference
b4863613 48 * and delete's every node.
26ac0430 49 * Any node with other References, and all nodes downstream will only
edce4d98 50 * free when those references are released.
26ac0430 51 * Stream nodes MAY hold references to the data member of the node.
edce4d98 52 *
63be0a78 53 \par
26ac0430 54 * Specifically - on creation no reference is made.
edce4d98 55 * If you pass a data variable to a node, give it an initial reference.
56 * If the data member is non-null on FREE, cbdataFree WILL be called.
57 * This you must never call cbdataFree on your own context without
58 * explicitly setting the stream node data member to NULL and
59 * cbdataReferenceDone'ing it.
60 *
63be0a78 61 \par
edce4d98 62 * No data member may hold a reference to it's stream node.
63 * The stream guarantees that DETACH will be called before
2f8abb64 64 * freeing the node, allowing data members to cleanup.
26ac0430 65 *
63be0a78 66 \par
edce4d98 67 * If a node's data holds a reference to something that needs to
68 * free the stream a circular reference list will occur.
69 * This results no data being freed until that reference is removed.
e6ccf245 70 * One way to accomplish thisObject is to explicitly remove the
edce4d98 71 * data from your own node before freeing the stream.
72 *
63be0a78 73 \code
74 mycontext = thisObject->data;
75 thisObject->data = NULL;
b4863613 76 delete thisObject->head;
63be0a78 77 mycontext = NULL;
78 return;
79 \endcode
80 *
9837567d 81 * TODO: rather than each node undeleting the next, have a clientStreamDelete that walks the list.
edce4d98 82 */
83
b4863613 84CBDATA_CLASS_INIT(clientStreamNode);
63be0a78 85
b4863613 86clientStreamNode::clientStreamNode(CSR * aReadfunc, CSCB * aCallback, CSD * aDetach, CSS * aStatus, ClientStreamData aData) :
aee3523a 87 head(nullptr),
b4863613
AJ
88 readfunc(aReadfunc),
89 callback(aCallback),
90 detach(aDetach),
91 status(aStatus),
92 data(aData)
93{}
edce4d98 94
b4863613 95clientStreamNode::~clientStreamNode()
edce4d98 96{
b4863613
AJ
97 debugs(87, 3, "Freeing clientStreamNode " << this);
98
99 removeFromStream();
aee3523a 100 data = nullptr;
edce4d98 101}
102
63be0a78 103/**
104 \ingroup ClientStreamInternal
edce4d98 105 * Initialise a client Stream.
106 * list is the stream
107 * func is the read function for the head
108 * callback is the callback for the tail
109 * tailbuf and taillen are the initial buffer and length for the tail.
110 */
111void
112clientStreamInit(dlink_list * list, CSR * func, CSD * rdetach, CSS * readstatus,
0655fa4d 113 ClientStreamData readdata, CSCB * callback, CSD * cdetach, ClientStreamData callbackdata,
62e76326 114 StoreIOBuffer tailBuffer)
edce4d98 115{
aee3523a 116 clientStreamNode *temp = new clientStreamNode(func, nullptr, rdetach, readstatus, readdata);
7e6b941f 117 dlinkAdd(cbdataReference(temp), &temp->node, list);
edce4d98 118 temp->head = list;
aee3523a 119 clientStreamInsertHead(list, nullptr, callback, cdetach, nullptr, callbackdata);
e6ccf245 120 temp = (clientStreamNode *)list->tail->data;
c8be6d7b 121 temp->readBuffer = tailBuffer;
edce4d98 122}
123
63be0a78 124/**
125 \ingroup ClientStreamInternal
edce4d98 126 * Doesn't actually insert at head. Instead it inserts one *after*
127 * head. This is because HEAD is a special node, as is tail
128 * This function is not suitable for inserting the real HEAD.
edce4d98 129 */
130void
131clientStreamInsertHead(dlink_list * list, CSR * func, CSCB * callback,
0655fa4d 132 CSD * detach, CSS * status, ClientStreamData data)
edce4d98 133{
edce4d98 134 /* test preconditions */
aee3523a 135 assert(list != nullptr);
edce4d98 136 assert(list->head);
b4863613 137 clientStreamNode *temp = new clientStreamNode(func, callback, detach, status, data);
edce4d98 138 temp->head = list;
bf8fe701 139 debugs(87, 3, "clientStreamInsertHead: Inserted node " << temp <<
140 " with data " << data.getRaw() << " after head");
43ae1d95 141
142 if (list->head->next)
143 temp->readBuffer = ((clientStreamNode *)list->head->next->data)->readBuffer;
144
7e6b941f 145 dlinkAddAfter(cbdataReference(temp), &temp->node, list->head, list);
edce4d98 146}
147
63be0a78 148// API
edce4d98 149void
59a1efb2 150clientStreamCallback(clientStreamNode * thisObject, ClientHttpRequest * http,
62e76326 151 HttpReply * rep, StoreIOBuffer replyBuffer)
edce4d98 152{
153 clientStreamNode *next;
e6ccf245 154 assert(thisObject && http && thisObject->node.next);
155 next = thisObject->next();
edce4d98 156
122a6e3c 157 debugs(87, 3, thisObject << " gives " << next->data << ' ' << replyBuffer);
c8be6d7b 158 next->callback(next, http, rep, replyBuffer);
edce4d98 159}
160
63be0a78 161/**
162 \ingroup ClientStreamInternal
edce4d98 163 * Call the previous node in the chain to read some data
63be0a78 164 *
f53969cc
SM
165 \param thisObject ??
166 \param http ??
167 \param readBuffer ??
edce4d98 168 */
169void
59a1efb2 170clientStreamRead(clientStreamNode * thisObject, ClientHttpRequest * http,
62e76326 171 StoreIOBuffer readBuffer)
edce4d98 172{
173 /* place the parameters on the 'stack' */
174 clientStreamNode *prev;
e6ccf245 175 assert(thisObject && http && thisObject->prev());
176 prev = thisObject->prev();
edce4d98 177
bf8fe701 178 debugs(87, 3, "clientStreamRead: Calling " << prev->readfunc <<
179 " with cbdata " << prev->data.getRaw() << " from node " << thisObject);
e6ccf245 180 thisObject->readBuffer = readBuffer;
edce4d98 181 prev->readfunc(prev, http);
182}
183
63be0a78 184/**
185 \ingroup ClientStreamInternal
edce4d98 186 * Detach from the stream - only allowed for terminal members
63be0a78 187 *
f53969cc
SM
188 \param thisObject ??
189 \param http ??
edce4d98 190 */
191void
59a1efb2 192clientStreamDetach(clientStreamNode * thisObject, ClientHttpRequest * http)
edce4d98 193{
e6ccf245 194 clientStreamNode *temp = thisObject;
edce4d98 195
aee3523a 196 assert(thisObject->node.next == nullptr);
bf8fe701 197 debugs(87, 3, "clientStreamDetach: Detaching node " << thisObject);
e6ccf245 198 /* And clean up thisObject node */
edce4d98 199 /* ESI TODO: push refcount class through to head */
aee3523a 200 clientStreamNode *prev = nullptr;
0655fa4d 201
202 if (thisObject->prev())
203 prev = cbdataReference(thisObject->prev());
204
205 thisObject->removeFromStream();
206
edce4d98 207 cbdataReferenceDone(temp);
0655fa4d 208
b4863613 209 delete thisObject;
0655fa4d 210
61beade2 211 /* and tell the prev that the detach has occurred */
edce4d98 212 /*
e6ccf245 213 * We do it in thisObject order so that the detaching node is always
edce4d98 214 * at the end of the list
215 */
62e76326 216
edce4d98 217 if (prev) {
bf8fe701 218 debugs(87, 3, "clientStreamDetach: Calling " << prev->detach << " with cbdata " << prev->data.getRaw());
0655fa4d 219
220 if (cbdataReferenceValid(prev))
221 prev->detach(prev, http);
222
223 cbdataReferenceDone(prev);
edce4d98 224 }
225}
226
63be0a78 227/**
228 \ingroup ClientStreamInternal
edce4d98 229 * Abort the stream - detach every node in the pipeline.
63be0a78 230 *
f53969cc
SM
231 \param thisObject ??
232 \param http ??
edce4d98 233 */
234void
59a1efb2 235clientStreamAbort(clientStreamNode * thisObject, ClientHttpRequest * http)
edce4d98 236{
237 dlink_list *list;
238
aee3523a
AR
239 assert(thisObject != nullptr);
240 assert(http != nullptr);
e6ccf245 241 list = thisObject->head;
bf8fe701 242 debugs(87, 3, "clientStreamAbort: Aborting stream with tail " << list->tail);
62e76326 243
edce4d98 244 if (list->tail) {
62e76326 245 clientStreamDetach((clientStreamNode *)list->tail->data, http);
edce4d98 246 }
247}
248
63be0a78 249/**
250 \ingroup ClientStreamInternal
251 * Call the upstream node to find it's status
252 *
f53969cc
SM
253 \param thisObject ??
254 \param http ??
edce4d98 255 */
256clientStream_status_t
59a1efb2 257clientStreamStatus(clientStreamNode * thisObject, ClientHttpRequest * http)
edce4d98 258{
259 clientStreamNode *prev;
e6ccf245 260 assert(thisObject && http && thisObject->node.prev);
261 prev = (clientStreamNode *)thisObject->node.prev->data;
edce4d98 262 return prev->status(prev, http);
263}
264
0655fa4d 265void
266clientStreamNode::removeFromStream()
267{
268 if (head)
269 dlinkDelete(&node, head);
270
aee3523a 271 head = nullptr;
0655fa4d 272}
273
43ae1d95 274clientStreamNode *
275clientStreamNode::prev() const
e6ccf245 276{
277 if (node.prev)
43ae1d95 278 return (clientStreamNode *)node.prev->data;
e6ccf245 279 else
aee3523a 280 return nullptr;
e6ccf245 281}
282
43ae1d95 283clientStreamNode *
284clientStreamNode::next() const
e6ccf245 285{
286 if (node.next)
43ae1d95 287 return (clientStreamNode *)node.next->data;
e6ccf245 288 else
aee3523a 289 return nullptr;
e6ccf245 290}
f53969cc 291