]>
Commit | Line | Data |
---|---|---|
edce4d98 | 1 | |
2 | /* | |
528b2c61 | 3 | * $Id: clientStream.cc,v 1.4 2003/01/23 00:37:17 robertc 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; |
edce4d98 | 99 | * cbdataReferenceDone (mycontext); |
e6ccf245 | 100 | * clientStreamFreeLinst (thisObject->head); |
edce4d98 | 101 | * cbdataFree (mycontext); |
102 | * return; | |
103 | */ | |
104 | ||
105 | /* Local functions */ | |
106 | static FREE clientStreamFree; | |
107 | ||
108 | clientStreamNode * | |
109 | clientStreamNew(CSR * readfunc, CSCB * callback, CSD * detach, CSS * status, | |
110 | void *data) | |
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, | |
132 | void *readdata, CSCB * callback, CSD * cdetach, void *callbackdata, | |
c8be6d7b | 133 | StoreIOBuffer tailBuffer) |
edce4d98 | 134 | { |
135 | clientStreamNode *temp = clientStreamNew(func, NULL, rdetach, readstatus, | |
136 | readdata); | |
137 | dlinkAdd(temp, &temp->node, list); | |
138 | cbdataReference(temp); | |
139 | temp->head = list; | |
140 | clientStreamInsertHead(list, NULL, callback, cdetach, NULL, callbackdata); | |
e6ccf245 | 141 | temp = (clientStreamNode *)list->tail->data; |
c8be6d7b | 142 | temp->readBuffer = tailBuffer; |
edce4d98 | 143 | } |
144 | ||
145 | /* | |
146 | * Doesn't actually insert at head. Instead it inserts one *after* | |
147 | * head. This is because HEAD is a special node, as is tail | |
148 | * This function is not suitable for inserting the real HEAD. | |
149 | * TODO: should we always initalise the buffers and length, to | |
150 | * allow safe insertion of elements in the downstream cycle? | |
151 | */ | |
152 | void | |
153 | clientStreamInsertHead(dlink_list * list, CSR * func, CSCB * callback, | |
154 | CSD * detach, CSS * status, void *data) | |
155 | { | |
156 | clientStreamNode *temp; | |
157 | ||
158 | /* test preconditions */ | |
159 | assert(list != NULL); | |
160 | assert(list->head); | |
161 | temp = clientStreamNew(func, callback, detach, status, data); | |
162 | temp->head = list; | |
c8be6d7b | 163 | debug(87, 3) |
edce4d98 | 164 | ("clientStreamInsertHead: Inserted node %p with data %p after head\n", |
165 | temp, data); | |
166 | dlinkAddAfter(temp, &temp->node, list->head, list); | |
167 | cbdataReference(temp); | |
168 | } | |
169 | ||
170 | /* | |
171 | * Callback the next node the in chain with it's requested data | |
172 | */ | |
173 | void | |
e6ccf245 | 174 | clientStreamCallback(clientStreamNode * thisObject, clientHttpRequest * http, |
c8be6d7b | 175 | HttpReply * rep, StoreIOBuffer replyBuffer) |
edce4d98 | 176 | { |
177 | clientStreamNode *next; | |
e6ccf245 | 178 | assert(thisObject && http && thisObject->node.next); |
179 | next = thisObject->next(); | |
edce4d98 | 180 | |
181 | debug(87, | |
182 | 3) ("clientStreamCallback: Calling %p with cbdata %p from node %p\n", | |
e6ccf245 | 183 | next->callback, next->data, thisObject); |
c8be6d7b | 184 | next->callback(next, http, rep, replyBuffer); |
edce4d98 | 185 | } |
186 | ||
187 | /* | |
188 | * Call the previous node in the chain to read some data | |
189 | */ | |
190 | void | |
e6ccf245 | 191 | clientStreamRead(clientStreamNode * thisObject, clientHttpRequest * http, |
c8be6d7b | 192 | StoreIOBuffer readBuffer) |
edce4d98 | 193 | { |
194 | /* place the parameters on the 'stack' */ | |
195 | clientStreamNode *prev; | |
e6ccf245 | 196 | assert(thisObject && http && thisObject->prev()); |
197 | prev = thisObject->prev(); | |
edce4d98 | 198 | |
199 | debug(87, 3) ("clientStreamRead: Calling %p with cbdata %p from node %p\n", | |
e6ccf245 | 200 | prev->readfunc, prev->data, thisObject); |
201 | thisObject->readBuffer = readBuffer; | |
edce4d98 | 202 | prev->readfunc(prev, http); |
203 | } | |
204 | ||
205 | /* | |
206 | * Detach from the stream - only allowed for terminal members | |
207 | */ | |
208 | void | |
e6ccf245 | 209 | clientStreamDetach(clientStreamNode * thisObject, clientHttpRequest * http) |
edce4d98 | 210 | { |
e6ccf245 | 211 | clientStreamNode *prev = thisObject->prev(); |
212 | clientStreamNode *temp = thisObject; | |
edce4d98 | 213 | |
e6ccf245 | 214 | assert(thisObject->node.next == NULL); |
215 | debug(87, 3) ("clientStreamDetach: Detaching node %p\n", thisObject); | |
216 | /* And clean up thisObject node */ | |
edce4d98 | 217 | /* ESI TODO: push refcount class through to head */ |
218 | cbdataReferenceDone(temp); | |
e6ccf245 | 219 | cbdataFree(thisObject); |
edce4d98 | 220 | /* and tell the prev that the detach has occured */ |
221 | /* | |
e6ccf245 | 222 | * We do it in thisObject order so that the detaching node is always |
edce4d98 | 223 | * at the end of the list |
224 | */ | |
225 | if (prev) { | |
226 | debug(87, 3) ("clientStreamDetach: Calling %p with cbdata %p\n", | |
227 | prev->detach, prev->data); | |
228 | prev->detach(prev, http); | |
229 | } | |
230 | } | |
231 | ||
232 | /* | |
233 | * Abort the stream - detach every node in the pipeline. | |
234 | */ | |
235 | void | |
e6ccf245 | 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; |
edce4d98 | 243 | debug(87, 3) ("clientStreamAbort: Aborting stream with tail %p\n", |
244 | list->tail); | |
245 | if (list->tail) { | |
e6ccf245 | 246 | clientStreamDetach((clientStreamNode *)list->tail->data, http); |
edce4d98 | 247 | } |
248 | } | |
249 | ||
250 | /* | |
251 | * Call the upstream node to find it's status | |
252 | */ | |
253 | clientStream_status_t | |
e6ccf245 | 254 | clientStreamStatus(clientStreamNode * thisObject, clientHttpRequest * http) |
edce4d98 | 255 | { |
256 | clientStreamNode *prev; | |
e6ccf245 | 257 | assert(thisObject && http && thisObject->node.prev); |
258 | prev = (clientStreamNode *)thisObject->node.prev->data; | |
edce4d98 | 259 | return prev->status(prev, http); |
260 | } | |
261 | ||
262 | /* Local function bodies */ | |
263 | void | |
264 | clientStreamFree(void *foo) | |
265 | { | |
e6ccf245 | 266 | clientStreamNode *thisObject = (clientStreamNode *)foo; |
edce4d98 | 267 | |
e6ccf245 | 268 | debug(87, 3) ("Freeing clientStreamNode %p\n", thisObject); |
269 | if (thisObject->data) { | |
270 | cbdataFree(thisObject->data); | |
edce4d98 | 271 | } |
e6ccf245 | 272 | if (thisObject->node.next || thisObject->node.prev) { |
273 | dlinkDelete(&thisObject->node, thisObject->head); | |
edce4d98 | 274 | } |
275 | } | |
e6ccf245 | 276 | |
277 | _clientStreamNode * | |
278 | _clientStreamNode::prev() const | |
279 | { | |
280 | if (node.prev) | |
281 | return (_clientStreamNode *)node.prev->data; | |
282 | else | |
283 | return NULL; | |
284 | } | |
285 | ||
286 | _clientStreamNode * | |
287 | _clientStreamNode::next() const | |
288 | { | |
289 | if (node.next) | |
290 | return (_clientStreamNode *)node.next->data; | |
291 | else | |
292 | return NULL; | |
293 | } |