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