]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/icap/ServiceRep.cc
cdb8770fe22eb5b2149cc2789ff252afbd26a607
[thirdparty/squid.git] / src / adaptation / icap / ServiceRep.cc
1 /*
2 * DEBUG: section 93 ICAP (RFC 3507) Client
3 */
4
5 #include "squid.h"
6 #include "TextException.h"
7 #include "HttpReply.h"
8 #include "adaptation/icap/ServiceRep.h"
9 #include "adaptation/icap/Options.h"
10 #include "adaptation/icap/OptXact.h"
11 #include "ConfigParser.h"
12 #include "adaptation/icap/Config.h"
13 #include "adaptation/icap/ModXact.h"
14 #include "SquidTime.h"
15
16 CBDATA_NAMESPACED_CLASS_INIT(Adaptation::Icap, ServiceRep);
17
18 Adaptation::Icap::ServiceRep::ServiceRep(const Adaptation::ServiceConfig &cfg):
19 AsyncJob("Adaptation::Icap::ServiceRep"), Adaptation::Service(cfg),
20 theOptions(NULL), theOptionsFetcher(0), theLastUpdate(0),
21 theSessionFailures(0), isSuspended(0), notifying(false),
22 updateScheduled(false), self(NULL),
23 wasAnnouncedUp(true) // do not announce an "up" service at startup
24 {}
25
26 Adaptation::Icap::ServiceRep::~ServiceRep()
27 {
28 Must(!theOptionsFetcher);
29 changeOptions(0);
30 }
31
32 void
33 Adaptation::Icap::ServiceRep::setSelf(Pointer &aSelf)
34 {
35 assert(!self && aSelf != NULL);
36 self = aSelf;
37 }
38
39 void
40 Adaptation::Icap::ServiceRep::finalize()
41 {
42 Adaptation::Service::finalize();
43 assert(self != NULL);
44
45 // use /etc/services or default port if needed
46 const bool have_port = cfg().port >= 0;
47 if (!have_port) {
48 struct servent *serv = getservbyname("icap", "tcp");
49
50 if (serv) {
51 writeableCfg().port = htons(serv->s_port);
52 } else {
53 writeableCfg().port = 1344;
54 }
55 }
56 }
57
58 void Adaptation::Icap::ServiceRep::invalidate()
59 {
60 assert(self != NULL);
61 Pointer savedSelf = self; // to prevent destruction when we nullify self
62 self = NULL;
63
64 announceStatusChange("invalidated by reconfigure", false);
65
66 savedSelf = NULL; // may destroy us and, hence, invalidate cbdata(this)
67 // TODO: it would be nice to invalidate cbdata(this) when not destroyed
68 }
69
70 void Adaptation::Icap::ServiceRep::noteFailure()
71 {
72 ++theSessionFailures;
73 debugs(93,4, HERE << " failure " << theSessionFailures << " out of " <<
74 TheConfig.service_failure_limit << " allowed " << status());
75
76 if (isSuspended)
77 return;
78
79 if (TheConfig.service_failure_limit >= 0 &&
80 theSessionFailures > TheConfig.service_failure_limit)
81 suspend("too many failures");
82
83 // TODO: Should bypass setting affect how much Squid tries to talk to
84 // the ICAP service that is currently unusable and is likely to remain
85 // so for some time? The current code says "no". Perhaps the answer
86 // should be configurable.
87 }
88
89 void Adaptation::Icap::ServiceRep::suspend(const char *reason)
90 {
91 if (isSuspended) {
92 debugs(93,4, HERE << "keeping suspended, also for " << reason);
93 } else {
94 isSuspended = reason;
95 debugs(93,1, "suspending ICAP service for " << reason);
96 scheduleUpdate(squid_curtime + TheConfig.service_revival_delay);
97 announceStatusChange("suspended", true);
98 }
99 }
100
101 bool Adaptation::Icap::ServiceRep::probed() const
102 {
103 return theLastUpdate != 0;
104 }
105
106 bool Adaptation::Icap::ServiceRep::hasOptions() const
107 {
108 return theOptions && theOptions->valid() && theOptions->fresh();
109 }
110
111 bool Adaptation::Icap::ServiceRep::up() const
112 {
113 return self != NULL && !isSuspended && hasOptions();
114 }
115
116 bool Adaptation::Icap::ServiceRep::wantsUrl(const String &urlPath) const
117 {
118 Must(hasOptions());
119 return theOptions->transferKind(urlPath) != Adaptation::Icap::Options::xferIgnore;
120 }
121
122 bool Adaptation::Icap::ServiceRep::wantsPreview(const String &urlPath, size_t &wantedSize) const
123 {
124 Must(hasOptions());
125
126 if (theOptions->preview < 0)
127 return false;
128
129 if (theOptions->transferKind(urlPath) != Adaptation::Icap::Options::xferPreview)
130 return false;
131
132 wantedSize = theOptions->preview;
133
134 return true;
135 }
136
137 bool Adaptation::Icap::ServiceRep::allows204() const
138 {
139 Must(hasOptions());
140 return true; // in the future, we may have ACLs to prevent 204s
141 }
142
143
144 static
145 void ServiceRep_noteTimeToUpdate(void *data)
146 {
147 Adaptation::Icap::ServiceRep *service = static_cast<Adaptation::Icap::ServiceRep*>(data);
148 Must(service);
149 service->noteTimeToUpdate();
150 }
151
152 void Adaptation::Icap::ServiceRep::noteTimeToUpdate()
153 {
154 if (self != NULL)
155 updateScheduled = false;
156
157 if (!self || theOptionsFetcher) {
158 debugs(93,5, HERE << "ignores options update " << status());
159 return;
160 }
161
162 debugs(93,5, HERE << "performs a regular options update " << status());
163 startGettingOptions();
164 }
165
166 #if 0
167 static
168 void Adaptation::Icap::ServiceRep_noteTimeToNotify(void *data)
169 {
170 Adaptation::Icap::ServiceRep *service = static_cast<Adaptation::Icap::ServiceRep*>(data);
171 Must(service);
172 service->noteTimeToNotify();
173 }
174 #endif
175
176 void Adaptation::Icap::ServiceRep::noteTimeToNotify()
177 {
178 Must(!notifying);
179 notifying = true;
180 debugs(93,7, HERE << "notifies " << theClients.size() << " clients " <<
181 status());
182
183 // note: we must notify even if we are invalidated
184
185 Pointer us = NULL;
186
187 while (!theClients.empty()) {
188 Client i = theClients.pop_back();
189 ScheduleCallHere(i.callback);
190 i.callback = 0;
191 }
192
193 notifying = false;
194 }
195
196 void Adaptation::Icap::ServiceRep::callWhenReady(AsyncCall::Pointer &cb)
197 {
198 Must(cb!=NULL);
199
200 debugs(93,5, HERE << "Adaptation::Icap::Service is asked to call " << *cb <<
201 " when ready " << status());
202
203 Must(self != NULL);
204 Must(!broken()); // we do not wait for a broken service
205
206 Client i;
207 i.service = self; // TODO: is this really needed?
208 i.callback = cb;
209 theClients.push_back(i);
210
211 if (theOptionsFetcher || notifying)
212 return; // do nothing, we will be picked up in noteTimeToNotify()
213
214 if (needNewOptions())
215 startGettingOptions();
216 else
217 scheduleNotification();
218 }
219
220 void Adaptation::Icap::ServiceRep::scheduleNotification()
221 {
222 debugs(93,7, HERE << "will notify " << theClients.size() << " clients");
223 CallJobHere(93, 5, this, Adaptation::Icap::ServiceRep::noteTimeToNotify);
224 }
225
226 bool Adaptation::Icap::ServiceRep::needNewOptions() const
227 {
228 return self != NULL && !up();
229 }
230
231 void Adaptation::Icap::ServiceRep::changeOptions(Adaptation::Icap::Options *newOptions)
232 {
233 debugs(93,8, HERE << "changes options from " << theOptions << " to " <<
234 newOptions << ' ' << status());
235
236 delete theOptions;
237 theOptions = newOptions;
238 theSessionFailures = 0;
239 isSuspended = 0;
240 theLastUpdate = squid_curtime;
241
242 checkOptions();
243 announceStatusChange("down after an options fetch failure", true);
244 }
245
246 void Adaptation::Icap::ServiceRep::checkOptions()
247 {
248 if (theOptions == NULL)
249 return;
250
251 if (!theOptions->valid()) {
252 debugs(93,1, "WARNING: Squid got an invalid ICAP OPTIONS response " <<
253 "from service " << cfg().uri << "; error: " << theOptions->error);
254 return;
255 }
256
257 /*
258 * Issue a warning if the ICAP server returned methods in the
259 * options response that don't match the method from squid.conf.
260 */
261
262 if (!theOptions->methods.empty()) {
263 bool method_found = false;
264 String method_list;
265 Vector <ICAP::Method>::iterator iter = theOptions->methods.begin();
266
267 while (iter != theOptions->methods.end()) {
268
269 if (*iter == cfg().method) {
270 method_found = true;
271 break;
272 }
273
274 method_list.append(ICAP::methodStr(*iter));
275 method_list.append(" ", 1);
276 iter++;
277 }
278
279 if (!method_found) {
280 debugs(93,1, "WARNING: Squid is configured to use ICAP method " <<
281 cfg().methodStr() <<
282 " for service " << cfg().uri <<
283 " but OPTIONS response declares the methods are " << method_list);
284 }
285 }
286
287
288 /*
289 * Check the ICAP server's date header for clock skew
290 */
291 const int skew = (int)(theOptions->timestamp() - squid_curtime);
292 if (abs(skew) > theOptions->ttl()) {
293 // TODO: If skew is negative, the option will be considered down
294 // because of stale options. We should probably change this.
295 debugs(93, 1, "ICAP service's clock is skewed by " << skew <<
296 " seconds: " << cfg().uri);
297 }
298 }
299
300 void Adaptation::Icap::ServiceRep::announceStatusChange(const char *downPhrase, bool important) const
301 {
302 if (wasAnnouncedUp == up()) // no significant changes to announce
303 return;
304
305 const char *what = cfg().bypass ? "optional" : "essential";
306 const char *state = wasAnnouncedUp ? downPhrase : "up";
307 const int level = important ? 1 :2;
308 debugs(93,level, what << " ICAP service is " << state << ": " <<
309 cfg().uri << ' ' << status());
310
311 wasAnnouncedUp = !wasAnnouncedUp;
312 }
313
314 // we are receiving ICAP OPTIONS response headers here or NULL on failures
315 void Adaptation::Icap::ServiceRep::noteAdaptationAnswer(HttpMsg *msg)
316 {
317 Must(theOptionsFetcher);
318 clearAdaptation(theOptionsFetcher);
319
320 Must(msg);
321
322 debugs(93,5, HERE << "is interpreting new options " << status());
323
324 Adaptation::Icap::Options *newOptions = NULL;
325 if (HttpReply *r = dynamic_cast<HttpReply*>(msg)) {
326 newOptions = new Adaptation::Icap::Options;
327 newOptions->configure(r);
328 } else {
329 debugs(93,1, "ICAP service got wrong options message " << status());
330 }
331
332 handleNewOptions(newOptions);
333 }
334
335 void Adaptation::Icap::ServiceRep::noteAdaptationQueryAbort(bool)
336 {
337 Must(theOptionsFetcher);
338 clearAdaptation(theOptionsFetcher);
339
340 debugs(93,3, HERE << "failed to fetch options " << status());
341 handleNewOptions(0);
342 }
343
344 void Adaptation::Icap::ServiceRep::handleNewOptions(Adaptation::Icap::Options *newOptions)
345 {
346 // new options may be NULL
347 changeOptions(newOptions);
348
349 debugs(93,3, HERE << "got new options and is now " << status());
350
351 scheduleUpdate(optionsFetchTime());
352 scheduleNotification();
353 }
354
355 void Adaptation::Icap::ServiceRep::startGettingOptions()
356 {
357 Must(!theOptionsFetcher);
358 debugs(93,6, HERE << "will get new options " << status());
359
360 // XXX: second "this" is "self"; this works but may stop if API changes
361 theOptionsFetcher = initiateAdaptation(new Adaptation::Icap::OptXactLauncher(this, this));
362 Must(theOptionsFetcher);
363 // TODO: timeout in case Adaptation::Icap::OptXact never calls us back?
364 // Such a timeout should probably be a generic AsyncStart feature.
365 }
366
367 void Adaptation::Icap::ServiceRep::scheduleUpdate(time_t when)
368 {
369 if (updateScheduled) {
370 debugs(93,7, HERE << "reschedules update");
371 // XXX: check whether the event is there because AR saw
372 // an unreproducible eventDelete assertion on 2007/06/18
373 if (eventFind(&ServiceRep_noteTimeToUpdate, this))
374 eventDelete(&ServiceRep_noteTimeToUpdate, this);
375 else
376 debugs(93,1, "XXX: ICAP service lost an update event.");
377 updateScheduled = false;
378 }
379
380 debugs(93,7, HERE << "raw OPTIONS fetch at " << when << " or in " <<
381 (when - squid_curtime) << " sec");
382 debugs(93,9, HERE << "last fetched at " << theLastUpdate << " or " <<
383 (squid_curtime - theLastUpdate) << " sec ago");
384
385 /* adjust update time to prevent too-frequent updates */
386
387 if (when < squid_curtime)
388 when = squid_curtime;
389
390 // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig
391 const int minUpdateGap = 30; // seconds
392 if (when < theLastUpdate + minUpdateGap)
393 when = theLastUpdate + minUpdateGap;
394
395 const int delay = when - squid_curtime;
396 debugs(93,5, HERE << "will fetch OPTIONS in " << delay << " sec");
397
398 eventAdd("Adaptation::Icap::ServiceRep::noteTimeToUpdate",
399 &ServiceRep_noteTimeToUpdate, this, delay, 0, true);
400 updateScheduled = true;
401 }
402
403 // returns absolute time when OPTIONS should be fetched
404 time_t
405 Adaptation::Icap::ServiceRep::optionsFetchTime() const
406 {
407 if (theOptions && theOptions->valid()) {
408 const time_t expire = theOptions->expire();
409 debugs(93,7, HERE << "options expire on " << expire << " >= " << squid_curtime);
410
411 // conservative estimate of how long the OPTIONS transaction will take
412 // XXX: move hard-coded constants from here to Adaptation::Icap::TheConfig
413 const int expectedWait = 20; // seconds
414
415 // Unknown or invalid (too small) expiration times should not happen.
416 // Adaptation::Icap::Options should use the default TTL, and ICAP servers should not
417 // send invalid TTLs, but bugs and attacks happen.
418 if (expire < expectedWait)
419 return squid_curtime;
420 else
421 return expire - expectedWait; // before the current options expire
422 }
423
424 // use revival delay as "expiration" time for a service w/o valid options
425 return squid_curtime + TheConfig.service_revival_delay;
426 }
427
428 Adaptation::Initiate *
429 Adaptation::Icap::ServiceRep::makeXactLauncher(Adaptation::Initiator *initiator,
430 HttpMsg *virgin, HttpRequest *cause)
431 {
432 return new Adaptation::Icap::ModXactLauncher(initiator, virgin, cause, this);
433 }
434
435 // returns a temporary string depicting service status, for debugging
436 const char *Adaptation::Icap::ServiceRep::status() const
437 {
438 static MemBuf buf;
439
440 buf.reset();
441 buf.append("[", 1);
442
443 if (up())
444 buf.append("up", 2);
445 else {
446 buf.append("down", 4);
447 if (!self)
448 buf.append(",gone", 5);
449 if (isSuspended)
450 buf.append(",susp", 5);
451
452 if (!theOptions)
453 buf.append(",!opt", 5);
454 else
455 if (!theOptions->valid())
456 buf.append(",!valid", 7);
457 else
458 if (!theOptions->fresh())
459 buf.append(",stale", 6);
460 }
461
462 if (theOptionsFetcher)
463 buf.append(",fetch", 6);
464
465 if (notifying)
466 buf.append(",notif", 6);
467
468 if (theSessionFailures > 0)
469 buf.Printf(",fail%d", theSessionFailures);
470
471 buf.append("]", 1);
472 buf.terminate();
473
474 return buf.content();
475 }