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