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