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