]> git.ipfire.org Git - thirdparty/squid.git/blame - src/client_side_reply.cc
Remove remaining traces of dlmalloc
[thirdparty/squid.git] / src / client_side_reply.cc
CommitLineData
edce4d98 1
2/*
92caea77 3 * $Id: client_side_reply.cc,v 1.134 2007/08/17 03:45:56 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);
528b2c61 290 /* here the data to send is the data we just recieved */
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
edce4d98 507 switch (varyEvaluateMatch(e, r)) {
62e76326 508
edce4d98 509 case VARY_NONE:
62e76326 510 /* No variance detected. Continue as normal */
511 break;
512
edce4d98 513 case VARY_MATCH:
62e76326 514 /* This is the correct entity for this request. Continue */
bf8fe701 515 debugs(88, 2, "clientProcessHit: Vary MATCH!");
62e76326 516 break;
517
edce4d98 518 case VARY_OTHER:
62e76326 519 /* This is not the correct entity for this request. We need
520 * to requery the cache.
521 */
86a2f789 522 removeClientStoreReference(&sc, http);
62e76326 523 e = NULL;
524 /* Note: varyEvalyateMatch updates the request with vary information
525 * so we only get here once. (it also takes care of cancelling loops)
526 */
bf8fe701 527 debugs(88, 2, "clientProcessHit: Vary detected!");
0655fa4d 528 clientGetMoreData(ourNode, http);
62e76326 529 return;
530
edce4d98 531 case VARY_CANCEL:
62e76326 532 /* varyEvaluateMatch found a object loop. Process as miss */
bf8fe701 533 debugs(88, 1, "clientProcessHit: Vary object loop!");
0655fa4d 534 processMiss();
62e76326 535 return;
edce4d98 536 }
62e76326 537
edce4d98 538 if (r->method == METHOD_PURGE) {
86a2f789 539 removeClientStoreReference(&sc, http);
62e76326 540 e = NULL;
0655fa4d 541 purgeRequest();
62e76326 542 return;
edce4d98 543 }
62e76326 544
3900307b 545 if (e->checkNegativeHit()
36e6655b 546#if HTTP_VIOLATIONS
547 && !r->flags.nocache_hack
548#endif
549 ) {
62e76326 550 http->logType = LOG_TCP_NEGATIVE_HIT;
0655fa4d 551 sendMoreData(result);
f7cf9b8c 552 } else if (!Config.onoff.offline && refreshCheckHTTP(e, r) && !http->flags.internal) {
bf8fe701 553 debugs(88, 5, "clientCacheHit: in refreshCheck() block");
62e76326 554 /*
555 * We hold a stale copy; it needs to be validated
556 */
557 /*
558 * The 'need_validation' flag is used to prevent forwarding
559 * loops between siblings. If our copy of the object is stale,
560 * then we should probably only use parents for the validation
561 * request. Otherwise two siblings could generate a loop if
562 * both have a stale version of the object.
563 */
564 r->flags.need_validation = 1;
565
566 if (e->lastmod < 0) {
567 /*
568 * Previous reply didn't have a Last-Modified header,
569 * we cannot revalidate it.
570 */
571 http->logType = LOG_TCP_MISS;
0655fa4d 572 processMiss();
62e76326 573 } else if (r->flags.nocache) {
574 /*
575 * This did not match a refresh pattern that overrides no-cache
576 * we should honour the client no-cache header.
577 */
578 http->logType = LOG_TCP_CLIENT_REFRESH_MISS;
0655fa4d 579 processMiss();
62e76326 580 } else if (r->protocol == PROTO_HTTP) {
581 /*
582 * Object needs to be revalidated
583 * XXX This could apply to FTP as well, if Last-Modified is known.
584 */
0655fa4d 585 processExpired();
62e76326 586 } else {
587 /*
588 * We don't know how to re-validate other protocols. Handle
589 * them as if the object has expired.
590 */
591 http->logType = LOG_TCP_MISS;
0655fa4d 592 processMiss();
62e76326 593 }
edce4d98 594 } else if (r->flags.ims) {
62e76326 595 /*
596 * Handle If-Modified-Since requests from the client
597 */
598
599 if (e->getReply()->sline.status != HTTP_OK) {
bf8fe701 600 debugs(88, 4, "clientCacheHit: Reply code " <<
601 e->getReply()->sline.status << " != 200");
62e76326 602 http->logType = LOG_TCP_MISS;
0655fa4d 603 processMiss();
604 } else if (e->modifiedSince(http->request)) {
62e76326 605 http->logType = LOG_TCP_IMS_HIT;
0655fa4d 606 sendMoreData(result);
62e76326 607 } else {
0655fa4d 608 time_t const timestamp = e->timestamp;
06a5ae20 609 HttpReply *temprep = e->getReply()->make304();
62e76326 610 http->logType = LOG_TCP_IMS_HIT;
86a2f789 611 removeClientStoreReference(&sc, http);
0655fa4d 612 createStoreEntry(http->request->method,
613 request_flags());
86a2f789 614 e = http->storeEntry();
62e76326 615 /*
616 * Copy timestamp from the original entry so the 304
617 * reply has a meaningful Age: header.
618 */
619 e->timestamp = timestamp;
db237875 620 e->replaceHttpReply(temprep);
62e76326 621 e->complete();
622 /* TODO: why put this in the store and then serialise it and then parse it again.
623 * Simply mark the request complete in our context and
624 * write the reply struct to the client side
625 */
0655fa4d 626 triggerInitialStoreRead();
62e76326 627 }
edce4d98 628 } else {
62e76326 629 /*
630 * plain ol' cache hit
631 */
632
ffdf45f2 633 if (e->store_status != STORE_OK)
849b826a 634 http->logType = LOG_TCP_MISS;
ffdf45f2 635 else if (e->mem_status == IN_MEMORY)
62e76326 636 http->logType = LOG_TCP_MEM_HIT;
637 else if (Config.onoff.offline)
638 http->logType = LOG_TCP_OFFLINE_HIT;
639
0655fa4d 640 sendMoreData(result);
edce4d98 641 }
642}
643
644/*
645 * Prepare to fetch the object as it's a cache miss of some kind.
646 */
647void
0655fa4d 648clientReplyContext::processMiss()
edce4d98 649{
edce4d98 650 char *url = http->uri;
190154cf 651 HttpRequest *r = http->request;
edce4d98 652 ErrorState *err = NULL;
bf8fe701 653 debugs(88, 4, "clientProcessMiss: '" << RequestMethodStr[r->method] << " " << url << "'");
edce4d98 654 /*
655 * We might have a left-over StoreEntry from a failed cache hit
656 * or IMS request.
657 */
62e76326 658
86a2f789 659 if (http->storeEntry()) {
660 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_SPECIAL)) {
bf8fe701 661 debugs(88, 0, "clientProcessMiss: miss on a special object (" << url << ").");
662 debugs(88, 0, "\tlog_type = " << log_tags[http->logType]);
3900307b 663 http->storeEntry()->dump(1);
62e76326 664 }
665
86a2f789 666 removeClientStoreReference(&sc, http);
edce4d98 667 }
62e76326 668
edce4d98 669 if (r->method == METHOD_PURGE) {
0655fa4d 670 purgeRequest();
62e76326 671 return;
edce4d98 672 }
62e76326 673
0655fa4d 674 if (http->onlyIfCached()) {
675 processOnlyIfCachedMiss();
62e76326 676 return;
edce4d98 677 }
62e76326 678
edce4d98 679 /*
680 * Deny loops when running in accelerator/transproxy mode.
681 */
682 if (http->flags.accel && r->flags.loopdetect) {
62e76326 683 http->al.http.code = HTTP_FORBIDDEN;
684 err =
685 clientBuildError(ERR_ACCESS_DENIED, HTTP_FORBIDDEN, NULL,
98242069 686 &http->getConn()->peer.sin_addr, http->request);
0655fa4d 687 createStoreEntry(r->method, request_flags());
86a2f789 688 errorAppendEntry(http->storeEntry(), err);
0655fa4d 689 triggerInitialStoreRead();
62e76326 690 return;
edce4d98 691 } else {
62e76326 692 assert(http->out.offset == 0);
0655fa4d 693 createStoreEntry(r->method, r->flags);
694 triggerInitialStoreRead();
62e76326 695
696 if (http->redirect.status) {
06a5ae20 697 HttpReply *rep = new HttpReply;
edce4d98 698#if LOG_TCP_REDIRECTS
62e76326 699
700 http->logType = LOG_TCP_REDIRECT;
edce4d98 701#endif
62e76326 702
d88e3c49 703 http->storeEntry()->releaseRequest();
06a5ae20 704 rep->redirect(http->redirect.status, http->redirect.location);
db237875 705 http->storeEntry()->replaceHttpReply(rep);
86a2f789 706 http->storeEntry()->complete();
62e76326 707 return;
708 }
709
710 if (http->flags.internal)
711 r->protocol = PROTO_INTERNAL;
712
94a396a3 713 FwdState::fwdStart(http->getConn() != NULL ? http->getConn()->fd : -1,
be0c6690 714 http->storeEntry(),
715 r);
edce4d98 716 }
717}
718
719/*
720 * client issued a request with an only-if-cached cache-control directive;
721 * we did not find a cached object that can be returned without
722 * contacting other servers;
723 * respond with a 504 (Gateway Timeout) as suggested in [RFC 2068]
724 */
0655fa4d 725void
726clientReplyContext::processOnlyIfCachedMiss()
edce4d98 727{
edce4d98 728 ErrorState *err = NULL;
bf8fe701 729 debugs(88, 4, "clientProcessOnlyIfCachedMiss: '" <<
730 RequestMethodStr[http->request->method] << " " << http->uri << "'");
edce4d98 731 http->al.http.code = HTTP_GATEWAY_TIMEOUT;
732 err = clientBuildError(ERR_ONLY_IF_CACHED_MISS, HTTP_GATEWAY_TIMEOUT, NULL,
98242069 733 &http->getConn()->peer.sin_addr, http->request);
86a2f789 734 removeClientStoreReference(&sc, http);
0655fa4d 735 startError(err);
edce4d98 736}
737
738void
e6ccf245 739clientReplyContext::purgeRequestFindObjectToPurge()
740{
741 /* Try to find a base entry */
742 http->flags.purging = 1;
743 lookingforstore = 1;
3b13a8fd 744 StoreEntry::getPublicByRequestMethod(this, http->request, METHOD_GET);
e6ccf245 745}
746
747void
3b13a8fd 748clientReplyContext::created(StoreEntry *newEntry)
e6ccf245 749{
750 if (lookingforstore == 1)
62e76326 751 purgeFoundGet(newEntry);
e6ccf245 752 else if (lookingforstore == 2)
62e76326 753 purgeFoundHead(newEntry);
e6ccf245 754 else if (lookingforstore == 3)
62e76326 755 purgeDoPurgeGet(newEntry);
e6ccf245 756 else if (lookingforstore == 4)
62e76326 757 purgeDoPurgeHead(newEntry);
e6ccf245 758 else if (lookingforstore == 5)
62e76326 759 identifyFoundObject(newEntry);
e6ccf245 760}
761
762void
3b13a8fd 763clientReplyContext::purgeFoundGet(StoreEntry *newEntry)
e6ccf245 764{
765 if (newEntry->isNull()) {
62e76326 766 lookingforstore = 2;
767 StoreEntry::getPublicByRequestMethod(this, http->request, METHOD_HEAD);
e6ccf245 768 } else
62e76326 769 purgeFoundObject (newEntry);
e6ccf245 770}
771
772void
3b13a8fd 773clientReplyContext::purgeFoundHead(StoreEntry *newEntry)
e6ccf245 774{
775 if (newEntry->isNull())
62e76326 776 purgeDoMissPurge();
e6ccf245 777 else
62e76326 778 purgeFoundObject (newEntry);
e6ccf245 779}
62e76326 780
e6ccf245 781void
3b13a8fd 782clientReplyContext::purgeFoundObject(StoreEntry *entry)
e6ccf245 783{
784 assert (entry && !entry->isNull());
e4b9a2f0 785
786 if (EBIT_TEST(entry->flags, ENTRY_SPECIAL)) {
787 http->logType = LOG_TCP_DENIED;
788 ErrorState *err =
789 clientBuildError(ERR_ACCESS_DENIED, HTTP_FORBIDDEN, NULL,
790 &http->getConn()->peer.sin_addr, http->request);
791 startError(err);
792 return;
793 }
794
62e76326 795 StoreIOBuffer tempBuffer;
796 /* Swap in the metadata */
86a2f789 797 http->storeEntry(entry);
34266cde 798
799 http->storeEntry()->lock()
800
801 ;
3900307b 802 http->storeEntry()->createMemObject(http->uri, http->log_uri);
34266cde 803
86a2f789 804 http->storeEntry()->mem_obj->method = http->request->method;
34266cde 805
86a2f789 806 sc = storeClientListAdd(http->storeEntry(), this);
34266cde 807
62e76326 808 http->logType = LOG_TCP_HIT;
34266cde 809
62e76326 810 reqofs = 0;
34266cde 811
62e76326 812 tempBuffer.offset = http->out.offset;
34266cde 813
0655fa4d 814 tempBuffer.length = next()->readBuffer.length;
34266cde 815
0655fa4d 816 tempBuffer.data = next()->readBuffer.data;
34266cde 817
86a2f789 818 storeClientCopy(sc, http->storeEntry(),
0655fa4d 819 tempBuffer, CacheHit, this);
e6ccf245 820}
821
822void
823clientReplyContext::purgeRequest()
edce4d98 824{
bf8fe701 825 debugs(88, 3, "Config2.onoff.enable_purge = " <<
826 Config2.onoff.enable_purge);
62e76326 827
edce4d98 828 if (!Config2.onoff.enable_purge) {
62e76326 829 http->logType = LOG_TCP_DENIED;
830 ErrorState *err =
831 clientBuildError(ERR_ACCESS_DENIED, HTTP_FORBIDDEN, NULL,
98242069 832 &http->getConn()->peer.sin_addr, http->request);
0655fa4d 833 startError(err);
62e76326 834 return;
edce4d98 835 }
62e76326 836
edce4d98 837 /* Release both IP cache */
838 ipcacheInvalidate(http->request->host);
839
e6ccf245 840 if (!http->flags.purging)
62e76326 841 purgeRequestFindObjectToPurge();
e6ccf245 842 else
62e76326 843 purgeDoMissPurge();
e6ccf245 844}
845
846void
847clientReplyContext::purgeDoMissPurge()
848{
29b8d8d6 849 http->logType = LOG_TCP_MISS;
e6ccf245 850 lookingforstore = 3;
3b13a8fd 851 StoreEntry::getPublicByRequestMethod(this,http->request, METHOD_GET);
e6ccf245 852}
853
854void
3b13a8fd 855clientReplyContext::purgeDoPurgeGet(StoreEntry *newEntry)
e6ccf245 856{
857 assert (newEntry);
858 /* Move to new() when that is created */
859 purgeStatus = HTTP_NOT_FOUND;
860
861 if (!newEntry->isNull()) {
62e76326 862 /* Release the cached URI */
bf8fe701 863 debugs(88, 4, "clientPurgeRequest: GET '" << newEntry->url() << "'" );
5f33b71d 864 newEntry->release();
62e76326 865 purgeStatus = HTTP_OK;
edce4d98 866 }
62e76326 867
e6ccf245 868 lookingforstore = 4;
3b13a8fd 869 StoreEntry::getPublicByRequestMethod(this, http->request, METHOD_HEAD);
e6ccf245 870}
871
872void
3b13a8fd 873clientReplyContext::purgeDoPurgeHead(StoreEntry *newEntry)
e6ccf245 874{
4fc0ac76 875 if (newEntry && !newEntry->isNull()) {
bf8fe701 876 debugs(88, 4, "clientPurgeRequest: HEAD '" << newEntry->url() << "'" );
5f33b71d 877 newEntry->release();
62e76326 878 purgeStatus = HTTP_OK;
edce4d98 879 }
62e76326 880
edce4d98 881 /* And for Vary, release the base URI if none of the headers was included in the request */
62e76326 882
edce4d98 883 if (http->request->vary_headers
62e76326 884 && !strstr(http->request->vary_headers, "=")) {
885 StoreEntry *entry = storeGetPublic(urlCanonical(http->request), METHOD_GET);
886
887 if (entry) {
bf8fe701 888 debugs(88, 4, "clientPurgeRequest: Vary GET '" << entry->url() << "'" );
5f33b71d 889 entry->release();
62e76326 890 purgeStatus = HTTP_OK;
891 }
892
893 entry = storeGetPublic(urlCanonical(http->request), METHOD_HEAD);
894
895 if (entry) {
bf8fe701 896 debugs(88, 4, "clientPurgeRequest: Vary HEAD '" << entry->url() << "'" );
5f33b71d 897 entry->release();
62e76326 898 purgeStatus = HTTP_OK;
899 }
edce4d98 900 }
62e76326 901
edce4d98 902 /*
903 * Make a new entry to hold the reply to be written
904 * to the client.
905 */
528b2c61 906 /* FIXME: This doesn't need to go through the store. Simply
907 * push down the client chain
908 */
0655fa4d 909 createStoreEntry(http->request->method, request_flags());
62e76326 910
0655fa4d 911 triggerInitialStoreRead();
62e76326 912
7dc5f514 913 HttpReply *rep = new HttpReply;
62e76326 914
450e0c10 915 HttpVersion version(1,0);
62e76326 916
7dc5f514 917 rep->setHeaders(version, purgeStatus, NULL, NULL, 0, 0, -1);
62e76326 918
7dc5f514 919 http->storeEntry()->replaceHttpReply(rep);
62e76326 920
86a2f789 921 http->storeEntry()->complete();
edce4d98 922}
923
924void
0655fa4d 925clientReplyContext::traceReply(clientStreamNode * node)
edce4d98 926{
e6ccf245 927 clientStreamNode *next = (clientStreamNode *)node->node.next->data;
528b2c61 928 StoreIOBuffer tempBuffer;
0655fa4d 929 assert(http->request->max_forwards == 0);
930 createStoreEntry(http->request->method, request_flags());
931 tempBuffer.offset = next->readBuffer.offset + headers_sz;
c8be6d7b 932 tempBuffer.length = next->readBuffer.length;
933 tempBuffer.data = next->readBuffer.data;
86a2f789 934 storeClientCopy(sc, http->storeEntry(),
0655fa4d 935 tempBuffer, SendMoreData, this);
d88e3c49 936 http->storeEntry()->releaseRequest();
3900307b 937 http->storeEntry()->buffer();
7dc5f514 938 HttpReply *rep = new HttpReply;
450e0c10 939 HttpVersion version(1,0);
06a5ae20 940 rep->setHeaders(version, HTTP_OK, NULL, "text/plain",
5cafad19 941 http->request->prefixLen(), 0, squid_curtime);
db237875 942 http->storeEntry()->replaceHttpReply(rep);
5cafad19 943 http->request->swapOut(http->storeEntry());
86a2f789 944 http->storeEntry()->complete();
edce4d98 945}
946
947#define SENDING_BODY 0
948#define SENDING_HDRSONLY 1
949int
0655fa4d 950clientReplyContext::checkTransferDone()
edce4d98 951{
86a2f789 952 StoreEntry *entry = http->storeEntry();
62e76326 953
edce4d98 954 if (entry == NULL)
62e76326 955 return 0;
956
957 /*
edce4d98 958 * For now, 'done_copying' is used for special cases like
959 * Range and HEAD requests.
960 */
0655fa4d 961 if (http->flags.done_copying)
62e76326 962 return 1;
963
edce4d98 964 /*
965 * Handle STORE_OK objects.
966 * objectLen(entry) will be set proprely.
967 * RC: Does objectLen(entry) include the Headers?
0e3be1ea 968 * RC: Yes.
edce4d98 969 */
970 if (entry->store_status == STORE_OK) {
0655fa4d 971 return storeOKTransferDone();
e39b9382 972 } else {
0655fa4d 973 return storeNotOKTransferDone();
edce4d98 974 }
e39b9382 975}
976
977int
978clientReplyContext::storeOKTransferDone() const
979{
47f6e231 980 if (http->out.offset >= http->storeEntry()->objectLen() - headers_sz) {
981 debugs(88,3,HERE << "storeOKTransferDone " <<
982 " out.offset=" << http->out.offset <<
983 " objectLen()=" << http->storeEntry()->objectLen() <<
984 " headers_sz=" << headers_sz);
62e76326 985 return 1;
47f6e231 986 }
62e76326 987
e39b9382 988 return 0;
989}
990
991int
992clientReplyContext::storeNotOKTransferDone() const
993{
edce4d98 994 /*
995 * Now, handle STORE_PENDING objects
996 */
86a2f789 997 MemObject *mem = http->storeEntry()->mem_obj;
edce4d98 998 assert(mem != NULL);
999 assert(http->request != NULL);
0e3be1ea 1000 /* mem->reply was wrong because it uses the UPSTREAM header length!!! */
0353e724 1001 HttpReply const *reply = mem->getReply();
62e76326 1002
e39b9382 1003 if (headers_sz == 0)
62e76326 1004 /* haven't found end of headers yet */
1005 return 0;
e39b9382 1006
1007 int sending = SENDING_BODY;
62e76326 1008
e39b9382 1009 if (reply->sline.status == HTTP_NO_CONTENT ||
62e76326 1010 reply->sline.status == HTTP_NOT_MODIFIED ||
1011 reply->sline.status < HTTP_OK ||
1012 http->request->method == METHOD_HEAD)
1013 sending = SENDING_HDRSONLY;
1014
edce4d98 1015 /*
1016 * Figure out how much data we are supposed to send.
1017 * If we are sending a body and we don't have a content-length,
1018 * then we must wait for the object to become STORE_OK.
1019 */
0e3be1ea 1020 if (reply->content_length < 0)
62e76326 1021 return 0;
e39b9382 1022
47f6e231 1023 int64_t expectedLength = reply->content_length + http->out.headers_sz;
62e76326 1024
e39b9382 1025 if (http->out.size < expectedLength)
62e76326 1026 return 0;
47f6e231 1027 else {
1028 debugs(88,3,HERE << "storeNotOKTransferDone " <<
1029 " out.size=" << http->out.size <<
1030 " expectedLength=" << expectedLength);
62e76326 1031 return 1;
47f6e231 1032 }
edce4d98 1033}
1034
1035
edce4d98 1036/* A write has completed, what is the next status based on the
1037 * canonical request data?
1038 * 1 something is wrong
1039 * 0 nothing is wrong.
1040 *
1041 */
1042int
59a1efb2 1043clientHttpRequestStatus(int fd, ClientHttpRequest const *http)
edce4d98 1044{
47f6e231 1045#if SIZEOF_INT64_T == 4
edce4d98 1046 if (http->out.size > 0x7FFF0000) {
47f6e231 1047 debugs(88, 1, "WARNING: closing FD " << fd << " to prevent out.size counter overflow");
1048 debugs(88, 1, "\tclient " << (inet_ntoa(http->getConn() != NULL ? http->getConn()->peer.sin_addr : no_addr)));
1049 debugs(88, 1, "\treceived " << http->out.size << " bytes");
1050 debugs(88, 1, "\tURI " << http->log_uri);
62e76326 1051 return 1;
edce4d98 1052 }
62e76326 1053
edce4d98 1054#endif
47f6e231 1055#if SIZEOF_INT64_T == 4
edce4d98 1056 if (http->out.offset > 0x7FFF0000) {
3ec12e4a 1057 debugs(88, 1, "WARNING: closing FD " << fd < " to prevent out.offset counter overflow");
1058 debugs(88, 1, "\tclient " << (inet_ntoa(http->getConn() != NULL ? http->getConn()->peer.sin_addr : no_addr)));
1059 debugs(88, 1, "\treceived " << http->out.size << " bytes, offset " << http->out.offset);
1060 debugs(88, 1, "\tURI " << http->log_uri);
62e76326 1061 return 1;
edce4d98 1062 }
62e76326 1063
edce4d98 1064#endif
1065 return 0;
1066}
1067
62e76326 1068/* Preconditions:
edce4d98 1069 * *http is a valid structure.
1070 * fd is either -1, or an open fd.
1071 *
1072 * TODO: enumify this
1073 *
1074 * This function is used by any http request sink, to determine the status
1075 * of the object.
1076 */
1077clientStream_status_t
59a1efb2 1078clientReplyStatus(clientStreamNode * aNode, ClientHttpRequest * http)
edce4d98 1079{
0655fa4d 1080 clientReplyContext *context = dynamic_cast<clientReplyContext *>(aNode->data.getRaw());
1081 assert (context);
1082 assert (context->http == http);
1083 return context->replyStatus();
1084}
1085
1086clientStream_status_t
1087clientReplyContext::replyStatus()
1088{
edce4d98 1089 int done;
1090 /* Here because lower nodes don't need it */
62e76326 1091
86a2f789 1092 if (http->storeEntry() == NULL)
62e76326 1093 return STREAM_FAILED; /* yuck, but what can we do? */
1094
86a2f789 1095 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED))
62e76326 1096 /* TODO: Could upstream read errors (result.flags.error) be
1097 * lost, and result in undersize requests being considered
1098 * complete. Should we tcp reset such connections ?
1099 */
1100 return STREAM_FAILED;
1101
0655fa4d 1102 if ((done = checkTransferDone()) != 0 || flags.complete) {
bf8fe701 1103 debugs(88, 5, "clientReplyStatus: transfer is DONE");
62e76326 1104 /* Ok we're finished, but how? */
1105
06a5ae20 1106 if (http->storeEntry()->getReply()->bodySize(http->request->method) < 0) {
bf8fe701 1107 debugs(88, 5, "clientReplyStatus: closing, content_length < 0");
62e76326 1108 return STREAM_FAILED;
1109 }
1110
1111 if (!done) {
bf8fe701 1112 debugs(88, 5, "clientReplyStatus: closing, !done, but read 0 bytes");
62e76326 1113 return STREAM_FAILED;
1114 }
1115
0655fa4d 1116 if (!http->gotEnough()) {
bf8fe701 1117 debugs(88, 5, "clientReplyStatus: client didn't get all it expected");
62e76326 1118 return STREAM_UNPLANNED_COMPLETE;
1119 }
1120
1121 if (http->request->flags.proxy_keepalive) {
bf8fe701 1122 debugs(88, 5, "clientReplyStatus: stream complete and can keepalive");
62e76326 1123 return STREAM_COMPLETE;
1124 }
1125
bf8fe701 1126 debugs(88, 5, "clientReplyStatus: stream was not expected to complete!");
62e76326 1127 return STREAM_UNPLANNED_COMPLETE;
edce4d98 1128 }
62e76326 1129
b51aec66 1130 if (http->isReplyBodyTooLarge(http->out.offset - 4096)) {
1131 /* 4096 is a margin for the HTTP headers included in out.offset */
bf8fe701 1132 debugs(88, 5, "clientReplyStatus: client reply body is too large");
62e76326 1133 return STREAM_FAILED;
0e3be1ea 1134 }
62e76326 1135
edce4d98 1136 return STREAM_NONE;
1137}
1138
62e76326 1139/* Responses with no body will not have a content-type header,
edce4d98 1140 * which breaks the rep_mime_type acl, which
1141 * coincidentally, is the most common acl for reply access lists.
1142 * A better long term fix for this is to allow acl matchs on the various
1143 * status codes, and then supply a default ruleset that puts these
1144 * codes before any user defines access entries. That way the user
1145 * can choose to block these responses where appropriate, but won't get
1146 * mysterious breakages.
1147 */
0655fa4d 1148bool
1149clientReplyContext::alwaysAllowResponse(http_status sline) const
edce4d98 1150{
4ef4b952 1151 bool result;
1152
edce4d98 1153 switch (sline) {
62e76326 1154
edce4d98 1155 case HTTP_CONTINUE:
62e76326 1156
edce4d98 1157 case HTTP_SWITCHING_PROTOCOLS:
62e76326 1158
edce4d98 1159 case HTTP_PROCESSING:
62e76326 1160
edce4d98 1161 case HTTP_NO_CONTENT:
62e76326 1162
edce4d98 1163 case HTTP_NOT_MODIFIED:
4ef4b952 1164 result = true;
62e76326 1165 break;
1166
edce4d98 1167 default:
4ef4b952 1168 result = false;
edce4d98 1169 }
4ef4b952 1170
1171 return result;
edce4d98 1172}
1173
1174/*
1175 * filters out unwanted entries from original reply header
1176 * adds extra entries if we have more info than origin server
1177 * adds Squid specific entries
1178 */
0655fa4d 1179void
1180clientReplyContext::buildReplyHeader()
edce4d98 1181{
7dc5f514 1182 HttpHeader *hdr = &reply->header;
c8be6d7b 1183 int is_hit = logTypeIsATcpHit(http->logType);
190154cf 1184 HttpRequest *request = http->request;
edce4d98 1185#if DONT_FILTER_THESE
1186 /* but you might want to if you run Squid as an HTTP accelerator */
a9925b40 1187 /* hdr->delById(HDR_ACCEPT_RANGES); */
1188 hdr->delById(HDR_ETAG);
edce4d98 1189#endif
62e76326 1190
ba9fb01d 1191 // TODO: Should ESIInclude.cc that calls removeConnectionHeaderEntries
1192 // also delete HDR_PROXY_CONNECTION and HDR_KEEP_ALIVE like we do below?
1193
1194 // XXX: Should HDR_PROXY_CONNECTION by studied instead of HDR_CONNECTION?
1195 // httpHeaderHasConnDir does that but we do not. Is this is a bug?
a9925b40 1196 hdr->delById(HDR_PROXY_CONNECTION);
edce4d98 1197 /* here: Keep-Alive is a field-name, not a connection directive! */
ba9fb01d 1198 hdr->delById(HDR_KEEP_ALIVE);
edce4d98 1199 /* remove Set-Cookie if a hit */
62e76326 1200
edce4d98 1201 if (is_hit)
a9925b40 1202 hdr->delById(HDR_SET_COOKIE);
62e76326 1203
7dc5f514 1204 /*
1205 * Be sure to obey the Connection header
1206 */
1207 reply->header.removeConnectionHeaderEntries();
62e76326 1208
1209 // if (request->range)
7dc5f514 1210 // clientBuildRangeHeader(http, reply);
edce4d98 1211 /*
1212 * Add a estimated Age header on cache hits.
1213 */
1214 if (is_hit) {
62e76326 1215 /*
1216 * Remove any existing Age header sent by upstream caches
1217 * (note that the existing header is passed along unmodified
1218 * on cache misses)
1219 */
a9925b40 1220 hdr->delById(HDR_AGE);
62e76326 1221 /*
1222 * This adds the calculated object age. Note that the details of the
1223 * age calculation is performed by adjusting the timestamp in
3900307b 1224 * StoreEntry::timestampsSet(), not here.
62e76326 1225 *
1226 * BROWSER WORKAROUND: IE sometimes hangs when receiving a 0 Age
1227 * header, so don't use it unless there is a age to report. Please
1228 * note that Age is only used to make a conservative estimation of
1229 * the objects age, so a Age: 0 header does not add any useful
1230 * information to the reply in any case.
1231 */
1232
86a2f789 1233 if (NULL == http->storeEntry())
62e76326 1234 (void) 0;
86a2f789 1235 else if (http->storeEntry()->timestamp < 0)
62e76326 1236 (void) 0;
bbe58ab5 1237
1238 if (EBIT_TEST(http->storeEntry()->flags, ENTRY_SPECIAL)) {
a9925b40 1239 hdr->delById(HDR_DATE);
1240 hdr->insertTime(HDR_DATE, squid_curtime);
bbe58ab5 1241 } else if (http->storeEntry()->timestamp < squid_curtime) {
a9925b40 1242 hdr->putInt(HDR_AGE,
1243 squid_curtime - http->storeEntry()->timestamp);
62e76326 1244 /* Signal old objects. NB: rfc 2616 is not clear,
1245 * by implication, on whether we should do this to all
1246 * responses, or only cache hits.
1247 * 14.46 states it ONLY applys for heuristically caclulated
1248 * freshness values, 13.2.4 doesn't specify the same limitation.
1249 * We interpret RFC 2616 under the combination.
1250 */
1251 /* TODO: if maxage or s-maxage is present, don't do this */
1252
86a2f789 1253 if (squid_curtime - http->storeEntry()->timestamp >= 86400) {
62e76326 1254 char tempbuf[512];
1255 snprintf (tempbuf, sizeof(tempbuf), "%s %s %s",
1256 "113", ThisCache,
1257 "This cache hit is still fresh and more than 1 day old");
a9925b40 1258 hdr->putStr(HDR_WARNING, tempbuf);
62e76326 1259 }
1260 }
528b2c61 1261
edce4d98 1262 }
62e76326 1263
0bd9aa82 1264 /* Filter unproxyable authentication types */
ddd31ed3 1265 if (http->logType != LOG_TCP_DENIED &&
a9925b40 1266 (hdr->has(HDR_WWW_AUTHENTICATE) || hdr->has(HDR_PROXY_AUTHENTICATE))) {
62e76326 1267 HttpHeaderPos pos = HttpHeaderInitPos;
1268 HttpHeaderEntry *e;
1269
ba9fb01d 1270 int headers_deleted = 0;
a9925b40 1271 while ((e = hdr->getEntry(&pos))) {
62e76326 1272 if (e->id == HDR_WWW_AUTHENTICATE || e->id == HDR_PROXY_AUTHENTICATE) {
30abd221 1273 const char *value = e->value.buf();
62e76326 1274
1275 if ((strncasecmp(value, "NTLM", 4) == 0 &&
1276 (value[4] == '\0' || value[4] == ' '))
1277 ||
1278 (strncasecmp(value, "Negotiate", 9) == 0 &&
1279 (value[9] == '\0' || value[9] == ' ')))
ba9fb01d 1280 hdr->delAt(pos, headers_deleted);
62e76326 1281 }
1282 }
ba9fb01d 1283 if (headers_deleted)
1284 hdr->refreshMask();
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) {
bf8fe701 1305 debugs(88, 3, "clientBuildReplyHeader: can't keep-alive, unknown body size" );
62e76326 1306 request->flags.proxy_keepalive = 0;
edce4d98 1307 }
62e76326 1308
f5e45ad8 1309 if (fdUsageHigh()&& !request->flags.must_keepalive) {
bf8fe701 1310 debugs(88, 3, "clientBuildReplyHeader: Not many unused FDs, can't keep-alive");
a1ca9253 1311 request->flags.proxy_keepalive = 0;
1312 }
1313
58850d15 1314 if (!Config.onoff.error_pconns && reply->sline.status >= 400 && !request->flags.must_keepalive) {
bf8fe701 1315 debugs(33, 3, "clientBuildReplyHeader: Error, don't keep-alive");
58850d15 1316 request->flags.proxy_keepalive = 0;
1317 }
1318
f5e45ad8 1319 if (!Config.onoff.client_pconns && !request->flags.must_keepalive)
1320 request->flags.proxy_keepalive = 0;
1321
4dea3fdd 1322 /* Append VIA */
92caea77 1323 if (Config.onoff.via) {
62e76326 1324 LOCAL_ARRAY(char, bbuf, MAX_URL + 32);
30abd221 1325 String strVia;
aaac15e6 1326 hdr->getList(HDR_VIA, &strVia);
62e76326 1327 snprintf(bbuf, sizeof(bbuf), "%d.%d %s",
7dc5f514 1328 reply->sline.version.major,
1329 reply->sline.version.minor,
62e76326 1330 ThisCache);
1331 strListAdd(&strVia, bbuf, ',');
a9925b40 1332 hdr->delById(HDR_VIA);
30abd221 1333 hdr->putStr(HDR_VIA, strVia.buf());
c8be6d7b 1334 }
edce4d98 1335 /* Signal keep-alive if needed */
a9925b40 1336 hdr->putStr(http->flags.accel ? HDR_CONNECTION : HDR_PROXY_CONNECTION,
1337 request->flags.proxy_keepalive ? "keep-alive" : "close");
62e76326 1338
edce4d98 1339#if ADD_X_REQUEST_URI
1340 /*
1341 * Knowing the URI of the request is useful when debugging persistent
1342 * connections in a client; we cannot guarantee the order of http headers,
1343 * but X-Request-URI is likely to be the very last header to ease use from a
1344 * debugger [hdr->entries.count-1].
1345 */
a9925b40 1346 hdr->putStr(HDR_X_REQUEST_URI,
1347 http->memOjbect()->url ? http->memObject()->url : http->uri);
62e76326 1348
edce4d98 1349#endif
62e76326 1350
8c01ada0 1351 httpHdrMangleList(hdr, request, ROR_REPLY);
edce4d98 1352}
1353
1354
0655fa4d 1355void
1356clientReplyContext::buildReply(const char *buf, size_t size)
edce4d98 1357{
edce4d98 1358 size_t k = headersEnd(buf, size);
62e76326 1359
0655fa4d 1360 if (!k)
1361 return;
1362
7dc5f514 1363 assert(reply == NULL);
0655fa4d 1364
7dc5f514 1365 HttpReply *rep = new HttpReply;
1366
1367 reply = HTTPMSGLOCK(rep);
1368
1369 if (!reply->parseCharBuf(buf, k)) {
62e76326 1370 /* parsing failure, get rid of the invalid reply */
7dc5f514 1371 HTTPMSGUNLOCK(reply);
62e76326 1372
0655fa4d 1373 if (http->request->range) {
7dc5f514 1374 debugs(0,0,HERE << "look for bug here");
62e76326 1375 /* this will fail and destroy request->range */
7dc5f514 1376 // clientBuildRangeHeader(http, reply);
62e76326 1377 }
b51aec66 1378
979da574 1379 return;
edce4d98 1380 }
62e76326 1381
0655fa4d 1382 /* enforce 1.0 reply version */
7dc5f514 1383 reply->sline.version = HttpVersion(1,0);
0655fa4d 1384
1385 /* do header conversions */
1386 buildReplyHeader();
edce4d98 1387}
1388
e6ccf245 1389void
1390clientReplyContext::identifyStoreObject()
edce4d98 1391{
190154cf 1392 HttpRequest *r = http->request;
62e76326 1393
e6ccf245 1394 if (r->flags.cachable || r->flags.internal) {
62e76326 1395 lookingforstore = 5;
1396 StoreEntry::getPublicByRequest (this, r);
559da936 1397 } else {
62e76326 1398 identifyFoundObject (NullStoreEntry::getInstance());
559da936 1399 }
e6ccf245 1400}
1401
1402void
1403clientReplyContext::identifyFoundObject(StoreEntry *newEntry)
1404{
1405 StoreEntry *e = newEntry;
190154cf 1406 HttpRequest *r = http->request;
62e76326 1407
e6ccf245 1408 if (e->isNull()) {
86a2f789 1409 http->storeEntry(NULL);
e6ccf245 1410 } else {
86a2f789 1411 http->storeEntry(e);
e6ccf245 1412 }
62e76326 1413
86a2f789 1414 e = http->storeEntry();
a12a049a 1415 /* Release IP-cache entries on reload */
1416
1417 if (r->flags.nocache) {
1418
1419#if USE_DNSSERVERS
62e76326 1420
62e76326 1421 ipcacheInvalidate(r->host);
1422
a12a049a 1423#else
1424
1425 ipcacheInvalidateNegative(r->host);
1426
1427#endif /* USE_DNSSERVERS */
1428
1429 }
1430
edce4d98 1431#if HTTP_VIOLATIONS
62e76326 1432
a12a049a 1433 else if (r->flags.nocache_hack) {
1434
1435#if USE_DNSSERVERS
1436
62e76326 1437 ipcacheInvalidate(r->host);
1438
a12a049a 1439#else
1440
1441 ipcacheInvalidateNegative(r->host);
1442
1443#endif /* USE_DNSSERVERS */
1444
1445 }
1446
1447#endif /* HTTP_VIOLATIONS */
edce4d98 1448#if USE_CACHE_DIGESTS
62e76326 1449
86a2f789 1450 lookup_type = http->storeEntry() ? "HIT" : "MISS";
62e76326 1451
edce4d98 1452#endif
62e76326 1453
86a2f789 1454 if (NULL == http->storeEntry()) {
62e76326 1455 /* this object isn't in the cache */
bf8fe701 1456 debugs(85, 3, "clientProcessRequest2: storeGet() MISS");
62e76326 1457 http->logType = LOG_TCP_MISS;
1458 doGetMoreData();
1459 return;
edce4d98 1460 }
62e76326 1461
edce4d98 1462 if (Config.onoff.offline) {
bf8fe701 1463 debugs(85, 3, "clientProcessRequest2: offline HIT");
62e76326 1464 http->logType = LOG_TCP_HIT;
1465 doGetMoreData();
1466 return;
edce4d98 1467 }
62e76326 1468
edce4d98 1469 if (http->redirect.status) {
62e76326 1470 /* force this to be a miss */
86a2f789 1471 http->storeEntry(NULL);
62e76326 1472 http->logType = LOG_TCP_MISS;
1473 doGetMoreData();
1474 return;
edce4d98 1475 }
62e76326 1476
3900307b 1477 if (!e->validToSend()) {
bf8fe701 1478 debugs(85, 3, "clientProcessRequest2: !storeEntryValidToSend MISS" );
86a2f789 1479 http->storeEntry(NULL);
62e76326 1480 http->logType = LOG_TCP_MISS;
1481 doGetMoreData();
1482 return;
edce4d98 1483 }
62e76326 1484
edce4d98 1485 if (EBIT_TEST(e->flags, ENTRY_SPECIAL)) {
62e76326 1486 /* Special entries are always hits, no matter what the client says */
bf8fe701 1487 debugs(85, 3, "clientProcessRequest2: ENTRY_SPECIAL HIT");
62e76326 1488 http->logType = LOG_TCP_HIT;
1489 doGetMoreData();
1490 return;
edce4d98 1491 }
62e76326 1492
edce4d98 1493 if (r->flags.nocache) {
bf8fe701 1494 debugs(85, 3, "clientProcessRequest2: no-cache REFRESH MISS");
86a2f789 1495 http->storeEntry(NULL);
62e76326 1496 http->logType = LOG_TCP_CLIENT_REFRESH_MISS;
1497 doGetMoreData();
1498 return;
edce4d98 1499 }
62e76326 1500
bf8fe701 1501 debugs(85, 3, "clientProcessRequest2: default HIT");
e6ccf245 1502 http->logType = LOG_TCP_HIT;
1503 doGetMoreData();
edce4d98 1504}
1505
1506/* Request more data from the store for the client Stream
1507 * This is *the* entry point to this module.
1508 *
1509 * Preconditions:
1510 * This is the head of the list.
1511 * There is at least one more node.
1512 * data context is not null
1513 */
1514void
59a1efb2 1515clientGetMoreData(clientStreamNode * aNode, ClientHttpRequest * http)
edce4d98 1516{
edce4d98 1517 /* Test preconditions */
e6ccf245 1518 assert(aNode != NULL);
1519 assert(cbdataReferenceValid(aNode));
e6ccf245 1520 assert(aNode->node.prev == NULL);
1521 assert(aNode->node.next != NULL);
0655fa4d 1522 clientReplyContext *context = dynamic_cast<clientReplyContext *>(aNode->data.getRaw());
1523 assert (context);
edce4d98 1524 assert(context->http == http);
1525
559da936 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 */
86a2f789 1567 if (http->storeEntry() != NULL) {
62e76326 1568 /* someone found the object in the cache for us */
1569 StoreIOBuffer tempBuffer;
34266cde 1570
1571 http->storeEntry()->lock()
1572
1573 ;
62e76326 1574
86a2f789 1575 if (http->storeEntry()->mem_obj == NULL) {
62e76326 1576 /*
1577 * This if-block exists because we don't want to clobber
1578 * a preexiting mem_obj->method value if the mem_obj
1579 * already exists. For example, when a HEAD request
1580 * is a cache hit for a GET response, we want to keep
1581 * the method as GET.
1582 */
3900307b 1583 http->storeEntry()->createMemObject(http->uri, http->log_uri);
86a2f789 1584 http->storeEntry()->mem_obj->method =
62e76326 1585 http->request->method;
1586 }
1587
86a2f789 1588 sc = storeClientListAdd(http->storeEntry(), this);
edce4d98 1589#if DELAY_POOLS
62e76326 1590
1591 sc->setDelayId(DelayId::DelayClient(http));
edce4d98 1592#endif
62e76326 1593
1594 assert(http->logType == LOG_TCP_HIT);
1595 reqofs = 0;
1596 /* guarantee nothing has been sent yet! */
1597 assert(http->out.size == 0);
1598 assert(http->out.offset == 0);
1599 tempBuffer.offset = reqofs;
1600 tempBuffer.length = getNextNode()->readBuffer.length;
1601 tempBuffer.data = getNextNode()->readBuffer.data;
559da936 1602 storeClientCopy(sc, http->storeEntry(), tempBuffer, CacheHit, this);
edce4d98 1603 } else {
62e76326 1604 /* MISS CASE, http->logType is already set! */
0655fa4d 1605 processMiss();
edce4d98 1606 }
1607}
1608
1609/* the next node has removed itself from the stream. */
1610void
59a1efb2 1611clientReplyDetach(clientStreamNode * node, ClientHttpRequest * http)
edce4d98 1612{
1613 /* detach from the stream */
edce4d98 1614 clientStreamDetach(node, http);
1615}
1616
1617/*
1618 * accepts chunk of a http message in buf, parses prefix, filters headers and
1619 * such, writes processed message to the message recipient
1620 */
1621void
0655fa4d 1622clientReplyContext::SendMoreData(void *data, StoreIOBuffer result)
edce4d98 1623{
e6ccf245 1624 clientReplyContext *context = static_cast<clientReplyContext *>(data);
528b2c61 1625 context->sendMoreData (result);
1626}
1627
1628void
1629clientReplyContext::makeThisHead()
1630{
1631 /* At least, I think thats what this does */
1632 dlinkDelete(&http->active, &ClientActiveRequests);
1633 dlinkAdd(http, &http->active, &ClientActiveRequests);
1634}
1635
1636bool
1637clientReplyContext::errorInStream(StoreIOBuffer const &result, size_t const &sizeToProcess)const
1638{
1639 return /* aborted request */
86a2f789 1640 (http->storeEntry() && EBIT_TEST(http->storeEntry()->flags, ENTRY_ABORTED)) ||
62e76326 1641 /* Upstream read error */ (result.flags.error) ||
1642 /* Upstream EOF */ (sizeToProcess == 0);
528b2c61 1643}
1644
1645void
1646clientReplyContext::sendStreamError(StoreIOBuffer const &result)
1647{
1648 /* call clientWriteComplete so the client socket gets closed */
1649 /* We call into the stream, because we don't know that there is a
1650 * client socket!
1651 */
bf8fe701 1652 debugs(88, 5, "clientReplyContext::sendStreamError: A stream error has occured, marking as complete and sending no data.");
528b2c61 1653 StoreIOBuffer tempBuffer;
1654 flags.complete = 1;
1655 tempBuffer.flags.error = result.flags.error;
1656 clientStreamCallback((clientStreamNode*)http->client_stream.head->data, http, NULL,
62e76326 1657 tempBuffer);
528b2c61 1658}
1659
1660void
1661clientReplyContext::pushStreamData(StoreIOBuffer const &result, char *source)
1662{
1663 StoreIOBuffer tempBuffer;
62e76326 1664
528b2c61 1665 if (result.length == 0) {
bf8fe701 1666 debugs(88, 5, "clientReplyContext::pushStreamData: marking request as complete due to 0 length store result");
62e76326 1667 flags.complete = 1;
528b2c61 1668 }
62e76326 1669
43ae1d95 1670 assert(result.offset - headers_sz == next()->readBuffer.offset);
528b2c61 1671 tempBuffer.offset = result.offset - headers_sz;
1672 tempBuffer.length = result.length;
62e76326 1673
43ae1d95 1674 if (tempBuffer.length)
1675 tempBuffer.data = source;
62e76326 1676
528b2c61 1677 clientStreamCallback((clientStreamNode*)http->client_stream.head->data, http, NULL,
62e76326 1678 tempBuffer);
528b2c61 1679}
1680
1681clientStreamNode *
1682clientReplyContext::next() const
1683{
1684 assert ( (clientStreamNode*)http->client_stream.head->next->data == getNextNode());
1685 return getNextNode();
1686}
1687
1688void
0655fa4d 1689clientReplyContext::waitForMoreData ()
528b2c61 1690{
bf8fe701 1691 debugs(88, 5, "clientReplyContext::waitForMoreData: Waiting for more data to parse reply headers in client side.");
528b2c61 1692 /* We don't have enough to parse the metadata yet */
1693 /* TODO: the store should give us out of band metadata and
1694 * obsolete this routine
1695 */
1696 /* wait for more to arrive */
528b2c61 1697 startSendProcess();
1698}
1699
1700void
1701clientReplyContext::startSendProcess()
1702{
bf8fe701 1703 debugs(88, 5, "clientReplyContext::startSendProcess: triggering store read to SendMoreData");
528b2c61 1704 assert(reqofs <= HTTP_REQBUF_SZ);
1705 /* TODO: copy into the supplied buffer */
1706 StoreIOBuffer tempBuffer;
1707 tempBuffer.offset = reqofs;
1708 tempBuffer.length = next()->readBuffer.length - reqofs;
1709 tempBuffer.data = next()->readBuffer.data + reqofs;
86a2f789 1710 storeClientCopy(sc, http->storeEntry(),
0655fa4d 1711 tempBuffer, SendMoreData, this);
1712}
1713
b51aec66 1714/*
1715 * Calculates the maximum size allowed for an HTTP response
1716 */
1717void
1718clientReplyContext::buildMaxBodySize(HttpReply * reply)
1719{
ef1955a5 1720 acl_size_t *l = Config.ReplyBodySize;
1721 ACLChecklist *ch;
b51aec66 1722
8777d90e 1723 if (http->logType == LOG_TCP_DENIED)
1724 return;
1725
ef1955a5 1726 ch = clientAclChecklistCreate(NULL, http);
8777d90e 1727
7dc5f514 1728 ch->reply = HTTPMSGLOCK(reply);
ef1955a5 1729
1730 for (l = Config.ReplyBodySize; l; l = l -> next) {
7684c4b1 1731 if (ch->matchAclListFast(l->aclList)) {
ed013b6c 1732 if (l->size != -1) {
4a7a3d56 1733 debugs(58, 3, "clientReplyContext: Setting maxBodySize to " << l->size);
ef1955a5 1734 http->maxReplyBodySize(l->size);
43cb430e 1735 }
b51aec66 1736
ef1955a5 1737 break;
1738 }
b51aec66 1739 }
ef1955a5 1740
1741 delete ch;
b51aec66 1742}
1743
4993f571 1744void
1745clientReplyContext::processReplyAccess ()
1746{
7dc5f514 1747 assert(reply);
1748 buildMaxBodySize(reply);
62e76326 1749
7dc5f514 1750 if (http->isReplyBodyTooLarge(reply->content_length)) {
62e76326 1751 ErrorState *err =
1752 clientBuildError(ERR_TOO_BIG, HTTP_FORBIDDEN, NULL,
94a396a3 1753 http->getConn() != NULL ? &http->getConn()->peer.sin_addr : &no_addr,
62e76326 1754 http->request);
86a2f789 1755 removeClientStoreReference(&sc, http);
7dc5f514 1756 HTTPMSGUNLOCK(reply);
3c2c1d31 1757 startError(err);
62e76326 1758 return;
1759 }
1760
7dc5f514 1761 headers_sz = reply->hdr_sz;
62e76326 1762 ACLChecklist *replyChecklist;
1763 replyChecklist = clientAclChecklistCreate(Config.accessList.reply, http);
7dc5f514 1764 replyChecklist->reply = HTTPMSGLOCK(reply);
0655fa4d 1765 replyChecklist->nonBlockingCheck(ProcessReplyAccessResult, this);
4993f571 1766}
1767
1768void
0655fa4d 1769clientReplyContext::ProcessReplyAccessResult (int rv, void *voidMe)
4993f571 1770{
1771 clientReplyContext *me = static_cast<clientReplyContext *>(voidMe);
0655fa4d 1772 me->processReplyAccessResult(rv);
4993f571 1773}
1774
1775void
0655fa4d 1776clientReplyContext::processReplyAccessResult(bool accessAllowed)
4993f571 1777{
bf8fe701 1778 debugs(88, 2, "The reply for " << RequestMethodStr[http->request->method]
1779 << " " << http->uri << " is "
1780 << ( accessAllowed ? "ALLOWED" : "DENIED")
1781 << ", because it matched '"
1782 << (AclMatchedName ? AclMatchedName : "NO ACL's") << "'" );
62e76326 1783
7dc5f514 1784 if (!accessAllowed && reply->sline.status != HTTP_FORBIDDEN
1785 && !alwaysAllowResponse(reply->sline.status)) {
62e76326 1786 /* the if above is slightly broken, but there is no way
1787 * to tell if this is a squid generated error page, or one from
1788 * upstream at this point. */
1789 ErrorState *err;
0185bd6f 1790 err_type page_id;
9ce7856a 1791 page_id = aclGetDenyInfoPage(&Config.denyInfoList, AclMatchedName, 1);
0185bd6f 1792
1793 if (page_id == ERR_NONE)
1794 page_id = ERR_ACCESS_DENIED;
1795
62e76326 1796 err =
0185bd6f 1797 clientBuildError(page_id, HTTP_FORBIDDEN, NULL,
94a396a3 1798 http->getConn() != NULL ? &http->getConn()->peer.sin_addr : &no_addr,
62e76326 1799 http->request);
0185bd6f 1800
86a2f789 1801 removeClientStoreReference(&sc, http);
0185bd6f 1802
7dc5f514 1803 HTTPMSGUNLOCK(reply);
0185bd6f 1804
3c2c1d31 1805 startError(err);
1806
c2cf9bea 1807 http->logType = LOG_TCP_DENIED_REPLY;
0185bd6f 1808
62e76326 1809 return;
1810 }
1811
0976f8db 1812 /* Ok, the reply is allowed, */
1813 http->loggingEntry(http->storeEntry());
1814
7dc5f514 1815 ssize_t body_size = reqofs - reply->hdr_sz;
0976f8db 1816
62e76326 1817 assert(body_size >= 0);
0976f8db 1818
bf8fe701 1819 debugs(88, 3, "clientReplyContext::sendMoreData: Appending " <<
1820 (int) body_size << " bytes after " << reply->hdr_sz <<
1821 " bytes of headers");
0976f8db 1822
43ae1d95 1823#if ESI
62e76326 1824
7dc5f514 1825 if (http->flags.accel && reply->sline.status != HTTP_FORBIDDEN &&
1826 !alwaysAllowResponse(reply->sline.status) &&
1827 esiEnableProcessing(reply)) {
bf8fe701 1828 debugs(88, 2, "Enabling ESI processing for " << http->uri);
43ae1d95 1829 clientStreamInsertHead(&http->client_stream, esiStreamRead,
1830 esiProcessStream, esiStreamDetach, esiStreamStatus, NULL);
1831 }
1832
5ef38a13 1833#endif
1834
62e76326 1835 if (http->request->method == METHOD_HEAD) {
1836 /* do not forward body for HEAD replies */
1837 body_size = 0;
1838 http->flags.done_copying = 1;
1839 flags.complete = 1;
1840 }
1841
1842 assert (!flags.headersSent);
1843 flags.headersSent = true;
1844
1845 StoreIOBuffer tempBuffer;
1846 char *buf = next()->readBuffer.data;
7dc5f514 1847 char *body_buf = buf + reply->hdr_sz;
62e76326 1848
49ea0125 1849 //Server side may disable ranges under some circumstances.
1850
1851 if ((!http->request->range))
1852 next()->readBuffer.offset = 0;
1853
62e76326 1854 if (next()->readBuffer.offset != 0) {
1855 if (next()->readBuffer.offset > body_size) {
1856 /* Can't use any of the body we recieved. send nothing */
1857 tempBuffer.length = 0;
1858 tempBuffer.data = NULL;
1859 } else {
1860 tempBuffer.length = body_size - next()->readBuffer.offset;
1861 tempBuffer.data = body_buf + next()->readBuffer.offset;
1862 }
1863 } else {
1864 tempBuffer.length = body_size;
1865 tempBuffer.data = body_buf;
1866 }
1867
0655fa4d 1868 /* TODO??: move the data in the buffer back by the request header size */
62e76326 1869 clientStreamCallback((clientStreamNode *)http->client_stream.head->data,
7dc5f514 1870 http, reply, tempBuffer);
62e76326 1871
1872 return;
4993f571 1873}
1874
528b2c61 1875void
1876clientReplyContext::sendMoreData (StoreIOBuffer result)
1877{
50c09fc4 1878 if (deleting)
1879 return;
1880
86a2f789 1881 StoreEntry *entry = http->storeEntry();
50c09fc4 1882
a2ac85d9 1883 ConnStateData::Pointer conn = http->getConn();
50c09fc4 1884
94a396a3 1885 int fd = conn != NULL ? conn->fd : -1;
50c09fc4 1886
528b2c61 1887 char *buf = next()->readBuffer.data;
50c09fc4 1888
c8be6d7b 1889 char *body_buf = buf;
edce4d98 1890
62e76326 1891 /* This is always valid until we get the headers as metadata from
528b2c61 1892 * storeClientCopy.
1893 * Then it becomes reqofs == next->readBuffer.offset()
af29fa86 1894 */
528b2c61 1895 assert(reqofs == 0 || flags.storelogiccomplete);
af29fa86 1896
528b2c61 1897 if (flags.headersSent && buf != result.data) {
62e76326 1898 /* we've got to copy some data */
1899 assert(result.length <= next()->readBuffer.length);
1900 xmemcpy(buf, result.data, result.length);
1901 body_buf = buf;
528b2c61 1902 } else if (!flags.headersSent &&
62e76326 1903 buf + reqofs !=result.data) {
1904 /* we've got to copy some data */
1905 assert(result.length + reqofs <= next()->readBuffer.length);
1906 xmemcpy(buf + reqofs, result.data, result.length);
1907 body_buf = buf;
edce4d98 1908 }
62e76326 1909
edce4d98 1910 /* We've got the final data to start pushing... */
528b2c61 1911 flags.storelogiccomplete = 1;
edce4d98 1912
4993f571 1913 reqofs += result.length;
1914
1915 assert(reqofs <= HTTP_REQBUF_SZ || flags.headersSent);
62e76326 1916
edce4d98 1917 assert(http->request != NULL);
62e76326 1918
edce4d98 1919 /* ESI TODO: remove this assert once everything is stable */
1920 assert(http->client_stream.head->data
62e76326 1921 && cbdataReferenceValid(http->client_stream.head->data));
528b2c61 1922
1923 makeThisHead();
62e76326 1924
bf8fe701 1925 debugs(88, 5, "clientReplyContext::sendMoreData: " << http->uri << ", " <<
47f6e231 1926 reqofs << " bytes (" << result.length <<
bf8fe701 1927 " new bytes)");
47f6e231 1928 debugs(88, 5, "clientReplyContext::sendMoreData:"
1929 " FD " << fd <<
1930 " '" << entry->url() << "'" <<
1931 " out.offset=" << http->out.offset);
528b2c61 1932
edce4d98 1933 /* update size of the request */
4993f571 1934 reqsize = reqofs;
62e76326 1935
4993f571 1936 if (errorInStream(result, reqofs)) {
62e76326 1937 sendStreamError(result);
1938 return;
edce4d98 1939 }
528b2c61 1940
1941 if (flags.headersSent) {
62e76326 1942 pushStreamData (result, buf);
1943 return;
edce4d98 1944 }
62e76326 1945
0655fa4d 1946 buildReply(buf, reqofs);
4993f571 1947 ssize_t body_size = reqofs;
62e76326 1948
7dc5f514 1949 if (reply) {
21b92762 1950
1951 /* handle headers */
1952
1953 if (Config.onoff.log_mime_hdrs) {
1954 size_t k;
1955
1956 if ((k = headersEnd(buf, reqofs))) {
1957 safe_free(http->al.headers.reply);
1958 http->al.headers.reply = (char *)xcalloc(k + 1, 1);
1959 xstrncpy(http->al.headers.reply, buf, k);
1960 }
1961 }
1962
62e76326 1963 holdingBuffer = result;
7dc5f514 1964 processReplyAccess();
62e76326 1965 return;
43ae1d95 1966
4993f571 1967 } else if (reqofs < HTTP_REQBUF_SZ && entry->store_status == STORE_PENDING) {
0655fa4d 1968 waitForMoreData();
62e76326 1969 return;
528b2c61 1970 } else if (http->request->method == METHOD_HEAD) {
62e76326 1971 /*
1972 * If we are here, then store_status == STORE_OK and it
1973 * seems we have a HEAD repsponse which is missing the
1974 * empty end-of-headers line (home.mira.net, phttpd/0.99.72
0655fa4d 1975 * does this). Because buildReply() fails we just
62e76326 1976 * call this reply a body, set the done_copying flag and
1977 * continue...
1978 */
1979 /* RBC: Note that this is seriously broken, as we *need* the
1980 * metadata to allow further client modules to work. As such
1981 * webservers are seriously broken, this is probably not
1982 * going to get fixed.. perhapos we should remove it?
1983 */
bf8fe701 1984 debugs(88, 0, "Broken head response - probably phttpd/0.99.72");
62e76326 1985 http->flags.done_copying = 1;
1986 flags.complete = 1;
1987 /*
1988 * And as this is a malformed HTTP reply we cannot keep
1989 * the connection persistent
1990 */
1991 http->request->flags.proxy_keepalive = 0;
1992
62e76326 1993 assert(body_buf && body_size);
0655fa4d 1994 StoreIOBuffer tempBuffer (body_size, 0 ,body_buf);
62e76326 1995 clientStreamCallback((clientStreamNode *)http->client_stream.head->data,
1996 http, NULL, tempBuffer);
528b2c61 1997 } else {
bf8fe701 1998 debugs(88, 0, "clientReplyContext::sendMoreData: Unable to parse reply headers within a single HTTP_REQBUF_SZ length buffer");
62e76326 1999 StoreIOBuffer tempBuffer;
2000 tempBuffer.flags.error = 1;
0655fa4d 2001 /* XXX FIXME: make an html error page here */
62e76326 2002 sendStreamError(tempBuffer);
2003 return;
edce4d98 2004 }
528b2c61 2005 fatal ("clientReplyContext::sendMoreData: Unreachable code reached \n");
edce4d98 2006}
2007
62e76326 2008
edce4d98 2009
2010/* Using this breaks the client layering just a little!
2011 */
0655fa4d 2012void
2013clientReplyContext::createStoreEntry(method_t m, request_flags flags)
edce4d98 2014{
0655fa4d 2015 assert(http != NULL);
edce4d98 2016 /*
2017 * For erroneous requests, we might not have a h->request,
2018 * so make a fake one.
2019 */
62e76326 2020
0655fa4d 2021 if (http->request == NULL)
6dd9f4bd 2022 http->request = HTTPMSGLOCK(new HttpRequest(m, PROTO_NONE, null_string));
62e76326 2023
0655fa4d 2024 StoreEntry *e = storeCreateEntry(http->uri, http->log_uri, flags, m);
62e76326 2025
0655fa4d 2026 sc = storeClientListAdd(e, this);
62e76326 2027
edce4d98 2028#if DELAY_POOLS
62e76326 2029
0655fa4d 2030 sc->setDelayId(DelayId::DelayClient(http));
62e76326 2031
edce4d98 2032#endif
62e76326 2033
0655fa4d 2034 reqofs = 0;
62e76326 2035
0655fa4d 2036 reqsize = 0;
62e76326 2037
edce4d98 2038 /* I don't think this is actually needed! -- adrian */
0655fa4d 2039 /* http->reqbuf = http->norm_reqbuf; */
2040 // assert(http->reqbuf == http->norm_reqbuf);
edce4d98 2041 /* The next line is illegal because we don't know if the client stream
2042 * buffers have been set up
2043 */
0655fa4d 2044 // storeClientCopy(http->sc, e, 0, HTTP_REQBUF_SZ, http->reqbuf,
2045 // SendMoreData, this);
edce4d98 2046 /* So, we mark the store logic as complete */
0655fa4d 2047 this->flags.storelogiccomplete = 1;
62e76326 2048
edce4d98 2049 /* and get the caller to request a read, from whereever they are */
62e76326 2050 /* NOTE: after ANY data flows down the pipe, even one step,
edce4d98 2051 * this function CAN NOT be used to manage errors
2052 */
86a2f789 2053 http->storeEntry(e);
edce4d98 2054}
2055
2056ErrorState *
2057clientBuildError(err_type page_id, http_status status, char const *url,
62e76326 2058
ddfcbc22 2059 struct IN_ADDR * src_addr, HttpRequest * request)
edce4d98 2060{
2cc81f1f 2061 ErrorState *err = errorCon(page_id, status, request);
edce4d98 2062 err->src_addr = *src_addr;
62e76326 2063
edce4d98 2064 if (url)
62e76326 2065 err->url = xstrdup(url);
2066
edce4d98 2067 return err;
2068}