1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
|
/** @file
A brief file description
@section license License
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*****************************************************************************
*
* CacheControl.cc - Implementation to Cache Control system
*
*
****************************************************************************/
#include <sys/types.h>
#include "tscore/ink_config.h"
#include "tscore/Filenames.h"
#include "CacheControl.h"
#include "ControlMatcher.h"
#include "Main.h"
#include "P_EventSystem.h"
#include "ProxyConfig.h"
#include "HTTP.h"
#include "HttpConfig.h"
#include "P_Cache.h"
#include "tscore/Regex.h"
static const char modulePrefix[] = "[CacheControl]";
#define TWEAK_CACHE_RESPONSES_TO_COOKIES "cache-responses-to-cookies"
static const char *CC_directive_str[CC_NUM_TYPES] = {
"INVALID",
"REVALIDATE_AFTER",
"NEVER_CACHE",
"STANDARD_CACHE",
"IGNORE_NO_CACHE",
"IGNORE_CLIENT_NO_CACHE",
"IGNORE_SERVER_NO_CACHE",
"PIN_IN_CACHE",
"TTL_IN_CACHE"
// "CACHE_AUTH_CONTENT"
};
using CC_table = ControlMatcher<CacheControlRecord, CacheControlResult>;
// Global Ptrs
static Ptr<ProxyMutex> reconfig_mutex;
CC_table *CacheControlTable = nullptr;
// struct CC_FreerContinuation
// Continuation to free old cache control lists after
// a timeout
//
struct CC_FreerContinuation;
using CC_FreerContHandler = int (CC_FreerContinuation::*)(int, void *);
struct CC_FreerContinuation : public Continuation {
CC_table *p;
int
freeEvent(int /* event ATS_UNUSED */, Event * /* e ATS_UNUSED */)
{
Debug("cache_control", "Deleting old table");
delete p;
delete this;
return EVENT_DONE;
}
CC_FreerContinuation(CC_table *ap) : Continuation(nullptr), p(ap) { SET_HANDLER(&CC_FreerContinuation::freeEvent); }
};
// struct CC_UpdateContinuation
//
// Used to read the cache.conf file after the manager signals
// a change
//
struct CC_UpdateContinuation : public Continuation {
int
file_update_handler(int /* etype ATS_UNUSED */, void * /* data ATS_UNUSED */)
{
reloadCacheControl();
delete this;
return EVENT_DONE;
}
CC_UpdateContinuation(Ptr<ProxyMutex> &m) : Continuation(m) { SET_HANDLER(&CC_UpdateContinuation::file_update_handler); }
};
int
cacheControlFile_CB(const char * /* name ATS_UNUSED */, RecDataT /* data_type ATS_UNUSED */, RecData /* data ATS_UNUSED */,
void * /* cookie ATS_UNUSED */)
{
eventProcessor.schedule_imm(new CC_UpdateContinuation(reconfig_mutex), ET_CACHE);
return 0;
}
//
// Begin API functions
//
bool
host_rule_in_CacheControlTable()
{
return (CacheControlTable->hostMatch ? true : false);
}
bool
ip_rule_in_CacheControlTable()
{
return (CacheControlTable->ipMatch ? true : false);
}
void
initCacheControl()
{
ink_assert(CacheControlTable == nullptr);
reconfig_mutex = new_ProxyMutex();
CacheControlTable = new CC_table("proxy.config.cache.control.filename", modulePrefix, &http_dest_tags);
REC_RegisterConfigUpdateFunc("proxy.config.cache.control.filename", cacheControlFile_CB, nullptr);
}
// void reloadCacheControl()
//
// Called when the cache.conf file changes. Since it called
// infrequently, we do the load of new file as blocking I/O and
// lock acquire is also blocking
//
void
reloadCacheControl()
{
Note("%s loading ...", ts::filename::CACHE);
CC_table *newTable;
Debug("cache_control", "%s updated, reloading", ts::filename::CACHE);
eventProcessor.schedule_in(new CC_FreerContinuation(CacheControlTable), CACHE_CONTROL_TIMEOUT, ET_CACHE);
newTable = new CC_table("proxy.config.cache.control.filename", modulePrefix, &http_dest_tags);
ink_atomic_swap(&CacheControlTable, newTable);
Note("%s finished loading", ts::filename::CACHE);
}
void
getCacheControl(CacheControlResult *result, HttpRequestData *rdata, const OverridableHttpConfigParams *h_txn_conf, char *tag)
{
rdata->tag = tag;
CacheControlTable->Match(rdata, result);
if (h_txn_conf->cache_ignore_client_no_cache) {
result->ignore_client_no_cache = true;
}
if (h_txn_conf->cache_ignore_server_no_cache) {
result->ignore_server_no_cache = true;
}
if (!h_txn_conf->cache_ignore_client_cc_max_age) {
result->ignore_client_cc_max_age = false;
}
}
//
// End API functions
//
// void CacheControlResult::Print()
//
// Debugging Method
//
void
CacheControlResult::Print() const
{
printf("\t reval: %d, never-cache: %d, pin: %d, ignore-c: %d ignore-s: %d\n", revalidate_after, never_cache, pin_in_cache_for,
ignore_client_no_cache, ignore_server_no_cache);
}
// void CacheControlRecord::Print()
//
// Debugging Method
//
void
CacheControlRecord::Print() const
{
switch (this->directive) {
case CC_REVALIDATE_AFTER:
printf("\t\tDirective: %s : %d\n", CC_directive_str[CC_REVALIDATE_AFTER], this->time_arg);
break;
case CC_PIN_IN_CACHE:
printf("\t\tDirective: %s : %d\n", CC_directive_str[CC_PIN_IN_CACHE], this->time_arg);
break;
case CC_TTL_IN_CACHE:
printf("\t\tDirective: %s : %d\n", CC_directive_str[CC_TTL_IN_CACHE], this->time_arg);
break;
case CC_IGNORE_CLIENT_NO_CACHE:
case CC_IGNORE_SERVER_NO_CACHE:
case CC_NEVER_CACHE:
case CC_STANDARD_CACHE:
case CC_IGNORE_NO_CACHE:
printf("\t\tDirective: %s\n", CC_directive_str[this->directive]);
break;
case CC_INVALID:
case CC_NUM_TYPES:
printf("\t\tDirective: INVALID\n");
break;
}
if (cache_responses_to_cookies >= 0) {
printf("\t\t - " TWEAK_CACHE_RESPONSES_TO_COOKIES ":%d\n", cache_responses_to_cookies);
}
ControlBase::Print();
}
// Result CacheControlRecord::Init(matcher_line* line_info)
//
// matcher_line* line_info - contains parsed label/value
// pairs of the current cache.config line
//
// Returns NULL if everything is OK
// Otherwise, returns an error string that the caller MUST
// DEALLOCATE with free()
//
Result
CacheControlRecord::Init(matcher_line *line_info)
{
int time_in;
const char *tmp;
char *label;
char *val;
bool d_found = false;
this->line_num = line_info->line_num;
// First pass for optional tweaks.
for (int i = 0; i < MATCHER_MAX_TOKENS && line_info->num_el; ++i) {
bool used = false;
label = line_info->line[0][i];
val = line_info->line[1][i];
if (!label) {
continue;
}
if (strcasecmp(label, TWEAK_CACHE_RESPONSES_TO_COOKIES) == 0) {
char *ptr = nullptr;
int v = strtol(val, &ptr, 0);
if (!ptr || v < 0 || v > 4) {
return Result::failure("Value for " TWEAK_CACHE_RESPONSES_TO_COOKIES " must be an integer in the range 0..4");
} else {
cache_responses_to_cookies = v;
}
used = true;
}
// Clip pair if used.
if (used) {
line_info->line[0][i] = nullptr;
--(line_info->num_el);
}
}
// Now look for the directive.
for (int i = 0; i < MATCHER_MAX_TOKENS; i++) {
label = line_info->line[0][i];
val = line_info->line[1][i];
if (label == nullptr) {
continue;
}
if (strcasecmp(label, "action") == 0) {
if (strcasecmp(val, "never-cache") == 0) {
directive = CC_NEVER_CACHE;
d_found = true;
} else if (strcasecmp(val, "standard-cache") == 0) {
directive = CC_STANDARD_CACHE;
d_found = true;
} else if (strcasecmp(val, "ignore-no-cache") == 0) {
directive = CC_IGNORE_NO_CACHE;
d_found = true;
} else if (strcasecmp(val, "ignore-client-no-cache") == 0) {
directive = CC_IGNORE_CLIENT_NO_CACHE;
d_found = true;
} else if (strcasecmp(val, "ignore-server-no-cache") == 0) {
directive = CC_IGNORE_SERVER_NO_CACHE;
d_found = true;
} else {
return Result::failure("%s Invalid action at line %d in %s", modulePrefix, line_num, ts::filename::CACHE);
}
} else {
if (strcasecmp(label, "revalidate") == 0) {
directive = CC_REVALIDATE_AFTER;
d_found = true;
} else if (strcasecmp(label, "pin-in-cache") == 0) {
directive = CC_PIN_IN_CACHE;
d_found = true;
} else if (strcasecmp(label, "ttl-in-cache") == 0) {
directive = CC_TTL_IN_CACHE;
d_found = true;
}
// Process the time argument for the remaining directives
if (d_found == true) {
tmp = processDurationString(val, &time_in);
if (tmp == nullptr) {
this->time_arg = time_in;
} else {
return Result::failure("%s %s at line %d in %s", modulePrefix, tmp, line_num, ts::filename::CACHE);
}
}
}
if (d_found == true) {
// Consume the label/value pair we used
line_info->line[0][i] = nullptr;
line_info->num_el--;
break;
}
}
if (d_found == false) {
return Result::failure("%s No directive in %s at line %d", modulePrefix, ts::filename::CACHE, line_num);
}
// Process any modifiers to the directive, if they exist
if (line_info->num_el > 0) {
tmp = ProcessModifiers(line_info);
if (tmp != nullptr) {
return Result::failure("%s %s at line %d in %s", modulePrefix, tmp, line_num, ts::filename::CACHE);
}
}
return Result::ok();
}
// void CacheControlRecord::UpdateMatch(CacheControlResult* result, RequestData* rdata)
//
// Updates the parameters in result if the this element
// appears later in the file
//
void
CacheControlRecord::UpdateMatch(CacheControlResult *result, RequestData *rdata)
{
bool match = false;
HttpRequestData *h_rdata = (HttpRequestData *)rdata;
switch (this->directive) {
case CC_REVALIDATE_AFTER:
if (this->CheckForMatch(h_rdata, result->reval_line) == true) {
result->revalidate_after = time_arg;
result->reval_line = this->line_num;
match = true;
}
break;
case CC_NEVER_CACHE:
if (this->CheckForMatch(h_rdata, result->never_line) == true) {
// ttl-in-cache overrides never-cache
if (result->ttl_line == -1) {
result->never_cache = true;
result->never_line = this->line_num;
match = true;
}
}
break;
case CC_STANDARD_CACHE:
// Standard cache just overrides never-cache
if (this->CheckForMatch(h_rdata, result->never_line) == true) {
result->never_cache = false;
result->never_line = this->line_num;
match = true;
}
break;
case CC_IGNORE_NO_CACHE:
// We cover both client & server cases for this directive
// FALLTHROUGH
case CC_IGNORE_CLIENT_NO_CACHE:
if (this->CheckForMatch(h_rdata, result->ignore_client_line) == true) {
result->ignore_client_no_cache = true;
result->ignore_client_line = this->line_num;
match = true;
}
if (this->directive != CC_IGNORE_NO_CACHE) {
break;
}
// FALLTHROUGH
case CC_IGNORE_SERVER_NO_CACHE:
if (this->CheckForMatch(h_rdata, result->ignore_server_line) == true) {
result->ignore_server_no_cache = true;
result->ignore_server_line = this->line_num;
match = true;
}
break;
case CC_PIN_IN_CACHE:
if (this->CheckForMatch(h_rdata, result->pin_line) == true) {
result->pin_in_cache_for = time_arg;
result->pin_line = this->line_num;
match = true;
}
break;
case CC_TTL_IN_CACHE:
if (this->CheckForMatch(h_rdata, result->ttl_line) == true) {
result->ttl_in_cache = time_arg;
result->ttl_line = this->line_num;
// ttl-in-cache overrides never-cache
result->never_cache = false;
result->never_line = this->line_num;
match = true;
}
break;
case CC_INVALID:
case CC_NUM_TYPES:
default:
// Should not get here
Warning("Impossible directive in CacheControlRecord::UpdateMatch");
ink_assert(0);
break;
}
if (cache_responses_to_cookies >= 0) {
result->cache_responses_to_cookies = cache_responses_to_cookies;
}
if (match == true) {
char crtc_debug[80];
if (result->cache_responses_to_cookies >= 0) {
snprintf(crtc_debug, sizeof(crtc_debug), " [" TWEAK_CACHE_RESPONSES_TO_COOKIES "=%d]", result->cache_responses_to_cookies);
} else {
crtc_debug[0] = 0;
}
Debug("cache_control", "Matched with for %s at line %d%s", CC_directive_str[this->directive], this->line_num, crtc_debug);
}
}
|