]>
Commit | Line | Data |
---|---|---|
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> |
1adcebc3 | 10 | #include "adaptation/Answer.h" |
22fff3bf | 11 | #include "adaptation/ecap/Config.h" |
602d9612 | 12 | #include "adaptation/ecap/XactionRep.h" |
3af10ac0 | 13 | #include "adaptation/Initiator.h" |
3d93a84d | 14 | #include "base/TextException.h" |
602d9612 A |
15 | #include "HttpReply.h" |
16 | #include "HttpRequest.h" | |
17 | #include "SquidTime.h" | |
fdc96a39 | 18 | |
574b508c | 19 | CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Ecap::XactionRep, XactionRep); |
fdc96a39 | 20 | |
5038f9d8 AR |
21 | /// a libecap Visitor for converting adapter transaction options to HttpHeader |
22 | class OptionsExtractor: public libecap::NamedValueVisitor | |
23 | { | |
24 | public: | |
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 | 38 | Adaptation::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 | 54 | Adaptation::Ecap::XactionRep::~XactionRep() |
fdc96a39 AR |
55 | { |
56 | assert(!theMaster); | |
027320b4 | 57 | delete theCauseRep; |
4d0854d4 | 58 | theAnswerRep.reset(); |
fdc96a39 AR |
59 | } |
60 | ||
61 | void | |
574b508c | 62 | Adaptation::Ecap::XactionRep::master(const AdapterXaction &x) |
fdc96a39 AR |
63 | { |
64 | Must(!theMaster); | |
65 | Must(x != NULL); | |
66 | theMaster = x; | |
67 | } | |
68 | ||
a22e6cd3 AR |
69 | Adaptation::Service & |
70 | Adaptation::Ecap::XactionRep::service() | |
71 | { | |
72 | Must(theService != NULL); | |
73 | return *theService; | |
74 | } | |
75 | ||
22fff3bf AR |
76 | const libecap::Area |
77 | Adaptation::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 | ||
92 | void | |
93 | Adaptation::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 | ||
111 | const libecap::Area | |
112 | Adaptation::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; | |
4dd643d5 | 127 | if (!client_addr.isAnyAddr() && !client_addr.isNoAddr()) { |
22fff3bf | 128 | char ntoabuf[MAX_IPSTRLEN] = ""; |
4dd643d5 | 129 | client_addr.toStr(ntoabuf,MAX_IPSTRLEN); |
22fff3bf AR |
130 | return libecap::Area::FromTempBuffer(ntoabuf, strlen(ntoabuf)); |
131 | } | |
ec4d1a1d | 132 | } |
22fff3bf AR |
133 | return libecap::Area(); |
134 | } | |
135 | ||
136 | const libecap::Area | |
137 | Adaptation::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)); | |
b38b26cb | 146 | else if (request->extacl_user.size() > 0) |
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 |
154 | const libecap::Area |
155 | Adaptation::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 |
171 | const libecap::Area |
172 | Adaptation::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 | |
d7f4a0b7 | 180 | typedef Notes::iterator ACAMLI; |
71ee0835 | 181 | for (ACAMLI i = Adaptation::Config::metaHeaders.begin(); i != Adaptation::Config::metaHeaders.end(); ++i) { |
d7f4a0b7 | 182 | if (name == (*i)->key.termedBuf()) { |
71be37e0 CT |
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 | 194 | void |
71be37e0 CT |
195 | Adaptation::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 | |
d7f4a0b7 | 202 | typedef Notes::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) { | |
d7f4a0b7 | 206 | const libecap::Name name((*i)->key.termedBuf()); |
71be37e0 CT |
207 | const libecap::Area value = libecap::Area::FromTempString(v); |
208 | visitor.visit(name, value); | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
fdc96a39 | 213 | void |
574b508c | 214 | Adaptation::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 | |
d7f4a0b7 | 221 | HttpRequest *request = dynamic_cast<HttpRequest*> (theCauseRep ? |
ffb82151 | 222 | theCauseRep->raw().header : theVirginRep.raw().header); |
3ff65596 | 223 | Must(request); |
d7f4a0b7 CT |
224 | |
225 | HttpReply *reply = dynamic_cast<HttpReply*>(theVirginRep.raw().header); | |
226 | ||
a22e6cd3 | 227 | Adaptation::History::Pointer ah = request->adaptLogHistory(); |
e1381638 | 228 | if (ah != NULL) { |
3ff65596 AR |
229 | // retrying=false because ecap never retries transactions |
230 | adaptHistoryId = ah->recordXactStart(service().cfg().key, current_time, false); | |
d7f4a0b7 CT |
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); | |
cf9f0261 CT |
234 | if (v) { |
235 | if (ah->metaHeaders == NULL) | |
236 | ah->metaHeaders = new NotePairs(); | |
7e6ef752 | 237 | if (!ah->metaHeaders->hasPair((*i)->key.termedBuf(), v)) |
cf9f0261 | 238 | ah->metaHeaders->add((*i)->key.termedBuf(), v); |
d7f4a0b7 CT |
239 | } |
240 | } | |
3ff65596 AR |
241 | } |
242 | ||
fdc96a39 AR |
243 | theMaster->start(); |
244 | } | |
245 | ||
246 | void | |
574b508c | 247 | Adaptation::Ecap::XactionRep::swanSong() |
fdc96a39 | 248 | { |
506a0530 | 249 | // clear body_pipes, if any |
ea76d91e | 250 | // this code does not maintain proxying* and canAccessVb states; should it? |
506a0530 AR |
251 | |
252 | if (theAnswerRep != NULL) { | |
f1a768b2 AR |
253 | BodyPipe::Pointer body_pipe = answer().body_pipe; |
254 | if (body_pipe != NULL) { | |
255 | Must(body_pipe->stillProducing(this)); | |
256 | stopProducingFor(body_pipe, false); | |
257 | } | |
258 | } | |
506a0530 | 259 | |
e1e90d26 AR |
260 | BodyPipe::Pointer &body_pipe = theVirginRep.raw().body_pipe; |
261 | if (body_pipe != NULL && body_pipe->stillConsuming(this)) | |
262 | stopConsumingFrom(body_pipe); | |
506a0530 | 263 | |
fdc96a39 | 264 | terminateMaster(); |
3ff65596 AR |
265 | |
266 | const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ? | |
e1381638 | 267 | theCauseRep->raw().header : theVirginRep.raw().header); |
3ff65596 | 268 | Must(request); |
a22e6cd3 | 269 | Adaptation::History::Pointer ah = request->adaptLogHistory(); |
3ff65596 AR |
270 | if (ah != NULL && adaptHistoryId >= 0) |
271 | ah->recordXactFinish(adaptHistoryId); | |
272 | ||
fdc96a39 AR |
273 | Adaptation::Initiate::swanSong(); |
274 | } | |
275 | ||
fdc96a39 | 276 | libecap::Message & |
574b508c | 277 | Adaptation::Ecap::XactionRep::virgin() |
fdc96a39 | 278 | { |
7b67e5b6 | 279 | return theVirginRep; |
fdc96a39 AR |
280 | } |
281 | ||
4d0854d4 | 282 | const libecap::Message & |
574b508c | 283 | Adaptation::Ecap::XactionRep::cause() |
fdc96a39 | 284 | { |
4d0854d4 AR |
285 | Must(theCauseRep != NULL); |
286 | return *theCauseRep; | |
fdc96a39 AR |
287 | } |
288 | ||
4d0854d4 | 289 | libecap::Message & |
574b508c | 290 | Adaptation::Ecap::XactionRep::adapted() |
fdc96a39 | 291 | { |
4d0854d4 AR |
292 | Must(theAnswerRep != NULL); |
293 | return *theAnswerRep; | |
294 | } | |
295 | ||
296 | Adaptation::Message & | |
574b508c | 297 | Adaptation::Ecap::XactionRep::answer() |
4d0854d4 | 298 | { |
f1a768b2 AR |
299 | MessageRep *rep = dynamic_cast<MessageRep*>(theAnswerRep.get()); |
300 | Must(rep); | |
4d0854d4 AR |
301 | return rep->raw(); |
302 | } | |
303 | ||
ea76d91e | 304 | void |
574b508c | 305 | Adaptation::Ecap::XactionRep::terminateMaster() |
4d0854d4 AR |
306 | { |
307 | if (theMaster) { | |
ea76d91e AR |
308 | AdapterXaction x = theMaster; |
309 | theMaster.reset(); | |
310 | x->stop(); | |
f1a768b2 | 311 | } |
4d0854d4 AR |
312 | } |
313 | ||
4d0854d4 | 314 | bool |
574b508c | 315 | Adaptation::Ecap::XactionRep::doneAll() const |
4d0854d4 | 316 | { |
e1e90d26 | 317 | return makingVb >= opComplete && proxyingAb >= opComplete && |
26ac0430 | 318 | Adaptation::Initiate::doneAll(); |
4d0854d4 AR |
319 | } |
320 | ||
e1e90d26 | 321 | // stops receiving virgin and enables auto-consumption, dropping any vb bytes |
4d0854d4 | 322 | void |
e1e90d26 | 323 | Adaptation::Ecap::XactionRep::sinkVb(const char *reason) |
4d0854d4 | 324 | { |
e1e90d26 | 325 | debugs(93,4, HERE << "sink for " << reason << "; status:" << status()); |
4d0854d4 | 326 | |
e1e90d26 AR |
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(); | |
3af10ac0 | 331 | |
e1e90d26 AR |
332 | forgetVb(reason); |
333 | } | |
334 | ||
335 | // stops receiving virgin but preserves it for others to use | |
336 | void | |
337 | Adaptation::Ecap::XactionRep::preserveVb(const char *reason) | |
338 | { | |
339 | debugs(93,4, HERE << "preserve for " << reason << "; status:" << status()); | |
340 | ||
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()); | |
3af10ac0 AR |
346 | } |
347 | ||
e1e90d26 AR |
348 | forgetVb(reason); |
349 | } | |
350 | ||
351 | // disassociates us from vb; the last step of sinking or preserving vb | |
352 | void | |
353 | Adaptation::Ecap::XactionRep::forgetVb(const char *reason) | |
354 | { | |
355 | debugs(93,9, HERE << "forget vb " << reason << "; status:" << status()); | |
ea76d91e | 356 | |
e1e90d26 AR |
357 | BodyPipePointer &p = theVirginRep.raw().body_pipe; |
358 | if (p != NULL && p->stillConsuming(this)) | |
359 | stopConsumingFrom(p); | |
360 | ||
361 | if (makingVb == opUndecided) | |
362 | makingVb = opNever; | |
363 | else if (makingVb == opOn) | |
364 | makingVb = opComplete; | |
fdc96a39 AR |
365 | } |
366 | ||
26ac0430 | 367 | void |
574b508c | 368 | Adaptation::Ecap::XactionRep::useVirgin() |
fdc96a39 | 369 | { |
4d0854d4 | 370 | debugs(93,3, HERE << status()); |
ea76d91e AR |
371 | Must(proxyingAb == opUndecided); |
372 | proxyingAb = opNever; | |
4d0854d4 | 373 | |
e1e90d26 | 374 | preserveVb("useVirgin"); |
2874d9e3 AR |
375 | |
376 | HttpMsg *clone = theVirginRep.raw().header->clone(); | |
377 | // check that clone() copies the pipe so that we do not have to | |
e1e90d26 | 378 | Must(!theVirginRep.raw().header->body_pipe == !clone->body_pipe); |
ea76d91e | 379 | |
aaf0559d | 380 | updateHistory(clone); |
3af10ac0 | 381 | sendAnswer(Answer::Forward(clone)); |
4d0854d4 | 382 | Must(done()); |
fdc96a39 AR |
383 | } |
384 | ||
26ac0430 | 385 | void |
574b508c | 386 | Adaptation::Ecap::XactionRep::useAdapted(const libecap::shared_ptr<libecap::Message> &m) |
fdc96a39 | 387 | { |
4d0854d4 | 388 | debugs(93,3, HERE << status()); |
ea76d91e | 389 | Must(m); |
4d0854d4 | 390 | theAnswerRep = m; |
ea76d91e AR |
391 | Must(proxyingAb == opUndecided); |
392 | ||
f1a768b2 | 393 | HttpMsg *msg = answer().header; |
ea76d91e AR |
394 | if (!theAnswerRep->body()) { // final, bodyless answer |
395 | proxyingAb = opNever; | |
aaf0559d | 396 | updateHistory(msg); |
3af10ac0 | 397 | sendAnswer(Answer::Forward(msg)); |
f1a768b2 | 398 | } else { // got answer headers but need to handle body |
ea76d91e | 399 | proxyingAb = opOn; |
f1a768b2 | 400 | Must(!msg->body_pipe); // only host can set body pipes |
ea76d91e | 401 | MessageRep *rep = dynamic_cast<MessageRep*>(theAnswerRep.get()); |
f1a768b2 AR |
402 | Must(rep); |
403 | rep->tieBody(this); // sets us as a producer | |
404 | Must(msg->body_pipe != NULL); // check tieBody | |
ea76d91e | 405 | |
aaf0559d | 406 | updateHistory(msg); |
3af10ac0 | 407 | sendAnswer(Answer::Forward(msg)); |
ea76d91e | 408 | |
4d0854d4 | 409 | debugs(93,4, HERE << "adapter will produce body" << status()); |
8679e6c2 | 410 | theMaster->abMake(); // libecap will produce |
4d0854d4 | 411 | } |
fdc96a39 AR |
412 | } |
413 | ||
3af10ac0 AR |
414 | void |
415 | Adaptation::Ecap::XactionRep::blockVirgin() | |
416 | { | |
417 | debugs(93,3, HERE << status()); | |
418 | Must(proxyingAb == opUndecided); | |
419 | proxyingAb = opNever; | |
420 | ||
e1e90d26 | 421 | sinkVb("blockVirgin"); |
3af10ac0 | 422 | |
aaf0559d | 423 | updateHistory(NULL); |
3af10ac0 AR |
424 | sendAnswer(Answer::Block(service().cfg().key)); |
425 | Must(done()); | |
426 | } | |
427 | ||
5038f9d8 AR |
428 | /// Called just before sendAnswer() to record adapter meta-information |
429 | /// which may affect answer processing and may be needed for logging. | |
430 | void | |
aaf0559d | 431 | Adaptation::Ecap::XactionRep::updateHistory(HttpMsg *adapted) |
5038f9d8 AR |
432 | { |
433 | if (!theMaster) // all updates rely on being able to query the adapter | |
434 | return; | |
435 | ||
436 | const HttpRequest *request = dynamic_cast<const HttpRequest*>(theCauseRep ? | |
437 | theCauseRep->raw().header : theVirginRep.raw().header); | |
438 | Must(request); | |
439 | ||
440 | // TODO: move common ICAP/eCAP logic to Adaptation::Xaction or similar | |
441 | // TODO: optimize Area-to-String conversion | |
442 | ||
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); | |
446 | if (ah != NULL) { | |
447 | libecap::Name xxName(xxNameStr); // TODO: optimize? | |
448 | if (const libecap::Area val = theMaster->option(xxName)) | |
449 | ah->updateXxRecord(xxNameStr, val.toString().c_str()); | |
450 | } | |
451 | } | |
452 | ||
453 | // update the adaptation plan if needed | |
454 | if (service().cfg().routing) { | |
455 | String services; | |
456 | if (const libecap::Area services = theMaster->option(libecap::metaNextServices)) { | |
457 | Adaptation::History::Pointer ah = request->adaptHistory(true); | |
458 | if (ah != NULL) | |
459 | ah->updateNextServices(services.toString().c_str()); | |
460 | } | |
461 | } // TODO: else warn (occasionally!) if we got libecap::metaNextServices | |
462 | ||
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(); | |
467 | if (ah != NULL) { | |
468 | HttpHeader meta(hoReply); | |
469 | OptionsExtractor extractor(meta); | |
470 | theMaster->visitEachOption(extractor); | |
471 | ah->recordMeta(&meta); | |
472 | } | |
aaf0559d AR |
473 | |
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); | |
5038f9d8 AR |
477 | } |
478 | ||
4d0854d4 | 479 | void |
574b508c | 480 | Adaptation::Ecap::XactionRep::vbDiscard() |
fdc96a39 | 481 | { |
e1e90d26 | 482 | Must(makingVb == opUndecided); |
8679e6c2 | 483 | // if adapter does not need vb, we do not need to send it |
e1e90d26 AR |
484 | sinkVb("vbDiscard"); |
485 | Must(makingVb == opNever); | |
fdc96a39 AR |
486 | } |
487 | ||
4d0854d4 | 488 | void |
574b508c | 489 | Adaptation::Ecap::XactionRep::vbMake() |
fdc96a39 | 490 | { |
e1e90d26 | 491 | Must(makingVb == opUndecided); |
ea76d91e AR |
492 | BodyPipePointer &p = theVirginRep.raw().body_pipe; |
493 | Must(p != NULL); | |
e1e90d26 AR |
494 | Must(p->setConsumerIfNotLate(this)); // to deliver vb, we must receive vb |
495 | makingVb = opOn; | |
fdc96a39 AR |
496 | } |
497 | ||
4d0854d4 | 498 | void |
574b508c | 499 | Adaptation::Ecap::XactionRep::vbStopMaking() |
fdc96a39 | 500 | { |
e1e90d26 | 501 | Must(makingVb == opOn); |
ea76d91e | 502 | // if adapter does not need vb, we do not need to receive it |
e1e90d26 AR |
503 | sinkVb("vbStopMaking"); |
504 | Must(makingVb == opComplete); | |
4d0854d4 AR |
505 | } |
506 | ||
507 | void | |
574b508c | 508 | Adaptation::Ecap::XactionRep::vbMakeMore() |
4d0854d4 | 509 | { |
e1e90d26 | 510 | Must(makingVb == opOn); // cannot make more if done proxying |
ea76d91e | 511 | // we cannot guarantee more vb, but we can check that there is a chance |
e1e90d26 AR |
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 | |
4d0854d4 AR |
515 | } |
516 | ||
8679e6c2 | 517 | libecap::Area |
574b508c | 518 | Adaptation::Ecap::XactionRep::vbContent(libecap::size_type o, libecap::size_type s) |
4d0854d4 | 519 | { |
e1e90d26 | 520 | // We may not be makingVb yet. It should be OK, but see vbContentShift(). |
ea76d91e | 521 | |
8679e6c2 | 522 | const BodyPipePointer &p = theVirginRep.raw().body_pipe; |
ea76d91e AR |
523 | Must(p != NULL); |
524 | ||
525 | // TODO: make MemBuf use size_t? | |
526 | const size_t haveSize = static_cast<size_t>(p->buf().contentSize()); | |
4d0854d4 | 527 | |
8679e6c2 AR |
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 | |
531 | ||
532 | // nsize means no size limit: all content starting from offset | |
533 | const size_t size = s == libecap::nsize ? | |
26ac0430 | 534 | haveSize - offset : static_cast<size_t>(s); |
8679e6c2 | 535 | |
8679e6c2 AR |
536 | // XXX: optimize by making theBody a shared_ptr (see Area::FromTemp*() src) |
537 | return libecap::Area::FromTempBuffer(p->buf().content() + offset, | |
26ac0430 | 538 | min(static_cast<size_t>(haveSize - offset), size)); |
4d0854d4 AR |
539 | } |
540 | ||
541 | void | |
574b508c | 542 | Adaptation::Ecap::XactionRep::vbContentShift(libecap::size_type n) |
4d0854d4 | 543 | { |
e1e90d26 | 544 | // We may not be makingVb yet. It should be OK now, but if BodyPipe |
ea76d91e AR |
545 | // consume() requirements change, we would have to return empty vbContent |
546 | // until the adapter registers as a consumer | |
547 | ||
8679e6c2 AR |
548 | BodyPipePointer &p = theVirginRep.raw().body_pipe; |
549 | Must(p != NULL); | |
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)); | |
4d0854d4 AR |
553 | } |
554 | ||
555 | void | |
574b508c | 556 | Adaptation::Ecap::XactionRep::noteAbContentDone(bool atEnd) |
4d0854d4 | 557 | { |
7477a343 AR |
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"); | |
562 | moveAbContent(); | |
4d0854d4 AR |
563 | } |
564 | ||
8679e6c2 | 565 | void |
574b508c | 566 | Adaptation::Ecap::XactionRep::noteAbContentAvailable() |
4d0854d4 | 567 | { |
7477a343 | 568 | Must(proxyingAb == opOn && !abProductionFinished); |
8679e6c2 | 569 | moveAbContent(); |
4d0854d4 AR |
570 | } |
571 | ||
ea76d91e | 572 | #if 0 /* XXX: implement */ |
4d0854d4 | 573 | void |
574b508c | 574 | Adaptation::Ecap::XactionRep::setAdaptedBodySize(const libecap::BodySize &size) |
4d0854d4 AR |
575 | { |
576 | Must(answer().body_pipe != NULL); | |
8679e6c2 AR |
577 | if (size.known()) |
578 | answer().body_pipe->setBodySize(size.value()); | |
579 | // else the piped body size is unknown by default | |
4d0854d4 | 580 | } |
8679e6c2 | 581 | #endif |
4d0854d4 AR |
582 | |
583 | void | |
574b508c | 584 | Adaptation::Ecap::XactionRep::adaptationDelayed(const libecap::Delay &d) |
4d0854d4 AR |
585 | { |
586 | debugs(93,3, HERE << "adapter needs time: " << | |
26ac0430 | 587 | d.state << '/' << d.progress); |
4d0854d4 | 588 | // XXX: set timeout? |
fdc96a39 AR |
589 | } |
590 | ||
26ac0430 | 591 | void |
574b508c | 592 | Adaptation::Ecap::XactionRep::adaptationAborted() |
fdc96a39 | 593 | { |
fdc96a39 | 594 | tellQueryAborted(true); // should eCAP support retries? |
ea76d91e | 595 | mustStop("adaptationAborted"); |
fdc96a39 AR |
596 | } |
597 | ||
8679e6c2 | 598 | bool |
574b508c | 599 | Adaptation::Ecap::XactionRep::callable() const |
8679e6c2 AR |
600 | { |
601 | return !done(); | |
602 | } | |
603 | ||
26ac0430 | 604 | void |
574b508c | 605 | Adaptation::Ecap::XactionRep::noteMoreBodySpaceAvailable(RefCount<BodyPipe> bp) |
fdc96a39 | 606 | { |
ea76d91e AR |
607 | Must(proxyingAb == opOn); |
608 | moveAbContent(); | |
fdc96a39 AR |
609 | } |
610 | ||
26ac0430 | 611 | void |
574b508c | 612 | Adaptation::Ecap::XactionRep::noteBodyConsumerAborted(RefCount<BodyPipe> bp) |
fdc96a39 | 613 | { |
ea76d91e AR |
614 | Must(proxyingAb == opOn); |
615 | stopProducingFor(answer().body_pipe, false); | |
616 | Must(theMaster); | |
617 | theMaster->abStopMaking(); | |
618 | proxyingAb = opComplete; | |
fdc96a39 AR |
619 | } |
620 | ||
621 | void | |
574b508c | 622 | Adaptation::Ecap::XactionRep::noteMoreBodyDataAvailable(RefCount<BodyPipe> bp) |
fdc96a39 | 623 | { |
e1e90d26 | 624 | Must(makingVb == opOn); // or we would not be registered as a consumer |
fdc96a39 | 625 | Must(theMaster); |
8679e6c2 | 626 | theMaster->noteVbContentAvailable(); |
fdc96a39 AR |
627 | } |
628 | ||
629 | void | |
574b508c | 630 | Adaptation::Ecap::XactionRep::noteBodyProductionEnded(RefCount<BodyPipe> bp) |
fdc96a39 | 631 | { |
e1e90d26 | 632 | Must(makingVb == opOn); // or we would not be registered as a consumer |
fdc96a39 | 633 | Must(theMaster); |
8679e6c2 | 634 | theMaster->noteVbContentDone(true); |
e1e90d26 | 635 | vbProductionFinished = true; |
fdc96a39 AR |
636 | } |
637 | ||
638 | void | |
574b508c | 639 | Adaptation::Ecap::XactionRep::noteBodyProducerAborted(RefCount<BodyPipe> bp) |
fdc96a39 | 640 | { |
e1e90d26 | 641 | Must(makingVb == opOn); // or we would not be registered as a consumer |
8679e6c2 AR |
642 | Must(theMaster); |
643 | theMaster->noteVbContentDone(false); | |
e1e90d26 | 644 | vbProductionFinished = true; |
fdc96a39 AR |
645 | } |
646 | ||
647 | void | |
574b508c | 648 | Adaptation::Ecap::XactionRep::noteInitiatorAborted() |
fdc96a39 AR |
649 | { |
650 | mustStop("initiator aborted"); | |
651 | } | |
652 | ||
8679e6c2 AR |
653 | // get content from the adapter and put it into the adapted pipe |
654 | void | |
574b508c | 655 | Adaptation::Ecap::XactionRep::moveAbContent() |
8679e6c2 | 656 | { |
ea76d91e | 657 | Must(proxyingAb == opOn); |
8679e6c2 | 658 | const libecap::Area c = theMaster->abContent(0, libecap::nsize); |
7477a343 AR |
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"); | |
e1381638 | 664 | } else if (c.size > 0) { |
7477a343 AR |
665 | if (const size_t used = answer().body_pipe->putMoreData(c.start, c.size)) |
666 | theMaster->abContentShift(used); | |
667 | } | |
8679e6c2 AR |
668 | } |
669 | ||
670 | const char * | |
574b508c | 671 | Adaptation::Ecap::XactionRep::status() const |
fdc96a39 | 672 | { |
4d0854d4 AR |
673 | static MemBuf buf; |
674 | buf.reset(); | |
675 | ||
676 | buf.append(" [", 2); | |
677 | ||
e1e90d26 AR |
678 | if (makingVb) |
679 | buf.Printf("M%d", static_cast<int>(makingVb)); | |
680 | ||
681 | const BodyPipePointer &vp = theVirginRep.raw().body_pipe; | |
682 | if (!vp) | |
683 | buf.append(" !V", 3); | |
ec4d1a1d | 684 | else if (vp->stillConsuming(const_cast<XactionRep*>(this))) |
e1e90d26 AR |
685 | buf.append(" Vc", 3); |
686 | else | |
687 | buf.append(" V?", 3); | |
688 | ||
689 | if (vbProductionFinished) | |
690 | buf.append(".", 1); | |
691 | ||
e1e90d26 | 692 | buf.Printf(" A%d", static_cast<int>(proxyingAb)); |
506a0530 | 693 | |
ea76d91e AR |
694 | if (proxyingAb == opOn) { |
695 | MessageRep *rep = dynamic_cast<MessageRep*>(theAnswerRep.get()); | |
696 | Must(rep); | |
f1a768b2 | 697 | const BodyPipePointer &ap = rep->raw().body_pipe; |
e1e90d26 AR |
698 | if (!ap) |
699 | buf.append(" !A", 3); | |
700 | else if (ap->stillProducing(const_cast<XactionRep*>(this))) | |
701 | buf.append(" Ap", 3); | |
702 | else | |
703 | buf.append(" A?", 3); | |
f1a768b2 | 704 | } |
4d0854d4 | 705 | |
52ed047a | 706 | buf.Printf(" %s%u]", id.Prefix, id.value); |
4d0854d4 AR |
707 | |
708 | buf.terminate(); | |
709 | ||
710 | return buf.content(); | |
fdc96a39 | 711 | } |