]> git.ipfire.org Git - thirdparty/squid.git/blame - src/http.cc
cosmetic: Remove silly "typedef class ClientHttpRequest clientHttpRequest"
[thirdparty/squid.git] / src / http.cc
CommitLineData
da2b3a17 1
30a4f2a8 2/*
032785bf 3 * $Id: http.cc,v 1.455 2005/08/31 19:15:36 wessels Exp $
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.
24 *
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
29 *
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
cbdec147 32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
e25c139f 33 *
30a4f2a8 34 */
019dd986 35
4a83b852 36/*
37 * Anonymizing patch by lutz@as-node.jena.thur.de
de3bdb4c 38 * have a look into http-anon.c to get more informations.
4a83b852 39 */
40
44a47c6e 41#include "squid.h"
e6ccf245 42#include "http.h"
f5691f9c 43#include "AuthUserRequest.h"
e6ccf245 44#include "Store.h"
528b2c61 45#include "HttpReply.h"
46#include "HttpRequest.h"
47#include "MemObject.h"
48#include "HttpHdrContRange.h"
4fb35c3c 49#include "ACLChecklist.h"
21b92762 50#include "fde.h"
b67e2c8c 51#if DELAY_POOLS
52#include "DelayPools.h"
53#endif
e6ccf245 54
55CBDATA_TYPE(HttpStateData);
56
090089c4 57
6bf8443a 58static const char *const crlf = "\r\n";
4db43fab 59
c3d63b2a 60static CWCB httpSendRequestEntity;
54220df8 61
c4b7a5a9 62static IOCB httpReadReply;
b6a2f15e 63static void httpSendRequest(HttpStateData *);
9e4ad609 64static PF httpStateFree;
65static PF httpTimeout;
f5b8bbc4 66static void httpCacheNegatively(StoreEntry *);
67static void httpMakePrivate(StoreEntry *);
68static void httpMakePublic(StoreEntry *);
f9cece6e 69static void httpMaybeRemovePublic(StoreEntry *, http_status);
190154cf 70static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, String strConnection, HttpRequest * request, HttpRequest * orig_request,
62e76326 71 HttpHeader * hdr_out, int we_do_ranges, http_state_flags);
190154cf 72static int decideIfWeDoRanges (HttpRequest * orig_request);
528b2c61 73
b8d8561b 74
b177367b 75static void
59715b38 76httpStateFree(int fd, void *data)
f5558c95 77{
e6ccf245 78 HttpStateData *httpState = static_cast<HttpStateData *>(data);
62e76326 79
0d4d4170 80 if (httpState == NULL)
62e76326 81 return;
82
21b92762 83 if (httpState->body_buf) {
9ea12fca 84 if (httpState->orig_request->body_connection.getRaw()) {
85 clientAbortBody(httpState->orig_request);
86 }
21b92762 87
88 if (httpState->body_buf) {
89 memFree(httpState->body_buf, MEM_8K_BUF);
90 httpState->body_buf = NULL;
91 }
92 }
93
f88211e8 94 storeUnlockObject(httpState->entry);
62e76326 95
4eb368f9 96 if (!memBufIsNull(&httpState->reply_hdr)) {
97 memBufClean(&httpState->reply_hdr);
0d4d4170 98 }
62e76326 99
30a4f2a8 100 requestUnlink(httpState->request);
20cc1450 101 requestUnlink(httpState->orig_request);
7dd44885 102 httpState->request = NULL;
103 httpState->orig_request = NULL;
104 cbdataFree(httpState);
f5558c95 105}
106
b8d8561b 107int
75e88d56 108httpCachable(method_t method)
090089c4 109{
090089c4 110 /* GET and HEAD are cachable. Others are not. */
62e76326 111
6eb42cae 112 if (method != METHOD_GET && method != METHOD_HEAD)
62e76326 113 return 0;
114
090089c4 115 /* else cachable */
116 return 1;
117}
118
b8d8561b 119static void
5c5783a2 120httpTimeout(int fd, void *data)
090089c4 121{
e6ccf245 122 HttpStateData *httpState = static_cast<HttpStateData *>(data);
593c9a75 123 StoreEntry *entry = httpState->entry;
9fb13bb6 124 debug(11, 4) ("httpTimeout: FD %d: '%s'\n", fd, storeUrl(entry));
62e76326 125
12158bdc 126 if (entry->store_status == STORE_PENDING) {
62e76326 127 if (entry->isEmpty()) {
128 fwdFail(httpState->fwd,
129 errorCon(ERR_READ_TIMEOUT, HTTP_GATEWAY_TIMEOUT));
130 }
9b312a19 131 }
62e76326 132
0d4d4170 133 comm_close(fd);
090089c4 134}
135
30a4f2a8 136/* This object can be cached for a long time */
b8d8561b 137static void
138httpMakePublic(StoreEntry * entry)
30a4f2a8 139{
d46a87a8 140 if (EBIT_TEST(entry->flags, ENTRY_CACHABLE))
62e76326 141 storeSetPublicKey(entry);
30a4f2a8 142}
143
144/* This object should never be cached at all */
b8d8561b 145static void
146httpMakePrivate(StoreEntry * entry)
30a4f2a8 147{
30a4f2a8 148 storeExpireNow(entry);
30a4f2a8 149 storeReleaseRequest(entry); /* delete object when not used */
f3e570e9 150 /* storeReleaseRequest clears ENTRY_CACHABLE flag */
30a4f2a8 151}
152
153/* This object may be negatively cached */
b8d8561b 154static void
155httpCacheNegatively(StoreEntry * entry)
30a4f2a8 156{
79b5cc5f 157 storeNegativeCache(entry);
62e76326 158
d46a87a8 159 if (EBIT_TEST(entry->flags, ENTRY_CACHABLE))
62e76326 160 storeSetPublicKey(entry);
30a4f2a8 161}
162
f9cece6e 163static void
164httpMaybeRemovePublic(StoreEntry * e, http_status status)
165{
62e76326 166
167 int remove
168 = 0;
169
7e3ce7b9 170 int forbidden = 0;
62e76326 171
f9cece6e 172 StoreEntry *pe;
62e76326 173
d46a87a8 174 if (!EBIT_TEST(e->flags, KEY_PRIVATE))
62e76326 175 return;
176
f9cece6e 177 switch (status) {
62e76326 178
f9cece6e 179 case HTTP_OK:
62e76326 180
f9cece6e 181 case HTTP_NON_AUTHORITATIVE_INFORMATION:
62e76326 182
f9cece6e 183 case HTTP_MULTIPLE_CHOICES:
62e76326 184
f9cece6e 185 case HTTP_MOVED_PERMANENTLY:
62e76326 186
f9cece6e 187 case HTTP_MOVED_TEMPORARILY:
62e76326 188
f9cece6e 189 case HTTP_GONE:
62e76326 190
7e3ce7b9 191 case HTTP_NOT_FOUND:
62e76326 192
193 remove
194 = 1;
195
196 break;
197
7e3ce7b9 198 case HTTP_FORBIDDEN:
62e76326 199
7e3ce7b9 200 case HTTP_METHOD_NOT_ALLOWED:
62e76326 201 forbidden = 1;
202
203 break;
204
f9cece6e 205#if WORK_IN_PROGRESS
62e76326 206
c8fd0193 207 case HTTP_UNAUTHORIZED:
62e76326 208 forbidden = 1;
209
210 break;
211
f9cece6e 212#endif
62e76326 213
f9cece6e 214 default:
7e3ce7b9 215#if QUESTIONABLE
62e76326 216 /*
217 * Any 2xx response should eject previously cached entities...
218 */
abb929f0 219
62e76326 220 if (status >= 200 && status < 300)
221 remove
222 = 1;
223
7e3ce7b9 224#endif
62e76326 225
226 break;
f9cece6e 227 }
62e76326 228
229 if (!remove
230 && !forbidden)
231 return;
232
f9cece6e 233 assert(e->mem_obj);
62e76326 234
f66a9ef4 235 if (e->mem_obj->request)
62e76326 236 pe = storeGetPublicByRequest(e->mem_obj->request);
f66a9ef4 237 else
62e76326 238 pe = storeGetPublic(e->mem_obj->url, e->mem_obj->method);
239
f66a9ef4 240 if (pe != NULL) {
62e76326 241 assert(e != pe);
242 storeRelease(pe);
0856d155 243 }
62e76326 244
7e3ce7b9 245 /*
246 * Also remove any cached HEAD response in case the object has
247 * changed.
248 */
f66a9ef4 249 if (e->mem_obj->request)
62e76326 250 pe = storeGetPublicByRequestMethod(e->mem_obj->request, METHOD_HEAD);
f66a9ef4 251 else
62e76326 252 pe = storeGetPublic(e->mem_obj->url, METHOD_HEAD);
253
f66a9ef4 254 if (pe != NULL) {
62e76326 255 assert(e != pe);
256 storeRelease(pe);
7e3ce7b9 257 }
62e76326 258
7e3ce7b9 259 if (forbidden)
62e76326 260 return;
261
7e3ce7b9 262 switch (e->mem_obj->method) {
62e76326 263
7e3ce7b9 264 case METHOD_PUT:
62e76326 265
7e3ce7b9 266 case METHOD_DELETE:
62e76326 267
7e3ce7b9 268 case METHOD_PROPPATCH:
62e76326 269
7e3ce7b9 270 case METHOD_MKCOL:
62e76326 271
7e3ce7b9 272 case METHOD_MOVE:
62e76326 273
42b51993 274 case METHOD_BMOVE:
62e76326 275
42b51993 276 case METHOD_BDELETE:
62e76326 277 /*
278 * Remove any cached GET object if it is beleived that the
279 * object may have changed as a result of other methods
280 */
281
282 if (e->mem_obj->request)
283 pe = storeGetPublicByRequestMethod(e->mem_obj->request, METHOD_GET);
284 else
285 pe = storeGetPublic(e->mem_obj->url, METHOD_GET);
286
287 if (pe != NULL) {
288 assert(e != pe);
289 storeRelease(pe);
290 }
291
292 break;
293
c8be6d7b 294 default:
62e76326 295 /* Keep GCC happy. The methods above are all mutating HTTP methods
296 */
297 break;
0856d155 298 }
f9cece6e 299}
300
43ae1d95 301void
302HttpStateData::processSurrogateControl(HttpReply *reply)
303{
304#if ESI
305
306 if (request->flags.accelerated && reply->surrogate_control) {
307 HttpHdrScTarget *sctusable =
308 httpHdrScGetMergedTarget(reply->surrogate_control,
309 Config.Accel.surrogate_id);
310
311 if (sctusable) {
312 if (EBIT_TEST(sctusable->mask, SC_NO_STORE) ||
313 (Config.onoff.surrogate_is_remote
314 && EBIT_TEST(sctusable->mask, SC_NO_STORE_REMOTE))) {
315 surrogateNoStore = true;
316 httpMakePrivate(entry);
317 }
318
319 /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
320 * accelerated request or not...
321 * Still, this is an abtraction breach. - RC
322 */
323 if (sctusable->max_age != -1) {
324 if (sctusable->max_age < sctusable->max_stale)
325 reply->expires = reply->date + sctusable->max_age;
326 else
327 reply->expires = reply->date + sctusable->max_stale;
328
329 /* And update the timestamps */
330 storeTimestampsSet(entry);
331 }
332
333 /* We ignore cache-control directives as per the Surrogate specification */
334 ignoreCacheControl = true;
335
336 httpHdrScTargetDestroy(sctusable);
337 }
338 }
339
340#endif
341}
342
924f73bc 343int
344HttpStateData::cacheableReply()
c54e9052 345{
924f73bc 346 HttpReply const *rep = entry->getReply();
528b2c61 347 HttpHeader const *hdr = &rep->header;
d8b249ef 348 const int cc_mask = (rep->cache_control) ? rep->cache_control->mask : 0;
c68e9c6b 349 const char *v;
38f9c547 350#if HTTP_VIOLATIONS
62e76326 351
38f9c547 352 const refresh_t *R = NULL;
353#endif
43ae1d95 354
38f9c547 355 if (surrogateNoStore)
62e76326 356 return 0;
357
924f73bc 358 if (!ignoreCacheControl) {
38f9c547 359 if (EBIT_TEST(cc_mask, CC_PRIVATE)) {
360#if HTTP_VIOLATIONS
43ae1d95 361
38f9c547 362 if (!R)
363 R = refreshLimits(entry->mem_obj->url);
43ae1d95 364
38f9c547 365 if (R && !R->flags.ignore_private)
366#endif
367
368 return 0;
369 }
370
371 if (EBIT_TEST(cc_mask, CC_NO_CACHE)) {
372#if HTTP_VIOLATIONS
373
374 if (!R)
375 R = refreshLimits(entry->mem_obj->url);
376
377 if (R && !R->flags.ignore_no_cache)
378#endif
379
380 return 0;
381 }
382
383 if (EBIT_TEST(cc_mask, CC_NO_STORE)) {
384#if HTTP_VIOLATIONS
385
386 if (!R)
387 R = refreshLimits(entry->mem_obj->url);
388
389 if (R && !R->flags.ignore_no_store)
390#endif
391
392 return 0;
393 }
43ae1d95 394 }
395
924f73bc 396 if (request->flags.auth) {
62e76326 397 /*
398 * Responses to requests with authorization may be cached
399 * only if a Cache-Control: public reply header is present.
400 * RFC 2068, sec 14.9.4
401 */
402
38f9c547 403 if (!EBIT_TEST(cc_mask, CC_PUBLIC)) {
404#if HTTP_VIOLATIONS
405
406 if (!R)
407 R = refreshLimits(entry->mem_obj->url);
408
409 if (R && !R->flags.ignore_auth)
410#endif
411
412 return 0;
413 }
a6dfe2d9 414 }
62e76326 415
c68e9c6b 416 /* Pragma: no-cache in _replies_ is not documented in HTTP,
417 * but servers like "Active Imaging Webcast/2.0" sure do use it */
418 if (httpHeaderHas(hdr, HDR_PRAGMA)) {
62e76326 419 String s = httpHeaderGetList(hdr, HDR_PRAGMA);
420 const int no_cache = strListIsMember(&s, "no-cache", ',');
421 s.clean();
422
38f9c547 423 if (no_cache) {
424#if HTTP_VIOLATIONS
425
426 if (!R)
427 R = refreshLimits(entry->mem_obj->url);
428
429 if (R && !R->flags.ignore_no_cache)
430#endif
431
432 return 0;
433 }
c68e9c6b 434 }
62e76326 435
c68e9c6b 436 /*
437 * The "multipart/x-mixed-replace" content type is used for
438 * continuous push replies. These are generally dynamic and
439 * probably should not be cachable
440 */
441 if ((v = httpHeaderGetStr(hdr, HDR_CONTENT_TYPE)))
62e76326 442 if (!strncasecmp(v, "multipart/x-mixed-replace", 25))
443 return 0;
444
924f73bc 445 switch (entry->getReply()->sline.status) {
62e76326 446 /* Responses that are cacheable */
447
19a04dac 448 case HTTP_OK:
62e76326 449
19a04dac 450 case HTTP_NON_AUTHORITATIVE_INFORMATION:
62e76326 451
19a04dac 452 case HTTP_MULTIPLE_CHOICES:
62e76326 453
19a04dac 454 case HTTP_MOVED_PERMANENTLY:
62e76326 455
19a04dac 456 case HTTP_GONE:
62e76326 457 /*
458 * Don't cache objects that need to be refreshed on next request,
459 * unless we know how to refresh it.
460 */
461
924f73bc 462 if (!refreshIsCachable(entry))
62e76326 463 return 0;
464
465 /* don't cache objects from peers w/o LMT, Date, or Expires */
466 /* check that is it enough to check headers @?@ */
467 if (rep->date > -1)
468 return 1;
469 else if (rep->last_modified > -1)
470 return 1;
924f73bc 471 else if (!_peer)
62e76326 472 return 1;
473
474 /* @?@ (here and 302): invalid expires header compiles to squid_curtime */
475 else if (rep->expires > -1)
476 return 1;
477 else
478 return 0;
479
480 /* NOTREACHED */
481 break;
482
483 /* Responses that only are cacheable if the server says so */
484
19a04dac 485 case HTTP_MOVED_TEMPORARILY:
62e76326 486 if (rep->expires > -1)
487 return 1;
488 else
489 return 0;
490
491 /* NOTREACHED */
492 break;
493
494 /* Errors can be negatively cached */
495
19a04dac 496 case HTTP_NO_CONTENT:
62e76326 497
19a04dac 498 case HTTP_USE_PROXY:
62e76326 499
19a04dac 500 case HTTP_BAD_REQUEST:
62e76326 501
19a04dac 502 case HTTP_FORBIDDEN:
62e76326 503
19a04dac 504 case HTTP_NOT_FOUND:
62e76326 505
19a04dac 506 case HTTP_METHOD_NOT_ALLOWED:
62e76326 507
19a04dac 508 case HTTP_REQUEST_URI_TOO_LARGE:
62e76326 509
19a04dac 510 case HTTP_INTERNAL_SERVER_ERROR:
62e76326 511
19a04dac 512 case HTTP_NOT_IMPLEMENTED:
62e76326 513
19a04dac 514 case HTTP_BAD_GATEWAY:
62e76326 515
19a04dac 516 case HTTP_SERVICE_UNAVAILABLE:
62e76326 517
19a04dac 518 case HTTP_GATEWAY_TIMEOUT:
62e76326 519 return -1;
520
521 /* NOTREACHED */
522 break;
523
524 /* Some responses can never be cached */
525
0cdcddb9 526 case HTTP_PARTIAL_CONTENT: /* Not yet supported */
62e76326 527
19a04dac 528 case HTTP_SEE_OTHER:
62e76326 529
19a04dac 530 case HTTP_NOT_MODIFIED:
62e76326 531
19a04dac 532 case HTTP_UNAUTHORIZED:
62e76326 533
19a04dac 534 case HTTP_PROXY_AUTHENTICATION_REQUIRED:
62e76326 535
0cdcddb9 536 case HTTP_INVALID_HEADER: /* Squid header parsing error */
4eb368f9 537
538 case HTTP_HEADER_TOO_LARGE:
62e76326 539 return 0;
540
c54e9052 541 default: /* Unknown status code */
924f73bc 542 debug (11,0)("HttpStateData::cacheableReply: unknown http status code in reply\n");
62e76326 543
544 return 0;
545
546 /* NOTREACHED */
547 break;
c54e9052 548 }
62e76326 549
79d39a72 550 /* NOTREACHED */
c54e9052 551}
090089c4 552
f66a9ef4 553/*
554 * For Vary, store the relevant request headers as
555 * virtual headers in the reply
556 * Returns false if the variance cannot be stored
557 */
558const char *
190154cf 559httpMakeVaryMark(HttpRequest * request, HttpReply const * reply)
f66a9ef4 560{
f66a9ef4 561 String vary, hdr;
562 const char *pos = NULL;
563 const char *item;
564 const char *value;
565 int ilen;
528b2c61 566 static String vstr;
f66a9ef4 567
528b2c61 568 vstr.clean();
f66a9ef4 569 vary = httpHeaderGetList(&reply->header, HDR_VARY);
62e76326 570
f66a9ef4 571 while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
62e76326 572 char *name = (char *)xmalloc(ilen + 1);
573 xstrncpy(name, item, ilen + 1);
574 Tolower(name);
9776e3cc 575
576 if (strcmp(name, "*") == 0) {
577 /* Can not handle "Vary: *" withtout ETag support */
578 safe_free(name);
579 vstr.clean();
580 break;
581 }
582
62e76326 583 strListAdd(&vstr, name, ',');
584 hdr = httpHeaderGetByName(&request->header, name);
585 safe_free(name);
586 value = hdr.buf();
587
588 if (value) {
589 value = rfc1738_escape_part(value);
590 vstr.append("=\"", 2);
591 vstr.append(value);
592 vstr.append("\"", 1);
593 }
594
595 hdr.clean();
f66a9ef4 596 }
62e76326 597
528b2c61 598 vary.clean();
f66a9ef4 599#if X_ACCELERATOR_VARY
62e76326 600
aa38be4a 601 pos = NULL;
f66a9ef4 602 vary = httpHeaderGetList(&reply->header, HDR_X_ACCELERATOR_VARY);
62e76326 603
f66a9ef4 604 while (strListGetItem(&vary, ',', &item, &ilen, &pos)) {
62e76326 605 char *name = (char *)xmalloc(ilen + 1);
606 xstrncpy(name, item, ilen + 1);
607 Tolower(name);
608 strListAdd(&vstr, name, ',');
609 hdr = httpHeaderGetByName(&request->header, name);
610 safe_free(name);
611 value = hdr.buf();
612
613 if (value) {
614 value = rfc1738_escape_part(value);
615 vstr.append("=\"", 2);
616 vstr.append(value);
617 vstr.append("\"", 1);
618 }
619
620 hdr.clean();
f66a9ef4 621 }
62e76326 622
528b2c61 623 vary.clean();
f66a9ef4 624#endif
62e76326 625
528b2c61 626 debug(11, 3) ("httpMakeVaryMark: %s\n", vstr.buf());
627 return vstr.buf();
f66a9ef4 628}
629
4eb368f9 630void
631HttpStateData::failReply(HttpReply *reply, http_status const & status)
632{
633 reply->sline.version = HttpVersion(1, 0);
634 reply->sline.status = status;
635 storeEntryReplaceObject (entry, reply);
636
637 if (eof == 1) {
638 fwdComplete(fwd);
639 comm_close(fd);
640 }
641}
642
643/* rewrite this later using new interfaces @?@
644 * This creates the error page itself.. its likely
645 * that the forward ported reply header max size patch
646 * generates non http conformant error pages - in which
647 * case the errors where should be 'BAD_GATEWAY' etc
648 */
b8d8561b 649void
e6ccf245 650HttpStateData::processReplyHeader(const char *buf, int size)
f5558c95 651{
9bc73deb 652 size_t hdr_len;
bdec8360 653 size_t hdr_size;
528b2c61 654 /* Creates a blank header. If this routine is made incremental, this will
655 * not do
656 */
657 HttpReply *reply = httpReplyCreate();
82384411 658 Ctx ctx = ctx_enter(entry->mem_obj->url);
b6cfb65c 659 debug(11, 3) ("httpProcessReplyHeader: key '%s'\n",
62e76326 660 entry->getMD5Text());
661
4eb368f9 662 if (memBufIsNull(&reply_hdr))
663 memBufDefInit(&reply_hdr);
62e76326 664
1a98175f 665 assert(!flags.headers_parsed);
62e76326 666
bdec8360 667 memBufAppend(&reply_hdr, buf, size);
62e76326 668
4eb368f9 669 hdr_len = reply_hdr.size;
62e76326 670
4eb368f9 671 if (hdr_len > 4 && strncmp(reply_hdr.buf, "HTTP/", 5)) {
c8f4eac4 672 debugs(11, 3, "httpProcessReplyHeader: Non-HTTP-compliant header: '" << reply_hdr.buf << "'");
1a98175f 673 flags.headers_parsed = 1;
4eb368f9 674 memBufClean(&reply_hdr);
675 failReply (reply, HTTP_INVALID_HEADER);
82384411 676 ctx_exit(ctx);
4eb368f9 677 return;
678 }
62e76326 679
bdec8360 680 hdr_size = headersEnd(reply_hdr.buf, hdr_len);
62e76326 681
4eb368f9 682 if (hdr_size)
683 hdr_len = hdr_size;
62e76326 684
4eb368f9 685 if (hdr_len > Config.maxReplyHeaderSize) {
c8f4eac4 686 debugs(11, 1, "httpProcessReplyHeader: Too large reply header");
62e76326 687
4eb368f9 688 if (!memBufIsNull(&reply_hdr))
689 memBufClean(&reply_hdr);
62e76326 690
4eb368f9 691 failReply (reply, HTTP_HEADER_TOO_LARGE);
62e76326 692
1a98175f 693 flags.headers_parsed = 1;
82384411 694
695 ctx_exit(ctx);
696
62e76326 697 return;
f5558c95 698 }
62e76326 699
9bc73deb 700 /* headers can be incomplete only if object still arriving */
4eb368f9 701 if (!hdr_size) {
702 if (eof)
703 hdr_size = hdr_len;
82384411 704 else {
705 ctx_exit(ctx);
62e76326 706 return; /* headers not complete */
82384411 707 }
9bc73deb 708 }
62e76326 709
4eb368f9 710 /* Cut away any excess body data (only needed for debug?) */
711 memBufAppend(&reply_hdr, "\0", 1);
712
713 reply_hdr.buf[hdr_size] = '\0';
714
1a98175f 715 flags.headers_parsed = 1;
4eb368f9 716
9bc73deb 717 debug(11, 9) ("GOT HTTP REPLY HDR:\n---------\n%s\n----------\n",
4eb368f9 718 reply_hdr.buf);
719
9bc73deb 720 /* Parse headers into reply structure */
721 /* what happens if we fail to parse here? */
4eb368f9 722 httpReplyParse(reply, reply_hdr.buf, hdr_size);
47ac2ebe 723
724 if (reply->sline.status >= HTTP_INVALID_HEADER) {
c8f4eac4 725 debugs(11, 3, "httpProcessReplyHeader: Non-HTTP-compliant header: '" << reply_hdr.buf << "'");
4eb368f9 726 failReply (reply, HTTP_INVALID_HEADER);
82384411 727 ctx_exit(ctx);
47ac2ebe 728 return;
729 }
730
43ae1d95 731 processSurrogateControl (reply);
528b2c61 732 /* TODO: we need our own reply * in the httpState, as we probably don't want to replace
733 * the storeEntry with interim headers
734 */
735
736 /* TODO: IF the reply is a 1.0 reply, AND it has a Connection: Header
737 * Parse the header and remove all referenced headers
738 */
739
740 storeEntryReplaceObject(entry, reply);
741 /* DO NOT USE reply now */
742 reply = NULL;
743
123516e5 744 if (entry->getReply()->sline.status == HTTP_PARTIAL_CONTENT &&
745 entry->getReply()->content_range)
62e76326 746 currentOffset = entry->getReply()->content_range->spec.offset;
747
9bc73deb 748 storeTimestampsSet(entry);
62e76326 749
9bc73deb 750 /* Check if object is cacheable or not based on reply code */
528b2c61 751 debug(11, 3) ("httpProcessReplyHeader: HTTP CODE: %d\n", entry->getReply()->sline.status);
62e76326 752
9bc73deb 753 if (neighbors_do_private_keys)
62e76326 754 httpMaybeRemovePublic(entry, entry->getReply()->sline.status);
e6ccf245 755
4b44c907 756 if (httpHeaderHas(&entry->getReply()->header, HDR_VARY)
f66a9ef4 757#if X_ACCELERATOR_VARY
4b44c907 758 || httpHeaderHas(&entry->getReply()->header, HDR_X_ACCELERATOR_VARY)
f66a9ef4 759#endif
4b44c907 760 ) {
761 const char *vary = httpMakeVaryMark(orig_request, entry->getReply());
762
763 if (!vary) {
764 httpMakePrivate(entry);
765 goto no_cache;
766
62e76326 767 }
768
4b44c907 769 entry->mem_obj->vary_headers = xstrdup(vary);
770 }
771
772 switch (cacheableReply()) {
773
774 case 1:
775 httpMakePublic(entry);
62e76326 776 break;
777
9bc73deb 778 case 0:
62e76326 779 httpMakePrivate(entry);
780 break;
781
9bc73deb 782 case -1:
4b44c907 783
784 if (Config.negativeTtl > 0)
785 httpCacheNegatively(entry);
786 else
787 httpMakePrivate(entry);
788
62e76326 789 break;
790
9bc73deb 791 default:
62e76326 792 assert(0);
4b44c907 793
62e76326 794 break;
9bc73deb 795 }
62e76326 796
4b44c907 797no_cache:
798
43ae1d95 799 if (!ignoreCacheControl && entry->getReply()->cache_control) {
62e76326 800 if (EBIT_TEST(entry->getReply()->cache_control->mask, CC_PROXY_REVALIDATE))
801 EBIT_SET(entry->flags, ENTRY_REVALIDATE);
802 else if (EBIT_TEST(entry->getReply()->cache_control->mask, CC_MUST_REVALIDATE))
803 EBIT_SET(entry->flags, ENTRY_REVALIDATE);
9bc73deb 804 }
62e76326 805
e6ccf245 806 if (flags.keepalive)
62e76326 807 if (_peer)
808 _peer->stats.n_keepalives_sent++;
809
21b92762 810 if (entry->getReply()->keep_alive) {
62e76326 811 if (_peer)
812 _peer->stats.n_keepalives_recv++;
813
21b92762 814 if (Config.onoff.detect_broken_server_pconns && httpReplyBodySize(request->method, reply) == -1) {
815 debug(11, 1) ("httpProcessReplyHeader: Impossible keep-alive header from '%s'\n", storeUrl(entry));
816 debug(11, 2) ("GOT HTTP REPLY HDR:\n---------\n%s\n----------\n", reply_hdr.buf);
817 flags.keepalive_broken = 1;
818 }
819 }
820
528b2c61 821 if (entry->getReply()->date > -1 && !_peer) {
411c6ea3 822 int skew = abs((int)(entry->getReply()->date - squid_curtime));
62e76326 823
824 if (skew > 86400)
825 debug(11, 3) ("%s's clock is skewed by %d seconds!\n",
826 request->host, skew);
f5558c95 827 }
62e76326 828
9bc73deb 829 ctx_exit(ctx);
c3609322 830#if HEADERS_LOG
62e76326 831
528b2c61 832 headersLog(1, 0, request->method, entry->getReply());
c3609322 833#endif
62e76326 834
e6ccf245 835 if (eof == 1) {
62e76326 836 fwdComplete(fwd);
837 comm_close(fd);
e6ccf245 838 }
f5558c95 839}
840
528b2c61 841HttpStateData::ConnectionStatus
842HttpStateData::statusIfComplete() const
603a02fd 843{
528b2c61 844 HttpReply const *reply = entry->getReply();
845 /* If the reply wants to close the connection, it takes precedence */
62e76326 846
528b2c61 847 if (httpHeaderHasConnDir(&reply->header, "close"))
62e76326 848 return COMPLETE_NONPERSISTENT_MSG;
849
528b2c61 850 /* If we didn't send a keep-alive request header, then this
978e455f 851 * can not be a persistent connection.
852 */
528b2c61 853 if (!flags.keepalive)
62e76326 854 return COMPLETE_NONPERSISTENT_MSG;
855
72b63f06 856 /*
857 * If we haven't sent the whole request then this can not be a persistent
858 * connection.
859 */
860 if (!flags.request_sent) {
2afdbf48 861 debug(11, 1) ("statusIfComplete: Request not yet fully sent \"%s %s\"\n",
72b63f06 862 RequestMethodStr[orig_request->method],
863 storeUrl(entry));
864 return COMPLETE_NONPERSISTENT_MSG;
865 }
866
9f5a2895 867 /*
868 * What does the reply have to say about keep-alive?
869 */
b6a2f15e 870 /*
871 * XXX BUG?
872 * If the origin server (HTTP/1.0) does not send a keep-alive
873 * header, but keeps the connection open anyway, what happens?
874 * We'll return here and http.c waits for an EOF before changing
875 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
876 * and an error status code, and we might have to wait until
877 * the server times out the socket.
878 */
9f5a2895 879 if (!reply->keep_alive)
528b2c61 880 return COMPLETE_NONPERSISTENT_MSG;
62e76326 881
528b2c61 882 return COMPLETE_PERSISTENT_MSG;
883}
884
885HttpStateData::ConnectionStatus
886HttpStateData::persistentConnStatus() const
887{
888 HttpReply const *reply = entry->getReply();
889 int clen;
890 debug(11, 3) ("httpPconnTransferDone: FD %d\n", fd);
891 ConnectionStatus result = statusIfComplete();
51fdcbd5 892 debug(11, 5) ("httpPconnTransferDone: content_length=%d\n",
62e76326 893 reply->content_length);
35282fbf 894 /* If we haven't seen the end of reply headers, we are not done */
62e76326 895
1a98175f 896 if (!flags.headers_parsed)
62e76326 897 return INCOMPLETE_MSG;
898
528b2c61 899 clen = httpReplyBodySize(request->method, reply);
62e76326 900
35282fbf 901 /* If there is no message body, we can be persistent */
902 if (0 == clen)
62e76326 903 return result;
904
35282fbf 905 /* If the body size is unknown we must wait for EOF */
906 if (clen < 0)
62e76326 907 return INCOMPLETE_MSG;
908
35282fbf 909 /* If the body size is known, we must wait until we've gotten all of it. */
528b2c61 910 if (entry->mem_obj->endOffset() < reply->content_length + reply->hdr_sz)
62e76326 911 return INCOMPLETE_MSG;
912
35282fbf 913 /* We got it all */
528b2c61 914 return result;
603a02fd 915}
090089c4 916
b8d8561b 917static void
c4b7a5a9 918httpReadReply(int fd, char *buf, size_t len, comm_err_t flag, int xerrno,void *data)
919{
920 HttpStateData *httpState = static_cast<HttpStateData *>(data);
7194987f 921 assert (fd == httpState->fd);
1d5161bd 922 PROF_start(HttpStateData_readReply);
c4b7a5a9 923 httpState->readReply (fd, buf, len, flag, xerrno, data);
1d5161bd 924 PROF_stop(HttpStateData_readReply);
c4b7a5a9 925}
926
2afdbf48 927/* This will be called when data is ready to be read from fd. Read until
928 * error or connection closed. */
929/* XXX this function is too long! */
c4b7a5a9 930void
528b2c61 931HttpStateData::readReply (int fd, char *readBuf, size_t len, comm_err_t flag, int xerrno,void *data)
090089c4 932{
30a4f2a8 933 int bin;
090089c4 934 int clen;
f61f0107 935 flags.do_next_read = 0;
c4b7a5a9 936
937
528b2c61 938 assert(buf == readBuf);
c4b7a5a9 939
940 /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us
62e76326 941 */
942
c4b7a5a9 943 if (flag == COMM_ERR_CLOSING) {
d09176e1 944 debug (11,3)("http socket closing\n");
c4b7a5a9 945 return;
946 }
947
e92e4e44 948 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
62e76326 949 maybeReadData();
950 return;
e92e4e44 951 }
c4b7a5a9 952
1513873c 953 errno = 0;
c4b7a5a9 954 /* prepare the read size for the next read (if any) */
447e176b 955#if DELAY_POOLS
62e76326 956
a46d2c0e 957 DelayId delayId = entry->mem_obj->mostBytesAllowed();
62e76326 958
447e176b 959#endif
62e76326 960
c4b7a5a9 961 debug(11, 5) ("httpReadReply: FD %d: len %d.\n", fd, (int)len);
62e76326 962
c4b7a5a9 963 if (flag == COMM_OK && len > 0) {
447e176b 964#if DELAY_POOLS
62e76326 965 delayId.bytesIn(len);
447e176b 966#endif
62e76326 967
968 kb_incr(&statCounter.server.all.kbytes_in, len);
969 kb_incr(&statCounter.server.http.kbytes_in, len);
62e76326 970 IOStats.Http.reads++;
971
972 for (clen = len - 1, bin = 0; clen; bin++)
973 clen >>= 1;
974
975 IOStats.Http.read_hist[bin]++;
30a4f2a8 976 }
62e76326 977
5fa061b8 978 /* here the RFC says we should ignore whitespace between replies, but we can't as
979 * doing so breaks HTTP/0.9 replies beginning with witespace, and in addition
980 * the response splitting countermeasures is extremely likely to trigger on this,
981 * not allowing connection reuse in the first place.
982 */
983#if DONT_DO_THIS
1a98175f 984 if (!flags.headers_parsed && flag == COMM_OK && len > 0 && fd_table[fd].uses > 1) {
5fa061b8 985 /* Skip whitespace between replies */
62e76326 986
5fa061b8 987 while (len > 0 && isspace(*buf))
62e76326 988 xmemmove(buf, buf + 1, len--);
989
990 if (len == 0) {
991 /* Continue to read... */
21b92762 992 /* Timeout NOT increased. This whitespace was from previous reply */
f61f0107 993 flags.do_next_read = 1;
62e76326 994 maybeReadData();
995 return;
996 }
5ede6c8f 997 }
62e76326 998
5fa061b8 999#endif
1000
c4b7a5a9 1001 if (flag != COMM_OK || len < 0) {
62e76326 1002 debug(50, 2) ("httpReadReply: FD %d: read failure: %s.\n",
1003 fd, xstrerror());
1004
1005 if (ignoreErrno(errno)) {
f61f0107 1006 flags.do_next_read = 1;
62e76326 1007 } else if (entry->isEmpty()) {
1008 ErrorState *err;
4eb368f9 1009 err = errorCon(ERR_READ_ERROR, HTTP_BAD_GATEWAY);
190154cf 1010 err->request = requestLink((HttpRequest *) request);
62e76326 1011 err->xerrno = errno;
1012 fwdFail(fwd, err);
f61f0107 1013 flags.do_next_read = 0;
62e76326 1014 comm_close(fd);
1015 } else {
f61f0107 1016 flags.do_next_read = 0;
62e76326 1017 comm_close(fd);
1018 }
528b2c61 1019 } else if (flag == COMM_OK && len == 0 && entry->isEmpty()) {
62e76326 1020 ErrorState *err;
4eb368f9 1021 err = errorCon(ERR_ZERO_SIZE_OBJECT, HTTP_BAD_GATEWAY);
62e76326 1022 err->xerrno = errno;
190154cf 1023 err->request = requestLink((HttpRequest *) request);
62e76326 1024 fwdFail(fwd, err);
1025 eof = 1;
f61f0107 1026 flags.do_next_read = 0;
62e76326 1027 comm_close(fd);
c4b7a5a9 1028 } else if (flag == COMM_OK && len == 0) {
62e76326 1029 /* Connection closed; retrieval done. */
1030 eof = 1;
1031
1a98175f 1032 if (!flags.headers_parsed)
62e76326 1033 /*
1a98175f 1034 * When we called httpProcessReplyHeader() before, we
1035 * didn't find the end of headers, but now we are
1036 * definately at EOF, so we want to process the reply
1037 * headers.
62e76326 1038 */
62e76326 1039 processReplyHeader(buf, len);
b735b90c 1040 else if (entry->getReply()->sline.status == HTTP_INVALID_HEADER && HttpVersion(0,9) != entry->getReply()->sline.version) {
47ac2ebe 1041 ErrorState *err;
52d3f198 1042 err = errorCon(ERR_INVALID_RESP, HTTP_BAD_GATEWAY);
47ac2ebe 1043 err->request = requestLink((HttpRequest *) request);
1044 fwdFail(fwd, err);
f61f0107 1045 flags.do_next_read = 0;
47ac2ebe 1046 } else {
4eb368f9 1047 if (entry->mem_obj->getReply()->sline.status == HTTP_HEADER_TOO_LARGE) {
1048 ErrorState *err;
1049 storeEntryReset(entry);
1050 err = errorCon(ERR_TOO_BIG, HTTP_BAD_GATEWAY);
1051 err->request = requestLink((HttpRequest *) request);
1052 fwdFail(fwd, err);
1053 fwd->flags.dont_retry = 1;
1054 } else {
1055 fwdComplete(fwd);
1056 }
1057
f61f0107 1058 flags.do_next_read = 0;
62e76326 1059 comm_close(fd);
1060 }
090089c4 1061 } else {
1a98175f 1062 if (!flags.headers_parsed) {
62e76326 1063 processReplyHeader(buf, len);
1064
1a98175f 1065 if (flags.headers_parsed) {
62e76326 1066 http_status s = entry->getReply()->sline.status;
47ac2ebe 1067 HttpVersion httpver = entry->getReply()->sline.version;
1068
b735b90c 1069 if (s == HTTP_INVALID_HEADER && httpver != HttpVersion(0,9)) {
47ac2ebe 1070 ErrorState *err;
1071 storeEntryReset(entry);
52d3f198 1072 err = errorCon(ERR_INVALID_RESP, HTTP_BAD_GATEWAY);
47ac2ebe 1073 err->request = requestLink((HttpRequest *) request);
1074 fwdFail(fwd, err);
1075 comm_close(fd);
1076 return;
1077 }
1078
225644d7 1079#if WIP_FWD_LOG
62e76326 1080
1081 fwdStatus(fwd, s);
47ac2ebe 1082
225644d7 1083#endif
62e76326 1084 /*
1085 * If its not a reply that we will re-forward, then
1086 * allow the client to get it.
1087 */
1088
1089 if (!fwdReforwardableStatus(s))
1090 EBIT_CLR(entry->flags, ENTRY_FWD_HDR_WAIT);
1091 }
1092 }
1093
1d5161bd 1094 PROF_start(HttpStateData_processReplyData);
62e76326 1095 processReplyData(buf, len);
1d5161bd 1096 PROF_stop(HttpStateData_processReplyData);
e6ccf245 1097 }
1098}
1099
1100void
528b2c61 1101HttpStateData::processReplyData(const char *buf, size_t len)
e6ccf245 1102{
1a98175f 1103 if (!flags.headers_parsed) {
f61f0107 1104 flags.do_next_read = 1;
62e76326 1105 maybeReadData();
1106 return;
528b2c61 1107 }
62e76326 1108
528b2c61 1109 StoreIOBuffer tempBuffer;
62e76326 1110
1111 if (!flags.headers_pushed) {
1112 /* The first block needs us to skip the headers */
1113 /* TODO: make this cleaner. WE should push the headers, NOT the parser */
1114 size_t end = headersEnd (buf, len);
1115 /* IF len > end, we need to append data after the
1116 * out of band update to the store
1117 */
1118
1119 if (len > end) {
1120 tempBuffer.data = (char *)buf+end;
1121 tempBuffer.length = len - end;
1122 tempBuffer.offset = currentOffset;
1123 currentOffset += tempBuffer.length;
1124 entry->write (tempBuffer);
1125 }
1126
1127 flags.headers_pushed = 1;
1128 } else {
1129 tempBuffer.data = (char *)buf;
1130 tempBuffer.length = len;
1131 tempBuffer.offset = currentOffset;
1132 currentOffset += len;
1133 entry->write(tempBuffer);
1134 }
528b2c61 1135
e6ccf245 1136 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
62e76326 1137 /*
1a98175f 1138 * the above entry->write() call could ABORT this entry,
62e76326 1139 * in that case, the server FD should already be closed.
1140 * there's nothing for us to do.
1141 */
1142 (void) 0;
1143 } else
1144 switch (persistentConnStatus()) {
1145
1146 case INCOMPLETE_MSG:
21b92762 1147 /* Wait for more data or EOF condition */
1148
1149 if (flags.keepalive_broken) {
1150 commSetTimeout(fd, 10, NULL, NULL);
1151 } else {
1152 commSetTimeout(fd, Config.Timeout.read, NULL, NULL);
1153 }
1154
f61f0107 1155 flags.do_next_read = 1;
62e76326 1156 break;
1157
1158 case COMPLETE_PERSISTENT_MSG:
1159 /* yes we have to clear all these! */
62e76326 1160 commSetTimeout(fd, -1, NULL, NULL);
f61f0107 1161 flags.do_next_read = 0;
62e76326 1162
1163 comm_remove_close_handler(fd, httpStateFree, this);
1164 fwdUnregister(fd, fwd);
bd0723ad 1165
1166 if (_peer) {
1167 if (_peer->options.originserver)
1168 pconnPush(fd, _peer->name, orig_request->port, orig_request->host);
1169 else
1170 pconnPush(fd, _peer->name, _peer->http_port, NULL);
1171 } else {
1172 pconnPush(fd, request->host, request->port, NULL);
1173 }
1174
62e76326 1175 fwdComplete(fwd);
1176 fd = -1;
1177 httpStateFree(fd, this);
1178 return;
1179
1180 case COMPLETE_NONPERSISTENT_MSG:
1181 /* close the connection ourselves */
1182 /* yes - same as for a complete persistent conn here */
62e76326 1183 commSetTimeout(fd, -1, NULL, NULL);
1184 commSetSelect(fd, COMM_SELECT_READ, NULL, NULL, 0);
62e76326 1185 comm_remove_close_handler(fd, httpStateFree, this);
1186 fwdUnregister(fd, fwd);
1187 fwdComplete(fwd);
1188 /* TODO: check that fd is still open here */
1189 comm_close (fd);
1190 fd = -1;
1191 httpStateFree(fd, this);
1192 return;
1193 }
1194
c4b7a5a9 1195 maybeReadData();
1196}
1197
1198void
1199HttpStateData::maybeReadData()
1200{
f61f0107 1201 if (flags.do_next_read) {
1202 flags.do_next_read = 0;
a46d2c0e 1203 entry->delayAwareRead(fd, buf, SQUID_TCP_SO_RCVBUF, httpReadReply, this);
528b2c61 1204 }
090089c4 1205}
1206
1207/* This will be called when request write is complete. Schedule read of
1208 * reply. */
d576a6a6 1209void
1210HttpStateData::SendComplete(int fd, char *bufnotused, size_t size, comm_err_t errflag, void *data)
090089c4 1211{
e6ccf245 1212 HttpStateData *httpState = static_cast<HttpStateData *>(data);
9b312a19 1213 StoreEntry *entry = httpState->entry;
1214 ErrorState *err;
a3d5953d 1215 debug(11, 5) ("httpSendComplete: FD %d: size %d: errflag %d.\n",
62e76326 1216 fd, (int) size, errflag);
bc87dc25 1217#if URL_CHECKSUM_DEBUG
62e76326 1218
528b2c61 1219 entry->mem_obj->checkUrlChecksum();
bc87dc25 1220#endif
62e76326 1221
ee1679df 1222 if (size > 0) {
62e76326 1223 fd_bytes(fd, size, FD_WRITE);
1224 kb_incr(&statCounter.server.all.kbytes_out, size);
1225 kb_incr(&statCounter.server.http.kbytes_out, size);
ee1679df 1226 }
62e76326 1227
ea3a2a69 1228 if (errflag == COMM_ERR_CLOSING)
62e76326 1229 return;
1230
090089c4 1231 if (errflag) {
4eb368f9 1232 err = errorCon(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY);
62e76326 1233 err->xerrno = errno;
1234 err->request = requestLink(httpState->orig_request);
1235 errorAppendEntry(entry, err);
1236 comm_close(fd);
1237 return;
090089c4 1238 } else {
62e76326 1239 /*
1240 * Set the read timeout here because it hasn't been set yet.
1241 * We only set the read timeout after the request has been
1242 * fully written to the server-side. If we start the timeout
1243 * after connection establishment, then we are likely to hit
1244 * the timeout for POST/PUT requests that have very large
1245 * request bodies.
1246 */
1247 commSetTimeout(fd, Config.Timeout.read, httpTimeout, httpState);
090089c4 1248 }
72b63f06 1249
1250 httpState->flags.request_sent = 1;
090089c4 1251}
1252
99edd1c3 1253/*
1254 * build request headers and append them to a given MemBuf
1255 * used by httpBuildRequestPrefix()
818c6c9e 1256 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
99edd1c3 1257 */
e1e72f06 1258void
190154cf 1259httpBuildRequestHeader(HttpRequest * request,
1260 HttpRequest * orig_request,
62e76326 1261 StoreEntry * entry,
1262 HttpHeader * hdr_out,
1263 http_state_flags flags)
6bf8443a 1264{
99edd1c3 1265 /* building buffer for complex strings */
5999b776 1266#define BBUF_SZ (MAX_URL+32)
99edd1c3 1267 LOCAL_ARRAY(char, bbuf, BBUF_SZ);
99edd1c3 1268 const HttpHeader *hdr_in = &orig_request->header;
1269 const HttpHeaderEntry *e;
6bccf575 1270 String strFwd;
99edd1c3 1271 HttpHeaderPos pos = HttpHeaderInitPos;
75faaa7a 1272 assert (hdr_out->owner == hoRequest);
99edd1c3 1273 /* append our IMS header */
62e76326 1274
9bc73deb 1275 if (request->lastmod > -1 && request->method == METHOD_GET)
62e76326 1276 httpHeaderPutTime(hdr_out, HDR_IF_MODIFIED_SINCE, request->lastmod);
99edd1c3 1277
528b2c61 1278 bool we_do_ranges = decideIfWeDoRanges (orig_request);
1279
650c4b88 1280 String strConnection (httpHeaderGetList(hdr_in, HDR_CONNECTION));
62e76326 1281
528b2c61 1282 while ((e = httpHeaderGetEntry(hdr_in, &pos)))
62e76326 1283 copyOneHeaderFromClientsideRequestToUpstreamRequest(e, strConnection, request, orig_request, hdr_out, we_do_ranges, flags);
528b2c61 1284
43ae1d95 1285 /* Abstraction break: We should interpret multipart/byterange responses
528b2c61 1286 * into offset-length data, and this works around our inability to do so.
1287 */
62e76326 1288 if (!we_do_ranges && orig_request->multipartRangeRequest()) {
1289 /* don't cache the result */
1290 orig_request->flags.cachable = 0;
1291 /* pretend it's not a range request */
00d77d6b 1292 delete orig_request->range;
62e76326 1293 orig_request->range = NULL;
1294 orig_request->flags.range = 0;
1295 }
528b2c61 1296
99edd1c3 1297
99edd1c3 1298 /* append Via */
736cb6aa 1299 if (Config.onoff.via) {
43ae1d95 1300 String strVia;
1301 strVia = httpHeaderGetList(hdr_in, HDR_VIA);
62e76326 1302 snprintf(bbuf, BBUF_SZ, "%d.%d %s",
1303 orig_request->http_ver.major,
1304 orig_request->http_ver.minor, ThisCache);
1305 strListAdd(&strVia, bbuf, ',');
1306 httpHeaderPutStr(hdr_out, HDR_VIA, strVia.buf());
1307 strVia.clean();
736cb6aa 1308 }
62e76326 1309
43ae1d95 1310#if ESI
1311 {
1312 /* Append Surrogate-Capabilities */
1313 String strSurrogate (httpHeaderGetList(hdr_in, HDR_SURROGATE_CAPABILITY));
ec43ae0e 1314 snprintf(bbuf, BBUF_SZ, "%s=\"Surrogate/1.0 ESI/1.0\"",
43ae1d95 1315 Config.Accel.surrogate_id);
1316 strListAdd(&strSurrogate, bbuf, ',');
1317 httpHeaderPutStr(hdr_out, HDR_SURROGATE_CAPABILITY, strSurrogate.buf());
1318 }
1319#endif
1320
99edd1c3 1321 /* append X-Forwarded-For */
6bccf575 1322 strFwd = httpHeaderGetList(hdr_in, HDR_X_FORWARDED_FOR);
62e76326 1323
6056ae68 1324 if (opt_forwarded_for && orig_request->client_addr.s_addr != no_addr.s_addr)
62e76326 1325 strListAdd(&strFwd, inet_ntoa(orig_request->client_addr), ',');
6056ae68 1326 else
62e76326 1327 strListAdd(&strFwd, "unknown", ',');
1328
528b2c61 1329 httpHeaderPutStr(hdr_out, HDR_X_FORWARDED_FOR, strFwd.buf());
62e76326 1330
528b2c61 1331 strFwd.clean();
6bccf575 1332
99edd1c3 1333 /* append Host if not there already */
1334 if (!httpHeaderHas(hdr_out, HDR_HOST)) {
62e76326 1335 if (orig_request->peer_domain) {
1336 httpHeaderPutStr(hdr_out, HDR_HOST, orig_request->peer_domain);
1337 } else if (orig_request->port == urlDefaultPort(orig_request->protocol)) {
1338 /* use port# only if not default */
1339 httpHeaderPutStr(hdr_out, HDR_HOST, orig_request->host);
1340 } else {
1341 httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d",
1342 orig_request->host, (int) orig_request->port);
1343 }
6bf8443a 1344 }
62e76326 1345
c68e9c6b 1346 /* append Authorization if known in URL, not in header and going direct */
1347 if (!httpHeaderHas(hdr_out, HDR_AUTHORIZATION)) {
62e76326 1348 if (!request->flags.proxying && *request->login) {
1349 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s",
1350 base64_encode(request->login));
1351 }
c68e9c6b 1352 }
62e76326 1353
c68e9c6b 1354 /* append Proxy-Authorization if configured for peer, and proxying */
c3b33cb7 1355 if (request->flags.proxying && orig_request->peer_login &&
62e76326 1356 !httpHeaderHas(hdr_out, HDR_PROXY_AUTHORIZATION)) {
1357 if (*orig_request->peer_login == '*') {
1358 /* Special mode, to pass the username to the upstream cache */
1359 char loginbuf[256];
1360 const char *username = "-";
1361
1362 if (orig_request->auth_user_request)
1363 username = orig_request->auth_user_request->username();
abb929f0 1364 else if (orig_request->extacl_user.size())
1365 username = orig_request->extacl_user.buf();
62e76326 1366
1367 snprintf(loginbuf, sizeof(loginbuf), "%s%s", username, orig_request->peer_login + 1);
1368
1369 httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s",
1370 base64_encode(loginbuf));
1371 } else if (strcmp(orig_request->peer_login, "PASS") == 0) {
abb929f0 1372 if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size()) {
1373 char loginbuf[256];
1374 snprintf(loginbuf, sizeof(loginbuf), "%s:%s", orig_request->extacl_user.buf(), orig_request->extacl_passwd.buf());
1375 httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s",
1376 base64_encode(loginbuf));
1377 }
62e76326 1378 } else if (strcmp(orig_request->peer_login, "PROXYPASS") == 0) {
1379 /* Nothing to do */
1380 } else {
1381 httpHeaderPutStrf(hdr_out, HDR_PROXY_AUTHORIZATION, "Basic %s",
1382 base64_encode(orig_request->peer_login));
1383 }
c68e9c6b 1384 }
62e76326 1385
be753325 1386 /* append WWW-Authorization if configured for peer */
1387 if (flags.originpeer && orig_request->peer_login &&
62e76326 1388 !httpHeaderHas(hdr_out, HDR_AUTHORIZATION)) {
1389 if (strcmp(orig_request->peer_login, "PASS") == 0) {
1390 /* No credentials to forward.. (should have been done above if available) */
1391 } else if (strcmp(orig_request->peer_login, "PROXYPASS") == 0) {
1392 /* Special mode, convert proxy authentication to WWW authentication
abb929f0 1393 * (also applies to authentication provided by external acl)
62e76326 1394 */
1395 const char *auth = httpHeaderGetStr(hdr_in, HDR_PROXY_AUTHORIZATION);
1396
1397 if (auth && strncasecmp(auth, "basic ", 6) == 0) {
1398 httpHeaderPutStr(hdr_out, HDR_AUTHORIZATION, auth);
abb929f0 1399 } else if (orig_request->extacl_user.size() && orig_request->extacl_passwd.size()) {
1400 char loginbuf[256];
1401 snprintf(loginbuf, sizeof(loginbuf), "%s:%s", orig_request->extacl_user.buf(), orig_request->extacl_passwd.buf());
1402 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s",
1403 base64_encode(loginbuf));
62e76326 1404 }
1405 } else if (*orig_request->peer_login == '*') {
1406 /* Special mode, to pass the username to the upstream cache */
1407 char loginbuf[256];
1408 const char *username = "-";
1409
1410 if (orig_request->auth_user_request)
f5691f9c 1411 username = orig_request->auth_user_request->username();
abb929f0 1412 else if (orig_request->extacl_user.size())
1413 username = orig_request->extacl_user.buf();
62e76326 1414
1415 snprintf(loginbuf, sizeof(loginbuf), "%s%s", username, orig_request->peer_login + 1);
1416
1417 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s",
1418 base64_encode(loginbuf));
1419 } else {
1420 /* Fixed login string */
1421 httpHeaderPutStrf(hdr_out, HDR_AUTHORIZATION, "Basic %s",
1422 base64_encode(orig_request->peer_login));
1423 }
be753325 1424 }
62e76326 1425
abb929f0 1426 /* append Cache-Control, add max-age if not there already */ {
62e76326 1427 HttpHdrCc *cc = httpHeaderGetCc(hdr_in);
1428
1429 if (!cc)
1430 cc = httpHdrCcCreate();
1431
1432 if (!EBIT_TEST(cc->mask, CC_MAX_AGE)) {
43ae1d95 1433 const char *url =
1434 entry ? storeUrl(entry) : urlCanonical(orig_request);
62e76326 1435 httpHdrCcSetMaxAge(cc, getMaxAge(url));
1436
1437 if (request->urlpath.size())
1438 assert(strstr(url, request->urlpath.buf()));
1439 }
1440
ce2d6441 1441 /* Set no-cache if determined needed but not found */
1442 if (orig_request->flags.nocache && !httpHeaderHas(hdr_in, HDR_PRAGMA))
1443 EBIT_SET(cc->mask, CC_NO_CACHE);
1444
1445 /* Enforce sibling relations */
62e76326 1446 if (flags.only_if_cached)
1447 EBIT_SET(cc->mask, CC_ONLY_IF_CACHED);
1448
1449 httpHeaderPutCc(hdr_out, cc);
1450
1451 httpHdrCcDestroy(cc);
6bf8443a 1452 }
62e76326 1453
99edd1c3 1454 /* maybe append Connection: keep-alive */
b515fc11 1455 if (flags.keepalive) {
62e76326 1456 if (flags.proxying) {
1457 httpHeaderPutStr(hdr_out, HDR_PROXY_CONNECTION, "keep-alive");
1458 } else {
1459 httpHeaderPutStr(hdr_out, HDR_CONNECTION, "keep-alive");
1460 }
603a02fd 1461 }
62e76326 1462
a7ad6e4e 1463 /* append Front-End-Https */
1464 if (flags.front_end_https) {
62e76326 1465 if (flags.front_end_https == 1 || request->protocol == PROTO_HTTPS)
1466 httpHeaderPutStr(hdr_out, HDR_FRONT_END_HTTPS, "On");
a7ad6e4e 1467 }
1468
6bccf575 1469 /* Now mangle the headers. */
8c01ada0 1470 httpHdrMangleList(hdr_out, request, ROR_REQUEST);
62e76326 1471
528b2c61 1472 strConnection.clean();
99edd1c3 1473}
1474
528b2c61 1475
1476void
190154cf 1477copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry *e, String strConnection, HttpRequest * request, HttpRequest * orig_request, HttpHeader * hdr_out, int we_do_ranges, http_state_flags flags)
528b2c61 1478{
1479 debug(11, 5) ("httpBuildRequestHeader: %s: %s\n",
62e76326 1480 e->name.buf(), e->value.buf());
1481
528b2c61 1482 if (!httpRequestHdrAllowed(e, &strConnection)) {
62e76326 1483 debug(11, 2) ("'%s' header denied by anonymize_headers configuration\n",+ e->name.buf());
1484 return;
528b2c61 1485 }
62e76326 1486
528b2c61 1487 switch (e->id) {
62e76326 1488
be753325 1489 case HDR_PROXY_AUTHORIZATION:
62e76326 1490 /* Only pass on proxy authentication to peers for which
1491 * authentication forwarding is explicitly enabled
1492 */
1493
1494 if (flags.proxying && orig_request->peer_login &&
abb929f0 1495 (strcmp(orig_request->peer_login, "PASS") == 0 ||
1496 strcmp(orig_request->peer_login, "PROXYPASS") == 0)) {
62e76326 1497 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1498 }
1499
1500 break;
1501
be753325 1502 case HDR_AUTHORIZATION:
62e76326 1503 /* Pass on WWW authentication */
1504
1505 if (!flags.originpeer) {
1506 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1507 } else {
1508 /* In accelerators, only forward authentication if enabled
1509 * (see also below for proxy->server authentication)
1510 */
1511
abb929f0 1512 if (orig_request->peer_login &&
1513 (strcmp(orig_request->peer_login, "PASS") == 0 ||
1514 strcmp(orig_request->peer_login, "PROXYPASS") == 0)) {
62e76326 1515 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1516 }
1517 }
1518
1519 break;
1520
be753325 1521 case HDR_HOST:
62e76326 1522 /*
b883b594 1523 * Normally Squid rewrites the Host: header.
1524 * However, there is one case when we don't: If the URL
62e76326 1525 * went through our redirector and the admin configured
1526 * 'redir_rewrites_host' to be off.
1527 */
1528
b883b594 1529 if (request->flags.redirected && !Config.onoff.redir_rewrites_host)
1530 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1531 else {
1532 /* use port# only if not default */
1533
1534 if (orig_request->port == urlDefaultPort(orig_request->protocol)) {
1535 httpHeaderPutStr(hdr_out, HDR_HOST, orig_request->host);
1536 } else {
1537 httpHeaderPutStrf(hdr_out, HDR_HOST, "%s:%d",
1538 orig_request->host, (int) orig_request->port);
1539 }
1540 }
62e76326 1541
1542 break;
1543
be753325 1544 case HDR_IF_MODIFIED_SINCE:
62e76326 1545 /* append unless we added our own;
1546 * note: at most one client's ims header can pass through */
b883b594 1547
62e76326 1548 if (!httpHeaderHas(hdr_out, HDR_IF_MODIFIED_SINCE))
1549 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1550
1551 break;
1552
be753325 1553 case HDR_MAX_FORWARDS:
62e76326 1554 if (orig_request->method == METHOD_TRACE) {
1555 const int hops = httpHeaderEntryGetInt(e);
1556
1557 if (hops > 0)
1558 httpHeaderPutInt(hdr_out, HDR_MAX_FORWARDS, hops - 1);
1559 }
1560
1561 break;
1562
be753325 1563 case HDR_VIA:
62e76326 1564 /* If Via is disabled then forward any received header as-is */
1565
1566 if (!Config.onoff.via)
1567 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1568
1569 break;
1570
be753325 1571 case HDR_RANGE:
62e76326 1572
be753325 1573 case HDR_IF_RANGE:
62e76326 1574
be753325 1575 case HDR_REQUEST_RANGE:
62e76326 1576 if (!we_do_ranges)
1577 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1578
1579 break;
1580
be753325 1581 case HDR_PROXY_CONNECTION:
62e76326 1582
be753325 1583 case HDR_CONNECTION:
62e76326 1584
be753325 1585 case HDR_X_FORWARDED_FOR:
62e76326 1586
be753325 1587 case HDR_CACHE_CONTROL:
62e76326 1588 /* append these after the loop if needed */
1589 break;
1590
be753325 1591 case HDR_FRONT_END_HTTPS:
62e76326 1592 if (!flags.front_end_https)
1593 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
1594
1595 break;
1596
be753325 1597 default:
62e76326 1598 /* pass on all other header fields */
1599 httpHeaderAddEntry(hdr_out, httpHeaderEntryClone(e));
528b2c61 1600 }
1601}
1602
1603int
190154cf 1604decideIfWeDoRanges (HttpRequest * orig_request)
528b2c61 1605{
62e76326 1606 int result = 1;
1607 /* decide if we want to do Ranges ourselves
1608 * and fetch the whole object now)
1609 * We want to handle Ranges ourselves iff
1610 * - we can actually parse client Range specs
1611 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
1612 * - reply will be cachable
1613 * (If the reply will be uncachable we have to throw it away after
1614 * serving this request, so it is better to forward ranges to
1615 * the server and fetch only the requested content)
1616 */
1617
1618 if (NULL == orig_request->range || !orig_request->flags.cachable
1619 || orig_request->range->offsetLimitExceeded())
1620 result = 0;
1621
1622 debug(11, 8) ("decideIfWeDoRanges: range specs: %p, cachable: %d; we_do_ranges: %d\n",
1623 orig_request->range, orig_request->flags.cachable, result);
1624
1625 return result;
528b2c61 1626}
1627
1628
62e76326 1629/* build request prefix and append it to a given MemBuf;
99edd1c3 1630 * return the length of the prefix */
9bc73deb 1631mb_size_t
190154cf 1632httpBuildRequestPrefix(HttpRequest * request,
1633 HttpRequest * orig_request,
62e76326 1634 StoreEntry * entry,
1635 MemBuf * mb,
1636 http_state_flags flags)
99edd1c3 1637{
1638 const int offset = mb->size;
450e0c10 1639 HttpVersion httpver(1, 0);
528b2c61 1640 memBufPrintf(mb, "%s %s HTTP/%d.%d\r\n",
62e76326 1641 RequestMethodStr[request->method],
1642 request->urlpath.size() ? request->urlpath.buf() : "/",
1643 httpver.major,httpver.minor);
99edd1c3 1644 /* build and pack headers */
1645 {
75faaa7a 1646 HttpHeader hdr(hoRequest);
62e76326 1647 Packer p;
1648 httpBuildRequestHeader(request, orig_request, entry, &hdr, flags);
1649 packerToMemInit(&p, mb);
1650 httpHeaderPackInto(&hdr, &p);
1651 httpHeaderClean(&hdr);
1652 packerClean(&p);
9d9d144b 1653 }
99edd1c3 1654 /* append header terminator */
b8890359 1655 memBufAppend(mb, crlf, 2);
99edd1c3 1656 return mb->size - offset;
6bf8443a 1657}
62e76326 1658
090089c4 1659/* This will be called when connect completes. Write request. */
b8d8561b 1660static void
b6a2f15e 1661httpSendRequest(HttpStateData * httpState)
090089c4 1662{
99edd1c3 1663 MemBuf mb;
190154cf 1664 HttpRequest *req = httpState->request;
620da955 1665 StoreEntry *entry = httpState->entry;
29b8d8d6 1666 peer *p = httpState->_peer;
901e234d 1667 CWCB *sendHeaderDone;
21b92762 1668 int fd = httpState->fd;
090089c4 1669
21b92762 1670 debug(11, 5) ("httpSendRequest: FD %d: httpState %p.\n", fd,
43ae1d95 1671 httpState);
090089c4 1672
21b92762 1673 /* Schedule read reply. */
1674 commSetTimeout(fd, Config.Timeout.lifetime, httpTimeout, httpState);
1675 entry->delayAwareRead(fd, httpState->buf, SQUID_TCP_SO_RCVBUF, httpReadReply, httpState);
1676
a2ac85d9 1677 if (httpState->orig_request->body_connection.getRaw() != NULL)
62e76326 1678 sendHeaderDone = httpSendRequestEntity;
7db8b16d 1679 else
62e76326 1680 sendHeaderDone = HttpStateData::SendComplete;
54220df8 1681
be753325 1682 if (p != NULL) {
62e76326 1683 if (p->options.originserver) {
1684 httpState->flags.proxying = 0;
1685 httpState->flags.originpeer = 1;
1686 } else {
1687 httpState->flags.proxying = 1;
1688 httpState->flags.originpeer = 0;
1689 }
be753325 1690 } else {
62e76326 1691 httpState->flags.proxying = 0;
1692 httpState->flags.originpeer = 0;
be753325 1693 }
62e76326 1694
efb9218c 1695 /*
99edd1c3 1696 * Is keep-alive okay for all request methods?
efb9218c 1697 */
efd900cb 1698 if (!Config.onoff.server_pconns)
62e76326 1699 httpState->flags.keepalive = 0;
efd900cb 1700 else if (p == NULL)
62e76326 1701 httpState->flags.keepalive = 1;
efb9218c 1702 else if (p->stats.n_keepalives_sent < 10)
62e76326 1703 httpState->flags.keepalive = 1;
43ae1d95 1704 else if ((double) p->stats.n_keepalives_recv /
1705 (double) p->stats.n_keepalives_sent > 0.50)
62e76326 1706 httpState->flags.keepalive = 1;
1707
a7ad6e4e 1708 if (httpState->_peer) {
62e76326 1709 if (neighborType(httpState->_peer, httpState->request) == PEER_SIBLING &&
1710 !httpState->_peer->options.allow_miss)
1711 httpState->flags.only_if_cached = 1;
1712
1713 httpState->flags.front_end_https = httpState->_peer->front_end_https;
a7ad6e4e 1714 }
62e76326 1715
99edd1c3 1716 memBufDefInit(&mb);
1717 httpBuildRequestPrefix(req,
62e76326 1718 httpState->orig_request,
1719 entry,
1720 &mb,
1721 httpState->flags);
21b92762 1722 debug(11, 6) ("httpSendRequest: FD %d:\n%s\n", fd, mb.buf);
032785bf 1723 comm_old_write_mbuf(fd, &mb, sendHeaderDone, httpState);
090089c4 1724}
b6a2f15e 1725
910169e5 1726void
db1cd23c 1727httpStart(FwdState * fwd)
603a02fd 1728{
db1cd23c 1729 int fd = fwd->server_fd;
28c60158 1730 HttpStateData *httpState;
190154cf 1731 HttpRequest *proxy_req;
1732 HttpRequest *orig_req = fwd->request;
910169e5 1733 debug(11, 3) ("httpStart: \"%s %s\"\n",
62e76326 1734 RequestMethodStr[orig_req->method],
1735 storeUrl(fwd->entry));
e6ccf245 1736 CBDATA_INIT_TYPE(HttpStateData);
72711e31 1737 httpState = cbdataAlloc(HttpStateData);
43ae1d95 1738 httpState->ignoreCacheControl = false;
1739 httpState->surrogateNoStore = false;
db1cd23c 1740 storeLockObject(fwd->entry);
1741 httpState->fwd = fwd;
1742 httpState->entry = fwd->entry;
9e4ad609 1743 httpState->fd = fd;
62e76326 1744
db1cd23c 1745 if (fwd->servers)
62e76326 1746 httpState->_peer = fwd->servers->_peer; /* might be NULL */
1747
29b8d8d6 1748 if (httpState->_peer) {
62e76326 1749 const char *url;
1750
1751 if (httpState->_peer->options.originserver)
1752 url = orig_req->urlpath.buf();
1753 else
1754 url = storeUrl(httpState->entry);
1755
1756 proxy_req = requestCreate(orig_req->method,
1757 orig_req->protocol, url);
1758
1759 xstrncpy(proxy_req->host, httpState->_peer->host, SQUIDHOSTNAMELEN);
1760
1761 proxy_req->port = httpState->_peer->http_port;
1762
1763 proxy_req->flags = orig_req->flags;
1764
1765 proxy_req->lastmod = orig_req->lastmod;
1766
1767 httpState->request = requestLink(proxy_req);
1768
1769 httpState->orig_request = requestLink(orig_req);
1770
1771 proxy_req->flags.proxying = 1;
1772
1773 /*
1774 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
1775 * We might end up getting the object from somewhere else if,
1776 * for example, the request to this neighbor fails.
1777 */
1778 if (httpState->_peer->options.proxy_only)
1779 storeReleaseRequest(httpState->entry);
1780
95e36d02 1781#if DELAY_POOLS
62e76326 1782
a46d2c0e 1783 httpState->entry->setNoDelay(httpState->_peer->options.no_delay);
62e76326 1784
95e36d02 1785#endif
62e76326 1786
603a02fd 1787 } else {
62e76326 1788 httpState->request = requestLink(orig_req);
1789 httpState->orig_request = requestLink(orig_req);
603a02fd 1790 }
62e76326 1791
910169e5 1792 /*
1793 * register the handler to free HTTP state data when the FD closes
1794 */
1795 comm_add_close_handler(fd, httpStateFree, httpState);
62e76326 1796
83704487 1797 statCounter.server.all.requests++;
62e76326 1798
83704487 1799 statCounter.server.http.requests++;
62e76326 1800
b6a2f15e 1801 httpSendRequest(httpState);
62e76326 1802
b6a2f15e 1803 /*
1804 * We used to set the read timeout here, but not any more.
1805 * Now its set in httpSendComplete() after the full request,
1806 * including request body, has been written to the server.
1807 */
090089c4 1808}
1809
54220df8 1810static void
c3d63b2a 1811httpSendRequestEntityDone(int fd, void *data)
54220df8 1812{
e6ccf245 1813 HttpStateData *httpState = static_cast<HttpStateData *>(data);
4fb35c3c 1814 ACLChecklist ch;
43ae1d95 1815 debug(11, 5) ("httpSendRequestEntityDone: FD %d\n", fd);
8000a965 1816 ch.request = requestLink(httpState->request);
506768d9 1817
1818 if (Config.accessList.brokenPosts)
1819 ch.accessList = cbdataReference(Config.accessList.brokenPosts);
62e76326 1820
94439e4e 1821 if (!Config.accessList.brokenPosts) {
62e76326 1822 debug(11, 5) ("httpSendRequestEntityDone: No brokenPosts list\n");
1823 HttpStateData::SendComplete(fd, NULL, 0, COMM_OK, data);
b448c119 1824 } else if (!ch.fastCheck()) {
62e76326 1825 debug(11, 5) ("httpSendRequestEntityDone: didn't match brokenPosts\n");
1826 HttpStateData::SendComplete(fd, NULL, 0, COMM_OK, data);
94439e4e 1827 } else {
62e76326 1828 debug(11, 2) ("httpSendRequestEntityDone: matched brokenPosts\n");
1829 comm_old_write(fd, "\r\n", 2, HttpStateData::SendComplete, data, NULL);
54220df8 1830 }
b448c119 1831
1832 ch.accessList = NULL;
94439e4e 1833}
1834
1835static void
e6ccf245 1836httpRequestBodyHandler(char *buf, ssize_t size, void *data)
94439e4e 1837{
1838 HttpStateData *httpState = (HttpStateData *) data;
21b92762 1839 httpState->body_buf = NULL;
62e76326 1840
94439e4e 1841 if (size > 0) {
1a98175f 1842 if (httpState->flags.headers_parsed && !httpState->flags.abuse_detected) {
21b92762 1843 httpState->flags.abuse_detected = 1;
1844 debug(11, 1) ("httpSendRequestEntryDone: Likely proxy abuse detected '%s' -> '%s'\n",
1845 inet_ntoa(httpState->orig_request->client_addr),
1846 storeUrl(httpState->entry));
1847
1848 if (httpState->entry->getReply()->sline.status == HTTP_INVALID_HEADER) {
1849 memFree8K(buf);
1850 comm_close(httpState->fd);
1851 return;
1852 }
1853 }
1854
62e76326 1855 comm_old_write(httpState->fd, buf, size, httpSendRequestEntity, data, memFree8K);
94439e4e 1856 } else if (size == 0) {
62e76326 1857 /* End of body */
1858 memFree8K(buf);
1859 httpSendRequestEntityDone(httpState->fd, data);
94439e4e 1860 } else {
62e76326 1861 /* Failed to get whole body, probably aborted */
1862 memFree8K(buf);
1863 HttpStateData::SendComplete(httpState->fd, NULL, 0, COMM_ERR_CLOSING, data);
b6a2f15e 1864 }
376bb137 1865}
1866
1867static void
3d7e9d7c 1868httpSendRequestEntity(int fd, char *bufnotused, size_t size, comm_err_t errflag, void *data)
376bb137 1869{
e6ccf245 1870 HttpStateData *httpState = static_cast<HttpStateData *>(data);
376bb137 1871 StoreEntry *entry = httpState->entry;
1872 ErrorState *err;
c3d63b2a 1873 debug(11, 5) ("httpSendRequestEntity: FD %d: size %d: errflag %d.\n",
62e76326 1874 fd, (int) size, errflag);
1875
376bb137 1876 if (size > 0) {
62e76326 1877 fd_bytes(fd, size, FD_WRITE);
1878 kb_incr(&statCounter.server.all.kbytes_out, size);
1879 kb_incr(&statCounter.server.http.kbytes_out, size);
376bb137 1880 }
62e76326 1881
376bb137 1882 if (errflag == COMM_ERR_CLOSING)
62e76326 1883 return;
1884
376bb137 1885 if (errflag) {
4eb368f9 1886 err = errorCon(ERR_WRITE_ERROR, HTTP_BAD_GATEWAY);
62e76326 1887 err->xerrno = errno;
1888 err->request = requestLink(httpState->orig_request);
1889 errorAppendEntry(entry, err);
1890 comm_close(fd);
1891 return;
376bb137 1892 }
62e76326 1893
94439e4e 1894 if (EBIT_TEST(entry->flags, ENTRY_ABORTED)) {
62e76326 1895 comm_close(fd);
1896 return;
376bb137 1897 }
62e76326 1898
21b92762 1899 httpState->body_buf = (char *)memAllocate(MEM_8K_BUF);
1900 clientReadBody(httpState->orig_request, httpState->body_buf, 8192, httpRequestBodyHandler, httpState);
54220df8 1901}
ccf44862 1902
1903void
450e0c10 1904httpBuildVersion(HttpVersion * version, unsigned int major, unsigned int minor)
110eb4e5 1905{
1906 version->major = major;
1907 version->minor = minor;
ccf44862 1908}