]> git.ipfire.org Git - thirdparty/squid.git/blame - src/client_side_reply.cc
ICAP bug: request satisfaction mode was not working. The presence of
[thirdparty/squid.git] / src / client_side_reply.cc
CommitLineData
edce4d98 1
2/*
1d7ab0f4 3 * $Id: client_side_reply.cc,v 1.109 2006/06/07 22:39:34 hno Exp $
edce4d98 4 *
5 * DEBUG: section 88 Client-side Reply Routines
6 * AUTHOR: Robert Collins (Originally Duane Wessels in client_side.c)
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36#include "squid.h"
0655fa4d 37#include "client_side_reply.h"
c8be6d7b 38#include "StoreClient.h"
e6ccf245 39#include "Store.h"
528b2c61 40#include "HttpReply.h"
41#include "HttpRequest.h"
b6b6f466 42#include "forward.h"
e6ccf245 43
c8be6d7b 44#include "clientStream.h"
f5691f9c 45#include "AuthUserRequest.h"
43ae1d95 46#if ESI
47#include "ESI.h"
48#endif
5ef38a13 49#if ICAP_CLIENT_RESPMOD_POSTCACHE
50#include "ICAPClient.h"
51#endif
528b2c61 52#include "MemObject.h"
4fb35c3c 53#include "ACLChecklist.h"
b0dd28ba 54#include "ACL.h"
b67e2c8c 55#if DELAY_POOLS
56#include "DelayPools.h"
57#endif
a46d2c0e 58#include "client_side.h"
985c86bc 59#include "SquidTime.h"
e6ccf245 60
0655fa4d 61CBDATA_CLASS_INIT(clientReplyContext);
edce4d98 62
edce4d98 63/* Local functions */
e6ccf245 64extern "C" CSS clientReplyStatus;
edce4d98 65extern ErrorState *clientBuildError(err_type, http_status, char const *,
62e76326 66
ddfcbc22 67 struct IN_ADDR *, HttpRequest *);
edce4d98 68
edce4d98 69/* privates */
edce4d98 70
0655fa4d 71clientReplyContext::~clientReplyContext()
edce4d98 72{
50c09fc4 73 deleting = true;
74 /* This may trigger a callback back into SendMoreData as the cbdata
75 * is still valid
76 */
86a2f789 77 removeClientStoreReference(&sc, http);
edce4d98 78 /* old_entry might still be set if we didn't yet get the reply
0655fa4d 79 * code in HandleIMSReply() */
80 removeStoreReference(&old_sc, &old_entry);
81 safe_free(tempBuffer.data);
82 cbdataReferenceDone(http);
2b7d324b 83 HTTPMSGUNLOCK(reply);
edce4d98 84}
85
59a1efb2 86clientReplyContext::clientReplyContext(ClientHttpRequest *clientContext) : http (cbdataReference(clientContext)), old_entry (NULL), old_sc(NULL), deleting(false)
0655fa4d 87{}
edce4d98 88
89/* create an error in the store awaiting the client side to read it. */
0655fa4d 90/* This may be better placed in the clientStream logic, but it has not been
91 * relocated there yet
92 */
edce4d98 93void
0655fa4d 94clientReplyContext::setReplyToError(
95 err_type err, http_status status, method_t method, char const *uri,
62e76326 96
ddfcbc22 97 struct IN_ADDR *addr, HttpRequest * failedrequest, char *unparsedrequest,
0655fa4d 98 auth_user_request_t * auth_user_request)
edce4d98 99{
edce4d98 100 ErrorState *errstate =
62e76326 101 clientBuildError(err, status, uri, addr, failedrequest);
102
edce4d98 103 if (unparsedrequest)
62e76326 104 errstate->request_hdrs = xstrdup(unparsedrequest);
edce4d98 105
0655fa4d 106 if (status == HTTP_NOT_IMPLEMENTED && http->request)
62e76326 107 /* prevent confusion over whether we default to persistent or not */
0655fa4d 108 http->request->flags.proxy_keepalive = 0;
62e76326 109
0655fa4d 110 http->al.http.code = errstate->httpStatus;
edce4d98 111
0655fa4d 112 createStoreEntry(method, request_flags());
62e76326 113
114 if (auth_user_request)
115 {
116 errstate->auth_user_request = auth_user_request;
f5691f9c 117
118 errstate->auth_user_request->lock()
119
120 ;
edce4d98 121 }
62e76326 122
edce4d98 123 assert(errstate->callback_data == NULL);
86a2f789 124 errorAppendEntry(http->storeEntry(), errstate);
edce4d98 125 /* Now the caller reads to get this */
126}
127
128void
0655fa4d 129clientReplyContext::removeStoreReference(store_client ** scp,
130 StoreEntry ** ep)
edce4d98 131{
132 StoreEntry *e;
133 store_client *sc = *scp;
62e76326 134
edce4d98 135 if ((e = *ep) != NULL) {
62e76326 136 *ep = NULL;
0655fa4d 137 storeUnregister(sc, e, this);
62e76326 138 *scp = NULL;
97b5e68f 139 e->unlock();
edce4d98 140 }
141}
142
86a2f789 143void
144clientReplyContext::removeClientStoreReference(store_client **scp, ClientHttpRequest *http)
145{
146 StoreEntry *reference = http->storeEntry();
147 removeStoreReference(scp, &reference);
148 http->storeEntry(reference);
149}
150
e6ccf245 151void *
a695955b 152clientReplyContext::operator new (size_t byteCount)
e6ccf245 153{
154 /* derived classes with different sizes must implement their own new */
155 assert (byteCount == sizeof (clientReplyContext));
0655fa4d 156 CBDATA_INIT_TYPE(clientReplyContext);
e6ccf245 157 return cbdataAlloc(clientReplyContext);
158}
159
160void
161clientReplyContext::operator delete (void *address)
162{
a50bfe93 163 clientReplyContext * tmp = (clientReplyContext *)address;
164 cbdataFree (tmp);
e6ccf245 165}
166
edce4d98 167void
0655fa4d 168clientReplyContext::saveState()
edce4d98 169{
e6ccf245 170 assert(old_sc == NULL);
61fb2d9a 171 debug(88, 3)("clientReplyContext::saveState: saving store context\n");
86a2f789 172 old_entry = http->storeEntry();
e6ccf245 173 old_sc = sc;
174 old_reqsize = reqsize;
175 tempBuffer.offset = reqofs;
edce4d98 176 /* Prevent accessing the now saved entries */
86a2f789 177 http->storeEntry(NULL);
e6ccf245 178 sc = NULL;
179 reqsize = 0;
180 reqofs = 0;
edce4d98 181}
182
183void
0655fa4d 184clientReplyContext::restoreState()
edce4d98 185{
e6ccf245 186 assert(old_sc != NULL);
61fb2d9a 187 debug(88, 3)("clientReplyContext::restoreState: Restoring store context\n");
86a2f789 188 removeClientStoreReference(&sc, http);
189 http->storeEntry(old_entry);
e6ccf245 190 sc = old_sc;
191 reqsize = old_reqsize;
192 reqofs = tempBuffer.offset;
edce4d98 193 /* Prevent accessed the old saved entries */
0655fa4d 194 old_entry = NULL;
e6ccf245 195 old_sc = NULL;
196 old_reqsize = 0;
197 tempBuffer.offset = 0;
c8be6d7b 198}
199
200void
0655fa4d 201clientReplyContext::startError(ErrorState * err)
c8be6d7b 202{
0655fa4d 203 createStoreEntry(http->request->method, request_flags());
204 triggerInitialStoreRead();
86a2f789 205 errorAppendEntry(http->storeEntry(), err);
edce4d98 206}
207
e6ccf245 208clientStreamNode *
528b2c61 209clientReplyContext::getNextNode() const
e6ccf245 210{
62e76326 211 return (clientStreamNode *)ourNode->node.next->data;
e6ccf245 212}
213
62e76326 214/* This function is wrong - the client parameters don't include the
528b2c61 215 * header offset
216 */
c8be6d7b 217void
0655fa4d 218clientReplyContext::triggerInitialStoreRead()
c8be6d7b 219{
0655fa4d 220 /* when confident, 0 becomes reqofs, and then this factors into
528b2c61 221 * startSendProcess
222 */
0655fa4d 223 assert(reqofs == 0);
dd272b8e 224 StoreIOBuffer tempBuffer (next()->readBuffer.length, 0, next()->readBuffer.data);
86a2f789 225 storeClientCopy(sc, http->storeEntry(), tempBuffer, SendMoreData, this);
c8be6d7b 226}
edce4d98 227
228/* there is an expired entry in the store.
229 * setup a temporary buffer area and perform an IMS to the origin
230 */
0655fa4d 231void
232clientReplyContext::processExpired()
edce4d98 233{
edce4d98 234 char *url = http->uri;
235 StoreEntry *entry = NULL;
0655fa4d 236 debug(88, 3)("clientReplyContext::processExpired: '%s'", http->uri);
86a2f789 237 assert(http->storeEntry()->lastmod >= 0);
edce4d98 238 /*
239 * check if we are allowed to contact other servers
240 * @?@: Instead of a 504 (Gateway Timeout) reply, we may want to return
241 * a stale entry *if* it matches client requirements
242 */
62e76326 243
0655fa4d 244 if (http->onlyIfCached()) {
245 processOnlyIfCachedMiss();
62e76326 246 return;
edce4d98 247 }
62e76326 248
edce4d98 249 http->request->flags.refresh = 1;
250#if STORE_CLIENT_LIST_DEBUG
c8be6d7b 251 /* Prevent a race with the store client memory free routines
edce4d98 252 */
0655fa4d 253 assert(storeClientIsThisAClient(sc, this));
edce4d98 254#endif
255 /* Prepare to make a new temporary request */
0655fa4d 256 saveState();
edce4d98 257 entry = storeCreateEntry(url,
62e76326 258 http->log_uri, http->request->flags, http->request->method);
34266cde 259 /* NOTE, don't call StoreEntry->lock(), storeCreateEntry() does it */
0655fa4d 260 sc = storeClientListAdd(entry, this);
edce4d98 261#if DELAY_POOLS
262 /* delay_id is already set on original store client */
0655fa4d 263 sc->setDelayId(DelayId::DelayClient(http));
edce4d98 264#endif
62e76326 265
0655fa4d 266 http->request->lastmod = old_entry->lastmod;
267 debug(88, 5)("clientReplyContext::processExpired : lastmod %ld",
62e76326 268 (long int) entry->lastmod);
86a2f789 269 http->storeEntry(entry);
c8be6d7b 270 assert(http->out.offset == 0);
b2d22df6 271
272 /*
273 * A refcounted pointer so that FwdState stays around as long as
274 * this clientReplyContext does
275 */
be0c6690 276 FwdState::fwdStart(http->getConn().getRaw() != NULL ? http->getConn()->fd : -1,
277 http->storeEntry(),
278 http->request);
edce4d98 279 /* Register with storage manager to receive updates when data comes in. */
62e76326 280
edce4d98 281 if (EBIT_TEST(entry->flags, ENTRY_ABORTED))
0655fa4d 282 debug(88, 0) ("clientReplyContext::processExpired: Found ENTRY_ABORTED object");
62e76326 283
c8be6d7b 284 {
62e76326 285 /* start counting the length from 0 */
0655fa4d 286 StoreIOBuffer tempBuffer(HTTP_REQBUF_SZ, 0, tempbuf);
287 storeClientCopy(sc, entry, tempBuffer, HandleIMSReply, this);
edce4d98 288 }
289}
290
edce4d98 291
528b2c61 292void
293clientReplyContext::sendClientUpstreamResponse()
294{
295 StoreIOBuffer tempresult;
0655fa4d 296 removeStoreReference(&old_sc, &old_entry);
528b2c61 297 /* here the data to send is the data we just recieved */
298 tempBuffer.offset = 0;
299 old_reqsize = 0;
0655fa4d 300 /* sendMoreData tracks the offset as well.
528b2c61 301 * Force it back to zero */
302 reqofs = 0;
86a2f789 303 assert(!EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED));
0655fa4d 304 /* TODO: provide sendMoreData with the ready parsed reply */
528b2c61 305 tempresult.length = reqsize;
306 tempresult.data = tempbuf;
0655fa4d 307 sendMoreData(tempresult);
528b2c61 308}
309
edce4d98 310void
0655fa4d 311clientReplyContext::HandleIMSReply(void *data, StoreIOBuffer result)
edce4d98 312{
e6ccf245 313 clientReplyContext *context = (clientReplyContext *)data;
0655fa4d 314 context->handleIMSReply(result);
315}
62e76326 316
0655fa4d 317void
318clientReplyContext::sendClientOldEntry()
319{
320 /* Get the old request back */
321 restoreState();
322 /* here the data to send is in the next nodes buffers already */
86a2f789 323 assert(!EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED));
0655fa4d 324 /* sendMoreData tracks the offset as well.
325 * Force it back to zero */
326 reqofs = 0;
327 StoreIOBuffer tempresult (reqsize, reqofs, next()->readBuffer.data);
328 sendMoreData(tempresult);
329}
62e76326 330
1d7ab0f4 331/* This is the workhorse of the HandleIMSReply callback.
332 *
333 * It is called when we've got data back from the origin following our
334 * IMS request to revalidate a stale entry.
335 */
0655fa4d 336void
1d7ab0f4 337clientReplyContext::handleIMSReply(StoreIOBuffer result)
0655fa4d 338{
1d7ab0f4 339 if (deleting)
340 return;
62e76326 341
1d7ab0f4 342 debug(88, 3) ("handleIMSReply: %s, %lu bytes\n",
343 storeUrl(http->storeEntry()),
344 (long unsigned) result.length);
62e76326 345
1d7ab0f4 346 if (http->storeEntry() == NULL)
347 return;
07947ad8 348
1d7ab0f4 349 if (result.flags.error && !EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED))
350 return;
62e76326 351
1d7ab0f4 352 /* update size of the request */
353 reqsize = result.length + reqofs;
62e76326 354
1d7ab0f4 355 const http_status status = http->storeEntry()->getReply()->sline.status;
62e76326 356
1d7ab0f4 357 // request to origin was aborted
358 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED)) {
359 debug(88, 3) ("handleIMSReply: request to origin aborted '%s', sending old entry to client\n", storeUrl(http->storeEntry()));
360 http->logType = LOG_TCP_REFRESH_FAIL;
361 sendClientOldEntry();
0655fa4d 362 }
62e76326 363
1d7ab0f4 364 // we have a partial reply from the origin
365 else if (STORE_PENDING == http->storeEntry()->store_status && 0 == status) {
366 // header is too large, send old entry
62e76326 367
1d7ab0f4 368 if (reqsize >= HTTP_REQBUF_SZ) {
369 debug(88, 3) ("handleIMSReply: response from origin is too large '%s', sending old entry to client\n", storeUrl(http->storeEntry()));
370 http->logType = LOG_TCP_REFRESH_FAIL;
371 sendClientOldEntry();
0655fa4d 372 }
62e76326 373
1d7ab0f4 374 // everything looks fine, we're just waiting for more data
375 else {
376 debug(88, 3) ("handleIMSReply: incomplete headers for '%s', waiting for more data\n", storeUrl(http->storeEntry()));
377 reqofs = reqsize;
378 waitForMoreData();
379 }
0655fa4d 380 }
62e76326 381
1d7ab0f4 382 // we have a reply from the origin
383 else {
384 HttpReply *old_rep = (HttpReply *) old_entry->getReply();
045a76ab 385
1d7ab0f4 386 // origin replied 304
62e76326 387
1d7ab0f4 388 if (status == HTTP_NOT_MODIFIED) {
389 http->logType = LOG_TCP_REFRESH_UNMODIFIED;
62e76326 390
1d7ab0f4 391 // update headers on existing entry
392 HttpReply *old_rep = (HttpReply *) old_entry->getReply();
393 old_rep->updateOnNotModified(http->storeEntry()->getReply());
394 storeTimestampsSet(old_entry);
62e76326 395
1d7ab0f4 396 // if client sent IMS
397
398 if (http->request->flags.ims) {
399 // forward the 304 from origin
400 debug(88,3) ("handleIMSReply: origin replied 304, revalidating existing entry and forwarding 304 to client\n");
401 sendClientUpstreamResponse();
402 } else {
403 // send existing entry, it's still valid
404 debug(88,3) ("handleIMSReply: origin replied 304, revalidating existing entry and sending %d to client\n", old_rep->sline.status);
405 sendClientOldEntry();
406 }
407 }
62e76326 408
1d7ab0f4 409 // origin replied with a non-error code
410 else if (status > HTTP_STATUS_NONE && status < HTTP_INTERNAL_SERVER_ERROR) {
411 // forward response from origin
412 http->logType = LOG_TCP_REFRESH_MODIFIED;
413 debug(88,3) ("handleIMSReply: origin replied %d, replacing existing entry and forwarding to client\n", status);
414 sendClientUpstreamResponse();
415 }
62e76326 416
1d7ab0f4 417 // origin replied with an error
418 else {
419 // ignore and let client have old entry
420 http->logType = LOG_TCP_REFRESH_FAIL;
421 debug(88,3) ("handleIMSReply: origin replied with error %d, sending old entry (%d) to client\n", status, old_rep->sline.status);
422 sendClientOldEntry();
423 }
424 }
edce4d98 425}
426
e6ccf245 427extern "C" CSR clientGetMoreData;
428extern "C" CSD clientReplyDetach;
edce4d98 429
430/*
431 * clientCacheHit should only be called until the HTTP reply headers
432 * have been parsed. Normally this should be a single call, but
433 * it might take more than one. As soon as we have the headers,
0655fa4d 434 * we hand off to clientSendMoreData, processExpired, or
435 * processMiss.
edce4d98 436 */
437void
0655fa4d 438clientReplyContext::CacheHit(void *data, StoreIOBuffer result)
edce4d98 439{
e6ccf245 440 clientReplyContext *context = (clientReplyContext *)data;
0655fa4d 441 context->cacheHit (result);
442}
443
444void
445clientReplyContext::cacheHit(StoreIOBuffer result)
446{
045a76ab 447 if (deleting)
448 return;
449
86a2f789 450 StoreEntry *e = http->storeEntry();
045a76ab 451
190154cf 452 HttpRequest *r = http->request;
045a76ab 453
c4b7a5a9 454 debug(88, 3) ("clientCacheHit: %s, %ud bytes\n", http->uri, (unsigned int)result.length);
62e76326 455
86a2f789 456 if (http->storeEntry() == NULL) {
62e76326 457 debug(88, 3) ("clientCacheHit: request aborted\n");
458 return;
c8be6d7b 459 } else if (result.flags.error) {
62e76326 460 /* swap in failure */
461 debug(88, 3) ("clientCacheHit: swapin failure for %s\n", http->uri);
462 http->logType = LOG_TCP_SWAPFAIL_MISS;
86a2f789 463 removeClientStoreReference(&sc, http);
0655fa4d 464 processMiss();
62e76326 465 return;
edce4d98 466 }
62e76326 467
d3b3ab85 468 if (result.length == 0) {
62e76326 469 /* the store couldn't get enough data from the file for us to id the
470 * object
471 */
472 /* treat as a miss */
473 http->logType = LOG_TCP_MISS;
0655fa4d 474 processMiss();
62e76326 475 return;
d3b3ab85 476 }
62e76326 477
edce4d98 478 assert(!EBIT_TEST(e->flags, ENTRY_ABORTED));
479 /* update size of the request */
0655fa4d 480 reqsize = result.length + reqofs;
62e76326 481
528b2c61 482 if (e->getReply()->sline.status == 0) {
62e76326 483 /*
484 * we don't have full reply headers yet; either wait for more or
485 * punt to clientProcessMiss.
486 */
487
488 if (e->mem_status == IN_MEMORY || e->store_status == STORE_OK) {
0655fa4d 489 processMiss();
490 } else if (result.length + reqofs >= HTTP_REQBUF_SZ
62e76326 491 && http->out.offset == 0) {
0655fa4d 492 processMiss();
62e76326 493 } else {
62e76326 494 debug(88, 3) ("clientCacheHit: waiting for HTTP reply headers\n");
0655fa4d 495 reqofs += result.length;
496 assert(reqofs <= HTTP_REQBUF_SZ);
62e76326 497 /* get the next users' buffer */
0655fa4d 498 StoreIOBuffer tempBuffer;
499 tempBuffer.offset = http->out.offset + reqofs;
500 tempBuffer.length = next()->readBuffer.length - reqofs;
501 tempBuffer.data = next()->readBuffer.data + reqofs;
502 storeClientCopy(sc, e,
503 tempBuffer, CacheHit, this);
62e76326 504 }
505
506 return;
edce4d98 507 }
62e76326 508
edce4d98 509 /*
510 * Got the headers, now grok them
511 */
29b8d8d6 512 assert(http->logType == LOG_TCP_HIT);
62e76326 513
edce4d98 514 switch (varyEvaluateMatch(e, r)) {
62e76326 515
edce4d98 516 case VARY_NONE:
62e76326 517 /* No variance detected. Continue as normal */
518 break;
519
edce4d98 520 case VARY_MATCH:
62e76326 521 /* This is the correct entity for this request. Continue */
522 debug(88, 2) ("clientProcessHit: Vary MATCH!\n");
523 break;
524
edce4d98 525 case VARY_OTHER:
62e76326 526 /* This is not the correct entity for this request. We need
527 * to requery the cache.
528 */
86a2f789 529 removeClientStoreReference(&sc, http);
62e76326 530 e = NULL;
531 /* Note: varyEvalyateMatch updates the request with vary information
532 * so we only get here once. (it also takes care of cancelling loops)
533 */
534 debug(88, 2) ("clientProcessHit: Vary detected!\n");
0655fa4d 535 clientGetMoreData(ourNode, http);
62e76326 536 return;
537
edce4d98 538 case VARY_CANCEL:
62e76326 539 /* varyEvaluateMatch found a object loop. Process as miss */
540 debug(88, 1) ("clientProcessHit: Vary object loop!\n");
0655fa4d 541 processMiss();
62e76326 542 return;
edce4d98 543 }
62e76326 544
edce4d98 545 if (r->method == METHOD_PURGE) {
86a2f789 546 removeClientStoreReference(&sc, http);
62e76326 547 e = NULL;
0655fa4d 548 purgeRequest();
62e76326 549 return;
edce4d98 550 }
62e76326 551
36e6655b 552 if (storeCheckNegativeHit(e)
553#if HTTP_VIOLATIONS
554 && !r->flags.nocache_hack
555#endif
556 ) {
62e76326 557 http->logType = LOG_TCP_NEGATIVE_HIT;
0655fa4d 558 sendMoreData(result);
f7cf9b8c 559 } else if (!Config.onoff.offline && refreshCheckHTTP(e, r) && !http->flags.internal) {
62e76326 560 debug(88, 5) ("clientCacheHit: in refreshCheck() block\n");
561 /*
562 * We hold a stale copy; it needs to be validated
563 */
564 /*
565 * The 'need_validation' flag is used to prevent forwarding
566 * loops between siblings. If our copy of the object is stale,
567 * then we should probably only use parents for the validation
568 * request. Otherwise two siblings could generate a loop if
569 * both have a stale version of the object.
570 */
571 r->flags.need_validation = 1;
572
573 if (e->lastmod < 0) {
574 /*
575 * Previous reply didn't have a Last-Modified header,
576 * we cannot revalidate it.
577 */
578 http->logType = LOG_TCP_MISS;
0655fa4d 579 processMiss();
62e76326 580 } else if (r->flags.nocache) {
581 /*
582 * This did not match a refresh pattern that overrides no-cache
583 * we should honour the client no-cache header.
584 */
585 http->logType = LOG_TCP_CLIENT_REFRESH_MISS;
0655fa4d 586 processMiss();
62e76326 587 } else if (r->protocol == PROTO_HTTP) {
588 /*
589 * Object needs to be revalidated
590 * XXX This could apply to FTP as well, if Last-Modified is known.
591 */
0655fa4d 592 processExpired();
62e76326 593 } else {
594 /*
595 * We don't know how to re-validate other protocols. Handle
596 * them as if the object has expired.
597 */
598 http->logType = LOG_TCP_MISS;
0655fa4d 599 processMiss();
62e76326 600 }
edce4d98 601 } else if (r->flags.ims) {
62e76326 602 /*
603 * Handle If-Modified-Since requests from the client
604 */
605
606 if (e->getReply()->sline.status != HTTP_OK) {
607 debug(88, 4) ("clientCacheHit: Reply code %d != 200\n",
608 e->getReply()->sline.status);
609 http->logType = LOG_TCP_MISS;
0655fa4d 610 processMiss();
611 } else if (e->modifiedSince(http->request)) {
62e76326 612 http->logType = LOG_TCP_IMS_HIT;
0655fa4d 613 sendMoreData(result);
62e76326 614 } else {
0655fa4d 615 time_t const timestamp = e->timestamp;
06a5ae20 616 HttpReply *temprep = e->getReply()->make304();
62e76326 617 http->logType = LOG_TCP_IMS_HIT;
86a2f789 618 removeClientStoreReference(&sc, http);
0655fa4d 619 createStoreEntry(http->request->method,
620 request_flags());
86a2f789 621 e = http->storeEntry();
62e76326 622 /*
623 * Copy timestamp from the original entry so the 304
624 * reply has a meaningful Age: header.
625 */
626 e->timestamp = timestamp;
db237875 627 e->replaceHttpReply(temprep);
62e76326 628 e->complete();
629 /* TODO: why put this in the store and then serialise it and then parse it again.
630 * Simply mark the request complete in our context and
631 * write the reply struct to the client side
632 */
0655fa4d 633 triggerInitialStoreRead();
62e76326 634 }
edce4d98 635 } else {
62e76326 636 /*
637 * plain ol' cache hit
638 */
639
ffdf45f2 640 if (e->store_status != STORE_OK)
849b826a 641 http->logType = LOG_TCP_MISS;
ffdf45f2 642 else if (e->mem_status == IN_MEMORY)
62e76326 643 http->logType = LOG_TCP_MEM_HIT;
644 else if (Config.onoff.offline)
645 http->logType = LOG_TCP_OFFLINE_HIT;
646
0655fa4d 647 sendMoreData(result);
edce4d98 648 }
649}
650
651/*
652 * Prepare to fetch the object as it's a cache miss of some kind.
653 */
654void
0655fa4d 655clientReplyContext::processMiss()
edce4d98 656{
edce4d98 657 char *url = http->uri;
190154cf 658 HttpRequest *r = http->request;
edce4d98 659 ErrorState *err = NULL;
660 debug(88, 4) ("clientProcessMiss: '%s %s'\n",
62e76326 661 RequestMethodStr[r->method], url);
edce4d98 662 /*
663 * We might have a left-over StoreEntry from a failed cache hit
664 * or IMS request.
665 */
62e76326 666
86a2f789 667 if (http->storeEntry()) {
668 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_SPECIAL)) {
62e76326 669 debug(88, 0) ("clientProcessMiss: miss on a special object (%s).\n",
670 url);
671 debug(88, 0) ("\tlog_type = %s\n", log_tags[http->logType]);
86a2f789 672 storeEntryDump(http->storeEntry(), 1);
62e76326 673 }
674
86a2f789 675 removeClientStoreReference(&sc, http);
edce4d98 676 }
62e76326 677
edce4d98 678 if (r->method == METHOD_PURGE) {
0655fa4d 679 purgeRequest();
62e76326 680 return;
edce4d98 681 }
62e76326 682
0655fa4d 683 if (http->onlyIfCached()) {
684 processOnlyIfCachedMiss();
62e76326 685 return;
edce4d98 686 }
62e76326 687
edce4d98 688 /*
689 * Deny loops when running in accelerator/transproxy mode.
690 */
691 if (http->flags.accel && r->flags.loopdetect) {
62e76326 692 http->al.http.code = HTTP_FORBIDDEN;
693 err =
694 clientBuildError(ERR_ACCESS_DENIED, HTTP_FORBIDDEN, NULL,
98242069 695 &http->getConn()->peer.sin_addr, http->request);
0655fa4d 696 createStoreEntry(r->method, request_flags());
86a2f789 697 errorAppendEntry(http->storeEntry(), err);
0655fa4d 698 triggerInitialStoreRead();
62e76326 699 return;
edce4d98 700 } else {
62e76326 701 assert(http->out.offset == 0);
0655fa4d 702 createStoreEntry(r->method, r->flags);
703 triggerInitialStoreRead();
62e76326 704
705 if (http->redirect.status) {
06a5ae20 706 HttpReply *rep = new HttpReply;
edce4d98 707#if LOG_TCP_REDIRECTS
62e76326 708
709 http->logType = LOG_TCP_REDIRECT;
edce4d98 710#endif
62e76326 711
86a2f789 712 storeReleaseRequest(http->storeEntry());
06a5ae20 713 rep->redirect(http->redirect.status, http->redirect.location);
db237875 714 http->storeEntry()->replaceHttpReply(rep);
86a2f789 715 http->storeEntry()->complete();
62e76326 716 return;
717 }
718
719 if (http->flags.internal)
720 r->protocol = PROTO_INTERNAL;
721
be0c6690 722 FwdState::fwdStart(http->getConn().getRaw() != NULL ? http->getConn()->fd : -1,
723 http->storeEntry(),
724 r);
edce4d98 725 }
726}
727
728/*
729 * client issued a request with an only-if-cached cache-control directive;
730 * we did not find a cached object that can be returned without
731 * contacting other servers;
732 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
733 */
0655fa4d 734void
735clientReplyContext::processOnlyIfCachedMiss()
edce4d98 736{
edce4d98 737 ErrorState *err = NULL;
edce4d98 738 debug(88, 4) ("clientProcessOnlyIfCachedMiss: '%s %s'\n",
0655fa4d 739 RequestMethodStr[http->request->method], http->uri);
edce4d98 740 http->al.http.code = HTTP_GATEWAY_TIMEOUT;
741 err = clientBuildError(ERR_ONLY_IF_CACHED_MISS, HTTP_GATEWAY_TIMEOUT, NULL,
98242069 742 &http->getConn()->peer.sin_addr, http->request);
86a2f789 743 removeClientStoreReference(&sc, http);
0655fa4d 744 startError(err);
edce4d98 745}
746
747void
e6ccf245 748clientReplyContext::purgeRequestFindObjectToPurge()
749{
750 /* Try to find a base entry */
751 http->flags.purging = 1;
752 lookingforstore = 1;
3b13a8fd 753 StoreEntry::getPublicByRequestMethod(this, http->request, METHOD_GET);
e6ccf245 754}
755
756void
3b13a8fd 757clientReplyContext::created(StoreEntry *newEntry)
e6ccf245 758{
759 if (lookingforstore == 1)
62e76326 760 purgeFoundGet(newEntry);
e6ccf245 761 else if (lookingforstore == 2)
62e76326 762 purgeFoundHead(newEntry);
e6ccf245 763 else if (lookingforstore == 3)
62e76326 764 purgeDoPurgeGet(newEntry);
e6ccf245 765 else if (lookingforstore == 4)
62e76326 766 purgeDoPurgeHead(newEntry);
e6ccf245 767 else if (lookingforstore == 5)
62e76326 768 identifyFoundObject(newEntry);
e6ccf245 769}
770
771void
3b13a8fd 772clientReplyContext::purgeFoundGet(StoreEntry *newEntry)
e6ccf245 773{
774 if (newEntry->isNull()) {
62e76326 775 lookingforstore = 2;
776 StoreEntry::getPublicByRequestMethod(this, http->request, METHOD_HEAD);
e6ccf245 777 } else
62e76326 778 purgeFoundObject (newEntry);
e6ccf245 779}
780
781void
3b13a8fd 782clientReplyContext::purgeFoundHead(StoreEntry *newEntry)
e6ccf245 783{
784 if (newEntry->isNull())
62e76326 785 purgeDoMissPurge();
e6ccf245 786 else
62e76326 787 purgeFoundObject (newEntry);
e6ccf245 788}
62e76326 789
e6ccf245 790void
3b13a8fd 791clientReplyContext::purgeFoundObject(StoreEntry *entry)
e6ccf245 792{
793 assert (entry && !entry->isNull());
e4b9a2f0 794
795 if (EBIT_TEST(entry->flags, ENTRY_SPECIAL)) {
796 http->logType = LOG_TCP_DENIED;
797 ErrorState *err =
798 clientBuildError(ERR_ACCESS_DENIED, HTTP_FORBIDDEN, NULL,
799 &http->getConn()->peer.sin_addr, http->request);
800 startError(err);
801 return;
802 }
803
62e76326 804 StoreIOBuffer tempBuffer;
805 /* Swap in the metadata */
86a2f789 806 http->storeEntry(entry);
34266cde 807
808 http->storeEntry()->lock()
809
810 ;
86a2f789 811 storeCreateMemObject(http->storeEntry(), http->uri, http->log_uri);
34266cde 812
86a2f789 813 http->storeEntry()->mem_obj->method = http->request->method;
34266cde 814
86a2f789 815 sc = storeClientListAdd(http->storeEntry(), this);
34266cde 816
62e76326 817 http->logType = LOG_TCP_HIT;
34266cde 818
62e76326 819 reqofs = 0;
34266cde 820
62e76326 821 tempBuffer.offset = http->out.offset;
34266cde 822
0655fa4d 823 tempBuffer.length = next()->readBuffer.length;
34266cde 824
0655fa4d 825 tempBuffer.data = next()->readBuffer.data;
34266cde 826
86a2f789 827 storeClientCopy(sc, http->storeEntry(),
0655fa4d 828 tempBuffer, CacheHit, this);
e6ccf245 829}
830
831void
832clientReplyContext::purgeRequest()
edce4d98 833{
edce4d98 834 debug(88, 3) ("Config2.onoff.enable_purge = %d\n",
62e76326 835 Config2.onoff.enable_purge);
836
edce4d98 837 if (!Config2.onoff.enable_purge) {
62e76326 838 http->logType = LOG_TCP_DENIED;
839 ErrorState *err =
840 clientBuildError(ERR_ACCESS_DENIED, HTTP_FORBIDDEN, NULL,
98242069 841 &http->getConn()->peer.sin_addr, http->request);
0655fa4d 842 startError(err);
62e76326 843 return;
edce4d98 844 }
62e76326 845
edce4d98 846 /* Release both IP cache */
847 ipcacheInvalidate(http->request->host);
848
e6ccf245 849 if (!http->flags.purging)
62e76326 850 purgeRequestFindObjectToPurge();
e6ccf245 851 else
62e76326 852 purgeDoMissPurge();
e6ccf245 853}
854
855void
856clientReplyContext::purgeDoMissPurge()
857{
29b8d8d6 858 http->logType = LOG_TCP_MISS;
e6ccf245 859 lookingforstore = 3;
3b13a8fd 860 StoreEntry::getPublicByRequestMethod(this,http->request, METHOD_GET);
e6ccf245 861}
862
863void
3b13a8fd 864clientReplyContext::purgeDoPurgeGet(StoreEntry *newEntry)
e6ccf245 865{
866 assert (newEntry);
867 /* Move to new() when that is created */
868 purgeStatus = HTTP_NOT_FOUND;
869
870 if (!newEntry->isNull()) {
62e76326 871 /* Release the cached URI */
872 debug(88, 4) ("clientPurgeRequest: GET '%s'\n", storeUrl(newEntry));
5f33b71d 873 newEntry->release();
62e76326 874 purgeStatus = HTTP_OK;
edce4d98 875 }
62e76326 876
e6ccf245 877 lookingforstore = 4;
3b13a8fd 878 StoreEntry::getPublicByRequestMethod(this, http->request, METHOD_HEAD);
e6ccf245 879}
880
881void
3b13a8fd 882clientReplyContext::purgeDoPurgeHead(StoreEntry *newEntry)
e6ccf245 883{
4fc0ac76 884 if (newEntry && !newEntry->isNull()) {
62e76326 885 debug(88, 4) ("clientPurgeRequest: HEAD '%s'\n", storeUrl(newEntry));
5f33b71d 886 newEntry->release();
62e76326 887 purgeStatus = HTTP_OK;
edce4d98 888 }
62e76326 889
edce4d98 890 /* And for Vary, release the base URI if none of the headers was included in the request */
62e76326 891
edce4d98 892 if (http->request->vary_headers
62e76326 893 && !strstr(http->request->vary_headers, "=")) {
894 StoreEntry *entry = storeGetPublic(urlCanonical(http->request), METHOD_GET);
895
896 if (entry) {
897 debug(88, 4) ("clientPurgeRequest: Vary GET '%s'\n",
898 storeUrl(entry));
5f33b71d 899 entry->release();
62e76326 900 purgeStatus = HTTP_OK;
901 }
902
903 entry = storeGetPublic(urlCanonical(http->request), METHOD_HEAD);
904
905 if (entry) {
906 debug(88, 4) ("clientPurgeRequest: Vary HEAD '%s'\n",
907 storeUrl(entry));
5f33b71d 908 entry->release();
62e76326 909 purgeStatus = HTTP_OK;
910 }
edce4d98 911 }
62e76326 912
edce4d98 913 /*
914 * Make a new entry to hold the reply to be written
915 * to the client.
916 */
528b2c61 917 /* FIXME: This doesn't need to go through the store. Simply
918 * push down the client chain
919 */
0655fa4d 920 createStoreEntry(http->request->method, request_flags());
62e76326 921
0655fa4d 922 triggerInitialStoreRead();
62e76326 923
7dc5f514 924 HttpReply *rep = new HttpReply;
62e76326 925
450e0c10 926 HttpVersion version(1,0);
62e76326 927
7dc5f514 928 rep->setHeaders(version, purgeStatus, NULL, NULL, 0, 0, -1);
62e76326 929
7dc5f514 930 http->storeEntry()->replaceHttpReply(rep);
62e76326 931
86a2f789 932 http->storeEntry()->complete();
edce4d98 933}
934
935void
0655fa4d 936clientReplyContext::traceReply(clientStreamNode * node)
edce4d98 937{
e6ccf245 938 clientStreamNode *next = (clientStreamNode *)node->node.next->data;
528b2c61 939 StoreIOBuffer tempBuffer;
0655fa4d 940 assert(http->request->max_forwards == 0);
941 createStoreEntry(http->request->method, request_flags());
942 tempBuffer.offset = next->readBuffer.offset + headers_sz;
c8be6d7b 943 tempBuffer.length = next->readBuffer.length;
944 tempBuffer.data = next->readBuffer.data;
86a2f789 945 storeClientCopy(sc, http->storeEntry(),
0655fa4d 946 tempBuffer, SendMoreData, this);
86a2f789 947 storeReleaseRequest(http->storeEntry());
948 storeBuffer(http->storeEntry());
7dc5f514 949 HttpReply *rep = new HttpReply;
450e0c10 950 HttpVersion version(1,0);
06a5ae20 951 rep->setHeaders(version, HTTP_OK, NULL, "text/plain",
5cafad19 952 http->request->prefixLen(), 0, squid_curtime);
db237875 953 http->storeEntry()->replaceHttpReply(rep);
5cafad19 954 http->request->swapOut(http->storeEntry());
86a2f789 955 http->storeEntry()->complete();
edce4d98 956}
957
958#define SENDING_BODY 0
959#define SENDING_HDRSONLY 1
960int
0655fa4d 961clientReplyContext::checkTransferDone()
edce4d98 962{
86a2f789 963 StoreEntry *entry = http->storeEntry();
62e76326 964
edce4d98 965 if (entry == NULL)
62e76326 966 return 0;
967
968 /*
edce4d98 969 * For now, 'done_copying' is used for special cases like
970 * Range and HEAD requests.
971 */
0655fa4d 972 if (http->flags.done_copying)
62e76326 973 return 1;
974
edce4d98 975 /*
976 * Handle STORE_OK objects.
977 * objectLen(entry) will be set proprely.
978 * RC: Does objectLen(entry) include the Headers?
0e3be1ea 979 * RC: Yes.
edce4d98 980 */
981 if (entry->store_status == STORE_OK) {
0655fa4d 982 return storeOKTransferDone();
e39b9382 983 } else {
0655fa4d 984 return storeNotOKTransferDone();
edce4d98 985 }
e39b9382 986}
987
988int
989clientReplyContext::storeOKTransferDone() const
990{
86a2f789 991 if (http->out.offset >= objectLen(http->storeEntry()) - headers_sz)
62e76326 992 return 1;
993
e39b9382 994 return 0;
995}
996
997int
998clientReplyContext::storeNotOKTransferDone() const
999{
edce4d98 1000 /*
1001 * Now, handle STORE_PENDING objects
1002 */
86a2f789 1003 MemObject *mem = http->storeEntry()->mem_obj;
edce4d98 1004 assert(mem != NULL);
1005 assert(http->request != NULL);
0e3be1ea 1006 /* mem->reply was wrong because it uses the UPSTREAM header length!!! */
0353e724 1007 HttpReply const *reply = mem->getReply();
62e76326 1008
e39b9382 1009 if (headers_sz == 0)
62e76326 1010 /* haven't found end of headers yet */
1011 return 0;
e39b9382 1012
1013 int sending = SENDING_BODY;
62e76326 1014
e39b9382 1015 if (reply->sline.status == HTTP_NO_CONTENT ||
62e76326 1016 reply->sline.status == HTTP_NOT_MODIFIED ||
1017 reply->sline.status < HTTP_OK ||
1018 http->request->method == METHOD_HEAD)
1019 sending = SENDING_HDRSONLY;
1020
edce4d98 1021 /*
1022 * Figure out how much data we are supposed to send.
1023 * If we are sending a body and we don't have a content-length,
1024 * then we must wait for the object to become STORE_OK.
1025 */
0e3be1ea 1026 if (reply->content_length < 0)
62e76326 1027 return 0;
e39b9382 1028
1029 size_t expectedLength = http->out.headers_sz + reply->content_length;
62e76326 1030
e39b9382 1031 if (http->out.size < expectedLength)
62e76326 1032 return 0;
edce4d98 1033 else
62e76326 1034 return 1;
edce4d98 1035}
1036
1037
edce4d98 1038/* A write has completed, what is the next status based on the
1039 * canonical request data?
1040 * 1 something is wrong
1041 * 0 nothing is wrong.
1042 *
1043 */
1044int
59a1efb2 1045clientHttpRequestStatus(int fd, ClientHttpRequest const *http)
edce4d98 1046{
1047#if SIZEOF_SIZE_T == 4
62e76326 1048
edce4d98 1049 if (http->out.size > 0x7FFF0000) {
62e76326 1050 debug(88, 1) ("WARNING: closing FD %d to prevent counter overflow\n",
1051 fd);
1052 debug(88, 1) ("\tclient %s\n",
a2ac85d9 1053 inet_ntoa(http->getConn().getRaw() != NULL ? http->getConn()->peer.sin_addr : no_addr));
62e76326 1054 debug(88, 1) ("\treceived %d bytes\n", (int) http->out.size);
1055 debug(88, 1) ("\tURI %s\n", http->log_uri);
1056 return 1;
edce4d98 1057 }
62e76326 1058
edce4d98 1059#endif
1060#if SIZEOF_OFF_T == 4
1061 if (http->out.offset > 0x7FFF0000) {
62e76326 1062 debug(88, 1) ("WARNING: closing FD %d to prevent counter overflow\n",
1063 fd);
1064 debug(88, 1) ("\tclient %s\n",
a2ac85d9 1065 inet_ntoa(http->getConn().getRaw() != NULL ? http->getConn()->peer.sin_addr : no_addr));
62e76326 1066 debug(88, 1) ("\treceived %d bytes (offset %d)\n", (int) http->out.size,
1067 (int) http->out.offset);
1068 debug(88, 1) ("\tURI %s\n", http->log_uri);
1069 return 1;
edce4d98 1070 }
62e76326 1071
edce4d98 1072#endif
1073 return 0;
1074}
1075
62e76326 1076/* Preconditions:
edce4d98 1077 * *http is a valid structure.
1078 * fd is either -1, or an open fd.
1079 *
1080 * TODO: enumify this
1081 *
1082 * This function is used by any http request sink, to determine the status
1083 * of the object.
1084 */
1085clientStream_status_t
59a1efb2 1086clientReplyStatus(clientStreamNode * aNode, ClientHttpRequest * http)
edce4d98 1087{
0655fa4d 1088 clientReplyContext *context = dynamic_cast<clientReplyContext *>(aNode->data.getRaw());
1089 assert (context);
1090 assert (context->http == http);
1091 return context->replyStatus();
1092}
1093
1094clientStream_status_t
1095clientReplyContext::replyStatus()
1096{
edce4d98 1097 int done;
1098 /* Here because lower nodes don't need it */
62e76326 1099
86a2f789 1100 if (http->storeEntry() == NULL)
62e76326 1101 return STREAM_FAILED; /* yuck, but what can we do? */
1102
86a2f789 1103 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED))
62e76326 1104 /* TODO: Could upstream read errors (result.flags.error) be
1105 * lost, and result in undersize requests being considered
1106 * complete. Should we tcp reset such connections ?
1107 */
1108 return STREAM_FAILED;
1109
0655fa4d 1110 if ((done = checkTransferDone()) != 0 || flags.complete) {
62e76326 1111 debug(88, 5) ("clientReplyStatus: transfer is DONE\n");
1112 /* Ok we're finished, but how? */
1113
06a5ae20 1114 if (http->storeEntry()->getReply()->bodySize(http->request->method) < 0) {
62e76326 1115 debug(88, 5) ("clientReplyStatus: closing, content_length < 0\n");
1116 return STREAM_FAILED;
1117 }
1118
1119 if (!done) {
1120 debug(88, 5) ("clientReplyStatus: closing, !done, but read 0 bytes\n");
1121 return STREAM_FAILED;
1122 }
1123
0655fa4d 1124 if (!http->gotEnough()) {
62e76326 1125 debug(88, 5) ("clientReplyStatus: client didn't get all it expected\n");
1126 return STREAM_UNPLANNED_COMPLETE;
1127 }
1128
1129 if (http->request->flags.proxy_keepalive) {
1130 debug(88, 5) ("clientReplyStatus: stream complete and can keepalive\n");
1131 return STREAM_COMPLETE;
1132 }
1133
1134 debug(88, 5) ("clientReplyStatus: stream was not expected to complete!\n");
1135 return STREAM_UNPLANNED_COMPLETE;
edce4d98 1136 }
62e76326 1137
b51aec66 1138 if (http->isReplyBodyTooLarge(http->out.offset - 4096)) {
1139 /* 4096 is a margin for the HTTP headers included in out.offset */
62e76326 1140 debug(88, 5) ("clientReplyStatus: client reply body is too large\n");
1141 return STREAM_FAILED;
0e3be1ea 1142 }
62e76326 1143
edce4d98 1144 return STREAM_NONE;
1145}
1146
62e76326 1147/* Responses with no body will not have a content-type header,
edce4d98 1148 * which breaks the rep_mime_type acl, which
1149 * coincidentally, is the most common acl for reply access lists.
1150 * A better long term fix for this is to allow acl matchs on the various
1151 * status codes, and then supply a default ruleset that puts these
1152 * codes before any user defines access entries. That way the user
1153 * can choose to block these responses where appropriate, but won't get
1154 * mysterious breakages.
1155 */
0655fa4d 1156bool
1157clientReplyContext::alwaysAllowResponse(http_status sline) const
edce4d98 1158{
4ef4b952 1159 bool result;
1160
edce4d98 1161 switch (sline) {
62e76326 1162
edce4d98 1163 case HTTP_CONTINUE:
62e76326 1164
edce4d98 1165 case HTTP_SWITCHING_PROTOCOLS:
62e76326 1166
edce4d98 1167 case HTTP_PROCESSING:
62e76326 1168
edce4d98 1169 case HTTP_NO_CONTENT:
62e76326 1170
edce4d98 1171 case HTTP_NOT_MODIFIED:
4ef4b952 1172 result = true;
62e76326 1173 break;
1174
edce4d98 1175 default:
4ef4b952 1176 result = false;
edce4d98 1177 }
4ef4b952 1178
1179 return result;
edce4d98 1180}
1181
1182/*
1183 * filters out unwanted entries from original reply header
1184 * adds extra entries if we have more info than origin server
1185 * adds Squid specific entries
1186 */
0655fa4d 1187void
1188clientReplyContext::buildReplyHeader()
edce4d98 1189{
7dc5f514 1190 HttpHeader *hdr = &reply->header;
c8be6d7b 1191 int is_hit = logTypeIsATcpHit(http->logType);
190154cf 1192 HttpRequest *request = http->request;
edce4d98 1193#if DONT_FILTER_THESE
1194 /* but you might want to if you run Squid as an HTTP accelerator */
a9925b40 1195 /* hdr->delById(HDR_ACCEPT_RANGES); */
1196 hdr->delById(HDR_ETAG);
edce4d98 1197#endif
62e76326 1198
a9925b40 1199 hdr->delById(HDR_PROXY_CONNECTION);
edce4d98 1200 /* here: Keep-Alive is a field-name, not a connection directive! */
a9925b40 1201 hdr->delByName("Keep-Alive");
edce4d98 1202 /* remove Set-Cookie if a hit */
62e76326 1203
edce4d98 1204 if (is_hit)
a9925b40 1205 hdr->delById(HDR_SET_COOKIE);
62e76326 1206
7dc5f514 1207 /*
1208 * Be sure to obey the Connection header
1209 */
1210 reply->header.removeConnectionHeaderEntries();
62e76326 1211
1212 // if (request->range)
7dc5f514 1213 // clientBuildRangeHeader(http, reply);
edce4d98 1214 /*
1215 * Add a estimated Age header on cache hits.
1216 */
1217 if (is_hit) {
62e76326 1218 /*
1219 * Remove any existing Age header sent by upstream caches
1220 * (note that the existing header is passed along unmodified
1221 * on cache misses)
1222 */
a9925b40 1223 hdr->delById(HDR_AGE);
62e76326 1224 /*
1225 * This adds the calculated object age. Note that the details of the
1226 * age calculation is performed by adjusting the timestamp in
1227 * storeTimestampsSet(), not here.
1228 *
1229 * BROWSER WORKAROUND: IE sometimes hangs when receiving a 0 Age
1230 * header, so don't use it unless there is a age to report. Please
1231 * note that Age is only used to make a conservative estimation of
1232 * the objects age, so a Age: 0 header does not add any useful
1233 * information to the reply in any case.
1234 */
1235
86a2f789 1236 if (NULL == http->storeEntry())
62e76326 1237 (void) 0;
86a2f789 1238 else if (http->storeEntry()->timestamp < 0)
62e76326 1239 (void) 0;
bbe58ab5 1240
1241 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_SPECIAL)) {
a9925b40 1242 hdr->delById(HDR_DATE);
1243 hdr->insertTime(HDR_DATE, squid_curtime);
bbe58ab5 1244 } else if (http->storeEntry()->timestamp < squid_curtime) {
a9925b40 1245 hdr->putInt(HDR_AGE,
1246 squid_curtime - http->storeEntry()->timestamp);
62e76326 1247 /* Signal old objects. NB: rfc 2616 is not clear,
1248 * by implication, on whether we should do this to all
1249 * responses, or only cache hits.
1250 * 14.46 states it ONLY applys for heuristically caclulated
1251 * freshness values, 13.2.4 doesn't specify the same limitation.
1252 * We interpret RFC 2616 under the combination.
1253 */
1254 /* TODO: if maxage or s-maxage is present, don't do this */
1255
86a2f789 1256 if (squid_curtime - http->storeEntry()->timestamp >= 86400) {
62e76326 1257 char tempbuf[512];
1258 snprintf (tempbuf, sizeof(tempbuf), "%s %s %s",
1259 "113", ThisCache,
1260 "This cache hit is still fresh and more than 1 day old");
a9925b40 1261 hdr->putStr(HDR_WARNING, tempbuf);
62e76326 1262 }
1263 }
528b2c61 1264
edce4d98 1265 }
62e76326 1266
0bd9aa82 1267 /* Filter unproxyable authentication types */
ddd31ed3 1268 if (http->logType != LOG_TCP_DENIED &&
a9925b40 1269 (hdr->has(HDR_WWW_AUTHENTICATE) || hdr->has(HDR_PROXY_AUTHENTICATE))) {
62e76326 1270 HttpHeaderPos pos = HttpHeaderInitPos;
1271 HttpHeaderEntry *e;
1272
a9925b40 1273 while ((e = hdr->getEntry(&pos))) {
62e76326 1274 if (e->id == HDR_WWW_AUTHENTICATE || e->id == HDR_PROXY_AUTHENTICATE) {
1275 const char *value = e->value.buf();
1276
1277 if ((strncasecmp(value, "NTLM", 4) == 0 &&
1278 (value[4] == '\0' || value[4] == ' '))
1279 ||
1280 (strncasecmp(value, "Negotiate", 9) == 0 &&
1281 (value[9] == '\0' || value[9] == ' ')))
a9925b40 1282 hdr->delAt(pos);
62e76326 1283 }
1284 }
0bd9aa82 1285 }
62e76326 1286
edce4d98 1287 /* Handle authentication headers */
1288 if (request->auth_user_request)
7dc5f514 1289 authenticateFixHeader(reply, request->auth_user_request, request,
62e76326 1290 http->flags.accel, 0);
1291
edce4d98 1292 /* Append X-Cache */
1293 httpHeaderPutStrf(hdr, HDR_X_CACHE, "%s from %s",
62e76326 1294 is_hit ? "HIT" : "MISS", getMyHostname());
1295
edce4d98 1296#if USE_CACHE_DIGESTS
1297 /* Append X-Cache-Lookup: -- temporary hack, to be removed @?@ @?@ */
1298 httpHeaderPutStrf(hdr, HDR_X_CACHE_LOOKUP, "%s from %s:%d",
0655fa4d 1299 lookup_type ? lookup_type : "NONE",
62e76326 1300 getMyHostname(), getMyPort());
1301
edce4d98 1302#endif
62e76326 1303
7dc5f514 1304 if (reply->bodySize(request->method) < 0) {
62e76326 1305 debug(88,
1306 3)
1307 ("clientBuildReplyHeader: can't keep-alive, unknown body size\n");
1308 request->flags.proxy_keepalive = 0;
edce4d98 1309 }
62e76326 1310
f5e45ad8 1311 if (fdUsageHigh()&& !request->flags.must_keepalive) {
a1ca9253 1312 debug(88, 3) ("clientBuildReplyHeader: Not many unused FDs, can't keep-alive\n");
1313 request->flags.proxy_keepalive = 0;
1314 }
1315
58850d15 1316 if (!Config.onoff.error_pconns && reply->sline.status >= 400 && !request->flags.must_keepalive) {
1317 debug(33, 3) ("clientBuildReplyHeader: Error, don't keep-alive\n");
1318 request->flags.proxy_keepalive = 0;
1319 }
1320
f5e45ad8 1321 if (!Config.onoff.client_pconns && !request->flags.must_keepalive)
1322 request->flags.proxy_keepalive = 0;
1323
4dea3fdd 1324 /* Append VIA */
c8be6d7b 1325 {
62e76326 1326 LOCAL_ARRAY(char, bbuf, MAX_URL + 32);
a9925b40 1327 String strVia = hdr->getList(HDR_VIA);
62e76326 1328 snprintf(bbuf, sizeof(bbuf), "%d.%d %s",
7dc5f514 1329 reply->sline.version.major,
1330 reply->sline.version.minor,
62e76326 1331 ThisCache);
1332 strListAdd(&strVia, bbuf, ',');
a9925b40 1333 hdr->delById(HDR_VIA);
1334 hdr->putStr(HDR_VIA, strVia.buf());
62e76326 1335 strVia.clean();
c8be6d7b 1336 }
edce4d98 1337 /* Signal keep-alive if needed */
a9925b40 1338 hdr->putStr(http->flags.accel ? HDR_CONNECTION : HDR_PROXY_CONNECTION,
1339 request->flags.proxy_keepalive ? "keep-alive" : "close");
62e76326 1340
edce4d98 1341#if ADD_X_REQUEST_URI
1342 /*
1343 * Knowing the URI of the request is useful when debugging persistent
1344 * connections in a client; we cannot guarantee the order of http headers,
1345 * but X-Request-URI is likely to be the very last header to ease use from a
1346 * debugger [hdr->entries.count-1].
1347 */
a9925b40 1348 hdr->putStr(HDR_X_REQUEST_URI,
1349 http->memOjbect()->url ? http->memObject()->url : http->uri);
62e76326 1350
edce4d98 1351#endif
62e76326 1352
8c01ada0 1353 httpHdrMangleList(hdr, request, ROR_REPLY);
edce4d98 1354}
1355
1356
0655fa4d 1357void
1358clientReplyContext::buildReply(const char *buf, size_t size)
edce4d98 1359{
edce4d98 1360 size_t k = headersEnd(buf, size);
62e76326 1361
0655fa4d 1362 if (!k)
1363 return;
1364
7dc5f514 1365 assert(reply == NULL);
0655fa4d 1366
7dc5f514 1367 HttpReply *rep = new HttpReply;
1368
1369 reply = HTTPMSGLOCK(rep);
1370
1371 if (!reply->parseCharBuf(buf, k)) {
62e76326 1372 /* parsing failure, get rid of the invalid reply */
7dc5f514 1373 HTTPMSGUNLOCK(reply);
62e76326 1374
0655fa4d 1375 if (http->request->range) {
7dc5f514 1376 debugs(0,0,HERE << "look for bug here");
62e76326 1377 /* this will fail and destroy request->range */
7dc5f514 1378 // clientBuildRangeHeader(http, reply);
62e76326 1379 }
b51aec66 1380
979da574 1381 return;
edce4d98 1382 }
62e76326 1383
0655fa4d 1384 /* enforce 1.0 reply version */
7dc5f514 1385 reply->sline.version = HttpVersion(1,0);
0655fa4d 1386
1387 /* do header conversions */
1388 buildReplyHeader();
edce4d98 1389}
1390
e6ccf245 1391void
1392clientReplyContext::identifyStoreObject()
edce4d98 1393{
190154cf 1394 HttpRequest *r = http->request;
62e76326 1395
e6ccf245 1396 if (r->flags.cachable || r->flags.internal) {
62e76326 1397 lookingforstore = 5;
1398 StoreEntry::getPublicByRequest (this, r);
e6ccf245 1399 } else
62e76326 1400 identifyFoundObject (NullStoreEntry::getInstance());
e6ccf245 1401}
1402
1403void
1404clientReplyContext::identifyFoundObject(StoreEntry *newEntry)
1405{
1406 StoreEntry *e = newEntry;
190154cf 1407 HttpRequest *r = http->request;
62e76326 1408
e6ccf245 1409 if (e->isNull()) {
86a2f789 1410 http->storeEntry(NULL);
e6ccf245 1411 } else {
86a2f789 1412 http->storeEntry(e);
e6ccf245 1413 }
62e76326 1414
86a2f789 1415 e = http->storeEntry();
a12a049a 1416 /* Release IP-cache entries on reload */
1417
1418 if (r->flags.nocache) {
1419
1420#if USE_DNSSERVERS
62e76326 1421
62e76326 1422 ipcacheInvalidate(r->host);
1423
a12a049a 1424#else
1425
1426 ipcacheInvalidateNegative(r->host);
1427
1428#endif /* USE_DNSSERVERS */
1429
1430 }
1431
edce4d98 1432#if HTTP_VIOLATIONS
62e76326 1433
a12a049a 1434 else if (r->flags.nocache_hack) {
1435
1436#if USE_DNSSERVERS
1437
62e76326 1438 ipcacheInvalidate(r->host);
1439
a12a049a 1440#else
1441
1442 ipcacheInvalidateNegative(r->host);
1443
1444#endif /* USE_DNSSERVERS */
1445
1446 }
1447
1448#endif /* HTTP_VIOLATIONS */
edce4d98 1449#if USE_CACHE_DIGESTS
62e76326 1450
86a2f789 1451 lookup_type = http->storeEntry() ? "HIT" : "MISS";
62e76326 1452
edce4d98 1453#endif
62e76326 1454
86a2f789 1455 if (NULL == http->storeEntry()) {
62e76326 1456 /* this object isn't in the cache */
1457 debug(85, 3) ("clientProcessRequest2: storeGet() MISS\n");
1458 http->logType = LOG_TCP_MISS;
1459 doGetMoreData();
1460 return;
edce4d98 1461 }
62e76326 1462
edce4d98 1463 if (Config.onoff.offline) {
62e76326 1464 debug(85, 3) ("clientProcessRequest2: offline HIT\n");
1465 http->logType = LOG_TCP_HIT;
1466 doGetMoreData();
1467 return;
edce4d98 1468 }
62e76326 1469
edce4d98 1470 if (http->redirect.status) {
62e76326 1471 /* force this to be a miss */
86a2f789 1472 http->storeEntry(NULL);
62e76326 1473 http->logType = LOG_TCP_MISS;
1474 doGetMoreData();
1475 return;
edce4d98 1476 }
62e76326 1477
edce4d98 1478 if (!storeEntryValidToSend(e)) {
62e76326 1479 debug(85, 3) ("clientProcessRequest2: !storeEntryValidToSend MISS\n");
86a2f789 1480 http->storeEntry(NULL);
62e76326 1481 http->logType = LOG_TCP_MISS;
1482 doGetMoreData();
1483 return;
edce4d98 1484 }
62e76326 1485
edce4d98 1486 if (EBIT_TEST(e->flags, ENTRY_SPECIAL)) {
62e76326 1487 /* Special entries are always hits, no matter what the client says */
1488 debug(85, 3) ("clientProcessRequest2: ENTRY_SPECIAL HIT\n");
1489 http->logType = LOG_TCP_HIT;
1490 doGetMoreData();
1491 return;
edce4d98 1492 }
62e76326 1493
edce4d98 1494 if (r->flags.nocache) {
62e76326 1495 debug(85, 3) ("clientProcessRequest2: no-cache REFRESH MISS\n");
86a2f789 1496 http->storeEntry(NULL);
62e76326 1497 http->logType = LOG_TCP_CLIENT_REFRESH_MISS;
1498 doGetMoreData();
1499 return;
edce4d98 1500 }
62e76326 1501
edce4d98 1502 debug(85, 3) ("clientProcessRequest2: default HIT\n");
e6ccf245 1503 http->logType = LOG_TCP_HIT;
1504 doGetMoreData();
edce4d98 1505}
1506
1507/* Request more data from the store for the client Stream
1508 * This is *the* entry point to this module.
1509 *
1510 * Preconditions:
1511 * This is the head of the list.
1512 * There is at least one more node.
1513 * data context is not null
1514 */
1515void
59a1efb2 1516clientGetMoreData(clientStreamNode * aNode, ClientHttpRequest * http)
edce4d98 1517{
edce4d98 1518 /* Test preconditions */
e6ccf245 1519 assert(aNode != NULL);
1520 assert(cbdataReferenceValid(aNode));
e6ccf245 1521 assert(aNode->node.prev == NULL);
1522 assert(aNode->node.next != NULL);
0655fa4d 1523 clientReplyContext *context = dynamic_cast<clientReplyContext *>(aNode->data.getRaw());
1524 assert (context);
edce4d98 1525 assert(context->http == http);
1526
0655fa4d 1527 clientStreamNode *next = ( clientStreamNode *)aNode->node.next->data;
62e76326 1528
edce4d98 1529 if (!context->ourNode)
62e76326 1530 context->ourNode = aNode;
1531
e6ccf245 1532 /* no cbdatareference, this is only used once, and safely */
edce4d98 1533 if (context->flags.storelogiccomplete) {
62e76326 1534 StoreIOBuffer tempBuffer;
1535 tempBuffer.offset = next->readBuffer.offset + context->headers_sz;
1536 tempBuffer.length = next->readBuffer.length;
1537 tempBuffer.data = next->readBuffer.data;
1538
86a2f789 1539 storeClientCopy(context->sc, http->storeEntry(),
0655fa4d 1540 tempBuffer, clientReplyContext::SendMoreData, context);
62e76326 1541 return;
edce4d98 1542 }
62e76326 1543
edce4d98 1544 if (context->http->request->method == METHOD_PURGE) {
62e76326 1545 context->purgeRequest();
1546 return;
edce4d98 1547 }
62e76326 1548
edce4d98 1549 if (context->http->request->method == METHOD_TRACE) {
62e76326 1550 if (context->http->request->max_forwards == 0) {
0655fa4d 1551 context->traceReply(aNode);
62e76326 1552 return;
1553 }
1554
1555 /* continue forwarding, not finished yet. */
1556 http->logType = LOG_TCP_MISS;
1557
1558 context->doGetMoreData();
edce4d98 1559 } else
62e76326 1560 context->identifyStoreObject();
e6ccf245 1561}
1562
1563void
1564clientReplyContext::doGetMoreData()
1565{
edce4d98 1566 /* We still have to do store logic processing - vary, cache hit etc */
62e76326 1567
86a2f789 1568 if (http->storeEntry() != NULL) {
62e76326 1569 /* someone found the object in the cache for us */
1570 StoreIOBuffer tempBuffer;
34266cde 1571
1572 http->storeEntry()->lock()
1573
1574 ;
62e76326 1575
86a2f789 1576 if (http->storeEntry()->mem_obj == NULL) {
62e76326 1577 /*
1578 * This if-block exists because we don't want to clobber
1579 * a preexiting mem_obj->method value if the mem_obj
1580 * already exists. For example, when a HEAD request
1581 * is a cache hit for a GET response, we want to keep
1582 * the method as GET.
1583 */
86a2f789 1584 storeCreateMemObject(http->storeEntry(), http->uri,
62e76326 1585 http->log_uri);
86a2f789 1586 http->storeEntry()->mem_obj->method =
62e76326 1587 http->request->method;
1588 }
1589
86a2f789 1590 sc = storeClientListAdd(http->storeEntry(), this);
edce4d98 1591#if DELAY_POOLS
62e76326 1592
1593 sc->setDelayId(DelayId::DelayClient(http));
edce4d98 1594#endif
62e76326 1595
1596 assert(http->logType == LOG_TCP_HIT);
1597 reqofs = 0;
1598 /* guarantee nothing has been sent yet! */
1599 assert(http->out.size == 0);
1600 assert(http->out.offset == 0);
1601 tempBuffer.offset = reqofs;
1602 tempBuffer.length = getNextNode()->readBuffer.length;
1603 tempBuffer.data = getNextNode()->readBuffer.data;
86a2f789 1604 storeClientCopy(sc, http->storeEntry(),
0655fa4d 1605 tempBuffer, CacheHit, this);
edce4d98 1606 } else {
62e76326 1607 /* MISS CASE, http->logType is already set! */
0655fa4d 1608 processMiss();
edce4d98 1609 }
1610}
1611
1612/* the next node has removed itself from the stream. */
1613void
59a1efb2 1614clientReplyDetach(clientStreamNode * node, ClientHttpRequest * http)
edce4d98 1615{
1616 /* detach from the stream */
edce4d98 1617 clientStreamDetach(node, http);
1618}
1619
1620/*
1621 * accepts chunk of a http message in buf, parses prefix, filters headers and
1622 * such, writes processed message to the message recipient
1623 */
1624void
0655fa4d 1625clientReplyContext::SendMoreData(void *data, StoreIOBuffer result)
edce4d98 1626{
e6ccf245 1627 clientReplyContext *context = static_cast<clientReplyContext *>(data);
528b2c61 1628 context->sendMoreData (result);
1629}
1630
1631void
1632clientReplyContext::makeThisHead()
1633{
1634 /* At least, I think thats what this does */
1635 dlinkDelete(&http->active, &ClientActiveRequests);
1636 dlinkAdd(http, &http->active, &ClientActiveRequests);
1637}
1638
1639bool
1640clientReplyContext::errorInStream(StoreIOBuffer const &result, size_t const &sizeToProcess)const
1641{
1642 return /* aborted request */
86a2f789 1643 (http->storeEntry() && EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED)) ||
62e76326 1644 /* Upstream read error */ (result.flags.error) ||
1645 /* Upstream EOF */ (sizeToProcess == 0);
528b2c61 1646}
1647
1648void
1649clientReplyContext::sendStreamError(StoreIOBuffer const &result)
1650{
1651 /* call clientWriteComplete so the client socket gets closed */
1652 /* We call into the stream, because we don't know that there is a
1653 * client socket!
1654 */
1655 debug(88,5)("clientReplyContext::sendStreamError: A stream error has occured, marking as complete and sending no data.\n");
1656 StoreIOBuffer tempBuffer;
1657 flags.complete = 1;
1658 tempBuffer.flags.error = result.flags.error;
1659 clientStreamCallback((clientStreamNode*)http->client_stream.head->data, http, NULL,
62e76326 1660 tempBuffer);
528b2c61 1661}
1662
1663void
1664clientReplyContext::pushStreamData(StoreIOBuffer const &result, char *source)
1665{
1666 StoreIOBuffer tempBuffer;
62e76326 1667
528b2c61 1668 if (result.length == 0) {
62e76326 1669 debug (88,5)("clientReplyContext::pushStreamData: marking request as complete due to 0 length store result\n");
1670 flags.complete = 1;
528b2c61 1671 }
62e76326 1672
43ae1d95 1673 assert(result.offset - headers_sz == next()->readBuffer.offset);
528b2c61 1674 tempBuffer.offset = result.offset - headers_sz;
1675 tempBuffer.length = result.length;
62e76326 1676
43ae1d95 1677 if (tempBuffer.length)
1678 tempBuffer.data = source;
62e76326 1679
528b2c61 1680 clientStreamCallback((clientStreamNode*)http->client_stream.head->data, http, NULL,
62e76326 1681 tempBuffer);
528b2c61 1682}
1683
1684clientStreamNode *
1685clientReplyContext::next() const
1686{
1687 assert ( (clientStreamNode*)http->client_stream.head->next->data == getNextNode());
1688 return getNextNode();
1689}
1690
1691void
0655fa4d 1692clientReplyContext::waitForMoreData ()
528b2c61 1693{
1694 debug(88,5)("clientReplyContext::waitForMoreData: Waiting for more data to parse reply headers in client side.\n");
1695 /* We don't have enough to parse the metadata yet */
1696 /* TODO: the store should give us out of band metadata and
1697 * obsolete this routine
1698 */
1699 /* wait for more to arrive */
528b2c61 1700 startSendProcess();
1701}
1702
1703void
1704clientReplyContext::startSendProcess()
1705{
0655fa4d 1706 debug(88,5)("clientReplyContext::startSendProcess: triggering store read to SendMoreData\n");
528b2c61 1707 assert(reqofs <= HTTP_REQBUF_SZ);
1708 /* TODO: copy into the supplied buffer */
1709 StoreIOBuffer tempBuffer;
1710 tempBuffer.offset = reqofs;
1711 tempBuffer.length = next()->readBuffer.length - reqofs;
1712 tempBuffer.data = next()->readBuffer.data + reqofs;
86a2f789 1713 storeClientCopy(sc, http->storeEntry(),
0655fa4d 1714 tempBuffer, SendMoreData, this);
1715}
1716
b51aec66 1717/*
1718 * Calculates the maximum size allowed for an HTTP response
1719 */
1720void
1721clientReplyContext::buildMaxBodySize(HttpReply * reply)
1722{
ef1955a5 1723 acl_size_t *l = Config.ReplyBodySize;
1724 ACLChecklist *ch;
b51aec66 1725
8777d90e 1726 if (http->logType == LOG_TCP_DENIED)
1727 return;
1728
ef1955a5 1729 ch = clientAclChecklistCreate(NULL, http);
8777d90e 1730
7dc5f514 1731 ch->reply = HTTPMSGLOCK(reply);
ef1955a5 1732
1733 for (l = Config.ReplyBodySize; l; l = l -> next) {
7684c4b1 1734 if (ch->matchAclListFast(l->aclList)) {
43cb430e 1735 if (l->size != static_cast<size_t>(-1)) {
1736 debug(58, 3) ("clientReplyContext: Setting maxBodySize to %ld\n", (long int) l->size);
ef1955a5 1737 http->maxReplyBodySize(l->size);
43cb430e 1738 }
b51aec66 1739
ef1955a5 1740 break;
1741 }
b51aec66 1742 }
ef1955a5 1743
1744 delete ch;
b51aec66 1745}
1746
4993f571 1747void
1748clientReplyContext::processReplyAccess ()
1749{
7dc5f514 1750 assert(reply);
1751 buildMaxBodySize(reply);
62e76326 1752
7dc5f514 1753 if (http->isReplyBodyTooLarge(reply->content_length)) {
62e76326 1754 ErrorState *err =
1755 clientBuildError(ERR_TOO_BIG, HTTP_FORBIDDEN, NULL,
a2ac85d9 1756 http->getConn().getRaw() != NULL ? &http->getConn()->peer.sin_addr : &no_addr,
62e76326 1757 http->request);
86a2f789 1758 removeClientStoreReference(&sc, http);
0655fa4d 1759 startError(err);
7dc5f514 1760 HTTPMSGUNLOCK(reply);
62e76326 1761 return;
1762 }
1763
7dc5f514 1764 headers_sz = reply->hdr_sz;
62e76326 1765 ACLChecklist *replyChecklist;
1766 replyChecklist = clientAclChecklistCreate(Config.accessList.reply, http);
7dc5f514 1767 replyChecklist->reply = HTTPMSGLOCK(reply);
0655fa4d 1768 replyChecklist->nonBlockingCheck(ProcessReplyAccessResult, this);
4993f571 1769}
1770
1771void
0655fa4d 1772clientReplyContext::ProcessReplyAccessResult (int rv, void *voidMe)
4993f571 1773{
1774 clientReplyContext *me = static_cast<clientReplyContext *>(voidMe);
0655fa4d 1775 me->processReplyAccessResult(rv);
4993f571 1776}
1777
1778void
0655fa4d 1779clientReplyContext::processReplyAccessResult(bool accessAllowed)
4993f571 1780{
62e76326 1781 debug(88, 2) ("The reply for %s %s is %s, because it matched '%s'\n",
1782 RequestMethodStr[http->request->method], http->uri,
1783 accessAllowed ? "ALLOWED" : "DENIED",
1784 AclMatchedName ? AclMatchedName : "NO ACL's");
62e76326 1785
7dc5f514 1786 if (!accessAllowed && reply->sline.status != HTTP_FORBIDDEN
1787 && !alwaysAllowResponse(reply->sline.status)) {
62e76326 1788 /* the if above is slightly broken, but there is no way
1789 * to tell if this is a squid generated error page, or one from
1790 * upstream at this point. */
1791 ErrorState *err;
0185bd6f 1792 err_type page_id;
1793 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName);
1794
1795 if (page_id == ERR_NONE)
1796 page_id = ERR_ACCESS_DENIED;
1797
62e76326 1798 err =
0185bd6f 1799 clientBuildError(page_id, HTTP_FORBIDDEN, NULL,
a2ac85d9 1800 http->getConn().getRaw() != NULL ? &http->getConn()->peer.sin_addr : &no_addr,
62e76326 1801 http->request);
0185bd6f 1802
86a2f789 1803 removeClientStoreReference(&sc, http);
0185bd6f 1804
0655fa4d 1805 startError(err);
0185bd6f 1806
7dc5f514 1807 HTTPMSGUNLOCK(reply);
0185bd6f 1808
c2cf9bea 1809 http->logType = LOG_TCP_DENIED_REPLY;
0185bd6f 1810
62e76326 1811 return;
1812 }
1813
0976f8db 1814 /* Ok, the reply is allowed, */
1815 http->loggingEntry(http->storeEntry());
1816
7dc5f514 1817 ssize_t body_size = reqofs - reply->hdr_sz;
0976f8db 1818
62e76326 1819 assert(body_size >= 0);
0976f8db 1820
62e76326 1821 debug(88,3)
0655fa4d 1822 ("clientReplyContext::sendMoreData: Appending %d bytes after %d bytes of headers\n",
7dc5f514 1823 (int) body_size, reply->hdr_sz);
0976f8db 1824
43ae1d95 1825#if ESI
62e76326 1826
7dc5f514 1827 if (http->flags.accel && reply->sline.status != HTTP_FORBIDDEN &&
1828 !alwaysAllowResponse(reply->sline.status) &&
1829 esiEnableProcessing(reply)) {
43ae1d95 1830 debug(88, 2) ("Enabling ESI processing for %s\n", http->uri);
1831 clientStreamInsertHead(&http->client_stream, esiStreamRead,
1832 esiProcessStream, esiStreamDetach, esiStreamStatus, NULL);
1833 }
1834
1835#endif
5ef38a13 1836
1837#if ICAP_CLIENT_RESPMOD_POSTCACHE
1838
1839 debug(88, 0) ("Enabling ICAP processing for %s\n", http->uri);
1840
1841 clientStreamInsertHead(&http->client_stream, icapclientStreamRead,
1842 icapclientProcessStream,
1843 icapclientStreamDetach,
1844 icapclientStreamStatus,
1845 NULL);
1846
1847#endif
1848
62e76326 1849 if (http->request->method == METHOD_HEAD) {
1850 /* do not forward body for HEAD replies */
1851 body_size = 0;
1852 http->flags.done_copying = 1;
1853 flags.complete = 1;
1854 }
1855
1856 assert (!flags.headersSent);
1857 flags.headersSent = true;
1858
1859 StoreIOBuffer tempBuffer;
1860 char *buf = next()->readBuffer.data;
7dc5f514 1861 char *body_buf = buf + reply->hdr_sz;
62e76326 1862
49ea0125 1863 //Server side may disable ranges under some circumstances.
1864
1865 if ((!http->request->range))
1866 next()->readBuffer.offset = 0;
1867
62e76326 1868 if (next()->readBuffer.offset != 0) {
1869 if (next()->readBuffer.offset > body_size) {
1870 /* Can't use any of the body we recieved. send nothing */
1871 tempBuffer.length = 0;
1872 tempBuffer.data = NULL;
1873 } else {
1874 tempBuffer.length = body_size - next()->readBuffer.offset;
1875 tempBuffer.data = body_buf + next()->readBuffer.offset;
1876 }
1877 } else {
1878 tempBuffer.length = body_size;
1879 tempBuffer.data = body_buf;
1880 }
1881
0655fa4d 1882 /* TODO??: move the data in the buffer back by the request header size */
62e76326 1883 clientStreamCallback((clientStreamNode *)http->client_stream.head->data,
7dc5f514 1884 http, reply, tempBuffer);
62e76326 1885
1886 return;
4993f571 1887}
1888
528b2c61 1889void
1890clientReplyContext::sendMoreData (StoreIOBuffer result)
1891{
50c09fc4 1892 if (deleting)
1893 return;
1894
86a2f789 1895 StoreEntry *entry = http->storeEntry();
50c09fc4 1896
a2ac85d9 1897 ConnStateData::Pointer conn = http->getConn();
50c09fc4 1898
a2ac85d9 1899 int fd = conn.getRaw() != NULL ? conn->fd : -1;
50c09fc4 1900
528b2c61 1901 char *buf = next()->readBuffer.data;
50c09fc4 1902
c8be6d7b 1903 char *body_buf = buf;
edce4d98 1904
62e76326 1905 /* This is always valid until we get the headers as metadata from
528b2c61 1906 * storeClientCopy.
1907 * Then it becomes reqofs == next->readBuffer.offset()
af29fa86 1908 */
528b2c61 1909 assert(reqofs == 0 || flags.storelogiccomplete);
af29fa86 1910
528b2c61 1911 if (flags.headersSent && buf != result.data) {
62e76326 1912 /* we've got to copy some data */
1913 assert(result.length <= next()->readBuffer.length);
1914 xmemcpy(buf, result.data, result.length);
1915 body_buf = buf;
528b2c61 1916 } else if (!flags.headersSent &&
62e76326 1917 buf + reqofs !=result.data) {
1918 /* we've got to copy some data */
1919 assert(result.length + reqofs <= next()->readBuffer.length);
1920 xmemcpy(buf + reqofs, result.data, result.length);
1921 body_buf = buf;
edce4d98 1922 }
62e76326 1923
edce4d98 1924 /* We've got the final data to start pushing... */
528b2c61 1925 flags.storelogiccomplete = 1;
edce4d98 1926
4993f571 1927 reqofs += result.length;
1928
1929 assert(reqofs <= HTTP_REQBUF_SZ || flags.headersSent);
62e76326 1930
edce4d98 1931 assert(http->request != NULL);
62e76326 1932
edce4d98 1933 /* ESI TODO: remove this assert once everything is stable */
1934 assert(http->client_stream.head->data
62e76326 1935 && cbdataReferenceValid(http->client_stream.head->data));
528b2c61 1936
1937 makeThisHead();
62e76326 1938
0655fa4d 1939 debug(88, 5) ("clientReplyContext::sendMoreData: %s, %d bytes (%u new bytes)\n",
62e76326 1940 http->uri, (int) reqofs, (unsigned int)result.length);
1941
0655fa4d 1942 debug(88, 5) ("clientReplyContext::sendMoreData: FD %d '%s', out.offset=%ld \n",
62e76326 1943 fd, storeUrl(entry), (long int) http->out.offset);
528b2c61 1944
edce4d98 1945 /* update size of the request */
4993f571 1946 reqsize = reqofs;
62e76326 1947
e429f975 1948 if (http->request->flags.resetTCP()) {
62e76326 1949 /* yuck. FIXME: move to client_side.c */
1950
1951 if (fd != -1)
1952 comm_reset_close(fd);
1953
1954 return;
528b2c61 1955 }
62e76326 1956
4993f571 1957 if (errorInStream(result, reqofs)) {
62e76326 1958 sendStreamError(result);
1959 return;
edce4d98 1960 }
528b2c61 1961
1962 if (flags.headersSent) {
62e76326 1963 pushStreamData (result, buf);
1964 return;
edce4d98 1965 }
62e76326 1966
0655fa4d 1967 buildReply(buf, reqofs);
4993f571 1968 ssize_t body_size = reqofs;
62e76326 1969
7dc5f514 1970 if (reply) {
21b92762 1971
1972 /* handle headers */
1973
1974 if (Config.onoff.log_mime_hdrs) {
1975 size_t k;
1976
1977 if ((k = headersEnd(buf, reqofs))) {
1978 safe_free(http->al.headers.reply);
1979 http->al.headers.reply = (char *)xcalloc(k + 1, 1);
1980 xstrncpy(http->al.headers.reply, buf, k);
1981 }
1982 }
1983
62e76326 1984 holdingBuffer = result;
7dc5f514 1985 processReplyAccess();
62e76326 1986 return;
43ae1d95 1987
4993f571 1988 } else if (reqofs < HTTP_REQBUF_SZ && entry->store_status == STORE_PENDING) {
0655fa4d 1989 waitForMoreData();
62e76326 1990 return;
528b2c61 1991 } else if (http->request->method == METHOD_HEAD) {
62e76326 1992 /*
1993 * If we are here, then store_status == STORE_OK and it
1994 * seems we have a HEAD repsponse which is missing the
1995 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
0655fa4d 1996 * does this). Because buildReply() fails we just
62e76326 1997 * call this reply a body, set the done_copying flag and
1998 * continue...
1999 */
2000 /* RBC: Note that this is seriously broken, as we *need* the
2001 * metadata to allow further client modules to work. As such
2002 * webservers are seriously broken, this is probably not
2003 * going to get fixed.. perhapos we should remove it?
2004 */
2005 debug (88,0)("Broken head response - probably phttpd/0.99.72\n");
2006 http->flags.done_copying = 1;
2007 flags.complete = 1;
2008 /*
2009 * And as this is a malformed HTTP reply we cannot keep
2010 * the connection persistent
2011 */
2012 http->request->flags.proxy_keepalive = 0;
2013
62e76326 2014 assert(body_buf && body_size);
0655fa4d 2015 StoreIOBuffer tempBuffer (body_size, 0 ,body_buf);
62e76326 2016 clientStreamCallback((clientStreamNode *)http->client_stream.head->data,
2017 http, NULL, tempBuffer);
528b2c61 2018 } else {
62e76326 2019 debug (88,0)("clientReplyContext::sendMoreData: Unable to parse reply headers within a single HTTP_REQBUF_SZ length buffer\n");
2020 StoreIOBuffer tempBuffer;
2021 tempBuffer.flags.error = 1;
0655fa4d 2022 /* XXX FIXME: make an html error page here */
62e76326 2023 sendStreamError(tempBuffer);
2024 return;
edce4d98 2025 }
62e76326 2026
528b2c61 2027 fatal ("clientReplyContext::sendMoreData: Unreachable code reached \n");
edce4d98 2028}
2029
62e76326 2030
edce4d98 2031
2032/* Using this breaks the client layering just a little!
2033 */
0655fa4d 2034void
2035clientReplyContext::createStoreEntry(method_t m, request_flags flags)
edce4d98 2036{
0655fa4d 2037 assert(http != NULL);
edce4d98 2038 /*
2039 * For erroneous requests, we might not have a h->request,
2040 * so make a fake one.
2041 */
62e76326 2042
0655fa4d 2043 if (http->request == NULL)
6dd9f4bd 2044 http->request = HTTPMSGLOCK(new HttpRequest(m, PROTO_NONE, null_string));
62e76326 2045
0655fa4d 2046 StoreEntry *e = storeCreateEntry(http->uri, http->log_uri, flags, m);
62e76326 2047
0655fa4d 2048 sc = storeClientListAdd(e, this);
62e76326 2049
edce4d98 2050#if DELAY_POOLS
62e76326 2051
0655fa4d 2052 sc->setDelayId(DelayId::DelayClient(http));
62e76326 2053
edce4d98 2054#endif
62e76326 2055
0655fa4d 2056 reqofs = 0;
62e76326 2057
0655fa4d 2058 reqsize = 0;
62e76326 2059
edce4d98 2060 /* I don't think this is actually needed! -- adrian */
0655fa4d 2061 /* http->reqbuf = http->norm_reqbuf; */
2062 // assert(http->reqbuf == http->norm_reqbuf);
edce4d98 2063 /* The next line is illegal because we don't know if the client stream
2064 * buffers have been set up
2065 */
0655fa4d 2066 // storeClientCopy(http->sc, e, 0, HTTP_REQBUF_SZ, http->reqbuf,
2067 // SendMoreData, this);
edce4d98 2068 /* So, we mark the store logic as complete */
0655fa4d 2069 this->flags.storelogiccomplete = 1;
62e76326 2070
edce4d98 2071 /* and get the caller to request a read, from whereever they are */
62e76326 2072 /* NOTE: after ANY data flows down the pipe, even one step,
edce4d98 2073 * this function CAN NOT be used to manage errors
2074 */
86a2f789 2075 http->storeEntry(e);
edce4d98 2076}
2077
2078ErrorState *
2079clientBuildError(err_type page_id, http_status status, char const *url,
62e76326 2080
ddfcbc22 2081 struct IN_ADDR * src_addr, HttpRequest * request)
edce4d98 2082{
2083 ErrorState *err = errorCon(page_id, status);
2084 err->src_addr = *src_addr;
62e76326 2085
edce4d98 2086 if (url)
62e76326 2087 err->url = xstrdup(url);
2088
edce4d98 2089 if (request)
6dd9f4bd 2090 err->request = HTTPMSGLOCK(request);
62e76326 2091
edce4d98 2092 return err;
2093}