]> git.ipfire.org Git - thirdparty/squid.git/blob - src/http.cc
Policy: use USE_* from code wrappers and ENABLE_* for conditionals.
[thirdparty/squid.git] / src / http.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 11 Hypertext Transfer Protocol (HTTP)
6 * AUTHOR: Harvest Derived
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
11 * Squid is the result of efforts by numerous individuals from
12 * the Internet community; see the CONTRIBUTORS file for full
13 * details. Many organizations have provided support for Squid's
14 * development; see the SPONSORS file for full details. Squid is
15 * Copyrighted (C) 2001 by the Regents of the University of
16 * California; see the COPYRIGHT file for full details. Squid
17 * incorporates software developed and/or copyrighted by other
18 * sources; see the CREDITS file for full details.
19 *
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
33 *
34 */
35
36 /*
37 * Anonymizing patch by lutz@as-node.jena.thur.de
38 * have a look into http-anon.c to get more informations.
39 */
40
41 #include "squid.h"
42
43 #include "acl/FilledChecklist.h"
44 #include "auth/UserRequest.h"
45 #include "base/AsyncJobCalls.h"
46 #include "base/TextException.h"
47 #include "base64.h"
48 #include "comm/Write.h"
49 #if USE_DELAY_POOLS
50 #include "DelayPools.h"
51 #endif
52 #include "errorpage.h"
53 #include "http.h"
54 #include "HttpControlMsg.h"
55 #include "HttpHdrContRange.h"
56 #include "HttpHdrSc.h"
57 #include "HttpHdrScTarget.h"
58 #include "HttpReply.h"
59 #include "HttpRequest.h"
60 #include "MemBuf.h"
61 #include "MemObject.h"
62 #include "protos.h"
63 #include "rfc1738.h"
64 #include "SquidTime.h"
65 #include "Store.h"
66
67
68 #define SQUID_ENTER_THROWING_CODE() try {
69 #define SQUID_EXIT_THROWING_CODE(status) \
70 status = true; \
71 } \
72 catch (const std::exception &e) { \
73 debugs (11, 1, "Exception error:" << e.what()); \
74 status = false; \
75 }
76
77 CBDATA_CLASS_INIT(HttpStateData);
78
79 static const char *const crlf = "\r\n";
80
81 static void httpMaybeRemovePublic(StoreEntry *, http_status);
82 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, HttpRequest * request, const HttpRequest * orig_request,
83 HttpHeader * hdr_out, const int we_do_ranges, const http_state_flags);
84
85 HttpStateData::HttpStateData(FwdState *theFwdState) : AsyncJob("HttpStateData"), ServerStateData(theFwdState),
86 lastChunk(0), header_bytes_read(0), reply_bytes_read(0),
87 body_bytes_truncated(0), httpChunkDecoder(NULL)
88 {
89 debugs(11,5,HERE << "HttpStateData " << this << " created");
90 ignoreCacheControl = false;
91 surrogateNoStore = false;
92 fd = fwd->server_fd;
93 readBuf = new MemBuf;
94 readBuf->init();
95 orig_request = HTTPMSGLOCK(fwd->request);
96
97 // reset peer response time stats for %<pt
98 orig_request->hier.peer_http_request_sent.tv_sec = 0;
99 orig_request->hier.peer_http_request_sent.tv_usec = 0;
100
101 if (fwd->servers)
102 _peer = fwd->servers->_peer; /* might be NULL */
103
104 if (_peer) {
105 const char *url;
106
107 if (_peer->options.originserver)
108 url = orig_request->urlpath.termedBuf();
109 else
110 url = entry->url();
111
112 HttpRequest * proxy_req = new HttpRequest(orig_request->method,
113 orig_request->protocol, url);
114
115 proxy_req->SetHost(_peer->host);
116
117 proxy_req->port = _peer->http_port;
118
119 proxy_req->flags = orig_request->flags;
120
121 proxy_req->lastmod = orig_request->lastmod;
122
123 proxy_req->flags.proxying = 1;
124
125 HTTPMSGUNLOCK(request);
126
127 request = HTTPMSGLOCK(proxy_req);
128
129 /*
130 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
131 * We might end up getting the object from somewhere else if,
132 * for example, the request to this neighbor fails.
133 */
134 if (_peer->options.proxy_only)
135 entry->releaseRequest();
136
137 #if USE_DELAY_POOLS
138 entry->setNoDelay(_peer->options.no_delay);
139 #endif
140 }
141
142 /*
143 * register the handler to free HTTP state data when the FD closes
144 */
145 typedef CommCbMemFunT<HttpStateData, CommCloseCbParams> Dialer;
146 closeHandler = JobCallback(9, 5,
147 Dialer, this, HttpStateData::httpStateConnClosed);
148 comm_add_close_handler(fd, closeHandler);
149 }
150
151 HttpStateData::~HttpStateData()
152 {
153 /*
154 * don't forget that ~ServerStateData() gets called automatically
155 */
156
157 if (!readBuf->isNull())
158 readBuf->clean();
159
160 delete readBuf;
161
162 if (httpChunkDecoder)
163 delete httpChunkDecoder;
164
165 HTTPMSGUNLOCK(orig_request);
166
167 debugs(11,5, HERE << "HttpStateData " << this << " destroyed; FD " << fd);
168 }
169
170 int
171 HttpStateData::dataDescriptor() const
172 {
173 return fd;
174 }
175 /*
176 static void
177 httpStateFree(int fd, void *data)
178 {
179 HttpStateData *httpState = static_cast<HttpStateData *>(data);
180 debugs(11, 5, "httpStateFree: FD " << fd << ", httpState=" << data);
181 delete httpState;
182 }*/
183
184 void
185 HttpStateData::httpStateConnClosed(const CommCloseCbParams &params)
186 {
187 debugs(11, 5, "httpStateFree: FD " << params.fd << ", httpState=" << params.data);
188 deleteThis("HttpStateData::httpStateConnClosed");
189 }
190
191 int
192 httpCachable(const HttpRequestMethod& method)
193 {
194 /* GET and HEAD are cachable. Others are not. */
195
196 // TODO: replase to HttpRequestMethod::isCachable() ?
197 if (method != METHOD_GET && method != METHOD_HEAD)
198 return 0;
199
200 /* else cachable */
201 return 1;
202 }
203
204 void
205 HttpStateData::httpTimeout(const CommTimeoutCbParams &params)
206 {
207 debugs(11, 4, "httpTimeout: FD " << fd << ": '" << entry->url() << "'" );
208
209 if (entry->store_status == STORE_PENDING) {
210 fwd->fail(errorCon(ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT, fwd->request));
211 }
212
213 comm_close(fd);
214 }
215
216 static void
217 httpMaybeRemovePublic(StoreEntry * e, http_status status)
218 {
219 int remove = 0;
220 int forbidden = 0;
221 StoreEntry *pe;
222
223 if (!EBIT_TEST(e->flags, KEY_PRIVATE))
224 return;
225
226 switch (status) {
227
228 case HTTP_OK:
229
230 case HTTP_NON_AUTHORITATIVE_INFORMATION:
231
232 case HTTP_MULTIPLE_CHOICES:
233
234 case HTTP_MOVED_PERMANENTLY:
235
236 case HTTP_MOVED_TEMPORARILY:
237
238 case HTTP_GONE:
239
240 case HTTP_NOT_FOUND:
241 remove = 1;
242
243 break;
244
245 case HTTP_FORBIDDEN:
246
247 case HTTP_METHOD_NOT_ALLOWED:
248 forbidden = 1;
249
250 break;
251
252 #if WORK_IN_PROGRESS
253
254 case HTTP_UNAUTHORIZED:
255 forbidden = 1;
256
257 break;
258
259 #endif
260
261 default:
262 #if QUESTIONABLE
263 /*
264 * Any 2xx response should eject previously cached entities...
265 */
266
267 if (status >= 200 && status < 300)
268 remove = 1;
269
270 #endif
271
272 break;
273 }
274
275 if (!remove && !forbidden)
276 return;
277
278 assert(e->mem_obj);
279
280 if (e->mem_obj->request)
281 pe = storeGetPublicByRequest(e->mem_obj->request);
282 else
283 pe = storeGetPublic(e->mem_obj->url, e->mem_obj->method);
284
285 if (pe != NULL) {
286 assert(e != pe);
287 #if USE_HTCP
288 neighborsHtcpClear(e, NULL, e->mem_obj->request, e->mem_obj->method, HTCP_CLR_INVALIDATION);
289 #endif
290 pe->release();
291 }
292
293 /** \par
294 * Also remove any cached HEAD response in case the object has
295 * changed.
296 */
297 if (e->mem_obj->request)
298 pe = storeGetPublicByRequestMethod(e->mem_obj->request, METHOD_HEAD);
299 else
300 pe = storeGetPublic(e->mem_obj->url, METHOD_HEAD);
301
302 if (pe != NULL) {
303 assert(e != pe);
304 #if USE_HTCP
305 neighborsHtcpClear(e, NULL, e->mem_obj->request, HttpRequestMethod(METHOD_HEAD), HTCP_CLR_INVALIDATION);
306 #endif
307 pe->release();
308 }
309 }
310
311 void
312 HttpStateData::processSurrogateControl(HttpReply *reply)
313 {
314 if (request->flags.accelerated && reply->surrogate_control) {
315 HttpHdrScTarget *sctusable = httpHdrScGetMergedTarget(reply->surrogate_control, Config.Accel.surrogate_id);
316
317 if (sctusable) {
318 if (EBIT_TEST(sctusable->mask, SC_NO_STORE) ||
319 (Config.onoff.surrogate_is_remote
320 && EBIT_TEST(sctusable->mask, SC_NO_STORE_REMOTE))) {
321 surrogateNoStore = true;
322 entry->makePrivate();
323 }
324
325 /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
326 * accelerated request or not...
327 * Still, this is an abstraction breach. - RC
328 */
329 if (sctusable->max_age != -1) {
330 if (sctusable->max_age < sctusable->max_stale)
331 reply->expires = reply->date + sctusable->max_age;
332 else
333 reply->expires = reply->date + sctusable->max_stale;
334
335 /* And update the timestamps */
336 entry->timestampsSet();
337 }
338
339 /* We ignore cache-control directives as per the Surrogate specification */
340 ignoreCacheControl = true;
341
342 httpHdrScTargetDestroy(sctusable);
343 }
344 }
345 }
346
347 int
348 HttpStateData::cacheableReply()
349 {
350 HttpReply const *rep = finalReply();
351 HttpHeader const *hdr = &rep->header;
352 const int cc_mask = (rep->cache_control) ? rep->cache_control->mask : 0;
353 const char *v;
354 #if USE_HTTP_VIOLATIONS
355
356 const refresh_t *R = NULL;
357
358 /* This strange looking define first looks up the refresh pattern
359 * and then checks if the specified flag is set. The main purpose
360 * of this is to simplify the refresh pattern lookup and USE_HTTP_VIOLATIONS
361 * condition
362 */
363 #define REFRESH_OVERRIDE(flag) \
364 ((R = (R ? R : refreshLimits(entry->mem_obj->url))) , \
365 (R && R->flags.flag))
366 #else
367 #define REFRESH_OVERRIDE(flag) 0
368 #endif
369
370 if (surrogateNoStore)
371 return 0;
372
373 // RFC 2616: do not cache replies to responses with no-store CC directive
374 if (request && request->cache_control &&
375 EBIT_TEST(request->cache_control->mask, CC_NO_STORE) &&
376 !REFRESH_OVERRIDE(ignore_no_store))
377 return 0;
378
379 if (!ignoreCacheControl) {
380 if (EBIT_TEST(cc_mask, CC_PRIVATE)) {
381 if (!REFRESH_OVERRIDE(ignore_private))
382 return 0;
383 }
384
385 if (EBIT_TEST(cc_mask, CC_NO_CACHE)) {
386 if (!REFRESH_OVERRIDE(ignore_no_cache))
387 return 0;
388 }
389
390 if (EBIT_TEST(cc_mask, CC_NO_STORE)) {
391 if (!REFRESH_OVERRIDE(ignore_no_store))
392 return 0;
393 }
394 }
395
396 if (request->flags.auth || request->flags.auth_sent) {
397 /*
398 * Responses to requests with authorization may be cached
399 * only if a Cache-Control: public reply header is present.
400 * RFC 2068, sec 14.9.4
401 */
402
403 if (!EBIT_TEST(cc_mask, CC_PUBLIC)) {
404 if (!REFRESH_OVERRIDE(ignore_auth))
405 return 0;
406 }
407 }
408
409 /* Pragma: no-cache in _replies_ is not documented in HTTP,
410 * but servers like "Active Imaging Webcast/2.0" sure do use it */
411 if (hdr->has(HDR_PRAGMA)) {
412 String s = hdr->getList(HDR_PRAGMA);
413 const int no_cache = strListIsMember(&s, "no-cache", ',');
414 s.clean();
415
416 if (no_cache) {
417 if (!REFRESH_OVERRIDE(ignore_no_cache))
418 return 0;
419 }
420 }
421
422 /*
423 * The "multipart/x-mixed-replace" content type is used for
424 * continuous push replies. These are generally dynamic and
425 * probably should not be cachable
426 */
427 if ((v = hdr->getStr(HDR_CONTENT_TYPE)))
428 if (!strncasecmp(v, "multipart/x-mixed-replace", 25))
429 return 0;
430
431 switch (rep->sline.status) {
432 /* Responses that are cacheable */
433
434 case HTTP_OK:
435
436 case HTTP_NON_AUTHORITATIVE_INFORMATION:
437
438 case HTTP_MULTIPLE_CHOICES:
439
440 case HTTP_MOVED_PERMANENTLY:
441
442 case HTTP_GONE:
443 /*
444 * Don't cache objects that need to be refreshed on next request,
445 * unless we know how to refresh it.
446 */
447
448 if (!refreshIsCachable(entry) && !REFRESH_OVERRIDE(store_stale)) {
449 debugs(22, 3, "refreshIsCachable() returned non-cacheable..");
450 return 0;
451 } else
452 return 1;
453
454 /* NOTREACHED */
455 break;
456
457 /* Responses that only are cacheable if the server says so */
458
459 case HTTP_MOVED_TEMPORARILY:
460 case HTTP_TEMPORARY_REDIRECT:
461 if (rep->expires > rep->date && rep->date > 0)
462 return 1;
463 else
464 return 0;
465
466 /* NOTREACHED */
467 break;
468
469 /* Errors can be negatively cached */
470
471 case HTTP_NO_CONTENT:
472
473 case HTTP_USE_PROXY:
474
475 case HTTP_BAD_REQUEST:
476
477 case HTTP_FORBIDDEN:
478
479 case HTTP_NOT_FOUND:
480
481 case HTTP_METHOD_NOT_ALLOWED:
482
483 case HTTP_REQUEST_URI_TOO_LARGE:
484
485 case HTTP_INTERNAL_SERVER_ERROR:
486
487 case HTTP_NOT_IMPLEMENTED:
488
489 case HTTP_BAD_GATEWAY:
490
491 case HTTP_SERVICE_UNAVAILABLE:
492
493 case HTTP_GATEWAY_TIMEOUT:
494 return -1;
495
496 /* NOTREACHED */
497 break;
498
499 /* Some responses can never be cached */
500
501 case HTTP_PARTIAL_CONTENT: /* Not yet supported */
502
503 case HTTP_SEE_OTHER:
504
505 case HTTP_NOT_MODIFIED:
506
507 case HTTP_UNAUTHORIZED:
508
509 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
510
511 case HTTP_INVALID_HEADER: /* Squid header parsing error */
512
513 case HTTP_HEADER_TOO_LARGE:
514
515 case HTTP_PAYMENT_REQUIRED:
516 case HTTP_NOT_ACCEPTABLE:
517 case HTTP_REQUEST_TIMEOUT:
518 case HTTP_CONFLICT:
519 case HTTP_LENGTH_REQUIRED:
520 case HTTP_PRECONDITION_FAILED:
521 case HTTP_REQUEST_ENTITY_TOO_LARGE:
522 case HTTP_UNSUPPORTED_MEDIA_TYPE:
523 case HTTP_UNPROCESSABLE_ENTITY:
524 case HTTP_LOCKED:
525 case HTTP_FAILED_DEPENDENCY:
526 case HTTP_INSUFFICIENT_STORAGE:
527 case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE:
528 case HTTP_EXPECTATION_FAILED:
529
530 return 0;
531
532 default:
533 /* RFC 2616 section 6.1.1: an unrecognized response MUST NOT be cached. */
534 debugs (11, 3, HERE << "Unknown HTTP status code " << rep->sline.status << ". Not cacheable.");
535
536 return 0;
537
538 /* NOTREACHED */
539 break;
540 }
541
542 /* NOTREACHED */
543 }
544
545 /*
546 * For Vary, store the relevant request headers as
547 * virtual headers in the reply
548 * Returns false if the variance cannot be stored
549 */
550 const char *
551 httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
552 {
553 String vary, hdr;
554 const char *pos = NULL;
555 const char *item;
556 const char *value;
557 int ilen;
558 static String vstr;
559
560 vstr.clean();
561 vary = reply->header.getList(HDR_VARY);
562
563 while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
564 char *name = (char *)xmalloc(ilen + 1);
565 xstrncpy(name, item, ilen + 1);
566 Tolower(name);
567
568 if (strcmp(name, "*") == 0) {
569 /* Can not handle "Vary: *" withtout ETag support */
570 safe_free(name);
571 vstr.clean();
572 break;
573 }
574
575 strListAdd(&vstr, name, ',');
576 hdr = request->header.getByName(name);
577 safe_free(name);
578 value = hdr.termedBuf();
579
580 if (value) {
581 value = rfc1738_escape_part(value);
582 vstr.append("=\"", 2);
583 vstr.append(value);
584 vstr.append("\"", 1);
585 }
586
587 hdr.clean();
588 }
589
590 vary.clean();
591 #if X_ACCELERATOR_VARY
592
593 pos = NULL;
594 vary = reply->header.getList(HDR_X_ACCELERATOR_VARY);
595
596 while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
597 char *name = (char *)xmalloc(ilen + 1);
598 xstrncpy(name, item, ilen + 1);
599 Tolower(name);
600 strListAdd(&vstr, name, ',');
601 hdr = request->header.getByName(name);
602 safe_free(name);
603 value = hdr.termedBuf();
604
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 vary.clean();
616 #endif
617
618 debugs(11, 3, "httpMakeVaryMark: " << vstr);
619 return vstr.termedBuf();
620 }
621
622 void
623 HttpStateData::keepaliveAccounting(HttpReply *reply)
624 {
625 if (flags.keepalive)
626 if (_peer)
627 _peer->stats.n_keepalives_sent++;
628
629 if (reply->keep_alive) {
630 if (_peer)
631 _peer->stats.n_keepalives_recv++;
632
633 if (Config.onoff.detect_broken_server_pconns
634 && reply->bodySize(request->method) == -1 && !flags.chunked) {
635 debugs(11, 1, "keepaliveAccounting: Impossible keep-alive header from '" << entry->url() << "'" );
636 // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" );
637 flags.keepalive_broken = 1;
638 }
639 }
640 }
641
642 void
643 HttpStateData::checkDateSkew(HttpReply *reply)
644 {
645 if (reply->date > -1 && !_peer) {
646 int skew = abs((int)(reply->date - squid_curtime));
647
648 if (skew > 86400)
649 debugs(11, 3, "" << request->GetHost() << "'s clock is skewed by " << skew << " seconds!");
650 }
651 }
652
653 /**
654 * This creates the error page itself.. its likely
655 * that the forward ported reply header max size patch
656 * generates non http conformant error pages - in which
657 * case the errors where should be 'BAD_GATEWAY' etc
658 */
659 void
660 HttpStateData::processReplyHeader()
661 {
662 /** Creates a blank header. If this routine is made incremental, this will not do */
663
664 /* NP: all exit points to this function MUST call ctx_exit(ctx) */
665 Ctx ctx = ctx_enter(entry->mem_obj->url);
666
667 debugs(11, 3, "processReplyHeader: key '" << entry->getMD5Text() << "'");
668
669 assert(!flags.headers_parsed);
670
671 if (!readBuf->hasContent()) {
672 ctx_exit(ctx);
673 return;
674 }
675
676 http_status error = HTTP_STATUS_NONE;
677
678 HttpReply *newrep = new HttpReply;
679 const bool parsed = newrep->parse(readBuf, eof, &error);
680
681 if (!parsed && readBuf->contentSize() > 5 && strncmp(readBuf->content(), "HTTP/", 5) != 0 && strncmp(readBuf->content(), "ICY", 3) != 0) {
682 MemBuf *mb;
683 HttpReply *tmprep = new HttpReply;
684 tmprep->setHeaders(HTTP_OK, "Gatewaying", NULL, -1, -1, -1);
685 tmprep->header.putExt("X-Transformed-From", "HTTP/0.9");
686 mb = tmprep->pack();
687 newrep->parse(mb, eof, &error);
688 delete mb;
689 delete tmprep;
690 } else {
691 if (!parsed && error > 0) { // unrecoverable parsing error
692 debugs(11, 3, "processReplyHeader: Non-HTTP-compliant header: '" << readBuf->content() << "'");
693 flags.headers_parsed = 1;
694 newrep->sline.version = HttpVersion(1,1);
695 newrep->sline.status = error;
696 HttpReply *vrep = setVirginReply(newrep);
697 entry->replaceHttpReply(vrep);
698 ctx_exit(ctx);
699 return;
700 }
701
702 if (!parsed) { // need more data
703 assert(!error);
704 assert(!eof);
705 delete newrep;
706 ctx_exit(ctx);
707 return;
708 }
709
710 debugs(11, 9, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------");
711
712 header_bytes_read = headersEnd(readBuf->content(), readBuf->contentSize());
713 readBuf->consume(header_bytes_read);
714 }
715
716 newrep->removeStaleWarnings();
717
718 if (newrep->sline.protocol == PROTO_HTTP && newrep->sline.status >= 100 && newrep->sline.status < 200) {
719 handle1xx(newrep);
720 ctx_exit(ctx);
721 return;
722 }
723
724 flags.chunked = 0;
725 if (newrep->sline.protocol == PROTO_HTTP && newrep->header.chunked()) {
726 flags.chunked = 1;
727 httpChunkDecoder = new ChunkedCodingParser;
728 }
729
730 if (!peerSupportsConnectionPinning())
731 orig_request->flags.connection_auth_disabled = 1;
732
733 HttpReply *vrep = setVirginReply(newrep);
734 flags.headers_parsed = 1;
735
736 keepaliveAccounting(vrep);
737
738 checkDateSkew(vrep);
739
740 processSurrogateControl (vrep);
741
742 /** \todo IF the reply is a 1.0 reply, AND it has a Connection: Header
743 * Parse the header and remove all referenced headers
744 */
745
746 orig_request->hier.peer_reply_status = newrep->sline.status;
747
748 ctx_exit(ctx);
749 }
750
751 /// ignore or start forwarding the 1xx response (a.k.a., control message)
752 void
753 HttpStateData::handle1xx(HttpReply *reply)
754 {
755 HttpMsgPointerT<HttpReply> msg(reply); // will destroy reply if unused
756
757 // one 1xx at a time: we must not be called while waiting for previous 1xx
758 Must(!flags.handling1xx);
759 flags.handling1xx = true;
760
761 if (!orig_request->canHandle1xx()) {
762 debugs(11, 2, HERE << "ignoring client-unsupported 1xx");
763 proceedAfter1xx();
764 return;
765 }
766
767 #if USE_HTTP_VIOLATIONS
768 // check whether the 1xx response forwarding is allowed by squid.conf
769 if (Config.accessList.reply) {
770 ACLFilledChecklist ch(Config.accessList.reply, request, NULL);
771 ch.reply = HTTPMSGLOCK(reply);
772 if (!ch.fastCheck()) { // TODO: support slow lookups?
773 debugs(11, 3, HERE << "ignoring denied 1xx");
774 proceedAfter1xx();
775 return;
776 }
777 }
778 #endif // USE_HTTP_VIOLATIONS
779
780 debugs(11, 2, HERE << "forwarding 1xx to client");
781
782 // the Sink will use this to call us back after writing 1xx to the client
783 typedef NullaryMemFunT<HttpStateData> CbDialer;
784 const AsyncCall::Pointer cb = JobCallback(11, 3, CbDialer, this,
785 HttpStateData::proceedAfter1xx);
786 CallJobHere1(11, 4, orig_request->clientConnection, ConnStateData,
787 ConnStateData::sendControlMsg, HttpControlMsg(msg, cb));
788 // If the call is not fired, then the Sink is gone, and HttpStateData
789 // will terminate due to an aborted store entry or another similar error.
790 // If we get stuck, it is not handle1xx fault if we could get stuck
791 // for similar reasons without a 1xx response.
792 }
793
794 /// restores state and resumes processing after 1xx is ignored or forwarded
795 void
796 HttpStateData::proceedAfter1xx()
797 {
798 Must(flags.handling1xx);
799
800 debugs(11, 2, HERE << "consuming " << header_bytes_read <<
801 " header and " << reply_bytes_read << " body bytes read after 1xx");
802 header_bytes_read = 0;
803 reply_bytes_read = 0;
804
805 CallJobHere(11, 3, this, HttpStateData, HttpStateData::processReply);
806 }
807
808
809 /**
810 * returns true if the peer can support connection pinning
811 */
812 bool HttpStateData::peerSupportsConnectionPinning() const
813 {
814 const HttpReply *rep = entry->mem_obj->getReply();
815 const HttpHeader *hdr = &rep->header;
816 bool rc;
817 String header;
818
819 if (!_peer)
820 return true;
821
822 /*If this peer does not support connection pinning (authenticated
823 connections) return false
824 */
825 if (!_peer->connection_auth)
826 return false;
827
828 /*The peer supports connection pinning and the http reply status
829 is not unauthorized, so the related connection can be pinned
830 */
831 if (rep->sline.status != HTTP_UNAUTHORIZED)
832 return true;
833
834 /*The server respond with HTTP_UNAUTHORIZED and the peer configured
835 with "connection-auth=on" we know that the peer supports pinned
836 connections
837 */
838 if (_peer->connection_auth == 1)
839 return true;
840
841 /*At this point peer has configured with "connection-auth=auto"
842 parameter so we need some extra checks to decide if we are going
843 to allow pinned connections or not
844 */
845
846 /*if the peer configured with originserver just allow connection
847 pinning (squid 2.6 behaviour)
848 */
849 if (_peer->options.originserver)
850 return true;
851
852 /*if the connections it is already pinned it is OK*/
853 if (request->flags.pinned)
854 return true;
855
856 /*Allow pinned connections only if the Proxy-support header exists in
857 reply and has in its list the "Session-Based-Authentication"
858 which means that the peer supports connection pinning.
859 */
860 if (!hdr->has(HDR_PROXY_SUPPORT))
861 return false;
862
863 header = hdr->getStrOrList(HDR_PROXY_SUPPORT);
864 /* XXX This ought to be done in a case-insensitive manner */
865 rc = (strstr(header.termedBuf(), "Session-Based-Authentication") != NULL);
866
867 return rc;
868 }
869
870 // Called when we parsed (and possibly adapted) the headers but
871 // had not starting storing (a.k.a., sending) the body yet.
872 void
873 HttpStateData::haveParsedReplyHeaders()
874 {
875 ServerStateData::haveParsedReplyHeaders();
876
877 Ctx ctx = ctx_enter(entry->mem_obj->url);
878 HttpReply *rep = finalReply();
879
880 if (rep->sline.status == HTTP_PARTIAL_CONTENT &&
881 rep->content_range)
882 currentOffset = rep->content_range->spec.offset;
883
884 entry->timestampsSet();
885
886 /* Check if object is cacheable or not based on reply code */
887 debugs(11, 3, "haveParsedReplyHeaders: HTTP CODE: " << rep->sline.status);
888
889 if (neighbors_do_private_keys)
890 httpMaybeRemovePublic(entry, rep->sline.status);
891
892 if (rep->header.has(HDR_VARY)
893 #if X_ACCELERATOR_VARY
894 || rep->header.has(HDR_X_ACCELERATOR_VARY)
895 #endif
896 ) {
897 const char *vary = httpMakeVaryMark(orig_request, rep);
898
899 if (!vary) {
900 entry->makePrivate();
901 if (!fwd->reforwardableStatus(rep->sline.status))
902 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
903 goto no_cache;
904 }
905
906 entry->mem_obj->vary_headers = xstrdup(vary);
907 }
908
909 #if WIP_FWD_LOG
910 fwdStatus(fwd, s);
911
912 #endif
913 /*
914 * If its not a reply that we will re-forward, then
915 * allow the client to get it.
916 */
917 if (!fwd->reforwardableStatus(rep->sline.status))
918 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
919
920 switch (cacheableReply()) {
921
922 case 1:
923 entry->makePublic();
924 break;
925
926 case 0:
927 entry->makePrivate();
928 break;
929
930 case -1:
931
932 #if USE_HTTP_VIOLATIONS
933 if (Config.negativeTtl > 0)
934 entry->cacheNegatively();
935 else
936 #endif
937 entry->makePrivate();
938
939 break;
940
941 default:
942 assert(0);
943
944 break;
945 }
946
947 no_cache:
948
949 if (!ignoreCacheControl && rep->cache_control) {
950 if (EBIT_TEST(rep->cache_control->mask, CC_PROXY_REVALIDATE) ||
951 EBIT_TEST(rep->cache_control->mask, CC_MUST_REVALIDATE) ||
952 EBIT_TEST(rep->cache_control->mask, CC_S_MAXAGE))
953 EBIT_SET(entry->flags, ENTRY_REVALIDATE);
954 }
955
956 #if HEADERS_LOG
957 headersLog(1, 0, request->method, rep);
958
959 #endif
960
961 ctx_exit(ctx);
962 }
963
964 HttpStateData::ConnectionStatus
965 HttpStateData::statusIfComplete() const
966 {
967 const HttpReply *rep = virginReply();
968 /** \par
969 * If the reply wants to close the connection, it takes precedence */
970
971 if (httpHeaderHasConnDir(&rep->header, "close"))
972 return COMPLETE_NONPERSISTENT_MSG;
973
974 /** \par
975 * If we didn't send a keep-alive request header, then this
976 * can not be a persistent connection.
977 */
978 if (!flags.keepalive)
979 return COMPLETE_NONPERSISTENT_MSG;
980
981 /** \par
982 * If we haven't sent the whole request then this can not be a persistent
983 * connection.
984 */
985 if (!flags.request_sent) {
986 debugs(11, 1, "statusIfComplete: Request not yet fully sent \"" << RequestMethodStr(orig_request->method) << " " << entry->url() << "\"" );
987 return COMPLETE_NONPERSISTENT_MSG;
988 }
989
990 /** \par
991 * What does the reply have to say about keep-alive?
992 */
993 /**
994 \bug XXX BUG?
995 * If the origin server (HTTP/1.0) does not send a keep-alive
996 * header, but keeps the connection open anyway, what happens?
997 * We'll return here and http.c waits for an EOF before changing
998 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
999 * and an error status code, and we might have to wait until
1000 * the server times out the socket.
1001 */
1002 if (!rep->keep_alive)
1003 return COMPLETE_NONPERSISTENT_MSG;
1004
1005 return COMPLETE_PERSISTENT_MSG;
1006 }
1007
1008 HttpStateData::ConnectionStatus
1009 HttpStateData::persistentConnStatus() const
1010 {
1011 debugs(11, 3, "persistentConnStatus: FD " << fd << " eof=" << eof);
1012 const HttpReply *vrep = virginReply();
1013 debugs(11, 5, "persistentConnStatus: content_length=" << vrep->content_length);
1014
1015 /* If we haven't seen the end of reply headers, we are not done */
1016 debugs(11, 5, "persistentConnStatus: flags.headers_parsed=" << flags.headers_parsed);
1017
1018 if (!flags.headers_parsed)
1019 return INCOMPLETE_MSG;
1020
1021 if (eof) // already reached EOF
1022 return COMPLETE_NONPERSISTENT_MSG;
1023
1024 /** \par
1025 * In chunked response we do not know the content length but we are absolutely
1026 * sure about the end of response, so we are calling the statusIfComplete to
1027 * decide if we can be persistant
1028 */
1029 if (lastChunk && flags.chunked)
1030 return statusIfComplete();
1031
1032 const int64_t clen = vrep->bodySize(request->method);
1033
1034 debugs(11, 5, "persistentConnStatus: clen=" << clen);
1035
1036 /* If the body size is unknown we must wait for EOF */
1037 if (clen < 0)
1038 return INCOMPLETE_MSG;
1039
1040 /** \par
1041 * If the body size is known, we must wait until we've gotten all of it. */
1042 if (clen > 0) {
1043 // old technique:
1044 // if (entry->mem_obj->endOffset() < vrep->content_length + vrep->hdr_sz)
1045 const int64_t body_bytes_read = reply_bytes_read - header_bytes_read;
1046 debugs(11,5, "persistentConnStatus: body_bytes_read=" <<
1047 body_bytes_read << " content_length=" << vrep->content_length);
1048
1049 if (body_bytes_read < vrep->content_length)
1050 return INCOMPLETE_MSG;
1051
1052 if (body_bytes_truncated > 0) // already read more than needed
1053 return COMPLETE_NONPERSISTENT_MSG; // disable pconns
1054 }
1055
1056 /** \par
1057 * If there is no message body or we got it all, we can be persistent */
1058 return statusIfComplete();
1059 }
1060
1061 /*
1062 * This is the callback after some data has been read from the network
1063 */
1064 /*
1065 void
1066 HttpStateData::ReadReplyWrapper(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
1067 {
1068 HttpStateData *httpState = static_cast<HttpStateData *>(data);
1069 assert (fd == httpState->fd);
1070 // assert(buf == readBuf->content());
1071 PROF_start(HttpStateData_readReply);
1072 httpState->readReply(len, flag, xerrno);
1073 PROF_stop(HttpStateData_readReply);
1074 }
1075 */
1076
1077 /* XXX this function is too long! */
1078 void
1079 HttpStateData::readReply(const CommIoCbParams &io)
1080 {
1081 int bin;
1082 int clen;
1083 int len = io.size;
1084
1085 assert(fd == io.fd);
1086
1087 flags.do_next_read = 0;
1088
1089 debugs(11, 5, "httpReadReply: FD " << fd << ": len " << len << ".");
1090
1091 // Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us
1092 if (io.flag == COMM_ERR_CLOSING) {
1093 debugs(11, 3, "http socket closing");
1094 return;
1095 }
1096
1097 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1098 maybeReadVirginBody();
1099 return;
1100 }
1101
1102 // handle I/O errors
1103 if (io.flag != COMM_OK || len < 0) {
1104 debugs(11, 2, "httpReadReply: FD " << fd << ": read failure: " << xstrerror() << ".");
1105
1106 if (ignoreErrno(io.xerrno)) {
1107 flags.do_next_read = 1;
1108 } else {
1109 ErrorState *err;
1110 err = errorCon(ERR_READ_ERROR, HTTP_BAD_GATEWAY, fwd->request);
1111 err->xerrno = io.xerrno;
1112 fwd->fail(err);
1113 flags.do_next_read = 0;
1114 comm_close(fd);
1115 }
1116
1117 return;
1118 }
1119
1120 // update I/O stats
1121 if (len > 0) {
1122 readBuf->appended(len);
1123 reply_bytes_read += len;
1124 #if USE_DELAY_POOLS
1125 DelayId delayId = entry->mem_obj->mostBytesAllowed();
1126 delayId.bytesIn(len);
1127 #endif
1128
1129 kb_incr(&statCounter.server.all.kbytes_in, len);
1130 kb_incr(&statCounter.server.http.kbytes_in, len);
1131 IOStats.Http.reads++;
1132
1133 for (clen = len - 1, bin = 0; clen; bin++)
1134 clen >>= 1;
1135
1136 IOStats.Http.read_hist[bin]++;
1137
1138 // update peer response time stats (%<pt)
1139 const timeval &sent = orig_request->hier.peer_http_request_sent;
1140 orig_request->hier.peer_response_time =
1141 sent.tv_sec ? tvSubMsec(sent, current_time) : -1;
1142 }
1143
1144 /** \par
1145 * Here the RFC says we should ignore whitespace between replies, but we can't as
1146 * doing so breaks HTTP/0.9 replies beginning with witespace, and in addition
1147 * the response splitting countermeasures is extremely likely to trigger on this,
1148 * not allowing connection reuse in the first place.
1149 */
1150 #if DONT_DO_THIS
1151 if (!flags.headers_parsed && len > 0 && fd_table[fd].uses > 1) {
1152 /* Skip whitespace between replies */
1153
1154 while (len > 0 && xisspace(*buf))
1155 xmemmove(buf, buf + 1, len--);
1156
1157 if (len == 0) {
1158 /* Continue to read... */
1159 /* Timeout NOT increased. This whitespace was from previous reply */
1160 flags.do_next_read = 1;
1161 maybeReadVirginBody();
1162 return;
1163 }
1164 }
1165
1166 #endif
1167
1168 if (len == 0) { // reached EOF?
1169 eof = 1;
1170 flags.do_next_read = 0;
1171
1172 /* Bug 2879: Replies may terminate with \r\n then EOF instead of \r\n\r\n
1173 * Ensure here that we have at minimum two \r\n when EOF is seen.
1174 * TODO: Add eof parameter to headersEnd() and move this hack there.
1175 */
1176 if (readBuf->contentSize() && !flags.headers_parsed) {
1177 /*
1178 * Yes Henrik, there is a point to doing this. When we
1179 * called httpProcessReplyHeader() before, we didn't find
1180 * the end of headers, but now we are definately at EOF, so
1181 * we want to process the reply headers.
1182 */
1183 /* Fake an "end-of-headers" to work around such broken servers */
1184 readBuf->append("\r\n", 2);
1185 }
1186 }
1187
1188 processReply();
1189 }
1190
1191 /// processes the already read and buffered response data, possibly after
1192 /// waiting for asynchronous 1xx control message processing
1193 void
1194 HttpStateData::processReply()
1195 {
1196
1197 if (flags.handling1xx) { // we came back after handling a 1xx response
1198 debugs(11, 5, HERE << "done with 1xx handling");
1199 flags.handling1xx = false;
1200 Must(!flags.headers_parsed);
1201 }
1202
1203 if (!flags.headers_parsed) { // have not parsed headers yet?
1204 PROF_start(HttpStateData_processReplyHeader);
1205 processReplyHeader();
1206 PROF_stop(HttpStateData_processReplyHeader);
1207
1208 if (!continueAfterParsingHeader()) // parsing error or need more data
1209 return; // TODO: send errors to ICAP
1210
1211 adaptOrFinalizeReply();
1212 }
1213
1214 // kick more reads if needed and/or process the response body, if any
1215 PROF_start(HttpStateData_processReplyBody);
1216 processReplyBody(); // may call serverComplete()
1217 PROF_stop(HttpStateData_processReplyBody);
1218 }
1219
1220 /**
1221 \retval true if we can continue with processing the body or doing ICAP.
1222 */
1223 bool
1224 HttpStateData::continueAfterParsingHeader()
1225 {
1226 if (flags.handling1xx) {
1227 debugs(11, 5, HERE << "wait for 1xx handling");
1228 Must(!flags.headers_parsed);
1229 return false;
1230 }
1231
1232 if (!flags.headers_parsed && !eof) {
1233 debugs(11, 9, HERE << "needs more at " << readBuf->contentSize());
1234 flags.do_next_read = 1;
1235 /** \retval false If we have not finished parsing the headers and may get more data.
1236 * Schedules more reads to retrieve the missing data.
1237 */
1238 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1239 return false;
1240 }
1241
1242 /** If we are done with parsing, check for errors */
1243
1244 err_type error = ERR_NONE;
1245
1246 if (flags.headers_parsed) { // parsed headers, possibly with errors
1247 // check for header parsing errors
1248 if (HttpReply *vrep = virginReply()) {
1249 const http_status s = vrep->sline.status;
1250 const HttpVersion &v = vrep->sline.version;
1251 if (s == HTTP_INVALID_HEADER && v != HttpVersion(0,9)) {
1252 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() );
1253 error = ERR_INVALID_RESP;
1254 } else if (s == HTTP_HEADER_TOO_LARGE) {
1255 fwd->dontRetry(true);
1256 error = ERR_TOO_BIG;
1257 } else {
1258 return true; // done parsing, got reply, and no error
1259 }
1260 } else {
1261 // parsed headers but got no reply
1262 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: No reply at all for " << entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() );
1263 error = ERR_INVALID_RESP;
1264 }
1265 } else {
1266 assert(eof);
1267 if (readBuf->hasContent()) {
1268 error = ERR_INVALID_RESP;
1269 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Headers did not parse at all for " << entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() );
1270 } else {
1271 error = ERR_ZERO_SIZE_OBJECT;
1272 debugs(11, (orig_request->flags.accelerated?DBG_IMPORTANT:2), "WARNING: HTTP: Invalid Response: No object data received for " <<
1273 entry->url() << " AKA " << orig_request->GetHost() << orig_request->urlpath.termedBuf() );
1274 }
1275 }
1276
1277 assert(error != ERR_NONE);
1278 entry->reset();
1279 fwd->fail(errorCon(error, HTTP_BAD_GATEWAY, fwd->request));
1280 flags.do_next_read = 0;
1281 comm_close(fd);
1282 return false; // quit on error
1283 }
1284
1285 /** truncate what we read if we read too much so that writeReplyBody()
1286 writes no more than what we should have read */
1287 void
1288 HttpStateData::truncateVirginBody()
1289 {
1290 assert(flags.headers_parsed);
1291
1292 HttpReply *vrep = virginReply();
1293 int64_t clen = -1;
1294 if (!vrep->expectingBody(request->method, clen) || clen < 0)
1295 return; // no body or a body of unknown size, including chunked
1296
1297 const int64_t body_bytes_read = reply_bytes_read - header_bytes_read;
1298 if (body_bytes_read - body_bytes_truncated <= clen)
1299 return; // we did not read too much or already took care of the extras
1300
1301 if (const int64_t extras = body_bytes_read - body_bytes_truncated - clen) {
1302 // server sent more that the advertised content length
1303 debugs(11,5, HERE << "body_bytes_read=" << body_bytes_read <<
1304 " clen=" << clen << '/' << vrep->content_length <<
1305 " body_bytes_truncated=" << body_bytes_truncated << '+' << extras);
1306
1307 readBuf->truncate(extras);
1308 body_bytes_truncated += extras;
1309 }
1310 }
1311
1312 /**
1313 * Call this when there is data from the origin server
1314 * which should be sent to either StoreEntry, or to ICAP...
1315 */
1316 void
1317 HttpStateData::writeReplyBody()
1318 {
1319 truncateVirginBody(); // if needed
1320 const char *data = readBuf->content();
1321 int len = readBuf->contentSize();
1322 addVirginReplyBody(data, len);
1323 readBuf->consume(len);
1324 }
1325
1326 bool
1327 HttpStateData::decodeAndWriteReplyBody()
1328 {
1329 const char *data = NULL;
1330 int len;
1331 bool wasThereAnException = false;
1332 assert(flags.chunked);
1333 assert(httpChunkDecoder);
1334 SQUID_ENTER_THROWING_CODE();
1335 MemBuf decodedData;
1336 decodedData.init();
1337 const bool doneParsing = httpChunkDecoder->parse(readBuf,&decodedData);
1338 len = decodedData.contentSize();
1339 data=decodedData.content();
1340 addVirginReplyBody(data, len);
1341 if (doneParsing) {
1342 lastChunk = 1;
1343 flags.do_next_read = 0;
1344 }
1345 SQUID_EXIT_THROWING_CODE(wasThereAnException);
1346 return wasThereAnException;
1347 }
1348
1349 /**
1350 * processReplyBody has two purposes:
1351 * 1 - take the reply body data, if any, and put it into either
1352 * the StoreEntry, or give it over to ICAP.
1353 * 2 - see if we made it to the end of the response (persistent
1354 * connections and such)
1355 */
1356 void
1357 HttpStateData::processReplyBody()
1358 {
1359 AsyncCall::Pointer call;
1360 Ip::Address client_addr;
1361 bool ispinned = false;
1362
1363 if (!flags.headers_parsed) {
1364 flags.do_next_read = 1;
1365 maybeReadVirginBody();
1366 return;
1367 }
1368
1369 #if USE_ADAPTATION
1370 debugs(11,5, HERE << "adaptationAccessCheckPending=" << adaptationAccessCheckPending);
1371 if (adaptationAccessCheckPending)
1372 return;
1373
1374 #endif
1375
1376 /*
1377 * At this point the reply headers have been parsed and consumed.
1378 * That means header content has been removed from readBuf and
1379 * it contains only body data.
1380 */
1381 if (flags.chunked) {
1382 if (!decodeAndWriteReplyBody()) {
1383 flags.do_next_read = 0;
1384 serverComplete();
1385 return;
1386 }
1387 } else
1388 writeReplyBody();
1389
1390 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
1391 /*
1392 * The above writeReplyBody() call could ABORT this entry,
1393 * in that case, the server FD should already be closed.
1394 * there's nothing for us to do.
1395 */
1396 (void) 0;
1397 } else
1398 switch (persistentConnStatus()) {
1399 case INCOMPLETE_MSG:
1400 debugs(11, 5, "processReplyBody: INCOMPLETE_MSG");
1401 /* Wait for more data or EOF condition */
1402 if (flags.keepalive_broken) {
1403 call = NULL;
1404 commSetTimeout(fd, 10, call);
1405 } else {
1406 call = NULL;
1407 commSetTimeout(fd, Config.Timeout.read, call);
1408 }
1409
1410 flags.do_next_read = 1;
1411 break;
1412
1413 case COMPLETE_PERSISTENT_MSG:
1414 debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG");
1415 /* yes we have to clear all these! */
1416 call = NULL;
1417 commSetTimeout(fd, -1, call);
1418 flags.do_next_read = 0;
1419
1420 comm_remove_close_handler(fd, closeHandler);
1421 closeHandler = NULL;
1422 fwd->unregister(fd);
1423
1424 if (orig_request->flags.spoof_client_ip)
1425 client_addr = orig_request->client_addr;
1426
1427
1428 if (request->flags.pinned) {
1429 ispinned = true;
1430 } else if (request->flags.connection_auth && request->flags.auth_sent) {
1431 ispinned = true;
1432 }
1433
1434 if (orig_request->pinnedConnection() && ispinned) {
1435 orig_request->pinnedConnection()->pinConnection(fd, orig_request, _peer,
1436 (request->flags.connection_auth != 0));
1437 } else {
1438 fwd->pconnPush(fd, _peer, request, orig_request->GetHost(), client_addr);
1439 }
1440
1441 fd = -1;
1442
1443 serverComplete();
1444 return;
1445
1446 case COMPLETE_NONPERSISTENT_MSG:
1447 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG");
1448 serverComplete();
1449 return;
1450 }
1451
1452 maybeReadVirginBody();
1453 }
1454
1455 void
1456 HttpStateData::maybeReadVirginBody()
1457 {
1458 // we may need to grow the buffer if headers do not fit
1459 const int minRead = flags.headers_parsed ? 0 :1024;
1460 const int read_size = replyBodySpace(*readBuf, minRead);
1461
1462 debugs(11,9, HERE << (flags.do_next_read ? "may" : "wont") <<
1463 " read up to " << read_size << " bytes from FD " << fd);
1464
1465 /*
1466 * why <2? Because delayAwareRead() won't actually read if
1467 * you ask it to read 1 byte. The delayed read request
1468 * just gets re-queued until the client side drains, then
1469 * the I/O thread hangs. Better to not register any read
1470 * handler until we get a notification from someone that
1471 * its okay to read again.
1472 */
1473 if (read_size < 2)
1474 return;
1475
1476 if (flags.do_next_read) {
1477 flags.do_next_read = 0;
1478 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
1479 entry->delayAwareRead(fd, readBuf->space(read_size), read_size,
1480 JobCallback(11, 5, Dialer, this, HttpStateData::readReply));
1481 }
1482 }
1483
1484 /// called after writing the very last request byte (body, last-chunk, etc)
1485 void
1486 HttpStateData::wroteLast(const CommIoCbParams &io)
1487 {
1488 debugs(11, 5, HERE << "FD " << fd << ": size " << io.size << ": errflag " << io.flag << ".");
1489 #if URL_CHECKSUM_DEBUG
1490
1491 entry->mem_obj->checkUrlChecksum();
1492 #endif
1493
1494 if (io.size > 0) {
1495 fd_bytes(fd, io.size, FD_WRITE);
1496 kb_incr(&statCounter.server.all.kbytes_out, io.size);
1497 kb_incr(&statCounter.server.http.kbytes_out, io.size);
1498 }
1499
1500 if (io.flag == COMM_ERR_CLOSING)
1501 return;
1502
1503 if (io.flag) {
1504 ErrorState *err;
1505 err = errorCon(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY, fwd->request);
1506 err->xerrno = io.xerrno;
1507 fwd->fail(err);
1508 comm_close(fd);
1509 return;
1510 }
1511
1512 sendComplete();
1513 }
1514
1515 /// successfully wrote the entire request (including body, last-chunk, etc.)
1516 void
1517 HttpStateData::sendComplete()
1518 {
1519 /*
1520 * Set the read timeout here because it hasn't been set yet.
1521 * We only set the read timeout after the request has been
1522 * fully written to the server-side. If we start the timeout
1523 * after connection establishment, then we are likely to hit
1524 * the timeout for POST/PUT requests that have very large
1525 * request bodies.
1526 */
1527 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
1528 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
1529 TimeoutDialer, this, HttpStateData::httpTimeout);
1530
1531 commSetTimeout(fd, Config.Timeout.read, timeoutCall);
1532
1533 flags.request_sent = 1;
1534
1535 orig_request->hier.peer_http_request_sent = current_time;
1536 }
1537
1538 // Close the HTTP server connection. Used by serverComplete().
1539 void
1540 HttpStateData::closeServer()
1541 {
1542 debugs(11,5, HERE << "closing HTTP server FD " << fd << " this " << this);
1543
1544 if (fd >= 0) {
1545 fwd->unregister(fd);
1546 comm_remove_close_handler(fd, closeHandler);
1547 closeHandler = NULL;
1548 comm_close(fd);
1549 fd = -1;
1550 }
1551 }
1552
1553 bool
1554 HttpStateData::doneWithServer() const
1555 {
1556 return fd < 0;
1557 }
1558
1559
1560 /*
1561 * Fixup authentication request headers for special cases
1562 */
1563 static void
1564 httpFixupAuthentication(HttpRequest * request, HttpRequest * orig_request, const HttpHeader * hdr_in, HttpHeader * hdr_out, http_state_flags flags)
1565 {
1566 http_hdr_type header = flags.originpeer ? HDR_AUTHORIZATION : HDR_PROXY_AUTHORIZATION;
1567
1568 /* Nothing to do unless we are forwarding to a peer */
1569 if (!request->flags.proxying)
1570 return;
1571
1572 /* Needs to be explicitly enabled */
1573 if (!orig_request->peer_login)
1574 return;
1575
1576 /* Maybe already dealt with? */
1577 if (hdr_out->has(header))
1578 return;
1579
1580 /* Nothing to do here for PASSTHRU */
1581 if (strcmp(orig_request->peer_login, "PASSTHRU") == 0)
1582 return;
1583
1584 /* PROXYPASS is a special case, single-signon to servers with the proxy password (basic only) */
1585 if (flags.originpeer && strcmp(orig_request->peer_login, "PROXYPASS") == 0 && hdr_in->has(HDR_PROXY_AUTHORIZATION)) {
1586 const char *auth = hdr_in->getStr(HDR_PROXY_AUTHORIZATION);
1587
1588 if (auth && strncasecmp(auth, "basic ", 6) == 0) {
1589 hdr_out->putStr(header, auth);
1590 return;
1591 }
1592 }
1593
1594 /* Special mode to pass the username to the upstream cache */
1595 if (*orig_request->peer_login == '*') {
1596 char loginbuf[256];
1597 const char *username = "-";
1598
1599 if (orig_request->extacl_user.size())
1600 username = orig_request->extacl_user.termedBuf();
1601 else if (orig_request->auth_user_request != NULL)
1602 username = orig_request->auth_user_request->username();
1603
1604 snprintf(loginbuf, sizeof(loginbuf), "%s%s", username, orig_request->peer_login + 1);
1605
1606 httpHeaderPutStrf(hdr_out, header, "Basic %s",
1607 base64_encode(loginbuf));
1608 return;
1609 }
1610
1611 /* external_acl provided credentials */
1612 if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size() &&
1613 (strcmp(orig_request->peer_login, "PASS") == 0 ||
1614 strcmp(orig_request->peer_login, "PROXYPASS") == 0)) {
1615 char loginbuf[256];
1616 snprintf(loginbuf, sizeof(loginbuf), SQUIDSTRINGPH ":" SQUIDSTRINGPH,
1617 SQUIDSTRINGPRINT(orig_request->extacl_user),
1618 SQUIDSTRINGPRINT(orig_request->extacl_passwd));
1619 httpHeaderPutStrf(hdr_out, header, "Basic %s",
1620 base64_encode(loginbuf));
1621 return;
1622 }
1623
1624 /* Kerberos login to peer */
1625 #if HAVE_KRB5 && HAVE_GSSAPI
1626 if (strncmp(orig_request->peer_login, "NEGOTIATE",strlen("NEGOTIATE")) == 0) {
1627 char *Token=NULL;
1628 char *PrincipalName=NULL,*p;
1629 if ((p=strchr(orig_request->peer_login,':')) != NULL ) {
1630 PrincipalName=++p;
1631 }
1632 Token = peer_proxy_negotiate_auth(PrincipalName,request->peer_host);
1633 if (Token) {
1634 httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Negotiate %s",Token);
1635 }
1636 return;
1637 }
1638 #endif /* HAVE_KRB5 && HAVE_GSSAPI */
1639
1640 httpHeaderPutStrf(hdr_out, header, "Basic %s",
1641 base64_encode(orig_request->peer_login));
1642 return;
1643 }
1644
1645 /*
1646 * build request headers and append them to a given MemBuf
1647 * used by buildRequestPrefix()
1648 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
1649 */
1650 void
1651 HttpStateData::httpBuildRequestHeader(HttpRequest * request,
1652 HttpRequest * orig_request,
1653 StoreEntry * entry,
1654 HttpHeader * hdr_out,
1655 const http_state_flags flags)
1656 {
1657 /* building buffer for complex strings */
1658 #define BBUF_SZ (MAX_URL+32)
1659 LOCAL_ARRAY(char, bbuf, BBUF_SZ);
1660 LOCAL_ARRAY(char, ntoabuf, MAX_IPSTRLEN);
1661 const HttpHeader *hdr_in = &orig_request->header;
1662 const HttpHeaderEntry *e = NULL;
1663 HttpHeaderPos pos = HttpHeaderInitPos;
1664 assert (hdr_out->owner == hoRequest);
1665
1666 /* append our IMS header */
1667 if (request->lastmod > -1)
1668 hdr_out->putTime(HDR_IF_MODIFIED_SINCE, request->lastmod);
1669
1670 bool we_do_ranges = decideIfWeDoRanges (orig_request);
1671
1672 String strConnection (hdr_in->getList(HDR_CONNECTION));
1673
1674 while ((e = hdr_in->getEntry(&pos)))
1675 copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, orig_request, hdr_out, we_do_ranges, flags);
1676
1677 /* Abstraction break: We should interpret multipart/byterange responses
1678 * into offset-length data, and this works around our inability to do so.
1679 */
1680 if (!we_do_ranges && orig_request->multipartRangeRequest()) {
1681 /* don't cache the result */
1682 orig_request->flags.cachable = 0;
1683 /* pretend it's not a range request */
1684 delete orig_request->range;
1685 orig_request->range = NULL;
1686 orig_request->flags.range = 0;
1687 }
1688
1689 /* append Via */
1690 if (Config.onoff.via) {
1691 String strVia;
1692 strVia = hdr_in->getList(HDR_VIA);
1693 snprintf(bbuf, BBUF_SZ, "%d.%d %s",
1694 orig_request->http_ver.major,
1695 orig_request->http_ver.minor, ThisCache);
1696 strListAdd(&strVia, bbuf, ',');
1697 hdr_out->putStr(HDR_VIA, strVia.termedBuf());
1698 strVia.clean();
1699 }
1700
1701 if (orig_request->flags.accelerated) {
1702 /* Append Surrogate-Capabilities */
1703 String strSurrogate(hdr_in->getList(HDR_SURROGATE_CAPABILITY));
1704 #if USE_SQUID_ESI
1705 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"", Config.Accel.surrogate_id);
1706 #else
1707 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0\"", Config.Accel.surrogate_id);
1708 #endif
1709 strListAdd(&strSurrogate, bbuf, ',');
1710 hdr_out->putStr(HDR_SURROGATE_CAPABILITY, strSurrogate.termedBuf());
1711 }
1712
1713 /** \pre Handle X-Forwarded-For */
1714 if (strcmp(opt_forwarded_for, "delete") != 0) {
1715
1716 String strFwd = hdr_in->getList(HDR_X_FORWARDED_FOR);
1717
1718 if (strFwd.size() > 65536/2) {
1719 // There is probably a forwarding loop with Via detection disabled.
1720 // If we do nothing, String will assert on overflow soon.
1721 // TODO: Terminate all transactions with huge XFF?
1722 strFwd = "error";
1723
1724 static int warnedCount = 0;
1725 if (warnedCount++ < 100) {
1726 const char *url = entry ? entry->url() : urlCanonical(orig_request);
1727 debugs(11, 1, "Warning: likely forwarding loop with " << url);
1728 }
1729 }
1730
1731 if (strcmp(opt_forwarded_for, "on") == 0) {
1732 /** If set to ON - append client IP or 'unknown'. */
1733 if ( orig_request->client_addr.IsNoAddr() )
1734 strListAdd(&strFwd, "unknown", ',');
1735 else
1736 strListAdd(&strFwd, orig_request->client_addr.NtoA(ntoabuf, MAX_IPSTRLEN), ',');
1737 } else if (strcmp(opt_forwarded_for, "off") == 0) {
1738 /** If set to OFF - append 'unknown'. */
1739 strListAdd(&strFwd, "unknown", ',');
1740 } else if (strcmp(opt_forwarded_for, "transparent") == 0) {
1741 /** If set to TRANSPARENT - pass through unchanged. */
1742 } else if (strcmp(opt_forwarded_for, "truncate") == 0) {
1743 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
1744 if ( orig_request->client_addr.IsNoAddr() )
1745 strFwd = "unknown";
1746 else
1747 strFwd = orig_request->client_addr.NtoA(ntoabuf, MAX_IPSTRLEN);
1748 }
1749 if (strFwd.size() > 0)
1750 hdr_out->putStr(HDR_X_FORWARDED_FOR, strFwd.termedBuf());
1751 }
1752 /** If set to DELETE - do not copy through. */
1753
1754 /* append Host if not there already */
1755 if (!hdr_out->has(HDR_HOST)) {
1756 if (orig_request->peer_domain) {
1757 hdr_out->putStr(HDR_HOST, orig_request->peer_domain);
1758 } else if (orig_request->port == urlDefaultPort(orig_request->protocol)) {
1759 /* use port# only if not default */
1760 hdr_out->putStr(HDR_HOST, orig_request->GetHost());
1761 } else {
1762 httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d",
1763 orig_request->GetHost(),
1764 (int) orig_request->port);
1765 }
1766 }
1767
1768 /* append Authorization if known in URL, not in header and going direct */
1769 if (!hdr_out->has(HDR_AUTHORIZATION)) {
1770 if (!request->flags.proxying && *request->login) {
1771 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s",
1772 base64_encode(request->login));
1773 }
1774 }
1775
1776 /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */
1777 httpFixupAuthentication(request, orig_request, hdr_in, hdr_out, flags);
1778
1779 /* append Cache-Control, add max-age if not there already */
1780 {
1781 HttpHdrCc *cc = hdr_in->getCc();
1782
1783 if (!cc)
1784 cc = httpHdrCcCreate();
1785
1786 #if 0 /* see bug 2330 */
1787 /* Set no-cache if determined needed but not found */
1788 if (orig_request->flags.nocache)
1789 EBIT_SET(cc->mask, CC_NO_CACHE);
1790 #endif
1791
1792 /* Add max-age only without no-cache */
1793 if (!EBIT_TEST(cc->mask, CC_MAX_AGE) && !EBIT_TEST(cc->mask, CC_NO_CACHE)) {
1794 const char *url =
1795 entry ? entry->url() : urlCanonical(orig_request);
1796 httpHdrCcSetMaxAge(cc, getMaxAge(url));
1797
1798 if (request->urlpath.size())
1799 assert(strstr(url, request->urlpath.termedBuf()));
1800 }
1801
1802 /* Enforce sibling relations */
1803 if (flags.only_if_cached)
1804 EBIT_SET(cc->mask, CC_ONLY_IF_CACHED);
1805
1806 hdr_out->putCc(cc);
1807
1808 httpHdrCcDestroy(cc);
1809 }
1810
1811 /* maybe append Connection: keep-alive */
1812 if (flags.keepalive) {
1813 hdr_out->putStr(HDR_CONNECTION, "keep-alive");
1814 }
1815
1816 /* append Front-End-Https */
1817 if (flags.front_end_https) {
1818 if (flags.front_end_https == 1 || request->protocol == PROTO_HTTPS)
1819 hdr_out->putStr(HDR_FRONT_END_HTTPS, "On");
1820 }
1821
1822 if (flags.chunked_request) {
1823 // Do not just copy the original value so that if the client-side
1824 // starts decode other encodings, this code may remain valid.
1825 hdr_out->putStr(HDR_TRANSFER_ENCODING, "chunked");
1826 }
1827
1828 /* Now mangle the headers. */
1829 if (Config2.onoff.mangle_request_headers)
1830 httpHdrMangleList(hdr_out, request, ROR_REQUEST);
1831
1832 strConnection.clean();
1833 }
1834
1835 /**
1836 * Decides whether a particular header may be cloned from the received Clients request
1837 * to our outgoing fetch request.
1838 */
1839 void
1840 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, HttpRequest * request, const HttpRequest * orig_request, HttpHeader * hdr_out, const int we_do_ranges, const http_state_flags flags)
1841 {
1842 debugs(11, 5, "httpBuildRequestHeader: " << e->name << ": " << e->value );
1843
1844 switch (e->id) {
1845
1846 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */
1847
1848 case HDR_PROXY_AUTHORIZATION:
1849 /** \par Proxy-Authorization:
1850 * Only pass on proxy authentication to peers for which
1851 * authentication forwarding is explicitly enabled
1852 */
1853 if (!flags.originpeer && flags.proxying && orig_request->peer_login &&
1854 (strcmp(orig_request->peer_login, "PASS") == 0 ||
1855 strcmp(orig_request->peer_login, "PROXYPASS") == 0 ||
1856 strcmp(orig_request->peer_login, "PASSTHRU") == 0)) {
1857 hdr_out->addEntry(e->clone());
1858 }
1859 break;
1860
1861 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */
1862
1863 case HDR_CONNECTION: /** \par Connection: */
1864 case HDR_TE: /** \par TE: */
1865 case HDR_KEEP_ALIVE: /** \par Keep-Alive: */
1866 case HDR_PROXY_AUTHENTICATE: /** \par Proxy-Authenticate: */
1867 case HDR_TRAILER: /** \par Trailer: */
1868 case HDR_UPGRADE: /** \par Upgrade: */
1869 case HDR_TRANSFER_ENCODING: /** \par Transfer-Encoding: */
1870 break;
1871
1872
1873 /** \par OTHER headers I haven't bothered to track down yet. */
1874
1875 case HDR_AUTHORIZATION:
1876 /** \par WWW-Authorization:
1877 * Pass on WWW authentication */
1878
1879 if (!flags.originpeer) {
1880 hdr_out->addEntry(e->clone());
1881 } else {
1882 /** \note In accelerators, only forward authentication if enabled
1883 * (see also httpFixupAuthentication for special cases)
1884 */
1885 if (orig_request->peer_login &&
1886 (strcmp(orig_request->peer_login, "PASS") == 0 ||
1887 strcmp(orig_request->peer_login, "PASSTHRU") == 0 ||
1888 strcmp(orig_request->peer_login, "PROXYPASS") == 0)) {
1889 hdr_out->addEntry(e->clone());
1890 }
1891 }
1892
1893 break;
1894
1895 case HDR_HOST:
1896 /** \par Host:
1897 * Normally Squid rewrites the Host: header.
1898 * However, there is one case when we don't: If the URL
1899 * went through our redirector and the admin configured
1900 * 'redir_rewrites_host' to be off.
1901 */
1902 if (orig_request->peer_domain)
1903 hdr_out->putStr(HDR_HOST, orig_request->peer_domain);
1904 else if (request->flags.redirected && !Config.onoff.redir_rewrites_host)
1905 hdr_out->addEntry(e->clone());
1906 else {
1907 /* use port# only if not default */
1908
1909 if (orig_request->port == urlDefaultPort(orig_request->protocol)) {
1910 hdr_out->putStr(HDR_HOST, orig_request->GetHost());
1911 } else {
1912 httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d",
1913 orig_request->GetHost(),
1914 (int) orig_request->port);
1915 }
1916 }
1917
1918 break;
1919
1920 case HDR_IF_MODIFIED_SINCE:
1921 /** \par If-Modified-Since:
1922 * append unless we added our own;
1923 * \note at most one client's ims header can pass through */
1924
1925 if (!hdr_out->has(HDR_IF_MODIFIED_SINCE))
1926 hdr_out->addEntry(e->clone());
1927
1928 break;
1929
1930 case HDR_MAX_FORWARDS:
1931 /** \par Max-Forwards:
1932 * pass only on TRACE or OPTIONS requests */
1933 if (orig_request->method == METHOD_TRACE || orig_request->method == METHOD_OPTIONS) {
1934 const int64_t hops = e->getInt64();
1935
1936 if (hops > 0)
1937 hdr_out->putInt64(HDR_MAX_FORWARDS, hops - 1);
1938 }
1939
1940 break;
1941
1942 case HDR_VIA:
1943 /** \par Via:
1944 * If Via is disabled then forward any received header as-is.
1945 * Otherwise leave for explicit updated addition later. */
1946
1947 if (!Config.onoff.via)
1948 hdr_out->addEntry(e->clone());
1949
1950 break;
1951
1952 case HDR_RANGE:
1953
1954 case HDR_IF_RANGE:
1955
1956 case HDR_REQUEST_RANGE:
1957 /** \par Range:, If-Range:, Request-Range:
1958 * Only pass if we accept ranges */
1959 if (!we_do_ranges)
1960 hdr_out->addEntry(e->clone());
1961
1962 break;
1963
1964 case HDR_PROXY_CONNECTION: // SHOULD ignore. But doing so breaks things.
1965 break;
1966
1967 case HDR_X_FORWARDED_FOR:
1968
1969 case HDR_CACHE_CONTROL:
1970 /** \par X-Forwarded-For:, Cache-Control:
1971 * handled specially by Squid, so leave off for now.
1972 * append these after the loop if needed */
1973 break;
1974
1975 case HDR_FRONT_END_HTTPS:
1976 /** \par Front-End-Https:
1977 * Pass thru only if peer is configured with front-end-https */
1978 if (!flags.front_end_https)
1979 hdr_out->addEntry(e->clone());
1980
1981 break;
1982
1983 default:
1984 /** \par default.
1985 * pass on all other header fields
1986 * which are NOT listed by the special Connection: header. */
1987
1988 if (strConnection.size()>0 && strListIsMember(&strConnection, e->name.termedBuf(), ',')) {
1989 debugs(11, 2, "'" << e->name << "' header cropped by Connection: definition");
1990 return;
1991 }
1992
1993 hdr_out->addEntry(e->clone());
1994 }
1995 }
1996
1997 bool
1998 HttpStateData::decideIfWeDoRanges (HttpRequest * orig_request)
1999 {
2000 bool result = true;
2001 /* decide if we want to do Ranges ourselves
2002 * and fetch the whole object now)
2003 * We want to handle Ranges ourselves iff
2004 * - we can actually parse client Range specs
2005 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
2006 * - reply will be cachable
2007 * (If the reply will be uncachable we have to throw it away after
2008 * serving this request, so it is better to forward ranges to
2009 * the server and fetch only the requested content)
2010 */
2011
2012 int64_t roffLimit = orig_request->getRangeOffsetLimit();
2013
2014 if (NULL == orig_request->range || !orig_request->flags.cachable
2015 || orig_request->range->offsetLimitExceeded(roffLimit) || orig_request->flags.connection_auth)
2016 result = false;
2017
2018 debugs(11, 8, "decideIfWeDoRanges: range specs: " <<
2019 orig_request->range << ", cachable: " <<
2020 orig_request->flags.cachable << "; we_do_ranges: " << result);
2021
2022 return result;
2023 }
2024
2025 /* build request prefix and append it to a given MemBuf;
2026 * return the length of the prefix */
2027 mb_size_t
2028 HttpStateData::buildRequestPrefix(HttpRequest * aRequest,
2029 HttpRequest * original_request,
2030 StoreEntry * sentry,
2031 MemBuf * mb)
2032 {
2033 const int offset = mb->size;
2034 HttpVersion httpver(1,1);
2035 mb->Printf("%s %s HTTP/%d.%d\r\n",
2036 RequestMethodStr(aRequest->method),
2037 aRequest->urlpath.size() ? aRequest->urlpath.termedBuf() : "/",
2038 httpver.major,httpver.minor);
2039 /* build and pack headers */
2040 {
2041 HttpHeader hdr(hoRequest);
2042 Packer p;
2043 httpBuildRequestHeader(aRequest, original_request, sentry, &hdr, flags);
2044
2045 if (aRequest->flags.pinned && aRequest->flags.connection_auth)
2046 aRequest->flags.auth_sent = 1;
2047 else if (hdr.has(HDR_AUTHORIZATION))
2048 aRequest->flags.auth_sent = 1;
2049
2050 packerToMemInit(&p, mb);
2051 hdr.packInto(&p);
2052 hdr.clean();
2053 packerClean(&p);
2054 }
2055 /* append header terminator */
2056 mb->append(crlf, 2);
2057 return mb->size - offset;
2058 }
2059
2060 /* This will be called when connect completes. Write request. */
2061 bool
2062 HttpStateData::sendRequest()
2063 {
2064 MemBuf mb;
2065
2066 debugs(11, 5, "httpSendRequest: FD " << fd << ", request " << request << ", this " << this << ".");
2067
2068 if (!canSend(fd)) {
2069 debugs(11,3, HERE << "cannot send request to closing FD " << fd);
2070 assert(closeHandler != NULL);
2071 return false;
2072 }
2073
2074 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
2075 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
2076 TimeoutDialer, this, HttpStateData::httpTimeout);
2077 commSetTimeout(fd, Config.Timeout.lifetime, timeoutCall);
2078 flags.do_next_read = 1;
2079 maybeReadVirginBody();
2080
2081 if (orig_request->body_pipe != NULL) {
2082 if (!startRequestBodyFlow()) // register to receive body data
2083 return false;
2084 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2085 requestSender = JobCallback(11,5,
2086 Dialer, this, HttpStateData::sentRequestBody);
2087
2088 Must(!flags.chunked_request);
2089 // Preserve original chunked encoding unless we learned the length.
2090 if (orig_request->header.chunked() && orig_request->content_length < 0)
2091 flags.chunked_request = 1;
2092 } else {
2093 assert(!requestBodySource);
2094 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2095 requestSender = JobCallback(11,5,
2096 Dialer, this, HttpStateData::wroteLast);
2097 }
2098
2099 if (_peer != NULL) {
2100 if (_peer->options.originserver) {
2101 flags.proxying = 0;
2102 flags.originpeer = 1;
2103 } else {
2104 flags.proxying = 1;
2105 flags.originpeer = 0;
2106 }
2107 } else {
2108 flags.proxying = 0;
2109 flags.originpeer = 0;
2110 }
2111
2112 /*
2113 * Is keep-alive okay for all request methods?
2114 */
2115 if (orig_request->flags.must_keepalive)
2116 flags.keepalive = 1;
2117 else if (!Config.onoff.server_pconns)
2118 flags.keepalive = 0;
2119 else if (_peer == NULL)
2120 flags.keepalive = 1;
2121 else if (_peer->stats.n_keepalives_sent < 10)
2122 flags.keepalive = 1;
2123 else if ((double) _peer->stats.n_keepalives_recv /
2124 (double) _peer->stats.n_keepalives_sent > 0.50)
2125 flags.keepalive = 1;
2126
2127 if (_peer) {
2128 if (neighborType(_peer, request) == PEER_SIBLING &&
2129 !_peer->options.allow_miss)
2130 flags.only_if_cached = 1;
2131
2132 flags.front_end_https = _peer->front_end_https;
2133 }
2134
2135 mb.init();
2136 request->peer_host=_peer?_peer->host:NULL;
2137 buildRequestPrefix(request, orig_request, entry, &mb);
2138 debugs(11, 6, "httpSendRequest: FD " << fd << ":\n" << mb.buf);
2139 Comm::Write(fd, &mb, requestSender);
2140
2141 return true;
2142 }
2143
2144 bool
2145 HttpStateData::getMoreRequestBody(MemBuf &buf)
2146 {
2147 // parent's implementation can handle the no-encoding case
2148 if (!flags.chunked_request)
2149 return ServerStateData::getMoreRequestBody(buf);
2150
2151 MemBuf raw;
2152
2153 Must(requestBodySource != NULL);
2154 if (!requestBodySource->getMoreData(raw))
2155 return false; // no request body bytes to chunk yet
2156
2157 // optimization: pre-allocate buffer size that should be enough
2158 const mb_size_t rawDataSize = raw.contentSize();
2159 // we may need to send: hex-chunk-size CRLF raw-data CRLF last-chunk
2160 buf.init(16 + 2 + rawDataSize + 2 + 5, raw.max_capacity);
2161
2162 buf.Printf("%x\r\n", static_cast<unsigned int>(rawDataSize));
2163 buf.append(raw.content(), rawDataSize);
2164 buf.Printf("\r\n");
2165
2166 Must(rawDataSize > 0); // we did not accidently created last-chunk above
2167
2168 // Do not send last-chunk unless we successfully received everything
2169 if (receivedWholeRequestBody) {
2170 Must(!flags.sentLastChunk);
2171 flags.sentLastChunk = true;
2172 buf.append("0\r\n\r\n", 5);
2173 }
2174
2175 return true;
2176 }
2177
2178 void
2179 httpStart(FwdState *fwd)
2180 {
2181 debugs(11, 3, "httpStart: \"" << RequestMethodStr(fwd->request->method) << " " << fwd->entry->url() << "\"" );
2182 HttpStateData *httpState = new HttpStateData(fwd);
2183
2184 if (!httpState->sendRequest()) {
2185 debugs(11, 3, "httpStart: aborted");
2186 delete httpState;
2187 return;
2188 }
2189
2190 statCounter.server.all.requests++;
2191 statCounter.server.http.requests++;
2192
2193 /*
2194 * We used to set the read timeout here, but not any more.
2195 * Now its set in httpSendComplete() after the full request,
2196 * including request body, has been written to the server.
2197 */
2198 }
2199
2200 /// if broken posts are enabled for the request, try to fix and return true
2201 bool
2202 HttpStateData::finishingBrokenPost()
2203 {
2204 #if USE_HTTP_VIOLATIONS
2205 if (!Config.accessList.brokenPosts) {
2206 debugs(11, 5, HERE << "No brokenPosts list");
2207 return false;
2208 }
2209
2210 ACLFilledChecklist ch(Config.accessList.brokenPosts, request, NULL);
2211 if (!ch.fastCheck()) {
2212 debugs(11, 5, HERE << "didn't match brokenPosts");
2213 return false;
2214 }
2215
2216 if (!canSend(fd)) {
2217 debugs(11,2, HERE << "ignoring broken POST for closing FD " << fd);
2218 assert(closeHandler != NULL);
2219 return true; // prevent caller from proceeding as if nothing happened
2220 }
2221
2222 debugs(11, 2, "finishingBrokenPost: fixing broken POST");
2223 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2224 requestSender = JobCallback(11,5,
2225 Dialer, this, HttpStateData::wroteLast);
2226 Comm::Write(fd, "\r\n", 2, requestSender, NULL);
2227 return true;
2228 #else
2229 return false;
2230 #endif /* USE_HTTP_VIOLATIONS */
2231 }
2232
2233 /// if needed, write last-chunk to end the request body and return true
2234 bool
2235 HttpStateData::finishingChunkedRequest()
2236 {
2237 if (flags.sentLastChunk) {
2238 debugs(11, 5, HERE << "already sent last-chunk");
2239 return false;
2240 }
2241
2242 Must(receivedWholeRequestBody); // or we should not be sending last-chunk
2243 flags.sentLastChunk = true;
2244
2245 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
2246 requestSender = JobCallback(11,5,
2247 Dialer, this, HttpStateData::wroteLast);
2248 Comm::Write(fd, "0\r\n\r\n", 5, requestSender, NULL);
2249 return true;
2250 }
2251
2252 void
2253 HttpStateData::doneSendingRequestBody()
2254 {
2255 ServerStateData::doneSendingRequestBody();
2256 debugs(11,5, HERE << "doneSendingRequestBody: FD " << fd);
2257
2258 // do we need to write something after the last body byte?
2259 if (flags.chunked_request && finishingChunkedRequest())
2260 return;
2261 if (!flags.chunked_request && finishingBrokenPost())
2262 return;
2263
2264 sendComplete();
2265 }
2266
2267 // more origin request body data is available
2268 void
2269 HttpStateData::handleMoreRequestBodyAvailable()
2270 {
2271 if (eof || fd < 0) {
2272 // XXX: we should check this condition in other callbacks then!
2273 // TODO: Check whether this can actually happen: We should unsubscribe
2274 // as a body consumer when the above condition(s) are detected.
2275 debugs(11, 1, HERE << "Transaction aborted while reading HTTP body");
2276 return;
2277 }
2278
2279 assert(requestBodySource != NULL);
2280
2281 if (requestBodySource->buf().hasContent()) {
2282 // XXX: why does not this trigger a debug message on every request?
2283
2284 if (flags.headers_parsed && !flags.abuse_detected) {
2285 flags.abuse_detected = 1;
2286 debugs(11, 1, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << orig_request->client_addr << "' -> '" << entry->url() << "'" );
2287
2288 if (virginReply()->sline.status == HTTP_INVALID_HEADER) {
2289 comm_close(fd);
2290 return;
2291 }
2292 }
2293 }
2294
2295 HttpStateData::handleMoreRequestBodyAvailable();
2296 }
2297
2298 // premature end of the request body
2299 void
2300 HttpStateData::handleRequestBodyProducerAborted()
2301 {
2302 ServerStateData::handleRequestBodyProducerAborted();
2303 if (entry->isEmpty()) {
2304 debugs(11, 3, "request body aborted: FD " << fd);
2305 ErrorState *err;
2306 err = errorCon(ERR_READ_ERROR, HTTP_BAD_GATEWAY, fwd->request);
2307 err->xerrno = errno;
2308 fwd->fail(err);
2309 }
2310
2311 abortTransaction("request body producer aborted");
2312 }
2313
2314 // called when we wrote request headers(!) or a part of the body
2315 void
2316 HttpStateData::sentRequestBody(const CommIoCbParams &io)
2317 {
2318 if (io.size > 0)
2319 kb_incr(&statCounter.server.http.kbytes_out, io.size);
2320
2321 ServerStateData::sentRequestBody(io);
2322 }
2323
2324 // Quickly abort the transaction
2325 // TODO: destruction should be sufficient as the destructor should cleanup,
2326 // including canceling close handlers
2327 void
2328 HttpStateData::abortTransaction(const char *reason)
2329 {
2330 debugs(11,5, HERE << "aborting transaction for " << reason <<
2331 "; FD " << fd << ", this " << this);
2332
2333 if (fd >= 0) {
2334 comm_close(fd);
2335 return;
2336 }
2337
2338 fwd->handleUnregisteredServerEnd();
2339 deleteThis("HttpStateData::abortTransaction");
2340 }
2341
2342 HttpRequest *
2343 HttpStateData::originalRequest()
2344 {
2345 return orig_request;
2346 }