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