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