Newer
Older
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
rv = apr_bucket_read(e, &str, &length, APR_BLOCK_READ);
APR_BUCKET_REMOVE(e);
APR_BRIGADE_INSERT_TAIL(out, e);
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, 0, r, APLOGNO(02377)
"Error when reading bucket for URL %s",
h->cache_obj->key);
/* Remove the intermediate cache file and return non-APR_SUCCESS */
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return rv;
}
/* don't write empty buckets to the cache */
if (!length) {
continue;
}
sobj->body_length += length;
if (sobj->body_length >= sobj->buffer_len - sobj->body_offset) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02378)
"URL %s failed the buffer size check "
"(%" APR_OFF_T_FMT ">=%" APR_SIZE_T_FMT ")",
h->cache_obj->key, sobj->body_length,
sobj->buffer_len - sobj->body_offset);
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return APR_EGENERAL;
}
memcpy(sobj->buffer + sobj->body_offset + sobj->body_length - length,
str, length);
/* have we reached the limit of how much we're prepared to write in one
* go? If so, leave, we'll get called again. This prevents us from trying
* to swallow too much data at once, or taking so long to write the data
* the client times out.
*/
sobj->offset -= length;
if (sobj->offset <= 0) {
sobj->offset = 0;
break;
}
if ((dconf->readtime && apr_time_now() > sobj->timeout)) {
sobj->timeout = 0;
break;
}
}
/* Was this the final bucket? If yes, perform sanity checks.
*/
if (seen_eos) {
const char *cl_header = apr_table_get(r->headers_out, "Content-Length");
if (r->connection->aborted || r->no_cache) {
ap_log_rerror(APLOG_MARK, APLOG_INFO, 0, r, APLOGNO(02380)
"Discarding body for URL %s "
"because connection has been aborted.",
h->cache_obj->key);
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return APR_EGENERAL;
}
if (cl_header) {
apr_off_t cl;
char *cl_endp;
if (apr_strtoff(&cl, cl_header, &cl_endp, 10) != APR_SUCCESS
|| *cl_endp != '\0' || cl != sobj->body_length) {
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02381)
"URL %s didn't receive complete response, not caching",
h->cache_obj->key);
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return APR_EGENERAL;
}
}
/* All checks were fine, we're good to go when the commit comes */
}
return APR_SUCCESS;
}
static apr_status_t commit_entity(cache_handle_t *h, request_rec *r)
{
cache_socache_conf *conf = ap_get_module_config(r->server->module_config,
&cache_socache_module);
cache_object_t *obj = h->cache_obj;
cache_socache_object_t *sobj = (cache_socache_object_t *) obj->vobj;
apr_status_t rv;
if (socache_mutex) {
apr_status_t status = apr_global_mutex_lock(socache_mutex);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02384)
"could not acquire lock, ignoring: %s", obj->key);
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return status;
}
}
rv = conf->provider->socache_provider->store(
conf->provider->socache_instance, r->server,
(unsigned char *) sobj->key, strlen(sobj->key), sobj->expire,
sobj->buffer, sobj->body_offset + sobj->body_length, sobj->pool);
if (socache_mutex) {
apr_status_t status = apr_global_mutex_unlock(socache_mutex);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02385)
"could not release lock, ignoring: %s", obj->key);
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return status;
}
}
if (rv != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_WARNING, rv, r, APLOGNO(02386)
"could not write to cache, ignoring: %s", sobj->key);
goto fail;
}
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(02387)
"commit_entity: Headers and body for URL %s cached for maximum of %d seconds.",
sobj->name, (apr_uint32_t)apr_time_sec(sobj->expire - r->request_time));
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return APR_SUCCESS;
fail:
/* For safety, remove any existing entry on failure, just in case it could not
* be revalidated successfully.
*/
if (socache_mutex) {
apr_status_t status = apr_global_mutex_lock(socache_mutex);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02388)
"could not acquire lock, ignoring: %s", obj->key);
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return rv;
}
}
conf->provider->socache_provider->remove(conf->provider->socache_instance,
r->server, (unsigned char *) sobj->key, strlen(sobj->key), r->pool);
if (socache_mutex) {
apr_status_t status = apr_global_mutex_unlock(socache_mutex);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02389)
"could not release lock, ignoring: %s", obj->key);
}
}
apr_pool_destroy(sobj->pool);
sobj->pool = NULL;
return rv;
}
static apr_status_t invalidate_entity(cache_handle_t *h, request_rec *r)
{
/* mark the entity as invalidated */
h->cache_obj->info.control.invalidated = 1;
return commit_entity(h, r);
}
static void *create_dir_config(apr_pool_t *p, char *dummy)
{
cache_socache_dir_conf *dconf =
apr_pcalloc(p, sizeof(cache_socache_dir_conf));
dconf->max = DEFAULT_MAX_FILE_SIZE;
dconf->maxtime = apr_time_from_sec(DEFAULT_MAXTIME);
dconf->mintime = apr_time_from_sec(DEFAULT_MINTIME);
dconf->readsize = DEFAULT_READSIZE;
dconf->readtime = DEFAULT_READTIME;
return dconf;
}
static void *merge_dir_config(apr_pool_t *p, void *basev, void *addv)
{
cache_socache_dir_conf
*new =
(cache_socache_dir_conf *) apr_pcalloc(p, sizeof(cache_socache_dir_conf));
cache_socache_dir_conf *add = (cache_socache_dir_conf *) addv;
cache_socache_dir_conf *base = (cache_socache_dir_conf *) basev;
new->max = (add->max_set == 0) ? base->max : add->max;
new->max_set = add->max_set || base->max_set;
new->maxtime = (add->maxtime_set == 0) ? base->maxtime : add->maxtime;
new->maxtime_set = add->maxtime_set || base->maxtime_set;
new->mintime = (add->mintime_set == 0) ? base->mintime : add->mintime;
new->mintime_set = add->mintime_set || base->mintime_set;
new->readsize = (add->readsize_set == 0) ? base->readsize : add->readsize;
new->readsize_set = add->readsize_set || base->readsize_set;
new->readtime = (add->readtime_set == 0) ? base->readtime : add->readtime;
new->readtime_set = add->readtime_set || base->readtime_set;
return new;
}
static void *create_config(apr_pool_t *p, server_rec *s)
{
cache_socache_conf *conf = apr_pcalloc(p, sizeof(cache_socache_conf));
return conf;
}
static void *merge_config(apr_pool_t *p, void *basev, void *overridesv)
{
cache_socache_conf *ps;
cache_socache_conf *base = (cache_socache_conf *) basev;
cache_socache_conf *overrides = (cache_socache_conf *) overridesv;
/* socache server config only has one field */
ps = overrides ? overrides : base;
return ps;
}
/*
* mod_cache_socache configuration directives handlers.
*/
static const char *set_cache_socache(cmd_parms *cmd, void *in_struct_ptr,
const char *arg)
{
cache_socache_conf *conf = ap_get_module_config(cmd->server->module_config,
&cache_socache_module);
cache_socache_provider_conf *provider = conf->provider
= apr_pcalloc(cmd->pool, sizeof(cache_socache_provider_conf));
const char *err = NULL, *sep, *name;
/* Argument is of form 'name:args' or just 'name'. */
sep = ap_strchr_c(arg, ':');
if (sep) {
name = apr_pstrmemdup(cmd->pool, arg, sep - arg);
sep++;
provider->args = sep;
}
else {
name = arg;
}
provider->socache_provider = ap_lookup_provider(AP_SOCACHE_PROVIDER_GROUP,
name, AP_SOCACHE_PROVIDER_VERSION);
if (provider->socache_provider == NULL) {
err = apr_psprintf(cmd->pool,
"Unknown socache provider '%s'. Maybe you need "
"to load the appropriate socache module "
"(mod_socache_%s?)", name, name);
}
return err;
}
static const char *set_cache_max(cmd_parms *parms, void *in_struct_ptr,
const char *arg)
{
cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr;
if (apr_strtoff(&dconf->max, arg, NULL, 10) != APR_SUCCESS
|| dconf->max < 1024 || dconf->max > APR_UINT32_MAX) {
return "CacheSocacheMaxSize argument must be a integer representing "
"the max size of a cached entry (headers and body), at least 1024 "
"and at most " APR_STRINGIFY(APR_UINT32_MAX);
}
dconf->max_set = 1;
return NULL;
}
static const char *set_cache_maxtime(cmd_parms *parms, void *in_struct_ptr,
const char *arg)
{
cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr;
apr_off_t seconds;
if (apr_strtoff(&seconds, arg, NULL, 10) != APR_SUCCESS || seconds < 0) {
return "CacheSocacheMaxTime argument must be the maximum amount of time in seconds to cache an entry.";
}
dconf->maxtime = apr_time_from_sec(seconds);
dconf->maxtime_set = 1;
return NULL;
}
static const char *set_cache_mintime(cmd_parms *parms, void *in_struct_ptr,
const char *arg)
{
cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr;
apr_off_t seconds;
if (apr_strtoff(&seconds, arg, NULL, 10) != APR_SUCCESS || seconds < 0) {
return "CacheSocacheMinTime argument must be the minimum amount of time in seconds to cache an entry.";
}
dconf->mintime = apr_time_from_sec(seconds);
dconf->mintime_set = 1;
return NULL;
}
static const char *set_cache_readsize(cmd_parms *parms, void *in_struct_ptr,
const char *arg)
{
cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr;
if (apr_strtoff(&dconf->readsize, arg, NULL, 10) != APR_SUCCESS
|| dconf->readsize < 0) {
return "CacheSocacheReadSize argument must be a non-negative integer representing the max amount of data to cache in go.";
}
dconf->readsize_set = 1;
return NULL;
}
static const char *set_cache_readtime(cmd_parms *parms, void *in_struct_ptr,
const char *arg)
{
cache_socache_dir_conf *dconf = (cache_socache_dir_conf *) in_struct_ptr;
apr_off_t milliseconds;
if (apr_strtoff(&milliseconds, arg, NULL, 10) != APR_SUCCESS
|| milliseconds < 0) {
return "CacheSocacheReadTime argument must be a non-negative integer representing the max amount of time taken to cache in go.";
}
dconf->readtime = apr_time_from_msec(milliseconds);
dconf->readtime_set = 1;
return NULL;
}
static apr_status_t remove_lock(void *data)
{
if (socache_mutex) {
apr_global_mutex_destroy(socache_mutex);
socache_mutex = NULL;
}
return APR_SUCCESS;
}
static apr_status_t destroy_cache(void *data)
{
server_rec *s = data;
cache_socache_conf *conf =
ap_get_module_config(s->module_config, &cache_socache_module);
if (conf->provider && conf->provider->socache_instance) {
conf->provider->socache_provider->destroy(
conf->provider->socache_instance, s);
conf->provider->socache_instance = NULL;
}
return APR_SUCCESS;
}
static int socache_status_hook(request_rec *r, int flags)
{
apr_status_t status = APR_SUCCESS;
cache_socache_conf *conf = ap_get_module_config(r->server->module_config,
&cache_socache_module);
if (!conf->provider || !conf->provider->socache_provider ||
!conf->provider->socache_instance) {
return DECLINED;
}
if (!(flags & AP_STATUS_SHORT)) {
ap_rputs("<hr>\n"
"<table cellspacing=0 cellpadding=0>\n"
"<tr><td bgcolor=\"#000000\">\n"
"<b><font color=\"#ffffff\" face=\"Arial,Helvetica\">"
"mod_cache_socache Status:</font></b>\n"
"</td></tr>\n"
"<tr><td bgcolor=\"#ffffff\">\n", r);
}
else {
ap_rputs("ModCacheSocacheStatus\n", r);
}
if (socache_mutex) {
status = apr_global_mutex_lock(socache_mutex);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02816)
"could not acquire lock for cache status");
}
}
if (status != APR_SUCCESS) {
if (!(flags & AP_STATUS_SHORT)) {
ap_rputs("No cache status data available\n", r);
}
else {
ap_rputs("NotAvailable\n", r);
}
} else {
conf->provider->socache_provider->status(conf->provider->socache_instance,
r, flags);
}
if (socache_mutex && status == APR_SUCCESS) {
status = apr_global_mutex_unlock(socache_mutex);
if (status != APR_SUCCESS) {
ap_log_rerror(APLOG_MARK, APLOG_ERR, status, r, APLOGNO(02817)
"could not release lock for cache status");
}
}
if (!(flags & AP_STATUS_SHORT)) {
ap_rputs("</td></tr>\n</table>\n", r);
}
return OK;
}
static void socache_status_register(apr_pool_t *p)
{
APR_OPTIONAL_HOOK(ap, status_hook, socache_status_hook, NULL, NULL, APR_HOOK_MIDDLE);
}
static int socache_precfg(apr_pool_t *pconf, apr_pool_t *plog, apr_pool_t *ptmp)
{
apr_status_t rv = ap_mutex_register(pconf, cache_socache_id, NULL,
APR_LOCK_DEFAULT, 0);
if (rv != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(02390)
"failed to register %s mutex", cache_socache_id);
return 500; /* An HTTP status would be a misnomer! */
}
/* Register to handle mod_status status page generation */
socache_status_register(pconf);
return OK;
}
static int socache_post_config(apr_pool_t *pconf, apr_pool_t *plog,
apr_pool_t *ptmp, server_rec *base_server)
{
server_rec *s;
apr_status_t rv;
const char *errmsg;
static struct ap_socache_hints socache_hints =
{ 64, 2048, 60000000 };
for (s = base_server; s; s = s->next) {
cache_socache_conf *conf =
ap_get_module_config(s->module_config, &cache_socache_module);
if (!conf->provider) {
continue;
}
if (!socache_mutex && conf->provider->socache_provider->flags
& AP_SOCACHE_FLAG_NOTMPSAFE) {
rv = ap_global_mutex_create(&socache_mutex, NULL, cache_socache_id,
NULL, s, pconf, 0);
if (rv != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(02391)
"failed to create %s mutex", cache_socache_id);
return 500; /* An HTTP status would be a misnomer! */
}
apr_pool_cleanup_register(pconf, NULL, remove_lock,
apr_pool_cleanup_null);
}
errmsg = conf->provider->socache_provider->create(
&conf->provider->socache_instance, conf->provider->args, ptmp,
pconf);
if (errmsg) {
ap_log_perror(APLOG_MARK, APLOG_CRIT, 0, plog,
APLOGNO(02392) "%s", errmsg);
return 500; /* An HTTP status would be a misnomer! */
}
rv = conf->provider->socache_provider->init(
conf->provider->socache_instance, cache_socache_id,
&socache_hints, s, pconf);
if (rv != APR_SUCCESS) {
ap_log_perror(APLOG_MARK, APLOG_CRIT, rv, plog, APLOGNO(02393)
"failed to initialise %s cache", cache_socache_id);
return 500; /* An HTTP status would be a misnomer! */
}
apr_pool_cleanup_register(pconf, (void *) s, destroy_cache,
apr_pool_cleanup_null);
}
return OK;
}
static void socache_child_init(apr_pool_t *p, server_rec *s)
{
const char *lock;
apr_status_t rv;
if (!socache_mutex) {
return; /* don't waste the overhead of creating mutex & cache */
}
lock = apr_global_mutex_lockfile(socache_mutex);
rv = apr_global_mutex_child_init(&socache_mutex, lock, p);
if (rv != APR_SUCCESS) {
ap_log_error(APLOG_MARK, APLOG_CRIT, rv, s, APLOGNO(02394)
"failed to initialise mutex in child_init");
}
}
static const command_rec cache_socache_cmds[] =
{
AP_INIT_TAKE1("CacheSocache", set_cache_socache, NULL, RSRC_CONF,
"The shared object cache to store cache files"),
AP_INIT_TAKE1("CacheSocacheMaxTime", set_cache_maxtime, NULL, RSRC_CONF | ACCESS_CONF,
"The maximum cache expiry age to cache a document in seconds"),
AP_INIT_TAKE1("CacheSocacheMinTime", set_cache_mintime, NULL, RSRC_CONF | ACCESS_CONF,
"The minimum cache expiry age to cache a document in seconds"),
AP_INIT_TAKE1("CacheSocacheMaxSize", set_cache_max, NULL, RSRC_CONF | ACCESS_CONF,
"The maximum cache entry size (headers and body) to cache a document"),
AP_INIT_TAKE1("CacheSocacheReadSize", set_cache_readsize, NULL, RSRC_CONF | ACCESS_CONF,
"The maximum quantity of data to attempt to read and cache in one go"),
AP_INIT_TAKE1("CacheSocacheReadTime", set_cache_readtime, NULL, RSRC_CONF | ACCESS_CONF,
"The maximum time taken to attempt to read and cache in go"),
{ NULL }
};
static const cache_provider cache_socache_provider =
{
&remove_entity, &store_headers, &store_body, &recall_headers, &recall_body,
&create_entity, &open_entity, &remove_url, &commit_entity,
&invalidate_entity
};
static void cache_socache_register_hook(apr_pool_t *p)
{
/* cache initializer */
ap_register_provider(p, CACHE_PROVIDER_GROUP, "socache", "0",
&cache_socache_provider);
ap_hook_pre_config(socache_precfg, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_post_config(socache_post_config, NULL, NULL, APR_HOOK_MIDDLE);
ap_hook_child_init(socache_child_init, NULL, NULL, APR_HOOK_MIDDLE);
}
AP_DECLARE_MODULE(cache_socache) = { STANDARD20_MODULE_STUFF,
create_dir_config, /* create per-directory config structure */
merge_dir_config, /* merge per-directory config structures */
create_config, /* create per-server config structure */
merge_config, /* merge per-server config structures */
cache_socache_cmds, /* command apr_table_t */
cache_socache_register_hook /* register hooks */
};