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