]> git.ipfire.org Git - thirdparty/squid.git/blob - src/client_side_reply.cc
Add debug messages indicating type/reason for HITs
[thirdparty/squid.git] / src / client_side_reply.cc
1 /*
2 * DEBUG: section 88 Client-side Reply Routines
3 * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c)
4 *
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
7 *
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
30 *
31 */
32 #include "squid.h"
33 #include "acl/FilledChecklist.h"
34 #include "acl/Gadgets.h"
35 #include "anyp/PortCfg.h"
36 #include "client_side_reply.h"
37 #include "errorpage.h"
38 #include "ETag.h"
39 #include "fd.h"
40 #include "fde.h"
41 #include "format/Token.h"
42 #include "FwdState.h"
43 #include "globals.h"
44 #include "globals.h"
45 #include "HttpHeaderTools.h"
46 #include "HttpReply.h"
47 #include "HttpRequest.h"
48 #include "ip/QosConfig.h"
49 #include "ipcache.h"
50 #include "log/access_log.h"
51 #include "MemObject.h"
52 #include "mime_header.h"
53 #include "neighbors.h"
54 #include "refresh.h"
55 #include "RequestFlags.h"
56 #include "SquidConfig.h"
57 #include "SquidTime.h"
58 #include "Store.h"
59 #include "StrList.h"
60 #include "tools.h"
61 #include "URL.h"
62 #if USE_AUTH
63 #include "auth/UserRequest.h"
64 #endif
65 #if USE_DELAY_POOLS
66 #include "DelayPools.h"
67 #endif
68 #if USE_SQUID_ESI
69 #include "esi/Esi.h"
70 #endif
71
72 #include <memory>
73
74 CBDATA_CLASS_INIT(clientReplyContext);
75
76 /* Local functions */
77 extern "C" CSS clientReplyStatus;
78 ErrorState *clientBuildError(err_type, Http::StatusCode, char const *, Ip::Address &, HttpRequest *);
79
80 /* privates */
81
82 clientReplyContext::~clientReplyContext()
83 {
84 deleting = true;
85 /* This may trigger a callback back into SendMoreData as the cbdata
86 * is still valid
87 */
88 removeClientStoreReference(&sc, http);
89 /* old_entry might still be set if we didn't yet get the reply
90 * code in HandleIMSReply() */
91 removeStoreReference(&old_sc, &old_entry);
92 safe_free(tempBuffer.data);
93 cbdataReferenceDone(http);
94 HTTPMSGUNLOCK(reply);
95 }
96
97 clientReplyContext::clientReplyContext(ClientHttpRequest *clientContext) : http (cbdataReference(clientContext)), old_entry (NULL), old_sc(NULL), deleting(false)
98 {}
99
100 /** Create an error in the store awaiting the client side to read it.
101 *
102 * This may be better placed in the clientStream logic, but it has not been
103 * relocated there yet
104 */
105 void
106 clientReplyContext::setReplyToError(
107 err_type err, Http::StatusCode status, const HttpRequestMethod& method, char const *uri,
108 Ip::Address &addr, HttpRequest * failedrequest, const char *unparsedrequest,
109 #if USE_AUTH
110 Auth::UserRequest::Pointer auth_user_request
111 #else
112 void*
113 #endif
114 )
115 {
116 ErrorState *errstate = clientBuildError(err, status, uri, addr, failedrequest);
117
118 if (unparsedrequest)
119 errstate->request_hdrs = xstrdup(unparsedrequest);
120
121 #if USE_AUTH
122 errstate->auth_user_request = auth_user_request;
123 #endif
124 setReplyToError(method, errstate);
125 }
126
127 void clientReplyContext::setReplyToError(const HttpRequestMethod& method, ErrorState *errstate)
128 {
129 if (errstate->httpStatus == Http::scNotImplemented && http->request)
130 /* prevent confusion over whether we default to persistent or not */
131 http->request->flags.proxyKeepalive = false;
132
133 http->al->http.code = errstate->httpStatus;
134
135 createStoreEntry(method, RequestFlags());
136 assert(errstate->callback_data == NULL);
137 errorAppendEntry(http->storeEntry(), errstate);
138 /* Now the caller reads to get this */
139 }
140
141 void clientReplyContext::setReplyToStoreEntry(StoreEntry *entry)
142 {
143 entry->lock("clientReplyContext::setReplyToStoreEntry"); // removeClientStoreReference() unlocks
144 sc = storeClientListAdd(entry, this);
145 #if USE_DELAY_POOLS
146 sc->setDelayId(DelayId::DelayClient(http));
147 #endif
148 reqofs = 0;
149 reqsize = 0;
150 flags.storelogiccomplete = 1;
151 http->storeEntry(entry);
152 }
153
154 void
155 clientReplyContext::removeStoreReference(store_client ** scp,
156 StoreEntry ** ep)
157 {
158 StoreEntry *e;
159 store_client *sc_tmp = *scp;
160
161 if ((e = *ep) != NULL) {
162 *ep = NULL;
163 storeUnregister(sc_tmp, e, this);
164 *scp = NULL;
165 e->unlock("clientReplyContext::removeStoreReference");
166 }
167 }
168
169 void
170 clientReplyContext::removeClientStoreReference(store_client **scp, ClientHttpRequest *aHttpRequest)
171 {
172 StoreEntry *reference = aHttpRequest->storeEntry();
173 removeStoreReference(scp, &reference);
174 aHttpRequest->storeEntry(reference);
175 }
176
177 void
178 clientReplyContext::saveState()
179 {
180 assert(old_sc == NULL);
181 debugs(88, 3, "clientReplyContext::saveState: saving store context");
182 old_entry = http->storeEntry();
183 old_sc = sc;
184 old_reqsize = reqsize;
185 tempBuffer.offset = reqofs;
186 /* Prevent accessing the now saved entries */
187 http->storeEntry(NULL);
188 sc = NULL;
189 reqsize = 0;
190 reqofs = 0;
191 }
192
193 void
194 clientReplyContext::restoreState()
195 {
196 assert(old_sc != NULL);
197 debugs(88, 3, "clientReplyContext::restoreState: Restoring store context");
198 removeClientStoreReference(&sc, http);
199 http->storeEntry(old_entry);
200 sc = old_sc;
201 reqsize = old_reqsize;
202 reqofs = tempBuffer.offset;
203 /* Prevent accessed the old saved entries */
204 old_entry = NULL;
205 old_sc = NULL;
206 old_reqsize = 0;
207 tempBuffer.offset = 0;
208 }
209
210 void
211 clientReplyContext::startError(ErrorState * err)
212 {
213 createStoreEntry(http->request->method, RequestFlags());
214 triggerInitialStoreRead();
215 errorAppendEntry(http->storeEntry(), err);
216 }
217
218 clientStreamNode *
219 clientReplyContext::getNextNode() const
220 {
221 return (clientStreamNode *)ourNode->node.next->data;
222 }
223
224 /* This function is wrong - the client parameters don't include the
225 * header offset
226 */
227 void
228 clientReplyContext::triggerInitialStoreRead()
229 {
230 /* when confident, 0 becomes reqofs, and then this factors into
231 * startSendProcess
232 */
233 assert(reqofs == 0);
234 StoreIOBuffer localTempBuffer (next()->readBuffer.length, 0, next()->readBuffer.data);
235 storeClientCopy(sc, http->storeEntry(), localTempBuffer, SendMoreData, this);
236 }
237
238 /* there is an expired entry in the store.
239 * setup a temporary buffer area and perform an IMS to the origin
240 */
241 void
242 clientReplyContext::processExpired()
243 {
244 const char *url = storeId();
245 StoreEntry *entry = NULL;
246 debugs(88, 3, "clientReplyContext::processExpired: '" << http->uri << "'");
247 assert(http->storeEntry()->lastmod >= 0);
248 /*
249 * check if we are allowed to contact other servers
250 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
251 * a stale entry *if* it matches client requirements
252 */
253
254 if (http->onlyIfCached()) {
255 processOnlyIfCachedMiss();
256 return;
257 }
258
259 http->request->flags.refresh = true;
260 #if STORE_CLIENT_LIST_DEBUG
261 /* Prevent a race with the store client memory free routines
262 */
263 assert(storeClientIsThisAClient(sc, this));
264 #endif
265 /* Prepare to make a new temporary request */
266 saveState();
267 entry = storeCreateEntry(url,
268 http->log_uri, http->request->flags, http->request->method);
269 /* NOTE, don't call StoreEntry->lock(), storeCreateEntry() does it */
270 sc = storeClientListAdd(entry, this);
271 #if USE_DELAY_POOLS
272 /* delay_id is already set on original store client */
273 sc->setDelayId(DelayId::DelayClient(http));
274 #endif
275
276 http->request->lastmod = old_entry->lastmod;
277
278 if (!http->request->header.has(HDR_IF_NONE_MATCH)) {
279 ETag etag = {NULL, -1}; // TODO: make that a default ETag constructor
280 if (old_entry->hasEtag(etag) && !etag.weak)
281 http->request->etag = etag.str;
282 }
283
284 debugs(88, 5, "clientReplyContext::processExpired : lastmod " << entry->lastmod );
285 http->storeEntry(entry);
286 assert(http->out.offset == 0);
287 assert(http->request->clientConnectionManager == http->getConn());
288
289 /*
290 * A refcounted pointer so that FwdState stays around as long as
291 * this clientReplyContext does
292 */
293 Comm::ConnectionPointer conn = http->getConn() != NULL ? http->getConn()->clientConnection : NULL;
294 FwdState::Start(conn, http->storeEntry(), http->request, http->al);
295
296 /* Register with storage manager to receive updates when data comes in. */
297
298 if (EBIT_TEST(entry->flags, ENTRY_ABORTED))
299 debugs(88, DBG_CRITICAL, "clientReplyContext::processExpired: Found ENTRY_ABORTED object");
300
301 {
302 /* start counting the length from 0 */
303 StoreIOBuffer localTempBuffer(HTTP_REQBUF_SZ, 0, tempbuf);
304 storeClientCopy(sc, entry, localTempBuffer, HandleIMSReply, this);
305 }
306 }
307
308 void
309 clientReplyContext::sendClientUpstreamResponse()
310 {
311 StoreIOBuffer tempresult;
312 removeStoreReference(&old_sc, &old_entry);
313 /* here the data to send is the data we just received */
314 tempBuffer.offset = 0;
315 old_reqsize = 0;
316 /* sendMoreData tracks the offset as well.
317 * Force it back to zero */
318 reqofs = 0;
319 assert(!EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED));
320 /* TODO: provide sendMoreData with the ready parsed reply */
321 tempresult.length = reqsize;
322 tempresult.data = tempbuf;
323 sendMoreData(tempresult);
324 }
325
326 void
327 clientReplyContext::HandleIMSReply(void *data, StoreIOBuffer result)
328 {
329 clientReplyContext *context = (clientReplyContext *)data;
330 context->handleIMSReply(result);
331 }
332
333 void
334 clientReplyContext::sendClientOldEntry()
335 {
336 /* Get the old request back */
337 restoreState();
338 /* here the data to send is in the next nodes buffers already */
339 assert(!EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED));
340 /* sendMoreData tracks the offset as well.
341 * Force it back to zero */
342 reqofs = 0;
343 StoreIOBuffer tempresult (reqsize, reqofs, next()->readBuffer.data);
344 sendMoreData(tempresult);
345 }
346
347 /* This is the workhorse of the HandleIMSReply callback.
348 *
349 * It is called when we've got data back from the origin following our
350 * IMS request to revalidate a stale entry.
351 */
352 void
353 clientReplyContext::handleIMSReply(StoreIOBuffer result)
354 {
355 if (deleting)
356 return;
357
358 debugs(88, 3, "handleIMSReply: " << http->storeEntry()->url() << ", " << (long unsigned) result.length << " bytes" );
359
360 if (http->storeEntry() == NULL)
361 return;
362
363 if (result.flags.error && !EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED))
364 return;
365
366 /* update size of the request */
367 reqsize = result.length + reqofs;
368
369 const Http::StatusCode status = http->storeEntry()->getReply()->sline.status();
370
371 // request to origin was aborted
372 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED)) {
373 debugs(88, 3, "handleIMSReply: request to origin aborted '" << http->storeEntry()->url() << "', sending old entry to client" );
374 http->logType = LOG_TCP_REFRESH_FAIL_OLD;
375 sendClientOldEntry();
376 }
377
378 HttpReply *old_rep = (HttpReply *) old_entry->getReply();
379
380 // origin replied 304
381 if (status == Http::scNotModified) {
382 http->logType = LOG_TCP_REFRESH_UNMODIFIED;
383 http->request->flags.staleIfHit = false; // old_entry is no longer stale
384
385 // update headers on existing entry
386 old_rep->updateOnNotModified(http->storeEntry()->getReply());
387 old_entry->timestampsSet();
388
389 // if client sent IMS
390
391 if (http->request->flags.ims && !old_entry->modifiedSince(http->request)) {
392 // forward the 304 from origin
393 debugs(88, 3, "handleIMSReply: origin replied 304, revalidating existing entry and forwarding 304 to client");
394 sendClientUpstreamResponse();
395 } else {
396 // send existing entry, it's still valid
397 debugs(88, 3, "handleIMSReply: origin replied 304, revalidating existing entry and sending " <<
398 old_rep->sline.status() << " to client");
399 sendClientOldEntry();
400 }
401 }
402
403 // origin replied with a non-error code
404 else if (status > Http::scNone && status < Http::scInternalServerError) {
405 // forward response from origin
406 http->logType = LOG_TCP_REFRESH_MODIFIED;
407 debugs(88, 3, "handleIMSReply: origin replied " << status << ", replacing existing entry and forwarding to client");
408 sendClientUpstreamResponse();
409 }
410
411 // origin replied with an error
412 else if (http->request->flags.failOnValidationError) {
413 http->logType = LOG_TCP_REFRESH_FAIL_ERR;
414 debugs(88, 3, "handleIMSReply: origin replied with error " << status <<
415 ", forwarding to client due to fail_on_validation_err");
416 sendClientUpstreamResponse();
417 } else {
418 // ignore and let client have old entry
419 http->logType = LOG_TCP_REFRESH_FAIL_OLD;
420 debugs(88, 3, "handleIMSReply: origin replied with error " <<
421 status << ", sending old entry (" << old_rep->sline.status() << ") to client");
422 sendClientOldEntry();
423 }
424 }
425
426 SQUIDCEXTERN CSR clientGetMoreData;
427 SQUIDCEXTERN CSD clientReplyDetach;
428
429 /**
430 * clientReplyContext::cacheHit Should only be called until the HTTP reply headers
431 * have been parsed. Normally this should be a single call, but
432 * it might take more than one. As soon as we have the headers,
433 * we hand off to clientSendMoreData, processExpired, or
434 * processMiss.
435 */
436 void
437 clientReplyContext::CacheHit(void *data, StoreIOBuffer result)
438 {
439 clientReplyContext *context = (clientReplyContext *)data;
440 context->cacheHit(result);
441 }
442
443 /**
444 * Process a possible cache HIT.
445 */
446 void
447 clientReplyContext::cacheHit(StoreIOBuffer result)
448 {
449 /** Ignore if the HIT object is being deleted. */
450 if (deleting) {
451 debugs(88, 3, "HIT object being deleted. Ignore the HIT.");
452 return;
453 }
454
455 StoreEntry *e = http->storeEntry();
456
457 HttpRequest *r = http->request;
458
459 debugs(88, 3, "clientCacheHit: " << http->uri << ", " << result.length << " bytes");
460
461 if (http->storeEntry() == NULL) {
462 debugs(88, 3, "clientCacheHit: request aborted");
463 return;
464 } else if (result.flags.error) {
465 /* swap in failure */
466 debugs(88, 3, "clientCacheHit: swapin failure for " << http->uri);
467 http->logType = LOG_TCP_SWAPFAIL_MISS;
468 removeClientStoreReference(&sc, http);
469 processMiss();
470 return;
471 }
472
473 if (result.length == 0) {
474 debugs(88, 5, "store IO buffer has no content. MISS");
475 /* the store couldn't get enough data from the file for us to id the
476 * object
477 */
478 /* treat as a miss */
479 http->logType = LOG_TCP_MISS;
480 processMiss();
481 return;
482 }
483
484 assert(!EBIT_TEST(e->flags, ENTRY_ABORTED));
485 /* update size of the request */
486 reqsize = result.length + reqofs;
487
488 /*
489 * Got the headers, now grok them
490 */
491 assert(http->logType == LOG_TCP_HIT);
492
493 if (strcmp(e->mem_obj->storeId(), http->request->storeId()) != 0) {
494 debugs(33, DBG_IMPORTANT, "clientProcessHit: URL mismatch, '" << e->mem_obj->storeId() << "' != '" << http->request->storeId() << "'");
495 http->logType = LOG_TCP_MISS; // we lack a more precise LOG_*_MISS code
496 processMiss();
497 return;
498 }
499
500 switch (varyEvaluateMatch(e, r)) {
501
502 case VARY_NONE:
503 /* No variance detected. Continue as normal */
504 break;
505
506 case VARY_MATCH:
507 /* This is the correct entity for this request. Continue */
508 debugs(88, 2, "clientProcessHit: Vary MATCH!");
509 break;
510
511 case VARY_OTHER:
512 /* This is not the correct entity for this request. We need
513 * to requery the cache.
514 */
515 removeClientStoreReference(&sc, http);
516 e = NULL;
517 /* Note: varyEvalyateMatch updates the request with vary information
518 * so we only get here once. (it also takes care of cancelling loops)
519 */
520 debugs(88, 2, "clientProcessHit: Vary detected!");
521 clientGetMoreData(ourNode, http);
522 return;
523
524 case VARY_CANCEL:
525 /* varyEvaluateMatch found a object loop. Process as miss */
526 debugs(88, DBG_IMPORTANT, "clientProcessHit: Vary object loop!");
527 http->logType = LOG_TCP_MISS; // we lack a more precise LOG_*_MISS code
528 processMiss();
529 return;
530 }
531
532 if (r->method == Http::METHOD_PURGE) {
533 debugs(88, 5, "PURGE gets a HIT");
534 removeClientStoreReference(&sc, http);
535 e = NULL;
536 purgeRequest();
537 return;
538 }
539
540 if (e->checkNegativeHit() && !r->flags.noCacheHack()) {
541 debugs(88, 5, "negative-HIT");
542 http->logType = LOG_TCP_NEGATIVE_HIT;
543 sendMoreData(result);
544 } else if (blockedHit()) {
545 debugs(88, 5, "send_hit forces a MISS");
546 http->logType = LOG_TCP_MISS;
547 processMiss();
548 return;
549 } else if (!http->flags.internal && refreshCheckHTTP(e, r)) {
550 debugs(88, 5, "clientCacheHit: in refreshCheck() block");
551 /*
552 * We hold a stale copy; it needs to be validated
553 */
554 /*
555 * The 'needValidation' flag is used to prevent forwarding
556 * loops between siblings. If our copy of the object is stale,
557 * then we should probably only use parents for the validation
558 * request. Otherwise two siblings could generate a loop if
559 * both have a stale version of the object.
560 */
561 r->flags.needValidation = true;
562
563 if (e->lastmod < 0) {
564 debugs(88, 3, "validate HIT object? NO. Missing Last-Modified header. Do MISS.");
565 /*
566 * Previous reply didn't have a Last-Modified header,
567 * we cannot revalidate it.
568 */
569 http->logType = LOG_TCP_MISS;
570 processMiss();
571 } else if (r->flags.noCache) {
572 debugs(88, 3, "validate HIT object? NO. Client sent CC:no-cache. Do CLIENT_REFRESH_MISS");
573 /*
574 * This did not match a refresh pattern that overrides no-cache
575 * we should honour the client no-cache header.
576 */
577 http->logType = LOG_TCP_CLIENT_REFRESH_MISS;
578 processMiss();
579 } else if (r->protocol == AnyP::PROTO_HTTP) {
580 debugs(88, 3, "validate HIT object? YES.");
581 /*
582 * Object needs to be revalidated
583 * XXX This could apply to FTP as well, if Last-Modified is known.
584 */
585 processExpired();
586 } else {
587 debugs(88, 3, "validate HIT object? NO. Client protocol non-HTTP. Do MISS.");
588 /*
589 * We don't know how to re-validate other protocols. Handle
590 * them as if the object has expired.
591 */
592 http->logType = LOG_TCP_MISS;
593 processMiss();
594 }
595 } else if (r->conditional()) {
596 debugs(88, 5, "conditional HIT");
597 processConditional(result);
598 } else {
599 /*
600 * plain ol' cache hit
601 */
602 debugs(88, 5, "plain old HIT");
603
604 #if USE_DELAY_POOLS
605 if (e->store_status != STORE_OK)
606 http->logType = LOG_TCP_MISS;
607 else
608 #endif
609 if (e->mem_status == IN_MEMORY)
610 http->logType = LOG_TCP_MEM_HIT;
611 else if (Config.onoff.offline)
612 http->logType = LOG_TCP_OFFLINE_HIT;
613
614 sendMoreData(result);
615 }
616 }
617
618 /**
619 * Prepare to fetch the object as it's a cache miss of some kind.
620 */
621 void
622 clientReplyContext::processMiss()
623 {
624 char *url = http->uri;
625 HttpRequest *r = http->request;
626 ErrorState *err = NULL;
627 debugs(88, 4, "clientProcessMiss: '" << RequestMethodStr(r->method) << " " << url << "'");
628
629 /**
630 * We might have a left-over StoreEntry from a failed cache hit
631 * or IMS request.
632 */
633 if (http->storeEntry()) {
634 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_SPECIAL)) {
635 debugs(88, DBG_CRITICAL, "clientProcessMiss: miss on a special object (" << url << ").");
636 debugs(88, DBG_CRITICAL, "\tlog_type = " << LogTags_str[http->logType]);
637 http->storeEntry()->dump(1);
638 }
639
640 removeClientStoreReference(&sc, http);
641 }
642
643 /** Check if its a PURGE request to be actioned. */
644 if (r->method == Http::METHOD_PURGE) {
645 purgeRequest();
646 return;
647 }
648
649 /** Check if its an 'OTHER' request. Purge all cached entries if so and continue. */
650 if (r->method == Http::METHOD_OTHER) {
651 purgeAllCached();
652 }
653
654 /** Check if 'only-if-cached' flag is set. Action if so. */
655 if (http->onlyIfCached()) {
656 processOnlyIfCachedMiss();
657 return;
658 }
659
660 /// Deny loops
661 if (r->flags.loopDetected) {
662 http->al->http.code = Http::scForbidden;
663 err = clientBuildError(ERR_ACCESS_DENIED, Http::scForbidden, NULL, http->getConn()->clientConnection->remote, http->request);
664 createStoreEntry(r->method, RequestFlags());
665 errorAppendEntry(http->storeEntry(), err);
666 triggerInitialStoreRead();
667 return;
668 } else {
669 assert(http->out.offset == 0);
670 createStoreEntry(r->method, r->flags);
671 triggerInitialStoreRead();
672
673 if (http->redirect.status) {
674 HttpReply *rep = new HttpReply;
675 http->logType = LOG_TCP_REDIRECT;
676 http->storeEntry()->releaseRequest();
677 rep->redirect(http->redirect.status, http->redirect.location);
678 http->storeEntry()->replaceHttpReply(rep);
679 http->storeEntry()->complete();
680 return;
681 }
682
683 /** Check for internal requests. Update Protocol info if so. */
684 if (http->flags.internal)
685 r->protocol = AnyP::PROTO_INTERNAL;
686
687 assert(r->clientConnectionManager == http->getConn());
688
689 /** Start forwarding to get the new object from network */
690 Comm::ConnectionPointer conn = http->getConn() != NULL ? http->getConn()->clientConnection : NULL;
691 FwdState::Start(conn, http->storeEntry(), r, http->al);
692 }
693 }
694
695 /**
696 * client issued a request with an only-if-cached cache-control directive;
697 * we did not find a cached object that can be returned without
698 * contacting other servers;
699 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
700 */
701 void
702 clientReplyContext::processOnlyIfCachedMiss()
703 {
704 debugs(88, 4, "clientProcessOnlyIfCachedMiss: '" <<
705 RequestMethodStr(http->request->method) << " " << http->uri << "'");
706 http->al->http.code = Http::scGateway_Timeout;
707 ErrorState *err = clientBuildError(ERR_ONLY_IF_CACHED_MISS, Http::scGateway_Timeout, NULL,
708 http->getConn()->clientConnection->remote, http->request);
709 removeClientStoreReference(&sc, http);
710 startError(err);
711 }
712
713 /// process conditional request from client
714 void
715 clientReplyContext::processConditional(StoreIOBuffer &result)
716 {
717 StoreEntry *const e = http->storeEntry();
718
719 if (e->getReply()->sline.status() != Http::scOkay) {
720 debugs(88, 4, "clientReplyContext::processConditional: Reply code " <<
721 e->getReply()->sline.status() << " != 200");
722 http->logType = LOG_TCP_MISS;
723 processMiss();
724 return;
725 }
726
727 HttpRequest &r = *http->request;
728
729 if (r.header.has(HDR_IF_MATCH) && !e->hasIfMatchEtag(r)) {
730 // RFC 2616: reply with 412 Precondition Failed if If-Match did not match
731 sendPreconditionFailedError();
732 return;
733 }
734
735 bool matchedIfNoneMatch = false;
736 if (r.header.has(HDR_IF_NONE_MATCH)) {
737 if (!e->hasIfNoneMatchEtag(r)) {
738 // RFC 2616: ignore IMS if If-None-Match did not match
739 r.flags.ims = false;
740 r.ims = -1;
741 r.imslen = 0;
742 r.header.delById(HDR_IF_MODIFIED_SINCE);
743 http->logType = LOG_TCP_MISS;
744 sendMoreData(result);
745 return;
746 }
747
748 if (!r.flags.ims) {
749 // RFC 2616: if If-None-Match matched and there is no IMS,
750 // reply with 304 Not Modified or 412 Precondition Failed
751 sendNotModifiedOrPreconditionFailedError();
752 return;
753 }
754
755 // otherwise check IMS below to decide if we reply with 304 or 412
756 matchedIfNoneMatch = true;
757 }
758
759 if (r.flags.ims) {
760 // handle If-Modified-Since requests from the client
761 if (e->modifiedSince(&r)) {
762 http->logType = LOG_TCP_IMS_HIT;
763 sendMoreData(result);
764 return;
765 }
766
767 if (matchedIfNoneMatch) {
768 // If-None-Match matched, reply with 304 Not Modified or
769 // 412 Precondition Failed
770 sendNotModifiedOrPreconditionFailedError();
771 return;
772 }
773
774 // otherwise reply with 304 Not Modified
775 sendNotModified();
776 }
777 }
778
779 /// whether squid.conf send_hit prevents us from serving this hit
780 bool
781 clientReplyContext::blockedHit() const
782 {
783 if (!Config.accessList.sendHit)
784 return false; // hits are not blocked by default
785
786 if (http->flags.internal)
787 return false; // internal content "hits" cannot be blocked
788
789 if (const HttpReply *rep = http->storeEntry()->getReply()) {
790 std::auto_ptr<ACLFilledChecklist> chl(clientAclChecklistCreate(Config.accessList.sendHit, http));
791 chl->reply = const_cast<HttpReply*>(rep); // ACLChecklist API bug
792 HTTPMSGLOCK(chl->reply);
793 return chl->fastCheck() != ACCESS_ALLOWED; // when in doubt, block
794 }
795
796 // This does not happen, I hope, because we are called from CacheHit, which
797 // is called via a storeClientCopy() callback, and store should initialize
798 // the reply before calling that callback.
799 debugs(88, 3, "Missing reply!");
800 return false;
801 }
802
803 void
804 clientReplyContext::purgeRequestFindObjectToPurge()
805 {
806 /* Try to find a base entry */
807 http->flags.purging = true;
808 lookingforstore = 1;
809
810 // TODO: can we use purgeAllCached() here instead of doing the
811 // getPublicByRequestMethod() dance?
812 StoreEntry::getPublicByRequestMethod(this, http->request, Http::METHOD_GET);
813 }
814
815 // Purges all entries with a given url
816 // TODO: move to SideAgent parent, when we have one
817 /*
818 * We probably cannot purge Vary-affected responses because their MD5
819 * keys depend on vary headers.
820 */
821 void
822 purgeEntriesByUrl(HttpRequest * req, const char *url)
823 {
824 #if USE_HTCP
825 bool get_or_head_sent = false;
826 #endif
827
828 for (HttpRequestMethod m(Http::METHOD_NONE); m != Http::METHOD_ENUM_END; ++m) {
829 if (m.respMaybeCacheable()) {
830 if (StoreEntry *entry = storeGetPublic(url, m)) {
831 debugs(88, 5, "purging " << *entry << ' ' << RequestMethodStr(m) << ' ' << url);
832 #if USE_HTCP
833 neighborsHtcpClear(entry, url, req, m, HTCP_CLR_INVALIDATION);
834 if (m == Http::METHOD_GET || m == Http::METHOD_HEAD) {
835 get_or_head_sent = true;
836 }
837 #endif
838 entry->release();
839 }
840 }
841 }
842
843 #if USE_HTCP
844 if (!get_or_head_sent) {
845 neighborsHtcpClear(NULL, url, req, HttpRequestMethod(Http::METHOD_GET), HTCP_CLR_INVALIDATION);
846 }
847 #endif
848 }
849
850 void
851 clientReplyContext::purgeAllCached()
852 {
853 const char *url = urlCanonical(http->request);
854 purgeEntriesByUrl(http->request, url);
855 }
856
857 void
858 clientReplyContext::created(StoreEntry *newEntry)
859 {
860 if (lookingforstore == 1)
861 purgeFoundGet(newEntry);
862 else if (lookingforstore == 2)
863 purgeFoundHead(newEntry);
864 else if (lookingforstore == 3)
865 purgeDoPurgeGet(newEntry);
866 else if (lookingforstore == 4)
867 purgeDoPurgeHead(newEntry);
868 else if (lookingforstore == 5)
869 identifyFoundObject(newEntry);
870 }
871
872 void
873 clientReplyContext::purgeFoundGet(StoreEntry *newEntry)
874 {
875 if (newEntry->isNull()) {
876 lookingforstore = 2;
877 StoreEntry::getPublicByRequestMethod(this, http->request, Http::METHOD_HEAD);
878 } else
879 purgeFoundObject (newEntry);
880 }
881
882 void
883 clientReplyContext::purgeFoundHead(StoreEntry *newEntry)
884 {
885 if (newEntry->isNull())
886 purgeDoMissPurge();
887 else
888 purgeFoundObject (newEntry);
889 }
890
891 void
892 clientReplyContext::purgeFoundObject(StoreEntry *entry)
893 {
894 assert (entry && !entry->isNull());
895
896 if (EBIT_TEST(entry->flags, ENTRY_SPECIAL)) {
897 http->logType = LOG_TCP_DENIED;
898 ErrorState *err = clientBuildError(ERR_ACCESS_DENIED, Http::scForbidden, NULL,
899 http->getConn()->clientConnection->remote, http->request);
900 startError(err);
901 return; // XXX: leaking unused entry if some store does not keep it
902 }
903
904 StoreIOBuffer localTempBuffer;
905 /* Swap in the metadata */
906 http->storeEntry(entry);
907
908 http->storeEntry()->lock("clientReplyContext::purgeFoundObject");
909 http->storeEntry()->createMemObject(storeId(), http->log_uri,
910 http->request->method);
911
912 sc = storeClientListAdd(http->storeEntry(), this);
913
914 http->logType = LOG_TCP_HIT;
915
916 reqofs = 0;
917
918 localTempBuffer.offset = http->out.offset;
919
920 localTempBuffer.length = next()->readBuffer.length;
921
922 localTempBuffer.data = next()->readBuffer.data;
923
924 storeClientCopy(sc, http->storeEntry(),
925 localTempBuffer, CacheHit, this);
926 }
927
928 void
929 clientReplyContext::purgeRequest()
930 {
931 debugs(88, 3, "Config2.onoff.enable_purge = " <<
932 Config2.onoff.enable_purge);
933
934 if (!Config2.onoff.enable_purge) {
935 http->logType = LOG_TCP_DENIED;
936 ErrorState *err = clientBuildError(ERR_ACCESS_DENIED, Http::scForbidden, NULL, http->getConn()->clientConnection->remote, http->request);
937 startError(err);
938 return;
939 }
940
941 /* Release both IP cache */
942 ipcacheInvalidate(http->request->GetHost());
943
944 if (!http->flags.purging)
945 purgeRequestFindObjectToPurge();
946 else
947 purgeDoMissPurge();
948 }
949
950 void
951 clientReplyContext::purgeDoMissPurge()
952 {
953 http->logType = LOG_TCP_MISS;
954 lookingforstore = 3;
955 StoreEntry::getPublicByRequestMethod(this,http->request, Http::METHOD_GET);
956 }
957
958 void
959 clientReplyContext::purgeDoPurgeGet(StoreEntry *newEntry)
960 {
961 assert (newEntry);
962 /* Move to new() when that is created */
963 purgeStatus = Http::scNotFound;
964
965 if (!newEntry->isNull()) {
966 /* Release the cached URI */
967 debugs(88, 4, "clientPurgeRequest: GET '" << newEntry->url() << "'" );
968 #if USE_HTCP
969 neighborsHtcpClear(newEntry, NULL, http->request, HttpRequestMethod(Http::METHOD_GET), HTCP_CLR_PURGE);
970 #endif
971 newEntry->release();
972 purgeStatus = Http::scOkay;
973 }
974
975 lookingforstore = 4;
976 StoreEntry::getPublicByRequestMethod(this, http->request, Http::METHOD_HEAD);
977 }
978
979 void
980 clientReplyContext::purgeDoPurgeHead(StoreEntry *newEntry)
981 {
982 if (newEntry && !newEntry->isNull()) {
983 debugs(88, 4, "clientPurgeRequest: HEAD '" << newEntry->url() << "'" );
984 #if USE_HTCP
985 neighborsHtcpClear(newEntry, NULL, http->request, HttpRequestMethod(Http::METHOD_HEAD), HTCP_CLR_PURGE);
986 #endif
987 newEntry->release();
988 purgeStatus = Http::scOkay;
989 }
990
991 /* And for Vary, release the base URI if none of the headers was included in the request */
992
993 if (http->request->vary_headers
994 && !strstr(http->request->vary_headers, "=")) {
995 StoreEntry *entry = storeGetPublic(urlCanonical(http->request), Http::METHOD_GET);
996
997 if (entry) {
998 debugs(88, 4, "clientPurgeRequest: Vary GET '" << entry->url() << "'" );
999 #if USE_HTCP
1000 neighborsHtcpClear(entry, NULL, http->request, HttpRequestMethod(Http::METHOD_GET), HTCP_CLR_PURGE);
1001 #endif
1002 entry->release();
1003 purgeStatus = Http::scOkay;
1004 }
1005
1006 entry = storeGetPublic(urlCanonical(http->request), Http::METHOD_HEAD);
1007
1008 if (entry) {
1009 debugs(88, 4, "clientPurgeRequest: Vary HEAD '" << entry->url() << "'" );
1010 #if USE_HTCP
1011 neighborsHtcpClear(entry, NULL, http->request, HttpRequestMethod(Http::METHOD_HEAD), HTCP_CLR_PURGE);
1012 #endif
1013 entry->release();
1014 purgeStatus = Http::scOkay;
1015 }
1016 }
1017
1018 /*
1019 * Make a new entry to hold the reply to be written
1020 * to the client.
1021 */
1022 /* FIXME: This doesn't need to go through the store. Simply
1023 * push down the client chain
1024 */
1025 createStoreEntry(http->request->method, RequestFlags());
1026
1027 triggerInitialStoreRead();
1028
1029 HttpReply *rep = new HttpReply;
1030 rep->setHeaders(purgeStatus, NULL, NULL, 0, 0, -1);
1031 http->storeEntry()->replaceHttpReply(rep);
1032 http->storeEntry()->complete();
1033 }
1034
1035 void
1036 clientReplyContext::traceReply(clientStreamNode * node)
1037 {
1038 clientStreamNode *nextNode = (clientStreamNode *)node->node.next->data;
1039 StoreIOBuffer localTempBuffer;
1040 createStoreEntry(http->request->method, RequestFlags());
1041 localTempBuffer.offset = nextNode->readBuffer.offset + headers_sz;
1042 localTempBuffer.length = nextNode->readBuffer.length;
1043 localTempBuffer.data = nextNode->readBuffer.data;
1044 storeClientCopy(sc, http->storeEntry(),
1045 localTempBuffer, SendMoreData, this);
1046 http->storeEntry()->releaseRequest();
1047 http->storeEntry()->buffer();
1048 HttpReply *rep = new HttpReply;
1049 rep->setHeaders(Http::scOkay, NULL, "text/plain", http->request->prefixLen(), 0, squid_curtime);
1050 http->storeEntry()->replaceHttpReply(rep);
1051 http->request->swapOut(http->storeEntry());
1052 http->storeEntry()->complete();
1053 }
1054
1055 #define SENDING_BODY 0
1056 #define SENDING_HDRSONLY 1
1057 int
1058 clientReplyContext::checkTransferDone()
1059 {
1060 StoreEntry *entry = http->storeEntry();
1061
1062 if (entry == NULL)
1063 return 0;
1064
1065 /*
1066 * For now, 'done_copying' is used for special cases like
1067 * Range and HEAD requests.
1068 */
1069 if (http->flags.done_copying)
1070 return 1;
1071
1072 if (http->request->flags.chunkedReply && !flags.complete) {
1073 // last-chunk was not sent
1074 return 0;
1075 }
1076
1077 /*
1078 * Handle STORE_OK objects.
1079 * objectLen(entry) will be set proprely.
1080 * RC: Does objectLen(entry) include the Headers?
1081 * RC: Yes.
1082 */
1083 if (entry->store_status == STORE_OK) {
1084 return storeOKTransferDone();
1085 } else {
1086 return storeNotOKTransferDone();
1087 }
1088 }
1089
1090 int
1091 clientReplyContext::storeOKTransferDone() const
1092 {
1093 assert(http->storeEntry()->objectLen() >= 0);
1094 assert(http->storeEntry()->objectLen() >= headers_sz);
1095 if (http->out.offset >= http->storeEntry()->objectLen() - headers_sz) {
1096 debugs(88,3,HERE << "storeOKTransferDone " <<
1097 " out.offset=" << http->out.offset <<
1098 " objectLen()=" << http->storeEntry()->objectLen() <<
1099 " headers_sz=" << headers_sz);
1100 return 1;
1101 }
1102
1103 return 0;
1104 }
1105
1106 int
1107 clientReplyContext::storeNotOKTransferDone() const
1108 {
1109 /*
1110 * Now, handle STORE_PENDING objects
1111 */
1112 MemObject *mem = http->storeEntry()->mem_obj;
1113 assert(mem != NULL);
1114 assert(http->request != NULL);
1115 /* mem->reply was wrong because it uses the UPSTREAM header length!!! */
1116 HttpReply const *curReply = mem->getReply();
1117
1118 if (headers_sz == 0)
1119 /* haven't found end of headers yet */
1120 return 0;
1121
1122 /*
1123 * Figure out how much data we are supposed to send.
1124 * If we are sending a body and we don't have a content-length,
1125 * then we must wait for the object to become STORE_OK.
1126 */
1127 if (curReply->content_length < 0)
1128 return 0;
1129
1130 int64_t expectedLength = curReply->content_length + http->out.headers_sz;
1131
1132 if (http->out.size < expectedLength)
1133 return 0;
1134 else {
1135 debugs(88,3,HERE << "storeNotOKTransferDone " <<
1136 " out.size=" << http->out.size <<
1137 " expectedLength=" << expectedLength);
1138 return 1;
1139 }
1140 }
1141
1142 /* A write has completed, what is the next status based on the
1143 * canonical request data?
1144 * 1 something is wrong
1145 * 0 nothing is wrong.
1146 *
1147 */
1148 int
1149 clientHttpRequestStatus(int fd, ClientHttpRequest const *http)
1150 {
1151 #if SIZEOF_INT64_T == 4
1152 if (http->out.size > 0x7FFF0000) {
1153 debugs(88, DBG_IMPORTANT, "WARNING: closing FD " << fd << " to prevent out.size counter overflow");
1154 debugs(88, DBG_IMPORTANT, "\tclient " << http->getConn()->peer);
1155 debugs(88, DBG_IMPORTANT, "\treceived " << http->out.size << " bytes");
1156 debugs(88, DBG_IMPORTANT, "\tURI " << http->log_uri);
1157 return 1;
1158 }
1159
1160 if (http->out.offset > 0x7FFF0000) {
1161 debugs(88, DBG_IMPORTANT, "WARNING: closing FD " << fd < " to prevent out.offset counter overflow");
1162 debugs(88, DBG_IMPORTANT, "\tclient " << http->getConn()->peer);
1163 debugs(88, DBG_IMPORTANT, "\treceived " << http->out.size << " bytes, offset " << http->out.offset);
1164 debugs(88, DBG_IMPORTANT, "\tURI " << http->log_uri);
1165 return 1;
1166 }
1167
1168 #endif
1169 return 0;
1170 }
1171
1172 /* Preconditions:
1173 * *http is a valid structure.
1174 * fd is either -1, or an open fd.
1175 *
1176 * TODO: enumify this
1177 *
1178 * This function is used by any http request sink, to determine the status
1179 * of the object.
1180 */
1181 clientStream_status_t
1182 clientReplyStatus(clientStreamNode * aNode, ClientHttpRequest * http)
1183 {
1184 clientReplyContext *context = dynamic_cast<clientReplyContext *>(aNode->data.getRaw());
1185 assert (context);
1186 assert (context->http == http);
1187 return context->replyStatus();
1188 }
1189
1190 clientStream_status_t
1191 clientReplyContext::replyStatus()
1192 {
1193 int done;
1194 /* Here because lower nodes don't need it */
1195
1196 if (http->storeEntry() == NULL) {
1197 debugs(88, 5, "clientReplyStatus: no storeEntry");
1198 return STREAM_FAILED; /* yuck, but what can we do? */
1199 }
1200
1201 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED)) {
1202 /* TODO: Could upstream read errors (result.flags.error) be
1203 * lost, and result in undersize requests being considered
1204 * complete. Should we tcp reset such connections ?
1205 */
1206 debugs(88, 5, "clientReplyStatus: aborted storeEntry");
1207 return STREAM_FAILED;
1208 }
1209
1210 if ((done = checkTransferDone()) != 0 || flags.complete) {
1211 debugs(88, 5, "clientReplyStatus: transfer is DONE: " << done << flags.complete);
1212 /* Ok we're finished, but how? */
1213
1214 const int64_t expectedBodySize =
1215 http->storeEntry()->getReply()->bodySize(http->request->method);
1216 if (!http->request->flags.proxyKeepalive && expectedBodySize < 0) {
1217 debugs(88, 5, "clientReplyStatus: closing, content_length < 0");
1218 return STREAM_FAILED;
1219 }
1220
1221 if (!done) {
1222 debugs(88, 5, "clientReplyStatus: closing, !done, but read 0 bytes");
1223 return STREAM_FAILED;
1224 }
1225
1226 if (expectedBodySize >= 0 && !http->gotEnough()) {
1227 debugs(88, 5, "clientReplyStatus: client didn't get all it expected");
1228 return STREAM_UNPLANNED_COMPLETE;
1229 }
1230
1231 debugs(88, 5, "clientReplyStatus: stream complete; keepalive=" <<
1232 http->request->flags.proxyKeepalive);
1233 return STREAM_COMPLETE;
1234 }
1235
1236 // XXX: Should this be checked earlier? We could return above w/o checking.
1237 if (reply->receivedBodyTooLarge(*http->request, http->out.offset - 4096)) {
1238 /* 4096 is a margin for the HTTP headers included in out.offset */
1239 debugs(88, 5, "clientReplyStatus: client reply body is too large");
1240 return STREAM_FAILED;
1241 }
1242
1243 return STREAM_NONE;
1244 }
1245
1246 /* Responses with no body will not have a content-type header,
1247 * which breaks the rep_mime_type acl, which
1248 * coincidentally, is the most common acl for reply access lists.
1249 * A better long term fix for this is to allow acl matchs on the various
1250 * status codes, and then supply a default ruleset that puts these
1251 * codes before any user defines access entries. That way the user
1252 * can choose to block these responses where appropriate, but won't get
1253 * mysterious breakages.
1254 */
1255 bool
1256 clientReplyContext::alwaysAllowResponse(Http::StatusCode sline) const
1257 {
1258 bool result;
1259
1260 switch (sline) {
1261
1262 case Http::scContinue:
1263
1264 case Http::scSwitchingProtocols:
1265
1266 case Http::scProcessing:
1267
1268 case Http::scNoContent:
1269
1270 case Http::scNotModified:
1271 result = true;
1272 break;
1273
1274 default:
1275 result = false;
1276 }
1277
1278 return result;
1279 }
1280
1281 /**
1282 * Generate the reply headers sent to client.
1283 *
1284 * Filters out unwanted entries and hop-by-hop from original reply header
1285 * then adds extra entries if we have more info than origin server
1286 * then adds Squid specific entries
1287 */
1288 void
1289 clientReplyContext::buildReplyHeader()
1290 {
1291 HttpHeader *hdr = &reply->header;
1292 int is_hit = logTypeIsATcpHit(http->logType);
1293 HttpRequest *request = http->request;
1294 #if DONT_FILTER_THESE
1295 /* but you might want to if you run Squid as an HTTP accelerator */
1296 /* hdr->delById(HDR_ACCEPT_RANGES); */
1297 hdr->delById(HDR_ETAG);
1298 #endif
1299
1300 if (is_hit)
1301 hdr->delById(HDR_SET_COOKIE);
1302 // TODO: RFC 2965 : Must honour Cache-Control: no-cache="set-cookie2" and remove header.
1303
1304 // if there is not configured a peer proxy with login=PASS or login=PASSTHRU option enabled
1305 // remove the Proxy-Authenticate header
1306 if ( !request->peer_login || (strcmp(request->peer_login,"PASS") != 0 && strcmp(request->peer_login,"PASSTHRU") != 0))
1307 reply->header.delById(HDR_PROXY_AUTHENTICATE);
1308
1309 reply->header.removeHopByHopEntries();
1310
1311 // if (request->range)
1312 // clientBuildRangeHeader(http, reply);
1313
1314 /*
1315 * Add a estimated Age header on cache hits.
1316 */
1317 if (is_hit) {
1318 /*
1319 * Remove any existing Age header sent by upstream caches
1320 * (note that the existing header is passed along unmodified
1321 * on cache misses)
1322 */
1323 hdr->delById(HDR_AGE);
1324 /*
1325 * This adds the calculated object age. Note that the details of the
1326 * age calculation is performed by adjusting the timestamp in
1327 * StoreEntry::timestampsSet(), not here.
1328 */
1329 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_SPECIAL)) {
1330 hdr->delById(HDR_DATE);
1331 hdr->insertTime(HDR_DATE, squid_curtime);
1332 } else if (http->getConn() && http->getConn()->port->actAsOrigin) {
1333 // Swap the Date: header to current time if we are simulating an origin
1334 HttpHeaderEntry *h = hdr->findEntry(HDR_DATE);
1335 if (h)
1336 hdr->putExt("X-Origin-Date", h->value.termedBuf());
1337 hdr->delById(HDR_DATE);
1338 hdr->insertTime(HDR_DATE, squid_curtime);
1339 h = hdr->findEntry(HDR_EXPIRES);
1340 if (h && http->storeEntry()->expires >= 0) {
1341 hdr->putExt("X-Origin-Expires", h->value.termedBuf());
1342 hdr->delById(HDR_EXPIRES);
1343 hdr->insertTime(HDR_EXPIRES, squid_curtime + http->storeEntry()->expires - http->storeEntry()->timestamp);
1344 }
1345 if (http->storeEntry()->timestamp <= squid_curtime) {
1346 // put X-Cache-Age: instead of Age:
1347 char age[64];
1348 snprintf(age, sizeof(age), "%ld", (long int) squid_curtime - http->storeEntry()->timestamp);
1349 hdr->putExt("X-Cache-Age", age);
1350 }
1351 } else if (http->storeEntry()->timestamp <= squid_curtime) {
1352 hdr->putInt(HDR_AGE,
1353 squid_curtime - http->storeEntry()->timestamp);
1354 /* Signal old objects. NB: rfc 2616 is not clear,
1355 * by implication, on whether we should do this to all
1356 * responses, or only cache hits.
1357 * 14.46 states it ONLY applys for heuristically caclulated
1358 * freshness values, 13.2.4 doesn't specify the same limitation.
1359 * We interpret RFC 2616 under the combination.
1360 */
1361 /* TODO: if maxage or s-maxage is present, don't do this */
1362
1363 if (squid_curtime - http->storeEntry()->timestamp >= 86400) {
1364 char tbuf[512];
1365 snprintf (tbuf, sizeof(tbuf), "%s %s %s",
1366 "113", ThisCache,
1367 "This cache hit is still fresh and more than 1 day old");
1368 hdr->putStr(HDR_WARNING, tbuf);
1369 }
1370 }
1371 }
1372
1373 /* RFC 2616: Section 14.18
1374 *
1375 * Add a Date: header if missing.
1376 * We have access to a clock therefore are required to amend any shortcoming in servers.
1377 *
1378 * NP: done after Age: to prevent ENTRY_SPECIAL double-handling this header.
1379 */
1380 if ( !hdr->has(HDR_DATE) ) {
1381 if (!http->storeEntry())
1382 hdr->insertTime(HDR_DATE, squid_curtime);
1383 else if (http->storeEntry()->timestamp > 0)
1384 hdr->insertTime(HDR_DATE, http->storeEntry()->timestamp);
1385 else {
1386 debugs(88,DBG_IMPORTANT,"BUG 3279: HTTP reply without Date:");
1387 /* dump something useful about the problem */
1388 http->storeEntry()->dump(DBG_IMPORTANT);
1389 }
1390 }
1391
1392 // add Warnings required by RFC 2616 if serving a stale hit
1393 if (http->request->flags.staleIfHit && logTypeIsATcpHit(http->logType)) {
1394 hdr->putWarning(110, "Response is stale");
1395 if (http->request->flags.needValidation)
1396 hdr->putWarning(111, "Revalidation failed");
1397 }
1398
1399 /* Filter unproxyable authentication types */
1400 if (http->logType != LOG_TCP_DENIED &&
1401 hdr->has(HDR_WWW_AUTHENTICATE)) {
1402 HttpHeaderPos pos = HttpHeaderInitPos;
1403 HttpHeaderEntry *e;
1404
1405 int connection_auth_blocked = 0;
1406 while ((e = hdr->getEntry(&pos))) {
1407 if (e->id == HDR_WWW_AUTHENTICATE) {
1408 const char *value = e->value.rawBuf();
1409
1410 if ((strncasecmp(value, "NTLM", 4) == 0 &&
1411 (value[4] == '\0' || value[4] == ' '))
1412 ||
1413 (strncasecmp(value, "Negotiate", 9) == 0 &&
1414 (value[9] == '\0' || value[9] == ' '))
1415 ||
1416 (strncasecmp(value, "Kerberos", 8) == 0 &&
1417 (value[8] == '\0' || value[8] == ' '))) {
1418 if (request->flags.connectionAuthDisabled) {
1419 hdr->delAt(pos, connection_auth_blocked);
1420 continue;
1421 }
1422 request->flags.mustKeepalive = true;
1423 if (!request->flags.accelerated && !request->flags.intercepted) {
1424 httpHeaderPutStrf(hdr, HDR_PROXY_SUPPORT, "Session-Based-Authentication");
1425 /*
1426 We send "[Proxy-]Connection: Proxy-Support" header to mark
1427 Proxy-Support as a hop-by-hop header for intermediaries that do not
1428 understand the semantics of this header. The RFC should have included
1429 this recommendation.
1430 */
1431 httpHeaderPutStrf(hdr, HDR_CONNECTION, "Proxy-support");
1432 }
1433 break;
1434 }
1435 }
1436 }
1437
1438 if (connection_auth_blocked)
1439 hdr->refreshMask();
1440 }
1441
1442 #if USE_AUTH
1443 /* Handle authentication headers */
1444 if (http->logType == LOG_TCP_DENIED &&
1445 ( reply->sline.status() == Http::scProxyAuthenticationRequired ||
1446 reply->sline.status() == Http::scUnauthorized)
1447 ) {
1448 /* Add authentication header */
1449 /*! \todo alter errorstate to be accel on|off aware. The 0 on the next line
1450 * depends on authenticate behaviour: all schemes to date send no extra
1451 * data on 407/401 responses, and do not check the accel state on 401/407
1452 * responses
1453 */
1454 authenticateFixHeader(reply, request->auth_user_request, request, 0, 1);
1455 } else if (request->auth_user_request != NULL)
1456 authenticateFixHeader(reply, request->auth_user_request, request, http->flags.accel, 0);
1457 #endif
1458
1459 /* Append X-Cache */
1460 httpHeaderPutStrf(hdr, HDR_X_CACHE, "%s from %s",
1461 is_hit ? "HIT" : "MISS", getMyHostname());
1462
1463 #if USE_CACHE_DIGESTS
1464 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1465 httpHeaderPutStrf(hdr, HDR_X_CACHE_LOOKUP, "%s from %s:%d",
1466 lookup_type ? lookup_type : "NONE",
1467 getMyHostname(), getMyPort());
1468
1469 #endif
1470
1471 const bool maySendChunkedReply = !request->multipartRangeRequest() &&
1472 reply->sline.protocol == AnyP::PROTO_HTTP && // response is HTTP
1473 (request->http_ver >= Http::ProtocolVersion(1, 1));
1474
1475 /* Check whether we should send keep-alive */
1476 if (!Config.onoff.error_pconns && reply->sline.status() >= 400 && !request->flags.mustKeepalive) {
1477 debugs(33, 3, "clientBuildReplyHeader: Error, don't keep-alive");
1478 request->flags.proxyKeepalive = false;
1479 } else if (!Config.onoff.client_pconns && !request->flags.mustKeepalive) {
1480 debugs(33, 2, "clientBuildReplyHeader: Connection Keep-Alive not requested by admin or client");
1481 request->flags.proxyKeepalive = false;
1482 } else if (request->flags.proxyKeepalive && shutting_down) {
1483 debugs(88, 3, "clientBuildReplyHeader: Shutting down, don't keep-alive.");
1484 request->flags.proxyKeepalive = false;
1485 } else if (request->flags.connectionAuth && !reply->keep_alive) {
1486 debugs(33, 2, "clientBuildReplyHeader: Connection oriented auth but server side non-persistent");
1487 request->flags.proxyKeepalive = false;
1488 } else if (reply->bodySize(request->method) < 0 && !maySendChunkedReply) {
1489 debugs(88, 3, "clientBuildReplyHeader: can't keep-alive, unknown body size" );
1490 request->flags.proxyKeepalive = false;
1491 } else if (fdUsageHigh()&& !request->flags.mustKeepalive) {
1492 debugs(88, 3, "clientBuildReplyHeader: Not many unused FDs, can't keep-alive");
1493 request->flags.proxyKeepalive = false;
1494 } else if (request->flags.sslBumped && !reply->persistent()) {
1495 // We do not really have to close, but we pretend we are a tunnel.
1496 debugs(88, 3, "clientBuildReplyHeader: bumped reply forces close");
1497 request->flags.proxyKeepalive = false;
1498 } else if (request->pinnedConnection() && !reply->persistent()) {
1499 // The peer wants to close the pinned connection
1500 debugs(88, 3, "pinned reply forces close");
1501 request->flags.proxyKeepalive = false;
1502 }
1503
1504 // Decide if we send chunked reply
1505 if (maySendChunkedReply &&
1506 request->flags.proxyKeepalive &&
1507 reply->bodySize(request->method) < 0) {
1508 debugs(88, 3, "clientBuildReplyHeader: chunked reply");
1509 request->flags.chunkedReply = true;
1510 hdr->putStr(HDR_TRANSFER_ENCODING, "chunked");
1511 }
1512
1513 /* Append VIA */
1514 if (Config.onoff.via) {
1515 LOCAL_ARRAY(char, bbuf, MAX_URL + 32);
1516 String strVia;
1517 hdr->getList(HDR_VIA, &strVia);
1518 snprintf(bbuf, MAX_URL + 32, "%d.%d %s",
1519 reply->sline.version.major,
1520 reply->sline.version.minor,
1521 ThisCache);
1522 strListAdd(&strVia, bbuf, ',');
1523 hdr->delById(HDR_VIA);
1524 hdr->putStr(HDR_VIA, strVia.termedBuf());
1525 }
1526 /* Signal keep-alive or close explicitly */
1527 hdr->putStr(HDR_CONNECTION, request->flags.proxyKeepalive ? "keep-alive" : "close");
1528
1529 #if ADD_X_REQUEST_URI
1530 /*
1531 * Knowing the URI of the request is useful when debugging persistent
1532 * connections in a client; we cannot guarantee the order of http headers,
1533 * but X-Request-URI is likely to be the very last header to ease use from a
1534 * debugger [hdr->entries.count-1].
1535 */
1536 hdr->putStr(HDR_X_REQUEST_URI,
1537 http->memOjbect()->url ? http->memObject()->url : http->uri);
1538
1539 #endif
1540
1541 /* Surrogate-Control requires Surrogate-Capability from upstream to pass on */
1542 if ( hdr->has(HDR_SURROGATE_CONTROL) ) {
1543 if (!request->header.has(HDR_SURROGATE_CAPABILITY)) {
1544 hdr->delById(HDR_SURROGATE_CONTROL);
1545 }
1546 /* TODO: else case: drop any controls intended specifically for our surrogate ID */
1547 }
1548
1549 httpHdrMangleList(hdr, request, ROR_REPLY);
1550 }
1551
1552 void
1553 clientReplyContext::cloneReply()
1554 {
1555 assert(reply == NULL);
1556
1557 reply = http->storeEntry()->getReply()->clone();
1558 HTTPMSGLOCK(reply);
1559
1560 if (reply->sline.protocol == AnyP::PROTO_HTTP) {
1561 /* RFC 2616 requires us to advertise our 1.1 version (but only on real HTTP traffic) */
1562 reply->sline.version = Http::ProtocolVersion(1,1);
1563 }
1564
1565 /* do header conversions */
1566 buildReplyHeader();
1567 }
1568
1569 /// Safely disposes of an entry pointing to a cache hit that we do not want.
1570 /// We cannot just ignore the entry because it may be locking or otherwise
1571 /// holding an associated cache resource of some sort.
1572 void
1573 clientReplyContext::forgetHit()
1574 {
1575 StoreEntry *e = http->storeEntry();
1576 assert(e); // or we are not dealing with a hit
1577 // We probably have not locked the entry earlier, unfortunately. We lock it
1578 // now so that we can unlock two lines later (and trigger cleanup).
1579 // Ideally, ClientHttpRequest::storeEntry() should lock/unlock, but it is
1580 // used so inconsistently that simply adding locking there leads to bugs.
1581 e->lock("clientReplyContext::forgetHit");
1582 http->storeEntry(NULL);
1583 e->unlock("clientReplyContext::forgetHit"); // may delete e
1584 }
1585
1586 void
1587 clientReplyContext::identifyStoreObject()
1588 {
1589 HttpRequest *r = http->request;
1590
1591 if (r->flags.cachable || r->flags.internal) {
1592 lookingforstore = 5;
1593 StoreEntry::getPublicByRequest (this, r);
1594 } else {
1595 identifyFoundObject (NullStoreEntry::getInstance());
1596 }
1597 }
1598
1599 /**
1600 * Check state of the current StoreEntry object.
1601 * to see if we can determine the final status of the request.
1602 */
1603 void
1604 clientReplyContext::identifyFoundObject(StoreEntry *newEntry)
1605 {
1606 StoreEntry *e = newEntry;
1607 HttpRequest *r = http->request;
1608
1609 /** \li If the entry received isNull() then we ignore it. */
1610 if (e->isNull()) {
1611 http->storeEntry(NULL);
1612 } else {
1613 http->storeEntry(e);
1614 }
1615
1616 e = http->storeEntry();
1617
1618 /* Release IP-cache entries on reload */
1619 /** \li If the request has no-cache flag set or some no_cache HACK in operation we
1620 * 'invalidate' the cached IP entries for this request ???
1621 */
1622 if (r->flags.noCache || r->flags.noCacheHack())
1623 ipcacheInvalidateNegative(r->GetHost());
1624
1625 #if USE_CACHE_DIGESTS
1626 lookup_type = http->storeEntry() ? "HIT" : "MISS";
1627 #endif
1628
1629 if (NULL == http->storeEntry()) {
1630 /** \li If no StoreEntry object is current assume this object isn't in the cache set MISS*/
1631 debugs(85, 3, "StoreEntry is NULL - MISS");
1632 http->logType = LOG_TCP_MISS;
1633 doGetMoreData();
1634 return;
1635 }
1636
1637 if (Config.onoff.offline) {
1638 /** \li If we are running in offline mode set to HIT */
1639 debugs(85, 3, "offline HIT " << *e);
1640 http->logType = LOG_TCP_HIT;
1641 doGetMoreData();
1642 return;
1643 }
1644
1645 if (http->redirect.status) {
1646 /** \li If redirection status is True force this to be a MISS */
1647 debugs(85, 3, "REDIRECT status forced StoreEntry to NULL (no body on 3XX responses) " << *e);
1648 forgetHit();
1649 http->logType = LOG_TCP_REDIRECT;
1650 doGetMoreData();
1651 return;
1652 }
1653
1654 if (!e->validToSend()) {
1655 debugs(85, 3, "!storeEntryValidToSend MISS " << *e);
1656 forgetHit();
1657 http->logType = LOG_TCP_MISS;
1658 doGetMoreData();
1659 return;
1660 }
1661
1662 if (EBIT_TEST(e->flags, ENTRY_SPECIAL)) {
1663 /* \li Special entries are always hits, no matter what the client says */
1664 debugs(85, 3, "ENTRY_SPECIAL HIT " << *e);
1665 http->logType = LOG_TCP_HIT;
1666 doGetMoreData();
1667 return;
1668 }
1669
1670 if (r->flags.noCache) {
1671 debugs(85, 3, "no-cache REFRESH MISS " << *e);
1672 forgetHit();
1673 http->logType = LOG_TCP_CLIENT_REFRESH_MISS;
1674 doGetMoreData();
1675 return;
1676 }
1677
1678 debugs(85, 3, "default HIT " << *e);
1679 http->logType = LOG_TCP_HIT;
1680 doGetMoreData();
1681 }
1682
1683 /**
1684 * Request more data from the store for the client Stream
1685 * This is *the* entry point to this module.
1686 *
1687 * Preconditions:
1688 * - This is the head of the list.
1689 * - There is at least one more node.
1690 * - Data context is not null
1691 */
1692 void
1693 clientGetMoreData(clientStreamNode * aNode, ClientHttpRequest * http)
1694 {
1695 /* Test preconditions */
1696 assert(aNode != NULL);
1697 assert(cbdataReferenceValid(aNode));
1698 assert(aNode->node.prev == NULL);
1699 assert(aNode->node.next != NULL);
1700 clientReplyContext *context = dynamic_cast<clientReplyContext *>(aNode->data.getRaw());
1701 assert (context);
1702 assert(context->http == http);
1703
1704 clientStreamNode *next = ( clientStreamNode *)aNode->node.next->data;
1705
1706 if (!context->ourNode)
1707 context->ourNode = aNode;
1708
1709 /* no cbdatareference, this is only used once, and safely */
1710 if (context->flags.storelogiccomplete) {
1711 StoreIOBuffer tempBuffer;
1712 tempBuffer.offset = next->readBuffer.offset + context->headers_sz;
1713 tempBuffer.length = next->readBuffer.length;
1714 tempBuffer.data = next->readBuffer.data;
1715
1716 storeClientCopy(context->sc, http->storeEntry(),
1717 tempBuffer, clientReplyContext::SendMoreData, context);
1718 return;
1719 }
1720
1721 if (context->http->request->method == Http::METHOD_PURGE) {
1722 context->purgeRequest();
1723 return;
1724 }
1725
1726 // OPTIONS with Max-Forwards:0 handled in clientProcessRequest()
1727
1728 if (context->http->request->method == Http::METHOD_TRACE) {
1729 if (context->http->request->header.getInt64(HDR_MAX_FORWARDS) == 0) {
1730 context->traceReply(aNode);
1731 return;
1732 }
1733
1734 /* continue forwarding, not finished yet. */
1735 http->logType = LOG_TCP_MISS;
1736
1737 context->doGetMoreData();
1738 } else
1739 context->identifyStoreObject();
1740 }
1741
1742 void
1743 clientReplyContext::doGetMoreData()
1744 {
1745 /* We still have to do store logic processing - vary, cache hit etc */
1746 if (http->storeEntry() != NULL) {
1747 /* someone found the object in the cache for us */
1748 StoreIOBuffer localTempBuffer;
1749
1750 http->storeEntry()->lock("clientReplyContext::doGetMoreData");
1751
1752 MemObject *mem_obj = http->storeEntry()->makeMemObject();
1753 if (!mem_obj->hasUris()) {
1754 /*
1755 * This if-block exists because we don't want to clobber
1756 * a preexiting mem_obj->method value if the mem_obj
1757 * already exists. For example, when a HEAD request
1758 * is a cache hit for a GET response, we want to keep
1759 * the method as GET.
1760 */
1761 mem_obj->setUris(storeId(), http->log_uri, http->request->method);
1762 /**
1763 * Here we can see if the object was
1764 * created using URL or alternative StoreID from helper.
1765 */
1766 debugs(88, 3, "storeId: " << http->storeEntry()->mem_obj->storeId());
1767 }
1768
1769 sc = storeClientListAdd(http->storeEntry(), this);
1770 #if USE_DELAY_POOLS
1771 sc->setDelayId(DelayId::DelayClient(http));
1772 #endif
1773
1774 assert(http->logType == LOG_TCP_HIT);
1775 reqofs = 0;
1776 /* guarantee nothing has been sent yet! */
1777 assert(http->out.size == 0);
1778 assert(http->out.offset == 0);
1779
1780 if (Ip::Qos::TheConfig.isHitTosActive()) {
1781 Ip::Qos::doTosLocalHit(http->getConn()->clientConnection);
1782 }
1783
1784 if (Ip::Qos::TheConfig.isHitNfmarkActive()) {
1785 Ip::Qos::doNfmarkLocalHit(http->getConn()->clientConnection);
1786 }
1787
1788 localTempBuffer.offset = reqofs;
1789 localTempBuffer.length = getNextNode()->readBuffer.length;
1790 localTempBuffer.data = getNextNode()->readBuffer.data;
1791 storeClientCopy(sc, http->storeEntry(), localTempBuffer, CacheHit, this);
1792 } else {
1793 /* MISS CASE, http->logType is already set! */
1794 processMiss();
1795 }
1796 }
1797
1798 /** The next node has removed itself from the stream. */
1799 void
1800 clientReplyDetach(clientStreamNode * node, ClientHttpRequest * http)
1801 {
1802 /** detach from the stream */
1803 clientStreamDetach(node, http);
1804 }
1805
1806 /**
1807 * Accepts chunk of a http message in buf, parses prefix, filters headers and
1808 * such, writes processed message to the message recipient
1809 */
1810 void
1811 clientReplyContext::SendMoreData(void *data, StoreIOBuffer result)
1812 {
1813 clientReplyContext *context = static_cast<clientReplyContext *>(data);
1814 context->sendMoreData (result);
1815 }
1816
1817 void
1818 clientReplyContext::makeThisHead()
1819 {
1820 /* At least, I think thats what this does */
1821 dlinkDelete(&http->active, &ClientActiveRequests);
1822 dlinkAdd(http, &http->active, &ClientActiveRequests);
1823 }
1824
1825 bool
1826 clientReplyContext::errorInStream(StoreIOBuffer const &result, size_t const &sizeToProcess)const
1827 {
1828 return /* aborted request */
1829 (http->storeEntry() && EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED)) ||
1830 /* Upstream read error */ (result.flags.error) ||
1831 /* Upstream EOF */ (sizeToProcess == 0);
1832 }
1833
1834 void
1835 clientReplyContext::sendStreamError(StoreIOBuffer const &result)
1836 {
1837 /** call clientWriteComplete so the client socket gets closed
1838 *
1839 * We call into the stream, because we don't know that there is a
1840 * client socket!
1841 */
1842 debugs(88, 5, "clientReplyContext::sendStreamError: A stream error has occured, marking as complete and sending no data.");
1843 StoreIOBuffer localTempBuffer;
1844 flags.complete = 1;
1845 http->request->flags.streamError = true;
1846 localTempBuffer.flags.error = result.flags.error;
1847 clientStreamCallback((clientStreamNode*)http->client_stream.head->data, http, NULL,
1848 localTempBuffer);
1849 }
1850
1851 void
1852 clientReplyContext::pushStreamData(StoreIOBuffer const &result, char *source)
1853 {
1854 StoreIOBuffer localTempBuffer;
1855
1856 if (result.length == 0) {
1857 debugs(88, 5, "clientReplyContext::pushStreamData: marking request as complete due to 0 length store result");
1858 flags.complete = 1;
1859 }
1860
1861 assert(result.offset - headers_sz == next()->readBuffer.offset);
1862 localTempBuffer.offset = result.offset - headers_sz;
1863 localTempBuffer.length = result.length;
1864
1865 if (localTempBuffer.length)
1866 localTempBuffer.data = source;
1867
1868 clientStreamCallback((clientStreamNode*)http->client_stream.head->data, http, NULL,
1869 localTempBuffer);
1870 }
1871
1872 clientStreamNode *
1873 clientReplyContext::next() const
1874 {
1875 assert ( (clientStreamNode*)http->client_stream.head->next->data == getNextNode());
1876 return getNextNode();
1877 }
1878
1879 void
1880 clientReplyContext::sendBodyTooLargeError()
1881 {
1882 Ip::Address tmp_noaddr;
1883 tmp_noaddr.setNoAddr(); // TODO: make a global const
1884 http->logType = LOG_TCP_DENIED_REPLY;
1885 ErrorState *err = clientBuildError(ERR_TOO_BIG, Http::scForbidden, NULL,
1886 http->getConn() != NULL ? http->getConn()->clientConnection->remote : tmp_noaddr,
1887 http->request);
1888 removeClientStoreReference(&(sc), http);
1889 HTTPMSGUNLOCK(reply);
1890 startError(err);
1891
1892 }
1893
1894 /// send 412 (Precondition Failed) to client
1895 void
1896 clientReplyContext::sendPreconditionFailedError()
1897 {
1898 http->logType = LOG_TCP_HIT;
1899 ErrorState *const err =
1900 clientBuildError(ERR_PRECONDITION_FAILED, Http::scPreconditionFailed,
1901 NULL, http->getConn()->clientConnection->remote, http->request);
1902 removeClientStoreReference(&sc, http);
1903 HTTPMSGUNLOCK(reply);
1904 startError(err);
1905 }
1906
1907 /// send 304 (Not Modified) to client
1908 void
1909 clientReplyContext::sendNotModified()
1910 {
1911 StoreEntry *e = http->storeEntry();
1912 const time_t timestamp = e->timestamp;
1913 HttpReply *const temprep = e->getReply()->make304();
1914 http->logType = LOG_TCP_IMS_HIT;
1915 removeClientStoreReference(&sc, http);
1916 createStoreEntry(http->request->method, RequestFlags());
1917 e = http->storeEntry();
1918 // Copy timestamp from the original entry so the 304
1919 // reply has a meaningful Age: header.
1920 e->timestampsSet();
1921 e->timestamp = timestamp;
1922 e->replaceHttpReply(temprep);
1923 e->complete();
1924 /*
1925 * TODO: why put this in the store and then serialise it and
1926 * then parse it again. Simply mark the request complete in
1927 * our context and write the reply struct to the client side.
1928 */
1929 triggerInitialStoreRead();
1930 }
1931
1932 /// send 304 (Not Modified) or 412 (Precondition Failed) to client
1933 /// depending on request method
1934 void
1935 clientReplyContext::sendNotModifiedOrPreconditionFailedError()
1936 {
1937 if (http->request->method == Http::METHOD_GET ||
1938 http->request->method == Http::METHOD_HEAD)
1939 sendNotModified();
1940 else
1941 sendPreconditionFailedError();
1942 }
1943
1944 void
1945 clientReplyContext::processReplyAccess ()
1946 {
1947 /* NP: this should probably soft-fail to a zero-sized-reply error ?? */
1948 assert(reply);
1949
1950 /** Don't block our own responses or HTTP status messages */
1951 if (http->logType == LOG_TCP_DENIED ||
1952 http->logType == LOG_TCP_DENIED_REPLY ||
1953 alwaysAllowResponse(reply->sline.status())) {
1954 headers_sz = reply->hdr_sz;
1955 processReplyAccessResult(ACCESS_ALLOWED);
1956 return;
1957 }
1958
1959 /** Check for reply to big error */
1960 if (reply->expectedBodyTooLarge(*http->request)) {
1961 sendBodyTooLargeError();
1962 return;
1963 }
1964
1965 headers_sz = reply->hdr_sz;
1966
1967 /** check for absent access controls (permit by default) */
1968 if (!Config.accessList.reply) {
1969 processReplyAccessResult(ACCESS_ALLOWED);
1970 return;
1971 }
1972
1973 /** Process http_reply_access lists */
1974 ACLFilledChecklist *replyChecklist =
1975 clientAclChecklistCreate(Config.accessList.reply, http);
1976 replyChecklist->reply = reply;
1977 HTTPMSGLOCK(replyChecklist->reply);
1978 replyChecklist->nonBlockingCheck(ProcessReplyAccessResult, this);
1979 }
1980
1981 void
1982 clientReplyContext::ProcessReplyAccessResult(allow_t rv, void *voidMe)
1983 {
1984 clientReplyContext *me = static_cast<clientReplyContext *>(voidMe);
1985 me->processReplyAccessResult(rv);
1986 }
1987
1988 void
1989 clientReplyContext::processReplyAccessResult(const allow_t &accessAllowed)
1990 {
1991 debugs(88, 2, "The reply for " << RequestMethodStr(http->request->method)
1992 << " " << http->uri << " is " << accessAllowed << ", because it matched '"
1993 << (AclMatchedName ? AclMatchedName : "NO ACL's") << "'" );
1994
1995 if (accessAllowed != ACCESS_ALLOWED) {
1996 ErrorState *err;
1997 err_type page_id;
1998 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, 1);
1999
2000 http->logType = LOG_TCP_DENIED_REPLY;
2001
2002 if (page_id == ERR_NONE)
2003 page_id = ERR_ACCESS_DENIED;
2004
2005 Ip::Address tmp_noaddr;
2006 tmp_noaddr.setNoAddr();
2007 err = clientBuildError(page_id, Http::scForbidden, NULL,
2008 http->getConn() != NULL ? http->getConn()->clientConnection->remote : tmp_noaddr,
2009 http->request);
2010
2011 removeClientStoreReference(&sc, http);
2012
2013 HTTPMSGUNLOCK(reply);
2014
2015 startError(err);
2016
2017 return;
2018 }
2019
2020 /* Ok, the reply is allowed, */
2021 http->loggingEntry(http->storeEntry());
2022
2023 ssize_t body_size = reqofs - reply->hdr_sz;
2024 if (body_size < 0) {
2025 reqofs = reply->hdr_sz;
2026 body_size = 0;
2027 }
2028
2029 debugs(88, 3, "clientReplyContext::sendMoreData: Appending " <<
2030 (int) body_size << " bytes after " << reply->hdr_sz <<
2031 " bytes of headers");
2032
2033 #if USE_SQUID_ESI
2034
2035 if (http->flags.accel && reply->sline.status() != Http::scForbidden &&
2036 !alwaysAllowResponse(reply->sline.status()) &&
2037 esiEnableProcessing(reply)) {
2038 debugs(88, 2, "Enabling ESI processing for " << http->uri);
2039 clientStreamInsertHead(&http->client_stream, esiStreamRead,
2040 esiProcessStream, esiStreamDetach, esiStreamStatus, NULL);
2041 }
2042
2043 #endif
2044
2045 if (http->request->method == Http::METHOD_HEAD) {
2046 /* do not forward body for HEAD replies */
2047 body_size = 0;
2048 http->flags.done_copying = true;
2049 flags.complete = 1;
2050 }
2051
2052 assert (!flags.headersSent);
2053 flags.headersSent = true;
2054
2055 StoreIOBuffer localTempBuffer;
2056 char *buf = next()->readBuffer.data;
2057 char *body_buf = buf + reply->hdr_sz;
2058
2059 //Server side may disable ranges under some circumstances.
2060
2061 if ((!http->request->range))
2062 next()->readBuffer.offset = 0;
2063
2064 body_buf -= next()->readBuffer.offset;
2065
2066 if (next()->readBuffer.offset != 0) {
2067 if (next()->readBuffer.offset > body_size) {
2068 /* Can't use any of the body we received. send nothing */
2069 localTempBuffer.length = 0;
2070 localTempBuffer.data = NULL;
2071 } else {
2072 localTempBuffer.length = body_size - next()->readBuffer.offset;
2073 localTempBuffer.data = body_buf + next()->readBuffer.offset;
2074 }
2075 } else {
2076 localTempBuffer.length = body_size;
2077 localTempBuffer.data = body_buf;
2078 }
2079
2080 /* TODO??: move the data in the buffer back by the request header size */
2081 clientStreamCallback((clientStreamNode *)http->client_stream.head->data,
2082 http, reply, localTempBuffer);
2083
2084 return;
2085 }
2086
2087 void
2088 clientReplyContext::sendMoreData (StoreIOBuffer result)
2089 {
2090 if (deleting)
2091 return;
2092
2093 StoreEntry *entry = http->storeEntry();
2094
2095 ConnStateData * conn = http->getConn();
2096
2097 // too late, our conn is closing
2098 // TODO: should we also quit?
2099 if (conn == NULL) {
2100 debugs(33,3, "not sending more data to a closed connection" );
2101 return;
2102 }
2103 if (!conn->isOpen()) {
2104 debugs(33,3, "not sending more data to closing connection " << conn->clientConnection);
2105 return;
2106 }
2107 if (conn->pinning.zeroReply) {
2108 debugs(33,3, "not sending more data after a pinned zero reply " << conn->clientConnection);
2109 return;
2110 }
2111
2112 char *buf = next()->readBuffer.data;
2113
2114 if (buf != result.data) {
2115 /* we've got to copy some data */
2116 assert(result.length <= next()->readBuffer.length);
2117 memcpy(buf, result.data, result.length);
2118 }
2119
2120 if (reqofs==0 && !logTypeIsATcpHit(http->logType) && Comm::IsConnOpen(conn->clientConnection)) {
2121 if (Ip::Qos::TheConfig.isHitTosActive()) {
2122 Ip::Qos::doTosLocalMiss(conn->clientConnection, http->request->hier.code);
2123 }
2124 if (Ip::Qos::TheConfig.isHitNfmarkActive()) {
2125 Ip::Qos::doNfmarkLocalMiss(conn->clientConnection, http->request->hier.code);
2126 }
2127 }
2128
2129 /* We've got the final data to start pushing... */
2130 flags.storelogiccomplete = 1;
2131
2132 reqofs += result.length;
2133
2134 assert(reqofs <= HTTP_REQBUF_SZ || flags.headersSent);
2135
2136 assert(http->request != NULL);
2137
2138 /* ESI TODO: remove this assert once everything is stable */
2139 assert(http->client_stream.head->data
2140 && cbdataReferenceValid(http->client_stream.head->data));
2141
2142 makeThisHead();
2143
2144 debugs(88, 5, "clientReplyContext::sendMoreData: " << http->uri << ", " <<
2145 reqofs << " bytes (" << result.length <<
2146 " new bytes)");
2147 debugs(88, 5, "clientReplyContext::sendMoreData:"
2148 << conn->clientConnection <<
2149 " '" << entry->url() << "'" <<
2150 " out.offset=" << http->out.offset);
2151
2152 /* update size of the request */
2153 reqsize = reqofs;
2154
2155 if (errorInStream(result, reqofs)) {
2156 sendStreamError(result);
2157 return;
2158 }
2159
2160 if (flags.headersSent) {
2161 pushStreamData (result, buf);
2162 return;
2163 }
2164
2165 cloneReply();
2166
2167 /* handle headers */
2168
2169 if (Config.onoff.log_mime_hdrs) {
2170 size_t k;
2171
2172 if ((k = headersEnd(buf, reqofs))) {
2173 safe_free(http->al->headers.reply);
2174 http->al->headers.reply = (char *)xcalloc(k + 1, 1);
2175 xstrncpy(http->al->headers.reply, buf, k);
2176 }
2177 }
2178
2179 holdingBuffer = result;
2180 processReplyAccess();
2181 return;
2182 }
2183
2184 /* Using this breaks the client layering just a little!
2185 */
2186 void
2187 clientReplyContext::createStoreEntry(const HttpRequestMethod& m, RequestFlags reqFlags)
2188 {
2189 assert(http != NULL);
2190 /*
2191 * For erroneous requests, we might not have a h->request,
2192 * so make a fake one.
2193 */
2194
2195 if (http->request == NULL) {
2196 http->request = new HttpRequest(m, AnyP::PROTO_NONE, null_string);
2197 HTTPMSGLOCK(http->request);
2198 }
2199
2200 StoreEntry *e = storeCreateEntry(storeId(), http->log_uri, reqFlags, m);
2201
2202 // Make entry collapsable ASAP, to increase collapsing chances for others,
2203 // TODO: every must-revalidate and similar request MUST reach the origin,
2204 // but do we have to prohibit others from collapsing on that request?
2205 if (Config.onoff.collapsed_forwarding && reqFlags.cachable &&
2206 !reqFlags.needValidation &&
2207 (m == Http::METHOD_GET || m == Http::METHOD_HEAD)) {
2208 // make the entry available for future requests now
2209 Store::Root().allowCollapsing(e, reqFlags, m);
2210 }
2211
2212 sc = storeClientListAdd(e, this);
2213
2214 #if USE_DELAY_POOLS
2215 sc->setDelayId(DelayId::DelayClient(http));
2216 #endif
2217
2218 reqofs = 0;
2219
2220 reqsize = 0;
2221
2222 /* I don't think this is actually needed! -- adrian */
2223 /* http->reqbuf = http->norm_reqbuf; */
2224 // assert(http->reqbuf == http->norm_reqbuf);
2225 /* The next line is illegal because we don't know if the client stream
2226 * buffers have been set up
2227 */
2228 // storeClientCopy(http->sc, e, 0, HTTP_REQBUF_SZ, http->reqbuf,
2229 // SendMoreData, this);
2230 /* So, we mark the store logic as complete */
2231 flags.storelogiccomplete = 1;
2232
2233 /* and get the caller to request a read, from whereever they are */
2234 /* NOTE: after ANY data flows down the pipe, even one step,
2235 * this function CAN NOT be used to manage errors
2236 */
2237 http->storeEntry(e);
2238 }
2239
2240 ErrorState *
2241 clientBuildError(err_type page_id, Http::StatusCode status, char const *url,
2242 Ip::Address &src_addr, HttpRequest * request)
2243 {
2244 ErrorState *err = new ErrorState(page_id, status, request);
2245 err->src_addr = src_addr;
2246
2247 if (url)
2248 err->url = xstrdup(url);
2249
2250 return err;
2251 }