2 * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
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.
9 #ifndef SQUID_CLIENTSTREAM_H
10 #define SQUID_CLIENTSTREAM_H
12 #include "base/RefCount.h"
13 #include "clientStreamForward.h"
15 #include "StoreIOBuffer.h"
18 \defgroup ClientStreamAPI Client Streams API
20 \section Introduction Introduction
22 * A ClientStream implements a unidirectional, non-blocking,
23 * pull pipeline. They allow code to be inserted into the
24 * reply logic on an as-needed basis. For instance,
25 * transfer-encoding logic is only needed when sending a
29 * Each node consists of four methods - read, callback, detach, and status,
30 * along with the stream housekeeping variables (a dlink node and pointer
31 * to the head of the list), context data for the node, and read request
32 * parameters - readbuf, readlen and readoff (in the body).
35 * clientStream is the basic unit for scheduling, and the clientStreamRead()
36 * and clientStreamCallback() calls allow for deferred scheduled activity if
39 \section OperationTheory Theory on stream operation
41 \li Something creates a pipeline. At a minimum it needs a head with a
42 * status method and a read method, and a tail with a callback method
43 * and a valid initial read request.
44 \li Other nodes may be added into the pipeline.
45 \li The tail-1th node's read method is called.
48 * For each node going up the pipeline, the node either:
49 \li satisfies the read request, or
50 \li inserts a new node above it and calls clientStreamRead(), or
51 \li calls clientStreamRead()
52 \todo DOCS: make the above list nested.
55 * There is no requirement for the Read parameters from different
56 * nodes to have any correspondence, as long as the callbacks provided are
59 \section WhatsInANode Whats in a node
61 \todo ClientStreams: These details should really be codified as a class which all ClientStream nodes inherit from.
63 \par Each node must have:
64 \li read method - to allow loose coupling in the pipeline. (The reader may
65 therefore change if the pipeline is altered, even mid-flow).
66 \li callback method - likewise.
67 \li status method - likewise.
68 \li detach method - used to ensure all resources are cleaned up properly.
69 \li dlink head pointer - to allow list inserts and deletes from within a node.
70 \li context data - to allow the called back nodes to maintain their private information.
71 \li read request parameters - For two reasons:
72 \li To allow a node to determine the requested data offset, length and target buffer dynamically. Again, this is to promote loose coupling.
73 \li Because of the callback nature of squid, every node would have to keep these parameters in their context anyway, so this reduces programmer overhead.
76 class clientStreamNode
78 CBDATA_CLASS(clientStreamNode
);
81 clientStreamNode(CSR
* aReadfunc
, CSCB
* aCallback
, CSD
* aDetach
, CSS
* aStatus
, ClientStreamData
);
84 clientStreamNode
*prev() const;
85 clientStreamNode
*next() const;
86 void removeFromStream();
89 dlink_list
*head
; /* sucks I know, but hey, the interface is limited */
92 CSD
*detach
; /* tell this node the next one downstream wants no more data */
94 ClientStreamData data
; /* Context for the node */
95 StoreIOBuffer readBuffer
; /* what, where and how much this node wants */
98 /// \ingroup ClientStreamAPI
99 void clientStreamInit(dlink_list
*, CSR
*, CSD
*, CSS
*, ClientStreamData
, CSCB
*, CSD
*, ClientStreamData
, StoreIOBuffer tailBuffer
);
101 /// \ingroup ClientStreamAPI
102 void clientStreamInsertHead(dlink_list
*, CSR
*, CSCB
*, CSD
*, CSS
*, ClientStreamData
);
105 \ingroup ClientStreamAPI
107 * Call back the next node the in chain with it's requested data.
108 * Return data to the next node in the stream.
109 * The data may be returned immediately, or may be delayed for a later scheduling cycle.
111 \param thisObject 'this' reference for the client stream
112 \param http Superset of request data, being winnowed down over time. MUST NOT be NULL.
113 \param rep Not NULL on the first call back only. Ownership is passed down the pipeline.
114 Each node may alter the reply if appropriate.
115 \param replyBuffer - buffer, length - where and how much.
117 void clientStreamCallback(clientStreamNode
*thisObject
, ClientHttpRequest
*http
, HttpReply
*rep
, StoreIOBuffer replyBuffer
);
120 \ingroup ClientStreamAPI
122 * Triggers a read of data that satisfies the httpClientRequest
123 * metainformation and (if appropriate) the offset,length and buffer
126 \param thisObject 'this' reference for the client stream
127 \param http Superset of request data, being winnowed down over time. MUST NOT be NULL.
128 \param readBuffer - offset, length, buffer - what, how much and where.
130 void clientStreamRead(clientStreamNode
*thisObject
, ClientHttpRequest
*http
, StoreIOBuffer readBuffer
);
133 \ingroup ClientStreamAPI
135 * Removes this node from a clientStream. The stream infrastructure handles the removal.
136 * This node MUST have cleaned up all context data, UNLESS scheduled callbacks will take care of that.
137 * Informs the prev node in the list of this nodes detachment.
139 \param thisObject 'this' reference for the client stream
140 \param http MUST NOT be NULL.
142 void clientStreamDetach(clientStreamNode
*thisObject
, ClientHttpRequest
*http
);
145 \ingroup ClientStreamAPI
147 * Detachs the tail of the stream. CURRENTLY DOES NOT clean up the tail node data -
148 * this must be done separately. Thus Abort may ONLY be called by the tail node.
150 \param thisObject 'this' reference for the client stream
151 \param http MUST NOT be NULL.
153 void clientStreamAbort(clientStreamNode
*thisObject
, ClientHttpRequest
*http
);
156 \ingroup ClientStreamAPI
158 * Allows nodes to query the upstream nodes for :
159 \li stream ABORTS - request cancelled for some reason. upstream will not accept further reads().
160 \li stream COMPLETION - upstream has completed and will not accept further reads().
161 \li stream UNPLANNED COMPLETION - upstream has completed, but not at a pre-planned location (used for keepalive checking), and will not accept further reads().
162 \li stream NONE - no special status, further reads permitted.
164 \param thisObject 'this' reference for the client stream
165 \param http MUST NOT be NULL.
167 clientStream_status_t
clientStreamStatus(clientStreamNode
*thisObject
, ClientHttpRequest
*http
);
169 #endif /* SQUID_CLIENTSTREAM_H */