]> git.ipfire.org Git - thirdparty/squid.git/blame - src/BodyPipe.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / BodyPipe.cc
CommitLineData
1b39caaa 1
f7f3304a 2#include "squid-old.h"
4299f876 3#include "base/AsyncJobCalls.h"
3d93a84d 4#include "base/TextException.h"
1b39caaa 5#include "BodyPipe.h"
6
7CBDATA_CLASS_INIT(BodyPipe);
8
e7352f30 9// BodySink is a BodyConsumer class which just consume and drops
26ac0430
AJ
10// data from a BodyPipe
11class BodySink: public BodyConsumer
12{
e7352f30 13public:
5a856d1e
AR
14 BodySink(const BodyPipe::Pointer &bp): AsyncJob("BodySink"), body_pipe(bp) {}
15 virtual ~BodySink() { assert(!body_pipe); }
26ac0430 16
e7352f30 17 virtual void noteMoreBodyDataAvailable(BodyPipe::Pointer bp) {
26ac0430
AJ
18 size_t contentSize = bp->buf().contentSize();
19 bp->consume(contentSize);
e7352f30 20 }
21 virtual void noteBodyProductionEnded(BodyPipe::Pointer bp) {
5a856d1e 22 stopConsumingFrom(body_pipe);
e7352f30 23 }
24 virtual void noteBodyProducerAborted(BodyPipe::Pointer bp) {
5a856d1e 25 stopConsumingFrom(body_pipe);
e7352f30 26 }
5a856d1e
AR
27 bool doneAll() const {return !body_pipe && AsyncJob::doneAll();}
28
29private:
30 BodyPipe::Pointer body_pipe; ///< the pipe we are consuming from
31
e7352f30 32 CBDATA_CLASS2(BodySink);
33};
34
35CBDATA_CLASS_INIT(BodySink);
36
37// The BodyProducerDialer is an AsyncCall class which used to schedule BodyProducer calls.
26ac0430 38// In addition to a normal AsyncCall checks if the BodyProducer is still the producer of
e7352f30 39// the BodyPipe passed as argument
40class BodyProducerDialer: public UnaryMemFunT<BodyProducer, BodyPipe::Pointer>
41{
42public:
26ac0430 43 typedef UnaryMemFunT<BodyProducer, BodyPipe::Pointer> Parent;
e7352f30 44
4299f876 45 BodyProducerDialer(const BodyProducer::Pointer &aProducer,
4cb2536f
A
46 Parent::Method aHandler, BodyPipe::Pointer bp):
47 Parent(aProducer, aHandler, bp) {}
e7352f30 48
26ac0430 49 virtual bool canDial(AsyncCall &call);
e7352f30 50};
51
52// The BodyConsumerDialer is an AsyncCall class which used to schedule BodyConsumer calls.
26ac0430 53// In addition to a normal AsyncCall checks if the BodyConsumer is still the reciptient
e7352f30 54// of the BodyPipe passed as argument
55class BodyConsumerDialer: public UnaryMemFunT<BodyConsumer, BodyPipe::Pointer>
56{
57public:
26ac0430 58 typedef UnaryMemFunT<BodyConsumer, BodyPipe::Pointer> Parent;
e7352f30 59
4299f876 60 BodyConsumerDialer(const BodyConsumer::Pointer &aConsumer,
4cb2536f
A
61 Parent::Method aHandler, BodyPipe::Pointer bp):
62 Parent(aConsumer, aHandler, bp) {}
e7352f30 63
26ac0430 64 virtual bool canDial(AsyncCall &call);
e7352f30 65};
66
67bool
26ac0430
AJ
68BodyProducerDialer::canDial(AsyncCall &call)
69{
70 if (!Parent::canDial(call))
71 return false;
72
4299f876 73 const BodyProducer::Pointer &producer = job;
26ac0430
AJ
74 BodyPipe::Pointer pipe = arg1;
75 if (!pipe->stillProducing(producer)) {
76 debugs(call.debugSection, call.debugLevel, HERE << producer <<
77 " no longer producing for " << pipe->status());
78 return call.cancel("no longer producing");
79 }
e7352f30 80
26ac0430 81 return true;
e7352f30 82}
83
84bool
26ac0430
AJ
85BodyConsumerDialer::canDial(AsyncCall &call)
86{
87 if (!Parent::canDial(call))
88 return false;
89
4299f876 90 const BodyConsumer::Pointer &consumer = job;
26ac0430
AJ
91 BodyPipe::Pointer pipe = arg1;
92 if (!pipe->stillConsuming(consumer)) {
93 debugs(call.debugSection, call.debugLevel, HERE << consumer <<
94 " no longer consuming from " << pipe->status());
95 return call.cancel("no longer consuming");
96 }
e7352f30 97
26ac0430 98 return true;
e7352f30 99}
100
101
102/* BodyProducer */
103
1b39caaa 104// inform the pipe that we are done and clear the Pointer
105void BodyProducer::stopProducingFor(RefCount<BodyPipe> &pipe, bool atEof)
106{
26ac0430
AJ
107 debugs(91,7, HERE << this << " will not produce for " << pipe <<
108 "; atEof: " << atEof);
109 assert(pipe != NULL); // be strict: the caller state may depend on this
110 pipe->clearProducer(atEof);
111 pipe = NULL;
1b39caaa 112}
113
e7352f30 114
115
116/* BodyConsumer */
117
1b39caaa 118// inform the pipe that we are done and clear the Pointer
119void BodyConsumer::stopConsumingFrom(RefCount<BodyPipe> &pipe)
120{
26ac0430
AJ
121 debugs(91,7, HERE << this << " will not consume from " << pipe);
122 assert(pipe != NULL); // be strict: the caller state may depend on this
123 pipe->clearConsumer();
124 pipe = NULL;
1b39caaa 125}
126
e7352f30 127
1b39caaa 128/* BodyPipe */
129
130BodyPipe::BodyPipe(Producer *aProducer): theBodySize(-1),
26ac0430
AJ
131 theProducer(aProducer), theConsumer(0),
132 thePutSize(0), theGetSize(0),
abe286b8 133 mustAutoConsume(false), abortedConsumption(false), isCheckedOut(false)
1b39caaa 134{
26ac0430
AJ
135 // TODO: teach MemBuf to start with zero minSize
136 // TODO: limit maxSize by theBodySize, when known?
137 theBuf.init(2*1024, MaxCapacity);
138 debugs(91,7, HERE << "created BodyPipe" << status());
1b39caaa 139}
140
141BodyPipe::~BodyPipe()
142{
26ac0430
AJ
143 debugs(91,7, HERE << "destroying BodyPipe" << status());
144 assert(!theProducer);
145 assert(!theConsumer);
146 theBuf.clean();
1b39caaa 147}
148
47f6e231 149void BodyPipe::setBodySize(uint64_t aBodySize)
1b39caaa 150{
26ac0430 151 assert(!bodySizeKnown());
26ac0430 152 assert(thePutSize <= aBodySize);
1b39caaa 153
26ac0430
AJ
154 // If this assert fails, we need to add code to check for eof and inform
155 // the consumer about the eof condition via scheduleBodyEndNotification,
156 // because just setting a body size limit may trigger the eof condition.
157 assert(!theConsumer);
1b39caaa 158
26ac0430
AJ
159 theBodySize = aBodySize;
160 debugs(91,7, HERE << "set body size" << status());
1b39caaa 161}
162
47f6e231 163uint64_t BodyPipe::bodySize() const
1b39caaa 164{
26ac0430
AJ
165 assert(bodySizeKnown());
166 return static_cast<uint64_t>(theBodySize);
1b39caaa 167}
168
47f6e231 169bool BodyPipe::expectMoreAfter(uint64_t offset) const
1b39caaa 170{
26ac0430
AJ
171 assert(theGetSize <= offset);
172 return offset < thePutSize || // buffer has more now or
173 (!productionEnded() && mayNeedMoreData()); // buffer will have more
1b39caaa 174}
175
176bool BodyPipe::exhausted() const
177{
26ac0430 178 return !expectMoreAfter(theGetSize);
1b39caaa 179}
180
610d8f3b 181uint64_t BodyPipe::unproducedSize() const
1b39caaa 182{
26ac0430 183 return bodySize() - thePutSize; // bodySize() asserts that size is known
1b39caaa 184}
185
83c51da9
CT
186void BodyPipe::expectProductionEndAfter(uint64_t size)
187{
188 const uint64_t expectedSize = thePutSize + size;
189 if (bodySizeKnown())
190 Must(bodySize() == expectedSize);
191 else
192 theBodySize = expectedSize;
193}
194
1b39caaa 195void
196BodyPipe::clearProducer(bool atEof)
197{
4299f876 198 if (theProducer.set()) {
26ac0430 199 debugs(91,7, HERE << "clearing BodyPipe producer" << status());
4299f876 200 theProducer.clear();
26ac0430
AJ
201 if (atEof) {
202 if (!bodySizeKnown())
203 theBodySize = thePutSize;
e1381638
AJ
204 else if (bodySize() != thePutSize)
205 debugs(91,3, HERE << "aborting on premature eof" << status());
26ac0430
AJ
206 } else {
207 // asserta that we can detect the abort if the consumer joins later
208 assert(!bodySizeKnown() || bodySize() != thePutSize);
209 }
210 scheduleBodyEndNotification();
211 }
1b39caaa 212}
213
214size_t
350e2aec 215BodyPipe::putMoreData(const char *aBuffer, size_t size)
1b39caaa 216{
26ac0430 217 if (bodySizeKnown())
d85c3078 218 size = min((uint64_t)size, unproducedSize());
26ac0430
AJ
219
220 const size_t spaceSize = static_cast<size_t>(theBuf.potentialSpaceSize());
d85c3078 221 if ((size = min(size, spaceSize))) {
350e2aec 222 theBuf.append(aBuffer, size);
26ac0430
AJ
223 postAppend(size);
224 return size;
225 }
226 return 0;
1b39caaa 227}
228
229bool
4299f876 230BodyPipe::setConsumerIfNotLate(const Consumer::Pointer &aConsumer)
1b39caaa 231{
26ac0430 232 assert(!theConsumer);
4299f876 233 assert(aConsumer.set()); // but might be invalid
26ac0430
AJ
234
235 // TODO: convert this into an exception and remove IfNotLate suffix
236 // If there is something consumed already, we are in an auto-consuming mode
237 // and it is too late to attach a real consumer to the pipe.
238 if (theGetSize > 0) {
239 assert(mustAutoConsume);
240 return false;
241 }
1b39caaa 242
abe286b8
AR
243 Must(!abortedConsumption); // did not promise to never consume
244
26ac0430
AJ
245 theConsumer = aConsumer;
246 debugs(91,7, HERE << "set consumer" << status());
247 if (theBuf.hasContent())
248 scheduleBodyDataNotification();
249 if (!theProducer)
250 scheduleBodyEndNotification();
1b39caaa 251
26ac0430 252 return true;
1b39caaa 253}
254
255void
26ac0430
AJ
256BodyPipe::clearConsumer()
257{
4299f876 258 if (theConsumer.set()) {
26ac0430 259 debugs(91,7, HERE << "clearing consumer" << status());
4299f876 260 theConsumer.clear();
abe286b8
AR
261 // do not abort if we have not consumed so that HTTP or ICAP can retry
262 // benign xaction failures due to persistent connection race conditions
263 if (consumedSize())
264 expectNoConsumption();
265 }
266}
267
268void
269BodyPipe::expectNoConsumption()
270{
271 Must(!theConsumer);
272 if (!abortedConsumption && !exhausted()) {
e6f9e263
A
273 AsyncCall::Pointer call= asyncCall(91, 7,
274 "BodyProducer::noteBodyConsumerAborted",
275 BodyProducerDialer(theProducer,
276 &BodyProducer::noteBodyConsumerAborted, this));
277 ScheduleCallHere(call);
278 abortedConsumption = true;
26ac0430 279 }
1b39caaa 280}
281
282size_t
350e2aec 283BodyPipe::getMoreData(MemBuf &aMemBuffer)
1b39caaa 284{
26ac0430
AJ
285 if (!theBuf.hasContent())
286 return 0; // did not touch the possibly uninitialized buf
1b39caaa 287
350e2aec
FC
288 if (aMemBuffer.isNull())
289 aMemBuffer.init();
290 const size_t size = min(theBuf.contentSize(), aMemBuffer.potentialSpaceSize());
291 aMemBuffer.append(theBuf.content(), size);
26ac0430
AJ
292 theBuf.consume(size);
293 postConsume(size);
294 return size; // cannot be zero if we called buf.init above
1b39caaa 295}
296
297void
298BodyPipe::consume(size_t size)
299{
26ac0430
AJ
300 theBuf.consume(size);
301 postConsume(size);
1b39caaa 302}
303
26ac0430 304// In the AutoConsumption mode the consumer has gone but the producer continues
e7352f30 305// producing data. We are using a BodySink BodyConsumer which just discards the produced data.
1b39caaa 306void
26ac0430
AJ
307BodyPipe::enableAutoConsumption()
308{
309 mustAutoConsume = true;
310 debugs(91,5, HERE << "enabled auto consumption" << status());
311 if (!theConsumer && theBuf.hasContent())
312 startAutoConsumption();
5121d48e
AR
313}
314
315// start auto consumption by creating body sink
316void
317BodyPipe::startAutoConsumption()
318{
26ac0430
AJ
319 Must(mustAutoConsume);
320 Must(!theConsumer);
5a856d1e 321 theConsumer = new BodySink(this);
26ac0430
AJ
322 debugs(91,7, HERE << "starting auto consumption" << status());
323 scheduleBodyDataNotification();
1b39caaa 324}
325
326MemBuf &
26ac0430
AJ
327BodyPipe::checkOut()
328{
329 assert(!isCheckedOut);
330 isCheckedOut = true;
331 return theBuf;
1b39caaa 332}
333
334void
335BodyPipe::checkIn(Checkout &checkout)
336{
26ac0430
AJ
337 assert(isCheckedOut);
338 isCheckedOut = false;
339 const size_t currentSize = theBuf.contentSize();
340 if (checkout.checkedOutSize > currentSize)
341 postConsume(checkout.checkedOutSize - currentSize);
e1381638
AJ
342 else if (checkout.checkedOutSize < currentSize)
343 postAppend(currentSize - checkout.checkedOutSize);
1b39caaa 344}
345
346void
347BodyPipe::undoCheckOut(Checkout &checkout)
348{
26ac0430
AJ
349 assert(isCheckedOut);
350 const size_t currentSize = theBuf.contentSize();
351 // We can only undo if size did not change, and even that carries
352 // some risk. If this becomes a problem, the code checking out
353 // raw buffers should always check them in (possibly unchanged)
354 // instead of relying on the automated undo mechanism of Checkout.
355 // The code can always use a temporary buffer to accomplish that.
87728a5b 356 Must(checkout.checkedOutSize == currentSize);
1b39caaa 357}
358
359// TODO: Optimize: inform consumer/producer about more data/space only if
360// they used the data/space since we notified them last time.
361
362void
26ac0430
AJ
363BodyPipe::postConsume(size_t size)
364{
365 assert(!isCheckedOut);
366 theGetSize += size;
367 debugs(91,7, HERE << "consumed " << size << " bytes" << status());
368 if (mayNeedMoreData()) {
369 AsyncCall::Pointer call= asyncCall(91, 7,
370 "BodyProducer::noteMoreBodySpaceAvailable",
371 BodyProducerDialer(theProducer,
372 &BodyProducer::noteMoreBodySpaceAvailable, this));
373 ScheduleCallHere(call);
374 }
1b39caaa 375}
376
377void
26ac0430
AJ
378BodyPipe::postAppend(size_t size)
379{
380 assert(!isCheckedOut);
381 thePutSize += size;
382 debugs(91,7, HERE << "added " << size << " bytes" << status());
1b39caaa 383
26ac0430
AJ
384 if (mustAutoConsume && !theConsumer && size > 0)
385 startAutoConsumption();
5121d48e 386
26ac0430
AJ
387 // We should not consume here even if mustAutoConsume because the
388 // caller may not be ready for the data to be consumed during this call.
389 scheduleBodyDataNotification();
1b39caaa 390
26ac0430
AJ
391 if (!mayNeedMoreData())
392 clearProducer(true); // reached end-of-body
1b39caaa 393}
394
395
6c56baf6 396void
397BodyPipe::scheduleBodyDataNotification()
398{
4299f876 399 if (theConsumer.valid()) { // TODO: allow asyncCall() to check this instead
26ac0430
AJ
400 AsyncCall::Pointer call = asyncCall(91, 7,
401 "BodyConsumer::noteMoreBodyDataAvailable",
402 BodyConsumerDialer(theConsumer,
403 &BodyConsumer::noteMoreBodyDataAvailable, this));
404 ScheduleCallHere(call);
405 }
6c56baf6 406}
407
1b39caaa 408void
409BodyPipe::scheduleBodyEndNotification()
410{
4299f876 411 if (theConsumer.valid()) { // TODO: allow asyncCall() to check this instead
26ac0430
AJ
412 if (bodySizeKnown() && bodySize() == thePutSize) {
413 AsyncCall::Pointer call = asyncCall(91, 7,
414 "BodyConsumer::noteBodyProductionEnded",
415 BodyConsumerDialer(theConsumer,
416 &BodyConsumer::noteBodyProductionEnded, this));
417 ScheduleCallHere(call);
418 } else {
419 AsyncCall::Pointer call = asyncCall(91, 7,
420 "BodyConsumer::noteBodyProducerAborted",
421 BodyConsumerDialer(theConsumer,
422 &BodyConsumer::noteBodyProducerAborted, this));
423 ScheduleCallHere(call);
424 }
425 }
1b39caaa 426}
427
1b39caaa 428// a short temporary string describing buffer status for debugging
429const char *BodyPipe::status() const
430{
350e2aec
FC
431 static MemBuf outputBuffer;
432 outputBuffer.reset();
1b39caaa 433
350e2aec 434 outputBuffer.append(" [", 2);
1b39caaa 435
350e2aec 436 outputBuffer.Printf("%"PRIu64"<=%"PRIu64, theGetSize, thePutSize);
1b39caaa 437 if (theBodySize >= 0)
350e2aec 438 outputBuffer.Printf("<=%"PRId64, theBodySize);
26ac0430 439 else
350e2aec 440 outputBuffer.append("<=?", 3);
1b39caaa 441
350e2aec 442 outputBuffer.Printf(" %d+%d", (int)theBuf.contentSize(), (int)theBuf.spaceSize());
1b39caaa 443
350e2aec 444 outputBuffer.Printf(" pipe%p", this);
4299f876
AR
445 if (theProducer.set())
446 outputBuffer.Printf(" prod%p", theProducer.get());
447 if (theConsumer.set())
448 outputBuffer.Printf(" cons%p", theConsumer.get());
1b39caaa 449
26ac0430 450 if (mustAutoConsume)
350e2aec 451 outputBuffer.append(" A", 2);
abe286b8
AR
452 if (abortedConsumption)
453 outputBuffer.append(" !C", 3);
26ac0430 454 if (isCheckedOut)
350e2aec 455 outputBuffer.append(" L", 2); // Locked
1b39caaa 456
350e2aec 457 outputBuffer.append("]", 1);
1b39caaa 458
350e2aec 459 outputBuffer.terminate();
1b39caaa 460
350e2aec 461 return outputBuffer.content();
1b39caaa 462}
463
464
465/* BodyPipeCheckout */
466
467BodyPipeCheckout::BodyPipeCheckout(BodyPipe &aPipe): pipe(aPipe),
26ac0430
AJ
468 buf(aPipe.checkOut()), offset(aPipe.consumedSize()),
469 checkedOutSize(buf.contentSize()), checkedIn(false)
1b39caaa 470{
471}
472
473BodyPipeCheckout::~BodyPipeCheckout()
474{
87728a5b
AR
475 if (!checkedIn) {
476 // Do not pipe.undoCheckOut(*this) because it asserts or throws
477 // TODO: consider implementing the long-term solution discussed at
478 // http://www.mail-archive.com/squid-dev@squid-cache.org/msg07910.html
479 debugs(91,2, HERE << "Warning: cannot undo BodyPipeCheckout");
480 pipe.checkIn(*this);
481 }
1b39caaa 482}
483
484void
485BodyPipeCheckout::checkIn()
486{
26ac0430
AJ
487 assert(!checkedIn);
488 pipe.checkIn(*this);
489 checkedIn = true;
1b39caaa 490}
491
492
493BodyPipeCheckout::BodyPipeCheckout(const BodyPipeCheckout &c): pipe(c.pipe),
26ac0430
AJ
494 buf(c.buf), offset(c.offset), checkedOutSize(c.checkedOutSize),
495 checkedIn(c.checkedIn)
1b39caaa 496{
26ac0430 497 assert(false); // prevent copying
1b39caaa 498}
499
500BodyPipeCheckout &
501BodyPipeCheckout::operator =(const BodyPipeCheckout &)
502{
26ac0430
AJ
503 assert(false); // prevent assignment
504 return *this;
1b39caaa 505}