]> git.ipfire.org Git - thirdparty/squid.git/blame - src/adaptation/ecap/XactionRep.cc
SourceFormat Enforcement
[thirdparty/squid.git] / src / adaptation / ecap / XactionRep.cc
CommitLineData
b510f3a1
AJ
1/*
2 * DEBUG: section 93 eCAP Interface
3 */
582c2af2 4#include "squid.h"
4d0854d4
AR
5#include <libecap/common/area.h>
6#include <libecap/common/delay.h>
22fff3bf
AR
7#include <libecap/common/named_values.h>
8#include <libecap/common/names.h>
fdc96a39 9#include <libecap/adapter/xaction.h>
fdc96a39
AR
10#include "HttpRequest.h"
11#include "HttpReply.h"
3ff65596 12#include "SquidTime.h"
1adcebc3 13#include "adaptation/Answer.h"
1f3c65fc 14#include "adaptation/ecap/XactionRep.h"
22fff3bf 15#include "adaptation/ecap/Config.h"
3af10ac0 16#include "adaptation/Initiator.h"
3d93a84d 17#include "base/TextException.h"
fdc96a39 18
574b508c 19CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Ecap::XactionRep, XactionRep);
fdc96a39 20
5038f9d8
AR
21/// a libecap Visitor for converting adapter transaction options to HttpHeader
22class OptionsExtractor: public libecap::NamedValueVisitor
23{
24public:
25 typedef libecap::Name Name;
26 typedef libecap::Area Area;
27
28 OptionsExtractor(HttpHeader &aMeta): meta(aMeta) {}
29
30 // libecap::NamedValueVisitor API
ec4d1a1d 31 virtual void visit(const Name &name, const Area &value) {
5038f9d8
AR
32 meta.putExt(name.image().c_str(), value.toString().c_str());
33 }
34
35 HttpHeader &meta; ///< where to put extracted options
36};
37
4299f876 38Adaptation::Ecap::XactionRep::XactionRep(
4cb2536f
A
39 HttpMsg *virginHeader, HttpRequest *virginCause,
40 const Adaptation::ServicePointer &aService):
574b508c 41 AsyncJob("Adaptation::Ecap::XactionRep"),
4299f876 42 Adaptation::Initiate("Adaptation::Ecap::XactionRep"),
a22e6cd3 43 theService(aService),
26ac0430 44 theVirginRep(virginHeader), theCauseRep(NULL),
e1e90d26 45 makingVb(opUndecided), proxyingAb(opUndecided),
3ff65596 46 adaptHistoryId(-1),
e1e90d26 47 vbProductionFinished(false),
7477a343 48 abProductionFinished(false), abProductionAtEnd(false)
fdc96a39 49{
027320b4 50 if (virginCause)
4d0854d4 51 theCauseRep = new MessageRep(virginCause);
fdc96a39
AR
52}
53
574b508c 54Adaptation::Ecap::XactionRep::~XactionRep()
fdc96a39
AR
55{
56 assert(!theMaster);
027320b4 57 delete theCauseRep;
4d0854d4 58 theAnswerRep.reset();
fdc96a39
AR
59}
60
61void
574b508c 62Adaptation::Ecap::XactionRep::master(const AdapterXaction &x)
fdc96a39
AR
63{
64 Must(!theMaster);
65 Must(x != NULL);
66 theMaster = x;
67}
68
a22e6cd3
AR
69Adaptation::Service &
70Adaptation::Ecap::XactionRep::service()
71{
72 Must(theService != NULL);
73 return *theService;
74}
75
22fff3bf
AR
76const libecap::Area
77Adaptation::Ecap::XactionRep::option(const libecap::Name &name) const
78{
79 if (name == libecap::metaClientIp)
80 return clientIpValue();
81 if (name == libecap::metaUserName)
82 return usernameValue();
71be37e0 83 if (Adaptation::Config::masterx_shared_name && name == Adaptation::Config::masterx_shared_name)
5038f9d8
AR
84 return masterxSharedValue(name);
85
86 // TODO: metaServerIp, metaAuthenticatedUser, and metaAuthenticatedGroups
71be37e0
CT
87
88 // If the name is unknown, metaValue returns an emtpy area
89 return metaValue(name);
22fff3bf
AR
90}
91
92void
93Adaptation::Ecap::XactionRep::visitEachOption(libecap::NamedValueVisitor &visitor) const
94{
95 if (const libecap::Area value = clientIpValue())
ec4d1a1d 96 visitor.visit(libecap::metaClientIp, value);
22fff3bf 97 if (const libecap::Area value = usernameValue())
ec4d1a1d 98 visitor.visit(libecap::metaUserName, value);
5038f9d8
AR
99
100 if (Adaptation::Config::masterx_shared_name) {
ec4d1a1d
A
101 const libecap::Name name(Adaptation::Config::masterx_shared_name);
102 if (const libecap::Area value = masterxSharedValue(name))
103 visitor.visit(name, value);
5038f9d8 104 }
71ee0835 105
71be37e0 106 visitEachMetaHeader(visitor);
5038f9d8
AR
107
108 // TODO: metaServerIp, metaAuthenticatedUser, and metaAuthenticatedGroups
22fff3bf
AR
109}
110
111const libecap::Area
112Adaptation::Ecap::XactionRep::clientIpValue() const
113{
114 const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ?
115 theCauseRep->raw().header : theVirginRep.raw().header);
116 Must(request);
117 // TODO: move this logic into HttpRequest::clientIp(bool) and
118 // HttpRequest::clientIpString(bool) and reuse everywhere
119 if (TheConfig.send_client_ip && request) {
120 Ip::Address client_addr;
121#if FOLLOW_X_FORWARDED_FOR
122 if (TheConfig.use_indirect_client) {
123 client_addr = request->indirect_client_addr;
ec4d1a1d 124 } else
22fff3bf
AR
125#endif
126 client_addr = request->client_addr;
127 if (!client_addr.IsAnyAddr() && !client_addr.IsNoAddr()) {
128 char ntoabuf[MAX_IPSTRLEN] = "";
129 client_addr.NtoA(ntoabuf,MAX_IPSTRLEN);
130 return libecap::Area::FromTempBuffer(ntoabuf, strlen(ntoabuf));
131 }
ec4d1a1d 132 }
22fff3bf
AR
133 return libecap::Area();
134}
135
136const libecap::Area
137Adaptation::Ecap::XactionRep::usernameValue() const
138{
321f219c 139#if USE_AUTH
22fff3bf
AR
140 const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ?
141 theCauseRep->raw().header : theVirginRep.raw().header);
142 Must(request);
143 if (request->auth_user_request != NULL) {
144 if (char const *name = request->auth_user_request->username())
145 return libecap::Area::FromTempBuffer(name, strlen(name));
cb72cd25 146 else if (request->extacl_user.defined() && request->extacl_user.size())
8661a1d0 147 return libecap::Area::FromTempBuffer(request->extacl_user.rawBuf(),
cb72cd25 148 request->extacl_user.size());
ec4d1a1d 149 }
321f219c 150#endif
22fff3bf
AR
151 return libecap::Area();
152}
153
5038f9d8
AR
154const libecap::Area
155Adaptation::Ecap::XactionRep::masterxSharedValue(const libecap::Name &name) const
156{
157 const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ?
158 theCauseRep->raw().header : theVirginRep.raw().header);
159 Must(request);
160 if (name.known()) { // must check to avoid empty names matching unset cfg
161 Adaptation::History::Pointer ah = request->adaptHistory(false);
162 if (ah != NULL) {
163 String name, value;
164 if (ah->getXxRecord(name, value))
165 return libecap::Area::FromTempBuffer(value.rawBuf(), value.size());
166 }
167 }
168 return libecap::Area();
169}
170
71be37e0
CT
171const libecap::Area
172Adaptation::Ecap::XactionRep::metaValue(const libecap::Name &name) const
173{
174 HttpRequest *request = dynamic_cast<HttpRequest*>(theCauseRep ?
71ee0835 175 theCauseRep->raw().header : theVirginRep.raw().header);
71be37e0
CT
176 Must(request);
177 HttpReply *reply = dynamic_cast<HttpReply*>(theVirginRep.raw().header);
178
179 if (name.known()) { // must check to avoid empty names matching unset cfg
180 typedef Adaptation::Config::MetaHeaders::iterator ACAMLI;
71ee0835 181 for (ACAMLI i = Adaptation::Config::metaHeaders.begin(); i != Adaptation::Config::metaHeaders.end(); ++i) {
71be37e0
CT
182 if (name == (*i)->name.termedBuf()) {
183 if (const char *value = (*i)->match(request, reply))
184 return libecap::Area::FromTempString(value);
185 else
186 return libecap::Area();
187 }
188 }
189 }
190
191 return libecap::Area();
192}
193
71ee0835 194void
71be37e0
CT
195Adaptation::Ecap::XactionRep::visitEachMetaHeader(libecap::NamedValueVisitor &visitor) const
196{
197 HttpRequest *request = dynamic_cast<HttpRequest*>(theCauseRep ?
71ee0835 198 theCauseRep->raw().header : theVirginRep.raw().header);
71be37e0
CT
199 Must(request);
200 HttpReply *reply = dynamic_cast<HttpReply*>(theVirginRep.raw().header);
71ee0835 201
71be37e0 202 typedef Adaptation::Config::MetaHeaders::iterator ACAMLI;
71ee0835 203 for (ACAMLI i = Adaptation::Config::metaHeaders.begin(); i != Adaptation::Config::metaHeaders.end(); ++i) {
baa31c42
HN
204 const char *v = (*i)->match(request, reply);
205 if (v) {
71be37e0
CT
206 const libecap::Name name((*i)->name.termedBuf());
207 const libecap::Area value = libecap::Area::FromTempString(v);
208 visitor.visit(name, value);
209 }
210 }
211}
212
fdc96a39 213void
574b508c 214Adaptation::Ecap::XactionRep::start()
fdc96a39
AR
215{
216 Must(theMaster);
4d0854d4 217
e1e90d26
AR
218 if (!theVirginRep.raw().body_pipe)
219 makingVb = opNever; // there is nothing to deliver
4d0854d4 220
3ff65596 221 const HttpRequest *request = dynamic_cast<const HttpRequest*> (theCauseRep ?
e1381638 222 theCauseRep->raw().header : theVirginRep.raw().header);
3ff65596 223 Must(request);
a22e6cd3 224 Adaptation::History::Pointer ah = request->adaptLogHistory();
e1381638 225 if (ah != NULL) {
3ff65596
AR
226 // retrying=false because ecap never retries transactions
227 adaptHistoryId = ah->recordXactStart(service().cfg().key, current_time, false);
228 }
229
fdc96a39
AR
230 theMaster->start();
231}
232
233void
574b508c 234Adaptation::Ecap::XactionRep::swanSong()
fdc96a39 235{
506a0530 236 // clear body_pipes, if any
ea76d91e 237 // this code does not maintain proxying* and canAccessVb states; should it?
506a0530
AR
238
239 if (theAnswerRep != NULL) {
f1a768b2
AR
240 BodyPipe::Pointer body_pipe = answer().body_pipe;
241 if (body_pipe != NULL) {
242 Must(body_pipe->stillProducing(this));
243 stopProducingFor(body_pipe, false);
244 }
245 }
506a0530 246
e1e90d26
AR
247 BodyPipe::Pointer &body_pipe = theVirginRep.raw().body_pipe;
248 if (body_pipe != NULL && body_pipe->stillConsuming(this))
249 stopConsumingFrom(body_pipe);
506a0530 250
fdc96a39 251 terminateMaster();
3ff65596
AR
252
253 const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ?
e1381638 254 theCauseRep->raw().header : theVirginRep.raw().header);
3ff65596 255 Must(request);
a22e6cd3 256 Adaptation::History::Pointer ah = request->adaptLogHistory();
3ff65596
AR
257 if (ah != NULL && adaptHistoryId >= 0)
258 ah->recordXactFinish(adaptHistoryId);
259
fdc96a39
AR
260 Adaptation::Initiate::swanSong();
261}
262
fdc96a39 263libecap::Message &
574b508c 264Adaptation::Ecap::XactionRep::virgin()
fdc96a39 265{
7b67e5b6 266 return theVirginRep;
fdc96a39
AR
267}
268
4d0854d4 269const libecap::Message &
574b508c 270Adaptation::Ecap::XactionRep::cause()
fdc96a39 271{
4d0854d4
AR
272 Must(theCauseRep != NULL);
273 return *theCauseRep;
fdc96a39
AR
274}
275
4d0854d4 276libecap::Message &
574b508c 277Adaptation::Ecap::XactionRep::adapted()
fdc96a39 278{
4d0854d4
AR
279 Must(theAnswerRep != NULL);
280 return *theAnswerRep;
281}
282
283Adaptation::Message &
574b508c 284Adaptation::Ecap::XactionRep::answer()
4d0854d4 285{
f1a768b2
AR
286 MessageRep *rep = dynamic_cast<MessageRep*>(theAnswerRep.get());
287 Must(rep);
4d0854d4
AR
288 return rep->raw();
289}
290
ea76d91e 291void
574b508c 292Adaptation::Ecap::XactionRep::terminateMaster()
4d0854d4
AR
293{
294 if (theMaster) {
ea76d91e
AR
295 AdapterXaction x = theMaster;
296 theMaster.reset();
297 x->stop();
f1a768b2 298 }
4d0854d4
AR
299}
300
4d0854d4 301bool
574b508c 302Adaptation::Ecap::XactionRep::doneAll() const
4d0854d4 303{
e1e90d26 304 return makingVb >= opComplete && proxyingAb >= opComplete &&
26ac0430 305 Adaptation::Initiate::doneAll();
4d0854d4
AR
306}
307
e1e90d26 308// stops receiving virgin and enables auto-consumption, dropping any vb bytes
4d0854d4 309void
e1e90d26 310Adaptation::Ecap::XactionRep::sinkVb(const char *reason)
4d0854d4 311{
e1e90d26 312 debugs(93,4, HERE << "sink for " << reason << "; status:" << status());
4d0854d4 313
e1e90d26
AR
314 // we reset raw().body_pipe when we are done, so use this one for checking
315 const BodyPipePointer &permPipe = theVirginRep.raw().header->body_pipe;
316 if (permPipe != NULL)
317 permPipe->enableAutoConsumption();
3af10ac0 318
e1e90d26
AR
319 forgetVb(reason);
320}
321
322// stops receiving virgin but preserves it for others to use
323void
324Adaptation::Ecap::XactionRep::preserveVb(const char *reason)
325{
326 debugs(93,4, HERE << "preserve for " << reason << "; status:" << status());
327
328 // we reset raw().body_pipe when we are done, so use this one for checking
329 const BodyPipePointer &permPipe = theVirginRep.raw().header->body_pipe;
330 if (permPipe != NULL) {
331 // if libecap consumed, we cannot preserve
332 Must(!permPipe->consumedSize());
3af10ac0
AR
333 }
334
e1e90d26
AR
335 forgetVb(reason);
336}
337
338// disassociates us from vb; the last step of sinking or preserving vb
339void
340Adaptation::Ecap::XactionRep::forgetVb(const char *reason)
341{
342 debugs(93,9, HERE << "forget vb " << reason << "; status:" << status());
ea76d91e 343
e1e90d26
AR
344 BodyPipePointer &p = theVirginRep.raw().body_pipe;
345 if (p != NULL && p->stillConsuming(this))
346 stopConsumingFrom(p);
347
348 if (makingVb == opUndecided)
349 makingVb = opNever;
350 else if (makingVb == opOn)
351 makingVb = opComplete;
fdc96a39
AR
352}
353
26ac0430 354void
574b508c 355Adaptation::Ecap::XactionRep::useVirgin()
fdc96a39 356{
4d0854d4 357 debugs(93,3, HERE << status());
ea76d91e
AR
358 Must(proxyingAb == opUndecided);
359 proxyingAb = opNever;
4d0854d4 360
e1e90d26 361 preserveVb("useVirgin");
2874d9e3
AR
362
363 HttpMsg *clone = theVirginRep.raw().header->clone();
364 // check that clone() copies the pipe so that we do not have to
e1e90d26 365 Must(!theVirginRep.raw().header->body_pipe == !clone->body_pipe);
ea76d91e 366
aaf0559d 367 updateHistory(clone);
3af10ac0 368 sendAnswer(Answer::Forward(clone));
4d0854d4 369 Must(done());
fdc96a39
AR
370}
371
26ac0430 372void
574b508c 373Adaptation::Ecap::XactionRep::useAdapted(const libecap::shared_ptr<libecap::Message> &m)
fdc96a39 374{
4d0854d4 375 debugs(93,3, HERE << status());
ea76d91e 376 Must(m);
4d0854d4 377 theAnswerRep = m;
ea76d91e
AR
378 Must(proxyingAb == opUndecided);
379
f1a768b2 380 HttpMsg *msg = answer().header;
ea76d91e
AR
381 if (!theAnswerRep->body()) { // final, bodyless answer
382 proxyingAb = opNever;
aaf0559d 383 updateHistory(msg);
3af10ac0 384 sendAnswer(Answer::Forward(msg));
f1a768b2 385 } else { // got answer headers but need to handle body
ea76d91e 386 proxyingAb = opOn;
f1a768b2 387 Must(!msg->body_pipe); // only host can set body pipes
ea76d91e 388 MessageRep *rep = dynamic_cast<MessageRep*>(theAnswerRep.get());
f1a768b2
AR
389 Must(rep);
390 rep->tieBody(this); // sets us as a producer
391 Must(msg->body_pipe != NULL); // check tieBody
ea76d91e 392
aaf0559d 393 updateHistory(msg);
3af10ac0 394 sendAnswer(Answer::Forward(msg));
ea76d91e 395
4d0854d4 396 debugs(93,4, HERE << "adapter will produce body" << status());
8679e6c2 397 theMaster->abMake(); // libecap will produce
4d0854d4 398 }
fdc96a39
AR
399}
400
3af10ac0
AR
401void
402Adaptation::Ecap::XactionRep::blockVirgin()
403{
404 debugs(93,3, HERE << status());
405 Must(proxyingAb == opUndecided);
406 proxyingAb = opNever;
407
e1e90d26 408 sinkVb("blockVirgin");
3af10ac0 409
aaf0559d 410 updateHistory(NULL);
3af10ac0
AR
411 sendAnswer(Answer::Block(service().cfg().key));
412 Must(done());
413}
414
5038f9d8
AR
415/// Called just before sendAnswer() to record adapter meta-information
416/// which may affect answer processing and may be needed for logging.
417void
aaf0559d 418Adaptation::Ecap::XactionRep::updateHistory(HttpMsg *adapted)
5038f9d8
AR
419{
420 if (!theMaster) // all updates rely on being able to query the adapter
421 return;
422
423 const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ?
424 theCauseRep->raw().header : theVirginRep.raw().header);
425 Must(request);
426
427 // TODO: move common ICAP/eCAP logic to Adaptation::Xaction or similar
428 // TODO: optimize Area-to-String conversion
429
430 // update the cross-transactional database if needed
431 if (const char *xxNameStr = Adaptation::Config::masterx_shared_name) {
432 Adaptation::History::Pointer ah = request->adaptHistory(true);
433 if (ah != NULL) {
434 libecap::Name xxName(xxNameStr); // TODO: optimize?
435 if (const libecap::Area val = theMaster->option(xxName))
436 ah->updateXxRecord(xxNameStr, val.toString().c_str());
437 }
438 }
439
440 // update the adaptation plan if needed
441 if (service().cfg().routing) {
442 String services;
443 if (const libecap::Area services = theMaster->option(libecap::metaNextServices)) {
444 Adaptation::History::Pointer ah = request->adaptHistory(true);
445 if (ah != NULL)
446 ah->updateNextServices(services.toString().c_str());
447 }
448 } // TODO: else warn (occasionally!) if we got libecap::metaNextServices
449
450 // Store received meta headers for adapt::<last_h logformat code use.
451 // If we already have stored headers from a previous adaptation transaction
452 // related to the same master transction, they will be replaced.
453 Adaptation::History::Pointer ah = request->adaptLogHistory();
454 if (ah != NULL) {
455 HttpHeader meta(hoReply);
456 OptionsExtractor extractor(meta);
457 theMaster->visitEachOption(extractor);
458 ah->recordMeta(&meta);
459 }
aaf0559d
AR
460
461 // Add just-created history to the adapted/cloned request that lacks it.
462 if (HttpRequest *adaptedReq = dynamic_cast<HttpRequest*>(adapted))
463 adaptedReq->adaptHistoryImport(*request);
5038f9d8
AR
464}
465
4d0854d4 466void
574b508c 467Adaptation::Ecap::XactionRep::vbDiscard()
fdc96a39 468{
e1e90d26 469 Must(makingVb == opUndecided);
8679e6c2 470 // if adapter does not need vb, we do not need to send it
e1e90d26
AR
471 sinkVb("vbDiscard");
472 Must(makingVb == opNever);
fdc96a39
AR
473}
474
4d0854d4 475void
574b508c 476Adaptation::Ecap::XactionRep::vbMake()
fdc96a39 477{
e1e90d26 478 Must(makingVb == opUndecided);
ea76d91e
AR
479 BodyPipePointer &p = theVirginRep.raw().body_pipe;
480 Must(p != NULL);
e1e90d26
AR
481 Must(p->setConsumerIfNotLate(this)); // to deliver vb, we must receive vb
482 makingVb = opOn;
fdc96a39
AR
483}
484
4d0854d4 485void
574b508c 486Adaptation::Ecap::XactionRep::vbStopMaking()
fdc96a39 487{
e1e90d26 488 Must(makingVb == opOn);
ea76d91e 489 // if adapter does not need vb, we do not need to receive it
e1e90d26
AR
490 sinkVb("vbStopMaking");
491 Must(makingVb == opComplete);
4d0854d4
AR
492}
493
494void
574b508c 495Adaptation::Ecap::XactionRep::vbMakeMore()
4d0854d4 496{
e1e90d26 497 Must(makingVb == opOn); // cannot make more if done proxying
ea76d91e 498 // we cannot guarantee more vb, but we can check that there is a chance
e1e90d26
AR
499 const BodyPipePointer &p = theVirginRep.raw().body_pipe;
500 Must(p != NULL && p->stillConsuming(this)); // we are plugged in
501 Must(!p->productionEnded() && p->mayNeedMoreData()); // and may get more
4d0854d4
AR
502}
503
8679e6c2 504libecap::Area
574b508c 505Adaptation::Ecap::XactionRep::vbContent(libecap::size_type o, libecap::size_type s)
4d0854d4 506{
e1e90d26 507 // We may not be makingVb yet. It should be OK, but see vbContentShift().
ea76d91e 508
8679e6c2 509 const BodyPipePointer &p = theVirginRep.raw().body_pipe;
ea76d91e
AR
510 Must(p != NULL);
511
512 // TODO: make MemBuf use size_t?
513 const size_t haveSize = static_cast<size_t>(p->buf().contentSize());
4d0854d4 514
8679e6c2
AR
515 // convert to Squid types; XXX: check for overflow
516 const uint64_t offset = static_cast<uint64_t>(o);
517 Must(offset <= haveSize); // equal iff at the end of content
518
519 // nsize means no size limit: all content starting from offset
520 const size_t size = s == libecap::nsize ?
26ac0430 521 haveSize - offset : static_cast<size_t>(s);
8679e6c2 522
8679e6c2
AR
523 // XXX: optimize by making theBody a shared_ptr (see Area::FromTemp*() src)
524 return libecap::Area::FromTempBuffer(p->buf().content() + offset,
26ac0430 525 min(static_cast<size_t>(haveSize - offset), size));
4d0854d4
AR
526}
527
528void
574b508c 529Adaptation::Ecap::XactionRep::vbContentShift(libecap::size_type n)
4d0854d4 530{
e1e90d26 531 // We may not be makingVb yet. It should be OK now, but if BodyPipe
ea76d91e
AR
532 // consume() requirements change, we would have to return empty vbContent
533 // until the adapter registers as a consumer
534
8679e6c2
AR
535 BodyPipePointer &p = theVirginRep.raw().body_pipe;
536 Must(p != NULL);
537 const size_t size = static_cast<size_t>(n); // XXX: check for overflow
538 const size_t haveSize = static_cast<size_t>(p->buf().contentSize()); // TODO: make MemBuf use size_t?
539 p->consume(min(size, haveSize));
4d0854d4
AR
540}
541
542void
574b508c 543Adaptation::Ecap::XactionRep::noteAbContentDone(bool atEnd)
4d0854d4 544{
7477a343
AR
545 Must(proxyingAb == opOn && !abProductionFinished);
546 abProductionFinished = true;
547 abProductionAtEnd = atEnd; // store until ready to stop producing ourselves
548 debugs(93,5, HERE << "adapted body production ended");
549 moveAbContent();
4d0854d4
AR
550}
551
8679e6c2 552void
574b508c 553Adaptation::Ecap::XactionRep::noteAbContentAvailable()
4d0854d4 554{
7477a343 555 Must(proxyingAb == opOn && !abProductionFinished);
8679e6c2 556 moveAbContent();
4d0854d4
AR
557}
558
ea76d91e 559#if 0 /* XXX: implement */
4d0854d4 560void
574b508c 561Adaptation::Ecap::XactionRep::setAdaptedBodySize(const libecap::BodySize &size)
4d0854d4
AR
562{
563 Must(answer().body_pipe != NULL);
8679e6c2
AR
564 if (size.known())
565 answer().body_pipe->setBodySize(size.value());
566 // else the piped body size is unknown by default
4d0854d4 567}
8679e6c2 568#endif
4d0854d4
AR
569
570void
574b508c 571Adaptation::Ecap::XactionRep::adaptationDelayed(const libecap::Delay &d)
4d0854d4
AR
572{
573 debugs(93,3, HERE << "adapter needs time: " <<
26ac0430 574 d.state << '/' << d.progress);
4d0854d4 575 // XXX: set timeout?
fdc96a39
AR
576}
577
26ac0430 578void
574b508c 579Adaptation::Ecap::XactionRep::adaptationAborted()
fdc96a39 580{
fdc96a39 581 tellQueryAborted(true); // should eCAP support retries?
ea76d91e 582 mustStop("adaptationAborted");
fdc96a39
AR
583}
584
8679e6c2 585bool
574b508c 586Adaptation::Ecap::XactionRep::callable() const
8679e6c2
AR
587{
588 return !done();
589}
590
26ac0430 591void
574b508c 592Adaptation::Ecap::XactionRep::noteMoreBodySpaceAvailable(RefCount<BodyPipe> bp)
fdc96a39 593{
ea76d91e
AR
594 Must(proxyingAb == opOn);
595 moveAbContent();
fdc96a39
AR
596}
597
26ac0430 598void
574b508c 599Adaptation::Ecap::XactionRep::noteBodyConsumerAborted(RefCount<BodyPipe> bp)
fdc96a39 600{
ea76d91e
AR
601 Must(proxyingAb == opOn);
602 stopProducingFor(answer().body_pipe, false);
603 Must(theMaster);
604 theMaster->abStopMaking();
605 proxyingAb = opComplete;
fdc96a39
AR
606}
607
608void
574b508c 609Adaptation::Ecap::XactionRep::noteMoreBodyDataAvailable(RefCount<BodyPipe> bp)
fdc96a39 610{
e1e90d26 611 Must(makingVb == opOn); // or we would not be registered as a consumer
fdc96a39 612 Must(theMaster);
8679e6c2 613 theMaster->noteVbContentAvailable();
fdc96a39
AR
614}
615
616void
574b508c 617Adaptation::Ecap::XactionRep::noteBodyProductionEnded(RefCount<BodyPipe> bp)
fdc96a39 618{
e1e90d26 619 Must(makingVb == opOn); // or we would not be registered as a consumer
fdc96a39 620 Must(theMaster);
8679e6c2 621 theMaster->noteVbContentDone(true);
e1e90d26 622 vbProductionFinished = true;
fdc96a39
AR
623}
624
625void
574b508c 626Adaptation::Ecap::XactionRep::noteBodyProducerAborted(RefCount<BodyPipe> bp)
fdc96a39 627{
e1e90d26 628 Must(makingVb == opOn); // or we would not be registered as a consumer
8679e6c2
AR
629 Must(theMaster);
630 theMaster->noteVbContentDone(false);
e1e90d26 631 vbProductionFinished = true;
fdc96a39
AR
632}
633
634void
574b508c 635Adaptation::Ecap::XactionRep::noteInitiatorAborted()
fdc96a39
AR
636{
637 mustStop("initiator aborted");
638}
639
8679e6c2
AR
640// get content from the adapter and put it into the adapted pipe
641void
574b508c 642Adaptation::Ecap::XactionRep::moveAbContent()
8679e6c2 643{
ea76d91e 644 Must(proxyingAb == opOn);
8679e6c2 645 const libecap::Area c = theMaster->abContent(0, libecap::nsize);
7477a343
AR
646 debugs(93,5, HERE << "up to " << c.size << " bytes");
647 if (c.size == 0 && abProductionFinished) { // no ab now and in the future
648 stopProducingFor(answer().body_pipe, abProductionAtEnd);
649 proxyingAb = opComplete;
650 debugs(93,5, HERE << "last adapted body data retrieved");
e1381638 651 } else if (c.size > 0) {
7477a343
AR
652 if (const size_t used = answer().body_pipe->putMoreData(c.start, c.size))
653 theMaster->abContentShift(used);
654 }
8679e6c2
AR
655}
656
657const char *
574b508c 658Adaptation::Ecap::XactionRep::status() const
fdc96a39 659{
4d0854d4
AR
660 static MemBuf buf;
661 buf.reset();
662
663 buf.append(" [", 2);
664
e1e90d26
AR
665 if (makingVb)
666 buf.Printf("M%d", static_cast<int>(makingVb));
667
668 const BodyPipePointer &vp = theVirginRep.raw().body_pipe;
669 if (!vp)
670 buf.append(" !V", 3);
ec4d1a1d 671 else if (vp->stillConsuming(const_cast<XactionRep*>(this)))
e1e90d26
AR
672 buf.append(" Vc", 3);
673 else
674 buf.append(" V?", 3);
675
676 if (vbProductionFinished)
677 buf.append(".", 1);
678
e1e90d26 679 buf.Printf(" A%d", static_cast<int>(proxyingAb));
506a0530 680
ea76d91e
AR
681 if (proxyingAb == opOn) {
682 MessageRep *rep = dynamic_cast<MessageRep*>(theAnswerRep.get());
683 Must(rep);
f1a768b2 684 const BodyPipePointer &ap = rep->raw().body_pipe;
e1e90d26
AR
685 if (!ap)
686 buf.append(" !A", 3);
687 else if (ap->stillProducing(const_cast<XactionRep*>(this)))
688 buf.append(" Ap", 3);
689 else
690 buf.append(" A?", 3);
f1a768b2 691 }
4d0854d4 692
52ed047a 693 buf.Printf(" %s%u]", id.Prefix, id.value);
4d0854d4
AR
694
695 buf.terminate();
696
697 return buf.content();
fdc96a39 698}