]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/BodyReader.cc
5 #include "BodyReader.h"
7 BodyReader::BodyReader(size_t len
, BodyReadFunc
*r
, BodyAbortFunc
*a
, BodyKickFunc
*k
, void *d
) :
8 _remaining(len
), _available(0),
9 read_func(r
), abort_func(a
), kick_func(k
), read_func_data(d
),
10 read_callback(NULL
), read_callback_data(NULL
)
12 theBuf
.init(4096, 65536);
13 debugs(32,3,HERE
<< this << " " << "created new BodyReader for content-length " << len
);
17 BodyReader::~BodyReader()
19 if (_remaining
&& abort_func
)
20 abort_func(read_func_data
, _remaining
);
22 if (callbackPending())
28 BodyReader::read(CBCB
*callback
, void *cbdata
)
30 assert(_remaining
|| theBuf
.contentSize());
31 debugs(32,3,HERE
<< this << " " << "remaining = " << _remaining
);
32 debugs(32,3,HERE
<< this << " " << "available = " << _available
);
34 if (read_callback
== NULL
) {
35 read_callback
= callback
;
36 read_callback_data
= cbdataReference(cbdata
);
38 assert(read_callback
== callback
);
39 assert(read_callback_data
== cbdata
);
42 if ((_available
== 0) && (theBuf
.contentSize() == 0)) {
43 debugs(32,3,HERE
<< this << " " << "read: no body data available, saving callback pointers");
46 kick_func(read_func_data
);
51 debugs(32,3,HERE
<< this << " " << "read_func=" << read_func
);
52 debugs(32,3,HERE
<< this << " " << "data=" << read_func_data
);
53 size_t size
= theBuf
.potentialSpaceSize();
55 debugs(32, 3, "BodyReader::read: available: " << _available
<< ", size " << size
<< ", remaining: " << _remaining
);
57 if (size
> _available
)
60 if (size
> _remaining
)
64 debugs(32,3,HERE
<< this << " " << "calling read_func for " << size
<< " bytes");
66 size_t nread
= read_func(read_func_data
, theBuf
, size
);
70 reduce_remaining(nread
);
72 debugs(32,3,HERE
<< this << " " << "Help, read_func() ret " << nread
);
76 if (theBuf
.contentSize() > 0) {
77 debugs(32,3,HERE
<< this << " have " << theBuf
.contentSize() << " bytes in theBuf, calling back");
83 BodyReader::notify(size_t now_available
)
85 debugs(32,3,HERE
<< this << " " << "old available = " << _available
);
86 debugs(32,3,HERE
<< this << " " << "now_available = " << now_available
);
87 _available
= now_available
;
89 if (!callbackPending()) {
90 debugs(32,3,HERE
<< this << " " << "no callback pending, nothing to do");
94 debugs(32,3,HERE
<< this << " " << "have data and pending callback, calling read()");
96 read(read_callback
, read_callback_data
);
100 BodyReader::callbackPending()
102 return read_callback
? true : false;
108 * Execute the read callback if there is a function registered
109 * and the read_callback_data is still valid.
112 BodyReader::doCallback()
114 CBCB
*t_callback
= read_callback
;
117 if (t_callback
== NULL
)
120 read_callback
= NULL
;
122 if (!cbdataReferenceValidDone(read_callback_data
, &t_cbdata
))
125 debugs(32,3,HERE
<< this << " doing callback, theBuf size = " << theBuf
.contentSize());
127 t_callback(theBuf
, t_cbdata
);
133 BodyReader::consume(size_t size
)
135 debugs(32,3,HERE
<< this << " BodyReader::consume consuming " << size
);
137 if (theBuf
.contentSize() < (mb_size_t
) size
) {
138 debugs(0,0,HERE
<< this << "BodyReader::consume failed");
139 debugs(0,0,HERE
<< this << "BodyReader::consume size = " << size
);
140 debugs(0,0,HERE
<< this << "BodyReader::consume contentSize() = " << theBuf
.contentSize());
144 theBuf
.consume(size
);
146 if (callbackPending() && _available
> 0) {
147 debugs(32,3,HERE
<< this << " " << "data avail and pending callback, calling read()");
148 read(read_callback
, read_callback_data
);
155 BodyReader::reduce_remaining(size_t size
)
157 assert(size
<= _remaining
);