]>
Commit | Line | Data |
---|---|---|
edce4d98 | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 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 | 84 | CBDATA_CLASS_INIT(clientStreamNode); |
63be0a78 | 85 | |
b4863613 | 86 | clientStreamNode::clientStreamNode(CSR * aReadfunc, CSCB * aCallback, CSD * aDetach, CSS * aStatus, ClientStreamData aData) : |
cc8c4af2 | 87 | head(NULL), |
b4863613 AJ |
88 | readfunc(aReadfunc), |
89 | callback(aCallback), | |
90 | detach(aDetach), | |
91 | status(aStatus), | |
92 | data(aData) | |
93 | {} | |
edce4d98 | 94 | |
b4863613 | 95 | clientStreamNode::~clientStreamNode() |
edce4d98 | 96 | { |
b4863613 AJ |
97 | debugs(87, 3, "Freeing clientStreamNode " << this); |
98 | ||
99 | removeFromStream(); | |
100 | data = NULL; | |
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 | */ | |
111 | void | |
112 | clientStreamInit(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 | { |
b4863613 | 116 | clientStreamNode *temp = new clientStreamNode(func, NULL, rdetach, readstatus, readdata); |
7e6b941f | 117 | dlinkAdd(cbdataReference(temp), &temp->node, list); |
edce4d98 | 118 | temp->head = list; |
119 | clientStreamInsertHead(list, NULL, callback, cdetach, NULL, 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 | */ |
130 | void | |
131 | clientStreamInsertHead(dlink_list * list, CSR * func, CSCB * callback, | |
0655fa4d | 132 | CSD * detach, CSS * status, ClientStreamData data) |
edce4d98 | 133 | { |
edce4d98 | 134 | /* test preconditions */ |
135 | assert(list != NULL); | |
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 | 149 | void |
59a1efb2 | 150 | clientStreamCallback(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 | |
26ac0430 | 157 | debugs(87, 3, "clientStreamCallback: Calling " << next->callback << " with cbdata " << |
bf8fe701 | 158 | next->data.getRaw() << " from node " << thisObject); |
c8be6d7b | 159 | next->callback(next, http, rep, replyBuffer); |
edce4d98 | 160 | } |
161 | ||
63be0a78 | 162 | /** |
163 | \ingroup ClientStreamInternal | |
edce4d98 | 164 | * Call the previous node in the chain to read some data |
63be0a78 | 165 | * |
f53969cc SM |
166 | \param thisObject ?? |
167 | \param http ?? | |
168 | \param readBuffer ?? | |
edce4d98 | 169 | */ |
170 | void | |
59a1efb2 | 171 | clientStreamRead(clientStreamNode * thisObject, ClientHttpRequest * http, |
62e76326 | 172 | StoreIOBuffer readBuffer) |
edce4d98 | 173 | { |
174 | /* place the parameters on the 'stack' */ | |
175 | clientStreamNode *prev; | |
e6ccf245 | 176 | assert(thisObject && http && thisObject->prev()); |
177 | prev = thisObject->prev(); | |
edce4d98 | 178 | |
bf8fe701 | 179 | debugs(87, 3, "clientStreamRead: Calling " << prev->readfunc << |
180 | " with cbdata " << prev->data.getRaw() << " from node " << thisObject); | |
e6ccf245 | 181 | thisObject->readBuffer = readBuffer; |
edce4d98 | 182 | prev->readfunc(prev, http); |
183 | } | |
184 | ||
63be0a78 | 185 | /** |
186 | \ingroup ClientStreamInternal | |
edce4d98 | 187 | * Detach from the stream - only allowed for terminal members |
63be0a78 | 188 | * |
f53969cc SM |
189 | \param thisObject ?? |
190 | \param http ?? | |
edce4d98 | 191 | */ |
192 | void | |
59a1efb2 | 193 | clientStreamDetach(clientStreamNode * thisObject, ClientHttpRequest * http) |
edce4d98 | 194 | { |
e6ccf245 | 195 | clientStreamNode *temp = thisObject; |
edce4d98 | 196 | |
e6ccf245 | 197 | assert(thisObject->node.next == NULL); |
bf8fe701 | 198 | debugs(87, 3, "clientStreamDetach: Detaching node " << thisObject); |
e6ccf245 | 199 | /* And clean up thisObject node */ |
edce4d98 | 200 | /* ESI TODO: push refcount class through to head */ |
0655fa4d | 201 | clientStreamNode *prev = NULL; |
202 | ||
203 | if (thisObject->prev()) | |
204 | prev = cbdataReference(thisObject->prev()); | |
205 | ||
206 | thisObject->removeFromStream(); | |
207 | ||
edce4d98 | 208 | cbdataReferenceDone(temp); |
0655fa4d | 209 | |
b4863613 | 210 | delete thisObject; |
0655fa4d | 211 | |
61beade2 | 212 | /* and tell the prev that the detach has occurred */ |
edce4d98 | 213 | /* |
e6ccf245 | 214 | * We do it in thisObject order so that the detaching node is always |
edce4d98 | 215 | * at the end of the list |
216 | */ | |
62e76326 | 217 | |
edce4d98 | 218 | if (prev) { |
bf8fe701 | 219 | debugs(87, 3, "clientStreamDetach: Calling " << prev->detach << " with cbdata " << prev->data.getRaw()); |
0655fa4d | 220 | |
221 | if (cbdataReferenceValid(prev)) | |
222 | prev->detach(prev, http); | |
223 | ||
224 | cbdataReferenceDone(prev); | |
edce4d98 | 225 | } |
226 | } | |
227 | ||
63be0a78 | 228 | /** |
229 | \ingroup ClientStreamInternal | |
edce4d98 | 230 | * Abort the stream - detach every node in the pipeline. |
63be0a78 | 231 | * |
f53969cc SM |
232 | \param thisObject ?? |
233 | \param http ?? | |
edce4d98 | 234 | */ |
235 | void | |
59a1efb2 | 236 | clientStreamAbort(clientStreamNode * thisObject, ClientHttpRequest * http) |
edce4d98 | 237 | { |
238 | dlink_list *list; | |
239 | ||
e6ccf245 | 240 | assert(thisObject != NULL); |
edce4d98 | 241 | assert(http != NULL); |
e6ccf245 | 242 | list = thisObject->head; |
bf8fe701 | 243 | debugs(87, 3, "clientStreamAbort: Aborting stream with tail " << list->tail); |
62e76326 | 244 | |
edce4d98 | 245 | if (list->tail) { |
62e76326 | 246 | clientStreamDetach((clientStreamNode *)list->tail->data, http); |
edce4d98 | 247 | } |
248 | } | |
249 | ||
63be0a78 | 250 | /** |
251 | \ingroup ClientStreamInternal | |
252 | * Call the upstream node to find it's status | |
253 | * | |
f53969cc SM |
254 | \param thisObject ?? |
255 | \param http ?? | |
edce4d98 | 256 | */ |
257 | clientStream_status_t | |
59a1efb2 | 258 | clientStreamStatus(clientStreamNode * thisObject, ClientHttpRequest * http) |
edce4d98 | 259 | { |
260 | clientStreamNode *prev; | |
e6ccf245 | 261 | assert(thisObject && http && thisObject->node.prev); |
262 | prev = (clientStreamNode *)thisObject->node.prev->data; | |
edce4d98 | 263 | return prev->status(prev, http); |
264 | } | |
265 | ||
0655fa4d | 266 | void |
267 | clientStreamNode::removeFromStream() | |
268 | { | |
269 | if (head) | |
270 | dlinkDelete(&node, head); | |
271 | ||
272 | head = NULL; | |
273 | } | |
274 | ||
43ae1d95 | 275 | clientStreamNode * |
276 | clientStreamNode::prev() const | |
e6ccf245 | 277 | { |
278 | if (node.prev) | |
43ae1d95 | 279 | return (clientStreamNode *)node.prev->data; |
e6ccf245 | 280 | else |
62e76326 | 281 | return NULL; |
e6ccf245 | 282 | } |
283 | ||
43ae1d95 | 284 | clientStreamNode * |
285 | clientStreamNode::next() const | |
e6ccf245 | 286 | { |
287 | if (node.next) | |
43ae1d95 | 288 | return (clientStreamNode *)node.next->data; |
e6ccf245 | 289 | else |
62e76326 | 290 | return NULL; |
e6ccf245 | 291 | } |
f53969cc | 292 |