2 * Copyright (C) 1996-2019 The Squid Software Foundation and contributors
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
9 /* DEBUG: section 88 Client-side Reply Routines */
12 #include "acl/FilledChecklist.h"
13 #include "acl/Gadgets.h"
14 #include "anyp/PortCfg.h"
15 #include "client_side_reply.h"
16 #include "errorpage.h"
20 #include "format/Token.h"
23 #include "http/Stream.h"
24 #include "HttpHeaderTools.h"
25 #include "HttpReply.h"
26 #include "HttpRequest.h"
27 #include "ip/QosConfig.h"
29 #include "log/access_log.h"
30 #include "MemObject.h"
31 #include "mime_header.h"
32 #include "neighbors.h"
34 #include "RequestFlags.h"
35 #include "SquidConfig.h"
36 #include "SquidTime.h"
41 #include "auth/UserRequest.h"
44 #include "DelayPools.h"
52 CBDATA_CLASS_INIT(clientReplyContext
);
55 extern "C" CSS clientReplyStatus
;
56 ErrorState
*clientBuildError(err_type
, Http::StatusCode
, char const *, Ip::Address
&, HttpRequest
*, const AccessLogEntry::Pointer
&);
60 clientReplyContext::~clientReplyContext()
63 /* This may trigger a callback back into SendMoreData as the cbdata
66 removeClientStoreReference(&sc
, http
);
67 /* old_entry might still be set if we didn't yet get the reply
68 * code in HandleIMSReply() */
69 removeStoreReference(&old_sc
, &old_entry
);
70 safe_free(tempBuffer
.data
);
71 cbdataReferenceDone(http
);
75 clientReplyContext::clientReplyContext(ClientHttpRequest
*clientContext
) :
76 purgeStatus(Http::scNone
),
78 http(cbdataReference(clientContext
)),
93 collapsedRevalidation(crNone
)
98 /** Create an error in the store awaiting the client side to read it.
100 * This may be better placed in the clientStream logic, but it has not been
101 * relocated there yet
104 clientReplyContext::setReplyToError(
105 err_type err
, Http::StatusCode status
, const HttpRequestMethod
& method
, char const *uri
,
106 Ip::Address
&addr
, HttpRequest
* failedrequest
, const char *unparsedrequest
,
108 Auth::UserRequest::Pointer auth_user_request
114 auto errstate
= clientBuildError(err
, status
, uri
, addr
, failedrequest
, http
->al
);
117 errstate
->request_hdrs
= xstrdup(unparsedrequest
);
120 errstate
->auth_user_request
= auth_user_request
;
122 setReplyToError(method
, errstate
);
125 void clientReplyContext::setReplyToError(const HttpRequestMethod
& method
, ErrorState
*errstate
)
127 if (errstate
->httpStatus
== Http::scNotImplemented
&& http
->request
)
128 /* prevent confusion over whether we default to persistent or not */
129 http
->request
->flags
.proxyKeepalive
= false;
131 http
->al
->http
.code
= errstate
->httpStatus
;
134 http
->request
->ignoreRange("responding with a Squid-generated error");
136 createStoreEntry(method
, RequestFlags());
137 assert(errstate
->callback_data
== NULL
);
138 errorAppendEntry(http
->storeEntry(), errstate
);
139 /* Now the caller reads to get this */
143 clientReplyContext::setReplyToReply(HttpReply
*futureReply
)
146 http
->al
->http
.code
= futureReply
->sline
.status();
148 HttpRequestMethod method
;
149 if (http
->request
) { // nil on responses to unparsable requests
150 http
->request
->ignoreRange("responding with a Squid-generated reply");
151 method
= http
->request
->method
;
154 createStoreEntry(method
, RequestFlags());
156 http
->storeEntry()->storeErrorResponse(futureReply
);
157 /* Now the caller reads to get futureReply */
160 // Assumes that the entry contains an error response without Content-Range.
161 // To use with regular entries, make HTTP Range header removal conditional.
162 void clientReplyContext::setReplyToStoreEntry(StoreEntry
*entry
, const char *reason
)
164 entry
->lock("clientReplyContext::setReplyToStoreEntry"); // removeClientStoreReference() unlocks
165 sc
= storeClientListAdd(entry
, this);
167 sc
->setDelayId(DelayId::DelayClient(http
));
172 http
->request
->ignoreRange(reason
);
173 flags
.storelogiccomplete
= 1;
174 http
->storeEntry(entry
);
178 clientReplyContext::removeStoreReference(store_client
** scp
,
182 store_client
*sc_tmp
= *scp
;
184 if ((e
= *ep
) != NULL
) {
186 storeUnregister(sc_tmp
, e
, this);
188 e
->unlock("clientReplyContext::removeStoreReference");
193 clientReplyContext::removeClientStoreReference(store_client
**scp
, ClientHttpRequest
*aHttpRequest
)
195 StoreEntry
*reference
= aHttpRequest
->storeEntry();
196 removeStoreReference(scp
, &reference
);
197 aHttpRequest
->storeEntry(reference
);
201 clientReplyContext::saveState()
203 assert(old_sc
== NULL
);
204 debugs(88, 3, "clientReplyContext::saveState: saving store context");
205 old_entry
= http
->storeEntry();
207 old_lastmod
= http
->request
->lastmod
;
208 old_etag
= http
->request
->etag
;
209 old_reqsize
= reqsize
;
210 tempBuffer
.offset
= reqofs
;
211 /* Prevent accessing the now saved entries */
212 http
->storeEntry(NULL
);
219 clientReplyContext::restoreState()
221 assert(old_sc
!= NULL
);
222 debugs(88, 3, "clientReplyContext::restoreState: Restoring store context");
223 removeClientStoreReference(&sc
, http
);
224 http
->storeEntry(old_entry
);
226 reqsize
= old_reqsize
;
227 reqofs
= tempBuffer
.offset
;
228 http
->request
->lastmod
= old_lastmod
;
229 http
->request
->etag
= old_etag
;
230 /* Prevent accessed the old saved entries */
236 tempBuffer
.offset
= 0;
240 clientReplyContext::startError(ErrorState
* err
)
242 createStoreEntry(http
->request
->method
, RequestFlags());
243 triggerInitialStoreRead();
244 errorAppendEntry(http
->storeEntry(), err
);
248 clientReplyContext::getNextNode() const
250 return (clientStreamNode
*)ourNode
->node
.next
->data
;
253 /* This function is wrong - the client parameters don't include the
257 clientReplyContext::triggerInitialStoreRead()
259 /* when confident, 0 becomes reqofs, and then this factors into
263 StoreIOBuffer
localTempBuffer (next()->readBuffer
.length
, 0, next()->readBuffer
.data
);
264 storeClientCopy(sc
, http
->storeEntry(), localTempBuffer
, SendMoreData
, this);
267 /* there is an expired entry in the store.
268 * setup a temporary buffer area and perform an IMS to the origin
271 clientReplyContext::processExpired()
273 const char *url
= storeId();
274 debugs(88, 3, "clientReplyContext::processExpired: '" << http
->uri
<< "'");
275 const time_t lastmod
= http
->storeEntry()->lastModified();
276 assert(lastmod
>= 0);
278 * check if we are allowed to contact other servers
279 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
280 * a stale entry *if* it matches client requirements
283 if (http
->onlyIfCached()) {
284 processOnlyIfCachedMiss();
288 http
->logType
.update(LOG_TCP_REFRESH
);
289 http
->request
->flags
.refresh
= true;
290 #if STORE_CLIENT_LIST_DEBUG
291 /* Prevent a race with the store client memory free routines
293 assert(storeClientIsThisAClient(sc
, this));
295 /* Prepare to make a new temporary request */
298 // TODO: Consider also allowing regular (non-collapsed) revalidation hits.
299 // TODO: support collapsed revalidation for Vary-controlled entries
300 bool collapsingAllowed
= Config
.onoff
.collapsed_forwarding
&&
301 !Store::Controller::SmpAware() &&
302 http
->request
->vary_headers
.isEmpty();
304 StoreEntry
*entry
= nullptr;
305 if (collapsingAllowed
) {
306 if (const auto e
= storeGetPublicByRequest(http
->request
, ksRevalidation
)) {
307 if (e
->hittingRequiresCollapsing() && startCollapsingOn(*e
, true)) {
309 entry
->lock("clientReplyContext::processExpired#alreadyRevalidating");
311 e
->abandon(__FUNCTION__
);
312 // assume mayInitiateCollapsing() would fail too
313 collapsingAllowed
= false;
319 entry
->ensureMemObject(url
, http
->log_uri
, http
->request
->method
);
320 debugs(88, 5, "collapsed on existing revalidation entry: " << *entry
);
321 collapsedRevalidation
= crSlave
;
323 entry
= storeCreateEntry(url
,
324 http
->log_uri
, http
->request
->flags
, http
->request
->method
);
325 /* NOTE, don't call StoreEntry->lock(), storeCreateEntry() does it */
327 if (collapsingAllowed
&& mayInitiateCollapsing() &&
328 Store::Root().allowCollapsing(entry
, http
->request
->flags
, http
->request
->method
)) {
329 debugs(88, 5, "allow other revalidation requests to collapse on " << *entry
);
330 collapsedRevalidation
= crInitiator
;
332 collapsedRevalidation
= crNone
;
336 sc
= storeClientListAdd(entry
, this);
338 /* delay_id is already set on original store client */
339 sc
->setDelayId(DelayId::DelayClient(http
));
342 http
->request
->lastmod
= lastmod
;
344 if (!http
->request
->header
.has(Http::HdrType::IF_NONE_MATCH
)) {
345 ETag etag
= {NULL
, -1}; // TODO: make that a default ETag constructor
346 if (old_entry
->hasEtag(etag
) && !etag
.weak
)
347 http
->request
->etag
= etag
.str
;
350 debugs(88, 5, "lastmod " << entry
->lastModified());
351 http
->storeEntry(entry
);
352 assert(http
->out
.offset
== 0);
353 assert(http
->request
->clientConnectionManager
== http
->getConn());
355 if (collapsedRevalidation
!= crSlave
) {
357 * A refcounted pointer so that FwdState stays around as long as
358 * this clientReplyContext does
360 Comm::ConnectionPointer conn
= http
->getConn() != NULL
? http
->getConn()->clientConnection
: NULL
;
361 FwdState::Start(conn
, http
->storeEntry(), http
->request
, http
->al
);
363 /* Register with storage manager to receive updates when data comes in. */
365 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
))
366 debugs(88, DBG_CRITICAL
, "clientReplyContext::processExpired: Found ENTRY_ABORTED object");
369 /* start counting the length from 0 */
370 StoreIOBuffer
localTempBuffer(HTTP_REQBUF_SZ
, 0, tempbuf
);
371 storeClientCopy(sc
, entry
, localTempBuffer
, HandleIMSReply
, this);
376 clientReplyContext::sendClientUpstreamResponse()
378 StoreIOBuffer tempresult
;
379 removeStoreReference(&old_sc
, &old_entry
);
380 /* here the data to send is the data we just received */
381 tempBuffer
.offset
= 0;
383 /* sendMoreData tracks the offset as well.
384 * Force it back to zero */
386 assert(!EBIT_TEST(http
->storeEntry()->flags
, ENTRY_ABORTED
));
387 /* TODO: provide sendMoreData with the ready parsed reply */
388 tempresult
.length
= reqsize
;
389 tempresult
.data
= tempbuf
;
390 sendMoreData(tempresult
);
394 clientReplyContext::HandleIMSReply(void *data
, StoreIOBuffer result
)
396 clientReplyContext
*context
= (clientReplyContext
*)data
;
397 context
->handleIMSReply(result
);
401 clientReplyContext::sendClientOldEntry()
403 /* Get the old request back */
405 /* here the data to send is in the next nodes buffers already */
406 assert(!EBIT_TEST(http
->storeEntry()->flags
, ENTRY_ABORTED
));
407 /* sendMoreData tracks the offset as well.
408 * Force it back to zero */
410 StoreIOBuffer
tempresult (reqsize
, reqofs
, next()->readBuffer
.data
);
411 sendMoreData(tempresult
);
414 /* This is the workhorse of the HandleIMSReply callback.
416 * It is called when we've got data back from the origin following our
417 * IMS request to revalidate a stale entry.
420 clientReplyContext::handleIMSReply(StoreIOBuffer result
)
425 debugs(88, 3, http
->storeEntry()->url() << ", " << (long unsigned) result
.length
<< " bytes");
427 if (http
->storeEntry() == NULL
)
430 if (result
.flags
.error
&& !EBIT_TEST(http
->storeEntry()->flags
, ENTRY_ABORTED
))
433 if (collapsedRevalidation
== crSlave
&& !http
->storeEntry()->mayStartHitting()) {
434 debugs(88, 3, "CF slave hit private non-shareable " << *http
->storeEntry() << ". MISS");
435 // restore context to meet processMiss() expectations
437 http
->logType
.update(LOG_TCP_MISS
);
442 /* update size of the request */
443 reqsize
= result
.length
+ reqofs
;
445 const Http::StatusCode status
= http
->storeEntry()->getReply()->sline
.status();
447 // request to origin was aborted
448 if (EBIT_TEST(http
->storeEntry()->flags
, ENTRY_ABORTED
)) {
449 debugs(88, 3, "request to origin aborted '" << http
->storeEntry()->url() << "', sending old entry to client");
450 http
->logType
.update(LOG_TCP_REFRESH_FAIL_OLD
);
451 sendClientOldEntry();
454 const HttpReply
*old_rep
= old_entry
->getReply();
456 // origin replied 304
457 if (status
== Http::scNotModified
) {
458 http
->logType
.update(LOG_TCP_REFRESH_UNMODIFIED
);
459 http
->request
->flags
.staleIfHit
= false; // old_entry is no longer stale
461 // update headers on existing entry
462 Store::Root().updateOnNotModified(old_entry
, *http
->storeEntry());
464 // if client sent IMS
466 if (http
->request
->flags
.ims
&& !old_entry
->modifiedSince(http
->request
->ims
, http
->request
->imslen
)) {
467 // forward the 304 from origin
468 debugs(88, 3, "origin replied 304, revalidating existing entry and forwarding 304 to client");
469 sendClientUpstreamResponse();
471 // send existing entry, it's still valid
472 debugs(88, 3, "origin replied 304, revalidating existing entry and sending " <<
473 old_rep
->sline
.status() << " to client");
474 sendClientOldEntry();
478 // origin replied with a non-error code
479 else if (status
> Http::scNone
&& status
< Http::scInternalServerError
) {
480 const HttpReply
*new_rep
= http
->storeEntry()->getReply();
481 // RFC 7234 section 4: a cache MUST use the most recent response
482 // (as determined by the Date header field)
483 if (new_rep
->olderThan(old_rep
)) {
484 http
->logType
.err
.ignored
= true;
485 debugs(88, 3, "origin replied " << status
<<
486 " but with an older date header, sending old entry (" <<
487 old_rep
->sline
.status() << ") to client");
488 sendClientOldEntry();
490 http
->logType
.update(LOG_TCP_REFRESH_MODIFIED
);
491 debugs(88, 3, "origin replied " << status
<<
492 ", replacing existing entry and forwarding to client");
494 if (collapsedRevalidation
)
495 http
->storeEntry()->clearPublicKeyScope();
497 sendClientUpstreamResponse();
501 // origin replied with an error
502 else if (http
->request
->flags
.failOnValidationError
) {
503 http
->logType
.update(LOG_TCP_REFRESH_FAIL_ERR
);
504 debugs(88, 3, "origin replied with error " << status
<<
505 ", forwarding to client due to fail_on_validation_err");
506 sendClientUpstreamResponse();
508 // ignore and let client have old entry
509 http
->logType
.update(LOG_TCP_REFRESH_FAIL_OLD
);
510 debugs(88, 3, "origin replied with error " <<
511 status
<< ", sending old entry (" << old_rep
->sline
.status() << ") to client");
512 sendClientOldEntry();
516 SQUIDCEXTERN CSR clientGetMoreData
;
517 SQUIDCEXTERN CSD clientReplyDetach
;
520 * clientReplyContext::cacheHit Should only be called until the HTTP reply headers
521 * have been parsed. Normally this should be a single call, but
522 * it might take more than one. As soon as we have the headers,
523 * we hand off to clientSendMoreData, processExpired, or
527 clientReplyContext::CacheHit(void *data
, StoreIOBuffer result
)
529 clientReplyContext
*context
= (clientReplyContext
*)data
;
530 context
->cacheHit(result
);
534 * Process a possible cache HIT.
537 clientReplyContext::cacheHit(StoreIOBuffer result
)
539 /** Ignore if the HIT object is being deleted. */
541 debugs(88, 3, "HIT object being deleted. Ignore the HIT.");
545 StoreEntry
*e
= http
->storeEntry();
547 HttpRequest
*r
= http
->request
;
549 debugs(88, 3, "clientCacheHit: " << http
->uri
<< ", " << result
.length
<< " bytes");
551 if (http
->storeEntry() == NULL
) {
552 debugs(88, 3, "clientCacheHit: request aborted");
554 } else if (result
.flags
.error
) {
555 /* swap in failure */
556 debugs(88, 3, "clientCacheHit: swapin failure for " << http
->uri
);
557 http
->logType
.update(LOG_TCP_SWAPFAIL_MISS
);
558 removeClientStoreReference(&sc
, http
);
563 // The previously identified hit suddenly became unsharable!
564 // This is common for collapsed forwarding slaves but might also
565 // happen to regular hits because we are called asynchronously.
566 if (!e
->mayStartHitting()) {
567 debugs(88, 3, "unsharable " << *e
<< ". MISS");
568 http
->logType
.update(LOG_TCP_MISS
);
573 if (result
.length
== 0) {
574 debugs(88, 5, "store IO buffer has no content. MISS");
575 /* the store couldn't get enough data from the file for us to id the
578 /* treat as a miss */
579 http
->logType
.update(LOG_TCP_MISS
);
584 assert(!EBIT_TEST(e
->flags
, ENTRY_ABORTED
));
585 /* update size of the request */
586 reqsize
= result
.length
+ reqofs
;
589 * Got the headers, now grok them
591 assert(http
->logType
.oldType
== LOG_TCP_HIT
);
593 if (http
->request
->storeId().cmp(e
->mem_obj
->storeId()) != 0) {
594 debugs(33, DBG_IMPORTANT
, "clientProcessHit: URL mismatch, '" << e
->mem_obj
->storeId() << "' != '" << http
->request
->storeId() << "'");
595 http
->logType
.update(LOG_TCP_MISS
); // we lack a more precise LOG_*_MISS code
600 switch (varyEvaluateMatch(e
, r
)) {
603 /* No variance detected. Continue as normal */
607 /* This is the correct entity for this request. Continue */
608 debugs(88, 2, "clientProcessHit: Vary MATCH!");
612 /* This is not the correct entity for this request. We need
613 * to requery the cache.
615 removeClientStoreReference(&sc
, http
);
617 /* Note: varyEvalyateMatch updates the request with vary information
618 * so we only get here once. (it also takes care of cancelling loops)
620 debugs(88, 2, "clientProcessHit: Vary detected!");
621 clientGetMoreData(ourNode
, http
);
625 /* varyEvaluateMatch found a object loop. Process as miss */
626 debugs(88, DBG_IMPORTANT
, "clientProcessHit: Vary object loop!");
627 http
->logType
.update(LOG_TCP_MISS
); // we lack a more precise LOG_*_MISS code
632 if (r
->method
== Http::METHOD_PURGE
) {
633 debugs(88, 5, "PURGE gets a HIT");
634 removeClientStoreReference(&sc
, http
);
640 if (e
->checkNegativeHit() && !r
->flags
.noCacheHack()) {
641 debugs(88, 5, "negative-HIT");
642 http
->logType
.update(LOG_TCP_NEGATIVE_HIT
);
643 sendMoreData(result
);
645 } else if (blockedHit()) {
646 debugs(88, 5, "send_hit forces a MISS");
647 http
->logType
.update(LOG_TCP_MISS
);
650 } else if (!http
->flags
.internal
&& refreshCheckHTTP(e
, r
)) {
651 debugs(88, 5, "clientCacheHit: in refreshCheck() block");
653 * We hold a stale copy; it needs to be validated
656 * The 'needValidation' flag is used to prevent forwarding
657 * loops between siblings. If our copy of the object is stale,
658 * then we should probably only use parents for the validation
659 * request. Otherwise two siblings could generate a loop if
660 * both have a stale version of the object.
662 r
->flags
.needValidation
= true;
664 if (e
->lastModified() < 0) {
665 debugs(88, 3, "validate HIT object? NO. Can't calculate entry modification time. Do MISS.");
667 * We cannot revalidate entries without knowing their
669 * XXX: BUG 1890 objects without Date do not get one added.
671 http
->logType
.update(LOG_TCP_MISS
);
673 } else if (r
->flags
.noCache
) {
674 debugs(88, 3, "validate HIT object? NO. Client sent CC:no-cache. Do CLIENT_REFRESH_MISS");
676 * This did not match a refresh pattern that overrides no-cache
677 * we should honour the client no-cache header.
679 http
->logType
.update(LOG_TCP_CLIENT_REFRESH_MISS
);
681 } else if (r
->url
.getScheme() == AnyP::PROTO_HTTP
|| r
->url
.getScheme() == AnyP::PROTO_HTTPS
) {
682 debugs(88, 3, "validate HIT object? YES.");
684 * Object needs to be revalidated
685 * XXX This could apply to FTP as well, if Last-Modified is known.
689 debugs(88, 3, "validate HIT object? NO. Client protocol non-HTTP. Do MISS.");
691 * We don't know how to re-validate other protocols. Handle
692 * them as if the object has expired.
694 http
->logType
.update(LOG_TCP_MISS
);
698 } else if (r
->conditional()) {
699 debugs(88, 5, "conditional HIT");
700 if (processConditional(result
))
705 * plain ol' cache hit
707 debugs(88, 5, "plain old HIT");
710 if (e
->store_status
!= STORE_OK
)
711 http
->logType
.update(LOG_TCP_MISS
);
714 if (e
->mem_status
== IN_MEMORY
)
715 http
->logType
.update(LOG_TCP_MEM_HIT
);
716 else if (Config
.onoff
.offline
)
717 http
->logType
.update(LOG_TCP_OFFLINE_HIT
);
719 sendMoreData(result
);
723 * Prepare to fetch the object as it's a cache miss of some kind.
726 clientReplyContext::processMiss()
728 char *url
= http
->uri
;
729 HttpRequest
*r
= http
->request
;
730 ErrorState
*err
= NULL
;
731 debugs(88, 4, r
->method
<< ' ' << url
);
734 * We might have a left-over StoreEntry from a failed cache hit
737 if (http
->storeEntry()) {
738 if (EBIT_TEST(http
->storeEntry()->flags
, ENTRY_SPECIAL
)) {
739 debugs(88, DBG_CRITICAL
, "clientProcessMiss: miss on a special object (" << url
<< ").");
740 debugs(88, DBG_CRITICAL
, "\tlog_type = " << http
->logType
.c_str());
741 http
->storeEntry()->dump(1);
744 removeClientStoreReference(&sc
, http
);
747 /** Check if its a PURGE request to be actioned. */
748 if (r
->method
== Http::METHOD_PURGE
) {
753 /** Check if its an 'OTHER' request. Purge all cached entries if so and continue. */
754 if (r
->method
== Http::METHOD_OTHER
) {
758 /** Check if 'only-if-cached' flag is set. Action if so. */
759 if (http
->onlyIfCached()) {
760 processOnlyIfCachedMiss();
764 Comm::ConnectionPointer conn
= http
->getConn() != nullptr ? http
->getConn()->clientConnection
: nullptr;
766 if (r
->flags
.loopDetected
) {
767 http
->al
->http
.code
= Http::scForbidden
;
768 Ip::Address tmp_noaddr
;
769 tmp_noaddr
.setNoAddr();
770 err
= clientBuildError(ERR_ACCESS_DENIED
, Http::scForbidden
, nullptr, conn
? conn
->remote
: tmp_noaddr
, http
->request
, http
->al
);
771 createStoreEntry(r
->method
, RequestFlags());
772 errorAppendEntry(http
->storeEntry(), err
);
773 triggerInitialStoreRead();
776 assert(http
->out
.offset
== 0);
777 createStoreEntry(r
->method
, r
->flags
);
778 triggerInitialStoreRead();
780 if (http
->redirect
.status
) {
781 HttpReply
*rep
= new HttpReply
;
782 http
->logType
.update(LOG_TCP_REDIRECT
);
783 http
->storeEntry()->releaseRequest();
784 rep
->redirect(http
->redirect
.status
, http
->redirect
.location
);
785 http
->storeEntry()->replaceHttpReply(rep
);
786 http
->storeEntry()->complete();
790 assert(r
->clientConnectionManager
== http
->getConn());
792 /** Start forwarding to get the new object from network */
793 FwdState::Start(conn
, http
->storeEntry(), r
, http
->al
);
798 * client issued a request with an only-if-cached cache-control directive;
799 * we did not find a cached object that can be returned without
800 * contacting other servers;
801 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
804 clientReplyContext::processOnlyIfCachedMiss()
806 debugs(88, 4, http
->request
->method
<< ' ' << http
->uri
);
807 http
->al
->http
.code
= Http::scGatewayTimeout
;
808 Ip::Address tmp_noaddr
;
809 tmp_noaddr
.setNoAddr();
810 ErrorState
*err
= clientBuildError(ERR_ONLY_IF_CACHED_MISS
, Http::scGatewayTimeout
, NULL
,
811 http
->getConn() ? http
->getConn()->clientConnection
->remote
: tmp_noaddr
,
812 http
->request
, http
->al
);
813 removeClientStoreReference(&sc
, http
);
817 /// process conditional request from client
819 clientReplyContext::processConditional(StoreIOBuffer
&result
)
821 StoreEntry
*const e
= http
->storeEntry();
823 if (e
->getReply()->sline
.status() != Http::scOkay
) {
824 debugs(88, 4, "Reply code " << e
->getReply()->sline
.status() << " != 200");
825 http
->logType
.update(LOG_TCP_MISS
);
830 HttpRequest
&r
= *http
->request
;
832 if (r
.header
.has(Http::HdrType::IF_MATCH
) && !e
->hasIfMatchEtag(r
)) {
833 // RFC 2616: reply with 412 Precondition Failed if If-Match did not match
834 sendPreconditionFailedError();
838 if (r
.header
.has(Http::HdrType::IF_NONE_MATCH
)) {
839 // RFC 7232: If-None-Match recipient MUST ignore IMS
843 r
.header
.delById(Http::HdrType::IF_MODIFIED_SINCE
);
845 if (e
->hasIfNoneMatchEtag(r
)) {
846 sendNotModifiedOrPreconditionFailedError();
850 // None-Match is true (no ETag matched); treat as an unconditional hit
855 // handle If-Modified-Since requests from the client
856 if (e
->modifiedSince(r
.ims
, r
.imslen
)) {
857 // Modified-Since is true; treat as an unconditional hit
861 // otherwise reply with 304 Not Modified
870 /// whether squid.conf send_hit prevents us from serving this hit
872 clientReplyContext::blockedHit() const
874 if (!Config
.accessList
.sendHit
)
875 return false; // hits are not blocked by default
877 if (http
->flags
.internal
)
878 return false; // internal content "hits" cannot be blocked
880 if (const HttpReply
*rep
= http
->storeEntry()->getReply()) {
881 std::unique_ptr
<ACLFilledChecklist
> chl(clientAclChecklistCreate(Config
.accessList
.sendHit
, http
));
882 chl
->reply
= const_cast<HttpReply
*>(rep
); // ACLChecklist API bug
883 HTTPMSGLOCK(chl
->reply
);
884 return !chl
->fastCheck().allowed(); // when in doubt, block
887 // This does not happen, I hope, because we are called from CacheHit, which
888 // is called via a storeClientCopy() callback, and store should initialize
889 // the reply before calling that callback.
890 debugs(88, 3, "Missing reply!");
895 clientReplyContext::purgeRequestFindObjectToPurge()
897 /* Try to find a base entry */
898 http
->flags
.purging
= true;
901 // TODO: can we use purgeAllCached() here instead of doing the
902 // getPublicByRequestMethod() dance?
903 StoreEntry::getPublicByRequestMethod(this, http
->request
, Http::METHOD_GET
);
906 // Purges all entries with a given url
907 // TODO: move to SideAgent parent, when we have one
909 * We probably cannot purge Vary-affected responses because their MD5
910 * keys depend on vary headers.
913 purgeEntriesByUrl(HttpRequest
* req
, const char *url
)
915 for (HttpRequestMethod
m(Http::METHOD_NONE
); m
!= Http::METHOD_ENUM_END
; ++m
) {
916 if (m
.respMaybeCacheable()) {
917 const cache_key
*key
= storeKeyPublic(url
, m
);
918 debugs(88, 5, m
<< ' ' << url
<< ' ' << storeKeyText(key
));
920 neighborsHtcpClear(nullptr, url
, req
, m
, HTCP_CLR_INVALIDATION
);
922 Store::Root().evictIfFound(key
);
928 clientReplyContext::purgeAllCached()
930 // XXX: performance regression, c_str() reallocates
931 SBuf
url(http
->request
->effectiveRequestUri());
932 purgeEntriesByUrl(http
->request
, url
.c_str());
936 clientReplyContext::created(StoreEntry
*newEntry
)
938 if (lookingforstore
== 1)
939 purgeFoundGet(newEntry
);
940 else if (lookingforstore
== 2)
941 purgeFoundHead(newEntry
);
942 else if (lookingforstore
== 3)
943 purgeDoPurgeGet(newEntry
);
944 else if (lookingforstore
== 4)
945 purgeDoPurgeHead(newEntry
);
946 else if (lookingforstore
== 5)
947 identifyFoundObject(newEntry
);
951 clientReplyContext::loggingTags()
953 // XXX: clientReplyContext code assumes that http cbdata is always valid.
954 // TODO: Either add cbdataReferenceValid(http) checks in all the relevant
955 // places, like this one, or remove cbdata protection of the http member.
956 return &http
->logType
;
960 clientReplyContext::purgeFoundGet(StoreEntry
*newEntry
)
964 StoreEntry::getPublicByRequestMethod(this, http
->request
, Http::METHOD_HEAD
);
966 purgeFoundObject (newEntry
);
970 clientReplyContext::purgeFoundHead(StoreEntry
*newEntry
)
975 purgeFoundObject (newEntry
);
979 clientReplyContext::purgeFoundObject(StoreEntry
*entry
)
983 if (EBIT_TEST(entry
->flags
, ENTRY_SPECIAL
)) {
984 http
->logType
.update(LOG_TCP_DENIED
);
985 Ip::Address tmp_noaddr
;
986 tmp_noaddr
.setNoAddr(); // TODO: make a global const
987 ErrorState
*err
= clientBuildError(ERR_ACCESS_DENIED
, Http::scForbidden
, NULL
,
988 http
->getConn() ? http
->getConn()->clientConnection
->remote
: tmp_noaddr
,
989 http
->request
, http
->al
);
991 return; // XXX: leaking unused entry if some store does not keep it
994 StoreIOBuffer localTempBuffer
;
995 /* Swap in the metadata */
996 http
->storeEntry(entry
);
998 http
->storeEntry()->lock("clientReplyContext::purgeFoundObject");
999 http
->storeEntry()->ensureMemObject(storeId(), http
->log_uri
,
1000 http
->request
->method
);
1002 sc
= storeClientListAdd(http
->storeEntry(), this);
1004 http
->logType
.update(LOG_TCP_HIT
);
1008 localTempBuffer
.offset
= http
->out
.offset
;
1010 localTempBuffer
.length
= next()->readBuffer
.length
;
1012 localTempBuffer
.data
= next()->readBuffer
.data
;
1014 storeClientCopy(sc
, http
->storeEntry(),
1015 localTempBuffer
, CacheHit
, this);
1019 clientReplyContext::purgeRequest()
1021 debugs(88, 3, "Config2.onoff.enable_purge = " <<
1022 Config2
.onoff
.enable_purge
);
1024 if (!Config2
.onoff
.enable_purge
) {
1025 http
->logType
.update(LOG_TCP_DENIED
);
1026 Ip::Address tmp_noaddr
;
1027 tmp_noaddr
.setNoAddr();
1028 ErrorState
*err
= clientBuildError(ERR_ACCESS_DENIED
, Http::scForbidden
, NULL
,
1029 http
->getConn() ? http
->getConn()->clientConnection
->remote
: tmp_noaddr
, http
->request
, http
->al
);
1034 /* Release both IP cache */
1035 ipcacheInvalidate(http
->request
->url
.host());
1037 if (!http
->flags
.purging
)
1038 purgeRequestFindObjectToPurge();
1044 clientReplyContext::purgeDoMissPurge()
1046 http
->logType
.update(LOG_TCP_MISS
);
1047 lookingforstore
= 3;
1048 StoreEntry::getPublicByRequestMethod(this,http
->request
, Http::METHOD_GET
);
1052 clientReplyContext::purgeDoPurgeGet(StoreEntry
*newEntry
)
1055 /* Move to new() when that is created */
1056 purgeStatus
= Http::scNotFound
;
1057 /* Release the cached URI */
1058 debugs(88, 4, "clientPurgeRequest: GET '" << newEntry
->url() << "'" );
1060 neighborsHtcpClear(newEntry
, NULL
, http
->request
, HttpRequestMethod(Http::METHOD_GET
), HTCP_CLR_PURGE
);
1062 newEntry
->release(true);
1063 purgeStatus
= Http::scOkay
;
1066 lookingforstore
= 4;
1067 StoreEntry::getPublicByRequestMethod(this, http
->request
, Http::METHOD_HEAD
);
1071 clientReplyContext::purgeDoPurgeHead(StoreEntry
*newEntry
)
1074 debugs(88, 4, "HEAD " << newEntry
->url());
1076 neighborsHtcpClear(newEntry
, NULL
, http
->request
, HttpRequestMethod(Http::METHOD_HEAD
), HTCP_CLR_PURGE
);
1078 newEntry
->release(true);
1079 purgeStatus
= Http::scOkay
;
1082 /* And for Vary, release the base URI if none of the headers was included in the request */
1083 if (!http
->request
->vary_headers
.isEmpty()
1084 && http
->request
->vary_headers
.find('=') != SBuf::npos
) {
1085 // XXX: performance regression, c_str() reallocates
1086 SBuf
tmp(http
->request
->effectiveRequestUri());
1087 StoreEntry
*entry
= storeGetPublic(tmp
.c_str(), Http::METHOD_GET
);
1090 debugs(88, 4, "Vary GET " << entry
->url());
1092 neighborsHtcpClear(entry
, NULL
, http
->request
, HttpRequestMethod(Http::METHOD_GET
), HTCP_CLR_PURGE
);
1094 entry
->release(true);
1095 purgeStatus
= Http::scOkay
;
1098 entry
= storeGetPublic(tmp
.c_str(), Http::METHOD_HEAD
);
1101 debugs(88, 4, "Vary HEAD " << entry
->url());
1103 neighborsHtcpClear(entry
, NULL
, http
->request
, HttpRequestMethod(Http::METHOD_HEAD
), HTCP_CLR_PURGE
);
1105 entry
->release(true);
1106 purgeStatus
= Http::scOkay
;
1111 * Make a new entry to hold the reply to be written
1114 /* FIXME: This doesn't need to go through the store. Simply
1115 * push down the client chain
1117 createStoreEntry(http
->request
->method
, RequestFlags());
1119 triggerInitialStoreRead();
1121 HttpReply
*rep
= new HttpReply
;
1122 rep
->setHeaders(purgeStatus
, NULL
, NULL
, 0, 0, -1);
1123 http
->storeEntry()->replaceHttpReply(rep
);
1124 http
->storeEntry()->complete();
1128 clientReplyContext::traceReply(clientStreamNode
* node
)
1130 clientStreamNode
*nextNode
= (clientStreamNode
*)node
->node
.next
->data
;
1131 StoreIOBuffer localTempBuffer
;
1132 createStoreEntry(http
->request
->method
, RequestFlags());
1133 localTempBuffer
.offset
= nextNode
->readBuffer
.offset
+ headers_sz
;
1134 localTempBuffer
.length
= nextNode
->readBuffer
.length
;
1135 localTempBuffer
.data
= nextNode
->readBuffer
.data
;
1136 storeClientCopy(sc
, http
->storeEntry(),
1137 localTempBuffer
, SendMoreData
, this);
1138 http
->storeEntry()->releaseRequest();
1139 http
->storeEntry()->buffer();
1140 HttpReply
*rep
= new HttpReply
;
1141 rep
->setHeaders(Http::scOkay
, NULL
, "text/plain", http
->request
->prefixLen(), 0, squid_curtime
);
1142 http
->storeEntry()->replaceHttpReply(rep
);
1143 http
->request
->swapOut(http
->storeEntry());
1144 http
->storeEntry()->complete();
1147 #define SENDING_BODY 0
1148 #define SENDING_HDRSONLY 1
1150 clientReplyContext::checkTransferDone()
1152 StoreEntry
*entry
= http
->storeEntry();
1158 * For now, 'done_copying' is used for special cases like
1159 * Range and HEAD requests.
1161 if (http
->flags
.done_copying
)
1164 if (http
->request
->flags
.chunkedReply
&& !flags
.complete
) {
1165 // last-chunk was not sent
1170 * Handle STORE_OK objects.
1171 * objectLen(entry) will be set proprely.
1172 * RC: Does objectLen(entry) include the Headers?
1175 if (entry
->store_status
== STORE_OK
) {
1176 return storeOKTransferDone();
1178 return storeNotOKTransferDone();
1183 clientReplyContext::storeOKTransferDone() const
1185 assert(http
->storeEntry()->objectLen() >= 0);
1186 assert(http
->storeEntry()->objectLen() >= headers_sz
);
1187 if (http
->out
.offset
>= http
->storeEntry()->objectLen() - headers_sz
) {
1188 debugs(88,3,HERE
<< "storeOKTransferDone " <<
1189 " out.offset=" << http
->out
.offset
<<
1190 " objectLen()=" << http
->storeEntry()->objectLen() <<
1191 " headers_sz=" << headers_sz
);
1199 clientReplyContext::storeNotOKTransferDone() const
1202 * Now, handle STORE_PENDING objects
1204 MemObject
*mem
= http
->storeEntry()->mem_obj
;
1205 assert(mem
!= NULL
);
1206 assert(http
->request
!= NULL
);
1208 /* mem->reply was wrong because it uses the UPSTREAM header length!!! */
1209 if (headers_sz
== 0)
1210 /* haven't found end of headers yet */
1213 const HttpReplyPointer
curReply(mem
->getReply());
1216 * Figure out how much data we are supposed to send.
1217 * If we are sending a body and we don't have a content-length,
1218 * then we must wait for the object to become STORE_OK.
1220 if (curReply
->content_length
< 0)
1223 uint64_t expectedLength
= curReply
->content_length
+ http
->out
.headers_sz
;
1225 if (http
->out
.size
< expectedLength
)
1228 debugs(88,3,HERE
<< "storeNotOKTransferDone " <<
1229 " out.size=" << http
->out
.size
<<
1230 " expectedLength=" << expectedLength
);
1235 /* A write has completed, what is the next status based on the
1236 * canonical request data?
1237 * 1 something is wrong
1238 * 0 nothing is wrong.
1242 clientHttpRequestStatus(int fd
, ClientHttpRequest
const *http
)
1244 #if SIZEOF_INT64_T == 4
1245 if (http
->out
.size
> 0x7FFF0000) {
1246 debugs(88, DBG_IMPORTANT
, "WARNING: closing FD " << fd
<< " to prevent out.size counter overflow");
1247 if (http
->getConn())
1248 debugs(88, DBG_IMPORTANT
, "\tclient " << http
->getConn()->peer
);
1249 debugs(88, DBG_IMPORTANT
, "\treceived " << http
->out
.size
<< " bytes");
1250 debugs(88, DBG_IMPORTANT
, "\tURI " << http
->log_uri
);
1254 if (http
->out
.offset
> 0x7FFF0000) {
1255 debugs(88, DBG_IMPORTANT
, "WARNING: closing FD " << fd
< " to prevent out.offset counter overflow");
1256 if (http
->getConn())
1257 debugs(88, DBG_IMPORTANT
, "\tclient " << http
->getConn()->peer
);
1258 debugs(88, DBG_IMPORTANT
, "\treceived " << http
->out
.size
<< " bytes, offset " << http
->out
.offset
);
1259 debugs(88, DBG_IMPORTANT
, "\tURI " << http
->log_uri
);
1268 * *http is a valid structure.
1269 * fd is either -1, or an open fd.
1271 * TODO: enumify this
1273 * This function is used by any http request sink, to determine the status
1276 clientStream_status_t
1277 clientReplyStatus(clientStreamNode
* aNode
, ClientHttpRequest
* http
)
1279 clientReplyContext
*context
= dynamic_cast<clientReplyContext
*>(aNode
->data
.getRaw());
1281 assert (context
->http
== http
);
1282 return context
->replyStatus();
1285 clientStream_status_t
1286 clientReplyContext::replyStatus()
1289 /* Here because lower nodes don't need it */
1291 if (http
->storeEntry() == NULL
) {
1292 debugs(88, 5, "clientReplyStatus: no storeEntry");
1293 return STREAM_FAILED
; /* yuck, but what can we do? */
1296 if (EBIT_TEST(http
->storeEntry()->flags
, ENTRY_ABORTED
)) {
1297 /* TODO: Could upstream read errors (result.flags.error) be
1298 * lost, and result in undersize requests being considered
1299 * complete. Should we tcp reset such connections ?
1301 debugs(88, 5, "clientReplyStatus: aborted storeEntry");
1302 return STREAM_FAILED
;
1305 if ((done
= checkTransferDone()) != 0 || flags
.complete
) {
1306 debugs(88, 5, "clientReplyStatus: transfer is DONE: " << done
<< flags
.complete
);
1307 /* Ok we're finished, but how? */
1309 if (EBIT_TEST(http
->storeEntry()->flags
, ENTRY_BAD_LENGTH
)) {
1310 debugs(88, 5, "clientReplyStatus: truncated response body");
1311 return STREAM_UNPLANNED_COMPLETE
;
1315 debugs(88, 5, "clientReplyStatus: closing, !done, but read 0 bytes");
1316 return STREAM_FAILED
;
1319 const int64_t expectedBodySize
=
1320 http
->storeEntry()->getReply()->bodySize(http
->request
->method
);
1321 if (expectedBodySize
>= 0 && !http
->gotEnough()) {
1322 debugs(88, 5, "clientReplyStatus: client didn't get all it expected");
1323 return STREAM_UNPLANNED_COMPLETE
;
1326 debugs(88, 5, "clientReplyStatus: stream complete; keepalive=" <<
1327 http
->request
->flags
.proxyKeepalive
);
1328 return STREAM_COMPLETE
;
1331 // XXX: Should this be checked earlier? We could return above w/o checking.
1332 if (reply
->receivedBodyTooLarge(*http
->request
, http
->out
.offset
- 4096)) {
1333 /* 4096 is a margin for the HTTP headers included in out.offset */
1334 debugs(88, 5, "clientReplyStatus: client reply body is too large");
1335 return STREAM_FAILED
;
1341 /* Responses with no body will not have a content-type header,
1342 * which breaks the rep_mime_type acl, which
1343 * coincidentally, is the most common acl for reply access lists.
1344 * A better long term fix for this is to allow acl matchs on the various
1345 * status codes, and then supply a default ruleset that puts these
1346 * codes before any user defines access entries. That way the user
1347 * can choose to block these responses where appropriate, but won't get
1348 * mysterious breakages.
1351 clientReplyContext::alwaysAllowResponse(Http::StatusCode sline
) const
1357 case Http::scContinue
:
1359 case Http::scSwitchingProtocols
:
1361 case Http::scProcessing
:
1363 case Http::scNoContent
:
1365 case Http::scNotModified
:
1377 * Generate the reply headers sent to client.
1379 * Filters out unwanted entries and hop-by-hop from original reply header
1380 * then adds extra entries if we have more info than origin server
1381 * then adds Squid specific entries
1384 clientReplyContext::buildReplyHeader()
1386 HttpHeader
*hdr
= &reply
->header
;
1387 const bool is_hit
= http
->logType
.isTcpHit();
1388 HttpRequest
*request
= http
->request
;
1389 #if DONT_FILTER_THESE
1390 /* but you might want to if you run Squid as an HTTP accelerator */
1391 /* hdr->delById(HDR_ACCEPT_RANGES); */
1392 hdr
->delById(HDR_ETAG
);
1395 if (is_hit
|| collapsedRevalidation
== crSlave
)
1396 hdr
->delById(Http::HdrType::SET_COOKIE
);
1397 // TODO: RFC 2965 : Must honour Cache-Control: no-cache="set-cookie2" and remove header.
1399 // if there is not configured a peer proxy with login=PASS or login=PASSTHRU option enabled
1400 // remove the Proxy-Authenticate header
1401 if ( !request
->peer_login
|| (strcmp(request
->peer_login
,"PASS") != 0 && strcmp(request
->peer_login
,"PASSTHRU") != 0)) {
1403 // but allow adaptation services to authenticate clients
1404 // via request satisfaction
1405 if (!http
->requestSatisfactionMode())
1407 reply
->header
.delById(Http::HdrType::PROXY_AUTHENTICATE
);
1410 reply
->header
.removeHopByHopEntries();
1411 // paranoid: ContentLengthInterpreter has cleaned non-generated replies
1412 reply
->removeIrrelevantContentLength();
1414 // if (request->range)
1415 // clientBuildRangeHeader(http, reply);
1418 * Add a estimated Age header on cache hits.
1422 * Remove any existing Age header sent by upstream caches
1423 * (note that the existing header is passed along unmodified
1426 hdr
->delById(Http::HdrType::AGE
);
1428 * This adds the calculated object age. Note that the details of the
1429 * age calculation is performed by adjusting the timestamp in
1430 * StoreEntry::timestampsSet(), not here.
1432 if (EBIT_TEST(http
->storeEntry()->flags
, ENTRY_SPECIAL
)) {
1433 hdr
->delById(Http::HdrType::DATE
);
1434 hdr
->putTime(Http::HdrType::DATE
, squid_curtime
);
1435 } else if (http
->getConn() && http
->getConn()->port
->actAsOrigin
) {
1436 // Swap the Date: header to current time if we are simulating an origin
1437 HttpHeaderEntry
*h
= hdr
->findEntry(Http::HdrType::DATE
);
1439 hdr
->putExt("X-Origin-Date", h
->value
.termedBuf());
1440 hdr
->delById(Http::HdrType::DATE
);
1441 hdr
->putTime(Http::HdrType::DATE
, squid_curtime
);
1442 h
= hdr
->findEntry(Http::HdrType::EXPIRES
);
1443 if (h
&& http
->storeEntry()->expires
>= 0) {
1444 hdr
->putExt("X-Origin-Expires", h
->value
.termedBuf());
1445 hdr
->delById(Http::HdrType::EXPIRES
);
1446 hdr
->putTime(Http::HdrType::EXPIRES
, squid_curtime
+ http
->storeEntry()->expires
- http
->storeEntry()->timestamp
);
1448 if (http
->storeEntry()->timestamp
<= squid_curtime
) {
1449 // put X-Cache-Age: instead of Age:
1451 snprintf(age
, sizeof(age
), "%" PRId64
, static_cast<int64_t>(squid_curtime
- http
->storeEntry()->timestamp
));
1452 hdr
->putExt("X-Cache-Age", age
);
1454 } else if (http
->storeEntry()->timestamp
<= squid_curtime
) {
1455 hdr
->putInt(Http::HdrType::AGE
,
1456 squid_curtime
- http
->storeEntry()->timestamp
);
1457 /* Signal old objects. NB: rfc 2616 is not clear,
1458 * by implication, on whether we should do this to all
1459 * responses, or only cache hits.
1460 * 14.46 states it ONLY applys for heuristically caclulated
1461 * freshness values, 13.2.4 doesn't specify the same limitation.
1462 * We interpret RFC 2616 under the combination.
1464 /* TODO: if maxage or s-maxage is present, don't do this */
1466 if (squid_curtime
- http
->storeEntry()->timestamp
>= 86400)
1467 hdr
->putWarning(113, "This cache hit is still fresh and more than 1 day old");
1471 /* RFC 2616: Section 14.18
1473 * Add a Date: header if missing.
1474 * We have access to a clock therefore are required to amend any shortcoming in servers.
1476 * NP: done after Age: to prevent ENTRY_SPECIAL double-handling this header.
1478 if ( !hdr
->has(Http::HdrType::DATE
) ) {
1479 if (!http
->storeEntry())
1480 hdr
->putTime(Http::HdrType::DATE
, squid_curtime
);
1481 else if (http
->storeEntry()->timestamp
> 0)
1482 hdr
->putTime(Http::HdrType::DATE
, http
->storeEntry()->timestamp
);
1484 debugs(88,DBG_IMPORTANT
,"BUG 3279: HTTP reply without Date:");
1485 /* dump something useful about the problem */
1486 http
->storeEntry()->dump(DBG_IMPORTANT
);
1490 // add Warnings required by RFC 2616 if serving a stale hit
1491 if (http
->request
->flags
.staleIfHit
&& http
->logType
.isTcpHit()) {
1492 hdr
->putWarning(110, "Response is stale");
1493 if (http
->request
->flags
.needValidation
)
1494 hdr
->putWarning(111, "Revalidation failed");
1497 /* Filter unproxyable authentication types */
1498 if (http
->logType
.oldType
!= LOG_TCP_DENIED
&&
1499 hdr
->has(Http::HdrType::WWW_AUTHENTICATE
)) {
1500 HttpHeaderPos pos
= HttpHeaderInitPos
;
1503 int connection_auth_blocked
= 0;
1504 while ((e
= hdr
->getEntry(&pos
))) {
1505 if (e
->id
== Http::HdrType::WWW_AUTHENTICATE
) {
1506 const char *value
= e
->value
.rawBuf();
1508 if ((strncasecmp(value
, "NTLM", 4) == 0 &&
1509 (value
[4] == '\0' || value
[4] == ' '))
1511 (strncasecmp(value
, "Negotiate", 9) == 0 &&
1512 (value
[9] == '\0' || value
[9] == ' '))
1514 (strncasecmp(value
, "Kerberos", 8) == 0 &&
1515 (value
[8] == '\0' || value
[8] == ' '))) {
1516 if (request
->flags
.connectionAuthDisabled
) {
1517 hdr
->delAt(pos
, connection_auth_blocked
);
1520 request
->flags
.mustKeepalive
= true;
1521 if (!request
->flags
.accelerated
&& !request
->flags
.intercepted
) {
1522 httpHeaderPutStrf(hdr
, Http::HdrType::PROXY_SUPPORT
, "Session-Based-Authentication");
1524 We send "Connection: Proxy-Support" header to mark
1525 Proxy-Support as a hop-by-hop header for intermediaries that do not
1526 understand the semantics of this header. The RFC should have included
1527 this recommendation.
1529 httpHeaderPutStrf(hdr
, Http::HdrType::CONNECTION
, "Proxy-support");
1536 if (connection_auth_blocked
)
1541 /* Handle authentication headers */
1542 if (http
->logType
.oldType
== LOG_TCP_DENIED
&&
1543 ( reply
->sline
.status() == Http::scProxyAuthenticationRequired
||
1544 reply
->sline
.status() == Http::scUnauthorized
)
1546 /* Add authentication header */
1547 /*! \todo alter errorstate to be accel on|off aware. The 0 on the next line
1548 * depends on authenticate behaviour: all schemes to date send no extra
1549 * data on 407/401 responses, and do not check the accel state on 401/407
1552 Auth::UserRequest::AddReplyAuthHeader(reply
, request
->auth_user_request
, request
, 0, 1);
1553 } else if (request
->auth_user_request
!= NULL
)
1554 Auth::UserRequest::AddReplyAuthHeader(reply
, request
->auth_user_request
, request
, http
->flags
.accel
, 0);
1557 /* Append X-Cache */
1558 httpHeaderPutStrf(hdr
, Http::HdrType::X_CACHE
, "%s from %s",
1559 is_hit
? "HIT" : "MISS", getMyHostname());
1561 #if USE_CACHE_DIGESTS
1562 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1563 httpHeaderPutStrf(hdr
, Http::HdrType::X_CACHE_LOOKUP
, "%s from %s:%d",
1564 lookup_type
? lookup_type
: "NONE",
1565 getMyHostname(), getMyPort());
1569 const bool maySendChunkedReply
= !request
->multipartRangeRequest() &&
1570 reply
->sline
.protocol
== AnyP::PROTO_HTTP
&& // response is HTTP
1571 (request
->http_ver
>= Http::ProtocolVersion(1,1));
1573 /* Check whether we should send keep-alive */
1574 if (!Config
.onoff
.error_pconns
&& reply
->sline
.status() >= 400 && !request
->flags
.mustKeepalive
) {
1575 debugs(33, 3, "clientBuildReplyHeader: Error, don't keep-alive");
1576 request
->flags
.proxyKeepalive
= false;
1577 } else if (!Config
.onoff
.client_pconns
&& !request
->flags
.mustKeepalive
) {
1578 debugs(33, 2, "clientBuildReplyHeader: Connection Keep-Alive not requested by admin or client");
1579 request
->flags
.proxyKeepalive
= false;
1580 } else if (request
->flags
.proxyKeepalive
&& shutting_down
) {
1581 debugs(88, 3, "clientBuildReplyHeader: Shutting down, don't keep-alive.");
1582 request
->flags
.proxyKeepalive
= false;
1583 } else if (request
->flags
.connectionAuth
&& !reply
->keep_alive
) {
1584 debugs(33, 2, "clientBuildReplyHeader: Connection oriented auth but server side non-persistent");
1585 request
->flags
.proxyKeepalive
= false;
1586 } else if (reply
->bodySize(request
->method
) < 0 && !maySendChunkedReply
) {
1587 debugs(88, 3, "clientBuildReplyHeader: can't keep-alive, unknown body size" );
1588 request
->flags
.proxyKeepalive
= false;
1589 } else if (fdUsageHigh()&& !request
->flags
.mustKeepalive
) {
1590 debugs(88, 3, "clientBuildReplyHeader: Not many unused FDs, can't keep-alive");
1591 request
->flags
.proxyKeepalive
= false;
1592 } else if (request
->flags
.sslBumped
&& !reply
->persistent()) {
1593 // We do not really have to close, but we pretend we are a tunnel.
1594 debugs(88, 3, "clientBuildReplyHeader: bumped reply forces close");
1595 request
->flags
.proxyKeepalive
= false;
1596 } else if (request
->pinnedConnection() && !reply
->persistent()) {
1597 // The peer wants to close the pinned connection
1598 debugs(88, 3, "pinned reply forces close");
1599 request
->flags
.proxyKeepalive
= false;
1600 } else if (http
->getConn()) {
1601 ConnStateData
* conn
= http
->getConn();
1602 if (!Comm::IsConnOpen(conn
->port
->listenConn
)) {
1603 // The listening port closed because of a reconfigure
1604 debugs(88, 3, "listening port closed");
1605 request
->flags
.proxyKeepalive
= false;
1609 // Decide if we send chunked reply
1610 if (maySendChunkedReply
&& reply
->bodySize(request
->method
) < 0) {
1611 debugs(88, 3, "clientBuildReplyHeader: chunked reply");
1612 request
->flags
.chunkedReply
= true;
1613 hdr
->putStr(Http::HdrType::TRANSFER_ENCODING
, "chunked");
1616 hdr
->addVia(reply
->sline
.version
);
1618 /* Signal keep-alive or close explicitly */
1619 hdr
->putStr(Http::HdrType::CONNECTION
, request
->flags
.proxyKeepalive
? "keep-alive" : "close");
1621 #if ADD_X_REQUEST_URI
1623 * Knowing the URI of the request is useful when debugging persistent
1624 * connections in a client; we cannot guarantee the order of http headers,
1625 * but X-Request-URI is likely to be the very last header to ease use from a
1626 * debugger [hdr->entries.count-1].
1628 hdr
->putStr(Http::HdrType::X_REQUEST_URI
,
1629 http
->memOjbect()->url
? http
->memObject()->url
: http
->uri
);
1633 /* Surrogate-Control requires Surrogate-Capability from upstream to pass on */
1634 if ( hdr
->has(Http::HdrType::SURROGATE_CONTROL
) ) {
1635 if (!request
->header
.has(Http::HdrType::SURROGATE_CAPABILITY
)) {
1636 hdr
->delById(Http::HdrType::SURROGATE_CONTROL
);
1638 /* TODO: else case: drop any controls intended specifically for our surrogate ID */
1641 httpHdrMangleList(hdr
, request
, http
->al
, ROR_REPLY
);
1645 clientReplyContext::cloneReply()
1647 assert(reply
== NULL
);
1649 reply
= http
->storeEntry()->getReply()->clone();
1652 http
->al
->reply
= reply
;
1654 if (reply
->sline
.protocol
== AnyP::PROTO_HTTP
) {
1655 /* RFC 2616 requires us to advertise our version (but only on real HTTP traffic) */
1656 reply
->sline
.version
= Http::ProtocolVersion();
1659 /* do header conversions */
1663 /// Safely disposes of an entry pointing to a cache hit that we do not want.
1664 /// We cannot just ignore the entry because it may be locking or otherwise
1665 /// holding an associated cache resource of some sort.
1667 clientReplyContext::forgetHit()
1669 StoreEntry
*e
= http
->storeEntry();
1670 assert(e
); // or we are not dealing with a hit
1671 // We probably have not locked the entry earlier, unfortunately. We lock it
1672 // now so that we can unlock two lines later (and trigger cleanup).
1673 // Ideally, ClientHttpRequest::storeEntry() should lock/unlock, but it is
1674 // used so inconsistently that simply adding locking there leads to bugs.
1675 e
->lock("clientReplyContext::forgetHit");
1676 http
->storeEntry(NULL
);
1677 e
->unlock("clientReplyContext::forgetHit"); // may delete e
1681 clientReplyContext::identifyStoreObject()
1683 HttpRequest
*r
= http
->request
;
1685 // client sent CC:no-cache or some other condition has been
1686 // encountered which prevents delivering a public/cached object.
1687 if (!r
->flags
.noCache
|| r
->flags
.internal
) {
1688 lookingforstore
= 5;
1689 StoreEntry::getPublicByRequest (this, r
);
1691 identifyFoundObject(nullptr);
1696 * Check state of the current StoreEntry object.
1697 * to see if we can determine the final status of the request.
1700 clientReplyContext::identifyFoundObject(StoreEntry
*newEntry
)
1702 HttpRequest
*r
= http
->request
;
1703 http
->storeEntry(newEntry
);
1704 const auto e
= http
->storeEntry();
1706 /* Release IP-cache entries on reload */
1707 /** \li If the request has no-cache flag set or some no_cache HACK in operation we
1708 * 'invalidate' the cached IP entries for this request ???
1710 if (r
->flags
.noCache
|| r
->flags
.noCacheHack())
1711 ipcacheInvalidateNegative(r
->url
.host());
1713 #if USE_CACHE_DIGESTS
1714 lookup_type
= e
? "HIT" : "MISS";
1718 /** \li If no StoreEntry object is current assume this object isn't in the cache set MISS*/
1719 debugs(85, 3, "StoreEntry is NULL - MISS");
1720 http
->logType
.update(LOG_TCP_MISS
);
1725 if (Config
.onoff
.offline
) {
1726 /** \li If we are running in offline mode set to HIT */
1727 debugs(85, 3, "offline HIT " << *e
);
1728 http
->logType
.update(LOG_TCP_HIT
);
1733 if (http
->redirect
.status
) {
1734 /** \li If redirection status is True force this to be a MISS */
1735 debugs(85, 3, "REDIRECT status forced StoreEntry to NULL (no body on 3XX responses) " << *e
);
1737 http
->logType
.update(LOG_TCP_REDIRECT
);
1742 if (!e
->validToSend()) {
1743 debugs(85, 3, "!storeEntryValidToSend MISS " << *e
);
1745 http
->logType
.update(LOG_TCP_MISS
);
1750 if (EBIT_TEST(e
->flags
, ENTRY_SPECIAL
)) {
1751 /* \li Special entries are always hits, no matter what the client says */
1752 debugs(85, 3, "ENTRY_SPECIAL HIT " << *e
);
1753 http
->logType
.update(LOG_TCP_HIT
);
1758 if (r
->flags
.noCache
) {
1759 debugs(85, 3, "no-cache REFRESH MISS " << *e
);
1761 http
->logType
.update(LOG_TCP_CLIENT_REFRESH_MISS
);
1766 if (e
->hittingRequiresCollapsing() && !startCollapsingOn(*e
, false)) {
1767 debugs(85, 3, "prohibited CF MISS " << *e
);
1769 http
->logType
.update(LOG_TCP_MISS
);
1774 debugs(85, 3, "default HIT " << *e
);
1775 http
->logType
.update(LOG_TCP_HIT
);
1780 * Request more data from the store for the client Stream
1781 * This is *the* entry point to this module.
1784 * - This is the head of the list.
1785 * - There is at least one more node.
1786 * - Data context is not null
1789 clientGetMoreData(clientStreamNode
* aNode
, ClientHttpRequest
* http
)
1791 /* Test preconditions */
1792 assert(aNode
!= NULL
);
1793 assert(cbdataReferenceValid(aNode
));
1794 assert(aNode
->node
.prev
== NULL
);
1795 assert(aNode
->node
.next
!= NULL
);
1796 clientReplyContext
*context
= dynamic_cast<clientReplyContext
*>(aNode
->data
.getRaw());
1798 assert(context
->http
== http
);
1800 clientStreamNode
*next
= ( clientStreamNode
*)aNode
->node
.next
->data
;
1802 if (!context
->ourNode
)
1803 context
->ourNode
= aNode
;
1805 /* no cbdatareference, this is only used once, and safely */
1806 if (context
->flags
.storelogiccomplete
) {
1807 StoreIOBuffer tempBuffer
;
1808 tempBuffer
.offset
= next
->readBuffer
.offset
+ context
->headers_sz
;
1809 tempBuffer
.length
= next
->readBuffer
.length
;
1810 tempBuffer
.data
= next
->readBuffer
.data
;
1812 storeClientCopy(context
->sc
, http
->storeEntry(),
1813 tempBuffer
, clientReplyContext::SendMoreData
, context
);
1817 if (context
->http
->request
->method
== Http::METHOD_PURGE
) {
1818 context
->purgeRequest();
1822 // OPTIONS with Max-Forwards:0 handled in clientProcessRequest()
1824 if (context
->http
->request
->method
== Http::METHOD_TRACE
) {
1825 if (context
->http
->request
->header
.getInt64(Http::HdrType::MAX_FORWARDS
) == 0) {
1826 context
->traceReply(aNode
);
1830 /* continue forwarding, not finished yet. */
1831 http
->logType
.update(LOG_TCP_MISS
);
1833 context
->doGetMoreData();
1835 context
->identifyStoreObject();
1839 clientReplyContext::doGetMoreData()
1841 /* We still have to do store logic processing - vary, cache hit etc */
1842 if (http
->storeEntry() != NULL
) {
1843 /* someone found the object in the cache for us */
1844 StoreIOBuffer localTempBuffer
;
1846 http
->storeEntry()->lock("clientReplyContext::doGetMoreData");
1848 http
->storeEntry()->ensureMemObject(storeId(), http
->log_uri
, http
->request
->method
);
1850 sc
= storeClientListAdd(http
->storeEntry(), this);
1852 sc
->setDelayId(DelayId::DelayClient(http
));
1855 assert(http
->logType
.oldType
== LOG_TCP_HIT
);
1857 /* guarantee nothing has been sent yet! */
1858 assert(http
->out
.size
== 0);
1859 assert(http
->out
.offset
== 0);
1861 if (ConnStateData
*conn
= http
->getConn()) {
1862 if (Ip::Qos::TheConfig
.isHitTosActive()) {
1863 Ip::Qos::doTosLocalHit(conn
->clientConnection
);
1866 if (Ip::Qos::TheConfig
.isHitNfmarkActive()) {
1867 Ip::Qos::doNfmarkLocalHit(conn
->clientConnection
);
1871 localTempBuffer
.offset
= reqofs
;
1872 localTempBuffer
.length
= getNextNode()->readBuffer
.length
;
1873 localTempBuffer
.data
= getNextNode()->readBuffer
.data
;
1874 storeClientCopy(sc
, http
->storeEntry(), localTempBuffer
, CacheHit
, this);
1876 /* MISS CASE, http->logType is already set! */
1881 /** The next node has removed itself from the stream. */
1883 clientReplyDetach(clientStreamNode
* node
, ClientHttpRequest
* http
)
1885 /** detach from the stream */
1886 clientStreamDetach(node
, http
);
1890 * Accepts chunk of a http message in buf, parses prefix, filters headers and
1891 * such, writes processed message to the message recipient
1894 clientReplyContext::SendMoreData(void *data
, StoreIOBuffer result
)
1896 clientReplyContext
*context
= static_cast<clientReplyContext
*>(data
);
1897 context
->sendMoreData (result
);
1901 clientReplyContext::makeThisHead()
1903 /* At least, I think thats what this does */
1904 dlinkDelete(&http
->active
, &ClientActiveRequests
);
1905 dlinkAdd(http
, &http
->active
, &ClientActiveRequests
);
1909 clientReplyContext::errorInStream(StoreIOBuffer
const &result
, size_t const &sizeToProcess
)const
1911 return /* aborted request */
1912 (http
->storeEntry() && EBIT_TEST(http
->storeEntry()->flags
, ENTRY_ABORTED
)) ||
1913 /* Upstream read error */ (result
.flags
.error
) ||
1914 /* Upstream EOF */ (sizeToProcess
== 0);
1918 clientReplyContext::sendStreamError(StoreIOBuffer
const &result
)
1920 /** call clientWriteComplete so the client socket gets closed
1922 * We call into the stream, because we don't know that there is a
1925 debugs(88, 5, "A stream error has occurred, marking as complete and sending no data.");
1926 StoreIOBuffer localTempBuffer
;
1928 http
->request
->flags
.streamError
= true;
1929 localTempBuffer
.flags
.error
= result
.flags
.error
;
1930 clientStreamCallback((clientStreamNode
*)http
->client_stream
.head
->data
, http
, NULL
,
1935 clientReplyContext::pushStreamData(StoreIOBuffer
const &result
, char *source
)
1937 StoreIOBuffer localTempBuffer
;
1939 if (result
.length
== 0) {
1940 debugs(88, 5, "clientReplyContext::pushStreamData: marking request as complete due to 0 length store result");
1944 assert(result
.offset
- headers_sz
== next()->readBuffer
.offset
);
1945 localTempBuffer
.offset
= result
.offset
- headers_sz
;
1946 localTempBuffer
.length
= result
.length
;
1948 if (localTempBuffer
.length
)
1949 localTempBuffer
.data
= source
;
1951 clientStreamCallback((clientStreamNode
*)http
->client_stream
.head
->data
, http
, NULL
,
1956 clientReplyContext::next() const
1958 assert ( (clientStreamNode
*)http
->client_stream
.head
->next
->data
== getNextNode());
1959 return getNextNode();
1963 clientReplyContext::sendBodyTooLargeError()
1965 Ip::Address tmp_noaddr
;
1966 tmp_noaddr
.setNoAddr(); // TODO: make a global const
1967 http
->logType
.update(LOG_TCP_DENIED_REPLY
);
1968 ErrorState
*err
= clientBuildError(ERR_TOO_BIG
, Http::scForbidden
, NULL
,
1969 http
->getConn() != NULL
? http
->getConn()->clientConnection
->remote
: tmp_noaddr
,
1970 http
->request
, http
->al
);
1971 removeClientStoreReference(&(sc
), http
);
1972 HTTPMSGUNLOCK(reply
);
1977 /// send 412 (Precondition Failed) to client
1979 clientReplyContext::sendPreconditionFailedError()
1981 http
->logType
.update(LOG_TCP_HIT
);
1982 Ip::Address tmp_noaddr
;
1983 tmp_noaddr
.setNoAddr();
1984 ErrorState
*const err
=
1985 clientBuildError(ERR_PRECONDITION_FAILED
, Http::scPreconditionFailed
,
1986 NULL
, http
->getConn() ? http
->getConn()->clientConnection
->remote
: tmp_noaddr
, http
->request
, http
->al
);
1987 removeClientStoreReference(&sc
, http
);
1988 HTTPMSGUNLOCK(reply
);
1992 /// send 304 (Not Modified) to client
1994 clientReplyContext::sendNotModified()
1996 StoreEntry
*e
= http
->storeEntry();
1997 const time_t timestamp
= e
->timestamp
;
1998 HttpReply
*const temprep
= e
->getReply()->make304();
1999 // log as TCP_INM_HIT if code 304 generated for
2000 // If-None-Match request
2001 if (!http
->request
->flags
.ims
)
2002 http
->logType
.update(LOG_TCP_INM_HIT
);
2004 http
->logType
.update(LOG_TCP_IMS_HIT
);
2005 removeClientStoreReference(&sc
, http
);
2006 createStoreEntry(http
->request
->method
, RequestFlags());
2007 e
= http
->storeEntry();
2008 // Copy timestamp from the original entry so the 304
2009 // reply has a meaningful Age: header.
2011 e
->timestamp
= timestamp
;
2012 e
->replaceHttpReply(temprep
);
2015 * TODO: why put this in the store and then serialise it and
2016 * then parse it again. Simply mark the request complete in
2017 * our context and write the reply struct to the client side.
2019 triggerInitialStoreRead();
2022 /// send 304 (Not Modified) or 412 (Precondition Failed) to client
2023 /// depending on request method
2025 clientReplyContext::sendNotModifiedOrPreconditionFailedError()
2027 if (http
->request
->method
== Http::METHOD_GET
||
2028 http
->request
->method
== Http::METHOD_HEAD
)
2031 sendPreconditionFailedError();
2035 clientReplyContext::processReplyAccess ()
2037 /* NP: this should probably soft-fail to a zero-sized-reply error ?? */
2040 /** Don't block our own responses or HTTP status messages */
2041 if (http
->logType
.oldType
== LOG_TCP_DENIED
||
2042 http
->logType
.oldType
== LOG_TCP_DENIED_REPLY
||
2043 alwaysAllowResponse(reply
->sline
.status())) {
2044 headers_sz
= reply
->hdr_sz
;
2045 processReplyAccessResult(ACCESS_ALLOWED
);
2049 /** Check for reply to big error */
2050 if (reply
->expectedBodyTooLarge(*http
->request
)) {
2051 sendBodyTooLargeError();
2055 headers_sz
= reply
->hdr_sz
;
2057 /** check for absent access controls (permit by default) */
2058 if (!Config
.accessList
.reply
) {
2059 processReplyAccessResult(ACCESS_ALLOWED
);
2063 /** Process http_reply_access lists */
2064 ACLFilledChecklist
*replyChecklist
=
2065 clientAclChecklistCreate(Config
.accessList
.reply
, http
);
2066 replyChecklist
->reply
= reply
;
2067 HTTPMSGLOCK(replyChecklist
->reply
);
2068 replyChecklist
->nonBlockingCheck(ProcessReplyAccessResult
, this);
2072 clientReplyContext::ProcessReplyAccessResult(Acl::Answer rv
, void *voidMe
)
2074 clientReplyContext
*me
= static_cast<clientReplyContext
*>(voidMe
);
2075 me
->processReplyAccessResult(rv
);
2079 clientReplyContext::processReplyAccessResult(const Acl::Answer
&accessAllowed
)
2081 debugs(88, 2, "The reply for " << http
->request
->method
2082 << ' ' << http
->uri
<< " is " << accessAllowed
<< ", because it matched "
2083 << (AclMatchedName
? AclMatchedName
: "NO ACL's"));
2085 if (!accessAllowed
.allowed()) {
2088 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
, 1);
2090 http
->logType
.update(LOG_TCP_DENIED_REPLY
);
2092 if (page_id
== ERR_NONE
)
2093 page_id
= ERR_ACCESS_DENIED
;
2095 Ip::Address tmp_noaddr
;
2096 tmp_noaddr
.setNoAddr();
2097 err
= clientBuildError(page_id
, Http::scForbidden
, NULL
,
2098 http
->getConn() != NULL
? http
->getConn()->clientConnection
->remote
: tmp_noaddr
,
2099 http
->request
, http
->al
);
2101 removeClientStoreReference(&sc
, http
);
2103 HTTPMSGUNLOCK(reply
);
2110 /* Ok, the reply is allowed, */
2111 http
->loggingEntry(http
->storeEntry());
2113 ssize_t body_size
= reqofs
- reply
->hdr_sz
;
2114 if (body_size
< 0) {
2115 reqofs
= reply
->hdr_sz
;
2119 debugs(88, 3, "clientReplyContext::sendMoreData: Appending " <<
2120 (int) body_size
<< " bytes after " << reply
->hdr_sz
<<
2121 " bytes of headers");
2125 if (http
->flags
.accel
&& reply
->sline
.status() != Http::scForbidden
&&
2126 !alwaysAllowResponse(reply
->sline
.status()) &&
2127 esiEnableProcessing(reply
)) {
2128 debugs(88, 2, "Enabling ESI processing for " << http
->uri
);
2129 clientStreamInsertHead(&http
->client_stream
, esiStreamRead
,
2130 esiProcessStream
, esiStreamDetach
, esiStreamStatus
, NULL
);
2135 if (http
->request
->method
== Http::METHOD_HEAD
) {
2136 /* do not forward body for HEAD replies */
2138 http
->flags
.done_copying
= true;
2142 assert (!flags
.headersSent
);
2143 flags
.headersSent
= true;
2145 StoreIOBuffer localTempBuffer
;
2146 char *buf
= next()->readBuffer
.data
;
2147 char *body_buf
= buf
+ reply
->hdr_sz
;
2149 //Server side may disable ranges under some circumstances.
2151 if ((!http
->request
->range
))
2152 next()->readBuffer
.offset
= 0;
2154 body_buf
-= next()->readBuffer
.offset
;
2156 if (next()->readBuffer
.offset
!= 0) {
2157 if (next()->readBuffer
.offset
> body_size
) {
2158 /* Can't use any of the body we received. send nothing */
2159 localTempBuffer
.length
= 0;
2160 localTempBuffer
.data
= NULL
;
2162 localTempBuffer
.length
= body_size
- next()->readBuffer
.offset
;
2163 localTempBuffer
.data
= body_buf
+ next()->readBuffer
.offset
;
2166 localTempBuffer
.length
= body_size
;
2167 localTempBuffer
.data
= body_buf
;
2170 /* TODO??: move the data in the buffer back by the request header size */
2171 clientStreamCallback((clientStreamNode
*)http
->client_stream
.head
->data
,
2172 http
, reply
, localTempBuffer
);
2178 clientReplyContext::sendMoreData (StoreIOBuffer result
)
2183 StoreEntry
*entry
= http
->storeEntry();
2185 if (ConnStateData
* conn
= http
->getConn()) {
2186 if (!conn
->isOpen()) {
2187 debugs(33,3, "not sending more data to closing connection " << conn
->clientConnection
);
2190 if (conn
->pinning
.zeroReply
) {
2191 debugs(33,3, "not sending more data after a pinned zero reply " << conn
->clientConnection
);
2195 if (reqofs
==0 && !http
->logType
.isTcpHit()) {
2196 if (Ip::Qos::TheConfig
.isHitTosActive()) {
2197 Ip::Qos::doTosLocalMiss(conn
->clientConnection
, http
->request
->hier
.code
);
2199 if (Ip::Qos::TheConfig
.isHitNfmarkActive()) {
2200 Ip::Qos::doNfmarkLocalMiss(conn
->clientConnection
, http
->request
->hier
.code
);
2204 debugs(88, 5, conn
->clientConnection
<<
2205 " '" << entry
->url() << "'" <<
2206 " out.offset=" << http
->out
.offset
);
2209 char *buf
= next()->readBuffer
.data
;
2211 if (buf
!= result
.data
) {
2212 /* we've got to copy some data */
2213 assert(result
.length
<= next()->readBuffer
.length
);
2214 memcpy(buf
, result
.data
, result
.length
);
2217 /* We've got the final data to start pushing... */
2218 flags
.storelogiccomplete
= 1;
2220 reqofs
+= result
.length
;
2222 assert(reqofs
<= HTTP_REQBUF_SZ
|| flags
.headersSent
);
2224 assert(http
->request
!= NULL
);
2226 /* ESI TODO: remove this assert once everything is stable */
2227 assert(http
->client_stream
.head
->data
2228 && cbdataReferenceValid(http
->client_stream
.head
->data
));
2232 debugs(88, 5, "clientReplyContext::sendMoreData: " << http
->uri
<< ", " <<
2233 reqofs
<< " bytes (" << result
.length
<<
2236 /* update size of the request */
2239 if (errorInStream(result
, reqofs
)) {
2240 sendStreamError(result
);
2244 if (flags
.headersSent
) {
2245 pushStreamData (result
, buf
);
2253 sc
->setDelayId(DelayId::DelayClient(http
,reply
));
2256 holdingBuffer
= result
;
2257 processReplyAccess();
2262 clientReplyContext::fillChecklist(ACLFilledChecklist
&checklist
) const
2264 clientAclChecklistFill(checklist
, http
);
2267 /* Using this breaks the client layering just a little!
2270 clientReplyContext::createStoreEntry(const HttpRequestMethod
& m
, RequestFlags reqFlags
)
2272 assert(http
!= NULL
);
2274 * For erroneous requests, we might not have a h->request,
2275 * so make a fake one.
2278 if (http
->request
== NULL
) {
2279 const MasterXaction::Pointer mx
= new MasterXaction(XactionInitiator::initClient
);
2280 // XXX: These fake URI parameters shadow the real (or error:...) URI.
2281 // TODO: Either always set the request earlier and assert here OR use
2282 // http->uri (converted to Anyp::Uri) to create this catch-all request.
2283 const_cast<HttpRequest
*&>(http
->request
) = new HttpRequest(m
, AnyP::PROTO_NONE
, "http", null_string
, mx
);
2284 HTTPMSGLOCK(http
->request
);
2287 StoreEntry
*e
= storeCreateEntry(storeId(), http
->log_uri
, reqFlags
, m
);
2289 // Make entry collapsable ASAP, to increase collapsing chances for others,
2290 // TODO: every must-revalidate and similar request MUST reach the origin,
2291 // but do we have to prohibit others from collapsing on that request?
2292 if (reqFlags
.cachable
&&
2293 !reqFlags
.needValidation
&&
2294 (m
== Http::METHOD_GET
|| m
== Http::METHOD_HEAD
) &&
2295 mayInitiateCollapsing()) {
2296 // make the entry available for future requests now
2297 (void)Store::Root().allowCollapsing(e
, reqFlags
, m
);
2300 sc
= storeClientListAdd(e
, this);
2303 sc
->setDelayId(DelayId::DelayClient(http
));
2310 /* I don't think this is actually needed! -- adrian */
2311 /* http->reqbuf = http->norm_reqbuf; */
2312 // assert(http->reqbuf == http->norm_reqbuf);
2313 /* The next line is illegal because we don't know if the client stream
2314 * buffers have been set up
2316 // storeClientCopy(http->sc, e, 0, HTTP_REQBUF_SZ, http->reqbuf,
2317 // SendMoreData, this);
2318 /* So, we mark the store logic as complete */
2319 flags
.storelogiccomplete
= 1;
2321 /* and get the caller to request a read, from whereever they are */
2322 /* NOTE: after ANY data flows down the pipe, even one step,
2323 * this function CAN NOT be used to manage errors
2325 http
->storeEntry(e
);
2329 clientBuildError(err_type page_id
, Http::StatusCode status
, char const *url
,
2330 Ip::Address
&src_addr
, HttpRequest
* request
, const AccessLogEntry::Pointer
&al
)
2332 const auto err
= new ErrorState(page_id
, status
, request
, al
);
2333 err
->src_addr
= src_addr
;
2336 err
->url
= xstrdup(url
);