]> git.ipfire.org Git - thirdparty/squid.git/blame - src/http.cc
Merged from trunk
[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"
fc54b8d2 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"
fc54b8d2 59#include "HttpHeaderTools.h"
9ca29d23
AJ
60#include "HttpReply.h"
61#include "HttpRequest.h"
fc54b8d2 62#include "log/access_log.h"
9ca29d23
AJ
63#include "MemBuf.h"
64#include "MemObject.h"
fc54b8d2 65#include "neighbors.h"
582c2af2 66#include "profiler/Profiler.h"
9ca29d23 67#include "protos.h"
fc54b8d2 68#include "refresh.h"
1fa9b1a7 69#include "rfc1738.h"
985c86bc 70#include "SquidTime.h"
e4f1fdae 71#include "StatCounters.h"
9ca29d23 72#include "Store.h"
fc54b8d2
FC
73#include "tools.h"
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
d67acb4e
AJ
794/**
795 * returns true if the peer can support connection pinning
796*/
797bool HttpStateData::peerSupportsConnectionPinning() const
798{
799 const HttpReply *rep = entry->mem_obj->getReply();
800 const HttpHeader *hdr = &rep->header;
801 bool rc;
802 String header;
803
804 if (!_peer)
9e008dda
AJ
805 return true;
806
807 /*If this peer does not support connection pinning (authenticated
d67acb4e
AJ
808 connections) return false
809 */
810 if (!_peer->connection_auth)
9e008dda 811 return false;
d67acb4e 812
9e008dda 813 /*The peer supports connection pinning and the http reply status
d67acb4e
AJ
814 is not unauthorized, so the related connection can be pinned
815 */
816 if (rep->sline.status != HTTP_UNAUTHORIZED)
9e008dda
AJ
817 return true;
818
819 /*The server respond with HTTP_UNAUTHORIZED and the peer configured
820 with "connection-auth=on" we know that the peer supports pinned
d67acb4e
AJ
821 connections
822 */
823 if (_peer->connection_auth == 1)
9e008dda 824 return true;
d67acb4e 825
9e008dda
AJ
826 /*At this point peer has configured with "connection-auth=auto"
827 parameter so we need some extra checks to decide if we are going
d67acb4e
AJ
828 to allow pinned connections or not
829 */
830
9e008dda 831 /*if the peer configured with originserver just allow connection
d67acb4e
AJ
832 pinning (squid 2.6 behaviour)
833 */
834 if (_peer->options.originserver)
9e008dda 835 return true;
d67acb4e
AJ
836
837 /*if the connections it is already pinned it is OK*/
838 if (request->flags.pinned)
9e008dda
AJ
839 return true;
840
841 /*Allow pinned connections only if the Proxy-support header exists in
842 reply and has in its list the "Session-Based-Authentication"
d67acb4e
AJ
843 which means that the peer supports connection pinning.
844 */
845 if (!hdr->has(HDR_PROXY_SUPPORT))
9e008dda 846 return false;
d67acb4e
AJ
847
848 header = hdr->getStrOrList(HDR_PROXY_SUPPORT);
849 /* XXX This ought to be done in a case-insensitive manner */
d53b3f6d 850 rc = (strstr(header.termedBuf(), "Session-Based-Authentication") != NULL);
d67acb4e
AJ
851
852 return rc;
853}
854
5f8252d2 855// Called when we parsed (and possibly adapted) the headers but
856// had not starting storing (a.k.a., sending) the body yet.
2afaba07 857void
858HttpStateData::haveParsedReplyHeaders()
859{
c1520b67
AJ
860 ServerStateData::haveParsedReplyHeaders();
861
2afaba07 862 Ctx ctx = ctx_enter(entry->mem_obj->url);
585ab260 863 HttpReply *rep = finalReply();
2afaba07 864
585ab260 865 if (rep->sline.status == HTTP_PARTIAL_CONTENT &&
866 rep->content_range)
867 currentOffset = rep->content_range->spec.offset;
62e76326 868
3900307b 869 entry->timestampsSet();
62e76326 870
9bc73deb 871 /* Check if object is cacheable or not based on reply code */
585ab260 872 debugs(11, 3, "haveParsedReplyHeaders: HTTP CODE: " << rep->sline.status);
62e76326 873
9bc73deb 874 if (neighbors_do_private_keys)
585ab260 875 httpMaybeRemovePublic(entry, rep->sline.status);
e6ccf245 876
585ab260 877 if (rep->header.has(HDR_VARY)
f66a9ef4 878#if X_ACCELERATOR_VARY
585ab260 879 || rep->header.has(HDR_X_ACCELERATOR_VARY)
f66a9ef4 880#endif
4b44c907 881 ) {
e24f13cd 882 const char *vary = httpMakeVaryMark(request, rep);
4b44c907 883
884 if (!vary) {
5ed72359 885 entry->makePrivate();
d7d3253b
AJ
886 if (!fwd->reforwardableStatus(rep->sline.status))
887 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
4b44c907 888 goto no_cache;
62e76326 889 }
890
4b44c907 891 entry->mem_obj->vary_headers = xstrdup(vary);
892 }
893
2afaba07 894 /*
895 * If its not a reply that we will re-forward, then
896 * allow the client to get it.
897 */
585ab260 898 if (!fwd->reforwardableStatus(rep->sline.status))
2afaba07 899 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
900
4b44c907 901 switch (cacheableReply()) {
902
903 case 1:
5ed72359 904 entry->makePublic();
62e76326 905 break;
906
9bc73deb 907 case 0:
5ed72359 908 entry->makePrivate();
62e76326 909 break;
910
9bc73deb 911 case -1:
4b44c907 912
626096be 913#if USE_HTTP_VIOLATIONS
4b44c907 914 if (Config.negativeTtl > 0)
5ed72359 915 entry->cacheNegatively();
4b44c907 916 else
ac9cc053 917#endif
5ed72359 918 entry->makePrivate();
4b44c907 919
62e76326 920 break;
921
9bc73deb 922 default:
62e76326 923 assert(0);
4b44c907 924
62e76326 925 break;
9bc73deb 926 }
62e76326 927
4b44c907 928no_cache:
929
585ab260 930 if (!ignoreCacheControl && rep->cache_control) {
4ce6e3b5
FC
931 if (rep->cache_control->proxyRevalidate() ||
932 rep->cache_control->mustRevalidate() ||
d74ad83f 933 rep->cache_control->hasSMaxAge()
77da1817 934 )
62e76326 935 EBIT_SET(entry->flags, ENTRY_REVALIDATE);
9bc73deb 936 }
62e76326 937
c3609322 938#if HEADERS_LOG
585ab260 939 headersLog(1, 0, request->method, rep);
fc68f6b1 940
c3609322 941#endif
5f8252d2 942
943 ctx_exit(ctx);
f5558c95 944}
945
528b2c61 946HttpStateData::ConnectionStatus
947HttpStateData::statusIfComplete() const
603a02fd 948{
585ab260 949 const HttpReply *rep = virginReply();
073ba374
AJ
950 /** \par
951 * If the reply wants to close the connection, it takes precedence */
62e76326 952
2afaba07 953 if (httpHeaderHasConnDir(&rep->header, "close"))
62e76326 954 return COMPLETE_NONPERSISTENT_MSG;
955
073ba374
AJ
956 /** \par
957 * If we didn't send a keep-alive request header, then this
978e455f 958 * can not be a persistent connection.
959 */
528b2c61 960 if (!flags.keepalive)
62e76326 961 return COMPLETE_NONPERSISTENT_MSG;
962
073ba374 963 /** \par
72b63f06 964 * If we haven't sent the whole request then this can not be a persistent
965 * connection.
966 */
967 if (!flags.request_sent) {
e24f13cd 968 debugs(11, 2, "statusIfComplete: Request not yet fully sent \"" << RequestMethodStr(request->method) << " " << entry->url() << "\"" );
72b63f06 969 return COMPLETE_NONPERSISTENT_MSG;
970 }
971
073ba374 972 /** \par
9f5a2895 973 * What does the reply have to say about keep-alive?
974 */
073ba374
AJ
975 /**
976 \bug XXX BUG?
b6a2f15e 977 * If the origin server (HTTP/1.0) does not send a keep-alive
978 * header, but keeps the connection open anyway, what happens?
979 * We'll return here and http.c waits for an EOF before changing
980 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
981 * and an error status code, and we might have to wait until
982 * the server times out the socket.
983 */
2afaba07 984 if (!rep->keep_alive)
528b2c61 985 return COMPLETE_NONPERSISTENT_MSG;
62e76326 986
528b2c61 987 return COMPLETE_PERSISTENT_MSG;
988}
989
990HttpStateData::ConnectionStatus
991HttpStateData::persistentConnStatus() const
992{
9cf7de1b 993 debugs(11, 3, HERE << serverConnection << " eof=" << eof);
839291ac
AJ
994 if (eof) // already reached EOF
995 return COMPLETE_NONPERSISTENT_MSG;
996
505c2f28
AR
997 /* If server fd is closing (but we have not been notified yet), stop Comm
998 I/O to avoid assertions. TODO: Change Comm API to handle callers that
999 want more I/O after async closing (usually initiated by others). */
1000 // XXX: add canReceive or s/canSend/canTalkToServer/
e7cea0ed 1001 if (!Comm::IsConnOpen(serverConnection))
505c2f28
AR
1002 return COMPLETE_NONPERSISTENT_MSG;
1003
9035d1d5
AJ
1004 /** \par
1005 * In chunked response we do not know the content length but we are absolutely
af0bb8e5 1006 * sure about the end of response, so we are calling the statusIfComplete to
9e008dda 1007 * decide if we can be persistant
af0bb8e5 1008 */
839291ac 1009 if (lastChunk && flags.chunked)
9e008dda 1010 return statusIfComplete();
af0bb8e5 1011
718d84bf
AR
1012 const HttpReply *vrep = virginReply();
1013 debugs(11, 5, "persistentConnStatus: content_length=" << vrep->content_length);
1014
47f6e231 1015 const int64_t clen = vrep->bodySize(request->method);
fc68f6b1 1016
bf8fe701 1017 debugs(11, 5, "persistentConnStatus: clen=" << clen);
2afaba07 1018
35282fbf 1019 /* If the body size is unknown we must wait for EOF */
1020 if (clen < 0)
62e76326 1021 return INCOMPLETE_MSG;
1022
9035d1d5
AJ
1023 /** \par
1024 * If the body size is known, we must wait until we've gotten all of it. */
5f8252d2 1025 if (clen > 0) {
1026 // old technique:
585ab260 1027 // if (entry->mem_obj->endOffset() < vrep->content_length + vrep->hdr_sz)
47f6e231 1028 const int64_t body_bytes_read = reply_bytes_read - header_bytes_read;
5f8252d2 1029 debugs(11,5, "persistentConnStatus: body_bytes_read=" <<
585ab260 1030 body_bytes_read << " content_length=" << vrep->content_length);
2afaba07 1031
585ab260 1032 if (body_bytes_read < vrep->content_length)
5f8252d2 1033 return INCOMPLETE_MSG;
821beb5e
AR
1034
1035 if (body_bytes_truncated > 0) // already read more than needed
1036 return COMPLETE_NONPERSISTENT_MSG; // disable pconns
5f8252d2 1037 }
62e76326 1038
9035d1d5
AJ
1039 /** \par
1040 * If there is no message body or we got it all, we can be persistent */
5f8252d2 1041 return statusIfComplete();
603a02fd 1042}
090089c4 1043
2afaba07 1044/*
1045 * This is the callback after some data has been read from the network
1046 */
dc56a9b1 1047/*
e5ee81f0 1048void
d53b3f6d 1049HttpStateData::ReadReplyWrapper(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
c4b7a5a9 1050{
1051 HttpStateData *httpState = static_cast<HttpStateData *>(data);
8d71285d 1052 assert (fd == httpState->serverConnection->fd);
d53b3f6d 1053 // assert(buf == readBuf->content());
1d5161bd 1054 PROF_start(HttpStateData_readReply);
e6edd8df 1055 httpState->readReply(len, flag, xerrno);
1d5161bd 1056 PROF_stop(HttpStateData_readReply);
c4b7a5a9 1057}
dc56a9b1 1058*/
073ba374 1059
2afdbf48 1060/* XXX this function is too long! */
c4b7a5a9 1061void
e6edd8df 1062HttpStateData::readReply(const CommIoCbParams &io)
090089c4 1063{
30a4f2a8 1064 int bin;
090089c4 1065 int clen;
dc56a9b1 1066 int len = io.size;
c4b7a5a9 1067
dc56a9b1 1068 flags.do_next_read = 0;
9e008dda 1069
3e4bebf8 1070 debugs(11, 5, HERE << io.conn << ": len " << len << ".");
62e76326 1071
7a7cc03f 1072 // Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us
dc56a9b1 1073 if (io.flag == COMM_ERR_CLOSING) {
bf8fe701 1074 debugs(11, 3, "http socket closing");
c4b7a5a9 1075 return;
1076 }
1077
e92e4e44 1078 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
6dd9a2e4 1079 abortTransaction("store entry aborted while reading reply");
62e76326 1080 return;
e92e4e44 1081 }
c4b7a5a9 1082
fdf55365 1083 // handle I/O errors
dc56a9b1 1084 if (io.flag != COMM_OK || len < 0) {
3e4bebf8 1085 debugs(11, 2, HERE << io.conn << ": read failure: " << xstrerror() << ".");
fdf55365 1086
dc56a9b1 1087 if (ignoreErrno(io.xerrno)) {
fdf55365 1088 flags.do_next_read = 1;
1089 } else {
913524f0 1090 ErrorState *err = new ErrorState(ERR_READ_ERROR, HTTP_BAD_GATEWAY, fwd->request);
dc56a9b1 1091 err->xerrno = io.xerrno;
fdf55365 1092 fwd->fail(err);
1093 flags.do_next_read = 0;
8d71285d 1094 serverConnection->close();
fdf55365 1095 }
1096
1097 return;
1098 }
1099
7a7cc03f 1100 // update I/O stats
fdf55365 1101 if (len > 0) {
2afaba07 1102 readBuf->appended(len);
5f8252d2 1103 reply_bytes_read += len;
9a0a18de 1104#if USE_DELAY_POOLS
2afaba07 1105 DelayId delayId = entry->mem_obj->mostBytesAllowed();
62e76326 1106 delayId.bytesIn(len);
447e176b 1107#endif
62e76326 1108
e4f1fdae
FC
1109 kb_incr(&(statCounter.server.all.kbytes_in), len);
1110 kb_incr(&(statCounter.server.http.kbytes_in), len);
95dc7ff4 1111 ++ IOStats.Http.reads;
62e76326 1112
95dc7ff4 1113 for (clen = len - 1, bin = 0; clen; ++bin)
62e76326 1114 clen >>= 1;
1115
95dc7ff4 1116 ++ IOStats.Http.read_hist[bin];
3ff65596
AR
1117
1118 // update peer response time stats (%<pt)
e24f13cd
CT
1119 const timeval &sent = request->hier.peer_http_request_sent;
1120 request->hier.peer_response_time =
3ff65596 1121 sent.tv_sec ? tvSubMsec(sent, current_time) : -1;
30a4f2a8 1122 }
62e76326 1123
073ba374
AJ
1124 /** \par
1125 * Here the RFC says we should ignore whitespace between replies, but we can't as
5fa061b8 1126 * doing so breaks HTTP/0.9 replies beginning with witespace, and in addition
1127 * the response splitting countermeasures is extremely likely to trigger on this,
1128 * not allowing connection reuse in the first place.
648f6eb2
AJ
1129 *
1130 * 2012-02-10: which RFC? not 2068 or 2616,
1131 * tolerance there is all about whitespace between requests and header tokens.
5fa061b8 1132 */
5fa061b8 1133
ba82c452 1134 if (len == 0) { // reached EOF?
62e76326 1135 eof = 1;
f61f0107 1136 flags.do_next_read = 0;
da6c8415 1137
b73a07d6 1138 /* Bug 2879: Replies may terminate with \r\n then EOF instead of \r\n\r\n
da6c8415 1139 * Ensure here that we have at minimum two \r\n when EOF is seen.
b73a07d6 1140 * TODO: Add eof parameter to headersEnd() and move this hack there.
da6c8415 1141 */
b73a07d6 1142 if (readBuf->contentSize() && !flags.headers_parsed) {
da6c8415
AJ
1143 /*
1144 * Yes Henrik, there is a point to doing this. When we
1145 * called httpProcessReplyHeader() before, we didn't find
1146 * the end of headers, but now we are definately at EOF, so
1147 * we want to process the reply headers.
1148 */
1149 /* Fake an "end-of-headers" to work around such broken servers */
1150 readBuf->append("\r\n", 2);
da6c8415 1151 }
ba82c452 1152 }
62e76326 1153
655daa06
AR
1154 processReply();
1155}
1156
1157/// processes the already read and buffered response data, possibly after
1158/// waiting for asynchronous 1xx control message processing
1159void
de48b288
A
1160HttpStateData::processReply()
1161{
655daa06
AR
1162
1163 if (flags.handling1xx) { // we came back after handling a 1xx response
1164 debugs(11, 5, HERE << "done with 1xx handling");
1165 flags.handling1xx = false;
1166 Must(!flags.headers_parsed);
1167 }
1168
ba82c452 1169 if (!flags.headers_parsed) { // have not parsed headers yet?
1170 PROF_start(HttpStateData_processReplyHeader);
1171 processReplyHeader();
1172 PROF_stop(HttpStateData_processReplyHeader);
1173
1174 if (!continueAfterParsingHeader()) // parsing error or need more data
1175 return; // TODO: send errors to ICAP
1176
ab593f19 1177 adaptOrFinalizeReply(); // may write to, abort, or "close" the entry
ba82c452 1178 }
1179
1180 // kick more reads if needed and/or process the response body, if any
1181 PROF_start(HttpStateData_processReplyBody);
1182 processReplyBody(); // may call serverComplete()
1183 PROF_stop(HttpStateData_processReplyBody);
1184}
1185
073ba374
AJ
1186/**
1187 \retval true if we can continue with processing the body or doing ICAP.
1188 */
ba82c452 1189bool
1190HttpStateData::continueAfterParsingHeader()
1191{
655daa06
AR
1192 if (flags.handling1xx) {
1193 debugs(11, 5, HERE << "wait for 1xx handling");
1194 Must(!flags.headers_parsed);
1195 return false;
1196 }
1197
073ba374 1198 if (!flags.headers_parsed && !eof) {
ba82c452 1199 debugs(11, 9, HERE << "needs more at " << readBuf->contentSize());
1200 flags.do_next_read = 1;
073ba374
AJ
1201 /** \retval false If we have not finished parsing the headers and may get more data.
1202 * Schedules more reads to retrieve the missing data.
1203 */
ba82c452 1204 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
073ba374 1205 return false;
ba82c452 1206 }
1207
073ba374 1208 /** If we are done with parsing, check for errors */
ba82c452 1209
1210 err_type error = ERR_NONE;
1211
1212 if (flags.headers_parsed) { // parsed headers, possibly with errors
1213 // check for header parsing errors
585ab260 1214 if (HttpReply *vrep = virginReply()) {
1215 const http_status s = vrep->sline.status;
1216 const HttpVersion &v = vrep->sline.version;
ba82c452 1217 if (s == HTTP_INVALID_HEADER && v != HttpVersion(0,9)) {
e24f13cd 1218 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: Bad header encountered from " << entry->url() << " AKA " << request->GetHost() << request->urlpath.termedBuf() );
ba82c452 1219 error = ERR_INVALID_RESP;
e1381638
AJ
1220 } else if (s == HTTP_HEADER_TOO_LARGE) {
1221 fwd->dontRetry(true);
1222 error = ERR_TOO_BIG;
1223 } else {
1224 return true; // done parsing, got reply, and no error
1225 }
ba82c452 1226 } else {
1227 // parsed headers but got no reply
e24f13cd 1228 debugs(11, DBG_IMPORTANT, "WARNING: HTTP: Invalid Response: No reply at all for " << entry->url() << " AKA " << request->GetHost() << request->urlpath.termedBuf() );
ba82c452 1229 error = ERR_INVALID_RESP;
62e76326 1230 }
090089c4 1231 } else {
ba82c452 1232 assert(eof);
9121eba6
AJ
1233 if (readBuf->hasContent()) {
1234 error = ERR_INVALID_RESP;
e24f13cd 1235 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
1236 } else {
1237 error = ERR_ZERO_SIZE_OBJECT;
e24f13cd
CT
1238 debugs(11, (request->flags.accelerated?DBG_IMPORTANT:2), "WARNING: HTTP: Invalid Response: No object data received for " <<
1239 entry->url() << " AKA " << request->GetHost() << request->urlpath.termedBuf() );
9121eba6 1240 }
2afaba07 1241 }
ba82c452 1242
1243 assert(error != ERR_NONE);
1244 entry->reset();
913524f0 1245 fwd->fail(new ErrorState(error, HTTP_BAD_GATEWAY, fwd->request));
ba82c452 1246 flags.do_next_read = 0;
8d71285d 1247 serverConnection->close();
ba82c452 1248 return false; // quit on error
2afaba07 1249}
1250
821beb5e
AR
1251/** truncate what we read if we read too much so that writeReplyBody()
1252 writes no more than what we should have read */
1253void
1254HttpStateData::truncateVirginBody()
1255{
1256 assert(flags.headers_parsed);
1257
1258 HttpReply *vrep = virginReply();
1259 int64_t clen = -1;
1260 if (!vrep->expectingBody(request->method, clen) || clen < 0)
1261 return; // no body or a body of unknown size, including chunked
1262
1263 const int64_t body_bytes_read = reply_bytes_read - header_bytes_read;
e1381638 1264 if (body_bytes_read - body_bytes_truncated <= clen)
821beb5e
AR
1265 return; // we did not read too much or already took care of the extras
1266
1267 if (const int64_t extras = body_bytes_read - body_bytes_truncated - clen) {
1268 // server sent more that the advertised content length
e1381638
AJ
1269 debugs(11,5, HERE << "body_bytes_read=" << body_bytes_read <<
1270 " clen=" << clen << '/' << vrep->content_length <<
1271 " body_bytes_truncated=" << body_bytes_truncated << '+' << extras);
821beb5e
AR
1272
1273 readBuf->truncate(extras);
1274 body_bytes_truncated += extras;
1275 }
1276}
1277
073ba374 1278/**
2afaba07 1279 * Call this when there is data from the origin server
1280 * which should be sent to either StoreEntry, or to ICAP...
1281 */
1282void
5f8252d2 1283HttpStateData::writeReplyBody()
2afaba07 1284{
821beb5e 1285 truncateVirginBody(); // if needed
5f8252d2 1286 const char *data = readBuf->content();
1287 int len = readBuf->contentSize();
bc81cb2b 1288 addVirginReplyBody(data, len);
5f8252d2 1289 readBuf->consume(len);
af0bb8e5 1290}
fc68f6b1 1291
af0bb8e5 1292bool
1293HttpStateData::decodeAndWriteReplyBody()
1294{
1295 const char *data = NULL;
1296 int len;
e053c141 1297 bool wasThereAnException = false;
af0bb8e5 1298 assert(flags.chunked);
1299 assert(httpChunkDecoder);
1300 SQUID_ENTER_THROWING_CODE();
1301 MemBuf decodedData;
1302 decodedData.init();
e053c141 1303 const bool doneParsing = httpChunkDecoder->parse(readBuf,&decodedData);
af0bb8e5 1304 len = decodedData.contentSize();
1305 data=decodedData.content();
1306 addVirginReplyBody(data, len);
e053c141 1307 if (doneParsing) {
839291ac
AJ
1308 lastChunk = 1;
1309 flags.do_next_read = 0;
af0bb8e5 1310 }
e053c141
FC
1311 SQUID_EXIT_THROWING_CODE(wasThereAnException);
1312 return wasThereAnException;
e6ccf245 1313}
1314
073ba374 1315/**
2afaba07 1316 * processReplyBody has two purposes:
1317 * 1 - take the reply body data, if any, and put it into either
1318 * the StoreEntry, or give it over to ICAP.
1319 * 2 - see if we made it to the end of the response (persistent
1320 * connections and such)
1321 */
e6ccf245 1322void
2afaba07 1323HttpStateData::processReplyBody()
e6ccf245 1324{
b7ac5457 1325 Ip::Address client_addr;
d67acb4e 1326 bool ispinned = false;
fc68f6b1 1327
1a98175f 1328 if (!flags.headers_parsed) {
f61f0107 1329 flags.do_next_read = 1;
5f8252d2 1330 maybeReadVirginBody();
62e76326 1331 return;
528b2c61 1332 }
62e76326 1333
a83c6ed6 1334#if USE_ADAPTATION
c30ac6ea 1335 debugs(11,5, HERE << "adaptationAccessCheckPending=" << adaptationAccessCheckPending);
a83c6ed6 1336 if (adaptationAccessCheckPending)
2afaba07 1337 return;
fc68f6b1 1338
2afaba07 1339#endif
62e76326 1340
2afaba07 1341 /*
1342 * At this point the reply headers have been parsed and consumed.
1343 * That means header content has been removed from readBuf and
1344 * it contains only body data.
1345 */
ef85ab2f
DK
1346 if (entry->isAccepting()) {
1347 if (flags.chunked) {
1348 if (!decodeAndWriteReplyBody()) {
1349 flags.do_next_read = 0;
1350 serverComplete();
1351 return;
1352 }
1353 } else
1354 writeReplyBody();
1355 }
528b2c61 1356
e6ccf245 1357 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
6dd9a2e4
AJ
1358 // The above writeReplyBody() call may have aborted the store entry.
1359 abortTransaction("store entry aborted while storing reply");
1360 return;
62e76326 1361 } else
1362 switch (persistentConnStatus()) {
dc49061a 1363 case INCOMPLETE_MSG: {
3e4bebf8 1364 debugs(11, 5, "processReplyBody: INCOMPLETE_MSG from " << serverConnection);
21b92762 1365 /* Wait for more data or EOF condition */
8d77a37c 1366 AsyncCall::Pointer nil;
21b92762 1367 if (flags.keepalive_broken) {
8d77a37c 1368 commSetConnTimeout(serverConnection, 10, nil);
21b92762 1369 } else {
8d77a37c 1370 commSetConnTimeout(serverConnection, Config.Timeout.read, nil);
21b92762 1371 }
1372
f61f0107 1373 flags.do_next_read = 1;
dc49061a
A
1374 }
1375 break;
62e76326 1376
1377 case COMPLETE_PERSISTENT_MSG:
3e4bebf8 1378 debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG from " << serverConnection);
62e76326 1379 /* yes we have to clear all these! */
8d77a37c 1380 commUnsetConnTimeout(serverConnection);
f61f0107 1381 flags.do_next_read = 0;
62e76326 1382
8d71285d 1383 comm_remove_close_handler(serverConnection->fd, closeHandler);
dc56a9b1 1384 closeHandler = NULL;
8d71285d 1385 fwd->unregister(serverConnection);
fc68f6b1 1386
e24f13cd
CT
1387 if (request->flags.spoof_client_ip)
1388 client_addr = request->client_addr;
fc68f6b1 1389
9e008dda
AJ
1390 if (request->flags.pinned) {
1391 ispinned = true;
1392 } else if (request->flags.connection_auth && request->flags.auth_sent) {
1393 ispinned = true;
1394 }
1395
e24f13cd
CT
1396 if (request->pinnedConnection() && ispinned) {
1397 request->pinnedConnection()->pinConnection(serverConnection, request, _peer,
9e008dda 1398 (request->flags.connection_auth != 0));
bd0723ad 1399 } else {
e24f13cd 1400 fwd->pconnPush(serverConnection, request->peer_host ? request->peer_host : request->GetHost());
bd0723ad 1401 }
1402
8d71285d 1403 serverConnection = NULL;
5f8252d2 1404 serverComplete();
62e76326 1405 return;
1406
1407 case COMPLETE_NONPERSISTENT_MSG:
3e4bebf8 1408 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG from " << serverConnection);
5f8252d2 1409 serverComplete();
62e76326 1410 return;
1411 }
1412
5f8252d2 1413 maybeReadVirginBody();
c4b7a5a9 1414}
1415
1416void
5f8252d2 1417HttpStateData::maybeReadVirginBody()
c4b7a5a9 1418{
85bef0a7
AR
1419 // too late to read
1420 if (!Comm::IsConnOpen(serverConnection) || fd_table[serverConnection->fd].closing())
1421 return;
1422
52edecde 1423 // we may need to grow the buffer if headers do not fit
1c9605c5 1424 const int minRead = flags.headers_parsed ? 0 :1024;
d5f8d05f 1425 const int read_size = replyBodySpace(*readBuf, minRead);
2afaba07 1426
5f8252d2 1427 debugs(11,9, HERE << (flags.do_next_read ? "may" : "wont") <<
9cf7de1b 1428 " read up to " << read_size << " bytes from " << serverConnection);
2afaba07 1429
1430 /*
1431 * why <2? Because delayAwareRead() won't actually read if
1432 * you ask it to read 1 byte. The delayed read request
1433 * just gets re-queued until the client side drains, then
1434 * the I/O thread hangs. Better to not register any read
1435 * handler until we get a notification from someone that
1436 * its okay to read again.
1437 */
d5f8d05f 1438 if (read_size < 2)
52edecde 1439 return;
2afaba07 1440
f61f0107 1441 if (flags.do_next_read) {
dc56a9b1 1442 flags.do_next_read = 0;
1443 typedef CommCbMemFunT<HttpStateData, CommIoCbParams> Dialer;
3e4bebf8 1444 entry->delayAwareRead(serverConnection, readBuf->space(read_size), read_size,
4cb2536f 1445 JobCallback(11, 5, Dialer, this, HttpStateData::readReply));
528b2c61 1446 }
090089c4 1447}
1448
39cb8c41 1449/// called after writing the very last request byte (body, last-chunk, etc)
d576a6a6 1450void
39cb8c41 1451HttpStateData::wroteLast(const CommIoCbParams &io)
090089c4 1452{
9cf7de1b 1453 debugs(11, 5, HERE << serverConnection << ": size " << io.size << ": errflag " << io.flag << ".");
bc87dc25 1454#if URL_CHECKSUM_DEBUG
62e76326 1455
528b2c61 1456 entry->mem_obj->checkUrlChecksum();
bc87dc25 1457#endif
62e76326 1458
dc56a9b1 1459 if (io.size > 0) {
49ae8b95 1460 fd_bytes(io.fd, io.size, FD_WRITE);
e4f1fdae
FC
1461 kb_incr(&(statCounter.server.all.kbytes_out), io.size);
1462 kb_incr(&(statCounter.server.http.kbytes_out), io.size);
ee1679df 1463 }
62e76326 1464
dc56a9b1 1465 if (io.flag == COMM_ERR_CLOSING)
62e76326 1466 return;
1467
dc56a9b1 1468 if (io.flag) {
913524f0 1469 ErrorState *err = new ErrorState(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY, fwd->request);
dc56a9b1 1470 err->xerrno = io.xerrno;
1471 fwd->fail(err);
8d71285d 1472 serverConnection->close();
62e76326 1473 return;
090089c4 1474 }
72b63f06 1475
39cb8c41
AR
1476 sendComplete();
1477}
1478
1479/// successfully wrote the entire request (including body, last-chunk, etc.)
1480void
1481HttpStateData::sendComplete()
1482{
2afaba07 1483 /*
1484 * Set the read timeout here because it hasn't been set yet.
1485 * We only set the read timeout after the request has been
1486 * fully written to the server-side. If we start the timeout
1487 * after connection establishment, then we are likely to hit
1488 * the timeout for POST/PUT requests that have very large
1489 * request bodies.
1490 */
dc56a9b1 1491 typedef CommCbMemFunT<HttpStateData, CommTimeoutCbParams> TimeoutDialer;
4299f876 1492 AsyncCall::Pointer timeoutCall = JobCallback(11, 5,
4cb2536f 1493 TimeoutDialer, this, HttpStateData::httpTimeout);
2afaba07 1494
8d77a37c 1495 commSetConnTimeout(serverConnection, Config.Timeout.read, timeoutCall);
dc56a9b1 1496
1497 flags.request_sent = 1;
e1381638 1498
e24f13cd 1499 request->hier.peer_http_request_sent = current_time;
090089c4 1500}
1501
5f8252d2 1502// Close the HTTP server connection. Used by serverComplete().
2afaba07 1503void
5f8252d2 1504HttpStateData::closeServer()
2afaba07 1505{
9cf7de1b 1506 debugs(11,5, HERE << "closing HTTP server " << serverConnection << " this " << this);
fc68f6b1 1507
9cf7de1b 1508 if (Comm::IsConnOpen(serverConnection)) {
8d71285d
AJ
1509 fwd->unregister(serverConnection);
1510 comm_remove_close_handler(serverConnection->fd, closeHandler);
dc56a9b1 1511 closeHandler = NULL;
8d71285d 1512 serverConnection->close();
2afaba07 1513 }
5f8252d2 1514}
2afaba07 1515
5f8252d2 1516bool
1517HttpStateData::doneWithServer() const
1518{
9cf7de1b 1519 return !Comm::IsConnOpen(serverConnection);
2afaba07 1520}
1521
ee0b94f4
HN
1522/*
1523 * Fixup authentication request headers for special cases
1524 */
1525static void
e24f13cd 1526httpFixupAuthentication(HttpRequest * request, const HttpHeader * hdr_in, HttpHeader * hdr_out, http_state_flags flags)
ee0b94f4
HN
1527{
1528 http_hdr_type header = flags.originpeer ? HDR_AUTHORIZATION : HDR_PROXY_AUTHORIZATION;
1529
1530 /* Nothing to do unless we are forwarding to a peer */
1531 if (!request->flags.proxying)
f54f527e 1532 return;
ee0b94f4
HN
1533
1534 /* Needs to be explicitly enabled */
e24f13cd 1535 if (!request->peer_login)
f54f527e 1536 return;
ee0b94f4
HN
1537
1538 /* Maybe already dealt with? */
1539 if (hdr_out->has(header))
f54f527e 1540 return;
ee0b94f4
HN
1541
1542 /* Nothing to do here for PASSTHRU */
e24f13cd 1543 if (strcmp(request->peer_login, "PASSTHRU") == 0)
f54f527e 1544 return;
ee0b94f4
HN
1545
1546 /* PROXYPASS is a special case, single-signon to servers with the proxy password (basic only) */
e24f13cd 1547 if (flags.originpeer && strcmp(request->peer_login, "PROXYPASS") == 0 && hdr_in->has(HDR_PROXY_AUTHORIZATION)) {
f54f527e 1548 const char *auth = hdr_in->getStr(HDR_PROXY_AUTHORIZATION);
ee0b94f4 1549
f54f527e
AJ
1550 if (auth && strncasecmp(auth, "basic ", 6) == 0) {
1551 hdr_out->putStr(header, auth);
1552 return;
1553 }
ee0b94f4
HN
1554 }
1555
1556 /* Special mode to pass the username to the upstream cache */
e24f13cd 1557 if (*request->peer_login == '*') {
f54f527e
AJ
1558 char loginbuf[256];
1559 const char *username = "-";
ee0b94f4 1560
e24f13cd
CT
1561 if (request->extacl_user.size())
1562 username = request->extacl_user.termedBuf();
2f1431ea 1563#if USE_AUTH
e24f13cd
CT
1564 else if (request->auth_user_request != NULL)
1565 username = request->auth_user_request->username();
2f1431ea 1566#endif
ee0b94f4 1567
e24f13cd 1568 snprintf(loginbuf, sizeof(loginbuf), "%s%s", username, request->peer_login + 1);
ee0b94f4 1569
f54f527e 1570 httpHeaderPutStrf(hdr_out, header, "Basic %s",
8bdd0cec 1571 old_base64_encode(loginbuf));
f54f527e 1572 return;
ee0b94f4
HN
1573 }
1574
1575 /* external_acl provided credentials */
e24f13cd
CT
1576 if (request->extacl_user.size() && request->extacl_passwd.size() &&
1577 (strcmp(request->peer_login, "PASS") == 0 ||
1578 strcmp(request->peer_login, "PROXYPASS") == 0)) {
f54f527e
AJ
1579 char loginbuf[256];
1580 snprintf(loginbuf, sizeof(loginbuf), SQUIDSTRINGPH ":" SQUIDSTRINGPH,
e24f13cd
CT
1581 SQUIDSTRINGPRINT(request->extacl_user),
1582 SQUIDSTRINGPRINT(request->extacl_passwd));
f54f527e 1583 httpHeaderPutStrf(hdr_out, header, "Basic %s",
8bdd0cec 1584 old_base64_encode(loginbuf));
f54f527e 1585 return;
ee0b94f4 1586 }
8fdaa8af
AJ
1587 // if no external user credentials are available to fake authentication with PASS acts like PASSTHRU
1588 if (strcmp(request->peer_login, "PASS") == 0)
1589 continue;
ee0b94f4 1590
9ca29d23 1591 /* Kerberos login to peer */
2f1431ea 1592#if HAVE_AUTH_MODULE_NEGOTIATE && HAVE_KRB5 && HAVE_GSSAPI
e24f13cd 1593 if (strncmp(request->peer_login, "NEGOTIATE",strlen("NEGOTIATE")) == 0) {
9ca29d23
AJ
1594 char *Token=NULL;
1595 char *PrincipalName=NULL,*p;
e24f13cd 1596 if ((p=strchr(request->peer_login,':')) != NULL ) {
9ca29d23
AJ
1597 PrincipalName=++p;
1598 }
e24f13cd 1599 Token = peer_proxy_negotiate_auth(PrincipalName, request->peer_host);
9ca29d23 1600 if (Token) {
63f03f79 1601 httpHeaderPutStrf(hdr_out, header, "Negotiate %s",Token);
9ca29d23
AJ
1602 }
1603 return;
1604 }
1605#endif /* HAVE_KRB5 && HAVE_GSSAPI */
1606
0606266f 1607 httpHeaderPutStrf(hdr_out, header, "Basic %s",
e24f13cd 1608 old_base64_encode(request->peer_login));
ee0b94f4
HN
1609 return;
1610}
1611
99edd1c3 1612/*
9e008dda 1613 * build request headers and append them to a given MemBuf
e5ee81f0 1614 * used by buildRequestPrefix()
818c6c9e 1615 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
99edd1c3 1616 */
e1e72f06 1617void
e5ee81f0 1618HttpStateData::httpBuildRequestHeader(HttpRequest * request,
e5ee81f0 1619 StoreEntry * entry,
4bf68cfa 1620 const AccessLogEntryPointer &al,
e5ee81f0 1621 HttpHeader * hdr_out,
386c7842 1622 const http_state_flags flags)
6bf8443a 1623{
99edd1c3 1624 /* building buffer for complex strings */
5999b776 1625#define BBUF_SZ (MAX_URL+32)
99edd1c3 1626 LOCAL_ARRAY(char, bbuf, BBUF_SZ);
67c06f0d 1627 LOCAL_ARRAY(char, ntoabuf, MAX_IPSTRLEN);
e24f13cd 1628 const HttpHeader *hdr_in = &request->header;
67c06f0d 1629 const HttpHeaderEntry *e = NULL;
99edd1c3 1630 HttpHeaderPos pos = HttpHeaderInitPos;
75faaa7a 1631 assert (hdr_out->owner == hoRequest);
62e76326 1632
262a0e14 1633 /* append our IMS header */
fa3e249f 1634 if (request->lastmod > -1)
a9925b40 1635 hdr_out->putTime(HDR_IF_MODIFIED_SINCE, request->lastmod);
99edd1c3 1636
e24f13cd 1637 bool we_do_ranges = decideIfWeDoRanges (request);
528b2c61 1638
30abd221 1639 String strConnection (hdr_in->getList(HDR_CONNECTION));
62e76326 1640
a9925b40 1641 while ((e = hdr_in->getEntry(&pos)))
e24f13cd 1642 copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, hdr_out, we_do_ranges, flags);
528b2c61 1643
43ae1d95 1644 /* Abstraction break: We should interpret multipart/byterange responses
528b2c61 1645 * into offset-length data, and this works around our inability to do so.
1646 */
e24f13cd 1647 if (!we_do_ranges && request->multipartRangeRequest()) {
62e76326 1648 /* don't cache the result */
e24f13cd 1649 request->flags.cachable = 0;
62e76326 1650 /* pretend it's not a range request */
e24f13cd
CT
1651 delete request->range;
1652 request->range = NULL;
1653 request->flags.range = 0;
62e76326 1654 }
528b2c61 1655
99edd1c3 1656 /* append Via */
736cb6aa 1657 if (Config.onoff.via) {
30abd221 1658 String strVia;
a9925b40 1659 strVia = hdr_in->getList(HDR_VIA);
62e76326 1660 snprintf(bbuf, BBUF_SZ, "%d.%d %s",
e24f13cd
CT
1661 request->http_ver.major,
1662 request->http_ver.minor, ThisCache);
62e76326 1663 strListAdd(&strVia, bbuf, ',');
d53b3f6d 1664 hdr_out->putStr(HDR_VIA, strVia.termedBuf());
30abd221 1665 strVia.clean();
736cb6aa 1666 }
62e76326 1667
e24f13cd 1668 if (request->flags.accelerated) {
43ae1d95 1669 /* Append Surrogate-Capabilities */
45cca89d
AJ
1670 String strSurrogate(hdr_in->getList(HDR_SURROGATE_CAPABILITY));
1671#if USE_SQUID_ESI
1672 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"", Config.Accel.surrogate_id);
1673#else
1674 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0\"", Config.Accel.surrogate_id);
1675#endif
43ae1d95 1676 strListAdd(&strSurrogate, bbuf, ',');
d53b3f6d 1677 hdr_out->putStr(HDR_SURROGATE_CAPABILITY, strSurrogate.termedBuf());
43ae1d95 1678 }
43ae1d95 1679
67c06f0d 1680 /** \pre Handle X-Forwarded-For */
9e008dda 1681 if (strcmp(opt_forwarded_for, "delete") != 0) {
c4f30223
AR
1682
1683 String strFwd = hdr_in->getList(HDR_X_FORWARDED_FOR);
1684
1685 if (strFwd.size() > 65536/2) {
1686 // There is probably a forwarding loop with Via detection disabled.
1687 // If we do nothing, String will assert on overflow soon.
1688 // TODO: Terminate all transactions with huge XFF?
1689 strFwd = "error";
1690
1691 static int warnedCount = 0;
1692 if (warnedCount++ < 100) {
e24f13cd 1693 const char *url = entry ? entry->url() : urlCanonical(request);
e0236918 1694 debugs(11, DBG_IMPORTANT, "Warning: likely forwarding loop with " << url);
c4f30223
AR
1695 }
1696 }
1697
9e008dda 1698 if (strcmp(opt_forwarded_for, "on") == 0) {
67c06f0d 1699 /** If set to ON - append client IP or 'unknown'. */
e24f13cd 1700 if ( request->client_addr.IsNoAddr() )
67c06f0d
AJ
1701 strListAdd(&strFwd, "unknown", ',');
1702 else
e24f13cd 1703 strListAdd(&strFwd, request->client_addr.NtoA(ntoabuf, MAX_IPSTRLEN), ',');
9e008dda 1704 } else if (strcmp(opt_forwarded_for, "off") == 0) {
67c06f0d 1705 /** If set to OFF - append 'unknown'. */
67c06f0d 1706 strListAdd(&strFwd, "unknown", ',');
9e008dda 1707 } else if (strcmp(opt_forwarded_for, "transparent") == 0) {
67c06f0d 1708 /** If set to TRANSPARENT - pass through unchanged. */
9e008dda 1709 } else if (strcmp(opt_forwarded_for, "truncate") == 0) {
67c06f0d 1710 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
e24f13cd 1711 if ( request->client_addr.IsNoAddr() )
67c06f0d
AJ
1712 strFwd = "unknown";
1713 else
e24f13cd 1714 strFwd = request->client_addr.NtoA(ntoabuf, MAX_IPSTRLEN);
67c06f0d 1715 }
9e008dda 1716 if (strFwd.size() > 0)
d53b3f6d 1717 hdr_out->putStr(HDR_X_FORWARDED_FOR, strFwd.termedBuf());
cc192b50 1718 }
67c06f0d 1719 /** If set to DELETE - do not copy through. */
6bccf575 1720
99edd1c3 1721 /* append Host if not there already */
a9925b40 1722 if (!hdr_out->has(HDR_HOST)) {
e24f13cd
CT
1723 if (request->peer_domain) {
1724 hdr_out->putStr(HDR_HOST, request->peer_domain);
1725 } else if (request->port == urlDefaultPort(request->protocol)) {
62e76326 1726 /* use port# only if not default */
e24f13cd 1727 hdr_out->putStr(HDR_HOST, request->GetHost());
62e76326 1728 } else {
1729 httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d",
e24f13cd
CT
1730 request->GetHost(),
1731 (int) request->port);
62e76326 1732 }
6bf8443a 1733 }
62e76326 1734
c68e9c6b 1735 /* append Authorization if known in URL, not in header and going direct */
a9925b40 1736 if (!hdr_out->has(HDR_AUTHORIZATION)) {
e24f13cd 1737 if (!request->flags.proxying && request->login && *request->login) {
62e76326 1738 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s",
8bdd0cec 1739 old_base64_encode(request->login));
62e76326 1740 }
c68e9c6b 1741 }
62e76326 1742
ee0b94f4 1743 /* Fixup (Proxy-)Authorization special cases. Plain relaying dealt with above */
e24f13cd 1744 httpFixupAuthentication(request, hdr_in, hdr_out, flags);
62e76326 1745
ee0b94f4
HN
1746 /* append Cache-Control, add max-age if not there already */
1747 {
a9925b40 1748 HttpHdrCc *cc = hdr_in->getCc();
62e76326 1749
1750 if (!cc)
a4a03b37 1751 cc = new HttpHdrCc();
62e76326 1752
7dc5c309
AJ
1753#if 0 /* see bug 2330 */
1754 /* Set no-cache if determined needed but not found */
e24f13cd 1755 if (request->flags.nocache)
7dc5c309
AJ
1756 EBIT_SET(cc->mask, CC_NO_CACHE);
1757#endif
1758
af6a12ee 1759 /* Add max-age only without no-cache */
904a4905 1760 if (!cc->hasMaxAge() && !cc->noCache()) {
43ae1d95 1761 const char *url =
e24f13cd 1762 entry ? entry->url() : urlCanonical(request);
cf7c2e94 1763 cc->maxAge(getMaxAge(url));
62e76326 1764
62e76326 1765 }
1766
ce2d6441 1767 /* Enforce sibling relations */
62e76326 1768 if (flags.only_if_cached)
4ce6e3b5 1769 cc->onlyIfCached(true);
62e76326 1770
a9925b40 1771 hdr_out->putCc(cc);
62e76326 1772
3d7782c1 1773 delete cc;
6bf8443a 1774 }
62e76326 1775
99edd1c3 1776 /* maybe append Connection: keep-alive */
b515fc11 1777 if (flags.keepalive) {
95e78500 1778 hdr_out->putStr(HDR_CONNECTION, "keep-alive");
603a02fd 1779 }
62e76326 1780
a7ad6e4e 1781 /* append Front-End-Https */
1782 if (flags.front_end_https) {
0c3d3f65 1783 if (flags.front_end_https == 1 || request->protocol == AnyP::PROTO_HTTPS)
a9925b40 1784 hdr_out->putStr(HDR_FRONT_END_HTTPS, "On");
a7ad6e4e 1785 }
1786
e31a1e67
AR
1787 if (flags.chunked_request) {
1788 // Do not just copy the original value so that if the client-side
1789 // starts decode other encodings, this code may remain valid.
39cb8c41
AR
1790 hdr_out->putStr(HDR_TRANSFER_ENCODING, "chunked");
1791 }
1792
6bccf575 1793 /* Now mangle the headers. */
4f56514c 1794 if (Config2.onoff.mangle_request_headers)
5967c0bf 1795 httpHdrMangleList(hdr_out, request, ROR_REQUEST);
62e76326 1796
f4698e0b 1797 if (Config.request_header_add && !Config.request_header_add->empty())
4bf68cfa 1798 httpHdrAdd(hdr_out, request, al, *Config.request_header_add);
f4698e0b 1799
30abd221 1800 strConnection.clean();
99edd1c3 1801}
1802
9e498bfb
AJ
1803/**
1804 * Decides whether a particular header may be cloned from the received Clients request
1805 * to our outgoing fetch request.
1806 */
528b2c61 1807void
e24f13cd 1808copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, const String strConnection, const HttpRequest * request, HttpHeader * hdr_out, const int we_do_ranges, const http_state_flags flags)
528b2c61 1809{
e8466ea9 1810 debugs(11, 5, "httpBuildRequestHeader: " << e->name << ": " << e->value );
62e76326 1811
528b2c61 1812 switch (e->id) {
62e76326 1813
af6a12ee 1814 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid should not pass on. */
9e498bfb 1815
be753325 1816 case HDR_PROXY_AUTHORIZATION:
9e498bfb
AJ
1817 /** \par Proxy-Authorization:
1818 * Only pass on proxy authentication to peers for which
62e76326 1819 * authentication forwarding is explicitly enabled
1820 */
e24f13cd
CT
1821 if (!flags.originpeer && flags.proxying && request->peer_login &&
1822 (strcmp(request->peer_login, "PASS") == 0 ||
1823 strcmp(request->peer_login, "PROXYPASS") == 0 ||
1824 strcmp(request->peer_login, "PASSTHRU") == 0)) {
eede25e7 1825 hdr_out->addEntry(e->clone());
62e76326 1826 }
62e76326 1827 break;
1828
af6a12ee 1829 /** \par RFC 2616 sect 13.5.1 - Hop-by-Hop headers which Squid does not pass on. */
9e498bfb
AJ
1830
1831 case HDR_CONNECTION: /** \par Connection: */
1832 case HDR_TE: /** \par TE: */
1833 case HDR_KEEP_ALIVE: /** \par Keep-Alive: */
1834 case HDR_PROXY_AUTHENTICATE: /** \par Proxy-Authenticate: */
a1651bad 1835 case HDR_TRAILER: /** \par Trailer: */
9e498bfb
AJ
1836 case HDR_UPGRADE: /** \par Upgrade: */
1837 case HDR_TRANSFER_ENCODING: /** \par Transfer-Encoding: */
1838 break;
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}