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