From f0baf1499cf6d52ed011177d95360b39c5a844bd Mon Sep 17 00:00:00 2001 From: Alex Rousskov Date: Sat, 8 Mar 2014 10:28:23 -0700 Subject: [PATCH] Avoid assertions on Range requests that trigger Squid-generated errors. Added HttpRequest::ignoreRange() to encapsulate range ignoring logic. Currently the new method only contains the code common among all callers. More work is needed to check whether further caller homogenization is possible. Documented that ClientSocketContext::getNextRangeOffset() may sometimes be called before it is ready to do its job. --- src/HttpRequest.cc | 14 ++++++++++++++ src/HttpRequest.h | 2 ++ src/client_side.cc | 17 +++++++++++------ src/client_side_reply.cc | 9 ++++++++- src/client_side_reply.h | 2 +- src/client_side_request.cc | 5 ++--- src/http.cc | 3 +-- 7 files changed, 39 insertions(+), 13 deletions(-) diff --git a/src/HttpRequest.cc b/src/HttpRequest.cc index d6dfd7410a..764e71f909 100644 --- a/src/HttpRequest.cc +++ b/src/HttpRequest.cc @@ -666,6 +666,20 @@ HttpRequest::getRangeOffsetLimit() return rangeOffsetLimit; } +void +HttpRequest::ignoreRange(const char *reason) +{ + if (range) { + debugs(73, 3, static_cast(range) << " for " << reason); + delete range; + range = NULL; + } + // Some callers also reset isRanged but it may not be safe for all callers: + // isRanged is used to determine whether a weak ETag comparison is allowed, + // and that check should not ignore the Range header if it was present. + // TODO: Some callers also delete HDR_RANGE, HDR_REQUEST_RANGE. Should we? +} + bool HttpRequest::canHandle1xx() const { diff --git a/src/HttpRequest.h b/src/HttpRequest.h index b1301fa706..65dbf5efd1 100644 --- a/src/HttpRequest.h +++ b/src/HttpRequest.h @@ -262,6 +262,8 @@ public: */ CbcPointer clientConnectionManager; + /// forgets about the cached Range header (for a reason) + void ignoreRange(const char *reason); int64_t getRangeOffsetLimit(); /* the result of this function gets cached in rangeOffsetLimit */ private: diff --git a/src/client_side.cc b/src/client_side.cc index 73efdba17f..a0f95eed00 100644 --- a/src/client_side.cc +++ b/src/client_side.cc @@ -1343,9 +1343,7 @@ ClientSocketContext::buildRangeHeader(HttpReply * rep) * offset data, but we won't be requesting it. * So, we can either re-request, or generate an error */ - debugs(33, 3, "clientBuildRangeHeader: will not do ranges: " << range_err << "."); - delete http->request->range; - http->request->range = NULL; + http->request->ignoreRange(range_err); } else { /* XXX: TODO: Review, this unconditional set may be wrong. */ rep->sline.set(rep->sline.version, Http::scPartialContent); @@ -1724,9 +1722,16 @@ ClientSocketContext::canPackMoreRanges() const int64_t ClientSocketContext::getNextRangeOffset() const { + debugs (33, 5, "range: " << http->request->range << + "; http offset " << http->out.offset << + "; reply " << reply); + + // XXX: This method is called from many places, including pullData() which + // may be called before prepareReply() [on some Squid-generated errors]. + // Hence, we may not even know yet whether we should honor/do ranges. + if (http->request->range) { /* offset in range specs does not count the prefix of an http msg */ - debugs (33, 5, "ClientSocketContext::getNextRangeOffset: http offset " << http->out.offset); /* check: reply was parsed and range iterator was initialized */ assert(http->range_iter.valid); /* filter out data according to range specs */ @@ -1763,7 +1768,7 @@ ClientSocketContext::getNextRangeOffset() const void ClientSocketContext::pullData() { - debugs(33, 5, HERE << clientConnection << " attempting to pull upstream data"); + debugs(33, 5, reply << " written " << http->out.size << " into " << clientConnection); /* More data will be coming from the stream. */ StoreIOBuffer readBuffer; @@ -2545,7 +2550,7 @@ bool ConnStateData::serveDelayedError(ClientSocketContext *context) clientReplyContext *repContext = dynamic_cast(node->data.getRaw()); assert(repContext); debugs(33, 5, "Responding with delated error for " << http->uri); - repContext->setReplyToStoreEntry(sslServerBump->entry); + repContext->setReplyToStoreEntry(sslServerBump->entry, "delayed SslBump error"); // save the original request for logging purposes if (!context->http->al->request) { diff --git a/src/client_side_reply.cc b/src/client_side_reply.cc index 5c0d9fa92a..a0ab9d9fd5 100644 --- a/src/client_side_reply.cc +++ b/src/client_side_reply.cc @@ -132,13 +132,18 @@ void clientReplyContext::setReplyToError(const HttpRequestMethod& method, ErrorS http->al->http.code = errstate->httpStatus; + if (http->request) + http->request->ignoreRange("responding with a Squid-generated error"); + createStoreEntry(method, RequestFlags()); assert(errstate->callback_data == NULL); errorAppendEntry(http->storeEntry(), errstate); /* Now the caller reads to get this */ } -void clientReplyContext::setReplyToStoreEntry(StoreEntry *entry) +// Assumes that the entry contains an error response without Content-Range. +// To use with regular entries, make HTTP Range header removal conditional. +void clientReplyContext::setReplyToStoreEntry(StoreEntry *entry, const char *reason) { entry->lock("clientReplyContext::setReplyToStoreEntry"); // removeClientStoreReference() unlocks sc = storeClientListAdd(entry, this); @@ -147,6 +152,8 @@ void clientReplyContext::setReplyToStoreEntry(StoreEntry *entry) #endif reqofs = 0; reqsize = 0; + if (http->request) + http->request->ignoreRange(reason); flags.storelogiccomplete = 1; http->storeEntry(entry); } diff --git a/src/client_side_reply.h b/src/client_side_reply.h index 29f72ac71a..9dc9f3a12e 100644 --- a/src/client_side_reply.h +++ b/src/client_side_reply.h @@ -68,7 +68,7 @@ public: int storeOKTransferDone() const; int storeNotOKTransferDone() const; /// replaces current response store entry with the given one - void setReplyToStoreEntry(StoreEntry *e); + void setReplyToStoreEntry(StoreEntry *e, const char *reason); /// builds error using clientBuildError() and calls setReplyToError() below void setReplyToError(err_type, Http::StatusCode, const HttpRequestMethod&, char const *, Ip::Address &, HttpRequest *, const char *, #if USE_AUTH diff --git a/src/client_side_request.cc b/src/client_side_request.cc index a81aa288c3..06c8685047 100644 --- a/src/client_side_request.cc +++ b/src/client_side_request.cc @@ -1151,8 +1151,7 @@ clientInterpretRequestHeaders(ClientHttpRequest * http) else { req_hdr->delById(HDR_RANGE); req_hdr->delById(HDR_REQUEST_RANGE); - delete request->range; - request->range = NULL; + request->ignoreRange("neither HEAD nor GET"); } if (req_hdr->has(HDR_AUTHORIZATION)) @@ -1819,7 +1818,7 @@ ClientHttpRequest::doCallouts() clientStreamNode *node = (clientStreamNode *)client_stream.tail->prev->data; clientReplyContext *repContext = dynamic_cast(node->data.getRaw()); assert (repContext); - repContext->setReplyToStoreEntry(e); + repContext->setReplyToStoreEntry(e, "immediate SslBump error"); errorAppendEntry(e, calloutContext->error); calloutContext->error = NULL; if (calloutContext->readNextRequest) diff --git a/src/http.cc b/src/http.cc index e7f853106b..95794cc082 100644 --- a/src/http.cc +++ b/src/http.cc @@ -1734,8 +1734,7 @@ HttpStateData::httpBuildRequestHeader(HttpRequest * request, /* don't cache the result */ request->flags.cachable = false; /* pretend it's not a range request */ - delete request->range; - request->range = NULL; + request->ignoreRange("want to request the whole object"); request->flags.isRanged = false; } -- 2.47.2