]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http.cc
Merged from trunk
[thirdparty/squid.git] / src / http.cc
1 /*
2 * Copyright (C) 1996-2016 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 informations.
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 "err_detail_type.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 "HttpStateFlags.h"
45 #include "log/access_log.h"
46 #include "MemBuf.h"
47 #include "MemObject.h"
48 #include "neighbors.h"
49 #include "peer_proxy_negotiate_auth.h"
50 #include "profiler/Profiler.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 "URL.h"
61 #include "util.h"
62
63 #if USE_AUTH
64 #include "auth/UserRequest.h"
65 #endif
66 #if USE_DELAY_POOLS
67 #include "DelayPools.h"
68 #endif
69
70 #define SQUID_ENTER_THROWING_CODE() try {
71 #define SQUID_EXIT_THROWING_CODE(status) \
72 status = true; \
73 } \
74 catch (const std::exception &e) { \
75 debugs (11, 1, "Exception error:" << e.what()); \
76 status = false; \
77 }
78
79 CBDATA_CLASS_INIT(HttpStateData);
80
81 static const char *const crlf = "\r\n";
82
83 static void httpMaybeRemovePublic(StoreEntry *, Http::StatusCode);
84 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest * request,
85 HttpHeader * hdr_out, const int we_do_ranges, const HttpStateFlags &);
86
87 HttpStateData::HttpStateData(FwdState *theFwdState) :
88 AsyncJob("HttpStateData"),
89 Client(theFwdState),
90 lastChunk(0),
91 httpChunkDecoder(NULL),
92 payloadSeen(0),
93 payloadTruncated(0),
94 sawDateGoBack(false)
95 {
96 debugs(11,5,HERE << "HttpStateData " << this << " created");
97 ignoreCacheControl = false;
98 surrogateNoStore = false;
99 serverConnection = fwd->serverConnection();
100
101 // reset peer response time stats for %<pt
102 request->hier.peer_http_request_sent.tv_sec = 0;
103 request->hier.peer_http_request_sent.tv_usec = 0;
104
105 if (fwd->serverConnection() != NULL)
106 _peer = cbdataReference(fwd->serverConnection()->getPeer()); /* might be NULL */
107
108 if (_peer) {
109 request->flags.proxying = true;
110 /*
111 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
112 * We might end up getting the object from somewhere else if,
113 * for example, the request to this neighbor fails.
114 */
115 if (_peer->options.proxy_only)
116 entry->releaseRequest();
117
118 #if USE_DELAY_POOLS
119 entry->setNoDelay(_peer->options.no_delay);
120 #endif
121 }
122
123 /*
124 * register the handler to free HTTP state data when the FD closes
125 */
126 typedef CommCbMemFunT<HttpStateData, CommCloseCbParams> Dialer;
127 closeHandler = JobCallback(9, 5, Dialer, this, HttpStateData::httpStateConnClosed);
128 comm_add_close_handler(serverConnection->fd, closeHandler);
129 }
130
131 HttpStateData::~HttpStateData()
132 {
133 /*
134 * don't forget that ~Client() gets called automatically
135 */
136
137 if (httpChunkDecoder)
138 delete httpChunkDecoder;
139
140 cbdataReferenceDone(_peer);
141
142 debugs(11,5, HERE << "HttpStateData " << this << " destroyed; " << serverConnection);
143 }
144
145 const Comm::ConnectionPointer &
146 HttpStateData::dataConnection() const
147 {
148 return serverConnection;
149 }
150
151 void
152 HttpStateData::httpStateConnClosed(const CommCloseCbParams &params)
153 {
154 debugs(11, 5, "httpStateFree: FD " << params.fd << ", httpState=" << params.data);
155 doneWithFwd = "httpStateConnClosed()"; // assume FwdState is monitoring too
156 mustStop("HttpStateData::httpStateConnClosed");
157 }
158
159 void
160 HttpStateData::httpTimeout(const CommTimeoutCbParams &)
161 {
162 debugs(11, 4, serverConnection << ": '" << entry->url() << "'");
163
164 if (entry->store_status == STORE_PENDING) {
165 fwd->fail(new ErrorState(ERR_READ_TIMEOUT, Http::scGatewayTimeout, fwd->request));
166 }
167
168 closeServer();
169 mustStop("HttpStateData::httpTimeout");
170 }
171
172 static StoreEntry *
173 findPreviouslyCachedEntry(StoreEntry *newEntry) {
174 assert(newEntry->mem_obj);
175 return newEntry->mem_obj->request ?
176 storeGetPublicByRequest(newEntry->mem_obj->request) :
177 storeGetPublic(newEntry->mem_obj->storeId(), newEntry->mem_obj->method);
178 }
179
180 /// Remove an existing public store entry if the incoming response (to be
181 /// stored in a currently private entry) is going to invalidate it.
182 static void
183 httpMaybeRemovePublic(StoreEntry * e, Http::StatusCode status)
184 {
185 int remove = 0;
186 int forbidden = 0;
187
188 // If the incoming response already goes into a public entry, then there is
189 // nothing to remove. This protects ready-for-collapsing entries as well.
190 if (!EBIT_TEST(e->flags, KEY_PRIVATE))
191 return;
192
193 switch (status) {
194
195 case Http::scOkay:
196
197 case Http::scNonAuthoritativeInformation:
198
199 case Http::scMultipleChoices:
200
201 case Http::scMovedPermanently:
202
203 case Http::scFound:
204
205 case Http::scSeeOther:
206
207 case Http::scGone:
208
209 case Http::scNotFound:
210 remove = 1;
211
212 break;
213
214 case Http::scForbidden:
215
216 case Http::scMethodNotAllowed:
217 forbidden = 1;
218
219 break;
220
221 #if WORK_IN_PROGRESS
222
223 case Http::scUnauthorized:
224 forbidden = 1;
225
226 break;
227
228 #endif
229
230 default:
231 #if QUESTIONABLE
232 /*
233 * Any 2xx response should eject previously cached entities...
234 */
235
236 if (status >= 200 && status < 300)
237 remove = 1;
238
239 #endif
240
241 break;
242 }
243
244 if (!remove && !forbidden)
245 return;
246
247 StoreEntry *pe = findPreviouslyCachedEntry(e);
248
249 if (pe != NULL) {
250 assert(e != pe);
251 #if USE_HTCP
252 neighborsHtcpClear(e, NULL, e->mem_obj->request, e->mem_obj->method, HTCP_CLR_INVALIDATION);
253 #endif
254 pe->release();
255 }
256
257 /** \par
258 * Also remove any cached HEAD response in case the object has
259 * changed.
260 */
261 if (e->mem_obj->request)
262 pe = storeGetPublicByRequestMethod(e->mem_obj->request, Http::METHOD_HEAD);
263 else
264 pe = storeGetPublic(e->mem_obj->storeId(), Http::METHOD_HEAD);
265
266 if (pe != NULL) {
267 assert(e != pe);
268 #if USE_HTCP
269 neighborsHtcpClear(e, NULL, e->mem_obj->request, HttpRequestMethod(Http::METHOD_HEAD), HTCP_CLR_INVALIDATION);
270 #endif
271 pe->release();
272 }
273 }
274
275 void
276 HttpStateData::processSurrogateControl(HttpReply *reply)
277 {
278 if (request->flags.accelerated && reply->surrogate_control) {
279 HttpHdrScTarget *sctusable = reply->surrogate_control->getMergedTarget(Config.Accel.surrogate_id);
280
281 if (sctusable) {
282 if (sctusable->noStore() ||
283 (Config.onoff.surrogate_is_remote
284 && sctusable->noStoreRemote())) {
285 surrogateNoStore = true;
286 entry->makePrivate();
287 }
288
289 /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
290 * accelerated request or not...
291 * Still, this is an abstraction breach. - RC
292 */
293 if (sctusable->hasMaxAge()) {
294 if (sctusable->maxAge() < sctusable->maxStale())
295 reply->expires = reply->date + sctusable->maxAge();
296 else
297 reply->expires = reply->date + sctusable->maxStale();
298
299 /* And update the timestamps */
300 entry->timestampsSet();
301 }
302
303 /* We ignore cache-control directives as per the Surrogate specification */
304 ignoreCacheControl = true;
305
306 delete sctusable;
307 }
308 }
309 }
310
311 int
312 HttpStateData::cacheableReply()
313 {
314 HttpReply const *rep = finalReply();
315 HttpHeader const *hdr = &rep->header;
316 const char *v;
317 #if USE_HTTP_VIOLATIONS
318
319 const RefreshPattern *R = NULL;
320
321 /* This strange looking define first looks up the refresh pattern
322 * and then checks if the specified flag is set. The main purpose
323 * of this is to simplify the refresh pattern lookup and USE_HTTP_VIOLATIONS
324 * condition
325 */
326 #define REFRESH_OVERRIDE(flag) \
327 ((R = (R ? R : refreshLimits(entry->mem_obj->storeId()))) , \
328 (R && R->flags.flag))
329 #else
330 #define REFRESH_OVERRIDE(flag) 0
331 #endif
332
333 if (EBIT_TEST(entry->flags, RELEASE_REQUEST)) {
334 debugs(22, 3, "NO because " << *entry << " has been released.");
335 return 0;
336 }
337
338 // RFC 7234 section 4: a cache MUST use the most recent response
339 // (as determined by the Date header field)
340 if (sawDateGoBack) {
341 debugs(22, 3, "NO because " << *entry << " has an older date header.");
342 return 0;
343 }
344
345 // Check for Surrogate/1.0 protocol conditions
346 // NP: reverse-proxy traffic our parent server has instructed us never to cache
347 if (surrogateNoStore) {
348 debugs(22, 3, HERE << "NO because Surrogate-Control:no-store");
349 return 0;
350 }
351
352 // RFC 2616: HTTP/1.1 Cache-Control conditions
353 if (!ignoreCacheControl) {
354 // XXX: check to see if the request headers alone were enough to prevent caching earlier
355 // (ie no-store request header) no need to check those all again here if so.
356 // for now we are not reliably doing that so we waste CPU re-checking request CC
357
358 // RFC 2616 section 14.9.2 - MUST NOT cache any response with request CC:no-store
359 if (request && request->cache_control && request->cache_control->noStore() &&
360 !REFRESH_OVERRIDE(ignore_no_store)) {
361 debugs(22, 3, HERE << "NO because client request Cache-Control:no-store");
362 return 0;
363 }
364
365 // NP: request CC:no-cache only means cache READ is forbidden. STORE is permitted.
366 if (rep->cache_control && rep->cache_control->hasNoCache() && rep->cache_control->noCache().size() > 0) {
367 /* TODO: we are allowed to cache when no-cache= has parameters.
368 * Provided we strip away any of the listed headers unless they are revalidated
369 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
370 * That is a bit tricky for squid right now so we avoid caching entirely.
371 */
372 debugs(22, 3, HERE << "NO because server reply Cache-Control:no-cache has parameters");
373 return 0;
374 }
375
376 // NP: request CC:private is undefined. We ignore.
377 // NP: other request CC flags are limiters on HIT/MISS. We don't care about here.
378
379 // RFC 2616 section 14.9.2 - MUST NOT cache any response with CC:no-store
380 if (rep->cache_control && rep->cache_control->noStore() &&
381 !REFRESH_OVERRIDE(ignore_no_store)) {
382 debugs(22, 3, HERE << "NO because server reply Cache-Control:no-store");
383 return 0;
384 }
385
386 // RFC 2616 section 14.9.1 - MUST NOT cache any response with CC:private in a shared cache like Squid.
387 // CC:private overrides CC:public when both are present in a response.
388 // TODO: add a shared/private cache configuration possibility.
389 if (rep->cache_control &&
390 rep->cache_control->hasPrivate() &&
391 !REFRESH_OVERRIDE(ignore_private)) {
392 /* TODO: we are allowed to cache when private= has parameters.
393 * Provided we strip away any of the listed headers unless they are revalidated
394 * successfully (ie, must revalidate AND these headers are prohibited on stale replies).
395 * That is a bit tricky for squid right now so we avoid caching entirely.
396 */
397 debugs(22, 3, HERE << "NO because server reply Cache-Control:private");
398 return 0;
399 }
400 }
401
402 // RFC 2068, sec 14.9.4 - MUST NOT cache any response with Authentication UNLESS certain CC controls are present
403 // allow HTTP violations to IGNORE those controls (ie re-block caching Auth)
404 if (request && (request->flags.auth || request->flags.authSent)) {
405 if (!rep->cache_control) {
406 debugs(22, 3, HERE << "NO because Authenticated and server reply missing Cache-Control");
407 return 0;
408 }
409
410 if (ignoreCacheControl) {
411 debugs(22, 3, HERE << "NO because Authenticated and ignoring Cache-Control");
412 return 0;
413 }
414
415 bool mayStore = false;
416 // HTTPbis pt6 section 3.2: a response CC:public is present
417 if (rep->cache_control->Public()) {
418 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:public");
419 mayStore = true;
420
421 // HTTPbis pt6 section 3.2: a response CC:must-revalidate is present
422 } else if (rep->cache_control->mustRevalidate()) {
423 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:must-revalidate");
424 mayStore = true;
425
426 #if USE_HTTP_VIOLATIONS
427 // NP: given the must-revalidate exception we should also be able to exempt no-cache.
428 // HTTPbis WG verdict on this is that it is omitted from the spec due to being 'unexpected' by
429 // some. The caching+revalidate is not exactly unsafe though with Squids interpretation of no-cache
430 // (without parameters) as equivalent to must-revalidate in the reply.
431 } else if (rep->cache_control->hasNoCache() && rep->cache_control->noCache().size() == 0) {
432 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:no-cache (equivalent to must-revalidate)");
433 mayStore = true;
434 #endif
435
436 // HTTPbis pt6 section 3.2: a response CC:s-maxage is present
437 } else if (rep->cache_control->sMaxAge()) {
438 debugs(22, 3, HERE << "Authenticated but server reply Cache-Control:s-maxage");
439 mayStore = true;
440 }
441
442 if (!mayStore) {
443 debugs(22, 3, HERE << "NO because Authenticated transaction");
444 return 0;
445 }
446
447 // NP: response CC:no-cache is equivalent to CC:must-revalidate,max-age=0. We MAY cache, and do so.
448 // NP: other request CC flags are limiters on HIT/MISS/REFRESH. We don't care about here.
449 }
450
451 /* HACK: The "multipart/x-mixed-replace" content type is used for
452 * continuous push replies. These are generally dynamic and
453 * probably should not be cachable
454 */
455 if ((v = hdr->getStr(Http::HdrType::CONTENT_TYPE)))
456 if (!strncasecmp(v, "multipart/x-mixed-replace", 25)) {
457 debugs(22, 3, HERE << "NO because Content-Type:multipart/x-mixed-replace");
458 return 0;
459 }
460
461 switch (rep->sline.status()) {
462 /* Responses that are cacheable */
463
464 case Http::scOkay:
465
466 case Http::scNonAuthoritativeInformation:
467
468 case Http::scMultipleChoices:
469
470 case Http::scMovedPermanently:
471 case Http::scPermanentRedirect:
472
473 case Http::scGone:
474 /*
475 * Don't cache objects that need to be refreshed on next request,
476 * unless we know how to refresh it.
477 */
478
479 if (!refreshIsCachable(entry) && !REFRESH_OVERRIDE(store_stale)) {
480 debugs(22, 3, "NO because refreshIsCachable() returned non-cacheable..");
481 return 0;
482 } else {
483 debugs(22, 3, HERE << "YES because HTTP status " << rep->sline.status());
484 return 1;
485 }
486 /* NOTREACHED */
487 break;
488
489 /* Responses that only are cacheable if the server says so */
490
491 case Http::scFound:
492 case Http::scTemporaryRedirect:
493 if (rep->date <= 0) {
494 debugs(22, 3, HERE << "NO because HTTP status " << rep->sline.status() << " and Date missing/invalid");
495 return 0;
496 }
497 if (rep->expires > rep->date) {
498 debugs(22, 3, HERE << "YES because HTTP status " << rep->sline.status() << " and Expires > Date");
499 return 1;
500 } else {
501 debugs(22, 3, HERE << "NO because HTTP status " << rep->sline.status() << " and Expires <= Date");
502 return 0;
503 }
504 /* NOTREACHED */
505 break;
506
507 /* Errors can be negatively cached */
508
509 case Http::scNoContent:
510
511 case Http::scUseProxy:
512
513 case Http::scBadRequest:
514
515 case Http::scForbidden:
516
517 case Http::scNotFound:
518
519 case Http::scMethodNotAllowed:
520
521 case Http::scUriTooLong:
522
523 case Http::scInternalServerError:
524
525 case Http::scNotImplemented:
526
527 case Http::scBadGateway:
528
529 case Http::scServiceUnavailable:
530
531 case Http::scGatewayTimeout:
532 case Http::scMisdirectedRequest:
533
534 debugs(22, 3, "MAYBE because HTTP status " << rep->sline.status());
535 return -1;
536
537 /* NOTREACHED */
538 break;
539
540 /* Some responses can never be cached */
541
542 case Http::scPartialContent: /* Not yet supported */
543
544 case Http::scSeeOther:
545
546 case Http::scNotModified:
547
548 case Http::scUnauthorized:
549
550 case Http::scProxyAuthenticationRequired:
551
552 case Http::scInvalidHeader: /* Squid header parsing error */
553
554 case Http::scHeaderTooLarge:
555
556 case Http::scPaymentRequired:
557 case Http::scNotAcceptable:
558 case Http::scRequestTimeout:
559 case Http::scConflict:
560 case Http::scLengthRequired:
561 case Http::scPreconditionFailed:
562 case Http::scPayloadTooLarge:
563 case Http::scUnsupportedMediaType:
564 case Http::scUnprocessableEntity:
565 case Http::scLocked:
566 case Http::scFailedDependency:
567 case Http::scInsufficientStorage:
568 case Http::scRequestedRangeNotSatisfied:
569 case Http::scExpectationFailed:
570
571 debugs(22, 3, HERE << "NO because HTTP status " << rep->sline.status());
572 return 0;
573
574 default:
575 /* RFC 2616 section 6.1.1: an unrecognized response MUST NOT be cached. */
576 debugs (11, 3, HERE << "NO because unknown HTTP status code " << rep->sline.status());
577 return 0;
578
579 /* NOTREACHED */
580 break;
581 }
582
583 /* NOTREACHED */
584 }
585
586 /// assemble a variant key (vary-mark) from the given Vary header and HTTP request
587 static void
588 assembleVaryKey(String &vary, SBuf &vstr, const HttpRequest &request)
589 {
590 static const SBuf asterisk("*");
591 const char *pos = nullptr;
592 const char *item = nullptr;
593 int ilen = 0;
594
595 while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
596 SBuf name(item, ilen);
597 if (name == asterisk) {
598 vstr = asterisk;
599 break;
600 }
601 name.toLower();
602 if (!vstr.isEmpty())
603 vstr.append(", ", 2);
604 vstr.append(name);
605 String hdr(request.header.getByName(name));
606 const char *value = hdr.termedBuf();
607 if (value) {
608 value = rfc1738_escape_part(value);
609 vstr.append("=\"", 2);
610 vstr.append(value);
611 vstr.append("\"", 1);
612 }
613
614 hdr.clean();
615 }
616 }
617
618 /*
619 * For Vary, store the relevant request headers as
620 * virtual headers in the reply
621 * Returns an empty SBuf if the variance cannot be stored
622 */
623 SBuf
624 httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
625 {
626 SBuf vstr;
627 String vary;
628
629 vary = reply->header.getList(Http::HdrType::VARY);
630 assembleVaryKey(vary, vstr, *request);
631
632 #if X_ACCELERATOR_VARY
633 vary.clean();
634 vary = reply->header.getList(Http::HdrType::HDR_X_ACCELERATOR_VARY);
635 assembleVaryKey(vary, vstr, *request);
636 #endif
637
638 debugs(11, 3, vstr);
639 return vstr;
640 }
641
642 void
643 HttpStateData::keepaliveAccounting(HttpReply *reply)
644 {
645 if (flags.keepalive)
646 if (_peer)
647 ++ _peer->stats.n_keepalives_sent;
648
649 if (reply->keep_alive) {
650 if (_peer)
651 ++ _peer->stats.n_keepalives_recv;
652
653 if (Config.onoff.detect_broken_server_pconns
654 && reply->bodySize(request->method) == -1 && !flags.chunked) {
655 debugs(11, DBG_IMPORTANT, "keepaliveAccounting: Impossible keep-alive header from '" << entry->url() << "'" );
656 // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" );
657 flags.keepalive_broken = true;
658 }
659 }
660 }
661
662 void
663 HttpStateData::checkDateSkew(HttpReply *reply)
664 {
665 if (reply->date > -1 && !_peer) {
666 int skew = abs((int)(reply->date - squid_curtime));
667
668 if (skew > 86400)
669 debugs(11, 3, "" << request->url.host() << "'s clock is skewed by " << skew << " seconds!");
670 }
671 }
672
673 /**
674 * This creates the error page itself.. its likely
675 * that the forward ported reply header max size patch
676 * generates non http conformant error pages - in which
677 * case the errors where should be 'BAD_GATEWAY' etc
678 */
679 void
680 HttpStateData::processReplyHeader()
681 {
682 /** Creates a blank header. If this routine is made incremental, this will not do */
683
684 /* NP: all exit points to this function MUST call ctx_exit(ctx) */
685 Ctx ctx = ctx_enter(entry->mem_obj->urlXXX());
686
687 debugs(11, 3, "processReplyHeader: key '" << entry->getMD5Text() << "'");
688
689 assert(!flags.headers_parsed);
690
691 if (!inBuf.length()) {
692 ctx_exit(ctx);
693 return;
694 }
695
696 /* Attempt to parse the first line; this will define where the protocol, status, reason-phrase and header begin */
697 {
698 if (hp == NULL)
699 hp = new Http1::ResponseParser;
700
701 bool parsedOk = hp->parse(inBuf);
702
703 // sync the buffers after parsing.
704 inBuf = hp->remaining();
705
706 if (hp->needsMoreData()) {
707 if (eof) { // no more data coming
708 /* Bug 2879: Replies may terminate with \r\n then EOF instead of \r\n\r\n.
709 * We also may receive truncated responses.
710 * Ensure here that we have at minimum two \r\n when EOF is seen.
711 */
712 inBuf.append("\r\n\r\n", 4);
713 // retry the parse
714 parsedOk = hp->parse(inBuf);
715 // sync the buffers after parsing.
716 inBuf = hp->remaining();
717 } else {
718 debugs(33, 5, "Incomplete response, waiting for end of response headers");
719 ctx_exit(ctx);
720 return;
721 }
722 }
723
724 if (!parsedOk) {
725 // unrecoverable parsing error
726 // TODO: Use Raw! XXX: inBuf no longer has the [beginning of the] malformed header.
727 debugs(11, 3, "Non-HTTP-compliant header:\n---------\n" << inBuf << "\n----------");
728 flags.headers_parsed = true;
729 HttpReply *newrep = new HttpReply;
730 newrep->sline.set(Http::ProtocolVersion(), hp->parseStatusCode);
731 setVirginReply(newrep);
732 ctx_exit(ctx);
733 return;
734 }
735 }
736
737 /* We know the whole response is in parser now */
738 debugs(11, 2, "HTTP Server " << serverConnection);
739 debugs(11, 2, "HTTP Server RESPONSE:\n---------\n" <<
740 hp->messageProtocol() << " " << hp->messageStatus() << " " << hp->reasonPhrase() << "\n" <<
741 hp->mimeHeader() <<
742 "----------");
743
744 // reset payload tracking to begin after message headers
745 payloadSeen = inBuf.length();
746
747 HttpReply *newrep = new HttpReply;
748 // XXX: RFC 7230 indicates we MAY ignore the reason phrase,
749 // and use an empty string on unknown status.
750 // We do that now to avoid performance regression from using SBuf::c_str()
751 newrep->sline.set(Http::ProtocolVersion(1,1), hp->messageStatus() /* , hp->reasonPhrase() */);
752 newrep->sline.protocol = newrep->sline.version.protocol = hp->messageProtocol().protocol;
753 newrep->sline.version.major = hp->messageProtocol().major;
754 newrep->sline.version.minor = hp->messageProtocol().minor;
755
756 // parse headers
757 if (!newrep->parseHeader(*hp)) {
758 // XXX: when Http::ProtocolVersion is a function, remove this hack. just set with messageProtocol()
759 newrep->sline.set(Http::ProtocolVersion(), Http::scInvalidHeader);
760 newrep->sline.version.protocol = hp->messageProtocol().protocol;
761 newrep->sline.version.major = hp->messageProtocol().major;
762 newrep->sline.version.minor = hp->messageProtocol().minor;
763 debugs(11, 2, "error parsing response headers mime block");
764 }
765
766 // done with Parser, now process using the HttpReply
767 hp = NULL;
768
769 newrep->sources |= request->url.getScheme() == AnyP::PROTO_HTTPS ? HttpMsg::srcHttps : HttpMsg::srcHttp;
770
771 newrep->removeStaleWarnings();
772
773 if (newrep->sline.protocol == AnyP::PROTO_HTTP && newrep->sline.status() >= 100 && newrep->sline.status() < 200) {
774 handle1xx(newrep);
775 ctx_exit(ctx);
776 return;
777 }
778
779 flags.chunked = false;
780 if (newrep->sline.protocol == AnyP::PROTO_HTTP && newrep->header.chunked()) {
781 flags.chunked = true;
782 httpChunkDecoder = new Http1::TeChunkedParser;
783 }
784
785 if (!peerSupportsConnectionPinning())
786 request->flags.connectionAuthDisabled = true;
787
788 HttpReply *vrep = setVirginReply(newrep);
789 flags.headers_parsed = true;
790
791 keepaliveAccounting(vrep);
792
793 checkDateSkew(vrep);
794
795 processSurrogateControl (vrep);
796
797 request->hier.peer_reply_status = newrep->sline.status();
798
799 ctx_exit(ctx);
800 }
801
802 /// ignore or start forwarding the 1xx response (a.k.a., control message)
803 void
804 HttpStateData::handle1xx(HttpReply *reply)
805 {
806 HttpReply::Pointer msg(reply); // will destroy reply if unused
807
808 // one 1xx at a time: we must not be called while waiting for previous 1xx
809 Must(!flags.handling1xx);
810 flags.handling1xx = true;
811
812 if (!request->canHandle1xx() || request->forcedBodyContinuation) {
813 debugs(11, 2, "ignoring 1xx because it is " << (request->forcedBodyContinuation ? "already sent" : "not supported by client"));
814 proceedAfter1xx();
815 return;
816 }
817
818 #if USE_HTTP_VIOLATIONS
819 // check whether the 1xx response forwarding is allowed by squid.conf
820 if (Config.accessList.reply) {
821 ACLFilledChecklist ch(Config.accessList.reply, originalRequest(), NULL);
822 ch.reply = reply;
823 HTTPMSGLOCK(ch.reply);
824 if (ch.fastCheck() != ACCESS_ALLOWED) { // TODO: support slow lookups?
825 debugs(11, 3, HERE << "ignoring denied 1xx");
826 proceedAfter1xx();
827 return;
828 }
829 }
830 #endif // USE_HTTP_VIOLATIONS
831
832 debugs(11, 2, HERE << "forwarding 1xx to client");
833
834 // the Sink will use this to call us back after writing 1xx to the client
835 typedef NullaryMemFunT<HttpStateData> CbDialer;
836 const AsyncCall::Pointer cb = JobCallback(11, 3, CbDialer, this,
837 HttpStateData::proceedAfter1xx);
838 CallJobHere1(11, 4, request->clientConnectionManager, ConnStateData,
839 ConnStateData::sendControlMsg, HttpControlMsg(msg, cb));
840 // If the call is not fired, then the Sink is gone, and HttpStateData
841 // will terminate due to an aborted store entry or another similar error.
842 // If we get stuck, it is not handle1xx fault if we could get stuck
843 // for similar reasons without a 1xx response.
844 }
845
846 /// restores state and resumes processing after 1xx is ignored or forwarded
847 void
848 HttpStateData::proceedAfter1xx()
849 {
850 Must(flags.handling1xx);
851 debugs(11, 2, "continuing with " << payloadSeen << " bytes in buffer after 1xx");
852 CallJobHere(11, 3, this, HttpStateData, HttpStateData::processReply);
853 }
854
855 /**
856 * returns true if the peer can support connection pinning
857 */
858 bool HttpStateData::peerSupportsConnectionPinning() const
859 {
860 const HttpReply *rep = entry->mem_obj->getReply();
861 const HttpHeader *hdr = &rep->header;
862 bool rc;
863 String header;
864
865 if (!_peer)
866 return true;
867
868 /*If this peer does not support connection pinning (authenticated
869 connections) return false
870 */
871 if (!_peer->connection_auth)
872 return false;
873
874 /*The peer supports connection pinning and the http reply status
875 is not unauthorized, so the related connection can be pinned
876 */
877 if (rep->sline.status() != Http::scUnauthorized)
878 return true;
879
880 /*The server respond with Http::scUnauthorized and the peer configured
881 with "connection-auth=on" we know that the peer supports pinned
882 connections
883 */
884 if (_peer->connection_auth == 1)
885 return true;
886
887 /*At this point peer has configured with "connection-auth=auto"
888 parameter so we need some extra checks to decide if we are going
889 to allow pinned connections or not
890 */
891
892 /*if the peer configured with originserver just allow connection
893 pinning (squid 2.6 behaviour)
894 */
895 if (_peer->options.originserver)
896 return true;
897
898 /*if the connections it is already pinned it is OK*/
899 if (request->flags.pinned)
900 return true;
901
902 /*Allow pinned connections only if the Proxy-support header exists in
903 reply and has in its list the "Session-Based-Authentication"
904 which means that the peer supports connection pinning.
905 */
906 if (!hdr->has(Http::HdrType::PROXY_SUPPORT))
907 return false;
908
909 header = hdr->getStrOrList(Http::HdrType::PROXY_SUPPORT);
910 /* XXX This ought to be done in a case-insensitive manner */
911 rc = (strstr(header.termedBuf(), "Session-Based-Authentication") != NULL);
912
913 return rc;
914 }
915
916 // Called when we parsed (and possibly adapted) the headers but
917 // had not starting storing (a.k.a., sending) the body yet.
918 void
919 HttpStateData::haveParsedReplyHeaders()
920 {
921 Client::haveParsedReplyHeaders();
922
923 Ctx ctx = ctx_enter(entry->mem_obj->urlXXX());
924 HttpReply *rep = finalReply();
925
926 entry->timestampsSet();
927
928 /* Check if object is cacheable or not based on reply code */
929 debugs(11, 3, "HTTP CODE: " << rep->sline.status());
930
931 if (const StoreEntry *oldEntry = findPreviouslyCachedEntry(entry))
932 sawDateGoBack = rep->olderThan(oldEntry->getReply());
933
934 if (neighbors_do_private_keys && !sawDateGoBack)
935 httpMaybeRemovePublic(entry, rep->sline.status());
936
937 bool varyFailure = false;
938 if (rep->header.has(Http::HdrType::VARY)
939 #if X_ACCELERATOR_VARY
940 || rep->header.has(Http::HdrType::HDR_X_ACCELERATOR_VARY)
941 #endif
942 ) {
943 const SBuf vary(httpMakeVaryMark(request, rep));
944
945 if (vary.isEmpty()) {
946 entry->makePrivate();
947 if (!fwd->reforwardableStatus(rep->sline.status()))
948 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
949 varyFailure = true;
950 } else {
951 entry->mem_obj->vary_headers = vary;
952
953 // RFC 7231 section 7.1.4
954 // Vary:* can be cached, but has mandatory revalidation
955 static const SBuf asterisk("*");
956 if (vary == asterisk)
957 EBIT_SET(entry->flags, ENTRY_REVALIDATE_ALWAYS);
958 }
959 }
960
961 if (!varyFailure) {
962 /*
963 * If its not a reply that we will re-forward, then
964 * allow the client to get it.
965 */
966 if (!fwd->reforwardableStatus(rep->sline.status()))
967 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
968
969 switch (cacheableReply()) {
970
971 case 1:
972 entry->makePublic();
973 break;
974
975 case 0:
976 entry->makePrivate();
977 break;
978
979 case -1:
980
981 #if USE_HTTP_VIOLATIONS
982 if (Config.negativeTtl > 0)
983 entry->cacheNegatively();
984 else
985 #endif
986 entry->makePrivate();
987 break;
988
989 default:
990 assert(0);
991 break;
992 }
993 }
994
995 if (!ignoreCacheControl) {
996 if (rep->cache_control) {
997 // We are required to revalidate on many conditions.
998 // For security reasons we do so even if storage was caused by refresh_pattern ignore-* option
999
1000 // CC:must-revalidate or CC:proxy-revalidate
1001 const bool ccMustRevalidate = (rep->cache_control->proxyRevalidate() || rep->cache_control->mustRevalidate());
1002
1003 // CC:no-cache (only if there are no parameters)
1004 const bool ccNoCacheNoParams = (rep->cache_control->hasNoCache() && rep->cache_control->noCache().size()==0);
1005
1006 // CC:s-maxage=N
1007 const bool ccSMaxAge = rep->cache_control->hasSMaxAge();
1008
1009 // CC:private (yes, these can sometimes be stored)
1010 const bool ccPrivate = rep->cache_control->hasPrivate();
1011
1012 if (ccNoCacheNoParams || ccPrivate)
1013 EBIT_SET(entry->flags, ENTRY_REVALIDATE_ALWAYS);
1014 else if (ccMustRevalidate || ccSMaxAge)
1015 EBIT_SET(entry->flags, ENTRY_REVALIDATE_STALE);
1016 }
1017 #if USE_HTTP_VIOLATIONS // response header Pragma::no-cache is undefined in HTTP
1018 else {
1019 // Expensive calculation. So only do it IF the CC: header is not present.
1020
1021 /* HACK: Pragma: no-cache in _replies_ is not documented in HTTP,
1022 * but servers like "Active Imaging Webcast/2.0" sure do use it */
1023 if (rep->header.has(Http::HdrType::PRAGMA) &&
1024 rep->header.hasListMember(Http::HdrType::PRAGMA,"no-cache",','))
1025 EBIT_SET(entry->flags, ENTRY_REVALIDATE_ALWAYS);
1026 }
1027 #endif
1028 }
1029
1030 #if HEADERS_LOG
1031 headersLog(1, 0, request->method, rep);
1032
1033 #endif
1034
1035 ctx_exit(ctx);
1036 }
1037
1038 HttpStateData::ConnectionStatus
1039 HttpStateData::statusIfComplete() const
1040 {
1041 const HttpReply *rep = virginReply();
1042 /** \par
1043 * If the reply wants to close the connection, it takes precedence */
1044
1045 if (httpHeaderHasConnDir(&rep->header, "close"))
1046 return COMPLETE_NONPERSISTENT_MSG;
1047
1048 /** \par
1049 * If we didn't send a keep-alive request header, then this
1050 * can not be a persistent connection.
1051 */
1052 if (!flags.keepalive)
1053 return COMPLETE_NONPERSISTENT_MSG;
1054
1055 /** \par
1056 * If we haven't sent the whole request then this can not be a persistent
1057 * connection.
1058 */
1059 if (!flags.request_sent) {
1060 debugs(11, 2, "Request not yet fully sent " << request->method << ' ' << entry->url());
1061 return COMPLETE_NONPERSISTENT_MSG;
1062 }
1063
1064 /** \par
1065 * What does the reply have to say about keep-alive?
1066 */
1067 /**
1068 \bug XXX BUG?
1069 * If the origin server (HTTP/1.0) does not send a keep-alive
1070 * header, but keeps the connection open anyway, what happens?
1071 * We'll return here and http.c waits for an EOF before changing
1072 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
1073 * and an error status code, and we might have to wait until
1074 * the server times out the socket.
1075 */
1076 if (!rep->keep_alive)
1077 return COMPLETE_NONPERSISTENT_MSG;
1078
1079 return COMPLETE_PERSISTENT_MSG;
1080 }
1081
1082 HttpStateData::ConnectionStatus
1083 HttpStateData::persistentConnStatus() const
1084 {
1085 debugs(11, 3, HERE << serverConnection << " eof=" << eof);
1086 if (eof) // already reached EOF
1087 return COMPLETE_NONPERSISTENT_MSG;
1088
1089 /* If server fd is closing (but we have not been notified yet), stop Comm
1090 I/O to avoid assertions. TODO: Change Comm API to handle callers that
1091 want more I/O after async closing (usually initiated by others). */
1092 // XXX: add canReceive or s/canSend/canTalkToServer/
1093 if (!Comm::IsConnOpen(serverConnection))
1094 return COMPLETE_NONPERSISTENT_MSG;
1095
1096 /** \par
1097 * In chunked response we do not know the content length but we are absolutely
1098 * sure about the end of response, so we are calling the statusIfComplete to
1099 * decide if we can be persistant
1100 */
1101 if (lastChunk && flags.chunked)
1102 return statusIfComplete();
1103
1104 const HttpReply *vrep = virginReply();
1105 debugs(11, 5, "persistentConnStatus: content_length=" << vrep->content_length);
1106
1107 const int64_t clen = vrep->bodySize(request->method);
1108
1109 debugs(11, 5, "persistentConnStatus: clen=" << clen);
1110
1111 /* If the body size is unknown we must wait for EOF */
1112 if (clen < 0)
1113 return INCOMPLETE_MSG;
1114
1115 /** \par
1116 * If the body size is known, we must wait until we've gotten all of it. */
1117 if (clen > 0) {
1118 debugs(11,5, "payloadSeen=" << payloadSeen << " content_length=" << vrep->content_length);
1119
1120 if (payloadSeen < vrep->content_length)
1121 return INCOMPLETE_MSG;
1122
1123 if (payloadTruncated > 0) // already read more than needed
1124 return COMPLETE_NONPERSISTENT_MSG; // disable pconns
1125 }
1126
1127 /** \par
1128 * If there is no message body or we got it all, we can be persistent */
1129 return statusIfComplete();
1130 }
1131
1132 #if USE_DELAY_POOLS
1133 static void
1134 readDelayed(void *context, CommRead const &)
1135 {
1136 HttpStateData *state = static_cast<HttpStateData*>(context);
1137 state->flags.do_next_read = true;
1138 state->maybeReadVirginBody();
1139 }
1140 #endif
1141
1142 void
1143 HttpStateData::readReply(const CommIoCbParams &io)
1144 {
1145 Must(!flags.do_next_read); // XXX: should have been set false by mayReadVirginBody()
1146 flags.do_next_read = false;
1147
1148 debugs(11, 5, io.conn);
1149
1150 // Bail out early on Comm::ERR_CLOSING - close handlers will tidy up for us
1151 if (io.flag == Comm::ERR_CLOSING) {
1152 debugs(11, 3, "http socket closing");
1153 return;
1154 }
1155
1156 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1157 abortTransaction("store entry aborted while reading reply");
1158 return;
1159 }
1160
1161 Must(Comm::IsConnOpen(serverConnection));
1162 Must(io.conn->fd == serverConnection->fd);
1163
1164 /*
1165 * Don't reset the timeout value here. The value should be
1166 * counting Config.Timeout.request and applies to the request
1167 * as a whole, not individual read() calls.
1168 * Plus, it breaks our lame *HalfClosed() detection
1169 */
1170
1171 Must(maybeMakeSpaceAvailable(true));
1172 CommIoCbParams rd(this); // will be expanded with ReadNow results
1173 rd.conn = io.conn;
1174 rd.size = entry->bytesWanted(Range<size_t>(0, inBuf.spaceSize()));
1175 #if USE_DELAY_POOLS
1176 if (rd.size < 1) {
1177 assert(entry->mem_obj);
1178
1179 /* read ahead limit */
1180 /* Perhaps these two calls should both live in MemObject */
1181 AsyncCall::Pointer nilCall;
1182 if (!entry->mem_obj->readAheadPolicyCanRead()) {
1183 entry->mem_obj->delayRead(DeferredRead(readDelayed, this, CommRead(io.conn, NULL, 0, nilCall)));
1184 return;
1185 }
1186
1187 /* delay id limit */
1188 entry->mem_obj->mostBytesAllowed().delayRead(DeferredRead(readDelayed, this, CommRead(io.conn, NULL, 0, nilCall)));
1189 return;
1190 }
1191 #endif
1192
1193 switch (Comm::ReadNow(rd, inBuf)) {
1194 case Comm::INPROGRESS:
1195 if (inBuf.isEmpty())
1196 debugs(33, 2, io.conn << ": no data to process, " << xstrerr(rd.xerrno));
1197 flags.do_next_read = true;
1198 maybeReadVirginBody();
1199 return;
1200
1201 case Comm::OK:
1202 {
1203 payloadSeen += rd.size;
1204 #if USE_DELAY_POOLS
1205 DelayId delayId = entry->mem_obj->mostBytesAllowed();
1206 delayId.bytesIn(rd.size);
1207 #endif
1208
1209 statCounter.server.all.kbytes_in += rd.size;
1210 statCounter.server.http.kbytes_in += rd.size;
1211 ++ IOStats.Http.reads;
1212
1213 int bin = 0;
1214 for (int clen = rd.size - 1; clen; ++bin)
1215 clen >>= 1;
1216
1217 ++ IOStats.Http.read_hist[bin];
1218
1219 // update peer response time stats (%<pt)
1220 const timeval &sent = request->hier.peer_http_request_sent;
1221 if (sent.tv_sec)
1222 tvSub(request->hier.peer_response_time, sent, current_time);
1223 else
1224 request->hier.peer_response_time.tv_sec = -1;
1225 }
1226
1227 /* Continue to process previously read data */
1228 break;
1229
1230 case Comm::ENDFILE: // close detected by 0-byte read
1231 eof = 1;
1232 flags.do_next_read = false;
1233
1234 /* Continue to process previously read data */
1235 break;
1236
1237 // case Comm::COMM_ERROR:
1238 default: // no other flags should ever occur
1239 debugs(11, 2, io.conn << ": read failure: " << xstrerr(rd.xerrno));
1240 ErrorState *err = new ErrorState(ERR_READ_ERROR, Http::scBadGateway, fwd->request);
1241 err->xerrno = rd.xerrno;
1242 fwd->fail(err);
1243 flags.do_next_read = false;
1244 closeServer();
1245 mustStop("HttpStateData::readReply");
1246 return;
1247 }
1248
1249 /* Process next response from buffer */
1250 processReply();
1251 }
1252
1253 /// processes the already read and buffered response data, possibly after
1254 /// waiting for asynchronous 1xx control message processing
1255 void
1256 HttpStateData::processReply()
1257 {
1258
1259 if (flags.handling1xx) { // we came back after handling a 1xx response
1260 debugs(11, 5, HERE << "done with 1xx handling");
1261 flags.handling1xx = false;
1262 Must(!flags.headers_parsed);
1263 }
1264
1265 if (!flags.headers_parsed) { // have not parsed headers yet?
1266 PROF_start(HttpStateData_processReplyHeader);
1267 processReplyHeader();
1268 PROF_stop(HttpStateData_processReplyHeader);
1269
1270 if (!continueAfterParsingHeader()) // parsing error or need more data
1271 return; // TODO: send errors to ICAP
1272
1273 adaptOrFinalizeReply(); // may write to, abort, or "close" the entry
1274 }
1275
1276 // kick more reads if needed and/or process the response body, if any
1277 PROF_start(HttpStateData_processReplyBody);
1278 processReplyBody(); // may call serverComplete()
1279 PROF_stop(HttpStateData_processReplyBody);
1280 }
1281
1282 /**
1283 \retval true if we can continue with processing the body or doing ICAP.
1284 */
1285 bool
1286 HttpStateData::continueAfterParsingHeader()
1287 {
1288 if (flags.handling1xx) {
1289 debugs(11, 5, HERE << "wait for 1xx handling");
1290 Must(!flags.headers_parsed);
1291 return false;
1292 }
1293
1294 if (!flags.headers_parsed && !eof) {
1295 debugs(11, 9, "needs more at " << inBuf.length());
1296 flags.do_next_read = true;
1297 /** \retval false If we have not finished parsing the headers and may get more data.
1298 * Schedules more reads to retrieve the missing data.
1299 */
1300 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1301 return false;
1302 }
1303
1304 /** If we are done with parsing, check for errors */
1305
1306 err_type error = ERR_NONE;
1307
1308 if (flags.headers_parsed) { // parsed headers, possibly with errors
1309 // check for header parsing errors
1310 if (HttpReply *vrep = virginReply()) {
1311 const Http::StatusCode s = vrep->sline.status();
1312 const AnyP::ProtocolVersion &v = vrep->sline.version;
1313 if (s == Http::scInvalidHeader && v != Http::ProtocolVersion(0,9)) {
1314 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry->url() << " AKA " << request->url);
1315 error = ERR_INVALID_RESP;
1316 } else if (s == Http::scHeaderTooLarge) {
1317 fwd->dontRetry(true);
1318 error = ERR_TOO_BIG;
1319 } else if (vrep->header.conflictingContentLength()) {
1320 fwd->dontRetry(true);
1321 error = ERR_INVALID_RESP;
1322 } else {
1323 return true; // done parsing, got reply, and no error
1324 }
1325 } else {
1326 // parsed headers but got no reply
1327 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: No reply at all for " << entry->url() << " AKA " << request->url);
1328 error = ERR_INVALID_RESP;
1329 }
1330 } else {
1331 assert(eof);
1332 if (inBuf.length()) {
1333 error = ERR_INVALID_RESP;
1334 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry->url() << " AKA " << request->url);
1335 } else {
1336 error = ERR_ZERO_SIZE_OBJECT;
1337 debugs(11, (request->flags.accelerated?DBG_IMPORTANT:2), "WARNING: HTTP: Invalid Response: No object data received for " << entry->url() << " AKA " << request->url);
1338 }
1339 }
1340
1341 assert(error != ERR_NONE);
1342 entry->reset();
1343 fwd->fail(new ErrorState(error, Http::scBadGateway, fwd->request));
1344 flags.do_next_read = false;
1345 closeServer();
1346 mustStop("HttpStateData::continueAfterParsingHeader");
1347 return false; // quit on error
1348 }
1349
1350 /** truncate what we read if we read too much so that writeReplyBody()
1351 writes no more than what we should have read */
1352 void
1353 HttpStateData::truncateVirginBody()
1354 {
1355 assert(flags.headers_parsed);
1356
1357 HttpReply *vrep = virginReply();
1358 int64_t clen = -1;
1359 if (!vrep->expectingBody(request->method, clen) || clen < 0)
1360 return; // no body or a body of unknown size, including chunked
1361
1362 if (payloadSeen - payloadTruncated <= clen)
1363 return; // we did not read too much or already took care of the extras
1364
1365 if (const int64_t extras = payloadSeen - payloadTruncated - clen) {
1366 // server sent more that the advertised content length
1367 debugs(11, 5, "payloadSeen=" << payloadSeen <<
1368 " clen=" << clen << '/' << vrep->content_length <<
1369 " trucated=" << payloadTruncated << '+' << extras);
1370
1371 inBuf.chop(0, inBuf.length() - extras);
1372 payloadTruncated += extras;
1373 }
1374 }
1375
1376 /**
1377 * Call this when there is data from the origin server
1378 * which should be sent to either StoreEntry, or to ICAP...
1379 */
1380 void
1381 HttpStateData::writeReplyBody()
1382 {
1383 truncateVirginBody(); // if needed
1384 const char *data = inBuf.rawContent();
1385 int len = inBuf.length();
1386 addVirginReplyBody(data, len);
1387 inBuf.consume(len);
1388 }
1389
1390 bool
1391 HttpStateData::decodeAndWriteReplyBody()
1392 {
1393 const char *data = NULL;
1394 int len;
1395 bool wasThereAnException = false;
1396 assert(flags.chunked);
1397 assert(httpChunkDecoder);
1398 SQUID_ENTER_THROWING_CODE();
1399 MemBuf decodedData;
1400 decodedData.init();
1401 httpChunkDecoder->setPayloadBuffer(&decodedData);
1402 const bool doneParsing = httpChunkDecoder->parse(inBuf);
1403 inBuf = httpChunkDecoder->remaining(); // sync buffers after parse
1404 len = decodedData.contentSize();
1405 data=decodedData.content();
1406 addVirginReplyBody(data, len);
1407 if (doneParsing) {
1408 lastChunk = 1;
1409 flags.do_next_read = false;
1410 }
1411 SQUID_EXIT_THROWING_CODE(wasThereAnException);
1412 return wasThereAnException;
1413 }
1414
1415 /**
1416 * processReplyBody has two purposes:
1417 * 1 - take the reply body data, if any, and put it into either
1418 * the StoreEntry, or give it over to ICAP.
1419 * 2 - see if we made it to the end of the response (persistent
1420 * connections and such)
1421 */
1422 void
1423 HttpStateData::processReplyBody()
1424 {
1425 Ip::Address client_addr;
1426 bool ispinned = false;
1427
1428 if (!flags.headers_parsed) {
1429 flags.do_next_read = true;
1430 maybeReadVirginBody();
1431 return;
1432 }
1433
1434 #if USE_ADAPTATION
1435 debugs(11,5, HERE << "adaptationAccessCheckPending=" << adaptationAccessCheckPending);
1436 if (adaptationAccessCheckPending)
1437 return;
1438
1439 #endif
1440
1441 /*
1442 * At this point the reply headers have been parsed and consumed.
1443 * That means header content has been removed from readBuf and
1444 * it contains only body data.
1445 */
1446 if (entry->isAccepting()) {
1447 if (flags.chunked) {
1448 if (!decodeAndWriteReplyBody()) {
1449 flags.do_next_read = false;
1450 serverComplete();
1451 return;
1452 }
1453 } else
1454 writeReplyBody();
1455 }
1456
1457 // storing/sending methods like earlier adaptOrFinalizeReply() or
1458 // above writeReplyBody() may release/abort the store entry.
1459 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1460 // TODO: In some cases (e.g., 304), we should keep persistent conn open.
1461 // Detect end-of-reply (and, hence, pool our idle pconn) earlier (ASAP).
1462 abortTransaction("store entry aborted while storing reply");
1463 return;
1464 } else
1465 switch (persistentConnStatus()) {
1466 case INCOMPLETE_MSG: {
1467 debugs(11, 5, "processReplyBody: INCOMPLETE_MSG from " << serverConnection);
1468 /* Wait for more data or EOF condition */
1469 AsyncCall::Pointer nil;
1470 if (flags.keepalive_broken) {
1471 commSetConnTimeout(serverConnection, 10, nil);
1472 } else {
1473 commSetConnTimeout(serverConnection, Config.Timeout.read, nil);
1474 }
1475
1476 flags.do_next_read = true;
1477 }
1478 break;
1479
1480 case COMPLETE_PERSISTENT_MSG:
1481 debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG from " << serverConnection);
1482 /* yes we have to clear all these! */
1483 commUnsetConnTimeout(serverConnection);
1484 flags.do_next_read = false;
1485
1486 comm_remove_close_handler(serverConnection->fd, closeHandler);
1487 closeHandler = NULL;
1488 fwd->unregister(serverConnection);
1489
1490 if (request->flags.spoofClientIp)
1491 client_addr = request->client_addr;
1492
1493 if (request->flags.pinned) {
1494 ispinned = true;
1495 } else if (request->flags.connectionAuth && request->flags.authSent) {
1496 ispinned = true;
1497 }
1498
1499 if (ispinned && request->clientConnectionManager.valid()) {
1500 request->clientConnectionManager->pinConnection(serverConnection, request, _peer,
1501 (request->flags.connectionAuth));
1502 } else {
1503 fwd->pconnPush(serverConnection, request->url.host());
1504 }
1505
1506 serverConnection = NULL;
1507 serverComplete();
1508 return;
1509
1510 case COMPLETE_NONPERSISTENT_MSG:
1511 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG from " << serverConnection);
1512 serverComplete();
1513 return;
1514 }
1515
1516 maybeReadVirginBody();
1517 }
1518
1519 bool
1520 HttpStateData::mayReadVirginReplyBody() const
1521 {
1522 // TODO: Be more precise here. For example, if/when reading trailer, we may
1523 // not be doneWithServer() yet, but we should return false. Similarly, we
1524 // could still be writing the request body after receiving the whole reply.
1525 return !doneWithServer();
1526 }
1527
1528 void
1529 HttpStateData::maybeReadVirginBody()
1530 {
1531 // too late to read
1532 if (!Comm::IsConnOpen(serverConnection) || fd_table[serverConnection->fd].closing())
1533 return;
1534
1535 if (!maybeMakeSpaceAvailable(false))
1536 return;
1537
1538 // XXX: get rid of the do_next_read flag
1539 // check for the proper reasons preventing read(2)
1540 if (!flags.do_next_read)
1541 return;
1542
1543 flags.do_next_read = false;
1544
1545 // must not already be waiting for read(2) ...
1546 assert(!Comm::MonitorsRead(serverConnection->fd));
1547
1548 // wait for read(2) to be possible.
1549 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
1550 AsyncCall::Pointer call = JobCallback(11, 5, Dialer, this, HttpStateData::readReply);
1551 Comm::Read(serverConnection, call);
1552 }
1553
1554 bool
1555 HttpStateData::maybeMakeSpaceAvailable(bool doGrow)
1556 {
1557 // how much we are allowed to buffer
1558 const int limitBuffer = (flags.headers_parsed ? Config.readAheadGap : Config.maxReplyHeaderSize);
1559
1560 if (limitBuffer < 0 || inBuf.length() >= (SBuf::size_type)limitBuffer) {
1561 // when buffer is at or over limit already
1562 debugs(11, 7, "wont read up to " << limitBuffer << ". buffer has (" << inBuf.length() << "/" << inBuf.spaceSize() << ") from " << serverConnection);
1563 debugs(11, DBG_DATA, "buffer has {" << inBuf << "}");
1564 // Process next response from buffer
1565 processReply();
1566 return false;
1567 }
1568
1569 // how much we want to read
1570 const size_t read_size = calcBufferSpaceToReserve(inBuf.spaceSize(), (limitBuffer - inBuf.length()));
1571
1572 if (!read_size) {
1573 debugs(11, 7, "wont read up to " << read_size << " into buffer (" << inBuf.length() << "/" << inBuf.spaceSize() << ") from " << serverConnection);
1574 return false;
1575 }
1576
1577 // just report whether we could grow or not, dont actually do it
1578 if (doGrow)
1579 return (read_size >= 2);
1580
1581 // we may need to grow the buffer
1582 inBuf.reserveSpace(read_size);
1583 debugs(11, 8, (!flags.do_next_read ? "wont" : "may") <<
1584 " read up to " << read_size << " bytes info buf(" << inBuf.length() << "/" << inBuf.spaceSize() <<
1585 ") from " << serverConnection);
1586
1587 return (inBuf.spaceSize() >= 2); // only read if there is 1+ bytes of space available
1588 }
1589
1590 /// called after writing the very last request byte (body, last-chunk, etc)
1591 void
1592 HttpStateData::wroteLast(const CommIoCbParams &io)
1593 {
1594 debugs(11, 5, HERE << serverConnection << ": size " << io.size << ": errflag " << io.flag << ".");
1595 #if URL_CHECKSUM_DEBUG
1596
1597 entry->mem_obj->checkUrlChecksum();
1598 #endif
1599
1600 if (io.size > 0) {
1601 fd_bytes(io.fd, io.size, FD_WRITE);
1602 statCounter.server.all.kbytes_out += io.size;
1603 statCounter.server.http.kbytes_out += io.size;
1604 }
1605
1606 if (io.flag == Comm::ERR_CLOSING)
1607 return;
1608
1609 if (io.flag) {
1610 ErrorState *err = new ErrorState(ERR_WRITE_ERROR, Http::scBadGateway, fwd->request);
1611 err->xerrno = io.xerrno;
1612 fwd->fail(err);
1613 closeServer();
1614 mustStop("HttpStateData::wroteLast");
1615 return;
1616 }
1617
1618 sendComplete();
1619 }
1620
1621 /// successfully wrote the entire request (including body, last-chunk, etc.)
1622 void
1623 HttpStateData::sendComplete()
1624 {
1625 /*
1626 * Set the read timeout here because it hasn't been set yet.
1627 * We only set the read timeout after the request has been
1628 * fully written to the peer. If we start the timeout
1629 * after connection establishment, then we are likely to hit
1630 * the timeout for POST/PUT requests that have very large
1631 * request bodies.
1632 */
1633 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
1634 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
1635 TimeoutDialer, this, HttpStateData::httpTimeout);
1636
1637 commSetConnTimeout(serverConnection, Config.Timeout.read, timeoutCall);
1638 flags.request_sent = true;
1639 request->hier.peer_http_request_sent = current_time;
1640 }
1641
1642 void
1643 HttpStateData::closeServer()
1644 {
1645 debugs(11,5, HERE << "closing HTTP server " << serverConnection << " this " << this);
1646
1647 if (Comm::IsConnOpen(serverConnection)) {
1648 fwd->unregister(serverConnection);
1649 comm_remove_close_handler(serverConnection->fd, closeHandler);
1650 closeHandler = NULL;
1651 serverConnection->close();
1652 }
1653 }
1654
1655 bool
1656 HttpStateData::doneWithServer() const
1657 {
1658 return !Comm::IsConnOpen(serverConnection);
1659 }
1660
1661 /*
1662 * Fixup authentication request headers for special cases
1663 */
1664 static void
1665 httpFixupAuthentication(HttpRequest * request, const HttpHeader * hdr_in, HttpHeader * hdr_out, const HttpStateFlags &flags)
1666 {
1667 Http::HdrType header = flags.originpeer ? Http::HdrType::AUTHORIZATION : Http::HdrType::PROXY_AUTHORIZATION;
1668
1669 /* Nothing to do unless we are forwarding to a peer */
1670 if (!request->flags.proxying)
1671 return;
1672
1673 /* Needs to be explicitly enabled */
1674 if (!request->peer_login)
1675 return;
1676
1677 /* Maybe already dealt with? */
1678 if (hdr_out->has(header))
1679 return;
1680
1681 /* Nothing to do here for PASSTHRU */
1682 if (strcmp(request->peer_login, "PASSTHRU") == 0)
1683 return;
1684
1685 /* PROXYPASS is a special case, single-signon to servers with the proxy password (basic only) */
1686 if (flags.originpeer && strcmp(request->peer_login, "PROXYPASS") == 0 && hdr_in->has(Http::HdrType::PROXY_AUTHORIZATION)) {
1687 const char *auth = hdr_in->getStr(Http::HdrType::PROXY_AUTHORIZATION);
1688
1689 if (auth && strncasecmp(auth, "basic ", 6) == 0) {
1690 hdr_out->putStr(header, auth);
1691 return;
1692 }
1693 }
1694
1695 uint8_t loginbuf[base64_encode_len(MAX_LOGIN_SZ)];
1696 size_t blen;
1697 struct base64_encode_ctx ctx;
1698 base64_encode_init(&ctx);
1699
1700 /* Special mode to pass the username to the upstream cache */
1701 if (*request->peer_login == '*') {
1702 const char *username = "-";
1703
1704 if (request->extacl_user.size())
1705 username = request->extacl_user.termedBuf();
1706 #if USE_AUTH
1707 else if (request->auth_user_request != NULL)
1708 username = request->auth_user_request->username();
1709 #endif
1710
1711 blen = base64_encode_update(&ctx, loginbuf, strlen(username), reinterpret_cast<const uint8_t*>(username));
1712 blen += base64_encode_update(&ctx, loginbuf+blen, strlen(request->peer_login +1), reinterpret_cast<const uint8_t*>(request->peer_login +1));
1713 blen += base64_encode_final(&ctx, loginbuf+blen);
1714 httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1715 return;
1716 }
1717
1718 /* external_acl provided credentials */
1719 if (request->extacl_user.size() && request->extacl_passwd.size() &&
1720 (strcmp(request->peer_login, "PASS") == 0 ||
1721 strcmp(request->peer_login, "PROXYPASS") == 0)) {
1722
1723 blen = base64_encode_update(&ctx, loginbuf, request->extacl_user.size(), reinterpret_cast<const uint8_t*>(request->extacl_user.rawBuf()));
1724 blen += base64_encode_update(&ctx, loginbuf+blen, 1, reinterpret_cast<const uint8_t*>(":"));
1725 blen += base64_encode_update(&ctx, loginbuf+blen, request->extacl_passwd.size(), reinterpret_cast<const uint8_t*>(request->extacl_passwd.rawBuf()));
1726 blen += base64_encode_final(&ctx, loginbuf+blen);
1727 httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1728 return;
1729 }
1730 // if no external user credentials are available to fake authentication with PASS acts like PASSTHRU
1731 if (strcmp(request->peer_login, "PASS") == 0)
1732 return;
1733
1734 /* Kerberos login to peer */
1735 #if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
1736 if (strncmp(request->peer_login, "NEGOTIATE",strlen("NEGOTIATE")) == 0) {
1737 char *Token=NULL;
1738 char *PrincipalName=NULL,*p;
1739 int negotiate_flags = 0;
1740
1741 if ((p=strchr(request->peer_login,':')) != NULL ) {
1742 PrincipalName=++p;
1743 }
1744 if (request->flags.auth_no_keytab) {
1745 negotiate_flags |= PEER_PROXY_NEGOTIATE_NOKEYTAB;
1746 }
1747 Token = peer_proxy_negotiate_auth(PrincipalName, request->peer_host, negotiate_flags);
1748 if (Token) {
1749 httpHeaderPutStrf(hdr_out, header, "Negotiate %s",Token);
1750 }
1751 return;
1752 }
1753 #endif /* HAVE_KRB5 && HAVE_GSSAPI */
1754
1755 blen = base64_encode_update(&ctx, loginbuf, strlen(request->peer_login), reinterpret_cast<const uint8_t*>(request->peer_login));
1756 blen += base64_encode_final(&ctx, loginbuf+blen);
1757 httpHeaderPutStrf(hdr_out, header, "Basic %.*s", (int)blen, loginbuf);
1758 return;
1759 }
1760
1761 /*
1762 * build request headers and append them to a given MemBuf
1763 * used by buildRequestPrefix()
1764 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
1765 */
1766 void
1767 HttpStateData::httpBuildRequestHeader(HttpRequest * request,
1768 StoreEntry * entry,
1769 const AccessLogEntryPointer &al,
1770 HttpHeader * hdr_out,
1771 const HttpStateFlags &flags)
1772 {
1773 /* building buffer for complex strings */
1774 #define BBUF_SZ (MAX_URL+32)
1775 LOCAL_ARRAY(char, bbuf, BBUF_SZ);
1776 LOCAL_ARRAY(char, ntoabuf, MAX_IPSTRLEN);
1777 const HttpHeader *hdr_in = &request->header;
1778 const HttpHeaderEntry *e = NULL;
1779 HttpHeaderPos pos = HttpHeaderInitPos;
1780 assert (hdr_out->owner == hoRequest);
1781
1782 /* use our IMS header if the cached entry has Last-Modified time */
1783 if (request->lastmod > -1)
1784 hdr_out->putTime(Http::HdrType::IF_MODIFIED_SINCE, request->lastmod);
1785
1786 // Add our own If-None-Match field if the cached entry has a strong ETag.
1787 // copyOneHeaderFromClientsideRequestToUpstreamRequest() adds client ones.
1788 if (request->etag.size() > 0) {
1789 hdr_out->addEntry(new HttpHeaderEntry(Http::HdrType::IF_NONE_MATCH, NULL,
1790 request->etag.termedBuf()));
1791 }
1792
1793 bool we_do_ranges = decideIfWeDoRanges (request);
1794
1795 String strConnection (hdr_in->getList(Http::HdrType::CONNECTION));
1796
1797 while ((e = hdr_in->getEntry(&pos)))
1798 copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, hdr_out, we_do_ranges, flags);
1799
1800 /* Abstraction break: We should interpret multipart/byterange responses
1801 * into offset-length data, and this works around our inability to do so.
1802 */
1803 if (!we_do_ranges && request->multipartRangeRequest()) {
1804 /* don't cache the result */
1805 request->flags.cachable = false;
1806 /* pretend it's not a range request */
1807 request->ignoreRange("want to request the whole object");
1808 request->flags.isRanged = false;
1809 }
1810
1811 /* append Via */
1812 if (Config.onoff.via) {
1813 String strVia;
1814 strVia = hdr_in->getList(Http::HdrType::VIA);
1815 snprintf(bbuf, BBUF_SZ, "%d.%d %s",
1816 request->http_ver.major,
1817 request->http_ver.minor, ThisCache);
1818 strListAdd(&strVia, bbuf, ',');
1819 hdr_out->putStr(Http::HdrType::VIA, strVia.termedBuf());
1820 strVia.clean();
1821 }
1822
1823 if (request->flags.accelerated) {
1824 /* Append Surrogate-Capabilities */
1825 String strSurrogate(hdr_in->getList(Http::HdrType::SURROGATE_CAPABILITY));
1826 #if USE_SQUID_ESI
1827 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"", Config.Accel.surrogate_id);
1828 #else
1829 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0\"", Config.Accel.surrogate_id);
1830 #endif
1831 strListAdd(&strSurrogate, bbuf, ',');
1832 hdr_out->putStr(Http::HdrType::SURROGATE_CAPABILITY, strSurrogate.termedBuf());
1833 }
1834
1835 /** \pre Handle X-Forwarded-For */
1836 if (strcmp(opt_forwarded_for, "delete") != 0) {
1837
1838 String strFwd = hdr_in->getList(Http::HdrType::X_FORWARDED_FOR);
1839
1840 // if we cannot double strFwd size, then it grew past 50% of the limit
1841 if (!strFwd.canGrowBy(strFwd.size())) {
1842 // There is probably a forwarding loop with Via detection disabled.
1843 // If we do nothing, String will assert on overflow soon.
1844 // TODO: Terminate all transactions with huge XFF?
1845 strFwd = "error";
1846
1847 static int warnedCount = 0;
1848 if (warnedCount++ < 100) {
1849 const SBuf url(entry ? SBuf(entry->url()) : request->effectiveRequestUri());
1850 debugs(11, DBG_IMPORTANT, "Warning: likely forwarding loop with " << url);
1851 }
1852 }
1853
1854 if (strcmp(opt_forwarded_for, "on") == 0) {
1855 /** If set to ON - append client IP or 'unknown'. */
1856 if ( request->client_addr.isNoAddr() )
1857 strListAdd(&strFwd, "unknown", ',');
1858 else
1859 strListAdd(&strFwd, request->client_addr.toStr(ntoabuf, MAX_IPSTRLEN), ',');
1860 } else if (strcmp(opt_forwarded_for, "off") == 0) {
1861 /** If set to OFF - append 'unknown'. */
1862 strListAdd(&strFwd, "unknown", ',');
1863 } else if (strcmp(opt_forwarded_for, "transparent") == 0) {
1864 /** If set to TRANSPARENT - pass through unchanged. */
1865 } else if (strcmp(opt_forwarded_for, "truncate") == 0) {
1866 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
1867 if ( request->client_addr.isNoAddr() )
1868 strFwd = "unknown";
1869 else
1870 strFwd = request->client_addr.toStr(ntoabuf, MAX_IPSTRLEN);
1871 }
1872 if (strFwd.size() > 0)
1873 hdr_out->putStr(Http::HdrType::X_FORWARDED_FOR, strFwd.termedBuf());
1874 }
1875 /** If set to DELETE - do not copy through. */
1876
1877 /* append Host if not there already */
1878 if (!hdr_out->has(Http::HdrType::HOST)) {
1879 if (request->peer_domain) {
1880 hdr_out->putStr(Http::HdrType::HOST, request->peer_domain);
1881 } else {
1882 SBuf authority = request->url.authority();
1883 hdr_out->putStr(Http::HdrType::HOST, authority.c_str());
1884 }
1885 }
1886
1887 /* append Authorization if known in URL, not in header and going direct */
1888 if (!hdr_out->has(Http::HdrType::AUTHORIZATION)) {
1889 if (!request->flags.proxying && !request->url.userInfo().isEmpty()) {
1890 static uint8_t result[base64_encode_len(MAX_URL*2)]; // should be big enough for a single URI segment
1891 struct base64_encode_ctx ctx;
1892 base64_encode_init(&ctx);
1893 size_t blen = base64_encode_update(&ctx, result, request->url.userInfo().length(), reinterpret_cast<const uint8_t*>(request->url.userInfo().rawContent()));
1894 blen += base64_encode_final(&ctx, result+blen);
1895 result[blen] = '\0';
1896 if (blen)
1897 httpHeaderPutStrf(hdr_out, Http::HdrType::AUTHORIZATION, "Basic %.*s", (int)blen, result);
1898 }
1899 }
1900
1901 /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */
1902 httpFixupAuthentication(request, hdr_in, hdr_out, flags);
1903
1904 /* append Cache-Control, add max-age if not there already */
1905 {
1906 HttpHdrCc *cc = hdr_in->getCc();
1907
1908 if (!cc)
1909 cc = new HttpHdrCc();
1910
1911 #if 0 /* see bug 2330 */
1912 /* Set no-cache if determined needed but not found */
1913 if (request->flags.nocache)
1914 EBIT_SET(cc->mask, HttpHdrCcType::CC_NO_CACHE);
1915 #endif
1916
1917 /* Add max-age only without no-cache */
1918 if (!cc->hasMaxAge() && !cc->hasNoCache()) {
1919 // XXX: performance regression. c_str() reallocates
1920 SBuf tmp(request->effectiveRequestUri());
1921 cc->maxAge(getMaxAge(entry ? entry->url() : tmp.c_str()));
1922 }
1923
1924 /* Enforce sibling relations */
1925 if (flags.only_if_cached)
1926 cc->onlyIfCached(true);
1927
1928 hdr_out->putCc(cc);
1929
1930 delete cc;
1931 }
1932
1933 /* maybe append Connection: keep-alive */
1934 if (flags.keepalive) {
1935 hdr_out->putStr(Http::HdrType::CONNECTION, "keep-alive");
1936 }
1937
1938 /* append Front-End-Https */
1939 if (flags.front_end_https) {
1940 if (flags.front_end_https == 1 || request->url.getScheme() == AnyP::PROTO_HTTPS)
1941 hdr_out->putStr(Http::HdrType::FRONT_END_HTTPS, "On");
1942 }
1943
1944 if (flags.chunked_request) {
1945 // Do not just copy the original value so that if the client-side
1946 // starts decode other encodings, this code may remain valid.
1947 hdr_out->putStr(Http::HdrType::TRANSFER_ENCODING, "chunked");
1948 }
1949
1950 /* Now mangle the headers. */
1951 httpHdrMangleList(hdr_out, request, al, ROR_REQUEST);
1952
1953 strConnection.clean();
1954 }
1955
1956 /**
1957 * Decides whether a particular header may be cloned from the received Clients request
1958 * to our outgoing fetch request.
1959 */
1960 void
1961 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest * request, HttpHeader * hdr_out, const int we_do_ranges, const HttpStateFlags &flags)
1962 {
1963 debugs(11, 5, "httpBuildRequestHeader: " << e->name << ": " << e->value );
1964
1965 switch (e->id) {
1966
1967 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */
1968
1969 case Http::HdrType::PROXY_AUTHORIZATION:
1970 /** \par Proxy-Authorization:
1971 * Only pass on proxy authentication to peers for which
1972 * authentication forwarding is explicitly enabled
1973 */
1974 if (!flags.originpeer && flags.proxying && request->peer_login &&
1975 (strcmp(request->peer_login, "PASS") == 0 ||
1976 strcmp(request->peer_login, "PROXYPASS") == 0 ||
1977 strcmp(request->peer_login, "PASSTHRU") == 0)) {
1978 hdr_out->addEntry(e->clone());
1979 }
1980 break;
1981
1982 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */
1983
1984 case Http::HdrType::CONNECTION: /** \par Connection: */
1985 case Http::HdrType::TE: /** \par TE: */
1986 case Http::HdrType::KEEP_ALIVE: /** \par Keep-Alive: */
1987 case Http::HdrType::PROXY_AUTHENTICATE: /** \par Proxy-Authenticate: */
1988 case Http::HdrType::TRAILER: /** \par Trailer: */
1989 case Http::HdrType::UPGRADE: /** \par Upgrade: */
1990 case Http::HdrType::TRANSFER_ENCODING: /** \par Transfer-Encoding: */
1991 break;
1992
1993 /** \par OTHER headers I haven't bothered to track down yet. */
1994
1995 case Http::HdrType::AUTHORIZATION:
1996 /** \par WWW-Authorization:
1997 * Pass on WWW authentication */
1998
1999 if (!flags.originpeer) {
2000 hdr_out->addEntry(e->clone());
2001 } else {
2002 /** \note In accelerators, only forward authentication if enabled
2003 * (see also httpFixupAuthentication for special cases)
2004 */
2005 if (request->peer_login &&
2006 (strcmp(request->peer_login, "PASS") == 0 ||
2007 strcmp(request->peer_login, "PASSTHRU") == 0 ||
2008 strcmp(request->peer_login, "PROXYPASS") == 0)) {
2009 hdr_out->addEntry(e->clone());
2010 }
2011 }
2012
2013 break;
2014
2015 case Http::HdrType::HOST:
2016 /** \par Host:
2017 * Normally Squid rewrites the Host: header.
2018 * However, there is one case when we don't: If the URL
2019 * went through our redirector and the admin configured
2020 * 'redir_rewrites_host' to be off.
2021 */
2022 if (request->peer_domain)
2023 hdr_out->putStr(Http::HdrType::HOST, request->peer_domain);
2024 else if (request->flags.redirected && !Config.onoff.redir_rewrites_host)
2025 hdr_out->addEntry(e->clone());
2026 else {
2027 SBuf authority = request->url.authority();
2028 hdr_out->putStr(Http::HdrType::HOST, authority.c_str());
2029 }
2030
2031 break;
2032
2033 case Http::HdrType::IF_MODIFIED_SINCE:
2034 /** \par If-Modified-Since:
2035 * append unless we added our own,
2036 * but only if cache_miss_revalidate is enabled, or
2037 * the request is not cacheable, or
2038 * the request contains authentication credentials.
2039 * \note at most one client's If-Modified-Since header can pass through
2040 */
2041 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2042 if (hdr_out->has(Http::HdrType::IF_MODIFIED_SINCE))
2043 break;
2044 else if (Config.onoff.cache_miss_revalidate || !request->flags.cachable || request->flags.auth)
2045 hdr_out->addEntry(e->clone());
2046 break;
2047
2048 case Http::HdrType::IF_NONE_MATCH:
2049 /** \par If-None-Match:
2050 * append if the wildcard '*' special case value is present, or
2051 * cache_miss_revalidate is disabled, or
2052 * the request is not cacheable in this proxy, or
2053 * the request contains authentication credentials.
2054 * \note this header lists a set of responses for the server to elide sending. Squid added values are extending that set.
2055 */
2056 // XXX: need to check and cleanup the auth case so cacheable auth requests get cached.
2057 if (hdr_out->hasListMember(Http::HdrType::IF_MATCH, "*", ',') || Config.onoff.cache_miss_revalidate || !request->flags.cachable || request->flags.auth)
2058 hdr_out->addEntry(e->clone());
2059 break;
2060
2061 case Http::HdrType::MAX_FORWARDS:
2062 /** \par Max-Forwards:
2063 * pass only on TRACE or OPTIONS requests */
2064 if (request->method == Http::METHOD_TRACE || request->method == Http::METHOD_OPTIONS) {
2065 const int64_t hops = e->getInt64();
2066
2067 if (hops > 0)
2068 hdr_out->putInt64(Http::HdrType::MAX_FORWARDS, hops - 1);
2069 }
2070
2071 break;
2072
2073 case Http::HdrType::VIA:
2074 /** \par Via:
2075 * If Via is disabled then forward any received header as-is.
2076 * Otherwise leave for explicit updated addition later. */
2077
2078 if (!Config.onoff.via)
2079 hdr_out->addEntry(e->clone());
2080
2081 break;
2082
2083 case Http::HdrType::RANGE:
2084
2085 case Http::HdrType::IF_RANGE:
2086
2087 case Http::HdrType::REQUEST_RANGE:
2088 /** \par Range:, If-Range:, Request-Range:
2089 * Only pass if we accept ranges */
2090 if (!we_do_ranges)
2091 hdr_out->addEntry(e->clone());
2092
2093 break;
2094
2095 case Http::HdrType::PROXY_CONNECTION: // SHOULD ignore. But doing so breaks things.
2096 break;
2097
2098 case Http::HdrType::CONTENT_LENGTH:
2099 // pass through unless we chunk; also, keeping this away from default
2100 // prevents request smuggling via Connection: Content-Length tricks
2101 if (!flags.chunked_request)
2102 hdr_out->addEntry(e->clone());
2103 break;
2104
2105 case Http::HdrType::X_FORWARDED_FOR:
2106
2107 case Http::HdrType::CACHE_CONTROL:
2108 /** \par X-Forwarded-For:, Cache-Control:
2109 * handled specially by Squid, so leave off for now.
2110 * append these after the loop if needed */
2111 break;
2112
2113 case Http::HdrType::FRONT_END_HTTPS:
2114 /** \par Front-End-Https:
2115 * Pass thru only if peer is configured with front-end-https */
2116 if (!flags.front_end_https)
2117 hdr_out->addEntry(e->clone());
2118
2119 break;
2120
2121 default:
2122 /** \par default.
2123 * pass on all other header fields
2124 * which are NOT listed by the special Connection: header. */
2125
2126 if (strConnection.size()>0 && strListIsMember(&strConnection, e->name.termedBuf(), ',')) {
2127 debugs(11, 2, "'" << e->name << "' header cropped by Connection: definition");
2128 return;
2129 }
2130
2131 hdr_out->addEntry(e->clone());
2132 }
2133 }
2134
2135 bool
2136 HttpStateData::decideIfWeDoRanges (HttpRequest * request)
2137 {
2138 bool result = true;
2139 /* decide if we want to do Ranges ourselves
2140 * and fetch the whole object now)
2141 * We want to handle Ranges ourselves iff
2142 * - we can actually parse client Range specs
2143 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
2144 * - reply will be cachable
2145 * (If the reply will be uncachable we have to throw it away after
2146 * serving this request, so it is better to forward ranges to
2147 * the server and fetch only the requested content)
2148 */
2149
2150 int64_t roffLimit = request->getRangeOffsetLimit();
2151
2152 if (NULL == request->range || !request->flags.cachable
2153 || request->range->offsetLimitExceeded(roffLimit) || request->flags.connectionAuth)
2154 result = false;
2155
2156 debugs(11, 8, "decideIfWeDoRanges: range specs: " <<
2157 request->range << ", cachable: " <<
2158 request->flags.cachable << "; we_do_ranges: " << result);
2159
2160 return result;
2161 }
2162
2163 /* build request prefix and append it to a given MemBuf;
2164 * return the length of the prefix */
2165 mb_size_t
2166 HttpStateData::buildRequestPrefix(MemBuf * mb)
2167 {
2168 const int offset = mb->size;
2169 /* Uses a local httpver variable to print the HTTP label
2170 * since the HttpRequest may have an older version label.
2171 * XXX: This could create protocol bugs as the headers sent and
2172 * flow control should all be based on the HttpRequest version
2173 * not the one we are sending. Needs checking.
2174 */
2175 const AnyP::ProtocolVersion httpver = Http::ProtocolVersion();
2176 const SBuf url(_peer && !_peer->options.originserver ? request->effectiveRequestUri() : request->url.path());
2177 mb->appendf(SQUIDSBUFPH " " SQUIDSBUFPH " %s/%d.%d\r\n",
2178 SQUIDSBUFPRINT(request->method.image()),
2179 SQUIDSBUFPRINT(url),
2180 AnyP::ProtocolType_str[httpver.protocol],
2181 httpver.major,httpver.minor);
2182 /* build and pack headers */
2183 {
2184 HttpHeader hdr(hoRequest);
2185 httpBuildRequestHeader(request, entry, fwd->al, &hdr, flags);
2186
2187 if (request->flags.pinned && request->flags.connectionAuth)
2188 request->flags.authSent = true;
2189 else if (hdr.has(Http::HdrType::AUTHORIZATION))
2190 request->flags.authSent = true;
2191
2192 hdr.packInto(mb);
2193 hdr.clean();
2194 }
2195 /* append header terminator */
2196 mb->append(crlf, 2);
2197 return mb->size - offset;
2198 }
2199
2200 /* This will be called when connect completes. Write request. */
2201 bool
2202 HttpStateData::sendRequest()
2203 {
2204 MemBuf mb;
2205
2206 debugs(11, 5, HERE << serverConnection << ", request " << request << ", this " << this << ".");
2207
2208 if (!Comm::IsConnOpen(serverConnection)) {
2209 debugs(11,3, HERE << "cannot send request to closing " << serverConnection);
2210 assert(closeHandler != NULL);
2211 return false;
2212 }
2213
2214 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
2215 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
2216 TimeoutDialer, this, HttpStateData::httpTimeout);
2217 commSetConnTimeout(serverConnection, Config.Timeout.lifetime, timeoutCall);
2218 flags.do_next_read = true;
2219 maybeReadVirginBody();
2220
2221 if (request->body_pipe != NULL) {
2222 if (!startRequestBodyFlow()) // register to receive body data
2223 return false;
2224 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2225 requestSender = JobCallback(11,5,
2226 Dialer, this, HttpStateData::sentRequestBody);
2227
2228 Must(!flags.chunked_request);
2229 // use chunked encoding if we do not know the length
2230 if (request->content_length < 0)
2231 flags.chunked_request = true;
2232 } else {
2233 assert(!requestBodySource);
2234 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2235 requestSender = JobCallback(11,5,
2236 Dialer, this, HttpStateData::wroteLast);
2237 }
2238
2239 flags.originpeer = (_peer != NULL && _peer->options.originserver);
2240 flags.proxying = (_peer != NULL && !flags.originpeer);
2241
2242 /*
2243 * Is keep-alive okay for all request methods?
2244 */
2245 if (request->flags.mustKeepalive)
2246 flags.keepalive = true;
2247 else if (request->flags.pinned)
2248 flags.keepalive = request->persistent();
2249 else if (!Config.onoff.server_pconns)
2250 flags.keepalive = false;
2251 else if (_peer == NULL)
2252 flags.keepalive = true;
2253 else if (_peer->stats.n_keepalives_sent < 10)
2254 flags.keepalive = true;
2255 else if ((double) _peer->stats.n_keepalives_recv /
2256 (double) _peer->stats.n_keepalives_sent > 0.50)
2257 flags.keepalive = true;
2258
2259 if (_peer) {
2260 /*The old code here was
2261 if (neighborType(_peer, request->url) == PEER_SIBLING && ...
2262 which is equivalent to:
2263 if (neighborType(_peer, URL()) == PEER_SIBLING && ...
2264 or better:
2265 if (((_peer->type == PEER_MULTICAST && p->options.mcast_siblings) ||
2266 _peer->type == PEER_SIBLINGS ) && _peer->options.allow_miss)
2267 flags.only_if_cached = 1;
2268
2269 But I suppose it was a bug
2270 */
2271 if (neighborType(_peer, request->url) == PEER_SIBLING && !_peer->options.allow_miss)
2272 flags.only_if_cached = true;
2273
2274 flags.front_end_https = _peer->front_end_https;
2275 }
2276
2277 mb.init();
2278 request->peer_host=_peer?_peer->host:NULL;
2279 buildRequestPrefix(&mb);
2280
2281 debugs(11, 2, "HTTP Server " << serverConnection);
2282 debugs(11, 2, "HTTP Server REQUEST:\n---------\n" << mb.buf << "\n----------");
2283
2284 Comm::Write(serverConnection, &mb, requestSender);
2285 return true;
2286 }
2287
2288 bool
2289 HttpStateData::getMoreRequestBody(MemBuf &buf)
2290 {
2291 // parent's implementation can handle the no-encoding case
2292 if (!flags.chunked_request)
2293 return Client::getMoreRequestBody(buf);
2294
2295 MemBuf raw;
2296
2297 Must(requestBodySource != NULL);
2298 if (!requestBodySource->getMoreData(raw))
2299 return false; // no request body bytes to chunk yet
2300
2301 // optimization: pre-allocate buffer size that should be enough
2302 const mb_size_t rawDataSize = raw.contentSize();
2303 // we may need to send: hex-chunk-size CRLF raw-data CRLF last-chunk
2304 buf.init(16 + 2 + rawDataSize + 2 + 5, raw.max_capacity);
2305
2306 buf.appendf("%x\r\n", static_cast<unsigned int>(rawDataSize));
2307 buf.append(raw.content(), rawDataSize);
2308 buf.append("\r\n", 2);
2309
2310 Must(rawDataSize > 0); // we did not accidently created last-chunk above
2311
2312 // Do not send last-chunk unless we successfully received everything
2313 if (receivedWholeRequestBody) {
2314 Must(!flags.sentLastChunk);
2315 flags.sentLastChunk = true;
2316 buf.append("0\r\n\r\n", 5);
2317 }
2318
2319 return true;
2320 }
2321
2322 void
2323 httpStart(FwdState *fwd)
2324 {
2325 debugs(11, 3, fwd->request->method << ' ' << fwd->entry->url());
2326 AsyncJob::Start(new HttpStateData(fwd));
2327 }
2328
2329 void
2330 HttpStateData::start()
2331 {
2332 if (!sendRequest()) {
2333 debugs(11, 3, "httpStart: aborted");
2334 mustStop("HttpStateData::start failed");
2335 return;
2336 }
2337
2338 ++ statCounter.server.all.requests;
2339 ++ statCounter.server.http.requests;
2340
2341 /*
2342 * We used to set the read timeout here, but not any more.
2343 * Now its set in httpSendComplete() after the full request,
2344 * including request body, has been written to the server.
2345 */
2346 }
2347
2348 /// if broken posts are enabled for the request, try to fix and return true
2349 bool
2350 HttpStateData::finishingBrokenPost()
2351 {
2352 #if USE_HTTP_VIOLATIONS
2353 if (!Config.accessList.brokenPosts) {
2354 debugs(11, 5, HERE << "No brokenPosts list");
2355 return false;
2356 }
2357
2358 ACLFilledChecklist ch(Config.accessList.brokenPosts, originalRequest(), NULL);
2359 if (ch.fastCheck() != ACCESS_ALLOWED) {
2360 debugs(11, 5, HERE << "didn't match brokenPosts");
2361 return false;
2362 }
2363
2364 if (!Comm::IsConnOpen(serverConnection)) {
2365 debugs(11, 3, HERE << "ignoring broken POST for closed " << serverConnection);
2366 assert(closeHandler != NULL);
2367 return true; // prevent caller from proceeding as if nothing happened
2368 }
2369
2370 debugs(11, 3, "finishingBrokenPost: fixing broken POST");
2371 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2372 requestSender = JobCallback(11,5,
2373 Dialer, this, HttpStateData::wroteLast);
2374 Comm::Write(serverConnection, "\r\n", 2, requestSender, NULL);
2375 return true;
2376 #else
2377 return false;
2378 #endif /* USE_HTTP_VIOLATIONS */
2379 }
2380
2381 /// if needed, write last-chunk to end the request body and return true
2382 bool
2383 HttpStateData::finishingChunkedRequest()
2384 {
2385 if (flags.sentLastChunk) {
2386 debugs(11, 5, HERE << "already sent last-chunk");
2387 return false;
2388 }
2389
2390 Must(receivedWholeRequestBody); // or we should not be sending last-chunk
2391 flags.sentLastChunk = true;
2392
2393 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2394 requestSender = JobCallback(11,5, Dialer, this, HttpStateData::wroteLast);
2395 Comm::Write(serverConnection, "0\r\n\r\n", 5, requestSender, NULL);
2396 return true;
2397 }
2398
2399 void
2400 HttpStateData::doneSendingRequestBody()
2401 {
2402 Client::doneSendingRequestBody();
2403 debugs(11,5, HERE << serverConnection);
2404
2405 // do we need to write something after the last body byte?
2406 if (flags.chunked_request && finishingChunkedRequest())
2407 return;
2408 if (!flags.chunked_request && finishingBrokenPost())
2409 return;
2410
2411 sendComplete();
2412 }
2413
2414 // more origin request body data is available
2415 void
2416 HttpStateData::handleMoreRequestBodyAvailable()
2417 {
2418 if (eof || !Comm::IsConnOpen(serverConnection)) {
2419 // XXX: we should check this condition in other callbacks then!
2420 // TODO: Check whether this can actually happen: We should unsubscribe
2421 // as a body consumer when the above condition(s) are detected.
2422 debugs(11, DBG_IMPORTANT, HERE << "Transaction aborted while reading HTTP body");
2423 return;
2424 }
2425
2426 assert(requestBodySource != NULL);
2427
2428 if (requestBodySource->buf().hasContent()) {
2429 // XXX: why does not this trigger a debug message on every request?
2430
2431 if (flags.headers_parsed && !flags.abuse_detected) {
2432 flags.abuse_detected = true;
2433 debugs(11, DBG_IMPORTANT, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << request->client_addr << "' -> '" << entry->url() << "'" );
2434
2435 if (virginReply()->sline.status() == Http::scInvalidHeader) {
2436 closeServer();
2437 mustStop("HttpStateData::handleMoreRequestBodyAvailable");
2438 return;
2439 }
2440 }
2441 }
2442
2443 HttpStateData::handleMoreRequestBodyAvailable();
2444 }
2445
2446 // premature end of the request body
2447 void
2448 HttpStateData::handleRequestBodyProducerAborted()
2449 {
2450 Client::handleRequestBodyProducerAborted();
2451 if (entry->isEmpty()) {
2452 debugs(11, 3, "request body aborted: " << serverConnection);
2453 // We usually get here when ICAP REQMOD aborts during body processing.
2454 // We might also get here if client-side aborts, but then our response
2455 // should not matter because either client-side will provide its own or
2456 // there will be no response at all (e.g., if the the client has left).
2457 ErrorState *err = new ErrorState(ERR_ICAP_FAILURE, Http::scInternalServerError, fwd->request);
2458 err->detailError(ERR_DETAIL_SRV_REQMOD_REQ_BODY);
2459 fwd->fail(err);
2460 }
2461
2462 abortTransaction("request body producer aborted");
2463 }
2464
2465 // called when we wrote request headers(!) or a part of the body
2466 void
2467 HttpStateData::sentRequestBody(const CommIoCbParams &io)
2468 {
2469 if (io.size > 0)
2470 statCounter.server.http.kbytes_out += io.size;
2471
2472 Client::sentRequestBody(io);
2473 }
2474
2475 void
2476 HttpStateData::abortAll(const char *reason)
2477 {
2478 debugs(11,5, HERE << "aborting transaction for " << reason <<
2479 "; " << serverConnection << ", this " << this);
2480 mustStop(reason);
2481 }
2482