]> git.ipfire.org Git - thirdparty/squid.git/blame - src/clientStream.cc
fix some of the header detection issues on FreeBSD
[thirdparty/squid.git] / src / clientStream.cc
CommitLineData
edce4d98 1
2/*
c8be6d7b 3 * $Id: clientStream.cc,v 1.2 2002/09/24 10:46:43 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
31 * along with this program; if not, write to the Free Software
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"
edce4d98 59
60CBDATA_TYPE(clientStreamNode);
61
62/*
63 * TODO: rather than each node undeleting the next, have a clientStreamDelete
64 * that walks the list
65 */
66
67/*
68 * clientStream quick notes:
69 *
70 * Each node including the HEAD of the clientStream has a cbdataReference
71 * held by the stream. Freeing the stream then removes that reference
72 * and cbdataFrees every node.
73 * Any node with other References, and all nodes downstream will only
74 * free when those references are released.
75 * Stream nodes MAY hold references to the data member of the node.
76 *
77 * Specifically - on creation no reference is made.
78 * If you pass a data variable to a node, give it an initial reference.
79 * If the data member is non-null on FREE, cbdataFree WILL be called.
80 * This you must never call cbdataFree on your own context without
81 * explicitly setting the stream node data member to NULL and
82 * cbdataReferenceDone'ing it.
83 *
84 * No data member may hold a reference to it's stream node.
85 * The stream guarantees that DETACH will be called before
86 * freeing the node, alowing data members to cleanup.
87 *
88 * If a node's data holds a reference to something that needs to
89 * free the stream a circular reference list will occur.
90 * This results no data being freed until that reference is removed.
91 * One way to accomplish this is to explicitly remove the
92 * data from your own node before freeing the stream.
93 *
94 * (i.e.
95 * mycontext = this->data;
96 * cbdataReferenceDone (mycontext);
97 * clientStreamFreeLinst (this->head);
98 * cbdataFree (mycontext);
99 * return;
100 */
101
102/* Local functions */
103static FREE clientStreamFree;
104
105clientStreamNode *
106clientStreamNew(CSR * readfunc, CSCB * callback, CSD * detach, CSS * status,
107 void *data)
108{
109 clientStreamNode *temp;
110 CBDATA_INIT_TYPE_FREECB(clientStreamNode, clientStreamFree);
111 temp = cbdataAlloc(clientStreamNode);
112 temp->readfunc = readfunc;
113 temp->callback = callback;
114 temp->detach = detach;
115 temp->status = status;
116 temp->data = data;
117 return temp;
118}
119
120/*
121 * Initialise a client Stream.
122 * list is the stream
123 * func is the read function for the head
124 * callback is the callback for the tail
125 * tailbuf and taillen are the initial buffer and length for the tail.
126 */
127void
128clientStreamInit(dlink_list * list, CSR * func, CSD * rdetach, CSS * readstatus,
129 void *readdata, CSCB * callback, CSD * cdetach, void *callbackdata,
c8be6d7b 130 StoreIOBuffer tailBuffer)
edce4d98 131{
132 clientStreamNode *temp = clientStreamNew(func, NULL, rdetach, readstatus,
133 readdata);
134 dlinkAdd(temp, &temp->node, list);
135 cbdataReference(temp);
136 temp->head = list;
137 clientStreamInsertHead(list, NULL, callback, cdetach, NULL, callbackdata);
138 temp = list->tail->data;
c8be6d7b 139 temp->readBuffer = tailBuffer;
edce4d98 140}
141
142/*
143 * Doesn't actually insert at head. Instead it inserts one *after*
144 * head. This is because HEAD is a special node, as is tail
145 * This function is not suitable for inserting the real HEAD.
146 * TODO: should we always initalise the buffers and length, to
147 * allow safe insertion of elements in the downstream cycle?
148 */
149void
150clientStreamInsertHead(dlink_list * list, CSR * func, CSCB * callback,
151 CSD * detach, CSS * status, void *data)
152{
153 clientStreamNode *temp;
154
155 /* test preconditions */
156 assert(list != NULL);
157 assert(list->head);
158 temp = clientStreamNew(func, callback, detach, status, data);
159 temp->head = list;
c8be6d7b 160 debug(87, 3)
edce4d98 161 ("clientStreamInsertHead: Inserted node %p with data %p after head\n",
162 temp, data);
163 dlinkAddAfter(temp, &temp->node, list->head, list);
164 cbdataReference(temp);
165}
166
167/*
168 * Callback the next node the in chain with it's requested data
169 */
170void
171clientStreamCallback(clientStreamNode * this, clientHttpRequest * http,
c8be6d7b 172 HttpReply * rep, StoreIOBuffer replyBuffer)
edce4d98 173{
174 clientStreamNode *next;
175 assert(this && http && this->node.next);
176 next = this->node.next->data;
177
178 debug(87,
179 3) ("clientStreamCallback: Calling %p with cbdata %p from node %p\n",
180 next->callback, next->data, this);
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 */
187void
188clientStreamRead(clientStreamNode * this, clientHttpRequest * http,
c8be6d7b 189 StoreIOBuffer readBuffer)
edce4d98 190{
191 /* place the parameters on the 'stack' */
192 clientStreamNode *prev;
193 assert(this && http && this->node.prev);
194 prev = this->node.prev->data;
195
196 debug(87, 3) ("clientStreamRead: Calling %p with cbdata %p from node %p\n",
197 prev->readfunc, prev->data, this);
c8be6d7b 198 this->readBuffer = readBuffer;
edce4d98 199 prev->readfunc(prev, http);
200}
201
202/*
203 * Detach from the stream - only allowed for terminal members
204 */
205void
206clientStreamDetach(clientStreamNode * this, clientHttpRequest * http)
207{
208 clientStreamNode *prev = NULL;
209 clientStreamNode *temp = this;
210
211 if (this->node.prev) {
212 prev = this->node.prev->data;
213 }
214 assert(this->node.next == NULL);
215 debug(87, 3) ("clientStreamDetach: Detaching node %p\n", this);
216 /* And clean up this node */
217 /* ESI TODO: push refcount class through to head */
218 cbdataReferenceDone(temp);
219 cbdataFree(this);
220 /* and tell the prev that the detach has occured */
221 /*
222 * We do it in this order so that the detaching node is always
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 */
235void
236clientStreamAbort(clientStreamNode * this, clientHttpRequest * http)
237{
238 dlink_list *list;
239
240 assert(this != NULL);
241 assert(http != NULL);
242 list = this->head;
243 debug(87, 3) ("clientStreamAbort: Aborting stream with tail %p\n",
244 list->tail);
245 if (list->tail) {
246 clientStreamDetach(list->tail->data, http);
247 }
248}
249
250/*
251 * Call the upstream node to find it's status
252 */
253clientStream_status_t
254clientStreamStatus(clientStreamNode * this, clientHttpRequest * http)
255{
256 clientStreamNode *prev;
257 assert(this && http && this->node.prev);
258 prev = this->node.prev->data;
259 return prev->status(prev, http);
260}
261
262/* Local function bodies */
263void
264clientStreamFree(void *foo)
265{
266 clientStreamNode *this = foo;
267
268 debug(87, 3) ("Freeing clientStreamNode %p\n", this);
269 if (this->data) {
270 cbdataFree(this->data);
271 }
272 if (this->node.next || this->node.prev) {
273 dlinkDelete(&this->node, this->head);
274 }
275}