]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http.cc
Source Format Enforcement (#963)
[thirdparty/squid.git] / src / http.cc
1 /*
2 * Copyright (C) 1996-2022 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"
33 #include "http/one/ResponseParser.h"
34 #include "http/one/TeChunkedParser.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"
44 #include "HttpUpgradeProtocolAccess.h"
45 #include "log/access_log.h"
46 #include "MemBuf.h"
47 #include "MemObject.h"
48 #include "neighbors.h"
49 #include "pconn.h"
50 #include "peer_proxy_negotiate_auth.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
69 CBDATA_CLASS_INIT(HttpStateData);
70
71 static const char *const crlf = "\r\n";
72
73 static void httpMaybeRemovePublic(StoreEntry *, Http::StatusCode);
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
77 HttpStateData::HttpStateData(FwdState *theFwdState) :
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, "HttpStateData " << this << " created");
87 ignoreCacheControl = false;
88 surrogateNoStore = false;
89 serverConnection = fwd->serverConnection();
90
91 if (fwd->serverConnection() != NULL)
92 _peer = cbdataReference(fwd->serverConnection()->getPeer()); /* might be NULL */
93
94 flags.peering = _peer;
95 flags.tunneling = (_peer && request->flags.sslBumped);
96 flags.toOrigin = (!_peer || _peer->options.originserver || request->flags.sslBumped);
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 */
104 if (!flags.tunneling && _peer->options.proxy_only)
105 entry->releaseRequest(true);
106
107 #if USE_DELAY_POOLS
108 entry->setNoDelay(_peer->options.no_delay);
109 #endif
110 }
111
112 /*
113 * register the handler to free HTTP state data when the FD closes
114 */
115 typedef CommCbMemFunT<HttpStateData, CommCloseCbParams> Dialer;
116 closeHandler = JobCallback(9, 5, Dialer, this, HttpStateData::httpStateConnClosed);
117 comm_add_close_handler(serverConnection->fd, closeHandler);
118 }
119
120 HttpStateData::~HttpStateData()
121 {
122 /*
123 * don't forget that ~Client() gets called automatically
124 */
125
126 if (httpChunkDecoder)
127 delete httpChunkDecoder;
128
129 cbdataReferenceDone(_peer);
130
131 delete upgradeHeaderOut;
132
133 debugs(11,5, "HttpStateData " << this << " destroyed; " << serverConnection);
134 }
135
136 const Comm::ConnectionPointer &
137 HttpStateData::dataConnection() const
138 {
139 return serverConnection;
140 }
141
142 void
143 HttpStateData::httpStateConnClosed(const CommCloseCbParams &params)
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
151 HttpStateData::httpTimeout(const CommTimeoutCbParams &)
152 {
153 debugs(11, 4, serverConnection << ": '" << entry->url() << "'");
154
155 if (entry->store_status == STORE_PENDING) {
156 fwd->fail(new ErrorState(ERR_READ_TIMEOUT, Http::scGatewayTimeout, fwd->request, fwd->al));
157 }
158
159 closeServer();
160 mustStop("HttpStateData::httpTimeout");
161 }
162
163 static StoreEntry *
164 findPreviouslyCachedEntry(StoreEntry *newEntry) {
165 assert(newEntry->mem_obj);
166 return newEntry->mem_obj->request ?
167 storeGetPublicByRequest(newEntry->mem_obj->request.getRaw()) :
168 storeGetPublic(newEntry->mem_obj->storeId(), newEntry->mem_obj->method);
169 }
170
171 /// Remove an existing public store entry if the incoming response (to be
172 /// stored in a currently private entry) is going to invalidate it.
173 static void
174 httpMaybeRemovePublic(StoreEntry * e, Http::StatusCode status)
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
194 case Http::scNonAuthoritativeInformation:
195
196 case Http::scMultipleChoices:
197
198 case Http::scMovedPermanently:
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
213 case Http::scMethodNotAllowed:
214 forbidden = 1;
215
216 break;
217
218 #if WORK_IN_PROGRESS
219
220 case Http::scUnauthorized:
221 forbidden = 1;
222
223 break;
224
225 #endif
226
227 default:
228 break;
229 }
230
231 if (!remove && !forbidden)
232 return;
233
234 StoreEntry *pe = findPreviouslyCachedEntry(e);
235
236 if (pe != NULL) {
237 assert(e != pe);
238 #if USE_HTCP
239 neighborsHtcpClear(e, e->mem_obj->request.getRaw(), e->mem_obj->method, HTCP_CLR_INVALIDATION);
240 #endif
241 pe->release(true);
242 }
243
244 /** \par
245 * Also remove any cached HEAD response in case the object has
246 * changed.
247 */
248 if (e->mem_obj->request)
249 pe = storeGetPublicByRequestMethod(e->mem_obj->request.getRaw(), Http::METHOD_HEAD);
250 else
251 pe = storeGetPublic(e->mem_obj->storeId(), Http::METHOD_HEAD);
252
253 if (pe != NULL) {
254 assert(e != pe);
255 #if USE_HTCP
256 neighborsHtcpClear(e, e->mem_obj->request.getRaw(), HttpRequestMethod(Http::METHOD_HEAD), HTCP_CLR_INVALIDATION);
257 #endif
258 pe->release(true);
259 }
260 }
261
262 void
263 HttpStateData::processSurrogateControl(HttpReply *reply)
264 {
265 if (request->flags.accelerated && reply->surrogate_control) {
266 HttpHdrScTarget *sctusable = reply->surrogate_control->getMergedTarget(Config.Accel.surrogate_id);
267
268 if (sctusable) {
269 if (sctusable->hasNoStore() ||
270 (Config.onoff.surrogate_is_remote
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
300 HttpStateData::ReuseDecision::Answers
301 HttpStateData::reusableReply(HttpStateData::ReuseDecision &decision)
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
322 if (EBIT_TEST(entry->flags, RELEASE_REQUEST))
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
343 if (request && request->cache_control && request->cache_control->hasNoStore() &&
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.
349 if (rep->cache_control && rep->cache_control->hasNoCacheWithParameters()) {
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, "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, "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, "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, "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";
440 ReuseDecision::Answers statusAnswer = ReuseDecision::reuseNot;
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
458 case Http::scNonAuthoritativeInformation:
459
460 case Http::scMultipleChoices:
461
462 case Http::scMovedPermanently:
463 case Http::scPermanentRedirect:
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:
480 case Http::scTemporaryRedirect:
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:
494 case Http::scMethodNotAllowed:
495 case Http::scUriTooLong:
496 case Http::scInternalServerError:
497 case Http::scNotImplemented:
498 case Http::scBadGateway:
499 case Http::scServiceUnavailable:
500 case Http::scGatewayTimeout:
501 case Http::scMisdirectedRequest:
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:
519 case Http::scUnauthorized:
520 case Http::scProxyAuthenticationRequired:
521 case Http::scPaymentRequired:
522 case Http::scInsufficientStorage:
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 */
528 case Http::scNotAcceptable:
529 case Http::scRequestTimeout: // TODO: is this shareable?
530 case Http::scConflict: // TODO: is this shareable?
531 case Http::scLengthRequired:
532 case Http::scPreconditionFailed:
533 case Http::scPayloadTooLarge:
534 case Http::scUnsupportedMediaType:
535 case Http::scUnprocessableEntity:
536 case Http::scLocked: // TODO: is this shareable?
537 case Http::scFailedDependency:
538 case Http::scRequestedRangeNotSatisfied:
539 case Http::scExpectationFailed:
540 case Http::scInvalidHeader: /* Squid header parsing error */
541 case Http::scHeaderTooLarge:
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
554 /// assemble a variant key (vary-mark) from the given Vary header and HTTP request
555 static void
556 assembleVaryKey(String &vary, SBuf &vstr, const HttpRequest &request)
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
592 httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
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();
602 vary = reply->header.getList(Http::HdrType::HDR_X_ACCELERATOR_VARY);
603 assembleVaryKey(vary, vstr, *request);
604 #endif
605
606 debugs(11, 3, vstr);
607 return vstr;
608 }
609
610 void
611 HttpStateData::keepaliveAccounting(HttpReply *reply)
612 {
613 if (flags.keepalive)
614 if (flags.peering && !flags.tunneling)
615 ++ _peer->stats.n_keepalives_sent;
616
617 if (reply->keep_alive) {
618 if (flags.peering && !flags.tunneling)
619 ++ _peer->stats.n_keepalives_recv;
620
621 if (Config.onoff.detect_broken_server_pconns
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
631 HttpStateData::checkDateSkew(HttpReply *reply)
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
641 /**
642 * This creates the error page itself.. its likely
643 * that the forward ported reply header max size patch
644 * generates non http conformant error pages - in which
645 * case the errors where should be 'BAD_GATEWAY' etc
646 */
647 void
648 HttpStateData::processReplyHeader()
649 {
650 /** Creates a blank header. If this routine is made incremental, this will not do */
651
652 debugs(11, 3, "processReplyHeader: key '" << entry->getMD5Text() << "'");
653
654 assert(!flags.headers_parsed);
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)
662 hp = new Http1::ResponseParser;
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
706 payloadSeen = inBuf.length();
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
723 newrep->sources |= request->url.getScheme() == AnyP::PROTO_HTTPS ? Http::Message::srcHttps : Http::Message::srcHttp;
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;
735 httpChunkDecoder = new Http1::TeChunkedParser;
736 }
737
738 if (!peerSupportsConnectionPinning())
739 request->flags.connectionAuthDisabled = true;
740
741 HttpReply *vrep = setVirginReply(newrep);
742 flags.headers_parsed = true;
743
744 keepaliveAccounting(vrep);
745
746 checkDateSkew(vrep);
747
748 processSurrogateControl (vrep);
749 }
750
751 /// ignore or start forwarding the 1xx response (a.k.a., control message)
752 void
753 HttpStateData::handle1xx(HttpReply *reply)
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
761 Must(!flags.handling1xx);
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
768 Must(!flags.serverSwitchedProtocols);
769 flags.serverSwitchedProtocols = (statusCode == Http::scSwitchingProtocols);
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) {
780 ACLFilledChecklist ch(Config.accessList.reply, originalRequest().getRaw());
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
790 if (flags.serverSwitchedProtocols) {
791 if (const auto reason = blockSwitchingProtocols(*reply))
792 return drop1xx(reason);
793 }
794
795 debugs(11, 2, "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,
800 HttpStateData::proceedAfter1xx);
801 CallJobHere1(11, 4, request->clientConnectionManager, ConnStateData,
802 ConnStateData::sendControlMsg, HttpControlMsg(msg, cb));
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
809 /// if possible, safely ignores the received 1xx control message
810 /// otherwise, terminates the server connection
811 void
812 HttpStateData::drop1xx(const char *reason)
813 {
814 if (flags.serverSwitchedProtocols) {
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
827 /// \retval nil if the HTTP/101 (Switching Protocols) reply should be forwarded
828 /// \retval reason why an attempt to switch protocols should be stopped
829 const char *
830 HttpStateData::blockSwitchingProtocols(const HttpReply &reply) const
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
855 /// restores state and resumes processing after 1xx is ignored or forwarded
856 void
857 HttpStateData::proceedAfter1xx()
858 {
859 Must(flags.handling1xx);
860
861 if (flags.serverSwitchedProtocols) {
862 // pass server connection ownership to request->clientConnectionManager
863 ConnStateData::ServerConnectionContext scc(serverConnection, inBuf);
864 typedef UnaryMemFunT<ConnStateData, ConnStateData::ServerConnectionContext> MyDialer;
865 AsyncCall::Pointer call = asyncCall(11, 3, "ConnStateData::noteTakeServerConnectionControl",
866 MyDialer(request->clientConnectionManager,
867 &ConnStateData::noteTakeServerConnectionControl, scc));
868 ScheduleCallHere(call);
869 fwd->unregister(serverConnection);
870 comm_remove_close_handler(serverConnection->fd, closeHandler);
871 closeHandler = nullptr;
872 serverConnection = nullptr;
873 doneWithFwd = "switched protocols";
874 mustStop(doneWithFwd);
875 return;
876 }
877
878 debugs(11, 2, "continuing with " << payloadSeen << " bytes in buffer after 1xx");
879 CallJobHere(11, 3, this, HttpStateData, HttpStateData::processReply);
880 }
881
882 /**
883 * returns true if the peer can support connection pinning
884 */
885 bool
886 HttpStateData::peerSupportsConnectionPinning() const
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 */
924 if (_peer->options.originserver)
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
944 HttpStateData::haveParsedReplyHeaders()
945 {
946 Client::haveParsedReplyHeaders();
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
962 if (neighbors_do_private_keys && !sawDateGoBack)
963 httpMaybeRemovePublic(entry, rep->sline.status());
964
965 bool varyFailure = false;
966 if (rep->header.has(Http::HdrType::VARY)
967 #if X_ACCELERATOR_VARY
968 || rep->header.has(Http::HdrType::HDR_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()))
978 EBIT_SET(entry->flags, ENTRY_FWD_HDR_WAIT);
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)
987 EBIT_SET(entry->flags, ENTRY_REVALIDATE_ALWAYS);
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()))
997 EBIT_SET(entry->flags, ENTRY_FWD_HDR_WAIT);
998
999 ReuseDecision decision(entry, statusCode);
1000
1001 switch (reusableReply(decision)) {
1002
1003 case ReuseDecision::reuseNot:
1004 entry->makePrivate(false);
1005 break;
1006
1007 case ReuseDecision::cachePositively:
1008 if (!entry->makePublic()) {
1009 decision.make(ReuseDecision::doNotCacheButShare, "public key creation error");
1010 entry->makePrivate(true);
1011 }
1012 break;
1013
1014 case ReuseDecision::cacheNegatively:
1015 if (!entry->cacheNegatively()) {
1016 decision.make(ReuseDecision::doNotCacheButShare, "public key creation error");
1017 entry->makePrivate(true);
1018 }
1019 break;
1020
1021 case ReuseDecision::doNotCacheButShare:
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)
1050 EBIT_SET(entry->flags, ENTRY_REVALIDATE_ALWAYS);
1051 else if (ccMustRevalidate || ccSMaxAge)
1052 EBIT_SET(entry->flags, ENTRY_REVALIDATE_STALE);
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",','))
1062 EBIT_SET(entry->flags, ENTRY_REVALIDATE_ALWAYS);
1063 }
1064 #endif
1065 }
1066
1067 #if HEADERS_LOG
1068 headersLog(1, 0, request->method, rep);
1069
1070 #endif
1071 }
1072
1073 HttpStateData::ConnectionStatus
1074 HttpStateData::statusIfComplete() const
1075 {
1076 const HttpReply *rep = virginReply();
1077 /** \par
1078 * If the reply wants to close the connection, it takes precedence */
1079
1080 static SBuf close("close", 5);
1081 if (httpHeaderHasConnDir(&rep->header, close))
1082 return COMPLETE_NONPERSISTENT_MSG;
1083
1084 /** \par
1085 * If we sent a Connection:close request header, then this
1086 * can not be a persistent connection.
1087 */
1088 if (!flags.keepalive)
1089 return COMPLETE_NONPERSISTENT_MSG;
1090
1091 /** \par
1092 * If we banned reuse, then this cannot be a persistent connection.
1093 */
1094 if (flags.forceClose)
1095 return COMPLETE_NONPERSISTENT_MSG;
1096
1097 /** \par
1098 * If we haven't sent the whole request then this can not be a persistent
1099 * connection.
1100 */
1101 if (!flags.request_sent) {
1102 debugs(11, 2, "Request not yet fully sent " << request->method << ' ' << entry->url());
1103 return COMPLETE_NONPERSISTENT_MSG;
1104 }
1105
1106 /** \par
1107 * What does the reply have to say about keep-alive?
1108 */
1109 /**
1110 \bug XXX BUG?
1111 * If the origin server (HTTP/1.0) does not send a keep-alive
1112 * header, but keeps the connection open anyway, what happens?
1113 * We'll return here and http.c waits for an EOF before changing
1114 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
1115 * and an error status code, and we might have to wait until
1116 * the server times out the socket.
1117 */
1118 if (!rep->keep_alive)
1119 return COMPLETE_NONPERSISTENT_MSG;
1120
1121 return COMPLETE_PERSISTENT_MSG;
1122 }
1123
1124 HttpStateData::ConnectionStatus
1125 HttpStateData::persistentConnStatus() const
1126 {
1127 debugs(11, 3, serverConnection << " eof=" << eof);
1128 if (eof) // already reached EOF
1129 return COMPLETE_NONPERSISTENT_MSG;
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/
1135 if (!Comm::IsConnOpen(serverConnection))
1136 return COMPLETE_NONPERSISTENT_MSG;
1137
1138 /** \par
1139 * In chunked response we do not know the content length but we are absolutely
1140 * sure about the end of response, so we are calling the statusIfComplete to
1141 * decide if we can be persistent
1142 */
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
1157 /** \par
1158 * If the body size is known, we must wait until we've gotten all of it. */
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
1169 /** \par
1170 * If there is no message body or we got it all, we can be persistent */
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
1183 HttpStateData::readReply(const CommIoCbParams &io)
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
1196 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1197 abortTransaction("store entry aborted while reading reply");
1198 return;
1199 }
1200
1201 Must(Comm::IsConnOpen(serverConnection));
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
1211 Must(maybeMakeSpaceAvailable(true));
1212 CommIoCbParams rd(this); // will be expanded with ReadNow results
1213 rd.conn = io.conn;
1214 rd.size = entry->bytesWanted(Range<size_t>(0, inBuf.spaceSize()));
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;
1228 maybeReadVirginBody();
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
1249 request->hier.notePeerRead();
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
1278 /// processes the already read and buffered response data, possibly after
1279 /// waiting for asynchronous 1xx control message processing
1280 void
1281 HttpStateData::processReply()
1282 {
1283
1284 if (flags.handling1xx) { // we came back after handling a 1xx response
1285 debugs(11, 5, "done with 1xx handling");
1286 flags.handling1xx = false;
1287 Must(!flags.headers_parsed);
1288 }
1289
1290 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
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?
1296 processReplyHeader();
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
1308 /**
1309 \retval true if we can continue with processing the body or doing ICAP.
1310 */
1311 bool
1312 HttpStateData::continueAfterParsingHeader()
1313 {
1314 if (flags.handling1xx) {
1315 debugs(11, 5, "wait for 1xx handling");
1316 Must(!flags.headers_parsed);
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;
1323 /** \retval false If we have not finished parsing the headers and may get more data.
1324 * Schedules more reads to retrieve the missing data.
1325 */
1326 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1327 return false;
1328 }
1329
1330 /** If we are done with parsing, check for errors */
1331
1332 err_type error = ERR_NONE;
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);
1341 error = ERR_INVALID_RESP;
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);
1347 error = ERR_INVALID_RESP;
1348 } else if (vrep->header.unsupportedTe()) {
1349 fwd->dontRetry(true);
1350 error = ERR_INVALID_RESP;
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);
1357 error = ERR_INVALID_RESP;
1358 }
1359 } else {
1360 assert(eof);
1361 if (inBuf.length()) {
1362 error = ERR_INVALID_RESP;
1363 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry->url() << " AKA " << request->url);
1364 } else {
1365 error = ERR_ZERO_SIZE_OBJECT;
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();
1372 fwd->fail(new ErrorState(error, Http::scBadGateway, fwd->request, fwd->al));
1373 flags.do_next_read = false;
1374 closeServer();
1375 mustStop("HttpStateData::continueAfterParsingHeader");
1376 return false; // quit on error
1377 }
1378
1379 /** truncate what we read if we read too much so that writeReplyBody()
1380 writes no more than what we should have read */
1381 void
1382 HttpStateData::truncateVirginBody()
1383 {
1384 assert(flags.headers_parsed);
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
1405 /**
1406 * Call this when there is data from the origin server
1407 * which should be sent to either StoreEntry, or to ICAP...
1408 */
1409 void
1410 HttpStateData::writeReplyBody()
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
1433 HttpStateData::decodeAndWriteReplyBody()
1434 {
1435 assert(flags.chunked);
1436 assert(httpChunkDecoder);
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
1457 /**
1458 * processReplyBody has two purposes:
1459 * 1 - take the reply body data, if any, and put it into either
1460 * the StoreEntry, or give it over to ICAP.
1461 * 2 - see if we made it to the end of the response (persistent
1462 * connections and such)
1463 */
1464 void
1465 HttpStateData::processReplyBody()
1466 {
1467 if (!flags.headers_parsed) {
1468 flags.do_next_read = true;
1469 maybeReadVirginBody();
1470 return;
1471 }
1472
1473 #if USE_ADAPTATION
1474 debugs(11,5, "adaptationAccessCheckPending=" << adaptationAccessCheckPending);
1475 if (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.
1498 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
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 */
1508 AsyncCall::Pointer nil;
1509 if (flags.keepalive_broken) {
1510 commSetConnTimeout(serverConnection, 10, nil);
1511 } else {
1512 commSetConnTimeout(serverConnection, Config.Timeout.read, nil);
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
1524 commUnsetConnTimeout(serverConnection);
1525 flags.do_next_read = false;
1526
1527 comm_remove_close_handler(serverConnection->fd, closeHandler);
1528 closeHandler = NULL;
1529
1530 Ip::Address client_addr; // XXX: Remove as unused. Why was it added?
1531 if (request->flags.spoofClientIp)
1532 client_addr = request->client_addr;
1533
1534 auto serverConnectionSaved = serverConnection;
1535 fwd->unregister(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) {
1546 if (request->clientConnectionManager.valid()) {
1547 CallJobHere1(11, 4, request->clientConnectionManager,
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
1563 case COMPLETE_NONPERSISTENT_MSG:
1564 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG from " << serverConnection);
1565
1566 serverComplete();
1567 return;
1568 }
1569
1570 maybeReadVirginBody();
1571 }
1572
1573 bool
1574 HttpStateData::mayReadVirginReplyBody() const
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
1583 HttpStateData::maybeReadVirginBody()
1584 {
1585 // too late to read
1586 if (!Comm::IsConnOpen(serverConnection) || fd_table[serverConnection->fd].closing())
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) ...
1600 assert(!Comm::MonitorsRead(serverConnection->fd));
1601
1602 // wait for read(2) to be possible.
1603 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
1604 AsyncCall::Pointer call = JobCallback(11, 5, Dialer, this, HttpStateData::readReply);
1605 Comm::Read(serverConnection, call);
1606 }
1607
1608 bool
1609 HttpStateData::maybeMakeSpaceAvailable(bool doGrow)
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
1644 /// called after writing the very last request byte (body, last-chunk, etc)
1645 void
1646 HttpStateData::wroteLast(const CommIoCbParams &io)
1647 {
1648 debugs(11, 5, 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
1667 request->hier.notePeerWrite();
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
1681 /// successfully wrote the entire request (including body, last-chunk, etc.)
1682 void
1683 HttpStateData::sendComplete()
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 */
1693 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
1694 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
1695 TimeoutDialer, this, HttpStateData::httpTimeout);
1696
1697 commSetConnTimeout(serverConnection, Config.Timeout.read, timeoutCall);
1698 flags.request_sent = true;
1699 }
1700
1701 void
1702 HttpStateData::closeServer()
1703 {
1704 debugs(11,5, "closing HTTP server " << serverConnection << " this " << this);
1705
1706 if (Comm::IsConnOpen(serverConnection)) {
1707 fwd->unregister(serverConnection);
1708 comm_remove_close_handler(serverConnection->fd, closeHandler);
1709 closeHandler = NULL;
1710 serverConnection->close();
1711 }
1712 }
1713
1714 bool
1715 HttpStateData::doneWithServer() const
1716 {
1717 return !Comm::IsConnOpen(serverConnection);
1718 }
1719
1720 /*
1721 * Fixup authentication request headers for special cases
1722 */
1723 static void
1724 httpFixupAuthentication(HttpRequest * request, const HttpHeader * hdr_in, HttpHeader * hdr_out, const Http::StateFlags &flags)
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
1738 const auto header = flags.toOrigin ? Http::HdrType::AUTHORIZATION : Http::HdrType::PROXY_AUTHORIZATION;
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 &&
1753 hdr_in->has(Http::HdrType::PROXY_AUTHORIZATION)) {
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
1835 HttpStateData::httpBuildRequestHeader(HttpRequest * request,
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;
1847 HttpHeaderPos pos = HttpHeaderInitPos;
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)
1852 hdr_out->putTime(Http::HdrType::IF_MODIFIED_SINCE, request->lastmod);
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) {
1857 hdr_out->addEntry(new HttpHeaderEntry(Http::HdrType::IF_NONE_MATCH, SBuf(),
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
1893 /** \pre Handle X-Forwarded-For */
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) {
1913 /** If set to ON - append client IP or 'unknown'. */
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) {
1919 /** If set to OFF - append 'unknown'. */
1920 strListAdd(&strFwd, "unknown", ',');
1921 } else if (strcmp(opt_forwarded_for, "transparent") == 0) {
1922 /** If set to TRANSPARENT - pass through unchanged. */
1923 } else if (strcmp(opt_forwarded_for, "truncate") == 0) {
1924 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
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 }
1933 /** If set to DELETE - do not copy through. */
1934
1935 /* append Host if not there already */
1936 if (!hdr_out->has(Http::HdrType::HOST)) {
1937 if (request->peer_domain) {
1938 hdr_out->putStr(Http::HdrType::HOST, 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
1972 SBuf tmp(request->effectiveRequestUri());
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) {
1993 if (flags.front_end_https == 1 || request->url.getScheme() == AnyP::PROTO_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
2009 /// copies from-client Upgrade info into the given to-server header while
2010 /// honoring configuration filters and following HTTP requirements
2011 void
2012 HttpStateData::forwardUpgrade(HttpHeader &hdrOut)
2013 {
2014 if (!Config.http_upgrade_request_protocols)
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.
2076 flags.forceClose = !flags.keepalive;
2077 flags.keepalive = true; // should already be true in most cases
2078 }
2079 }
2080
2081 /**
2082 * Decides whether a particular header may be cloned from the received Clients request
2083 * to our outgoing fetch request.
2084 */
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
2092 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */
2093
2094 case Http::HdrType::PROXY_AUTHORIZATION:
2095 /** \par Proxy-Authorization:
2096 * Only pass on proxy authentication to peers for which
2097 * authentication forwarding is explicitly enabled
2098 */
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
2107 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */
2108
2109 case Http::HdrType::CONNECTION: /** \par Connection: */
2110 case Http::HdrType::TE: /** \par TE: */
2111 case Http::HdrType::KEEP_ALIVE: /** \par Keep-Alive: */
2112 case Http::HdrType::PROXY_AUTHENTICATE: /** \par Proxy-Authenticate: */
2113 case Http::HdrType::TRAILER: /** \par Trailer: */
2114 case Http::HdrType::TRANSFER_ENCODING: /** \par Transfer-Encoding: */
2115 break;
2116
2117 /// \par Upgrade is hop-by-hop but forwardUpgrade() may send a filtered one
2118 case Http::HdrType::UPGRADE:
2119 break;
2120
2121 /** \par OTHER headers I haven't bothered to track down yet. */
2122
2123 case Http::HdrType::AUTHORIZATION:
2124 /** \par WWW-Authorization:
2125 * Pass on WWW authentication */
2126
2127 if (!flags.toOriginPeer()) {
2128 hdr_out->addEntry(e->clone());
2129 } else {
2130 /** \note Assume that talking to a cache_peer originserver makes
2131 * us a reverse proxy and only forward authentication if enabled
2132 * (see also httpFixupAuthentication for special cases)
2133 */
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:
2145 /** \par Host:
2146 * Normally Squid rewrites the Host: header.
2147 * However, there is one case when we don't: If the URL
2148 * went through our redirector and the admin configured
2149 * 'redir_rewrites_host' to be off.
2150 */
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
2162 case Http::HdrType::IF_MODIFIED_SINCE:
2163 /** \par If-Modified-Since:
2164 * append unless we added our own,
2165 * but only if cache_miss_revalidate is enabled, or
2166 * the request is not cacheable, or
2167 * the request contains authentication credentials.
2168 * \note at most one client's If-Modified-Since header can pass through
2169 */
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
2177 case Http::HdrType::IF_NONE_MATCH:
2178 /** \par If-None-Match:
2179 * append if the wildcard '*' special case value is present, or
2180 * cache_miss_revalidate is disabled, or
2181 * the request is not cacheable in this proxy, or
2182 * the request contains authentication credentials.
2183 * \note this header lists a set of responses for the server to elide sending. Squid added values are extending that set.
2184 */
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
2190 case Http::HdrType::MAX_FORWARDS:
2191 /** \par Max-Forwards:
2192 * pass only on TRACE or OPTIONS requests */
2193 if (request->method == Http::METHOD_TRACE || request->method == Http::METHOD_OPTIONS) {
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:
2203 /** \par Via:
2204 * If Via is disabled then forward any received header as-is.
2205 * Otherwise leave for explicit updated addition later. */
2206
2207 if (!Config.onoff.via)
2208 hdr_out->addEntry(e->clone());
2209
2210 break;
2211
2212 case Http::HdrType::RANGE:
2213
2214 case Http::HdrType::IF_RANGE:
2215
2216 case Http::HdrType::REQUEST_RANGE:
2217 /** \par Range:, If-Range:, Request-Range:
2218 * Only pass if we accept ranges */
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
2227 case Http::HdrType::CONTENT_LENGTH:
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
2234 case Http::HdrType::X_FORWARDED_FOR:
2235
2236 case Http::HdrType::CACHE_CONTROL:
2237 /** \par X-Forwarded-For:, Cache-Control:
2238 * handled specially by Squid, so leave off for now.
2239 * append these after the loop if needed */
2240 break;
2241
2242 case Http::HdrType::FRONT_END_HTTPS:
2243 /** \par Front-End-Https:
2244 * Pass thru only if peer is configured with front-end-https */
2245 if (!flags.front_end_https)
2246 hdr_out->addEntry(e->clone());
2247
2248 break;
2249
2250 default:
2251 /** \par default.
2252 * pass on all other header fields
2253 * which are NOT listed by the special Connection: header. */
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
2264 HttpStateData::decideIfWeDoRanges (HttpRequest * request)
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
2281 || request->range->offsetLimitExceeded(roffLimit) || request->flags.connectionAuth)
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
2294 HttpStateData::buildRequestPrefix(MemBuf * mb)
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();
2304 const SBuf url(flags.toOrigin ? request->url.path() : request->effectiveRequestUri());
2305 mb->appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\r\n",
2306 SQUIDSBUFPRINT(request->method.image()),
2307 SQUIDSBUFPRINT(url),
2308 AnyP::ProtocolType_str[httpver.protocol],
2309 httpver.major,httpver.minor);
2310 /* build and pack headers */
2311 {
2312 HttpHeader hdr(hoRequest);
2313 forwardUpgrade(hdr); // before httpBuildRequestHeader() for CONNECTION
2314 httpBuildRequestHeader(request.getRaw(), entry, fwd->al, &hdr, flags);
2315
2316 if (request->flags.pinned && request->flags.connectionAuth)
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)) {
2324 assert(!upgradeHeaderOut);
2325 upgradeHeaderOut = new String(hdr.getList(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
2338 HttpStateData::sendRequest()
2339 {
2340 MemBuf mb;
2341
2342 debugs(11, 5, serverConnection << ", request " << request << ", this " << this << ".");
2343
2344 if (!Comm::IsConnOpen(serverConnection)) {
2345 debugs(11,3, "cannot send request to closing " << serverConnection);
2346 assert(closeHandler != NULL);
2347 return false;
2348 }
2349
2350 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
2351 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
2352 TimeoutDialer, this, HttpStateData::httpTimeout);
2353 commSetConnTimeout(serverConnection, Config.Timeout.lifetime, timeoutCall);
2354 flags.do_next_read = true;
2355 maybeReadVirginBody();
2356
2357 if (request->body_pipe != NULL) {
2358 if (!startRequestBodyFlow()) // register to receive body data
2359 return false;
2360 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2361 requestSender = JobCallback(11,5,
2362 Dialer, this, HttpStateData::sentRequestBody);
2363
2364 Must(!flags.chunked_request);
2365 // use chunked encoding if we do not know the length
2366 if (request->content_length < 0)
2367 flags.chunked_request = true;
2368 } else {
2369 assert(!requestBodySource);
2370 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2371 requestSender = JobCallback(11,5,
2372 Dialer, this, HttpStateData::wroteLast);
2373 }
2374
2375 /*
2376 * Is keep-alive okay for all request methods?
2377 */
2378 if (request->flags.mustKeepalive)
2379 flags.keepalive = true;
2380 else if (request->flags.pinned)
2381 flags.keepalive = request->persistent();
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
2386 flags.keepalive = !request->flags.sslBumped;
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 */
2407 if (neighborType(_peer, request->url) == PEER_SIBLING && !_peer->options.allow_miss)
2408 flags.only_if_cached = true;
2409
2410 flags.front_end_https = _peer->front_end_https;
2411 }
2412
2413 mb.init();
2414 request->peer_host=_peer?_peer->host:NULL;
2415 buildRequestPrefix(&mb);
2416
2417 debugs(11, 2, "HTTP Server " << serverConnection);
2418 debugs(11, 2, "HTTP Server REQUEST:\n---------\n" << mb.buf << "\n----------");
2419
2420 Comm::Write(serverConnection, &mb, requestSender);
2421 return true;
2422 }
2423
2424 bool
2425 HttpStateData::getMoreRequestBody(MemBuf &buf)
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
2433 Must(requestBodySource != NULL);
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
2449 if (receivedWholeRequestBody) {
2450 Must(!flags.sentLastChunk);
2451 flags.sentLastChunk = true;
2452 buf.append("0\r\n\r\n", 5);
2453 }
2454
2455 return true;
2456 }
2457
2458 void
2459 httpStart(FwdState *fwd)
2460 {
2461 debugs(11, 3, fwd->request->method << ' ' << fwd->entry->url());
2462 AsyncJob::Start(new HttpStateData(fwd));
2463 }
2464
2465 void
2466 HttpStateData::start()
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
2484 /// if broken posts are enabled for the request, try to fix and return true
2485 bool
2486 HttpStateData::finishingBrokenPost()
2487 {
2488 #if USE_HTTP_VIOLATIONS
2489 if (!Config.accessList.brokenPosts) {
2490 debugs(11, 5, "No brokenPosts list");
2491 return false;
2492 }
2493
2494 ACLFilledChecklist ch(Config.accessList.brokenPosts, originalRequest().getRaw());
2495 ch.al = fwd->al;
2496 ch.syncAle(originalRequest().getRaw(), nullptr);
2497 if (!ch.fastCheck().allowed()) {
2498 debugs(11, 5, "didn't match brokenPosts");
2499 return false;
2500 }
2501
2502 if (!Comm::IsConnOpen(serverConnection)) {
2503 debugs(11, 3, "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");
2509 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2510 requestSender = JobCallback(11,5,
2511 Dialer, this, HttpStateData::wroteLast);
2512 Comm::Write(serverConnection, "\r\n", 2, requestSender, NULL);
2513 return true;
2514 #else
2515 return false;
2516 #endif /* USE_HTTP_VIOLATIONS */
2517 }
2518
2519 /// if needed, write last-chunk to end the request body and return true
2520 bool
2521 HttpStateData::finishingChunkedRequest()
2522 {
2523 if (flags.sentLastChunk) {
2524 debugs(11, 5, "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
2531 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
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
2538 HttpStateData::doneSendingRequestBody()
2539 {
2540 Client::doneSendingRequestBody();
2541 debugs(11,5, serverConnection);
2542
2543 // do we need to write something after the last body byte?
2544 if (flags.chunked_request && finishingChunkedRequest())
2545 return;
2546 if (!flags.chunked_request && finishingBrokenPost())
2547 return;
2548
2549 sendComplete();
2550 }
2551
2552 // more origin request body data is available
2553 void
2554 HttpStateData::handleMoreRequestBodyAvailable()
2555 {
2556 if (eof || !Comm::IsConnOpen(serverConnection)) {
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, "Transaction aborted while reading HTTP body");
2561 return;
2562 }
2563
2564 assert(requestBodySource != NULL);
2565
2566 if (requestBodySource->buf().hasContent()) {
2567 // XXX: why does not this trigger a debug message on every request?
2568
2569 if (flags.headers_parsed && !flags.abuse_detected) {
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
2581 HttpStateData::handleMoreRequestBodyAvailable();
2582 }
2583
2584 // premature end of the request body
2585 void
2586 HttpStateData::handleRequestBodyProducerAborted()
2587 {
2588 Client::handleRequestBodyProducerAborted();
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).
2595 const auto err = new ErrorState(ERR_ICAP_FAILURE, Http::scInternalServerError, fwd->request, fwd->al);
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
2606 HttpStateData::sentRequestBody(const CommIoCbParams &io)
2607 {
2608 if (io.size > 0)
2609 statCounter.server.http.kbytes_out += io.size;
2610
2611 Client::sentRequestBody(io);
2612 }
2613
2614 void
2615 HttpStateData::abortAll(const char *reason)
2616 {
2617 debugs(11,5, "aborting transaction for " << reason <<
2618 "; " << serverConnection << ", this " << this);
2619 mustStop(reason);
2620 }
2621
2622 HttpStateData::ReuseDecision::ReuseDecision(const StoreEntry *e, const Http::StatusCode code)
2623 : answer(HttpStateData::ReuseDecision::reuseNot), reason(nullptr), entry(e), statusCode(code) {}
2624
2625 HttpStateData::ReuseDecision::Answers
2626 HttpStateData::ReuseDecision::make(const HttpStateData::ReuseDecision::Answers ans, const char *why)
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
2642 assert(d.answer >= HttpStateData::ReuseDecision::reuseNot &&
2643 d.answer <= HttpStateData::ReuseDecision::doNotCacheButShare);
2644 return os << ReuseMessages[d.answer] << " because " << d.reason <<
2645 "; HTTP status " << d.statusCode << " " << *(d.entry);
2646 }
2647