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