/*
- * Copyright (C) 1996-2016 The Squid Software Foundation and contributors
+ * Copyright (C) 1996-2017 The Squid Software Foundation and contributors
*
* Squid software is distributed under GPLv2+ license and includes
* contributions from numerous individuals and organizations.
}
void
-StoreEntry::makePublic()
+StoreEntry::makePublic(const KeyScope scope)
{
/* This object can be cached for a long time */
-
if (!EBIT_TEST(flags, RELEASE_REQUEST))
- setPublicKey();
+ setPublicKey(scope);
}
void
* ->deferRead (fd, buf, len, callback, DelayAwareRead, this)
*/
- if (amountToRead == 0) {
+ if (amountToRead <= 0) {
assert (mem_obj);
- /* read ahead limit */
- /* Perhaps these two calls should both live in MemObject */
-#if USE_DELAY_POOLS
- if (!mem_obj->readAheadPolicyCanRead()) {
-#endif
- mem_obj->delayRead(DeferredRead(DeferReader, this, CommRead(conn, buf, len, callback)));
- return;
-#if USE_DELAY_POOLS
- }
-
- /* delay id limit */
- mem_obj->mostBytesAllowed().delayRead(DeferredRead(DeferReader, this, CommRead(conn, buf, len, callback)));
+ mem_obj->delayRead(DeferredRead(DeferReader, this, CommRead(conn, buf, len, callback)));
return;
-
-#endif
-
}
if (fd_table[conn->fd].closing()) {
timestamp(-1),
lastref(-1),
expires(-1),
- lastmod(-1),
+ lastModified_(-1),
swap_file_sz(0),
refcount(0),
flags(0),
}
StoreEntry *
-storeGetPublicByRequestMethod(HttpRequest * req, const HttpRequestMethod& method)
+storeGetPublicByRequestMethod(HttpRequest * req, const HttpRequestMethod& method, const KeyScope keyScope)
{
- return Store::Root().get(storeKeyPublicByRequestMethod(req, method));
+ return Store::Root().get(storeKeyPublicByRequestMethod(req, method, keyScope));
}
StoreEntry *
-storeGetPublicByRequest(HttpRequest * req)
+storeGetPublicByRequest(HttpRequest * req, const KeyScope keyScope)
{
- StoreEntry *e = storeGetPublicByRequestMethod(req, req->method);
+ StoreEntry *e = storeGetPublicByRequestMethod(req, req->method, keyScope);
if (e == NULL && req->method == Http::METHOD_HEAD)
/* We can generate a HEAD reply from a cached GET object */
- e = storeGetPublicByRequestMethod(req, Http::METHOD_GET);
+ e = storeGetPublicByRequestMethod(req, Http::METHOD_GET, keyScope);
return e;
}
void
StoreEntry::setPrivateKey()
{
- const cache_key *newkey;
-
if (key && EBIT_TEST(flags, KEY_PRIVATE))
return; /* is already private */
hashDelete();
}
- if (mem_obj && mem_obj->hasUris()) {
+ if (mem_obj && mem_obj->hasUris())
mem_obj->id = getKeyCounter();
- newkey = storeKeyPrivate(mem_obj->storeId(), mem_obj->method, mem_obj->id);
- } else {
- newkey = storeKeyPrivate("JUNK", Http::METHOD_NONE, getKeyCounter());
- }
+ const cache_key *newkey = storeKeyPrivate();
assert(hash_lookup(store_table, newkey) == NULL);
EBIT_SET(flags, KEY_PRIVATE);
}
void
-StoreEntry::setPublicKey()
+StoreEntry::setPublicKey(const KeyScope scope)
{
- const cache_key *newkey;
-
if (key && !EBIT_TEST(flags, KEY_PRIVATE))
return; /* is already public */
assert(!EBIT_TEST(flags, RELEASE_REQUEST));
- if (mem_obj->request) {
- HttpRequest *request = mem_obj->request;
-
- if (mem_obj->vary_headers.isEmpty()) {
- /* First handle the case where the object no longer varies */
- request->vary_headers.clear();
- } else {
- if (!request->vary_headers.isEmpty() && request->vary_headers.cmp(mem_obj->vary_headers) != 0) {
- /* Oops.. the variance has changed. Kill the base object
- * to record the new variance key
- */
- request->vary_headers.clear(); /* free old "bad" variance key */
- if (StoreEntry *pe = storeGetPublic(mem_obj->storeId(), mem_obj->method))
- pe->release();
- }
-
- /* Make sure the request knows the variance status */
- if (request->vary_headers.isEmpty())
- request->vary_headers = httpMakeVaryMark(request, mem_obj->getReply());
- }
-
- // TODO: storeGetPublic() calls below may create unlocked entries.
- // We should add/use storeHas() API or lock/unlock those entries.
- if (!mem_obj->vary_headers.isEmpty() && !storeGetPublic(mem_obj->storeId(), mem_obj->method)) {
- /* Create "vary" base object */
- String vary;
- StoreEntry *pe = storeCreateEntry(mem_obj->storeId(), mem_obj->logUri(), request->flags, request->method);
- /* We are allowed to do this typecast */
- HttpReply *rep = new HttpReply;
- rep->setHeaders(Http::scOkay, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime + 100000);
- vary = mem_obj->getReply()->header.getList(Http::HdrType::VARY);
-
- if (vary.size()) {
- /* Again, we own this structure layout */
- rep->header.putStr(Http::HdrType::VARY, vary.termedBuf());
- vary.clean();
- }
-
-#if X_ACCELERATOR_VARY
- vary = mem_obj->getReply()->header.getList(Http::HdrType::HDR_X_ACCELERATOR_VARY);
-
- if (vary.size() > 0) {
- /* Again, we own this structure layout */
- rep->header.putStr(Http::HdrType::HDR_X_ACCELERATOR_VARY, vary.termedBuf());
- vary.clean();
- }
-
-#endif
- pe->replaceHttpReply(rep, false); // no write until key is public
-
- pe->timestampsSet();
+ adjustVary();
+ forcePublicKey(calcPublicKey(scope));
+}
- pe->makePublic();
+void
+StoreEntry::clearPublicKeyScope()
+{
+ if (!key || EBIT_TEST(flags, KEY_PRIVATE))
+ return; // probably the old public key was deleted or made private
- pe->startWriting(); // after makePublic()
+ // TODO: adjustVary() when collapsed revalidation supports that
- pe->complete();
+ const cache_key *newKey = calcPublicKey(ksDefault);
+ if (!storeKeyHashCmp(key, newKey))
+ return; // probably another collapsed revalidation beat us to this change
- pe->unlock("StoreEntry::setPublicKey+Vary");
- }
-
- newkey = storeKeyPublicByRequest(mem_obj->request);
- } else
- newkey = storeKeyPublic(mem_obj->storeId(), mem_obj->method);
+ forcePublicKey(newKey);
+}
+/// Unconditionally sets public key for this store entry.
+/// Releases the old entry with the same public key (if any).
+void
+StoreEntry::forcePublicKey(const cache_key *newkey)
+{
if (StoreEntry *e2 = (StoreEntry *)hash_lookup(store_table, newkey)) {
+ assert(e2 != this);
debugs(20, 3, "Making old " << *e2 << " private.");
e2->setPrivateKey();
e2->release();
-
- if (mem_obj->request)
- newkey = storeKeyPublicByRequest(mem_obj->request);
- else
- newkey = storeKeyPublic(mem_obj->storeId(), mem_obj->method);
}
if (key)
storeDirSwapLog(this, SWAP_LOG_ADD);
}
+/// Calculates correct public key for feeding forcePublicKey().
+/// Assumes adjustVary() has been called for this entry already.
+const cache_key *
+StoreEntry::calcPublicKey(const KeyScope keyScope)
+{
+ assert(mem_obj);
+ return mem_obj->request ? storeKeyPublicByRequest(mem_obj->request.getRaw(), keyScope) :
+ storeKeyPublic(mem_obj->storeId(), mem_obj->method, keyScope);
+}
+
+/// Updates mem_obj->request->vary_headers to reflect the current Vary.
+/// The vary_headers field is used to calculate the Vary marker key.
+/// Releases the old Vary marker with an outdated key (if any).
+void
+StoreEntry::adjustVary()
+{
+ assert(mem_obj);
+
+ if (!mem_obj->request)
+ return;
+
+ HttpRequestPointer request(mem_obj->request);
+
+ if (mem_obj->vary_headers.isEmpty()) {
+ /* First handle the case where the object no longer varies */
+ request->vary_headers.clear();
+ } else {
+ if (!request->vary_headers.isEmpty() && request->vary_headers.cmp(mem_obj->vary_headers) != 0) {
+ /* Oops.. the variance has changed. Kill the base object
+ * to record the new variance key
+ */
+ request->vary_headers.clear(); /* free old "bad" variance key */
+ if (StoreEntry *pe = storeGetPublic(mem_obj->storeId(), mem_obj->method))
+ pe->release();
+ }
+
+ /* Make sure the request knows the variance status */
+ if (request->vary_headers.isEmpty())
+ request->vary_headers = httpMakeVaryMark(request.getRaw(), mem_obj->getReply().getRaw());
+ }
+
+ // TODO: storeGetPublic() calls below may create unlocked entries.
+ // We should add/use storeHas() API or lock/unlock those entries.
+ if (!mem_obj->vary_headers.isEmpty() && !storeGetPublic(mem_obj->storeId(), mem_obj->method)) {
+ /* Create "vary" base object */
+ String vary;
+ StoreEntry *pe = storeCreateEntry(mem_obj->storeId(), mem_obj->logUri(), request->flags, request->method);
+ /* We are allowed to do this typecast */
+ HttpReply *rep = new HttpReply;
+ rep->setHeaders(Http::scOkay, "Internal marker object", "x-squid-internal/vary", -1, -1, squid_curtime + 100000);
+ vary = mem_obj->getReply()->header.getList(Http::HdrType::VARY);
+
+ if (vary.size()) {
+ /* Again, we own this structure layout */
+ rep->header.putStr(Http::HdrType::VARY, vary.termedBuf());
+ vary.clean();
+ }
+
+#if X_ACCELERATOR_VARY
+ vary = mem_obj->getReply()->header.getList(Http::HdrType::HDR_X_ACCELERATOR_VARY);
+
+ if (vary.size() > 0) {
+ /* Again, we own this structure layout */
+ rep->header.putStr(Http::HdrType::HDR_X_ACCELERATOR_VARY, vary.termedBuf());
+ vary.clean();
+ }
+
+#endif
+ pe->replaceHttpReply(rep, false); // no write until key is public
+
+ pe->timestampsSet();
+
+ pe->makePublic();
+
+ pe->startWriting(); // after makePublic()
+
+ pe->complete();
+
+ pe->unlock("StoreEntry::forcePublicKey+Vary");
+ }
+}
+
StoreEntry *
storeCreatePureEntry(const char *url, const char *log_url, const RequestFlags &flags, const HttpRequestMethod& method)
{
assert(store_status == STORE_PENDING);
// XXX: caller uses content offset, but we also store headers
- if (const HttpReply *reply = mem_obj->getReply())
+ if (const HttpReplyPointer reply = mem_obj->getReply())
writeBuffer.offset += reply->hdr_sz;
debugs(20, 5, "storeWrite: writing " << writeBuffer.length << " bytes for '" << getMD5Text() << "'");
return 1;
}
-void
+bool
StoreEntry::timestampsSet()
{
const HttpReply *reply = getReply();
served_date -= (squid_curtime - request_sent);
}
+ time_t exp = 0;
if (reply->expires > 0 && reply->date > -1)
- expires = served_date + (reply->expires - reply->date);
+ exp = served_date + (reply->expires - reply->date);
else
- expires = reply->expires;
+ exp = reply->expires;
+
+ if (timestamp == served_date && expires == exp) {
+ // if the reply lacks LMT, then we now know that our effective
+ // LMT (i.e., timestamp) will stay the same, otherwise, old and
+ // new modification times must match
+ if (reply->last_modified < 0 || reply->last_modified == lastModified())
+ return false; // nothing has changed
+ }
- lastmod = reply->last_modified;
+ expires = exp;
+
+ lastModified_ = reply->last_modified;
timestamp = served_date;
+
+ return true;
}
void
debugs(20, l, "StoreEntry->timestamp: " << timestamp);
debugs(20, l, "StoreEntry->lastref: " << lastref);
debugs(20, l, "StoreEntry->expires: " << expires);
- debugs(20, l, "StoreEntry->lastmod: " << lastmod);
+ debugs(20, l, "StoreEntry->lastModified_: " << lastModified_);
debugs(20, l, "StoreEntry->swap_file_sz: " << swap_file_sz);
debugs(20, l, "StoreEntry->refcount: " << refcount);
debugs(20, l, "StoreEntry->flags: " << storeEntryFlags(this));
}
HttpReply const *
-StoreEntry::getReply () const
+StoreEntry::getReply() const
{
- if (NULL == mem_obj)
- return NULL;
-
- return mem_obj->getReply();
+ return (mem_obj ? mem_obj->getReply().getRaw() : nullptr);
}
void
StoreEntry::reset()
{
assert (mem_obj);
- debugs(20, 3, "StoreEntry::reset: " << url());
+ debugs(20, 3, url());
mem_obj->reset();
- HttpReply *rep = (HttpReply *) getReply(); // bypass const
- rep->reset();
- expires = lastmod = timestamp = -1;
+ expires = lastModified_ = timestamp = -1;
}
/*
return;
}
- mem_obj->replaceHttpReply(rep);
+ mem_obj->replaceReply(HttpReplyPointer(rep));
if (andStartWriting)
startWriting();
}
bool
-StoreEntry::modifiedSince(HttpRequest * request) const
+StoreEntry::modifiedSince(const time_t ims, const int imslen) const
{
int object_length;
- time_t mod_time = lastmod;
-
- if (mod_time < 0)
- mod_time = timestamp;
+ const time_t mod_time = lastModified();
debugs(88, 3, "modifiedSince: '" << url() << "'");
if (object_length < 0)
object_length = contentLen();
- if (mod_time > request->ims) {
+ if (mod_time > ims) {
debugs(88, 3, "--> YES: entry newer than client");
return true;
- } else if (mod_time < request->ims) {
+ } else if (mod_time < ims) {
debugs(88, 3, "--> NO: entry older than client");
return false;
- } else if (request->imslen < 0) {
+ } else if (imslen < 0) {
debugs(88, 3, "--> NO: same LMT, no client length");
return false;
- } else if (request->imslen == object_length) {
+ } else if (imslen == object_length) {
debugs(88, 3, "--> NO: same LMT, same length");
return false;
} else {
return true;
}
+const char *
+StoreEntry::describeTimestamps() const
+{
+ LOCAL_ARRAY(char, buf, 256);
+ snprintf(buf, 256, "LV:%-9d LU:%-9d LM:%-9d EX:%-9d",
+ static_cast<int>(timestamp),
+ static_cast<int>(lastref),
+ static_cast<int>(lastModified_),
+ static_cast<int>(expires));
+ return buf;
+}
+
std::ostream &operator <<(std::ostream &os, const StoreEntry &e)
{
os << "e:";
// print only set flags, using unique letters
if (e.flags) {
if (EBIT_TEST(e.flags, ENTRY_SPECIAL)) os << 'S';
- if (EBIT_TEST(e.flags, ENTRY_REVALIDATE)) os << 'R';
+ if (EBIT_TEST(e.flags, ENTRY_REVALIDATE_ALWAYS)) os << 'R';
if (EBIT_TEST(e.flags, DELAY_SENDING)) os << 'P';
if (EBIT_TEST(e.flags, RELEASE_REQUEST)) os << 'X';
if (EBIT_TEST(e.flags, REFRESH_REQUEST)) os << 'F';
+ if (EBIT_TEST(e.flags, ENTRY_REVALIDATE_STALE)) os << 'E';
if (EBIT_TEST(e.flags, ENTRY_DISPATCHED)) os << 'D';
if (EBIT_TEST(e.flags, KEY_PRIVATE)) os << 'I';
if (EBIT_TEST(e.flags, ENTRY_FWD_HDR_WAIT)) os << 'W';