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