http.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2021 The Squid Software Foundation and contributors
3  *
4  * Squid software is distributed under GPLv2+ license and includes
5  * contributions from numerous individuals and organizations.
6  * Please see the COPYING and CONTRIBUTORS files for details.
7  */
8 
9 /* DEBUG: section 11 Hypertext Transfer Protocol (HTTP) */
10 
11 /*
12  * Anonymizing patch by lutz@as-node.jena.thur.de
13  * have a look into http-anon.c to get more information.
14  */
15 
16 #include "squid.h"
17 #include "acl/FilledChecklist.h"
18 #include "base/AsyncJobCalls.h"
19 #include "base/TextException.h"
20 #include "base64.h"
21 #include "CachePeer.h"
22 #include "client_side.h"
23 #include "comm/Connection.h"
24 #include "comm/Read.h"
25 #include "comm/Write.h"
26 #include "CommRead.h"
27 #include "error/Detail.h"
28 #include "errorpage.h"
29 #include "fd.h"
30 #include "fde.h"
31 #include "globals.h"
32 #include "http.h"
35 #include "http/Stream.h"
36 #include "HttpControlMsg.h"
37 #include "HttpHdrCc.h"
38 #include "HttpHdrContRange.h"
39 #include "HttpHdrSc.h"
40 #include "HttpHdrScTarget.h"
41 #include "HttpHeaderTools.h"
42 #include "HttpReply.h"
43 #include "HttpRequest.h"
45 #include "log/access_log.h"
46 #include "MemBuf.h"
47 #include "MemObject.h"
48 #include "neighbors.h"
49 #include "pconn.h"
51 #include "refresh.h"
52 #include "RefreshPattern.h"
53 #include "rfc1738.h"
54 #include "SquidConfig.h"
55 #include "SquidTime.h"
56 #include "StatCounters.h"
57 #include "Store.h"
58 #include "StrList.h"
59 #include "tools.h"
60 #include "util.h"
61 
62 #if USE_AUTH
63 #include "auth/UserRequest.h"
64 #endif
65 #if USE_DELAY_POOLS
66 #include "DelayPools.h"
67 #endif
68 
70 
71 static const char *const crlf = "\r\n";
72 
74 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest * request,
75  HttpHeader * hdr_out, const int we_do_ranges, const Http::StateFlags &);
76 
78  AsyncJob("HttpStateData"),
79  Client(theFwdState),
80  lastChunk(0),
81  httpChunkDecoder(NULL),
82  payloadSeen(0),
83  payloadTruncated(0),
84  sawDateGoBack(false)
85 {
86  debugs(11,5,HERE << "HttpStateData " << this << " created");
87  ignoreCacheControl = false;
88  surrogateNoStore = false;
90 
91  if (fwd->serverConnection() != NULL)
92  _peer = cbdataReference(fwd->serverConnection()->getPeer()); /* might be NULL */
93 
97 
98  if (_peer) {
99  /*
100  * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
101  * We might end up getting the object from somewhere else if,
102  * for example, the request to this neighbor fails.
103  */
105  entry->releaseRequest(true);
106 
107 #if USE_DELAY_POOLS
109 #endif
110  }
111 
112  /*
113  * register the handler to free HTTP state data when the FD closes
114  */
118 }
119 
121 {
122  /*
123  * don't forget that ~Client() gets called automatically
124  */
125 
126  if (httpChunkDecoder)
127  delete httpChunkDecoder;
128 
130 
131  delete upgradeHeaderOut;
132 
133  debugs(11,5, HERE << "HttpStateData " << this << " destroyed; " << serverConnection);
134 }
135 
138 {
139  return serverConnection;
140 }
141 
142 void
144 {
145  debugs(11, 5, "httpStateFree: FD " << params.fd << ", httpState=" << params.data);
146  doneWithFwd = "httpStateConnClosed()"; // assume FwdState is monitoring too
147  mustStop("HttpStateData::httpStateConnClosed");
148 }
149 
150 void
152 {
153  debugs(11, 4, serverConnection << ": '" << entry->url() << "'");
154 
155  if (entry->store_status == STORE_PENDING) {
157  }
158 
159  closeServer();
160  mustStop("HttpStateData::httpTimeout");
161 }
162 
163 static StoreEntry *
165  assert(newEntry->mem_obj);
166  return newEntry->mem_obj->request ?
168  storeGetPublic(newEntry->mem_obj->storeId(), newEntry->mem_obj->method);
169 }
170 
173 static void
175 {
176  int remove = 0;
177  int forbidden = 0;
178 
179  // If the incoming response already goes into a public entry, then there is
180  // nothing to remove. This protects ready-for-collapsing entries as well.
181  if (!EBIT_TEST(e->flags, KEY_PRIVATE))
182  return;
183 
184  // If the new/incoming response cannot be stored, then it does not
185  // compete with the old stored response for the public key, and the
186  // old stored response should be left as is.
187  if (e->mem_obj->request && !e->mem_obj->request->flags.cachable)
188  return;
189 
190  switch (status) {
191 
192  case Http::scOkay:
193 
195 
197 
199 
200  case Http::scFound:
201 
202  case Http::scSeeOther:
203 
204  case Http::scGone:
205 
206  case Http::scNotFound:
207  remove = 1;
208 
209  break;
210 
211  case Http::scForbidden:
212 
214  forbidden = 1;
215 
216  break;
217 
218 #if WORK_IN_PROGRESS
219 
221  forbidden = 1;
222 
223  break;
224 
225 #endif
226 
227  default:
228  break;
229  }
230 
231  if (!remove && !forbidden)
232  return;
233 
235 
236  if (pe != NULL) {
237  assert(e != pe);
238 #if USE_HTCP
240 #endif
241  pe->release(true);
242  }
243 
248  if (e->mem_obj->request)
250  else
252 
253  if (pe != NULL) {
254  assert(e != pe);
255 #if USE_HTCP
257 #endif
258  pe->release(true);
259  }
260 }
261 
262 void
264 {
265  if (request->flags.accelerated && reply->surrogate_control) {
267 
268  if (sctusable) {
269  if (sctusable->hasNoStore() ||
271  && sctusable->noStoreRemote())) {
272  surrogateNoStore = true;
273  // Be conservative for now and make it non-shareable because
274  // there is no enough information here to make the decision.
275  entry->makePrivate(false);
276  }
277 
278  /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
279  * accelerated request or not...
280  * Still, this is an abstraction breach. - RC
281  */
282  if (sctusable->hasMaxAge()) {
283  if (sctusable->maxAge() < sctusable->maxStale())
284  reply->expires = reply->date + sctusable->maxAge();
285  else
286  reply->expires = reply->date + sctusable->maxStale();
287 
288  /* And update the timestamps */
289  entry->timestampsSet();
290  }
291 
292  /* We ignore cache-control directives as per the Surrogate specification */
293  ignoreCacheControl = true;
294 
295  delete sctusable;
296  }
297  }
298 }
299 
302 {
303  HttpReply const *rep = finalReply();
304  HttpHeader const *hdr = &rep->header;
305  const char *v;
306 #if USE_HTTP_VIOLATIONS
307 
308  const RefreshPattern *R = NULL;
309 
310  /* This strange looking define first looks up the refresh pattern
311  * and then checks if the specified flag is set. The main purpose
312  * of this is to simplify the refresh pattern lookup and USE_HTTP_VIOLATIONS
313  * condition
314  */
315 #define REFRESH_OVERRIDE(flag) \
316  ((R = (R ? R : refreshLimits(entry->mem_obj->storeId()))) , \
317  (R && R->flags.flag))
318 #else
319 #define REFRESH_OVERRIDE(flag) 0
320 #endif
321 
323  return decision.make(ReuseDecision::doNotCacheButShare, "the entry has been released");
324 
325  // RFC 7234 section 4: a cache MUST use the most recent response
326  // (as determined by the Date header field)
327  // TODO: whether such responses could be shareable?
328  if (sawDateGoBack)
329  return decision.make(ReuseDecision::reuseNot, "the response has an older date header");
330 
331  // Check for Surrogate/1.0 protocol conditions
332  // NP: reverse-proxy traffic our parent server has instructed us never to cache
333  if (surrogateNoStore)
334  return decision.make(ReuseDecision::reuseNot, "Surrogate-Control:no-store");
335 
336  // RFC 2616: HTTP/1.1 Cache-Control conditions
337  if (!ignoreCacheControl) {
338  // XXX: check to see if the request headers alone were enough to prevent caching earlier
339  // (ie no-store request header) no need to check those all again here if so.
340  // for now we are not reliably doing that so we waste CPU re-checking request CC
341 
342  // RFC 2616 section 14.9.2 - MUST NOT cache any response with request CC:no-store
344  !REFRESH_OVERRIDE(ignore_no_store))
345  return decision.make(ReuseDecision::reuseNot,
346  "client request Cache-Control:no-store");
347 
348  // NP: request CC:no-cache only means cache READ is forbidden. STORE is permitted.
350  /* TODO: we are allowed to cache when no-cache= has parameters.
351  * Provided we strip away any of the listed headers unless they are revalidated
352  * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
353  * That is a bit tricky for squid right now so we avoid caching entirely.
354  */
355  return decision.make(ReuseDecision::reuseNot,
356  "server reply Cache-Control:no-cache has parameters");
357  }
358 
359  // NP: request CC:private is undefined. We ignore.
360  // NP: other request CC flags are limiters on HIT/MISS. We don't care about here.
361 
362  // RFC 2616 section 14.9.2 - MUST NOT cache any response with CC:no-store
363  if (rep->cache_control && rep->cache_control->hasNoStore() &&
364  !REFRESH_OVERRIDE(ignore_no_store))
365  return decision.make(ReuseDecision::reuseNot,
366  "server reply Cache-Control:no-store");
367 
368  // RFC 2616 section 14.9.1 - MUST NOT cache any response with CC:private in a shared cache like Squid.
369  // CC:private overrides CC:public when both are present in a response.
370  // TODO: add a shared/private cache configuration possibility.
371  if (rep->cache_control &&
372  rep->cache_control->hasPrivate() &&
373  !REFRESH_OVERRIDE(ignore_private)) {
374  /* TODO: we are allowed to cache when private= has parameters.
375  * Provided we strip away any of the listed headers unless they are revalidated
376  * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
377  * That is a bit tricky for squid right now so we avoid caching entirely.
378  */
379  return decision.make(ReuseDecision::reuseNot,
380  "server reply Cache-Control:private");
381  }
382  }
383 
384  // RFC 2068, sec 14.9.4 - MUST NOT cache any response with Authentication UNLESS certain CC controls are present
385  // allow HTTP violations to IGNORE those controls (ie re-block caching Auth)
386  if (request && (request->flags.auth || request->flags.authSent)) {
387  if (!rep->cache_control)
388  return decision.make(ReuseDecision::reuseNot,
389  "authenticated and server reply missing Cache-Control");
390 
391  if (ignoreCacheControl)
392  return decision.make(ReuseDecision::reuseNot,
393  "authenticated and ignoring Cache-Control");
394 
395  bool mayStore = false;
396  // HTTPbis pt6 section 3.2: a response CC:public is present
397  if (rep->cache_control->hasPublic()) {
398  debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:public");
399  mayStore = true;
400 
401  // HTTPbis pt6 section 3.2: a response CC:must-revalidate is present
402  } else if (rep->cache_control->hasMustRevalidate()) {
403  debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:must-revalidate");
404  mayStore = true;
405 
406 #if USE_HTTP_VIOLATIONS
407  // NP: given the must-revalidate exception we should also be able to exempt no-cache.
408  // HTTPbis WG verdict on this is that it is omitted from the spec due to being 'unexpected' by
409  // some. The caching+revalidate is not exactly unsafe though with Squids interpretation of no-cache
410  // (without parameters) as equivalent to must-revalidate in the reply.
411  } else if (rep->cache_control->hasNoCacheWithoutParameters()) {
412  debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:no-cache (equivalent to must-revalidate)");
413  mayStore = true;
414 #endif
415 
416  // HTTPbis pt6 section 3.2: a response CC:s-maxage is present
417  } else if (rep->cache_control->hasSMaxAge()) {
418  debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:s-maxage");
419  mayStore = true;
420  }
421 
422  if (!mayStore)
423  return decision.make(ReuseDecision::reuseNot, "authenticated transaction");
424 
425  // NP: response CC:no-cache is equivalent to CC:must-revalidate,max-age=0. We MAY cache, and do so.
426  // NP: other request CC flags are limiters on HIT/MISS/REFRESH. We don't care about here.
427  }
428 
429  /* HACK: The "multipart/x-mixed-replace" content type is used for
430  * continuous push replies. These are generally dynamic and
431  * probably should not be cachable
432  */
433  if ((v = hdr->getStr(Http::HdrType::CONTENT_TYPE)))
434  if (!strncasecmp(v, "multipart/x-mixed-replace", 25))
435  return decision.make(ReuseDecision::reuseNot, "Content-Type:multipart/x-mixed-replace");
436 
437  // TODO: if possible, provide more specific message for each status code
438  static const char *shareableError = "shareable error status code";
439  static const char *nonShareableError = "non-shareable error status code";
441  const char *statusReason = nonShareableError;
442 
443  switch (rep->sline.status()) {
444 
445  /* There are several situations when a non-cacheable response may be
446  * still shareable (e.g., among collapsed clients). We assume that these
447  * are 3xx and 5xx responses, indicating server problems and some of
448  * 4xx responses, common for all clients with a given cache key (e.g.,
449  * 404 Not Found or 414 URI Too Long). On the other hand, we should not
450  * share non-cacheable client-specific errors, such as 400 Bad Request
451  * or 406 Not Acceptable.
452  */
453 
454  /* Responses that are cacheable */
455 
456  case Http::scOkay:
457 
459 
461 
464 
465  case Http::scGone:
466  /*
467  * Don't cache objects that need to be refreshed on next request,
468  * unless we know how to refresh it.
469  */
470 
471  if (refreshIsCachable(entry) || REFRESH_OVERRIDE(store_stale))
472  decision.make(ReuseDecision::cachePositively, "refresh check returned cacheable");
473  else
474  decision.make(ReuseDecision::doNotCacheButShare, "refresh check returned non-cacheable");
475  break;
476 
477  /* Responses that only are cacheable if the server says so */
478 
479  case Http::scFound:
481  if (rep->date <= 0)
482  decision.make(ReuseDecision::doNotCacheButShare, "Date is missing/invalid");
483  else if (rep->expires > rep->date)
484  decision.make(ReuseDecision::cachePositively, "Expires > Date");
485  else
486  decision.make(ReuseDecision::doNotCacheButShare, "Expires <= Date");
487  break;
488 
489  /* These responses can be negatively cached. Most can also be shared. */
490  case Http::scNoContent:
491  case Http::scUseProxy:
492  case Http::scForbidden:
493  case Http::scNotFound:
495  case Http::scUriTooLong:
498  case Http::scBadGateway:
502  statusAnswer = ReuseDecision::doNotCacheButShare;
503  statusReason = shareableError;
504  /* [[fallthrough]] to the actual decision making below */
505 
506  case Http::scBadRequest: // no sharing; perhaps the server did not like something specific to this request
507 #if USE_HTTP_VIOLATIONS
508  if (Config.negativeTtl > 0)
509  decision.make(ReuseDecision::cacheNegatively, "Config.negativeTtl > 0");
510  else
511 #endif
512  decision.make(statusAnswer, statusReason);
513  break;
514 
515  /* these responses can never be cached, some
516  of them can be shared though */
517  case Http::scSeeOther:
518  case Http::scNotModified:
523  // TODO: use more specific reason for non-error status codes
524  decision.make(ReuseDecision::doNotCacheButShare, shareableError);
525  break;
526 
527  case Http::scPartialContent: /* Not yet supported. TODO: make shareable for suitable ranges */
529  case Http::scRequestTimeout: // TODO: is this shareable?
530  case Http::scConflict: // TODO: is this shareable?
536  case Http::scLocked: // TODO: is this shareable?
540  case Http::scInvalidHeader: /* Squid header parsing error */
542  decision.make(ReuseDecision::reuseNot, nonShareableError);
543  break;
544 
545  default:
546  /* RFC 2616 section 6.1.1: an unrecognized response MUST NOT be cached. */
547  decision.make(ReuseDecision::reuseNot, "unknown status code");
548  break;
549  }
550 
551  return decision.answer;
552 }
553 
555 static void
557 {
558  static const SBuf asterisk("*");
559  const char *pos = nullptr;
560  const char *item = nullptr;
561  int ilen = 0;
562 
563  while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
564  SBuf name(item, ilen);
565  if (name == asterisk) {
566  vstr = asterisk;
567  break;
568  }
569  name.toLower();
570  if (!vstr.isEmpty())
571  vstr.append(", ", 2);
572  vstr.append(name);
573  String hdr(request.header.getByName(name));
574  const char *value = hdr.termedBuf();
575  if (value) {
576  value = rfc1738_escape_part(value);
577  vstr.append("=\"", 2);
578  vstr.append(value);
579  vstr.append("\"", 1);
580  }
581 
582  hdr.clean();
583  }
584 }
585 
586 /*
587  * For Vary, store the relevant request headers as
588  * virtual headers in the reply
589  * Returns an empty SBuf if the variance cannot be stored
590  */
591 SBuf
593 {
594  SBuf vstr;
595  String vary;
596 
597  vary = reply->header.getList(Http::HdrType::VARY);
598  assembleVaryKey(vary, vstr, *request);
599 
600 #if X_ACCELERATOR_VARY
601  vary.clean();
603  assembleVaryKey(vary, vstr, *request);
604 #endif
605 
606  debugs(11, 3, vstr);
607  return vstr;
608 }
609 
610 void
612 {
613  if (flags.keepalive)
614  if (flags.peering && !flags.tunneling)
616 
617  if (reply->keep_alive) {
618  if (flags.peering && !flags.tunneling)
620 
622  && reply->bodySize(request->method) == -1 && !flags.chunked) {
623  debugs(11, DBG_IMPORTANT, "keepaliveAccounting: Impossible keep-alive header from '" << entry->url() << "'" );
624  // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" );
625  flags.keepalive_broken = true;
626  }
627  }
628 }
629 
630 void
632 {
633  if (reply->date > -1 && flags.toOrigin) {
634  int skew = abs((int)(reply->date - squid_curtime));
635 
636  if (skew > 86400)
637  debugs(11, 3, "" << request->url.host() << "'s clock is skewed by " << skew << " seconds!");
638  }
639 }
640 
647 void
649 {
652  debugs(11, 3, "processReplyHeader: key '" << entry->getMD5Text() << "'");
653 
655 
656  if (!inBuf.length())
657  return;
658 
659  /* Attempt to parse the first line; this will define where the protocol, status, reason-phrase and header begin */
660  {
661  if (hp == NULL)
663 
664  bool parsedOk = hp->parse(inBuf);
665  // remember the actual received status-code before returning on errors,
666  // overwriting any previously stored value from earlier forwarding attempts
667  request->hier.peer_reply_status = hp->messageStatus(); // may still be scNone
668 
669  // sync the buffers after parsing.
670  inBuf = hp->remaining();
671 
672  if (hp->needsMoreData()) {
673  if (eof) { // no more data coming
674  assert(!parsedOk);
675  // fall through to handle this premature EOF as an error
676  } else {
677  debugs(33, 5, "Incomplete response, waiting for end of response headers");
678  return;
679  }
680  }
681 
682  if (!parsedOk) {
683  // unrecoverable parsing error
684  // TODO: Use Raw! XXX: inBuf no longer has the [beginning of the] malformed header.
685  debugs(11, 3, "Non-HTTP-compliant header:\n---------\n" << inBuf << "\n----------");
686  flags.headers_parsed = true;
687  HttpReply *newrep = new HttpReply;
688  // hp->needsMoreData() means hp->parseStatusCode is unusable, but, here,
689  // it also means that the reply header got truncated by a premature EOF
690  assert(!hp->needsMoreData() || eof);
691  const auto scode = hp->needsMoreData() ? Http::scInvalidHeader : hp->parseStatusCode;
692  newrep->sline.set(Http::ProtocolVersion(), scode);
693  setVirginReply(newrep);
694  return;
695  }
696  }
697 
698  /* We know the whole response is in parser now */
699  debugs(11, 2, "HTTP Server " << serverConnection);
700  debugs(11, 2, "HTTP Server RESPONSE:\n---------\n" <<
701  hp->messageProtocol() << " " << hp->messageStatus() << " " << hp->reasonPhrase() << "\n" <<
702  hp->mimeHeader() <<
703  "----------");
704 
705  // reset payload tracking to begin after message headers
707 
708  HttpReply *newrep = new HttpReply;
709  // XXX: RFC 7230 indicates we MAY ignore the reason phrase,
710  // and use an empty string on unknown status.
711  // We do that now to avoid performance regression from using SBuf::c_str()
712  newrep->sline.set(hp->messageProtocol(), hp->messageStatus() /* , hp->reasonPhrase() */);
713 
714  // parse headers
715  if (!newrep->parseHeader(*hp)) {
716  newrep->sline.set(hp->messageProtocol(), Http::scInvalidHeader);
717  debugs(11, 2, "error parsing response headers mime block");
718  }
719 
720  // done with Parser, now process using the HttpReply
721  hp = NULL;
722 
724 
725  newrep->removeStaleWarnings();
726 
727  if (newrep->sline.version.protocol == AnyP::PROTO_HTTP && Http::Is1xx(newrep->sline.status())) {
728  handle1xx(newrep);
729  return;
730  }
731 
732  flags.chunked = false;
733  if (newrep->sline.version.protocol == AnyP::PROTO_HTTP && newrep->header.chunked()) {
734  flags.chunked = true;
736  }
737 
740 
741  HttpReply *vrep = setVirginReply(newrep);
742  flags.headers_parsed = true;
743 
744  keepaliveAccounting(vrep);
745 
746  checkDateSkew(vrep);
747 
749 }
750 
752 void
754 {
755  if (fwd->al)
756  fwd->al->reply = reply;
757 
758  HttpReply::Pointer msg(reply); // will destroy reply if unused
759 
760  // one 1xx at a time: we must not be called while waiting for previous 1xx
762  flags.handling1xx = true;
763 
764  const auto statusCode = reply->sline.status();
765 
766  // drop1xx() needs to handle HTTP 101 (Switching Protocols) responses
767  // specially because they indicate that the server has stopped speaking HTTP
770 
771  if (statusCode == Http::scContinue && request->forcedBodyContinuation)
772  return drop1xx("we have sent it already");
773 
774  if (!request->canHandle1xx())
775  return drop1xx("the client does not support it");
776 
777 #if USE_HTTP_VIOLATIONS
778  // check whether the 1xx response forwarding is allowed by squid.conf
779  if (Config.accessList.reply) {
781  ch.al = fwd->al;
782  ch.reply = reply;
783  ch.syncAle(originalRequest().getRaw(), nullptr);
784  HTTPMSGLOCK(ch.reply);
785  if (!ch.fastCheck().allowed()) // TODO: support slow lookups?
786  return drop1xx("http_reply_access blocked it");
787  }
788 #endif // USE_HTTP_VIOLATIONS
789 
791  if (const auto reason = blockSwitchingProtocols(*reply))
792  return drop1xx(reason);
793  }
794 
795  debugs(11, 2, HERE << "forwarding 1xx to client");
796 
797  // the Sink will use this to call us back after writing 1xx to the client
798  typedef NullaryMemFunT<HttpStateData> CbDialer;
799  const AsyncCall::Pointer cb = JobCallback(11, 3, CbDialer, this,
803  // If the call is not fired, then the Sink is gone, and HttpStateData
804  // will terminate due to an aborted store entry or another similar error.
805  // If we get stuck, it is not handle1xx fault if we could get stuck
806  // for similar reasons without a 1xx response.
807 }
808 
811 void
812 HttpStateData::drop1xx(const char *reason)
813 {
815  debugs(11, 2, "bad 101 because " << reason);
816  const auto err = new ErrorState(ERR_INVALID_RESP, Http::scBadGateway, request.getRaw(), fwd->al);
817  fwd->fail(err);
818  closeServer();
819  mustStop("prohibited HTTP/101 response");
820  return;
821  }
822 
823  debugs(11, 2, "ignoring 1xx because " << reason);
824  proceedAfter1xx();
825 }
826 
829 const char *
831 {
832  if (!upgradeHeaderOut)
833  return "Squid offered no Upgrade at all, but server switched to a tunnel";
834 
835  // See RFC 7230 section 6.7 for the corresponding MUSTs
836 
837  if (!reply.header.has(Http::HdrType::UPGRADE))
838  return "server did not send an Upgrade header field";
839 
840  if (!reply.header.hasListMember(Http::HdrType::CONNECTION, "upgrade", ','))
841  return "server did not send 'Connection: upgrade'";
842 
843  const auto acceptedProtos = reply.header.getList(Http::HdrType::UPGRADE);
844  const char *pos = nullptr;
845  const char *accepted = nullptr;
846  int acceptedLen = 0;
847  while (strListGetItem(&acceptedProtos, ',', &accepted, &acceptedLen, &pos)) {
848  debugs(11, 5, "server accepted at least" << Raw(nullptr, accepted, acceptedLen));
849  return nullptr; // OK: let the client validate server's selection
850  }
851 
852  return "server sent an essentially empty Upgrade header field";
853 }
854 
856 void
858 {
860 
862  // pass server connection ownership to request->clientConnectionManager
865  AsyncCall::Pointer call = asyncCall(11, 3, "ConnStateData::noteTakeServerConnectionControl",
868  ScheduleCallHere(call);
871  closeHandler = nullptr;
872  serverConnection = nullptr;
873  doneWithFwd = "switched protocols";
875  return;
876  }
877 
878  debugs(11, 2, "continuing with " << payloadSeen << " bytes in buffer after 1xx");
880 }
881 
885 bool
887 {
888  if (!_peer)
889  return true;
890 
891  // we are talking "through" rather than "to" our _peer
892  if (flags.tunneling)
893  return true;
894 
895  /*If this peer does not support connection pinning (authenticated
896  connections) return false
897  */
898  if (!_peer->connection_auth)
899  return false;
900 
901  const auto &rep = entry->mem().freshestReply();
902 
903  /*The peer supports connection pinning and the http reply status
904  is not unauthorized, so the related connection can be pinned
905  */
906  if (rep.sline.status() != Http::scUnauthorized)
907  return true;
908 
909  /*The server respond with Http::scUnauthorized and the peer configured
910  with "connection-auth=on" we know that the peer supports pinned
911  connections
912  */
913  if (_peer->connection_auth == 1)
914  return true;
915 
916  /*At this point peer has configured with "connection-auth=auto"
917  parameter so we need some extra checks to decide if we are going
918  to allow pinned connections or not
919  */
920 
921  /*if the peer configured with originserver just allow connection
922  pinning (squid 2.6 behaviour)
923  */
925  return true;
926 
927  /*if the connections it is already pinned it is OK*/
928  if (request->flags.pinned)
929  return true;
930 
931  /*Allow pinned connections only if the Proxy-support header exists in
932  reply and has in its list the "Session-Based-Authentication"
933  which means that the peer supports connection pinning.
934  */
935  if (rep.header.hasListMember(Http::HdrType::PROXY_SUPPORT, "Session-Based-Authentication", ','))
936  return true;
937 
938  return false;
939 }
940 
941 // Called when we parsed (and possibly adapted) the headers but
942 // had not starting storing (a.k.a., sending) the body yet.
943 void
945 {
947 
948  HttpReply *rep = finalReply();
949  const Http::StatusCode statusCode = rep->sline.status();
950 
951  entry->timestampsSet();
952 
953  /* Check if object is cacheable or not based on reply code */
954  debugs(11, 3, "HTTP CODE: " << statusCode);
955 
956  if (StoreEntry *oldEntry = findPreviouslyCachedEntry(entry)) {
957  oldEntry->lock("HttpStateData::haveParsedReplyHeaders");
958  sawDateGoBack = rep->olderThan(oldEntry->hasFreshestReply());
959  oldEntry->unlock("HttpStateData::haveParsedReplyHeaders");
960  }
961 
964 
965  bool varyFailure = false;
966  if (rep->header.has(Http::HdrType::VARY)
967 #if X_ACCELERATOR_VARY
969 #endif
970  ) {
971  const SBuf vary(httpMakeVaryMark(request.getRaw(), rep));
972 
973  if (vary.isEmpty()) {
974  // TODO: check whether such responses are shareable.
975  // Do not share for now.
976  entry->makePrivate(false);
977  if (fwd->reforwardableStatus(rep->sline.status()))
979  varyFailure = true;
980  } else {
981  entry->mem_obj->vary_headers = vary;
982 
983  // RFC 7231 section 7.1.4
984  // Vary:* can be cached, but has mandatory revalidation
985  static const SBuf asterisk("*");
986  if (vary == asterisk)
988  }
989  }
990 
991  if (!varyFailure) {
992  /*
993  * If its not a reply that we will re-forward, then
994  * allow the client to get it.
995  */
996  if (fwd->reforwardableStatus(rep->sline.status()))
998 
999  ReuseDecision decision(entry, statusCode);
1000 
1001  switch (reusableReply(decision)) {
1002 
1004  entry->makePrivate(false);
1005  break;
1006 
1008  if (!entry->makePublic()) {
1009  decision.make(ReuseDecision::doNotCacheButShare, "public key creation error");
1010  entry->makePrivate(true);
1011  }
1012  break;
1013 
1015  if (!entry->cacheNegatively()) {
1016  decision.make(ReuseDecision::doNotCacheButShare, "public key creation error");
1017  entry->makePrivate(true);
1018  }
1019  break;
1020 
1022  entry->makePrivate(true);
1023  break;
1024 
1025  default:
1026  assert(0);
1027  break;
1028  }
1029  debugs(11, 3, "decided: " << decision);
1030  }
1031 
1032  if (!ignoreCacheControl) {
1033  if (rep->cache_control) {
1034  // We are required to revalidate on many conditions.
1035  // For security reasons we do so even if storage was caused by refresh_pattern ignore-* option
1036 
1037  // CC:must-revalidate or CC:proxy-revalidate
1038  const bool ccMustRevalidate = (rep->cache_control->hasProxyRevalidate() || rep->cache_control->hasMustRevalidate());
1039 
1040  // CC:no-cache (only if there are no parameters)
1041  const bool ccNoCacheNoParams = rep->cache_control->hasNoCacheWithoutParameters();
1042 
1043  // CC:s-maxage=N
1044  const bool ccSMaxAge = rep->cache_control->hasSMaxAge();
1045 
1046  // CC:private (yes, these can sometimes be stored)
1047  const bool ccPrivate = rep->cache_control->hasPrivate();
1048 
1049  if (ccNoCacheNoParams || ccPrivate)
1051  else if (ccMustRevalidate || ccSMaxAge)
1053  }
1054 #if USE_HTTP_VIOLATIONS // response header Pragma::no-cache is undefined in HTTP
1055  else {
1056  // Expensive calculation. So only do it IF the CC: header is not present.
1057 
1058  /* HACK: Pragma: no-cache in _replies_ is not documented in HTTP,
1059  * but servers like "Active Imaging Webcast/2.0" sure do use it */
1060  if (rep->header.has(Http::HdrType::PRAGMA) &&
1061  rep->header.hasListMember(Http::HdrType::PRAGMA,"no-cache",','))
1063  }
1064 #endif
1065  }
1066 
1067 #if HEADERS_LOG
1068  headersLog(1, 0, request->method, rep);
1069 
1070 #endif
1071 }
1072 
1075 {
1076  const HttpReply *rep = virginReply();
1080  static SBuf close("close", 5);
1081  if (httpHeaderHasConnDir(&rep->header, close))
1083 
1088  if (!flags.keepalive)
1090 
1094  if (flags.forceClose)
1096 
1101  if (!flags.request_sent) {
1102  debugs(11, 2, "Request not yet fully sent " << request->method << ' ' << entry->url());
1104  }
1105 
1118  if (!rep->keep_alive)
1120 
1121  return COMPLETE_PERSISTENT_MSG;
1122 }
1123 
1126 {
1127  debugs(11, 3, HERE << serverConnection << " eof=" << eof);
1128  if (eof) // already reached EOF
1130 
1131  /* If server fd is closing (but we have not been notified yet), stop Comm
1132  I/O to avoid assertions. TODO: Change Comm API to handle callers that
1133  want more I/O after async closing (usually initiated by others). */
1134  // XXX: add canReceive or s/canSend/canTalkToServer/
1137 
1143  if (lastChunk && flags.chunked)
1144  return statusIfComplete();
1145 
1146  const HttpReply *vrep = virginReply();
1147  debugs(11, 5, "persistentConnStatus: content_length=" << vrep->content_length);
1148 
1149  const int64_t clen = vrep->bodySize(request->method);
1150 
1151  debugs(11, 5, "persistentConnStatus: clen=" << clen);
1152 
1153  /* If the body size is unknown we must wait for EOF */
1154  if (clen < 0)
1155  return INCOMPLETE_MSG;
1156 
1159  if (clen > 0) {
1160  debugs(11,5, "payloadSeen=" << payloadSeen << " content_length=" << vrep->content_length);
1161 
1162  if (payloadSeen < vrep->content_length)
1163  return INCOMPLETE_MSG;
1164 
1165  if (payloadTruncated > 0) // already read more than needed
1166  return COMPLETE_NONPERSISTENT_MSG; // disable pconns
1167  }
1168 
1171  return statusIfComplete();
1172 }
1173 
1174 static void
1175 readDelayed(void *context, CommRead const &)
1176 {
1177  HttpStateData *state = static_cast<HttpStateData*>(context);
1178  state->flags.do_next_read = true;
1179  state->maybeReadVirginBody();
1180 }
1181 
1182 void
1184 {
1185  Must(!flags.do_next_read); // XXX: should have been set false by mayReadVirginBody()
1186  flags.do_next_read = false;
1187 
1188  debugs(11, 5, io.conn);
1189 
1190  // Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us
1191  if (io.flag == Comm::ERR_CLOSING) {
1192  debugs(11, 3, "http socket closing");
1193  return;
1194  }
1195 
1197  abortTransaction("store entry aborted while reading reply");
1198  return;
1199  }
1200 
1202  Must(io.conn->fd == serverConnection->fd);
1203 
1204  /*
1205  * Don't reset the timeout value here. The value should be
1206  * counting Config.Timeout.request and applies to the request
1207  * as a whole, not individual read() calls.
1208  * Plus, it breaks our lame *HalfClosed() detection
1209  */
1210 
1212  CommIoCbParams rd(this); // will be expanded with ReadNow results
1213  rd.conn = io.conn;
1215 
1216  if (rd.size <= 0) {
1217  assert(entry->mem_obj);
1218  AsyncCall::Pointer nilCall;
1219  entry->mem_obj->delayRead(DeferredRead(readDelayed, this, CommRead(io.conn, NULL, 0, nilCall)));
1220  return;
1221  }
1222 
1223  switch (Comm::ReadNow(rd, inBuf)) {
1224  case Comm::INPROGRESS:
1225  if (inBuf.isEmpty())
1226  debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
1227  flags.do_next_read = true;
1229  return;
1230 
1231  case Comm::OK:
1232  {
1233  payloadSeen += rd.size;
1234 #if USE_DELAY_POOLS
1235  DelayId delayId = entry->mem_obj->mostBytesAllowed();
1236  delayId.bytesIn(rd.size);
1237 #endif
1238 
1239  statCounter.server.all.kbytes_in += rd.size;
1240  statCounter.server.http.kbytes_in += rd.size;
1241  ++ IOStats.Http.reads;
1242 
1243  int bin = 0;
1244  for (int clen = rd.size - 1; clen; ++bin)
1245  clen >>= 1;
1246 
1247  ++ IOStats.Http.read_hist[bin];
1248 
1250  }
1251 
1252  /* Continue to process previously read data */
1253  break;
1254 
1255  case Comm::ENDFILE: // close detected by 0-byte read
1256  eof = 1;
1257  flags.do_next_read = false;
1258 
1259  /* Continue to process previously read data */
1260  break;
1261 
1262  // case Comm::COMM_ERROR:
1263  default: // no other flags should ever occur
1264  debugs(11, 2, io.conn << ": read failure: " << xstrerr(rd.xerrno));
1265  const auto err = new ErrorState(ERR_READ_ERROR, Http::scBadGateway, fwd->request, fwd->al);
1266  err->xerrno = rd.xerrno;
1267  fwd->fail(err);
1268  flags.do_next_read = false;
1269  closeServer();
1270  mustStop("HttpStateData::readReply");
1271  return;
1272  }
1273 
1274  /* Process next response from buffer */
1275  processReply();
1276 }
1277 
1280 void
1282 {
1283 
1284  if (flags.handling1xx) { // we came back after handling a 1xx response
1285  debugs(11, 5, HERE << "done with 1xx handling");
1286  flags.handling1xx = false;
1288  }
1289 
1291  abortTransaction("store entry aborted while we were waiting for processReply()");
1292  return;
1293  }
1294 
1295  if (!flags.headers_parsed) { // have not parsed headers yet?
1297 
1298  if (!continueAfterParsingHeader()) // parsing error or need more data
1299  return; // TODO: send errors to ICAP
1300 
1301  adaptOrFinalizeReply(); // may write to, abort, or "close" the entry
1302  }
1303 
1304  // kick more reads if needed and/or process the response body, if any
1305  processReplyBody(); // may call serverComplete()
1306 }
1307 
1311 bool
1313 {
1314  if (flags.handling1xx) {
1315  debugs(11, 5, HERE << "wait for 1xx handling");
1317  return false;
1318  }
1319 
1320  if (!flags.headers_parsed && !eof) {
1321  debugs(11, 9, "needs more at " << inBuf.length());
1322  flags.do_next_read = true;
1326  maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1327  return false;
1328  }
1329 
1333 
1334  if (flags.headers_parsed) { // parsed headers, possibly with errors
1335  // check for header parsing errors
1336  if (HttpReply *vrep = virginReply()) {
1337  const Http::StatusCode s = vrep->sline.status();
1338  const AnyP::ProtocolVersion &v = vrep->sline.version;
1339  if (s == Http::scInvalidHeader && v != Http::ProtocolVersion(0,9)) {
1340  debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry->url() << " AKA " << request->url);
1342  } else if (s == Http::scHeaderTooLarge) {
1343  fwd->dontRetry(true);
1344  error = ERR_TOO_BIG;
1345  } else if (vrep->header.conflictingContentLength()) {
1346  fwd->dontRetry(true);
1348  } else if (vrep->header.unsupportedTe()) {
1349  fwd->dontRetry(true);
1351  } else {
1352  return true; // done parsing, got reply, and no error
1353  }
1354  } else {
1355  // parsed headers but got no reply
1356  debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: No reply at all for " << entry->url() << " AKA " << request->url);
1358  }
1359  } else {
1360  assert(eof);
1361  if (inBuf.length()) {
1363  debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry->url() << " AKA " << request->url);
1364  } else {
1366  debugs(11, (request->flags.accelerated?DBG_IMPORTANT:2), "WARNING: HTTP: Invalid Response: No object data received for " << entry->url() << " AKA " << request->url);
1367  }
1368  }
1369 
1370  assert(error != ERR_NONE);
1371  entry->reset();
1373  flags.do_next_read = false;
1374  closeServer();
1375  mustStop("HttpStateData::continueAfterParsingHeader");
1376  return false; // quit on error
1377 }
1378 
1381 void
1383 {
1385 
1386  HttpReply *vrep = virginReply();
1387  int64_t clen = -1;
1388  if (!vrep->expectingBody(request->method, clen) || clen < 0)
1389  return; // no body or a body of unknown size, including chunked
1390 
1391  if (payloadSeen - payloadTruncated <= clen)
1392  return; // we did not read too much or already took care of the extras
1393 
1394  if (const int64_t extras = payloadSeen - payloadTruncated - clen) {
1395  // server sent more that the advertised content length
1396  debugs(11, 5, "payloadSeen=" << payloadSeen <<
1397  " clen=" << clen << '/' << vrep->content_length <<
1398  " truncated=" << payloadTruncated << '+' << extras);
1399 
1400  inBuf.chop(0, inBuf.length() - extras);
1401  payloadTruncated += extras;
1402  }
1403 }
1404 
1409 void
1411 {
1412  truncateVirginBody(); // if needed
1413  const char *data = inBuf.rawContent();
1414  int len = inBuf.length();
1415  addVirginReplyBody(data, len);
1416  inBuf.consume(len);
1417 
1418  // after addVirginReplyBody() wrote (when not adapting) everything we have
1419  // received to Store, check whether we have received/parsed the entire reply
1420  int64_t clen = -1;
1421  const char *parsedWhole = nullptr;
1422  if (!virginReply()->expectingBody(request->method, clen))
1423  parsedWhole = "http parsed header-only reply";
1424  else if (clen >= 0 && clen == payloadSeen - payloadTruncated)
1425  parsedWhole = "http parsed Content-Length body bytes";
1426  else if (clen < 0 && eof)
1427  parsedWhole = "http parsed body ending with expected/required EOF";
1428  if (parsedWhole)
1429  markParsedVirginReplyAsWhole(parsedWhole);
1430 }
1431 
1432 bool
1434 {
1435  assert(flags.chunked);
1437  try {
1438  MemBuf decodedData;
1439  decodedData.init();
1440  httpChunkDecoder->setPayloadBuffer(&decodedData);
1441  const bool doneParsing = httpChunkDecoder->parse(inBuf);
1442  inBuf = httpChunkDecoder->remaining(); // sync buffers after parse
1443  addVirginReplyBody(decodedData.content(), decodedData.contentSize());
1444  if (doneParsing) {
1445  lastChunk = 1;
1446  flags.do_next_read = false;
1447  markParsedVirginReplyAsWhole("http parsed last-chunk");
1448  }
1449  return true;
1450  }
1451  catch (...) {
1452  debugs (11, 2, "de-chunking failure: " << CurrentException);
1453  }
1454  return false;
1455 }
1456 
1464 void
1466 {
1467  if (!flags.headers_parsed) {
1468  flags.do_next_read = true;
1470  return;
1471  }
1472 
1473 #if USE_ADAPTATION
1474  debugs(11,5, HERE << "adaptationAccessCheckPending=" << adaptationAccessCheckPending);
1476  return;
1477 
1478 #endif
1479 
1480  /*
1481  * At this point the reply headers have been parsed and consumed.
1482  * That means header content has been removed from readBuf and
1483  * it contains only body data.
1484  */
1485  if (entry->isAccepting()) {
1486  if (flags.chunked) {
1487  if (!decodeAndWriteReplyBody()) {
1488  flags.do_next_read = false;
1489  serverComplete();
1490  return;
1491  }
1492  } else
1493  writeReplyBody();
1494  }
1495 
1496  // storing/sending methods like earlier adaptOrFinalizeReply() or
1497  // above writeReplyBody() may release/abort the store entry.
1499  // TODO: In some cases (e.g., 304), we should keep persistent conn open.
1500  // Detect end-of-reply (and, hence, pool our idle pconn) earlier (ASAP).
1501  abortTransaction("store entry aborted while storing reply");
1502  return;
1503  } else
1504  switch (persistentConnStatus()) {
1505  case INCOMPLETE_MSG: {
1506  debugs(11, 5, "processReplyBody: INCOMPLETE_MSG from " << serverConnection);
1507  /* Wait for more data or EOF condition */
1509  if (flags.keepalive_broken) {
1511  } else {
1513  }
1514 
1515  flags.do_next_read = true;
1516  }
1517  break;
1518 
1519  case COMPLETE_PERSISTENT_MSG: {
1520  debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG from " << serverConnection);
1521 
1522  // TODO: Remove serverConnectionSaved but preserve exception safety.
1523 
1525  flags.do_next_read = false;
1526 
1528  closeHandler = NULL;
1529 
1530  Ip::Address client_addr; // XXX: Remove as unused. Why was it added?
1532  client_addr = request->client_addr;
1533 
1534  auto serverConnectionSaved = serverConnection;
1536  serverConnection = nullptr;
1537 
1538  bool ispinned = false; // TODO: Rename to isOrShouldBePinned
1539  if (request->flags.pinned) {
1540  ispinned = true;
1541  } else if (request->flags.connectionAuth && request->flags.authSent) {
1542  ispinned = true;
1543  }
1544 
1545  if (ispinned) {
1548  ConnStateData,
1549  notePinnedConnectionBecameIdle,
1550  ConnStateData::PinnedIdleContext(serverConnectionSaved, request));
1551  } else {
1552  // must not pool/share ispinned connections, even orphaned ones
1553  serverConnectionSaved->close();
1554  }
1555  } else {
1556  fwdPconnPool->push(serverConnectionSaved, request->url.host());
1557  }
1558 
1559  serverComplete();
1560  return;
1561  }
1562 
1564  debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG from " << serverConnection);
1565 
1566  serverComplete();
1567  return;
1568  }
1569 
1571 }
1572 
1573 bool
1575 {
1576  // TODO: Be more precise here. For example, if/when reading trailer, we may
1577  // not be doneWithServer() yet, but we should return false. Similarly, we
1578  // could still be writing the request body after receiving the whole reply.
1579  return !doneWithServer();
1580 }
1581 
1582 void
1584 {
1585  // too late to read
1587  return;
1588 
1589  if (!maybeMakeSpaceAvailable(false))
1590  return;
1591 
1592  // XXX: get rid of the do_next_read flag
1593  // check for the proper reasons preventing read(2)
1594  if (!flags.do_next_read)
1595  return;
1596 
1597  flags.do_next_read = false;
1598 
1599  // must not already be waiting for read(2) ...
1601 
1602  // wait for read(2) to be possible.
1604  AsyncCall::Pointer call = JobCallback(11, 5, Dialer, this, HttpStateData::readReply);
1606 }
1607 
1608 bool
1610 {
1611  // how much we are allowed to buffer
1612  const int limitBuffer = (flags.headers_parsed ? Config.readAheadGap : Config.maxReplyHeaderSize);
1613 
1614  if (limitBuffer < 0 || inBuf.length() >= (SBuf::size_type)limitBuffer) {
1615  // when buffer is at or over limit already
1616  debugs(11, 7, "will not read up to " << limitBuffer << ". buffer has (" << inBuf.length() << "/" << inBuf.spaceSize() << ") from " << serverConnection);
1617  debugs(11, DBG_DATA, "buffer has {" << inBuf << "}");
1618  // Process next response from buffer
1619  processReply();
1620  return false;
1621  }
1622 
1623  // how much we want to read
1624  const size_t read_size = calcBufferSpaceToReserve(inBuf.spaceSize(), (limitBuffer - inBuf.length()));
1625 
1626  if (!read_size) {
1627  debugs(11, 7, "will not read up to " << read_size << " into buffer (" << inBuf.length() << "/" << inBuf.spaceSize() << ") from " << serverConnection);
1628  return false;
1629  }
1630 
1631  // just report whether we could grow or not, do not actually do it
1632  if (doGrow)
1633  return (read_size >= 2);
1634 
1635  // we may need to grow the buffer
1636  inBuf.reserveSpace(read_size);
1637  debugs(11, 8, (!flags.do_next_read ? "will not" : "may") <<
1638  " read up to " << read_size << " bytes info buf(" << inBuf.length() << "/" << inBuf.spaceSize() <<
1639  ") from " << serverConnection);
1640 
1641  return (inBuf.spaceSize() >= 2); // only read if there is 1+ bytes of space available
1642 }
1643 
1645 void
1647 {
1648  debugs(11, 5, HERE << serverConnection << ": size " << io.size << ": errflag " << io.flag << ".");
1649 #if URL_CHECKSUM_DEBUG
1650 
1651  entry->mem_obj->checkUrlChecksum();
1652 #endif
1653 
1654  // XXX: Keep in sync with Client::sentRequestBody().
1655  // TODO: Extract common parts.
1656 
1657  if (io.size > 0) {
1658  fd_bytes(io.fd, io.size, FD_WRITE);
1659  statCounter.server.all.kbytes_out += io.size;
1660  statCounter.server.http.kbytes_out += io.size;
1661  }
1662 
1663  if (io.flag == Comm::ERR_CLOSING)
1664  return;
1665 
1666  // both successful and failed writes affect response times
1668 
1669  if (io.flag) {
1670  const auto err = new ErrorState(ERR_WRITE_ERROR, Http::scBadGateway, fwd->request, fwd->al);
1671  err->xerrno = io.xerrno;
1672  fwd->fail(err);
1673  closeServer();
1674  mustStop("HttpStateData::wroteLast");
1675  return;
1676  }
1677 
1678  sendComplete();
1679 }
1680 
1682 void
1684 {
1685  /*
1686  * Set the read timeout here because it hasn't been set yet.
1687  * We only set the read timeout after the request has been
1688  * fully written to the peer. If we start the timeout
1689  * after connection establishment, then we are likely to hit
1690  * the timeout for POST/PUT requests that have very large
1691  * request bodies.
1692  */
1694  AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
1695  TimeoutDialer, this, HttpStateData::httpTimeout);
1696 
1698  flags.request_sent = true;
1699 }
1700 
1701 void
1703 {
1704  debugs(11,5, HERE << "closing HTTP server " << serverConnection << " this " << this);
1705 
1709  closeHandler = NULL;
1711  }
1712 }
1713 
1714 bool
1716 {
1718 }
1719 
1720 /*
1721  * Fixup authentication request headers for special cases
1722  */
1723 static void
1725 {
1726  /* Nothing to do unless we are forwarding to a peer */
1727  if (!flags.peering)
1728  return;
1729 
1730  // This request is going "through" rather than "to" our _peer.
1731  if (flags.tunneling)
1732  return;
1733 
1734  /* Needs to be explicitly enabled */
1735  if (!request->peer_login)
1736  return;
1737 
1739  /* Maybe already dealt with? */
1740  if (hdr_out->has(header))
1741  return;
1742 
1743  /* Nothing to do here for PASSTHRU */
1744  if (strcmp(request->peer_login, "PASSTHRU") == 0)
1745  return;
1746 
1747  // Dangerous and undocumented PROXYPASS is a single-signon to servers with
1748  // the proxy password. Only Basic Authentication can work this way. This
1749  // statement forwards a "basic" Proxy-Authorization value from our client
1750  // to an originserver peer. Other PROXYPASS cases are handled lower.
1751  if (flags.toOrigin &&
1752  strcmp(request->peer_login, "PROXYPASS") == 0 &&
1754 
1755  const char *auth = hdr_in->getStr(Http::HdrType::PROXY_AUTHORIZATION);
1756 
1757  if (auth && strncasecmp(auth, "basic ", 6) == 0) {
1758  hdr_out->putStr(header, auth);
1759  return;
1760  }
1761  }
1762 
1763  char loginbuf[base64_encode_len(MAX_LOGIN_SZ)];
1764  size_t blen;
1765  struct base64_encode_ctx ctx;
1766  base64_encode_init(&ctx);
1767 
1768  /* Special mode to pass the username to the upstream cache */
1769  if (*request->peer_login == '*') {
1770  const char *username = "-";
1771 
1772  if (request->extacl_user.size())
1773  username = request->extacl_user.termedBuf();
1774 #if USE_AUTH
1775  else if (request->auth_user_request != NULL)
1776  username = request->auth_user_request->username();
1777 #endif
1778 
1779  blen = base64_encode_update(&ctx, loginbuf, strlen(username), reinterpret_cast<const uint8_t*>(username));
1780  blen += base64_encode_update(&ctx, loginbuf+blen, strlen(request->peer_login +1), reinterpret_cast<const uint8_t*>(request->peer_login +1));
1781  blen += base64_encode_final(&ctx, loginbuf+blen);
1782  httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1783  return;
1784  }
1785 
1786  /* external_acl provided credentials */
1787  if (request->extacl_user.size() && request->extacl_passwd.size() &&
1788  (strcmp(request->peer_login, "PASS") == 0 ||
1789  strcmp(request->peer_login, "PROXYPASS") == 0)) {
1790 
1791  blen = base64_encode_update(&ctx, loginbuf, request->extacl_user.size(), reinterpret_cast<const uint8_t*>(request->extacl_user.rawBuf()));
1792  blen += base64_encode_update(&ctx, loginbuf+blen, 1, reinterpret_cast<const uint8_t*>(":"));
1793  blen += base64_encode_update(&ctx, loginbuf+blen, request->extacl_passwd.size(), reinterpret_cast<const uint8_t*>(request->extacl_passwd.rawBuf()));
1794  blen += base64_encode_final(&ctx, loginbuf+blen);
1795  httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1796  return;
1797  }
1798  // if no external user credentials are available to fake authentication with PASS acts like PASSTHRU
1799  if (strcmp(request->peer_login, "PASS") == 0)
1800  return;
1801 
1802  /* Kerberos login to peer */
1803 #if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
1804  if (strncmp(request->peer_login, "NEGOTIATE",strlen("NEGOTIATE")) == 0) {
1805  char *Token=NULL;
1806  char *PrincipalName=NULL,*p;
1807  int negotiate_flags = 0;
1808 
1809  if ((p=strchr(request->peer_login,':')) != NULL ) {
1810  PrincipalName=++p;
1811  }
1812  if (request->flags.auth_no_keytab) {
1813  negotiate_flags |= PEER_PROXY_NEGOTIATE_NOKEYTAB;
1814  }
1815  Token = peer_proxy_negotiate_auth(PrincipalName, request->peer_host, negotiate_flags);
1816  if (Token) {
1817  httpHeaderPutStrf(hdr_out, header, "Negotiate %s",Token);
1818  }
1819  return;
1820  }
1821 #endif /* HAVE_KRB5 && HAVE_GSSAPI */
1822 
1823  blen = base64_encode_update(&ctx, loginbuf, strlen(request->peer_login), reinterpret_cast<const uint8_t*>(request->peer_login));
1824  blen += base64_encode_final(&ctx, loginbuf+blen);
1825  httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1826  return;
1827 }
1828 
1829 /*
1830  * build request headers and append them to a given MemBuf
1831  * used by buildRequestPrefix()
1832  * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
1833  */
1834 void
1836  StoreEntry * entry,
1837  const AccessLogEntryPointer &al,
1838  HttpHeader * hdr_out,
1839  const Http::StateFlags &flags)
1840 {
1841  /* building buffer for complex strings */
1842 #define BBUF_SZ (MAX_URL+32)
1843  LOCAL_ARRAY(char, bbuf, BBUF_SZ);
1844  LOCAL_ARRAY(char, ntoabuf, MAX_IPSTRLEN);
1845  const HttpHeader *hdr_in = &request->header;
1846  const HttpHeaderEntry *e = NULL;
1848  assert (hdr_out->owner == hoRequest);
1849 
1850  /* use our IMS header if the cached entry has Last-Modified time */
1851  if (request->lastmod > -1)
1853 
1854  // Add our own If-None-Match field if the cached entry has a strong ETag.
1855  // copyOneHeaderFromClientsideRequestToUpstreamRequest() adds client ones.
1856  if (request->etag.size() > 0) {
1858  request->etag.termedBuf()));
1859  }
1860 
1861  bool we_do_ranges = decideIfWeDoRanges (request);
1862 
1863  String strConnection (hdr_in->getList(Http::HdrType::CONNECTION));
1864 
1865  while ((e = hdr_in->getEntry(&pos)))
1866  copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, hdr_out, we_do_ranges, flags);
1867 
1868  /* Abstraction break: We should interpret multipart/byterange responses
1869  * into offset-length data, and this works around our inability to do so.
1870  */
1871  if (!we_do_ranges && request->multipartRangeRequest()) {
1872  /* don't cache the result */
1873  request->flags.cachable = false;
1874  /* pretend it's not a range request */
1875  request->ignoreRange("want to request the whole object");
1876  request->flags.isRanged = false;
1877  }
1878 
1879  hdr_out->addVia(request->http_ver, hdr_in);
1880 
1881  if (request->flags.accelerated) {
1882  /* Append Surrogate-Capabilities */
1883  String strSurrogate(hdr_in->getList(Http::HdrType::SURROGATE_CAPABILITY));
1884 #if USE_SQUID_ESI
1885  snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"", Config.Accel.surrogate_id);
1886 #else
1887  snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0\"", Config.Accel.surrogate_id);
1888 #endif
1889  strListAdd(&strSurrogate, bbuf, ',');
1890  hdr_out->putStr(Http::HdrType::SURROGATE_CAPABILITY, strSurrogate.termedBuf());
1891  }
1892 
1894  if (strcmp(opt_forwarded_for, "delete") != 0) {
1895 
1896  String strFwd = hdr_in->getList(Http::HdrType::X_FORWARDED_FOR);
1897 
1898  // if we cannot double strFwd size, then it grew past 50% of the limit
1899  if (!strFwd.canGrowBy(strFwd.size())) {
1900  // There is probably a forwarding loop with Via detection disabled.
1901  // If we do nothing, String will assert on overflow soon.
1902  // TODO: Terminate all transactions with huge XFF?
1903  strFwd = "error";
1904 
1905  static int warnedCount = 0;
1906  if (warnedCount++ < 100) {
1907  const SBuf url(entry ? SBuf(entry->url()) : request->effectiveRequestUri());
1908  debugs(11, DBG_IMPORTANT, "Warning: likely forwarding loop with " << url);
1909  }
1910  }
1911 
1912  if (strcmp(opt_forwarded_for, "on") == 0) {
1914  if ( request->client_addr.isNoAddr() )
1915  strListAdd(&strFwd, "unknown", ',');
1916  else
1917  strListAdd(&strFwd, request->client_addr.toStr(ntoabuf, MAX_IPSTRLEN), ',');
1918  } else if (strcmp(opt_forwarded_for, "off") == 0) {
1920  strListAdd(&strFwd, "unknown", ',');
1921  } else if (strcmp(opt_forwarded_for, "transparent") == 0) {
1923  } else if (strcmp(opt_forwarded_for, "truncate") == 0) {
1925  if ( request->client_addr.isNoAddr() )
1926  strFwd = "unknown";
1927  else
1928  strFwd = request->client_addr.toStr(ntoabuf, MAX_IPSTRLEN);
1929  }
1930  if (strFwd.size() > 0)
1931  hdr_out->putStr(Http::HdrType::X_FORWARDED_FOR, strFwd.termedBuf());
1932  }
1935  /* append Host if not there already */
1936  if (!hdr_out->has(Http::HdrType::HOST)) {
1937  if (request->peer_domain) {
1939  } else {
1940  SBuf authority = request->url.authority();
1941  hdr_out->putStr(Http::HdrType::HOST, authority.c_str());
1942  }
1943  }
1944 
1945  /* append Authorization if known in URL, not in header and going direct */
1946  if (!hdr_out->has(Http::HdrType::AUTHORIZATION)) {
1947  if (flags.toOrigin && !request->url.userInfo().isEmpty()) {
1948  static char result[base64_encode_len(MAX_URL*2)]; // should be big enough for a single URI segment
1949  struct base64_encode_ctx ctx;
1950  base64_encode_init(&ctx);
1951  size_t blen = base64_encode_update(&ctx, result, request->url.userInfo().length(), reinterpret_cast<const uint8_t*>(request->url.userInfo().rawContent()));
1952  blen += base64_encode_final(&ctx, result+blen);
1953  result[blen] = '\0';
1954  if (blen)
1955  httpHeaderPutStrf(hdr_out, Http::HdrType::AUTHORIZATION, "Basic %.*s", (int)blen, result);
1956  }
1957  }
1958 
1959  /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */
1960  httpFixupAuthentication(request, hdr_in, hdr_out, flags);
1961 
1962  /* append Cache-Control, add max-age if not there already */
1963  {
1964  HttpHdrCc *cc = hdr_in->getCc();
1965 
1966  if (!cc)
1967  cc = new HttpHdrCc();
1968 
1969  /* Add max-age only without no-cache */
1970  if (!cc->hasMaxAge() && !cc->hasNoCache()) {
1971  // XXX: performance regression. c_str() reallocates
1973  cc->maxAge(getMaxAge(entry ? entry->url() : tmp.c_str()));
1974  }
1975 
1976  /* Enforce sibling relations */
1977  if (flags.only_if_cached)
1978  cc->onlyIfCached(true);
1979 
1980  hdr_out->putCc(cc);
1981 
1982  delete cc;
1983  }
1984 
1985  // Always send Connection because HTTP/1.0 servers need explicit
1986  // "keep-alive", HTTP/1.1 servers need explicit "close", Upgrade recipients
1987  // need bare "upgrade", and we do not always know the server expectations.
1988  if (!hdr_out->has(Http::HdrType::CONNECTION)) // forwardUpgrade() may add it
1989  hdr_out->putStr(Http::HdrType::CONNECTION, flags.keepalive ? "keep-alive" : "close");
1990 
1991  /* append Front-End-Https */
1992  if (flags.front_end_https) {
1994  hdr_out->putStr(Http::HdrType::FRONT_END_HTTPS, "On");
1995  }
1996 
1997  if (flags.chunked_request) {
1998  // Do not just copy the original value so that if the client-side
1999  // starts decode other encodings, this code may remain valid.
2000  hdr_out->putStr(Http::HdrType::TRANSFER_ENCODING, "chunked");
2001  }
2002 
2003  /* Now mangle the headers. */
2004  httpHdrMangleList(hdr_out, request, al, ROR_REQUEST);
2005 
2006  strConnection.clean();
2007 }
2008 
2011 void
2013 {
2015  return; // forward nothing by default
2016 
2017  /* RFC 7230 section 6.7 paragraph 10:
2018  * A server MUST ignore an Upgrade header field that is received in
2019  * an HTTP/1.0 request.
2020  */
2021  if (request->http_ver == Http::ProtocolVersion(1,0))
2022  return;
2023 
2024  const auto &hdrIn = request->header;
2025  if (!hdrIn.has(Http::HdrType::UPGRADE))
2026  return;
2027  const auto upgradeIn = hdrIn.getList(Http::HdrType::UPGRADE);
2028 
2029  String upgradeOut;
2030 
2031  ACLFilledChecklist ch(nullptr, request.getRaw());
2032  ch.al = fwd->al;
2033  const char *pos = nullptr;
2034  const char *offeredStr = nullptr;
2035  int offeredStrLen = 0;
2036  while (strListGetItem(&upgradeIn, ',', &offeredStr, &offeredStrLen, &pos)) {
2037  const ProtocolView offeredProto(offeredStr, offeredStrLen);
2038  debugs(11, 5, "checks all rules applicable to " << offeredProto);
2039  Config.http_upgrade_request_protocols->forApplicable(offeredProto, [&ch, offeredStr, offeredStrLen, &upgradeOut] (const SBuf &cfgProto, const acl_access *guard) {
2040  debugs(11, 5, "checks " << cfgProto << " rule(s)");
2041  ch.changeAcl(guard);
2042  const auto answer = ch.fastCheck();
2043  if (answer.implicit)
2044  return false; // keep looking for an explicit rule match
2045  if (answer.allowed())
2046  strListAdd(upgradeOut, offeredStr, offeredStrLen);
2047  // else drop the offer (explicitly denied cases and ACL errors)
2048  return true; // stop after an explicit rule match or an error
2049  });
2050  }
2051 
2052  if (upgradeOut.size()) {
2053  hdrOut.putStr(Http::HdrType::UPGRADE, upgradeOut.termedBuf());
2054 
2055  /* RFC 7230 section 6.7 paragraph 10:
2056  * When Upgrade is sent, the sender MUST also send a Connection header
2057  * field that contains an "upgrade" connection option, in
2058  * order to prevent Upgrade from being accidentally forwarded by
2059  * intermediaries that might not implement the listed protocols.
2060  *
2061  * NP: Squid does not truly implement the protocol(s) in this Upgrade.
2062  * For now we are treating an explicit blind tunnel as "implemented"
2063  * regardless of the security implications.
2064  */
2065  hdrOut.putStr(Http::HdrType::CONNECTION, "upgrade");
2066 
2067  // Connection:close and Connection:keepalive confuse some Upgrade
2068  // recipients, so we do not send those headers. Our Upgrade request
2069  // implicitly offers connection persistency per HTTP/1.1 defaults.
2070  // Update the keepalive flag to reflect that offer.
2071  // * If the server upgrades, then we would not be talking HTTP past the
2072  // HTTP 101 control message, and HTTP persistence would be irrelevant.
2073  // * Otherwise, our request will contradict onoff.server_pconns=off or
2074  // other no-keepalive conditions (if any). We compensate by copying
2075  // the original no-keepalive decision now and honoring it later.
2077  flags.keepalive = true; // should already be true in most cases
2078  }
2079 }
2080 
2085 void
2086 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest * request, HttpHeader * hdr_out, const int we_do_ranges, const Http::StateFlags &flags)
2087 {
2088  debugs(11, 5, "httpBuildRequestHeader: " << e->name << ": " << e->value );
2089 
2090  switch (e->id) {
2091 
2099  if (!flags.toOrigin && request->peer_login &&
2100  (strcmp(request->peer_login, "PASS") == 0 ||
2101  strcmp(request->peer_login, "PROXYPASS") == 0 ||
2102  strcmp(request->peer_login, "PASSTHRU") == 0)) {
2103  hdr_out->addEntry(e->clone());
2104  }
2105  break;
2106 
2110  case Http::HdrType::TE:
2113  case Http::HdrType::TRAILER:
2115  break;
2116 
2119  break;
2120 
2127  if (!flags.toOriginPeer()) {
2128  hdr_out->addEntry(e->clone());
2129  } else {
2134  if (request->peer_login &&
2135  (strcmp(request->peer_login, "PASS") == 0 ||
2136  strcmp(request->peer_login, "PASSTHRU") == 0 ||
2137  strcmp(request->peer_login, "PROXYPASS") == 0)) {
2138  hdr_out->addEntry(e->clone());
2139  }
2140  }
2141 
2142  break;
2143 
2144  case Http::HdrType::HOST:
2151  if (request->peer_domain)
2152  hdr_out->putStr(Http::HdrType::HOST, request->peer_domain);
2153  else if (request->flags.redirected && !Config.onoff.redir_rewrites_host)
2154  hdr_out->addEntry(e->clone());
2155  else {
2156  SBuf authority = request->url.authority();
2157  hdr_out->putStr(Http::HdrType::HOST, authority.c_str());
2158  }
2159 
2160  break;
2161 
2170  // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2171  if (hdr_out->has(Http::HdrType::IF_MODIFIED_SINCE))
2172  break;
2173  else if (Config.onoff.cache_miss_revalidate || !request->flags.cachable || request->flags.auth)
2174  hdr_out->addEntry(e->clone());
2175  break;
2176 
2185  // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2186  if (hdr_out->hasListMember(Http::HdrType::IF_MATCH, "*", ',') || Config.onoff.cache_miss_revalidate || !request->flags.cachable || request->flags.auth)
2187  hdr_out->addEntry(e->clone());
2188  break;
2189 
2194  const int64_t hops = e->getInt64();
2195 
2196  if (hops > 0)
2197  hdr_out->putInt64(Http::HdrType::MAX_FORWARDS, hops - 1);
2198  }
2199 
2200  break;
2201 
2202  case Http::HdrType::VIA:
2207  if (!Config.onoff.via)
2208  hdr_out->addEntry(e->clone());
2209 
2210  break;
2211 
2212  case Http::HdrType::RANGE:
2213 
2215 
2219  if (!we_do_ranges)
2220  hdr_out->addEntry(e->clone());
2221 
2222  break;
2223 
2224  case Http::HdrType::PROXY_CONNECTION: // SHOULD ignore. But doing so breaks things.
2225  break;
2226 
2228  // pass through unless we chunk; also, keeping this away from default
2229  // prevents request smuggling via Connection: Content-Length tricks
2230  if (!flags.chunked_request)
2231  hdr_out->addEntry(e->clone());
2232  break;
2233 
2235 
2240  break;
2241 
2245  if (!flags.front_end_https)
2246  hdr_out->addEntry(e->clone());
2247 
2248  break;
2249 
2250  default:
2254  if (strConnection.size()>0 && strListIsMember(&strConnection, e->name, ',')) {
2255  debugs(11, 2, "'" << e->name << "' header cropped by Connection: definition");
2256  return;
2257  }
2258 
2259  hdr_out->addEntry(e->clone());
2260  }
2261 }
2262 
2263 bool
2265 {
2266  bool result = true;
2267  /* decide if we want to do Ranges ourselves
2268  * and fetch the whole object now)
2269  * We want to handle Ranges ourselves iff
2270  * - we can actually parse client Range specs
2271  * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
2272  * - reply will be cachable
2273  * (If the reply will be uncachable we have to throw it away after
2274  * serving this request, so it is better to forward ranges to
2275  * the server and fetch only the requested content)
2276  */
2277 
2278  int64_t roffLimit = request->getRangeOffsetLimit();
2279 
2280  if (NULL == request->range || !request->flags.cachable
2282  result = false;
2283 
2284  debugs(11, 8, "decideIfWeDoRanges: range specs: " <<
2285  request->range << ", cachable: " <<
2286  request->flags.cachable << "; we_do_ranges: " << result);
2287 
2288  return result;
2289 }
2290 
2291 /* build request prefix and append it to a given MemBuf;
2292  * return the length of the prefix */
2293 mb_size_t
2295 {
2296  const int offset = mb->size;
2297  /* Uses a local httpver variable to print the HTTP label
2298  * since the HttpRequest may have an older version label.
2299  * XXX: This could create protocol bugs as the headers sent and
2300  * flow control should all be based on the HttpRequest version
2301  * not the one we are sending. Needs checking.
2302  */
2303  const AnyP::ProtocolVersion httpver = Http::ProtocolVersion();
2305  mb->appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\r\n",
2307  SQUIDSBUFPRINT(url),
2309  httpver.major,httpver.minor);
2310  /* build and pack headers */
2311  {
2312  HttpHeader hdr(hoRequest);
2313  forwardUpgrade(hdr); // before httpBuildRequestHeader() for CONNECTION
2315 
2317  request->flags.authSent = true;
2318  else if (hdr.has(Http::HdrType::AUTHORIZATION))
2319  request->flags.authSent = true;
2320 
2321  // The late placement of this check supports reply_header_add mangling,
2322  // but also complicates optimizing upgradeHeaderOut-like lookups.
2323  if (hdr.has(Http::HdrType::UPGRADE)) {
2326  }
2327 
2328  hdr.packInto(mb);
2329  hdr.clean();
2330  }
2331  /* append header terminator */
2332  mb->append(crlf, 2);
2333  return mb->size - offset;
2334 }
2335 
2336 /* This will be called when connect completes. Write request. */
2337 bool
2339 {
2340  MemBuf mb;
2341 
2342  debugs(11, 5, HERE << serverConnection << ", request " << request << ", this " << this << ".");
2343 
2345  debugs(11,3, HERE << "cannot send request to closing " << serverConnection);
2346  assert(closeHandler != NULL);
2347  return false;
2348  }
2349 
2351  AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
2352  TimeoutDialer, this, HttpStateData::httpTimeout);
2354  flags.do_next_read = true;
2356 
2357  if (request->body_pipe != NULL) {
2358  if (!startRequestBodyFlow()) // register to receive body data
2359  return false;
2361  requestSender = JobCallback(11,5,
2362  Dialer, this, HttpStateData::sentRequestBody);
2363 
2365  // use chunked encoding if we do not know the length
2366  if (request->content_length < 0)
2367  flags.chunked_request = true;
2368  } else {
2371  requestSender = JobCallback(11,5,
2372  Dialer, this, HttpStateData::wroteLast);
2373  }
2374 
2375  /*
2376  * Is keep-alive okay for all request methods?
2377  */
2379  flags.keepalive = true;
2380  else if (request->flags.pinned)
2382  else if (!Config.onoff.server_pconns)
2383  flags.keepalive = false;
2384  else if (flags.tunneling)
2385  // tunneled non pinned bumped requests must not keepalive
2387  else if (_peer == NULL)
2388  flags.keepalive = true;
2389  else if (_peer->stats.n_keepalives_sent < 10)
2390  flags.keepalive = true;
2391  else if ((double) _peer->stats.n_keepalives_recv /
2392  (double) _peer->stats.n_keepalives_sent > 0.50)
2393  flags.keepalive = true;
2394 
2395  if (_peer && !flags.tunneling) {
2396  /*The old code here was
2397  if (neighborType(_peer, request->url) == PEER_SIBLING && ...
2398  which is equivalent to:
2399  if (neighborType(_peer, URL()) == PEER_SIBLING && ...
2400  or better:
2401  if (((_peer->type == PEER_MULTICAST && p->options.mcast_siblings) ||
2402  _peer->type == PEER_SIBLINGS ) && _peer->options.allow_miss)
2403  flags.only_if_cached = 1;
2404 
2405  But I suppose it was a bug
2406  */
2408  flags.only_if_cached = true;
2409 
2411  }
2412 
2413  mb.init();
2415  buildRequestPrefix(&mb);
2416 
2417  debugs(11, 2, "HTTP Server " << serverConnection);
2418  debugs(11, 2, "HTTP Server REQUEST:\n---------\n" << mb.buf << "\n----------");
2419 
2421  return true;
2422 }
2423 
2424 bool
2426 {
2427  // parent's implementation can handle the no-encoding case
2428  if (!flags.chunked_request)
2429  return Client::getMoreRequestBody(buf);
2430 
2431  MemBuf raw;
2432 
2434  if (!requestBodySource->getMoreData(raw))
2435  return false; // no request body bytes to chunk yet
2436 
2437  // optimization: pre-allocate buffer size that should be enough
2438  const mb_size_t rawDataSize = raw.contentSize();
2439  // we may need to send: hex-chunk-size CRLF raw-data CRLF last-chunk
2440  buf.init(16 + 2 + rawDataSize + 2 + 5, raw.max_capacity);
2441 
2442  buf.appendf("%x\r\n", static_cast<unsigned int>(rawDataSize));
2443  buf.append(raw.content(), rawDataSize);
2444  buf.append("\r\n", 2);
2445 
2446  Must(rawDataSize > 0); // we did not accidentally created last-chunk above
2447 
2448  // Do not send last-chunk unless we successfully received everything
2451  flags.sentLastChunk = true;
2452  buf.append("0\r\n\r\n", 5);
2453  }
2454 
2455  return true;
2456 }
2457 
2458 void
2460 {
2461  debugs(11, 3, fwd->request->method << ' ' << fwd->entry->url());
2462  AsyncJob::Start(new HttpStateData(fwd));
2463 }
2464 
2465 void
2467 {
2468  if (!sendRequest()) {
2469  debugs(11, 3, "httpStart: aborted");
2470  mustStop("HttpStateData::start failed");
2471  return;
2472  }
2473 
2474  ++ statCounter.server.all.requests;
2475  ++ statCounter.server.http.requests;
2476 
2477  /*
2478  * We used to set the read timeout here, but not any more.
2479  * Now its set in httpSendComplete() after the full request,
2480  * including request body, has been written to the server.
2481  */
2482 }
2483 
2485 bool
2487 {
2488 #if USE_HTTP_VIOLATIONS
2489  if (!Config.accessList.brokenPosts) {
2490  debugs(11, 5, HERE << "No brokenPosts list");
2491  return false;
2492  }
2493 
2495  ch.al = fwd->al;
2496  ch.syncAle(originalRequest().getRaw(), nullptr);
2497  if (!ch.fastCheck().allowed()) {
2498  debugs(11, 5, HERE << "didn't match brokenPosts");
2499  return false;
2500  }
2501 
2503  debugs(11, 3, HERE << "ignoring broken POST for closed " << serverConnection);
2504  assert(closeHandler != NULL);
2505  return true; // prevent caller from proceeding as if nothing happened
2506  }
2507 
2508  debugs(11, 3, "finishingBrokenPost: fixing broken POST");
2510  requestSender = JobCallback(11,5,
2511  Dialer, this, HttpStateData::wroteLast);
2513  return true;
2514 #else
2515  return false;
2516 #endif /* USE_HTTP_VIOLATIONS */
2517 }
2518 
2520 bool
2522 {
2523  if (flags.sentLastChunk) {
2524  debugs(11, 5, HERE << "already sent last-chunk");
2525  return false;
2526  }
2527 
2528  Must(receivedWholeRequestBody); // or we should not be sending last-chunk
2529  flags.sentLastChunk = true;
2530 
2532  requestSender = JobCallback(11,5, Dialer, this, HttpStateData::wroteLast);
2533  Comm::Write(serverConnection, "0\r\n\r\n", 5, requestSender, NULL);
2534  return true;
2535 }
2536 
2537 void
2539 {
2541  debugs(11,5, HERE << serverConnection);
2542 
2543  // do we need to write something after the last body byte?
2545  return;
2547  return;
2548 
2549  sendComplete();
2550 }
2551 
2552 // more origin request body data is available
2553 void
2555 {
2557  // XXX: we should check this condition in other callbacks then!
2558  // TODO: Check whether this can actually happen: We should unsubscribe
2559  // as a body consumer when the above condition(s) are detected.
2560  debugs(11, DBG_IMPORTANT, HERE << "Transaction aborted while reading HTTP body");
2561  return;
2562  }
2563 
2565 
2566  if (requestBodySource->buf().hasContent()) {
2567  // XXX: why does not this trigger a debug message on every request?
2568 
2570  flags.abuse_detected = true;
2571  debugs(11, DBG_IMPORTANT, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << request->client_addr << "' -> '" << entry->url() << "'" );
2572 
2573  if (virginReply()->sline.status() == Http::scInvalidHeader) {
2574  closeServer();
2575  mustStop("HttpStateData::handleMoreRequestBodyAvailable");
2576  return;
2577  }
2578  }
2579  }
2580 
2582 }
2583 
2584 // premature end of the request body
2585 void
2587 {
2589  if (entry->isEmpty()) {
2590  debugs(11, 3, "request body aborted: " << serverConnection);
2591  // We usually get here when ICAP REQMOD aborts during body processing.
2592  // We might also get here if client-side aborts, but then our response
2593  // should not matter because either client-side will provide its own or
2594  // there will be no response at all (e.g., if the the client has left).
2596  static const auto d = MakeNamedErrorDetail("SRV_REQMOD_REQ_BODY");
2597  err->detailError(d);
2598  fwd->fail(err);
2599  }
2600 
2601  abortTransaction("request body producer aborted");
2602 }
2603 
2604 // called when we wrote request headers(!) or a part of the body
2605 void
2607 {
2608  if (io.size > 0)
2609  statCounter.server.http.kbytes_out += io.size;
2610 
2612 }
2613 
2614 void
2615 HttpStateData::abortAll(const char *reason)
2616 {
2617  debugs(11,5, HERE << "aborting transaction for " << reason <<
2618  "; " << serverConnection << ", this " << this);
2619  mustStop(reason);
2620 }
2621 
2623  : answer(HttpStateData::ReuseDecision::reuseNot), reason(nullptr), entry(e), statusCode(code) {}
2624 
2627 {
2628  answer = ans;
2629  reason = why;
2630  return answer;
2631 }
2632 
2633 std::ostream &operator <<(std::ostream &os, const HttpStateData::ReuseDecision &d)
2634 {
2635  static const char *ReuseMessages[] = {
2636  "do not cache and do not share", // reuseNot
2637  "cache positively and share", // cachePositively
2638  "cache negatively and share", // cacheNegatively
2639  "do not cache but share" // doNotCacheButShare
2640  };
2641 
2644  return os << ReuseMessages[d.answer] << " because " << d.reason <<
2645  "; HTTP status " << d.statusCode << " " << *(d.entry);
2646 }
2647 
const MemBuf & buf() const
Definition: BodyPipe.h:137
void drop1xx(const char *reason)
Definition: http.cc:812
void Read(const Comm::ConnectionPointer &conn, AsyncCall::Pointer &callback)
Definition: Read.cc:40
bool proxy_only
Definition: CachePeer.h:82
virtual void haveParsedReplyHeaders()
called when we have final (possibly adapted) reply headers; kids extend
Definition: http.cc:944
const char * xstrerr(int error)
Definition: xstrerror.cc:83
void makePrivate(const bool shareable)
Definition: store.cc:172
bool handling1xx
Whether we are processing an HTTP 1xx control message.
Definition: StateFlags.h:30
struct IoStats::@79 Http
char method[16]
Definition: tcp-banger2.c:115
bool hasProxyRevalidate() const
Definition: HttpHdrCc.h:117
bool maybeMakeSpaceAvailable(bool grow)
Definition: http.cc:1609
char * buf
Definition: MemBuf.h:134
AsyncCall::Pointer comm_add_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:922
int connection_auth
0 - off, 1 - on, 2 - auto
Definition: CachePeer.h:195
virtual bool getMoreRequestBody(MemBuf &buf)
either fill buf with available [encoded] request body bytes or return false
Definition: Client.cc:439
void truncateVirginBody()
Definition: http.cc:1382
char * peer_host
Definition: HttpRequest.h:165
bool keepalive_broken
Definition: StateFlags.h:58
unsigned int major
major version number
StoreEntry * entry
Definition: Client.h:168
bool keepalive
whether the Squid-sent request offers to keep the connection persistent
Definition: StateFlags.h:21
@ scMethodNotAllowed
Definition: StatusCode.h:49
bool hasPrivate(const String **val=nullptr) const
Definition: HttpHdrCc.h:74
@ scNonAuthoritativeInformation
Definition: StatusCode.h:29
AnyP::ProtocolVersion http_ver
Definition: Message.h:73
void userInfo(const SBuf &s)
Definition: Uri.h:79
#define base64_encode_len(length)
Definition: base64.h:169
@ HDR_X_ACCELERATOR_VARY
void ignoreRange(const char *reason)
forgets about the cached Range header (for a reason)
Definition: HttpRequest.cc:629
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
struct SquidConfig::@97 Timeout
@ scUnauthorized
Definition: StatusCode.h:45
@ scPayloadTooLarge
Definition: StatusCode.h:57
@ METHOD_HEAD
Definition: MethodType.h:28
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
http_hdr_owner_type owner
Definition: HttpHeader.h:173
bool ignoreCacheControl
Definition: http.h:70
Definition: Client.h:35
@ ERR_READ_ERROR
Definition: forward.h:28
Cbc * valid() const
was set and is valid
Definition: CbcPointer.h:41
BodyPipe::Pointer body_pipe
optional pipeline to receive message body
Definition: Message.h:98
bool spoofClientIp
Definition: RequestFlags.h:70
const char * storeId() const
Definition: MemObject.cc:53
void releaseRequest(const bool shareable=false)
Definition: store.cc:472
@ scBadRequest
Definition: StatusCode.h:44
bool chunked
reading a chunked response; TODO: rename
Definition: StateFlags.h:62
const char * getStr(Http::HdrType id) const
Definition: HttpHeader.cc:1191
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:75
mb_size_t size
Definition: MemBuf.h:135
virtual bool getMoreRequestBody(MemBuf &buf)
either fill buf with available [encoded] request body bytes or return false
Definition: http.cc:2425
int reads
Definition: IoStats.h:19
bool makePublic(const KeyScope keyScope=ksDefault)
Definition: store.cc:165
HttpHeader header
Definition: Message.h:75
bool offsetLimitExceeded(const int64_t limit) const
#define EBIT_SET(flag, bit)
Definition: defines.h:67
int hasListMember(Http::HdrType id, const char *member, const char separator) const
Definition: HttpHeader.cc:1690
Http::StateFlags flags
Definition: http.h:67
@ scMisdirectedRequest
Definition: StatusCode.h:62
Http::StatusCode peer_reply_status
last HTTP status code received
bool MonitorsRead(int fd)
whether the FD socket is being monitored for read
Definition: Read.cc:29
Answers make(const Answers ans, const char *why)
stores the corresponding decision
Definition: http.cc:2626
bool isEmpty() const
Definition: SBuf.h:424
struct CachePeer::@30 options
MemObject * mem_obj
Definition: Store.h:222
AsyncCall::Pointer requestSender
Definition: Client.h:174
RequestFlags flags
Definition: HttpRequest.h:141
String * upgradeHeaderOut
Upgrade header value sent to the origin server or cache peer.
Definition: http.h:74
void checkDateSkew(HttpReply *)
Definition: http.cc:631
ssize_t HttpHeaderPos
Definition: HttpHeader.h:45
int64_t payloadTruncated
positive when we read more than we wanted
Definition: http.h:155
virtual void abortAll(const char *reason)
abnormal transaction termination; reason is for debugging only
Definition: http.cc:2615
void reserveSpace(size_type minSpace)
Definition: SBuf.h:433
@ scLengthRequired
Definition: StatusCode.h:55
const Http::StatusCode statusCode
HTTP status for debugging.
Definition: http.h:41
virtual bool mayReadVirginReplyBody() const
whether we may receive more virgin response body bytes
Definition: http.cc:1574
bool surrogateNoStore
Definition: http.h:71
const char * url() const
Definition: store.cc:1583
a reference to a protocol name[/version] string; no 0-termination is assumed
int lastChunk
Definition: http.h:66
Comm::Flag ReadNow(CommIoCbParams &params, SBuf &buf)
Definition: Read.cc:81
@ KEY_PRIVATE
Definition: enums.h:102
size_t base64_encode_final(struct base64_encode_ctx *ctx, char *dst)
Definition: base64.c:308
static void httpFixupAuthentication(HttpRequest *request, const HttpHeader *hdr_in, HttpHeader *hdr_out, const Http::StateFlags &flags)
Definition: http.cc:1724
@ ENTRY_FWD_HDR_WAIT
Definition: enums.h:111
struct StatCounters::@129::@139 http
int64_t getRangeOffsetLimit()
Definition: HttpRequest.cc:602
MemObject & mem()
Definition: Store.h:52
#define HttpHeaderInitPos
Definition: HttpHeader.h:48
#define ScheduleCallHere(call)
Definition: AsyncCall.h:166
virtual void handleRequestBodyProducerAborted()=0
Definition: Client.cc:352
void error(char *format,...)
AccessLogEntryPointer al
info for the future access.log entry
Definition: FwdState.h:171
unsigned int minor
minor version number
@ PROXY_CONNECTION
#define PEER_PROXY_NEGOTIATE_NOKEYTAB
bool finishingBrokenPost()
if broken posts are enabled for the request, try to fix and return true
Definition: http.cc:2486
CachePeer * _peer
Definition: http.h:64
bool connectionAuth
Definition: RequestFlags.h:76
@ scUnsupportedMediaType
Definition: StatusCode.h:59
void httpHdrMangleList(HttpHeader *l, HttpRequest *request, const AccessLogEntryPointer &al, req_or_rep_t req_or_rep)
bool hasContent() const
Definition: MemBuf.h:54
HttpReplyPointer reply
int64_t bodySize(const HttpRequestMethod &) const
Definition: HttpReply.cc:375
void init(mb_size_t szInit, mb_size_t szMax)
Definition: MemBuf.cc:95
const char * ProtocolType_str[]
@ INPROGRESS
Definition: Flag.h:22
Definition: SBuf.h:87
virtual void sendControlMsg(HttpControlMsg)
called to send the 1xx message and notify the Source
mb_size_t buildRequestPrefix(MemBuf *mb)
Definition: http.cc:2294
size_t maxReplyHeaderSize
Definition: SquidConfig.h:135
ConnectionStatus statusIfComplete() const
Definition: http.cc:1074
AsyncCall::Pointer closeHandler
Definition: http.h:91
char * opt_forwarded_for
ConnectionStatus
Definition: http.h:92
@ scUseProxy
Definition: StatusCode.h:41
int commSetConnTimeout(const Comm::ConnectionPointer &conn, int timeout, AsyncCall::Pointer &callback)
Definition: comm.cc:564
HttpStateData(FwdState *)
Definition: http.cc:77
@ scMovedPermanently
Definition: StatusCode.h:37
void noStoreRemote(bool v)
static void assembleVaryKey(String &vary, SBuf &vstr, const HttpRequest &request)
assemble a variant key (vary-mark) from the given Vary header and HTTP request
Definition: http.cc:556
DelayId mostBytesAllowed() const
Definition: MemObject.cc:462
@ ERR_INVALID_RESP
Definition: forward.h:41
bool peering
Whether the next TCP hop is a cache_peer, including originserver.
Definition: StateFlags.h:40
BodyPipe::Pointer requestBodySource
Definition: Client.h:173
a representation of a refresh pattern.
const char * blockSwitchingProtocols(const HttpReply &) const
Definition: http.cc:830
PconnPool * fwdPconnPool
a collection of previously used persistent Squid-to-peer HTTP(S) connections
Definition: FwdState.cc:78
String getList(Http::HdrType id) const
Definition: HttpHeader.cc:841
bool sendRequest()
Definition: http.cc:2338
int fd
FD which the call was about. Set by the async call creator.
Definition: CommCalls.h:90
bool chunked_request
writing a chunked request
Definition: StateFlags.h:63
int detect_broken_server_pconns
Definition: SquidConfig.h:321
C * getRaw() const
Definition: RefCount.h:80
size_type spaceSize() const
Definition: SBuf.h:386
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:211
uint16_t flags
Definition: Store.h:233
AccessLogEntry::Pointer al
info for the future access.log, and external ACL
const SBuf & image() const
Acl::Answer const & fastCheck()
Definition: Checklist.cc:332
void processReplyBody()
Definition: http.cc:1465
void toLower()
converts all characters to lower case;
Definition: SBuf.cc:811
bool IsConnOpen(const Comm::ConnectionPointer &conn)
Definition: Connection.cc:27
bool allow_miss
Definition: CachePeer.h:103
IoStats IOStats
struct StatCounters::@129 server
@ OK
Definition: Flag.h:16
bool isAccepting() const
Definition: store.cc:1990
Http::StatusLine sline
Definition: HttpReply.h:56
time_t getMaxAge(const char *url)
Definition: refresh.cc:643
@ ENDFILE
Definition: Flag.h:27
Comm::ConnectionPointer serverConnection
Definition: http.h:90
@ ERR_NONE
Definition: forward.h:15
int surrogate_is_remote
Definition: SquidConfig.h:320
StatusCode
Definition: StatusCode.h:20
static const char *const crlf
Definition: http.cc:71
err_type
Definition: forward.h:14
void adaptOrFinalizeReply()
Definition: Client.cc:997
void unregister(Comm::ConnectionPointer &conn)
Definition: FwdState.cc:531
bool multipartRangeRequest() const
Definition: HttpRequest.cc:435
void fail(ErrorState *err)
Definition: FwdState.cc:490
void keepaliveAccounting(HttpReply *)
Definition: http.cc:611
bool startRequestBodyFlow()
Definition: Client.cc:240
@ scRequestedRangeNotSatisfied
Definition: StatusCode.h:60
@ ERR_CLOSING
Definition: Flag.h:25
virtual void haveParsedReplyHeaders()
called when we have final (possibly adapted) reply headers; kids extend
Definition: Client.cc:538
bool Is1xx(const int sc)
whether this is an informational 1xx response status code
Definition: StatusCode.h:92
#define cbdataReference(var)
Definition: cbdata.h:341
int front_end_https
0 - off, 1 - on, 2 - auto
Definition: CachePeer.h:194
bool connectionAuthDisabled
Definition: RequestFlags.h:78
void reset()
Definition: store.cc:1638
@ IF_MODIFIED_SINCE
#define DBG_IMPORTANT
Definition: Debug.h:41
bool decodeAndWriteReplyBody()
Definition: http.cc:1433
int xerrno
The last errno to occur. non-zero if flag is Comm::COMM_ERROR.
Definition: CommCalls.h:88
void httpHeaderPutStrf(HttpHeader *hdr, Http::HdrType id, const char *fmt,...)
StoreEntry * storeGetPublicByRequest(HttpRequest *req, const KeyScope keyScope)
Definition: store.cc:530
ProtocolType protocol
which protocol this version is for
void readReply(const CommIoCbParams &io)
Definition: http.cc:1183
static bool decideIfWeDoRanges(HttpRequest *orig_request)
Definition: http.cc:2264
size_t getMoreData(MemBuf &buf)
Definition: BodyPipe.cc:294
@ scSeeOther
Definition: StatusCode.h:39
SBuf httpMakeVaryMark(HttpRequest *request, HttpReply const *reply)
Definition: http.cc:592
StoreEntry * storeGetPublicByRequestMethod(HttpRequest *req, const HttpRequestMethod &method, const KeyScope keyScope)
Definition: store.cc:524
char * toStr(char *buf, const unsigned int blen, int force=AF_UNSPEC) const
Definition: Address.cc:792
AnyP::UriScheme const & getScheme() const
Definition: Uri.h:67
void serverComplete()
Definition: Client.cc:181
@ METHOD_OPTIONS
Definition: MethodType.h:31
@ CONTENT_LENGTH
SBuf & chop(size_type pos, size_type n=npos)
Definition: SBuf.cc:530
bool chunked() const
Definition: HttpHeader.h:165
mb_size_t max_capacity
Definition: MemBuf.h:142
peer_t neighborType(const CachePeer *p, const AnyP::Uri &url)
Definition: neighbors.cc:116
#define MAX_IPSTRLEN
Length of buffer that needs to be allocated to old a null-terminated IP-string.
Definition: forward.h:25
@ scNotAcceptable
Definition: StatusCode.h:50
@ ENTRY_ABORTED
Definition: enums.h:115
@ COMPLETE_NONPERSISTENT_MSG
Definition: http.h:95
Definition: Range.h:19
@ scGatewayTimeout
Definition: StatusCode.h:75
Definition: Debug.h:184
#define MAX_URL
Definition: defines.h:78
bool persistent() const
Definition: Message.cc:247
void sendComplete()
successfully wrote the entire request (including body, last-chunk, etc.)
Definition: http.cc:1683
time_t lastmod
Definition: HttpRequest.h:167
void strListAdd(String &str, const char *item, const size_t itemSize, const char delimiter)
Appends the given item of a given size to a delimiter-separated list in str.
Definition: StrList.cc:18
bool canGrowBy(const size_type growthLen) const
whether appending growthLen characters is safe (i.e., unlikely to assert)
Definition: SquidString.h:124
@ ERR_ICAP_FAILURE
Definition: forward.h:64
@ HTCP_CLR_INVALIDATION
Definition: enums.h:243
void bytesIn(int qty)
Definition: DelayId.cc:152
HttpHdrScTarget * getMergedTarget(const char *ourtarget)
Definition: HttpHdrSc.cc:296
String etag
A strong etag of the cached entry. Used for refreshing that entry.
Definition: HttpRequest.h:191
void processReplyHeader()
Definition: http.cc:648
ReuseDecision(const StoreEntry *e, const Http::StatusCode code)
Definition: http.cc:2622
virtual bool expectingBody(const HttpRequestMethod &, int64_t &) const
Definition: HttpReply.cc:488
bool httpHeaderHasConnDir(const HttpHeader *hdr, const SBuf &directive)
static void httpMaybeRemovePublic(StoreEntry *, Http::StatusCode)
Definition: http.cc:174
time_t expires
Definition: HttpReply.h:44
mb_size_t contentSize() const
available data size
Definition: MemBuf.h:47
int64_t getInt64() const
Definition: HttpHeader.cc:1562
@ scTemporaryRedirect
Definition: StatusCode.h:42
parameters for the async notePinnedConnectionBecameIdle() call
Definition: client_side.h:184
unsigned int front_end_https
send "Front-End-Https: On" header (off/on/auto=2)
Definition: StateFlags.h:18
#define NULL
Definition: types.h:166
@ COMPLETE_PERSISTENT_MSG
Definition: http.h:94
const char * rawContent() const
Definition: SBuf.cc:509
SBuf consume(size_type n=npos)
Definition: SBuf.cc:481
int n_keepalives_sent
Definition: CachePeer.h:51
@ scFailedDependency
Definition: StatusCode.h:65
void maxAge(int32_t v)
Definition: HttpHdrCc.h:123
@ scSwitchingProtocols
Definition: StatusCode.h:23
int strListIsMember(const String *list, const SBuf &m, char del)
Definition: StrList.cc:46
virtual void doneSendingRequestBody()=0
Definition: Client.cc:341
time_t read
Definition: SquidConfig.h:110
@ scForbidden
Definition: StatusCode.h:47
@ ROR_REQUEST
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
bool dontRetry()
Definition: FwdState.h:98
void mustStop(const char *aReason)
Definition: AsyncJob.cc:70
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
const HttpReply * virginReply() const
Definition: Client.cc:110
SBuf inBuf
I/O buffer for receiving server responses.
Definition: http.h:69
bundles HTTP 1xx reply and the "successfully forwarded" callback
@ scInsufficientStorage
Definition: StatusCode.h:78
SBuf vary_headers
Definition: MemObject.h:204
@ scPreconditionFailed
Definition: StatusCode.h:56
@ scNotImplemented
Definition: StatusCode.h:72
Http::StatusCode status() const
retrieve the status code for this status line
Definition: StatusLine.h:45
struct StatCounters::@129::@139 all
MemBlob::size_type size_type
Definition: SBuf.h:89
#define MAX_LOGIN_SZ
Definition: defines.h:79
bool hasNoCacheWithParameters() const
Definition: HttpHdrCc.h:87
void forApplicable(const ProtocolView &, const Visitor &) const
bool hasNoStore() const
virtual void handleMoreRequestBodyAvailable()
Definition: http.cc:2554
void handle1xx(HttpReply *msg)
ignore or start forwarding the 1xx response (a.k.a., control message)
Definition: http.cc:753
@ srcHttp
http_port or HTTP server
Definition: Message.h:39
@ scBadGateway
Definition: StatusCode.h:73
const StoreEntry * entry
entry for debugging
Definition: http.h:40
Definition: MemBuf.h:24
void doneSendingRequestBody()
Definition: http.cc:2538
bool toOriginPeer() const
Whether the next TCP/HTTP hop is an originserver cache_peer.
Definition: StateFlags.h:56
#define EBIT_TEST(flag, bit)
Definition: defines.h:69
@ RELEASE_REQUEST
prohibits making the key public
Definition: enums.h:98
CachePeer * getPeer() const
Definition: Connection.cc:124
struct CachePeer::@28 stats
void push(const Comm::ConnectionPointer &serverConn, const char *domain)
Definition: pconn.cc:412
unsigned char code
Definition: html_quote.c:20
bool canHandle1xx() const
whether the client is likely to be able to handle a 1xx reply
Definition: HttpRequest.cc:643
std::ostream & HERE(std::ostream &s)
Definition: Debug.h:152
virtual void handleRequestBodyProducerAborted()
Definition: http.cc:2586
@ scPartialContent
Definition: StatusCode.h:32
@ scGone
Definition: StatusCode.h:54
Http::HdrType id
Definition: HttpHeader.h:63
void proceedAfter1xx()
restores state and resumes processing after 1xx is ignored or forwarded
Definition: http.cc:857
ConnectionStatus persistentConnStatus() const
Definition: http.cc:1125
bool hasSMaxAge(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:127
@ INCOMPLETE_MSG
Definition: http.h:93
#define CallJobHere(debugSection, debugLevel, job, Class, method)
Definition: AsyncJobCalls.h:58
char * peer_domain
Definition: HttpRequest.h:172
@ scUnprocessableEntity
Definition: StatusCode.h:63
#define BBUF_SZ
void httpTimeout(const CommTimeoutCbParams &params)
Definition: http.cc:151
Comm::ConnectionPointer conn
Definition: CommCalls.h:85
Answers answer
the decision id
Definition: http.h:38
void addEntry(HttpHeaderEntry *e)
Definition: HttpHeader.cc:764
bool hasNoCache(const String **val=nullptr) const
Definition: HttpHdrCc.h:89
HttpRequestMethod method
Definition: MemObject.h:137
virtual bool doneWithServer() const
Definition: http.cc:1715
store_status_t store_status
Definition: Store.h:245
@ scPermanentRedirect
Definition: StatusCode.h:43
void putTime(Http::HdrType id, time_t htime)
Definition: HttpHeader.cc:1042
struct SquidConfig::@112 accessList
int neighbors_do_private_keys
@ ERR_ZERO_SIZE_OBJECT
Definition: forward.h:46
HttpUpgradeProtocolAccess * http_upgrade_request_protocols
http_upgrade_request_protocols
Definition: SquidConfig.h:486
#define assert(EX)
Definition: assert.h:19
@ scUriTooLong
Definition: StatusCode.h:58
char const * termedBuf() const
Definition: SquidString.h:92
struct SquidConfig::@104 Accel
void setPayloadBuffer(MemBuf *parsedContent)
set the buffer to be used to store decoded chunk data
@ scContinue
Definition: StatusCode.h:22
HttpHdrCc * cache_control
Definition: Message.h:77
FwdState::Pointer fwd
Definition: Client.h:169
HierarchyLogEntry hier
Definition: HttpRequest.h:157
acl_access * brokenPosts
Definition: SquidConfig.h:382
void HTTPMSGLOCK(Http::Message *a)
Definition: Message.h:161
virtual void syncAle(HttpRequest *adaptedRequest, const char *logUri) const
assigns uninitialized adapted_request and url ALE components
void neighborsHtcpClear(StoreEntry *e, HttpRequest *req, const HttpRequestMethod &method, htcp_clr_reason reason)
Definition: neighbors.cc:1816
std::ostream & CurrentException(std::ostream &os)
prints active (i.e., thrown but not yet handled) exception
virtual HttpRequestPointer originalRequest()
a hack to reach HttpStateData::orignal_request
Definition: Client.cc:571
Comm::Flag flag
comm layer result status.
Definition: CommCalls.h:87
const SBuf & remaining() const
the remaining unprocessed section of buffer
Definition: Parser.h:98
void abortTransaction(const char *reason)
Definition: http.h:113
@ TRANSFER_ENCODING
bool no_delay
Definition: CachePeer.h:101
@ scServiceUnavailable
Definition: StatusCode.h:74
@ METHOD_TRACE
Definition: MethodType.h:30
#define cbdataReferenceDone(var)
Definition: cbdata.h:350
bool adaptationAccessCheckPending
Definition: Client.h:181
void onlyIfCached(bool v)
Definition: HttpHdrCc.h:146
void writeReplyBody()
Definition: http.cc:1410
#define JobCallback(dbgSection, dbgLevel, Dialer, job, method)
Convenience macro to create a Dialer-based job callback.
Definition: AsyncJobCalls.h:69
@ scInternalServerError
Definition: StatusCode.h:71
HttpHdrRange * range
Definition: HttpRequest.h:143
HttpReply * setVirginReply(HttpReply *r)
Definition: Client.cc:117
void httpStateConnClosed(const CommCloseCbParams &params)
Definition: http.cc:143
void packInto(Packable *p, bool mask_sensitive_info=false) const
Definition: HttpHeader.cc:567
bool sentLastChunk
do not try to write last-chunk again
Definition: StateFlags.h:64
bool cacheNegatively()
Definition: store.cc:186
const char * c_str()
Definition: SBuf.cc:516
void Write(const Comm::ConnectionPointer &conn, const char *buf, int size, AsyncCall::Pointer &callback, FREE *free_func)
Definition: Write.cc:34
int commUnsetConnTimeout(const Comm::ConnectionPointer &conn)
Definition: comm.cc:590
virtual const Comm::ConnectionPointer & dataConnection() const
Definition: http.cc:137
bool olderThan(const HttpReply *them) const
Definition: HttpReply.cc:664
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:565
void base64_encode_init(struct base64_encode_ctx *ctx)
Definition: base64.c:232
bool receivedWholeRequestBody
handleRequestBodyProductionEnded called
Definition: Client.h:187
#define CBDATA_CLASS_INIT(type)
Definition: cbdata.h:318
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:408
time_t squid_curtime
Definition: stub_time.cc:17
size_t bytesWanted(Range< size_t > const aRange, bool ignoreDelayPool=false) const
Definition: store.cc:249
SBuf & append(const SBuf &S)
Definition: SBuf.cc:185
bool isNoAddr() const
Definition: Address.cc:284
int64_t content_length
Definition: Message.h:84
time_t date
Definition: HttpReply.h:40
HttpReply * finalReply()
Definition: Client.cc:130
void putCc(const HttpHdrCc *cc)
Definition: HttpHeader.cc:1067
void fd_bytes(int fd, int len, unsigned int type)
Definition: fd.cc:227
bool parseHeader(Http1::Parser &hp)
parses reply header using Parser
Definition: HttpReply.cc:467
@ scNotModified
Definition: StatusCode.h:40
ReuseDecision::Answers reusableReply(ReuseDecision &decision)
Definition: http.cc:301
void markParsedVirginReplyAsWhole(const char *reasonWeAreSure)
Definition: Client.cc:159
uint32_t sources
The message sources.
Definition: Message.h:100
const Acl::Tree * changeAcl(const Acl::Tree *t)
Definition: Checklist.h:176
bool mustKeepalive
Definition: RequestFlags.h:74
Http1::TeChunkedParser * httpChunkDecoder
Definition: http.h:150
void wroteLast(const CommIoCbParams &io)
called after writing the very last request byte (body, last-chunk, etc)
Definition: http.cc:1646
#define fd_table
Definition: fde.h:189
Http1::ResponseParserPointer hp
Parser being used at present to parse the HTTP/ICY server response.
Definition: http.h:149
@ ERR_READ_TIMEOUT
Definition: forward.h:26
assists in making and relaying entry caching/sharing decision
Definition: http.h:30
@ scFound
Definition: StatusCode.h:38
virtual bool parse(const SBuf &)
@ PROTO_HTTPS
Definition: ProtocolType.h:27
size_t calcBufferSpaceToReserve(const size_t space, const size_t wantSpace) const
determine how much space the buffer needs to reserve
Definition: Client.cc:1055
@ scRequestTimeout
Definition: StatusCode.h:52
bool accelerated
Definition: RequestFlags.h:58
void maxAge(int v)
HttpRequestMethod method
Definition: HttpRequest.h:114
void path(const char *p)
Definition: Uri.h:99
const char * reason
the decision reason
Definition: http.h:39
@ scNotFound
Definition: StatusCode.h:48
acl_access * reply
Definition: SquidConfig.h:386
noteTakeServerConnectionControl() callback parameter
Definition: client_side.h:214
@ PROTO_HTTP
Definition: ProtocolType.h:25
char * host
Definition: CachePeer.h:40
int eof
Definition: http.h:65
bool allowed() const
Definition: Acl.h:143
int cache_miss_revalidate
Definition: SquidConfig.h:326
bool sawDateGoBack
Definition: http.h:159
void addVirginReplyBody(const char *buf, ssize_t len)
Definition: Client.cc:1030
int server_pconns
Definition: SquidConfig.h:312
struct SquidConfig::@111 onoff
AnyP::ProtocolVersion version
breakdown of protocol version label: (HTTP/ICY) and (0.9/1.0/1.1)
Definition: StatusLine.h:62
const char * doneWithFwd
Definition: Client.h:191
void delayRead(DeferredRead const &)
Definition: MemObject.cc:441
virtual void sentRequestBody(const CommIoCbParams &io)=0
Definition: Client.cc:365
bool peerSupportsConnectionPinning() const
Definition: http.cc:886
bool timestampsSet()
Definition: store.cc:1410
@ scNoContent
Definition: StatusCode.h:30
@ PROXY_AUTHENTICATE
@ ERR_TOO_BIG
Definition: forward.h:40
bool forcedBodyContinuation
whether we have responded with HTTP 100 or FTP 150 already
Definition: HttpRequest.h:194
virtual void maybeReadVirginBody()
read response data from the network
Definition: http.cc:1583
int has(Http::HdrType id) const
Definition: HttpHeader.cc:990
void processReply()
Definition: http.cc:1281
char * content()
start of the added data
Definition: MemBuf.h:41
#define acl_access
Definition: forward.h:45
bool hasMaxAge(int32_t *val=nullptr) const
Definition: HttpHdrCc.h:122
void putStr(Http::HdrType id, const char *str)
Definition: HttpHeader.cc:1051
@ ERR_WRITE_ERROR
Definition: forward.h:29
void set(const AnyP::ProtocolVersion &newVersion, Http::StatusCode newStatus, const char *newReason=NULL)
Definition: StatusLine.cc:35
bool finishingChunkedRequest()
if needed, write last-chunk to end the request body and return true
Definition: http.cc:2521
size_type size() const
Definition: SquidString.h:73
char * url
Definition: tcp-banger2.c:114
bool isEmpty() const
Definition: Store.h:67
bool refreshIsCachable(const StoreEntry *entry)
Definition: refresh.cc:529
bool reforwardableStatus(const Http::StatusCode s) const
Definition: FwdState.cc:1404
#define Must(condition)
Like assert() but throws an exception instead of aborting the process.
Definition: TextException.h:73
int redir_rewrites_host
Definition: SquidConfig.h:304
@ SURROGATE_CAPABILITY
@ scProxyAuthenticationRequired
Definition: StatusCode.h:51
AsyncCall * asyncCall(int aDebugSection, int aDebugLevel, const char *aName, const Dialer &aDialer)
Definition: AsyncCall.h:156
void Controller::create() STUB void Controller Controller nil
size_t base64_encode_update(struct base64_encode_ctx *ctx, char *dst, size_t length, const uint8_t *src)
Definition: base64.c:265
char * surrogate_id
Definition: SquidConfig.h:218
StoreEntry * entry
Definition: FwdState.h:169
StoreEntry * storeGetPublic(const char *uri, const HttpRequestMethod &method)
Definition: store.cc:518
void release(const bool shareable=false)
Definition: store.cc:1161
virtual void start()
called by AsyncStart; do not call directly
Definition: http.cc:2466
const HttpReply & freshestReply() const
Definition: MemObject.h:67
ssize_t mb_size_t
Definition: MemBuf.h:17
HttpHdrSc * surrogate_control
Definition: HttpReply.h:48
@ scInvalidHeader
Definition: StatusCode.h:86
@ scPaymentRequired
Definition: StatusCode.h:46
@ scLocked
Definition: StatusCode.h:64
static StoreEntry * findPreviouslyCachedEntry(StoreEntry *newEntry)
Definition: http.cc:164
@ scConflict
Definition: StatusCode.h:53
int n_keepalives_recv
Definition: CachePeer.h:52
#define REFRESH_OVERRIDE(flag)
@ FD_WRITE
Definition: enums.h:24
static void httpBuildRequestHeader(HttpRequest *request, StoreEntry *entry, const AccessLogEntryPointer &al, HttpHeader *hdr_out, const Http::StateFlags &flags)
Definition: http.cc:1835
@ srcHttps
https_port or bumped http_port tunnel; HTTPS server
Definition: Message.h:33
virtual void sentRequestBody(const CommIoCbParams &io)
Definition: http.cc:2606
int64_t readAheadGap
Definition: SquidConfig.h:96
void setNoDelay(bool const)
Definition: store.cc:273
bool hasNoCacheWithoutParameters() const
Definition: HttpHdrCc.h:88
bool hasMustRevalidate() const
Definition: HttpHdrCc.h:112
void processSurrogateControl(HttpReply *)
Definition: http.cc:263
int64_t payloadSeen
amount of message payload/body received so far.
Definition: http.h:153
@ ENTRY_REVALIDATE_STALE
Definition: enums.h:100
bool continueAfterParsingHeader()
Definition: http.cc:1312
static void readDelayed(void *context, CommRead const &)
Definition: http.cc:1175
@ scMultipleChoices
Definition: StatusCode.h:36
@ ENTRY_REVALIDATE_ALWAYS
Definition: enums.h:85
bool serverSwitchedProtocols
Definition: StateFlags.h:35
@ scOkay
Definition: StatusCode.h:26
std::ostream & operator<<(std::ostream &os, const HttpStateData::ReuseDecision &d)
Definition: http.cc:2633
HttpHeaderEntry * getEntry(HttpHeaderPos *pos) const
Definition: HttpHeader.cc:611
@ scHeaderTooLarge
Definition: StatusCode.h:87
#define rfc1738_escape_part(x)
Definition: rfc1738.h:55
time_t lifetime
Definition: SquidConfig.h:112
virtual void closeServer()
Definition: http.cc:1702
static StatHist s
#define false
Definition: GnuRegex.c:233
const SBuf & effectiveRequestUri() const
RFC 7230 section 5.5 - Effective Request URI.
Definition: HttpRequest.cc:752
static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest *request, HttpHeader *hdr_out, const int we_do_ranges, const Http::StateFlags &)
Definition: http.cc:2086
void addVia(const AnyP::ProtocolVersion &ver, const HttpHeader *from=0)
Definition: HttpHeader.cc:998
HttpRequest * request
Definition: FwdState.h:170
Ip::Address client_addr
Definition: HttpRequest.h:149
bool hasNoStore() const
Definition: HttpHdrCc.h:102
void host(const char *src)
Definition: Uri.cc:98
void forwardUpgrade(HttpHeader &)
Definition: http.cc:2012
@ PEER_SIBLING
Definition: enums.h:29
@ scExpectationFailed
Definition: StatusCode.h:61
HttpRequestPointer request
Definition: Client.h:170
int read_hist[histSize]
Definition: IoStats.h:21
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
virtual void noteTakeServerConnectionControl(ServerConnectionContext)
Definition: client_side.h:230
short int keep_alive
Definition: HttpReply.h:53
@ hoRequest
Definition: HttpHeader.h:36
HttpRequestPointer request
Definition: MemObject.h:188
void clean()
Definition: HttpHeader.cc:189
void comm_remove_close_handler(int fd, CLCB *handler, void *data)
Definition: comm.cc:949
Comm::ConnectionPointer const & serverConnection() const
Definition: FwdState.h:106
const char * getMD5Text() const
Definition: store.cc:205
~HttpStateData()
Definition: http.cc:120
bool originserver
Definition: CachePeer.h:117
time_t negativeTtl
Definition: SquidConfig.h:100
HttpHdrCc * getCc() const
Definition: HttpHeader.cc:1222
#define SQUIDSBUFPH
Definition: SBuf.h:31
void putInt64(Http::HdrType id, int64_t number)
Definition: HttpHeader.cc:1033
HttpHeaderEntry * clone() const
Definition: HttpHeader.cc:1534
CbcPointer< ConnStateData > clientConnectionManager
Definition: HttpRequest.h:232
bool hasMaxAge() const
bool hasPublic() const
Definition: HttpHdrCc.h:69
class SquidConfig Config
Definition: SquidConfig.cc:12
void httpStart(FwdState *fwd)
Definition: http.cc:2459
int strListGetItem(const String *str, char del, const char **item, int *ilen, const char **pos)
Definition: StrList.cc:86
ErrorDetail::Pointer MakeNamedErrorDetail(const char *name)
Definition: Detail.cc:54
StatCounters statCounter
Definition: StatCounters.cc:12
void removeStaleWarnings()
Remove Warnings with warn-date different from Date value.
Definition: HttpReply.cc:599
void clean()
Definition: String.cc:120
static void Start(const Pointer &job)
Definition: AsyncJob.cc:24
AnyP::ProtocolVersion ProtocolVersion(unsigned int aMajor, unsigned int aMinor)
HTTP version label information.
@ PROXY_AUTHORIZATION
void maxStale(int v)
#define DBG_DATA
Definition: Debug.h:43
#define CallJobHere1(debugSection, debugLevel, job, Class, method, arg1)
Definition: AsyncJobCalls.h:63
@ STORE_PENDING
Definition: enums.h:51

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors