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