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