]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/XactionRep.cc
7ee3f1d9e3dcbfa6d516575b92a21d2fb389ef3b
2 * DEBUG: section 93 eCAP Interface
5 #include <libecap/common/area.h>
6 #include <libecap/common/delay.h>
7 #include <libecap/common/named_values.h>
8 #include <libecap/common/names.h>
9 #include <libecap/adapter/xaction.h>
10 #include "adaptation/Answer.h"
11 #include "adaptation/ecap/Config.h"
12 #include "adaptation/ecap/XactionRep.h"
13 #include "adaptation/Initiator.h"
14 #include "base/TextException.h"
15 #include "HttpReply.h"
16 #include "HttpRequest.h"
17 #include "SquidTime.h"
19 CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Ecap::XactionRep
, XactionRep
);
21 /// a libecap Visitor for converting adapter transaction options to HttpHeader
22 class OptionsExtractor
: public libecap::NamedValueVisitor
25 typedef libecap::Name Name
;
26 typedef libecap::Area Area
;
28 OptionsExtractor(HttpHeader
&aMeta
): meta(aMeta
) {}
30 // libecap::NamedValueVisitor API
31 virtual void visit(const Name
&name
, const Area
&value
) {
32 meta
.putExt(name
.image().c_str(), value
.toString().c_str());
35 HttpHeader
&meta
; ///< where to put extracted options
38 Adaptation::Ecap::XactionRep::XactionRep(
39 HttpMsg
*virginHeader
, HttpRequest
*virginCause
,
40 const Adaptation::ServicePointer
&aService
):
41 AsyncJob("Adaptation::Ecap::XactionRep"),
42 Adaptation::Initiate("Adaptation::Ecap::XactionRep"),
44 theVirginRep(virginHeader
), theCauseRep(NULL
),
45 makingVb(opUndecided
), proxyingAb(opUndecided
),
47 vbProductionFinished(false),
48 abProductionFinished(false), abProductionAtEnd(false)
51 theCauseRep
= new MessageRep(virginCause
);
54 Adaptation::Ecap::XactionRep::~XactionRep()
62 Adaptation::Ecap::XactionRep::master(const AdapterXaction
&x
)
70 Adaptation::Ecap::XactionRep::service()
72 Must(theService
!= NULL
);
77 Adaptation::Ecap::XactionRep::option(const libecap::Name
&name
) const
79 if (name
== libecap::metaClientIp
)
80 return clientIpValue();
81 if (name
== libecap::metaUserName
)
82 return usernameValue();
83 if (Adaptation::Config::masterx_shared_name
&& name
== Adaptation::Config::masterx_shared_name
)
84 return masterxSharedValue(name
);
86 // TODO: metaServerIp, metaAuthenticatedUser, and metaAuthenticatedGroups
88 // If the name is unknown, metaValue returns an emtpy area
89 return metaValue(name
);
93 Adaptation::Ecap::XactionRep::visitEachOption(libecap::NamedValueVisitor
&visitor
) const
95 if (const libecap::Area value
= clientIpValue())
96 visitor
.visit(libecap::metaClientIp
, value
);
97 if (const libecap::Area value
= usernameValue())
98 visitor
.visit(libecap::metaUserName
, value
);
100 if (Adaptation::Config::masterx_shared_name
) {
101 const libecap::Name
name(Adaptation::Config::masterx_shared_name
);
102 if (const libecap::Area value
= masterxSharedValue(name
))
103 visitor
.visit(name
, value
);
106 visitEachMetaHeader(visitor
);
108 // TODO: metaServerIp, metaAuthenticatedUser, and metaAuthenticatedGroups
112 Adaptation::Ecap::XactionRep::clientIpValue() const
114 const HttpRequest
*request
= dynamic_cast<const HttpRequest
*>(theCauseRep
?
115 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
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
;
126 client_addr
= request
->client_addr
;
127 if (!client_addr
.isAnyAddr() && !client_addr
.isNoAddr()) {
128 char ntoabuf
[MAX_IPSTRLEN
] = "";
129 client_addr
.toStr(ntoabuf
,MAX_IPSTRLEN
);
130 return libecap::Area::FromTempBuffer(ntoabuf
, strlen(ntoabuf
));
133 return libecap::Area();
137 Adaptation::Ecap::XactionRep::usernameValue() const
140 const HttpRequest
*request
= dynamic_cast<const HttpRequest
*>(theCauseRep
?
141 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
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
));
146 else if (request
->extacl_user
.defined() && request
->extacl_user
.size())
147 return libecap::Area::FromTempBuffer(request
->extacl_user
.rawBuf(),
148 request
->extacl_user
.size());
151 return libecap::Area();
155 Adaptation::Ecap::XactionRep::masterxSharedValue(const libecap::Name
&name
) const
157 const HttpRequest
*request
= dynamic_cast<const HttpRequest
*>(theCauseRep
?
158 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
160 if (name
.known()) { // must check to avoid empty names matching unset cfg
161 Adaptation::History::Pointer ah
= request
->adaptHistory(false);
164 if (ah
->getXxRecord(name
, value
))
165 return libecap::Area::FromTempBuffer(value
.rawBuf(), value
.size());
168 return libecap::Area();
172 Adaptation::Ecap::XactionRep::metaValue(const libecap::Name
&name
) const
174 HttpRequest
*request
= dynamic_cast<HttpRequest
*>(theCauseRep
?
175 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
177 HttpReply
*reply
= dynamic_cast<HttpReply
*>(theVirginRep
.raw().header
);
179 if (name
.known()) { // must check to avoid empty names matching unset cfg
180 typedef Notes::iterator ACAMLI
;
181 for (ACAMLI i
= Adaptation::Config::metaHeaders
.begin(); i
!= Adaptation::Config::metaHeaders
.end(); ++i
) {
182 if (name
== (*i
)->key
.termedBuf()) {
183 if (const char *value
= (*i
)->match(request
, reply
))
184 return libecap::Area::FromTempString(value
);
186 return libecap::Area();
191 return libecap::Area();
195 Adaptation::Ecap::XactionRep::visitEachMetaHeader(libecap::NamedValueVisitor
&visitor
) const
197 HttpRequest
*request
= dynamic_cast<HttpRequest
*>(theCauseRep
?
198 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
200 HttpReply
*reply
= dynamic_cast<HttpReply
*>(theVirginRep
.raw().header
);
202 typedef Notes::iterator ACAMLI
;
203 for (ACAMLI i
= Adaptation::Config::metaHeaders
.begin(); i
!= Adaptation::Config::metaHeaders
.end(); ++i
) {
204 const char *v
= (*i
)->match(request
, reply
);
206 const libecap::Name
name((*i
)->key
.termedBuf());
207 const libecap::Area value
= libecap::Area::FromTempString(v
);
208 visitor
.visit(name
, value
);
214 Adaptation::Ecap::XactionRep::start()
218 if (!theVirginRep
.raw().body_pipe
)
219 makingVb
= opNever
; // there is nothing to deliver
221 HttpRequest
*request
= dynamic_cast<HttpRequest
*> (theCauseRep
?
222 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
225 HttpReply
*reply
= dynamic_cast<HttpReply
*>(theVirginRep
.raw().header
);
227 Adaptation::History::Pointer ah
= request
->adaptLogHistory();
229 // retrying=false because ecap never retries transactions
230 adaptHistoryId
= ah
->recordXactStart(service().cfg().key
, current_time
, false);
231 typedef Notes::iterator ACAMLI
;
232 for (ACAMLI i
= Adaptation::Config::metaHeaders
.begin(); i
!= Adaptation::Config::metaHeaders
.end(); ++i
) {
233 const char *v
= (*i
)->match(request
, reply
);
235 if (ah
->metaHeaders
== NULL
)
236 ah
->metaHeaders
= new NotePairs();
237 if (!ah
->metaHeaders
->hasPair((*i
)->key
.termedBuf(), v
))
238 ah
->metaHeaders
->add((*i
)->key
.termedBuf(), v
);
247 Adaptation::Ecap::XactionRep::swanSong()
249 // clear body_pipes, if any
250 // this code does not maintain proxying* and canAccessVb states; should it?
252 if (theAnswerRep
!= NULL
) {
253 BodyPipe::Pointer body_pipe
= answer().body_pipe
;
254 if (body_pipe
!= NULL
) {
255 Must(body_pipe
->stillProducing(this));
256 stopProducingFor(body_pipe
, false);
260 BodyPipe::Pointer
&body_pipe
= theVirginRep
.raw().body_pipe
;
261 if (body_pipe
!= NULL
&& body_pipe
->stillConsuming(this))
262 stopConsumingFrom(body_pipe
);
266 const HttpRequest
*request
= dynamic_cast<const HttpRequest
*>(theCauseRep
?
267 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
269 Adaptation::History::Pointer ah
= request
->adaptLogHistory();
270 if (ah
!= NULL
&& adaptHistoryId
>= 0)
271 ah
->recordXactFinish(adaptHistoryId
);
273 Adaptation::Initiate::swanSong();
277 Adaptation::Ecap::XactionRep::virgin()
282 const libecap::Message
&
283 Adaptation::Ecap::XactionRep::cause()
285 Must(theCauseRep
!= NULL
);
290 Adaptation::Ecap::XactionRep::adapted()
292 Must(theAnswerRep
!= NULL
);
293 return *theAnswerRep
;
296 Adaptation::Message
&
297 Adaptation::Ecap::XactionRep::answer()
299 MessageRep
*rep
= dynamic_cast<MessageRep
*>(theAnswerRep
.get());
305 Adaptation::Ecap::XactionRep::terminateMaster()
308 AdapterXaction x
= theMaster
;
315 Adaptation::Ecap::XactionRep::doneAll() const
317 return makingVb
>= opComplete
&& proxyingAb
>= opComplete
&&
318 Adaptation::Initiate::doneAll();
321 // stops receiving virgin and enables auto-consumption, dropping any vb bytes
323 Adaptation::Ecap::XactionRep::sinkVb(const char *reason
)
325 debugs(93,4, HERE
<< "sink for " << reason
<< "; status:" << status());
327 // we reset raw().body_pipe when we are done, so use this one for checking
328 const BodyPipePointer
&permPipe
= theVirginRep
.raw().header
->body_pipe
;
329 if (permPipe
!= NULL
)
330 permPipe
->enableAutoConsumption();
335 // stops receiving virgin but preserves it for others to use
337 Adaptation::Ecap::XactionRep::preserveVb(const char *reason
)
339 debugs(93,4, HERE
<< "preserve for " << reason
<< "; status:" << status());
341 // we reset raw().body_pipe when we are done, so use this one for checking
342 const BodyPipePointer
&permPipe
= theVirginRep
.raw().header
->body_pipe
;
343 if (permPipe
!= NULL
) {
344 // if libecap consumed, we cannot preserve
345 Must(!permPipe
->consumedSize());
351 // disassociates us from vb; the last step of sinking or preserving vb
353 Adaptation::Ecap::XactionRep::forgetVb(const char *reason
)
355 debugs(93,9, HERE
<< "forget vb " << reason
<< "; status:" << status());
357 BodyPipePointer
&p
= theVirginRep
.raw().body_pipe
;
358 if (p
!= NULL
&& p
->stillConsuming(this))
359 stopConsumingFrom(p
);
361 if (makingVb
== opUndecided
)
363 else if (makingVb
== opOn
)
364 makingVb
= opComplete
;
368 Adaptation::Ecap::XactionRep::useVirgin()
370 debugs(93,3, HERE
<< status());
371 Must(proxyingAb
== opUndecided
);
372 proxyingAb
= opNever
;
374 preserveVb("useVirgin");
376 HttpMsg
*clone
= theVirginRep
.raw().header
->clone();
377 // check that clone() copies the pipe so that we do not have to
378 Must(!theVirginRep
.raw().header
->body_pipe
== !clone
->body_pipe
);
380 updateHistory(clone
);
381 sendAnswer(Answer::Forward(clone
));
386 Adaptation::Ecap::XactionRep::useAdapted(const libecap::shared_ptr
<libecap::Message
> &m
)
388 debugs(93,3, HERE
<< status());
391 Must(proxyingAb
== opUndecided
);
393 HttpMsg
*msg
= answer().header
;
394 if (!theAnswerRep
->body()) { // final, bodyless answer
395 proxyingAb
= opNever
;
397 sendAnswer(Answer::Forward(msg
));
398 } else { // got answer headers but need to handle body
400 Must(!msg
->body_pipe
); // only host can set body pipes
401 MessageRep
*rep
= dynamic_cast<MessageRep
*>(theAnswerRep
.get());
403 rep
->tieBody(this); // sets us as a producer
404 Must(msg
->body_pipe
!= NULL
); // check tieBody
407 sendAnswer(Answer::Forward(msg
));
409 debugs(93,4, HERE
<< "adapter will produce body" << status());
410 theMaster
->abMake(); // libecap will produce
415 Adaptation::Ecap::XactionRep::blockVirgin()
417 debugs(93,3, HERE
<< status());
418 Must(proxyingAb
== opUndecided
);
419 proxyingAb
= opNever
;
421 sinkVb("blockVirgin");
424 sendAnswer(Answer::Block(service().cfg().key
));
428 /// Called just before sendAnswer() to record adapter meta-information
429 /// which may affect answer processing and may be needed for logging.
431 Adaptation::Ecap::XactionRep::updateHistory(HttpMsg
*adapted
)
433 if (!theMaster
) // all updates rely on being able to query the adapter
436 const HttpRequest
*request
= dynamic_cast<const HttpRequest
*>(theCauseRep
?
437 theCauseRep
->raw().header
: theVirginRep
.raw().header
);
440 // TODO: move common ICAP/eCAP logic to Adaptation::Xaction or similar
441 // TODO: optimize Area-to-String conversion
443 // update the cross-transactional database if needed
444 if (const char *xxNameStr
= Adaptation::Config::masterx_shared_name
) {
445 Adaptation::History::Pointer ah
= request
->adaptHistory(true);
447 libecap::Name
xxName(xxNameStr
); // TODO: optimize?
448 if (const libecap::Area val
= theMaster
->option(xxName
))
449 ah
->updateXxRecord(xxNameStr
, val
.toString().c_str());
453 // update the adaptation plan if needed
454 if (service().cfg().routing
) {
456 if (const libecap::Area services
= theMaster
->option(libecap::metaNextServices
)) {
457 Adaptation::History::Pointer ah
= request
->adaptHistory(true);
459 ah
->updateNextServices(services
.toString().c_str());
461 } // TODO: else warn (occasionally!) if we got libecap::metaNextServices
463 // Store received meta headers for adapt::<last_h logformat code use.
464 // If we already have stored headers from a previous adaptation transaction
465 // related to the same master transction, they will be replaced.
466 Adaptation::History::Pointer ah
= request
->adaptLogHistory();
468 HttpHeader
meta(hoReply
);
469 OptionsExtractor
extractor(meta
);
470 theMaster
->visitEachOption(extractor
);
471 ah
->recordMeta(&meta
);
474 // Add just-created history to the adapted/cloned request that lacks it.
475 if (HttpRequest
*adaptedReq
= dynamic_cast<HttpRequest
*>(adapted
))
476 adaptedReq
->adaptHistoryImport(*request
);
480 Adaptation::Ecap::XactionRep::vbDiscard()
482 Must(makingVb
== opUndecided
);
483 // if adapter does not need vb, we do not need to send it
485 Must(makingVb
== opNever
);
489 Adaptation::Ecap::XactionRep::vbMake()
491 Must(makingVb
== opUndecided
);
492 BodyPipePointer
&p
= theVirginRep
.raw().body_pipe
;
494 Must(p
->setConsumerIfNotLate(this)); // to deliver vb, we must receive vb
499 Adaptation::Ecap::XactionRep::vbStopMaking()
501 Must(makingVb
== opOn
);
502 // if adapter does not need vb, we do not need to receive it
503 sinkVb("vbStopMaking");
504 Must(makingVb
== opComplete
);
508 Adaptation::Ecap::XactionRep::vbMakeMore()
510 Must(makingVb
== opOn
); // cannot make more if done proxying
511 // we cannot guarantee more vb, but we can check that there is a chance
512 const BodyPipePointer
&p
= theVirginRep
.raw().body_pipe
;
513 Must(p
!= NULL
&& p
->stillConsuming(this)); // we are plugged in
514 Must(!p
->productionEnded() && p
->mayNeedMoreData()); // and may get more
518 Adaptation::Ecap::XactionRep::vbContent(libecap::size_type o
, libecap::size_type s
)
520 // We may not be makingVb yet. It should be OK, but see vbContentShift().
522 const BodyPipePointer
&p
= theVirginRep
.raw().body_pipe
;
525 // TODO: make MemBuf use size_t?
526 const size_t haveSize
= static_cast<size_t>(p
->buf().contentSize());
528 // convert to Squid types; XXX: check for overflow
529 const uint64_t offset
= static_cast<uint64_t>(o
);
530 Must(offset
<= haveSize
); // equal iff at the end of content
532 // nsize means no size limit: all content starting from offset
533 const size_t size
= s
== libecap::nsize
?
534 haveSize
- offset
: static_cast<size_t>(s
);
536 // XXX: optimize by making theBody a shared_ptr (see Area::FromTemp*() src)
537 return libecap::Area::FromTempBuffer(p
->buf().content() + offset
,
538 min(static_cast<size_t>(haveSize
- offset
), size
));
542 Adaptation::Ecap::XactionRep::vbContentShift(libecap::size_type n
)
544 // We may not be makingVb yet. It should be OK now, but if BodyPipe
545 // consume() requirements change, we would have to return empty vbContent
546 // until the adapter registers as a consumer
548 BodyPipePointer
&p
= theVirginRep
.raw().body_pipe
;
550 const size_t size
= static_cast<size_t>(n
); // XXX: check for overflow
551 const size_t haveSize
= static_cast<size_t>(p
->buf().contentSize()); // TODO: make MemBuf use size_t?
552 p
->consume(min(size
, haveSize
));
556 Adaptation::Ecap::XactionRep::noteAbContentDone(bool atEnd
)
558 Must(proxyingAb
== opOn
&& !abProductionFinished
);
559 abProductionFinished
= true;
560 abProductionAtEnd
= atEnd
; // store until ready to stop producing ourselves
561 debugs(93,5, HERE
<< "adapted body production ended");
566 Adaptation::Ecap::XactionRep::noteAbContentAvailable()
568 Must(proxyingAb
== opOn
&& !abProductionFinished
);
572 #if 0 /* XXX: implement */
574 Adaptation::Ecap::XactionRep::setAdaptedBodySize(const libecap::BodySize
&size
)
576 Must(answer().body_pipe
!= NULL
);
578 answer().body_pipe
->setBodySize(size
.value());
579 // else the piped body size is unknown by default
584 Adaptation::Ecap::XactionRep::adaptationDelayed(const libecap::Delay
&d
)
586 debugs(93,3, HERE
<< "adapter needs time: " <<
587 d
.state
<< '/' << d
.progress
);
592 Adaptation::Ecap::XactionRep::adaptationAborted()
594 tellQueryAborted(true); // should eCAP support retries?
595 mustStop("adaptationAborted");
599 Adaptation::Ecap::XactionRep::callable() const
605 Adaptation::Ecap::XactionRep::noteMoreBodySpaceAvailable(RefCount
<BodyPipe
> bp
)
607 Must(proxyingAb
== opOn
);
612 Adaptation::Ecap::XactionRep::noteBodyConsumerAborted(RefCount
<BodyPipe
> bp
)
614 Must(proxyingAb
== opOn
);
615 stopProducingFor(answer().body_pipe
, false);
617 theMaster
->abStopMaking();
618 proxyingAb
= opComplete
;
622 Adaptation::Ecap::XactionRep::noteMoreBodyDataAvailable(RefCount
<BodyPipe
> bp
)
624 Must(makingVb
== opOn
); // or we would not be registered as a consumer
626 theMaster
->noteVbContentAvailable();
630 Adaptation::Ecap::XactionRep::noteBodyProductionEnded(RefCount
<BodyPipe
> bp
)
632 Must(makingVb
== opOn
); // or we would not be registered as a consumer
634 theMaster
->noteVbContentDone(true);
635 vbProductionFinished
= true;
639 Adaptation::Ecap::XactionRep::noteBodyProducerAborted(RefCount
<BodyPipe
> bp
)
641 Must(makingVb
== opOn
); // or we would not be registered as a consumer
643 theMaster
->noteVbContentDone(false);
644 vbProductionFinished
= true;
648 Adaptation::Ecap::XactionRep::noteInitiatorAborted()
650 mustStop("initiator aborted");
653 // get content from the adapter and put it into the adapted pipe
655 Adaptation::Ecap::XactionRep::moveAbContent()
657 Must(proxyingAb
== opOn
);
658 const libecap::Area c
= theMaster
->abContent(0, libecap::nsize
);
659 debugs(93,5, HERE
<< "up to " << c
.size
<< " bytes");
660 if (c
.size
== 0 && abProductionFinished
) { // no ab now and in the future
661 stopProducingFor(answer().body_pipe
, abProductionAtEnd
);
662 proxyingAb
= opComplete
;
663 debugs(93,5, HERE
<< "last adapted body data retrieved");
664 } else if (c
.size
> 0) {
665 if (const size_t used
= answer().body_pipe
->putMoreData(c
.start
, c
.size
))
666 theMaster
->abContentShift(used
);
671 Adaptation::Ecap::XactionRep::status() const
679 buf
.Printf("M%d", static_cast<int>(makingVb
));
681 const BodyPipePointer
&vp
= theVirginRep
.raw().body_pipe
;
683 buf
.append(" !V", 3);
684 else if (vp
->stillConsuming(const_cast<XactionRep
*>(this)))
685 buf
.append(" Vc", 3);
687 buf
.append(" V?", 3);
689 if (vbProductionFinished
)
692 buf
.Printf(" A%d", static_cast<int>(proxyingAb
));
694 if (proxyingAb
== opOn
) {
695 MessageRep
*rep
= dynamic_cast<MessageRep
*>(theAnswerRep
.get());
697 const BodyPipePointer
&ap
= rep
->raw().body_pipe
;
699 buf
.append(" !A", 3);
700 else if (ap
->stillProducing(const_cast<XactionRep
*>(this)))
701 buf
.append(" Ap", 3);
703 buf
.append(" A?", 3);
706 buf
.Printf(" %s%u]", id
.Prefix
, id
.value
);
710 return buf
.content();