3 * $Id: http.cc,v 1.414 2003/05/24 11:32:34 robertc Exp $
5 * DEBUG: section 11 Hypertext Transfer Protocol (HTTP)
6 * AUTHOR: Harvest Derived
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
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.
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.
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.
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
32 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
37 * Anonymizing patch by lutz@as-node.jena.thur.de
38 * have a look into http-anon.c to get more informations.
43 #include "authenticate.h"
45 #include "HttpReply.h"
46 #include "HttpRequest.h"
47 #include "MemObject.h"
48 #include "HttpHdrContRange.h"
49 #include "ACLChecklist.h"
51 #include "DelayPools.h"
54 CBDATA_TYPE(HttpStateData
);
57 static const char *const crlf
= "\r\n";
59 static CWCB httpSendRequestEntity
;
61 static IOCB httpReadReply
;
62 static void httpSendRequest(HttpStateData
*);
63 static PF httpStateFree
;
64 static PF httpTimeout
;
65 static void httpCacheNegatively(StoreEntry
*);
66 static void httpMakePrivate(StoreEntry
*);
67 static void httpMakePublic(StoreEntry
*);
68 static int httpCachableReply(HttpStateData
*);
69 static void httpMaybeRemovePublic(StoreEntry
*, http_status
);
70 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, String strConnection
, request_t
* request
, request_t
* orig_request
,
71 HttpHeader
* hdr_out
, int we_do_ranges
, http_state_flags
);
72 static int decideIfWeDoRanges (request_t
* orig_request
);
76 httpStateFree(int fd
, void *data
)
78 HttpStateData
*httpState
= static_cast<HttpStateData
*>(data
);
80 if (httpState
== NULL
)
83 storeUnlockObject(httpState
->entry
);
85 if (httpState
->reply_hdr
) {
86 memFree(httpState
->reply_hdr
, MEM_8K_BUF
);
87 httpState
->reply_hdr
= NULL
;
90 requestUnlink(httpState
->request
);
91 requestUnlink(httpState
->orig_request
);
92 httpState
->request
= NULL
;
93 httpState
->orig_request
= NULL
;
94 cbdataFree(httpState
);
98 httpCachable(method_t method
)
100 /* GET and HEAD are cachable. Others are not. */
102 if (method
!= METHOD_GET
&& method
!= METHOD_HEAD
)
110 httpTimeout(int fd
, void *data
)
112 HttpStateData
*httpState
= static_cast<HttpStateData
*>(data
);
113 StoreEntry
*entry
= httpState
->entry
;
114 debug(11, 4) ("httpTimeout: FD %d: '%s'\n", fd
, storeUrl(entry
));
116 if (entry
->store_status
== STORE_PENDING
) {
117 if (entry
->isEmpty()) {
118 fwdFail(httpState
->fwd
,
119 errorCon(ERR_READ_TIMEOUT
, HTTP_GATEWAY_TIMEOUT
));
126 /* This object can be cached for a long time */
128 httpMakePublic(StoreEntry
* entry
)
130 if (EBIT_TEST(entry
->flags
, ENTRY_CACHABLE
))
131 storeSetPublicKey(entry
);
134 /* This object should never be cached at all */
136 httpMakePrivate(StoreEntry
* entry
)
138 storeExpireNow(entry
);
139 storeReleaseRequest(entry
); /* delete object when not used */
140 /* storeReleaseRequest clears ENTRY_CACHABLE flag */
143 /* This object may be negatively cached */
145 httpCacheNegatively(StoreEntry
* entry
)
147 storeNegativeCache(entry
);
149 if (EBIT_TEST(entry
->flags
, ENTRY_CACHABLE
))
150 storeSetPublicKey(entry
);
154 httpMaybeRemovePublic(StoreEntry
* e
, http_status status
)
164 if (!EBIT_TEST(e
->flags
, KEY_PRIVATE
))
171 case HTTP_NON_AUTHORITATIVE_INFORMATION
:
173 case HTTP_MULTIPLE_CHOICES
:
175 case HTTP_MOVED_PERMANENTLY
:
177 case HTTP_MOVED_TEMPORARILY
:
190 case HTTP_METHOD_NOT_ALLOWED
:
197 case HTTP_UNAUTHORIZED
:
207 * Any 2xx response should eject previously cached entities...
209 if (status
>= 200 && status
< 300)
224 if (e
->mem_obj
->request
)
225 pe
= storeGetPublicByRequest(e
->mem_obj
->request
);
227 pe
= storeGetPublic(e
->mem_obj
->url
, e
->mem_obj
->method
);
235 * Also remove any cached HEAD response in case the object has
238 if (e
->mem_obj
->request
)
239 pe
= storeGetPublicByRequestMethod(e
->mem_obj
->request
, METHOD_HEAD
);
241 pe
= storeGetPublic(e
->mem_obj
->url
, METHOD_HEAD
);
251 switch (e
->mem_obj
->method
) {
257 case METHOD_PROPPATCH
:
267 * Remove any cached GET object if it is beleived that the
268 * object may have changed as a result of other methods
271 if (e
->mem_obj
->request
)
272 pe
= storeGetPublicByRequestMethod(e
->mem_obj
->request
, METHOD_GET
);
274 pe
= storeGetPublic(e
->mem_obj
->url
, METHOD_GET
);
284 /* Keep GCC happy. The methods above are all mutating HTTP methods
291 HttpStateData::processSurrogateControl(HttpReply
*reply
)
295 if (request
->flags
.accelerated
&& reply
->surrogate_control
) {
296 HttpHdrScTarget
*sctusable
=
297 httpHdrScGetMergedTarget(reply
->surrogate_control
,
298 Config
.Accel
.surrogate_id
);
301 if (EBIT_TEST(sctusable
->mask
, SC_NO_STORE
) ||
302 (Config
.onoff
.surrogate_is_remote
303 && EBIT_TEST(sctusable
->mask
, SC_NO_STORE_REMOTE
))) {
304 surrogateNoStore
= true;
305 httpMakePrivate(entry
);
308 /* The HttpHeader logic cannot tell if the header it's parsing is a reply to an
309 * accelerated request or not...
310 * Still, this is an abtraction breach. - RC
312 if (sctusable
->max_age
!= -1) {
313 if (sctusable
->max_age
< sctusable
->max_stale
)
314 reply
->expires
= reply
->date
+ sctusable
->max_age
;
316 reply
->expires
= reply
->date
+ sctusable
->max_stale
;
318 /* And update the timestamps */
319 storeTimestampsSet(entry
);
322 /* We ignore cache-control directives as per the Surrogate specification */
323 ignoreCacheControl
= true;
325 httpHdrScTargetDestroy(sctusable
);
333 cacheControlAllowsCaching(HttpHdrCc
*cc
)
336 const int cc_mask
= cc
->mask
;
338 if (EBIT_TEST(cc_mask
, CC_PRIVATE
))
341 if (EBIT_TEST(cc_mask
, CC_NO_CACHE
))
344 if (EBIT_TEST(cc_mask
, CC_NO_STORE
))
352 httpCachableReply(HttpStateData
* httpState
)
354 HttpReply
const *rep
= httpState
->entry
->getReply();
355 HttpHeader
const *hdr
= &rep
->header
;
356 const int cc_mask
= (rep
->cache_control
) ? rep
->cache_control
->mask
: 0;
359 if (httpState
->surrogateNoStore
)
362 if (!cacheControlAllowsCaching(rep
->cache_control
))
365 if (!httpState
->ignoreCacheControl
) {
366 if (EBIT_TEST(cc_mask
, CC_PRIVATE
))
369 if (EBIT_TEST(cc_mask
, CC_NO_CACHE
))
372 if (EBIT_TEST(cc_mask
, CC_NO_STORE
))
376 if (httpState
->request
->flags
.auth
) {
378 * Responses to requests with authorization may be cached
379 * only if a Cache-Control: public reply header is present.
380 * RFC 2068, sec 14.9.4
383 if (!EBIT_TEST(cc_mask
, CC_PUBLIC
))
387 /* Pragma: no-cache in _replies_ is not documented in HTTP,
388 * but servers like "Active Imaging Webcast/2.0" sure do use it */
389 if (httpHeaderHas(hdr
, HDR_PRAGMA
)) {
390 String s
= httpHeaderGetList(hdr
, HDR_PRAGMA
);
391 const int no_cache
= strListIsMember(&s
, "no-cache", ',');
399 * The "multipart/x-mixed-replace" content type is used for
400 * continuous push replies. These are generally dynamic and
401 * probably should not be cachable
403 if ((v
= httpHeaderGetStr(hdr
, HDR_CONTENT_TYPE
)))
404 if (!strncasecmp(v
, "multipart/x-mixed-replace", 25))
407 switch (httpState
->entry
->getReply()->sline
.status
) {
408 /* Responses that are cacheable */
412 case HTTP_NON_AUTHORITATIVE_INFORMATION
:
414 case HTTP_MULTIPLE_CHOICES
:
416 case HTTP_MOVED_PERMANENTLY
:
420 * Don't cache objects that need to be refreshed on next request,
421 * unless we know how to refresh it.
424 if (!refreshIsCachable(httpState
->entry
))
427 /* don't cache objects from peers w/o LMT, Date, or Expires */
428 /* check that is it enough to check headers @?@ */
431 else if (rep
->last_modified
> -1)
433 else if (!httpState
->_peer
)
436 /* @?@ (here and 302): invalid expires header compiles to squid_curtime */
437 else if (rep
->expires
> -1)
445 /* Responses that only are cacheable if the server says so */
447 case HTTP_MOVED_TEMPORARILY
:
448 if (rep
->expires
> -1)
456 /* Errors can be negatively cached */
458 case HTTP_NO_CONTENT
:
462 case HTTP_BAD_REQUEST
:
468 case HTTP_METHOD_NOT_ALLOWED
:
470 case HTTP_REQUEST_URI_TOO_LARGE
:
472 case HTTP_INTERNAL_SERVER_ERROR
:
474 case HTTP_NOT_IMPLEMENTED
:
476 case HTTP_BAD_GATEWAY
:
478 case HTTP_SERVICE_UNAVAILABLE
:
480 case HTTP_GATEWAY_TIMEOUT
:
486 /* Some responses can never be cached */
488 case HTTP_PARTIAL_CONTENT
: /* Not yet supported */
492 case HTTP_NOT_MODIFIED
:
494 case HTTP_UNAUTHORIZED
:
496 case HTTP_PROXY_AUTHENTICATION_REQUIRED
:
498 case HTTP_INVALID_HEADER
: /* Squid header parsing error */
501 default: /* Unknown status code */
502 debug (11,0)("httpCachableReply: unknown http status code in reply\n");
514 * For Vary, store the relevant request headers as
515 * virtual headers in the reply
516 * Returns false if the variance cannot be stored
519 httpMakeVaryMark(request_t
* request
, HttpReply
const * reply
)
522 const char *pos
= NULL
;
529 vary
= httpHeaderGetList(&reply
->header
, HDR_VARY
);
531 while (strListGetItem(&vary
, ',', &item
, &ilen
, &pos
)) {
532 char *name
= (char *)xmalloc(ilen
+ 1);
533 xstrncpy(name
, item
, ilen
+ 1);
535 strListAdd(&vstr
, name
, ',');
536 hdr
= httpHeaderGetByName(&request
->header
, name
);
541 value
= rfc1738_escape_part(value
);
542 vstr
.append("=\"", 2);
544 vstr
.append("\"", 1);
551 #if X_ACCELERATOR_VARY
553 vary
= httpHeaderGetList(&reply
->header
, HDR_X_ACCELERATOR_VARY
);
555 while (strListGetItem(&vary
, ',', &item
, &ilen
, &pos
)) {
556 char *name
= (char *)xmalloc(ilen
+ 1);
557 xstrncpy(name
, item
, ilen
+ 1);
559 strListAdd(&vstr
, name
, ',');
560 hdr
= httpHeaderGetByName(&request
->header
, name
);
565 value
= rfc1738_escape_part(value
);
566 vstr
.append("=\"", 2);
568 vstr
.append("\"", 1);
577 debug(11, 3) ("httpMakeVaryMark: %s\n", vstr
.buf());
581 /* rewrite this later using new interfaces @?@ */
583 HttpStateData::processReplyHeader(const char *buf
, int size
)
588 /* Creates a blank header. If this routine is made incremental, this will
591 HttpReply
*reply
= httpReplyCreate();
593 debug(11, 3) ("httpProcessReplyHeader: key '%s'\n",
594 entry
->getMD5Text());
596 if (reply_hdr
== NULL
)
597 reply_hdr
= (char *)memAllocate(MEM_8K_BUF
);
599 assert(reply_hdr_state
== 0);
601 hdr_len
= reply_hdr_size
;
603 room
= 8191 - hdr_len
;
605 xmemcpy(reply_hdr
+ hdr_len
, buf
, room
< size
? room
: size
);
607 hdr_len
+= room
< size
? room
: size
;
609 reply_hdr
[hdr_len
] = '\0';
611 reply_hdr_size
= hdr_len
;
613 if (hdr_len
> 4 && strncmp(reply_hdr
, "HTTP/", 5)) {
614 debug(11, 3) ("httpProcessReplyHeader: Non-HTTP-compliant header: '%s'\n", reply_hdr
);
615 reply_hdr_state
+= 2;
616 reply
->sline
.status
= HTTP_INVALID_HEADER
;
617 storeEntryReplaceObject (entry
, reply
);
627 t
= reply_hdr
+ hdr_len
;
628 /* headers can be incomplete only if object still arriving */
631 size_t k
= headersEnd(reply_hdr
, 8192);
639 return; /* headers not complete */
647 assert(reply_hdr_state
== 1);
648 ctx
= ctx_enter(entry
->mem_obj
->url
);
650 debug(11, 9) ("GOT HTTP REPLY HDR:\n---------\n%s\n----------\n",
652 /* Parse headers into reply structure */
653 /* what happens if we fail to parse here? */
654 httpReplyParse(reply
, reply_hdr
, hdr_len
);
655 processSurrogateControl (reply
);
656 /* TODO: we need our own reply * in the httpState, as we probably don't want to replace
657 * the storeEntry with interim headers
660 /* TODO: IF the reply is a 1.0 reply, AND it has a Connection: Header
661 * Parse the header and remove all referenced headers
664 storeEntryReplaceObject(entry
, reply
);
665 /* DO NOT USE reply now */
668 if (entry
->getReply()->content_range
)
669 currentOffset
= entry
->getReply()->content_range
->spec
.offset
;
671 storeTimestampsSet(entry
);
673 /* Check if object is cacheable or not based on reply code */
674 debug(11, 3) ("httpProcessReplyHeader: HTTP CODE: %d\n", entry
->getReply()->sline
.status
);
676 if (neighbors_do_private_keys
)
677 httpMaybeRemovePublic(entry
, entry
->getReply()->sline
.status
);
679 switch (httpCachableReply(this)) {
683 if (httpHeaderHas(&entry
->getReply()->header
, HDR_VARY
)
684 #if X_ACCELERATOR_VARY
685 || httpHeaderHas(&entry
->getReply()->header
, HDR_X_ACCELERATOR_VARY
)
688 const char *vary
= httpMakeVaryMark(orig_request
, entry
->getReply());
691 entry
->mem_obj
->vary_headers
= xstrdup(vary
);
692 /* Kill the old base object if a change in variance is detected */
693 httpMakePublic(entry
);
695 httpMakePrivate(entry
);
698 httpMakePublic(entry
);
704 httpMakePrivate(entry
);
708 httpCacheNegatively(entry
);
716 if (!ignoreCacheControl
&& entry
->getReply()->cache_control
) {
717 if (EBIT_TEST(entry
->getReply()->cache_control
->mask
, CC_PROXY_REVALIDATE
))
718 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE
);
719 else if (EBIT_TEST(entry
->getReply()->cache_control
->mask
, CC_MUST_REVALIDATE
))
720 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE
);
725 _peer
->stats
.n_keepalives_sent
++;
727 if (entry
->getReply()->keep_alive
)
729 _peer
->stats
.n_keepalives_recv
++;
731 if (entry
->getReply()->date
> -1 && !_peer
) {
732 int skew
= abs(entry
->getReply()->date
- squid_curtime
);
735 debug(11, 3) ("%s's clock is skewed by %d seconds!\n",
736 request
->host
, skew
);
742 headersLog(1, 0, request
->method
, entry
->getReply());
751 HttpStateData::ConnectionStatus
752 HttpStateData::statusIfComplete() const
754 HttpReply
const *reply
= entry
->getReply();
755 /* If the reply wants to close the connection, it takes precedence */
757 if (httpHeaderHasConnDir(&reply
->header
, "close"))
758 return COMPLETE_NONPERSISTENT_MSG
;
760 /* If we didn't send a keep-alive request header, then this
761 * can not be a persistent connection.
763 if (!flags
.keepalive
)
764 return COMPLETE_NONPERSISTENT_MSG
;
767 * What does the reply have to say about keep-alive?
771 * If the origin server (HTTP/1.0) does not send a keep-alive
772 * header, but keeps the connection open anyway, what happens?
773 * We'll return here and http.c waits for an EOF before changing
774 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
775 * and an error status code, and we might have to wait until
776 * the server times out the socket.
778 if (!reply
->keep_alive
)
779 return COMPLETE_NONPERSISTENT_MSG
;
781 return COMPLETE_PERSISTENT_MSG
;
784 HttpStateData::ConnectionStatus
785 HttpStateData::persistentConnStatus() const
787 HttpReply
const *reply
= entry
->getReply();
789 debug(11, 3) ("httpPconnTransferDone: FD %d\n", fd
);
790 ConnectionStatus result
= statusIfComplete();
791 debug(11, 5) ("httpPconnTransferDone: content_length=%d\n",
792 reply
->content_length
);
793 /* If we haven't seen the end of reply headers, we are not done */
795 if (reply_hdr_state
< 2)
796 return INCOMPLETE_MSG
;
798 clen
= httpReplyBodySize(request
->method
, reply
);
800 /* If there is no message body, we can be persistent */
804 /* If the body size is unknown we must wait for EOF */
806 return INCOMPLETE_MSG
;
808 /* If the body size is known, we must wait until we've gotten all of it. */
809 if (entry
->mem_obj
->endOffset() < reply
->content_length
+ reply
->hdr_sz
)
810 return INCOMPLETE_MSG
;
816 /* This will be called when data is ready to be read from fd. Read until
817 * error or connection closed. */
818 /* XXX this function is too long! */
820 httpReadReply(int fd
, char *buf
, size_t len
, comm_err_t flag
, int xerrno
,void *data
)
822 HttpStateData
*httpState
= static_cast<HttpStateData
*>(data
);
823 assert (fd
== httpState
->fd
);
824 httpState
->readReply (fd
, buf
, len
, flag
, xerrno
, data
);
828 HttpStateData::readReply (int fd
, char *readBuf
, size_t len
, comm_err_t flag
, int xerrno
,void *data
)
835 assert(buf
== readBuf
);
837 /* Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us
840 if (flag
== COMM_ERR_CLOSING
) {
841 debug (11,1)("http socket closing\n");
845 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
851 /* prepare the read size for the next read (if any) */
854 DelayId delayId
= entry
->mem_obj
->mostBytesAllowed();
858 debug(11, 5) ("httpReadReply: FD %d: len %d.\n", fd
, (int)len
);
860 if (flag
== COMM_OK
&& len
> 0) {
862 delayId
.bytesIn(len
);
865 kb_incr(&statCounter
.server
.all
.kbytes_in
, len
);
866 kb_incr(&statCounter
.server
.http
.kbytes_in
, len
);
867 commSetTimeout(fd
, Config
.Timeout
.read
, NULL
, NULL
);
868 IOStats
.Http
.reads
++;
870 for (clen
= len
- 1, bin
= 0; clen
; bin
++)
873 IOStats
.Http
.read_hist
[bin
]++;
876 if (!reply_hdr
&& flag
== COMM_OK
&& len
> 0) {
877 /* Skip whitespace */
879 while (len
> 0 && xisspace(*buf
))
880 xmemmove(buf
, buf
+ 1, len
--);
883 /* Continue to read... */
890 if (flag
!= COMM_OK
|| len
< 0) {
891 debug(50, 2) ("httpReadReply: FD %d: read failure: %s.\n",
894 if (ignoreErrno(errno
)) {
896 } else if (entry
->isEmpty()) {
898 err
= errorCon(ERR_READ_ERROR
, HTTP_INTERNAL_SERVER_ERROR
);
899 err
->request
= requestLink((request_t
*) request
);
908 } else if (flag
== COMM_OK
&& len
== 0 && entry
->isEmpty()) {
910 err
= errorCon(ERR_ZERO_SIZE_OBJECT
, HTTP_SERVICE_UNAVAILABLE
);
912 err
->request
= requestLink((request_t
*) request
);
917 } else if (flag
== COMM_OK
&& len
== 0) {
918 /* Connection closed; retrieval done. */
921 if (reply_hdr_state
< 2)
923 * Yes Henrik, there is a point to doing this. When we
924 * called httpProcessReplyHeader() before, we didn't find
925 * the end of headers, but now we are definately at EOF, so
926 * we want to process the reply headers.
929 processReplyHeader(buf
, len
);
936 if (reply_hdr_state
< 2) {
937 processReplyHeader(buf
, len
);
939 if (reply_hdr_state
== 2) {
940 http_status s
= entry
->getReply()->sline
.status
;
946 * If its not a reply that we will re-forward, then
947 * allow the client to get it.
950 if (!fwdReforwardableStatus(s
))
951 EBIT_CLR(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
955 processReplyData(buf
, len
);
960 HttpStateData::processReplyData(const char *buf
, size_t len
)
962 if (reply_hdr_state
< 2) {
968 StoreIOBuffer tempBuffer
;
970 if (!flags
.headers_pushed
) {
971 /* The first block needs us to skip the headers */
972 /* TODO: make this cleaner. WE should push the headers, NOT the parser */
973 size_t end
= headersEnd (buf
, len
);
974 /* IF len > end, we need to append data after the
975 * out of band update to the store
979 tempBuffer
.data
= (char *)buf
+end
;
980 tempBuffer
.length
= len
- end
;
981 tempBuffer
.offset
= currentOffset
;
982 currentOffset
+= tempBuffer
.length
;
983 entry
->write (tempBuffer
);
986 flags
.headers_pushed
= 1;
988 tempBuffer
.data
= (char *)buf
;
989 tempBuffer
.length
= len
;
990 tempBuffer
.offset
= currentOffset
;
991 currentOffset
+= len
;
992 entry
->write(tempBuffer
);
995 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
997 * the above storeAppend() call could ABORT this entry,
998 * in that case, the server FD should already be closed.
999 * there's nothing for us to do.
1003 switch (persistentConnStatus()) {
1005 case INCOMPLETE_MSG
:
1006 /* Wait for EOF condition */
1010 case COMPLETE_PERSISTENT_MSG
:
1011 /* yes we have to clear all these! */
1012 commSetTimeout(fd
, -1, NULL
, NULL
);
1015 comm_remove_close_handler(fd
, httpStateFree
, this);
1016 fwdUnregister(fd
, fwd
);
1017 pconnPush(fd
, request
->host
, request
->port
);
1020 httpStateFree(fd
, this);
1023 case COMPLETE_NONPERSISTENT_MSG
:
1024 /* close the connection ourselves */
1025 /* yes - same as for a complete persistent conn here */
1026 commSetTimeout(fd
, -1, NULL
, NULL
);
1027 commSetSelect(fd
, COMM_SELECT_READ
, NULL
, NULL
, 0);
1028 comm_remove_close_handler(fd
, httpStateFree
, this);
1029 fwdUnregister(fd
, fwd
);
1031 /* TODO: check that fd is still open here */
1034 httpStateFree(fd
, this);
1042 HttpStateData::maybeReadData()
1046 entry
->delayAwareRead(fd
, buf
, SQUID_TCP_SO_RCVBUF
, httpReadReply
, this);
1050 /* This will be called when request write is complete. Schedule read of
1053 HttpStateData::SendComplete(int fd
, char *bufnotused
, size_t size
, comm_err_t errflag
, void *data
)
1055 HttpStateData
*httpState
= static_cast<HttpStateData
*>(data
);
1056 StoreEntry
*entry
= httpState
->entry
;
1058 debug(11, 5) ("httpSendComplete: FD %d: size %d: errflag %d.\n",
1059 fd
, (int) size
, errflag
);
1060 #if URL_CHECKSUM_DEBUG
1062 entry
->mem_obj
->checkUrlChecksum();
1066 fd_bytes(fd
, size
, FD_WRITE
);
1067 kb_incr(&statCounter
.server
.all
.kbytes_out
, size
);
1068 kb_incr(&statCounter
.server
.http
.kbytes_out
, size
);
1071 if (errflag
== COMM_ERR_CLOSING
)
1075 err
= errorCon(ERR_WRITE_ERROR
, HTTP_INTERNAL_SERVER_ERROR
);
1076 err
->xerrno
= errno
;
1077 err
->request
= requestLink(httpState
->orig_request
);
1078 errorAppendEntry(entry
, err
);
1082 /* Schedule read reply. */
1083 entry
->delayAwareRead(fd
, httpState
->buf
, SQUID_TCP_SO_RCVBUF
, httpReadReply
, httpState
);
1085 * Set the read timeout here because it hasn't been set yet.
1086 * We only set the read timeout after the request has been
1087 * fully written to the server-side. If we start the timeout
1088 * after connection establishment, then we are likely to hit
1089 * the timeout for POST/PUT requests that have very large
1092 commSetTimeout(fd
, Config
.Timeout
.read
, httpTimeout
, httpState
);
1097 * build request headers and append them to a given MemBuf
1098 * used by httpBuildRequestPrefix()
1099 * note: calls httpHeaderInit(), the caller is responsible for Clean()-ing
1102 httpBuildRequestHeader(request_t
* request
,
1103 request_t
* orig_request
,
1105 HttpHeader
* hdr_out
,
1106 http_state_flags flags
)
1108 /* building buffer for complex strings */
1109 #define BBUF_SZ (MAX_URL+32)
1110 LOCAL_ARRAY(char, bbuf
, BBUF_SZ
);
1111 const HttpHeader
*hdr_in
= &orig_request
->header
;
1112 const HttpHeaderEntry
*e
;
1114 HttpHeaderPos pos
= HttpHeaderInitPos
;
1115 httpHeaderInit(hdr_out
, hoRequest
);
1116 /* append our IMS header */
1118 if (request
->lastmod
> -1 && request
->method
== METHOD_GET
)
1119 httpHeaderPutTime(hdr_out
, HDR_IF_MODIFIED_SINCE
, request
->lastmod
);
1121 bool we_do_ranges
= decideIfWeDoRanges (orig_request
);
1123 String
strConnection (httpHeaderGetList(hdr_in
, HDR_CONNECTION
));
1125 while ((e
= httpHeaderGetEntry(hdr_in
, &pos
)))
1126 copyOneHeaderFromClientsideRequestToUpstreamRequest(e
, strConnection
, request
, orig_request
, hdr_out
, we_do_ranges
, flags
);
1128 /* Abstraction break: We should interpret multipart/byterange responses
1129 * into offset-length data, and this works around our inability to do so.
1131 if (!we_do_ranges
&& orig_request
->multipartRangeRequest()) {
1132 /* don't cache the result */
1133 orig_request
->flags
.cachable
= 0;
1134 /* pretend it's not a range request */
1135 orig_request
->range
->deleteSelf();
1136 orig_request
->range
= NULL
;
1137 orig_request
->flags
.range
= 0;
1142 if (Config
.onoff
.via
) {
1144 strVia
= httpHeaderGetList(hdr_in
, HDR_VIA
);
1145 snprintf(bbuf
, BBUF_SZ
, "%d.%d %s",
1146 orig_request
->http_ver
.major
,
1147 orig_request
->http_ver
.minor
, ThisCache
);
1148 strListAdd(&strVia
, bbuf
, ',');
1149 httpHeaderPutStr(hdr_out
, HDR_VIA
, strVia
.buf());
1155 /* Append Surrogate-Capabilities */
1156 String
strSurrogate (httpHeaderGetList(hdr_in
, HDR_SURROGATE_CAPABILITY
));
1157 snprintf(bbuf
, BBUF_SZ
, "%s=\"Surrogate/1.0 ESI/1.0\"",
1158 Config
.Accel
.surrogate_id
);
1159 strListAdd(&strSurrogate
, bbuf
, ',');
1160 httpHeaderPutStr(hdr_out
, HDR_SURROGATE_CAPABILITY
, strSurrogate
.buf());
1164 /* append X-Forwarded-For */
1165 strFwd
= httpHeaderGetList(hdr_in
, HDR_X_FORWARDED_FOR
);
1167 if (opt_forwarded_for
&& orig_request
->client_addr
.s_addr
!= no_addr
.s_addr
)
1168 strListAdd(&strFwd
, inet_ntoa(orig_request
->client_addr
), ',');
1170 strListAdd(&strFwd
, "unknown", ',');
1172 httpHeaderPutStr(hdr_out
, HDR_X_FORWARDED_FOR
, strFwd
.buf());
1176 /* append Host if not there already */
1177 if (!httpHeaderHas(hdr_out
, HDR_HOST
)) {
1178 if (orig_request
->peer_domain
) {
1179 httpHeaderPutStr(hdr_out
, HDR_HOST
, orig_request
->peer_domain
);
1180 } else if (orig_request
->port
== urlDefaultPort(orig_request
->protocol
)) {
1181 /* use port# only if not default */
1182 httpHeaderPutStr(hdr_out
, HDR_HOST
, orig_request
->host
);
1184 httpHeaderPutStrf(hdr_out
, HDR_HOST
, "%s:%d",
1185 orig_request
->host
, (int) orig_request
->port
);
1189 /* append Authorization if known in URL, not in header and going direct */
1190 if (!httpHeaderHas(hdr_out
, HDR_AUTHORIZATION
)) {
1191 if (!request
->flags
.proxying
&& *request
->login
) {
1192 httpHeaderPutStrf(hdr_out
, HDR_AUTHORIZATION
, "Basic %s",
1193 base64_encode(request
->login
));
1197 /* append Proxy-Authorization if configured for peer, and proxying */
1198 if (request
->flags
.proxying
&& orig_request
->peer_login
&&
1199 !httpHeaderHas(hdr_out
, HDR_PROXY_AUTHORIZATION
)) {
1200 if (*orig_request
->peer_login
== '*') {
1201 /* Special mode, to pass the username to the upstream cache */
1203 const char *username
= "-";
1205 if (orig_request
->auth_user_request
)
1206 username
= orig_request
->auth_user_request
->username();
1208 snprintf(loginbuf
, sizeof(loginbuf
), "%s%s", username
, orig_request
->peer_login
+ 1);
1210 httpHeaderPutStrf(hdr_out
, HDR_PROXY_AUTHORIZATION
, "Basic %s",
1211 base64_encode(loginbuf
));
1212 } else if (strcmp(orig_request
->peer_login
, "PASS") == 0) {
1214 } else if (strcmp(orig_request
->peer_login
, "PROXYPASS") == 0) {
1217 httpHeaderPutStrf(hdr_out
, HDR_PROXY_AUTHORIZATION
, "Basic %s",
1218 base64_encode(orig_request
->peer_login
));
1222 /* append WWW-Authorization if configured for peer */
1223 if (flags
.originpeer
&& orig_request
->peer_login
&&
1224 !httpHeaderHas(hdr_out
, HDR_AUTHORIZATION
)) {
1225 if (strcmp(orig_request
->peer_login
, "PASS") == 0) {
1226 /* No credentials to forward.. (should have been done above if available) */
1227 } else if (strcmp(orig_request
->peer_login
, "PROXYPASS") == 0) {
1228 /* Special mode, convert proxy authentication to WWW authentication
1230 const char *auth
= httpHeaderGetStr(hdr_in
, HDR_PROXY_AUTHORIZATION
);
1232 if (auth
&& strncasecmp(auth
, "basic ", 6) == 0) {
1233 httpHeaderPutStr(hdr_out
, HDR_AUTHORIZATION
, auth
);
1235 } else if (*orig_request
->peer_login
== '*') {
1236 /* Special mode, to pass the username to the upstream cache */
1238 const char *username
= "-";
1240 if (orig_request
->auth_user_request
)
1241 username
= authenticateUserRequestUsername(orig_request
->auth_user_request
);
1243 snprintf(loginbuf
, sizeof(loginbuf
), "%s%s", username
, orig_request
->peer_login
+ 1);
1245 httpHeaderPutStrf(hdr_out
, HDR_AUTHORIZATION
, "Basic %s",
1246 base64_encode(loginbuf
));
1248 /* Fixed login string */
1249 httpHeaderPutStrf(hdr_out
, HDR_AUTHORIZATION
, "Basic %s",
1250 base64_encode(orig_request
->peer_login
));
1254 /* append Cache-Control, add max-age if not there already */
1256 HttpHdrCc
*cc
= httpHeaderGetCc(hdr_in
);
1259 cc
= httpHdrCcCreate();
1261 if (!EBIT_TEST(cc
->mask
, CC_MAX_AGE
)) {
1263 entry
? storeUrl(entry
) : urlCanonical(orig_request
);
1264 httpHdrCcSetMaxAge(cc
, getMaxAge(url
));
1266 if (request
->urlpath
.size())
1267 assert(strstr(url
, request
->urlpath
.buf()));
1270 if (flags
.only_if_cached
)
1271 EBIT_SET(cc
->mask
, CC_ONLY_IF_CACHED
);
1273 httpHeaderPutCc(hdr_out
, cc
);
1275 httpHdrCcDestroy(cc
);
1278 /* maybe append Connection: keep-alive */
1279 if (flags
.keepalive
) {
1280 if (flags
.proxying
) {
1281 httpHeaderPutStr(hdr_out
, HDR_PROXY_CONNECTION
, "keep-alive");
1283 httpHeaderPutStr(hdr_out
, HDR_CONNECTION
, "keep-alive");
1287 /* append Front-End-Https */
1288 if (flags
.front_end_https
) {
1289 if (flags
.front_end_https
== 1 || request
->protocol
== PROTO_HTTPS
)
1290 httpHeaderPutStr(hdr_out
, HDR_FRONT_END_HTTPS
, "On");
1293 /* Now mangle the headers. */
1294 httpHdrMangleList(hdr_out
, request
);
1296 strConnection
.clean();
1301 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, String strConnection
, request_t
* request
, request_t
* orig_request
, HttpHeader
* hdr_out
, int we_do_ranges
, http_state_flags flags
)
1303 debug(11, 5) ("httpBuildRequestHeader: %s: %s\n",
1304 e
->name
.buf(), e
->value
.buf());
1306 if (!httpRequestHdrAllowed(e
, &strConnection
)) {
1307 debug(11, 2) ("'%s' header denied by anonymize_headers configuration\n",+ e
->name
.buf());
1313 case HDR_PROXY_AUTHORIZATION
:
1314 /* Only pass on proxy authentication to peers for which
1315 * authentication forwarding is explicitly enabled
1318 if (flags
.proxying
&& orig_request
->peer_login
&&
1319 strcmp(orig_request
->peer_login
, "PASS") == 0) {
1320 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1325 case HDR_AUTHORIZATION
:
1326 /* Pass on WWW authentication */
1328 if (!flags
.originpeer
) {
1329 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1331 /* In accelerators, only forward authentication if enabled
1332 * (see also below for proxy->server authentication)
1335 if (orig_request
->peer_login
&& (strcmp(orig_request
->peer_login
, "PASS") == 0 || strcmp(orig_request
->peer_login
, "PROXYPASS") == 0)) {
1336 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1344 * Normally Squid does not copy the Host: header from
1345 * a client request into the forwarded request headers.
1346 * However, there is one case when we do: If the URL
1347 * went through our redirector and the admin configured
1348 * 'redir_rewrites_host' to be off.
1351 if (request
->flags
.redirected
)
1352 if (!Config
.onoff
.redir_rewrites_host
)
1353 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1357 case HDR_IF_MODIFIED_SINCE
:
1358 /* append unless we added our own;
1359 * note: at most one client's ims header can pass through */
1360 if (!httpHeaderHas(hdr_out
, HDR_IF_MODIFIED_SINCE
))
1361 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1365 case HDR_MAX_FORWARDS
:
1366 if (orig_request
->method
== METHOD_TRACE
) {
1367 const int hops
= httpHeaderEntryGetInt(e
);
1370 httpHeaderPutInt(hdr_out
, HDR_MAX_FORWARDS
, hops
- 1);
1376 /* If Via is disabled then forward any received header as-is */
1378 if (!Config
.onoff
.via
)
1379 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1387 case HDR_REQUEST_RANGE
:
1389 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1393 case HDR_PROXY_CONNECTION
:
1395 case HDR_CONNECTION
:
1397 case HDR_X_FORWARDED_FOR
:
1399 case HDR_CACHE_CONTROL
:
1400 /* append these after the loop if needed */
1403 case HDR_FRONT_END_HTTPS
:
1404 if (!flags
.front_end_https
)
1405 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1410 /* pass on all other header fields */
1411 httpHeaderAddEntry(hdr_out
, httpHeaderEntryClone(e
));
1416 decideIfWeDoRanges (request_t
* orig_request
)
1419 /* decide if we want to do Ranges ourselves
1420 * and fetch the whole object now)
1421 * We want to handle Ranges ourselves iff
1422 * - we can actually parse client Range specs
1423 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
1424 * - reply will be cachable
1425 * (If the reply will be uncachable we have to throw it away after
1426 * serving this request, so it is better to forward ranges to
1427 * the server and fetch only the requested content)
1430 if (NULL
== orig_request
->range
|| !orig_request
->flags
.cachable
1431 || orig_request
->range
->offsetLimitExceeded())
1434 debug(11, 8) ("decideIfWeDoRanges: range specs: %p, cachable: %d; we_do_ranges: %d\n",
1435 orig_request
->range
, orig_request
->flags
.cachable
, result
);
1441 /* build request prefix and append it to a given MemBuf;
1442 * return the length of the prefix */
1444 httpBuildRequestPrefix(request_t
* request
,
1445 request_t
* orig_request
,
1448 http_state_flags flags
)
1450 const int offset
= mb
->size
;
1451 http_version_t httpver
;
1452 httpBuildVersion(&httpver
, 1, 0);
1453 memBufPrintf(mb
, "%s %s HTTP/%d.%d\r\n",
1454 RequestMethodStr
[request
->method
],
1455 request
->urlpath
.size() ? request
->urlpath
.buf() : "/",
1456 httpver
.major
,httpver
.minor
);
1457 /* build and pack headers */
1461 httpBuildRequestHeader(request
, orig_request
, entry
, &hdr
, flags
);
1462 packerToMemInit(&p
, mb
);
1463 httpHeaderPackInto(&hdr
, &p
);
1464 httpHeaderClean(&hdr
);
1467 /* append header terminator */
1468 memBufAppend(mb
, crlf
, 2);
1469 return mb
->size
- offset
;
1472 /* This will be called when connect completes. Write request. */
1474 httpSendRequest(HttpStateData
* httpState
)
1477 request_t
*req
= httpState
->request
;
1478 StoreEntry
*entry
= httpState
->entry
;
1479 peer
*p
= httpState
->_peer
;
1480 CWCB
*sendHeaderDone
;
1482 debug(11, 5) ("httpSendRequest: FD %d: httpState %p.\n", httpState
->fd
,
1485 if (httpState
->orig_request
->body_connection
)
1486 sendHeaderDone
= httpSendRequestEntity
;
1488 sendHeaderDone
= HttpStateData::SendComplete
;
1491 if (p
->options
.originserver
) {
1492 httpState
->flags
.proxying
= 0;
1493 httpState
->flags
.originpeer
= 1;
1495 httpState
->flags
.proxying
= 1;
1496 httpState
->flags
.originpeer
= 0;
1499 httpState
->flags
.proxying
= 0;
1500 httpState
->flags
.originpeer
= 0;
1504 * Is keep-alive okay for all request methods?
1506 if (!Config
.onoff
.server_pconns
)
1507 httpState
->flags
.keepalive
= 0;
1509 httpState
->flags
.keepalive
= 1;
1510 else if (p
->stats
.n_keepalives_sent
< 10)
1511 httpState
->flags
.keepalive
= 1;
1512 else if ((double) p
->stats
.n_keepalives_recv
/
1513 (double) p
->stats
.n_keepalives_sent
> 0.50)
1514 httpState
->flags
.keepalive
= 1;
1516 if (httpState
->_peer
) {
1517 if (neighborType(httpState
->_peer
, httpState
->request
) == PEER_SIBLING
&&
1518 !httpState
->_peer
->options
.allow_miss
)
1519 httpState
->flags
.only_if_cached
= 1;
1521 httpState
->flags
.front_end_https
= httpState
->_peer
->front_end_https
;
1525 httpBuildRequestPrefix(req
,
1526 httpState
->orig_request
,
1530 debug(11, 6) ("httpSendRequest: FD %d:\n%s\n", httpState
->fd
, mb
.buf
);
1531 comm_old_write_mbuf(httpState
->fd
, mb
, sendHeaderDone
, httpState
);
1535 httpStart(FwdState
* fwd
)
1537 int fd
= fwd
->server_fd
;
1538 HttpStateData
*httpState
;
1539 request_t
*proxy_req
;
1540 request_t
*orig_req
= fwd
->request
;
1541 debug(11, 3) ("httpStart: \"%s %s\"\n",
1542 RequestMethodStr
[orig_req
->method
],
1543 storeUrl(fwd
->entry
));
1544 CBDATA_INIT_TYPE(HttpStateData
);
1545 httpState
= cbdataAlloc(HttpStateData
);
1546 httpState
->ignoreCacheControl
= false;
1547 httpState
->surrogateNoStore
= false;
1548 storeLockObject(fwd
->entry
);
1549 httpState
->fwd
= fwd
;
1550 httpState
->entry
= fwd
->entry
;
1554 httpState
->_peer
= fwd
->servers
->_peer
; /* might be NULL */
1556 if (httpState
->_peer
) {
1559 if (httpState
->_peer
->options
.originserver
)
1560 url
= orig_req
->urlpath
.buf();
1562 url
= storeUrl(httpState
->entry
);
1564 proxy_req
= requestCreate(orig_req
->method
,
1565 orig_req
->protocol
, url
);
1567 xstrncpy(proxy_req
->host
, httpState
->_peer
->host
, SQUIDHOSTNAMELEN
);
1569 proxy_req
->port
= httpState
->_peer
->http_port
;
1571 proxy_req
->flags
= orig_req
->flags
;
1573 proxy_req
->lastmod
= orig_req
->lastmod
;
1575 httpState
->request
= requestLink(proxy_req
);
1577 httpState
->orig_request
= requestLink(orig_req
);
1579 proxy_req
->flags
.proxying
= 1;
1582 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
1583 * We might end up getting the object from somewhere else if,
1584 * for example, the request to this neighbor fails.
1586 if (httpState
->_peer
->options
.proxy_only
)
1587 storeReleaseRequest(httpState
->entry
);
1591 httpState
->entry
->setNoDelay(httpState
->_peer
->options
.no_delay
);
1596 httpState
->request
= requestLink(orig_req
);
1597 httpState
->orig_request
= requestLink(orig_req
);
1601 * register the handler to free HTTP state data when the FD closes
1603 comm_add_close_handler(fd
, httpStateFree
, httpState
);
1605 statCounter
.server
.all
.requests
++;
1607 statCounter
.server
.http
.requests
++;
1609 httpSendRequest(httpState
);
1612 * We used to set the read timeout here, but not any more.
1613 * Now its set in httpSendComplete() after the full request,
1614 * including request body, has been written to the server.
1619 httpSendRequestEntityDone(int fd
, void *data
)
1621 HttpStateData
*httpState
= static_cast<HttpStateData
*>(data
);
1623 debug(11, 5) ("httpSendRequestEntityDone: FD %d\n", fd
);
1624 ch
.request
= requestLink(httpState
->request
);
1626 if (!Config
.accessList
.brokenPosts
) {
1627 debug(11, 5) ("httpSendRequestEntityDone: No brokenPosts list\n");
1628 HttpStateData::SendComplete(fd
, NULL
, 0, COMM_OK
, data
);
1629 } else if (!aclCheckFast(Config
.accessList
.brokenPosts
, &ch
)) {
1630 debug(11, 5) ("httpSendRequestEntityDone: didn't match brokenPosts\n");
1631 HttpStateData::SendComplete(fd
, NULL
, 0, COMM_OK
, data
);
1633 debug(11, 2) ("httpSendRequestEntityDone: matched brokenPosts\n");
1634 comm_old_write(fd
, "\r\n", 2, HttpStateData::SendComplete
, data
, NULL
);
1639 httpRequestBodyHandler(char *buf
, ssize_t size
, void *data
)
1641 HttpStateData
*httpState
= (HttpStateData
*) data
;
1644 comm_old_write(httpState
->fd
, buf
, size
, httpSendRequestEntity
, data
, memFree8K
);
1645 } else if (size
== 0) {
1648 httpSendRequestEntityDone(httpState
->fd
, data
);
1650 /* Failed to get whole body, probably aborted */
1652 HttpStateData::SendComplete(httpState
->fd
, NULL
, 0, COMM_ERR_CLOSING
, data
);
1657 httpSendRequestEntity(int fd
, char *bufnotused
, size_t size
, comm_err_t errflag
, void *data
)
1659 HttpStateData
*httpState
= static_cast<HttpStateData
*>(data
);
1660 StoreEntry
*entry
= httpState
->entry
;
1662 debug(11, 5) ("httpSendRequestEntity: FD %d: size %d: errflag %d.\n",
1663 fd
, (int) size
, errflag
);
1666 fd_bytes(fd
, size
, FD_WRITE
);
1667 kb_incr(&statCounter
.server
.all
.kbytes_out
, size
);
1668 kb_incr(&statCounter
.server
.http
.kbytes_out
, size
);
1671 if (errflag
== COMM_ERR_CLOSING
)
1675 err
= errorCon(ERR_WRITE_ERROR
, HTTP_INTERNAL_SERVER_ERROR
);
1676 err
->xerrno
= errno
;
1677 err
->request
= requestLink(httpState
->orig_request
);
1678 errorAppendEntry(entry
, err
);
1683 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1688 clientReadBody(httpState
->orig_request
, (char *)memAllocate(MEM_8K_BUF
), 8192, httpRequestBodyHandler
, httpState
);
1692 httpBuildVersion(http_version_t
* version
, unsigned int major
, unsigned int minor
)
1694 version
->major
= major
;
1695 version
->minor
= minor
;