3 * $Id: http.cc,v 1.547 2008/02/12 23:55:26 rousskov 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.
42 #include "errorpage.h"
45 #include "AuthUserRequest.h"
47 #include "HttpReply.h"
48 #include "HttpRequest.h"
49 #include "MemObject.h"
50 #include "HttpHdrContRange.h"
51 #include "HttpHdrSc.h"
52 #include "HttpHdrScTarget.h"
53 #include "ACLChecklist.h"
56 #include "DelayPools.h"
58 #include "SquidTime.h"
59 #include "TextException.h"
61 #define SQUID_ENTER_THROWING_CODE() try {
62 #define SQUID_EXIT_THROWING_CODE(status) \
65 catch (const std::exception &e) { \
66 debugs (11, 1, "Exception error:" << e.what()); \
70 CBDATA_CLASS_INIT(HttpStateData
);
72 static const char *const crlf
= "\r\n";
74 static void httpMaybeRemovePublic(StoreEntry
*, http_status
);
75 static void copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, String strConnection
, HttpRequest
* request
, HttpRequest
* orig_request
,
76 HttpHeader
* hdr_out
, int we_do_ranges
, http_state_flags
);
78 HttpStateData::HttpStateData(FwdState
*theFwdState
) : AsyncJob("HttpStateData"), ServerStateData(theFwdState
),
79 lastChunk(0), header_bytes_read(0), reply_bytes_read(0), httpChunkDecoder(NULL
)
81 debugs(11,5,HERE
<< "HttpStateData " << this << " created");
82 ignoreCacheControl
= false;
83 surrogateNoStore
= false;
86 readBuf
->init(4096, SQUID_TCP_SO_RCVBUF
);
87 orig_request
= HTTPMSGLOCK(fwd
->request
);
90 _peer
= fwd
->servers
->_peer
; /* might be NULL */
95 if (_peer
->options
.originserver
)
96 url
= orig_request
->urlpath
.buf();
100 HttpRequest
* proxy_req
= new HttpRequest(orig_request
->method
,
101 orig_request
->protocol
, url
);
103 proxy_req
->SetHost(_peer
->host
);
105 proxy_req
->port
= _peer
->http_port
;
107 proxy_req
->flags
= orig_request
->flags
;
109 proxy_req
->lastmod
= orig_request
->lastmod
;
111 proxy_req
->flags
.proxying
= 1;
113 HTTPMSGUNLOCK(request
);
115 request
= HTTPMSGLOCK(proxy_req
);
118 * This NEIGHBOR_PROXY_ONLY check probably shouldn't be here.
119 * We might end up getting the object from somewhere else if,
120 * for example, the request to this neighbor fails.
122 if (_peer
->options
.proxy_only
)
123 entry
->releaseRequest();
127 entry
->setNoDelay(_peer
->options
.no_delay
);
133 * register the handler to free HTTP state data when the FD closes
135 typedef CommCbMemFunT
<HttpStateData
, CommCloseCbParams
> Dialer
;
136 closeHandler
= asyncCall(9, 5, "httpStateData::httpStateConnClosed",
137 Dialer(this,&HttpStateData::httpStateConnClosed
));
138 comm_add_close_handler(fd
, closeHandler
);
141 HttpStateData::~HttpStateData()
144 * don't forget that ~ServerStateData() gets called automatically
147 if (!readBuf
->isNull())
152 if (httpChunkDecoder
)
153 delete httpChunkDecoder
;
155 HTTPMSGUNLOCK(orig_request
);
157 debugs(11,5, HERE
<< "HttpStateData " << this << " destroyed; FD " << fd
);
161 HttpStateData::dataDescriptor() const
167 httpStateFree(int fd, void *data)
169 HttpStateData *httpState = static_cast<HttpStateData *>(data);
170 debugs(11, 5, "httpStateFree: FD " << fd << ", httpState=" << data);
175 HttpStateData::httpStateConnClosed(const CommCloseCbParams
¶ms
)
177 debugs(11, 5, "httpStateFree: FD " << params
.fd
<< ", httpState=" << params
.data
);
178 deleteThis("HttpStateData::httpStateConnClosed");
182 httpCachable(const HttpRequestMethod
& method
)
184 /* GET and HEAD are cachable. Others are not. */
186 // TODO: replase to HttpRequestMethod::isCachable() ?
187 if (method
!= METHOD_GET
&& method
!= METHOD_HEAD
)
195 HttpStateData::httpTimeout(const CommTimeoutCbParams
¶ms
)
197 debugs(11, 4, "httpTimeout: FD " << fd
<< ": '" << entry
->url() << "'" );
199 if (entry
->store_status
== STORE_PENDING
) {
200 fwd
->fail(errorCon(ERR_READ_TIMEOUT
, HTTP_GATEWAY_TIMEOUT
, fwd
->request
));
207 httpMaybeRemovePublic(StoreEntry
* e
, http_status status
)
213 if (!EBIT_TEST(e
->flags
, KEY_PRIVATE
))
220 case HTTP_NON_AUTHORITATIVE_INFORMATION
:
222 case HTTP_MULTIPLE_CHOICES
:
224 case HTTP_MOVED_PERMANENTLY
:
226 case HTTP_MOVED_TEMPORARILY
:
237 case HTTP_METHOD_NOT_ALLOWED
:
244 case HTTP_UNAUTHORIZED
:
254 * Any 2xx response should eject previously cached entities...
257 if (status
>= 200 && status
< 300)
265 if (!remove
&& !forbidden
)
270 if (e
->mem_obj
->request
)
271 pe
= storeGetPublicByRequest(e
->mem_obj
->request
);
273 pe
= storeGetPublic(e
->mem_obj
->url
, e
->mem_obj
->method
);
278 neighborsHtcpClear(e
, NULL
, e
->mem_obj
->request
, e
->mem_obj
->method
, HTCP_CLR_INVALIDATION
);
284 * Also remove any cached HEAD response in case the object has
287 if (e
->mem_obj
->request
)
288 pe
= storeGetPublicByRequestMethod(e
->mem_obj
->request
, METHOD_HEAD
);
290 pe
= storeGetPublic(e
->mem_obj
->url
, METHOD_HEAD
);
295 neighborsHtcpClear(e
, NULL
, e
->mem_obj
->request
, HttpRequestMethod(METHOD_HEAD
), HTCP_CLR_INVALIDATION
);
302 HttpStateData::processSurrogateControl(HttpReply
*reply
)
306 if (request
->flags
.accelerated
&& reply
->surrogate_control
) {
307 HttpHdrScTarget
*sctusable
=
308 httpHdrScGetMergedTarget(reply
->surrogate_control
,
309 Config
.Accel
.surrogate_id
);
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 entry
->makePrivate();
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
323 if (sctusable
->max_age
!= -1) {
324 if (sctusable
->max_age
< sctusable
->max_stale
)
325 reply
->expires
= reply
->date
+ sctusable
->max_age
;
327 reply
->expires
= reply
->date
+ sctusable
->max_stale
;
329 /* And update the timestamps */
330 entry
->timestampsSet();
333 /* We ignore cache-control directives as per the Surrogate specification */
334 ignoreCacheControl
= true;
336 httpHdrScTargetDestroy(sctusable
);
344 HttpStateData::cacheableReply()
346 HttpReply
const *rep
= finalReply();
347 HttpHeader
const *hdr
= &rep
->header
;
348 const int cc_mask
= (rep
->cache_control
) ? rep
->cache_control
->mask
: 0;
352 const refresh_t
*R
= NULL
;
354 /* This strange looking define first looks up the refresh pattern
355 * and then checks if the specified flag is set. The main purpose
356 * of this is to simplify the refresh pattern lookup and HTTP_VIOLATIONS
359 #define REFRESH_OVERRIDE(flag) \
360 ((R = (R ? R : refreshLimits(entry->mem_obj->url))) , \
361 (R && R->flags.flag))
363 #define REFRESH_OVERRIDE(flag) 0
366 if (surrogateNoStore
)
369 if (!ignoreCacheControl
) {
370 if (EBIT_TEST(cc_mask
, CC_PRIVATE
)) {
371 if (!REFRESH_OVERRIDE(ignore_private
))
375 if (EBIT_TEST(cc_mask
, CC_NO_CACHE
)) {
376 if (!REFRESH_OVERRIDE(ignore_no_cache
))
380 if (EBIT_TEST(cc_mask
, CC_NO_STORE
)) {
381 if (!REFRESH_OVERRIDE(ignore_no_store
))
386 if (request
->flags
.auth
|| request
->flags
.auth_sent
) {
388 * Responses to requests with authorization may be cached
389 * only if a Cache-Control: public reply header is present.
390 * RFC 2068, sec 14.9.4
393 if (!EBIT_TEST(cc_mask
, CC_PUBLIC
)) {
394 if (!REFRESH_OVERRIDE(ignore_auth
))
399 /* Pragma: no-cache in _replies_ is not documented in HTTP,
400 * but servers like "Active Imaging Webcast/2.0" sure do use it */
401 if (hdr
->has(HDR_PRAGMA
)) {
402 String s
= hdr
->getList(HDR_PRAGMA
);
403 const int no_cache
= strListIsMember(&s
, "no-cache", ',');
407 if (!REFRESH_OVERRIDE(ignore_no_cache
))
413 * The "multipart/x-mixed-replace" content type is used for
414 * continuous push replies. These are generally dynamic and
415 * probably should not be cachable
417 if ((v
= hdr
->getStr(HDR_CONTENT_TYPE
)))
418 if (!strncasecmp(v
, "multipart/x-mixed-replace", 25))
421 switch (rep
->sline
.status
) {
422 /* Responses that are cacheable */
426 case HTTP_NON_AUTHORITATIVE_INFORMATION
:
428 case HTTP_MULTIPLE_CHOICES
:
430 case HTTP_MOVED_PERMANENTLY
:
434 * Don't cache objects that need to be refreshed on next request,
435 * unless we know how to refresh it.
438 if (!refreshIsCachable(entry
)) {
439 debugs(22, 3, "refreshIsCachable() returned non-cacheable..");
443 /* don't cache objects from peers w/o LMT, Date, or Expires */
444 /* check that is it enough to check headers @?@ */
447 else if (rep
->last_modified
> -1)
452 /* @?@ (here and 302): invalid expires header compiles to squid_curtime */
453 else if (rep
->expires
> -1)
461 /* Responses that only are cacheable if the server says so */
463 case HTTP_MOVED_TEMPORARILY
:
464 case HTTP_TEMPORARY_REDIRECT
:
465 if (rep
->expires
> rep
->date
&& rep
->date
> 0)
473 /* Errors can be negatively cached */
475 case HTTP_NO_CONTENT
:
479 case HTTP_BAD_REQUEST
:
485 case HTTP_METHOD_NOT_ALLOWED
:
487 case HTTP_REQUEST_URI_TOO_LARGE
:
489 case HTTP_INTERNAL_SERVER_ERROR
:
491 case HTTP_NOT_IMPLEMENTED
:
493 case HTTP_BAD_GATEWAY
:
495 case HTTP_SERVICE_UNAVAILABLE
:
497 case HTTP_GATEWAY_TIMEOUT
:
503 /* Some responses can never be cached */
505 case HTTP_PARTIAL_CONTENT
: /* Not yet supported */
509 case HTTP_NOT_MODIFIED
:
511 case HTTP_UNAUTHORIZED
:
513 case HTTP_PROXY_AUTHENTICATION_REQUIRED
:
515 case HTTP_INVALID_HEADER
: /* Squid header parsing error */
517 case HTTP_HEADER_TOO_LARGE
:
519 case HTTP_PAYMENT_REQUIRED
:
520 case HTTP_NOT_ACCEPTABLE
:
521 case HTTP_REQUEST_TIMEOUT
:
523 case HTTP_LENGTH_REQUIRED
:
524 case HTTP_PRECONDITION_FAILED
:
525 case HTTP_REQUEST_ENTITY_TOO_LARGE
:
526 case HTTP_UNSUPPORTED_MEDIA_TYPE
:
527 case HTTP_UNPROCESSABLE_ENTITY
:
529 case HTTP_FAILED_DEPENDENCY
:
530 case HTTP_INSUFFICIENT_STORAGE
:
531 case HTTP_REQUESTED_RANGE_NOT_SATISFIABLE
:
532 case HTTP_EXPECTATION_FAILED
:
536 default: /* Unknown status code */
537 debugs (11, 0, HERE
<< "HttpStateData::cacheableReply: unexpected http status code " << rep
->sline
.status
);
549 * For Vary, store the relevant request headers as
550 * virtual headers in the reply
551 * Returns false if the variance cannot be stored
554 httpMakeVaryMark(HttpRequest
* request
, HttpReply
const * reply
)
557 const char *pos
= NULL
;
564 vary
= reply
->header
.getList(HDR_VARY
);
566 while (strListGetItem(&vary
, ',', &item
, &ilen
, &pos
)) {
567 char *name
= (char *)xmalloc(ilen
+ 1);
568 xstrncpy(name
, item
, ilen
+ 1);
571 if (strcmp(name
, "*") == 0) {
572 /* Can not handle "Vary: *" withtout ETag support */
578 strListAdd(&vstr
, name
, ',');
579 hdr
= request
->header
.getByName(name
);
584 value
= rfc1738_escape_part(value
);
585 vstr
.append("=\"", 2);
587 vstr
.append("\"", 1);
594 #if X_ACCELERATOR_VARY
597 vary
= reply
->header
.getList(HDR_X_ACCELERATOR_VARY
);
599 while (strListGetItem(&vary
, ',', &item
, &ilen
, &pos
)) {
600 char *name
= (char *)xmalloc(ilen
+ 1);
601 xstrncpy(name
, item
, ilen
+ 1);
603 strListAdd(&vstr
, name
, ',');
604 hdr
= request
->header
.getByName(name
);
609 value
= rfc1738_escape_part(value
);
610 vstr
.append("=\"", 2);
612 vstr
.append("\"", 1);
621 debugs(11, 3, "httpMakeVaryMark: " << vstr
.buf());
626 HttpStateData::keepaliveAccounting(HttpReply
*reply
)
630 _peer
->stats
.n_keepalives_sent
++;
632 if (reply
->keep_alive
) {
634 _peer
->stats
.n_keepalives_recv
++;
636 if (Config
.onoff
.detect_broken_server_pconns
&& reply
->bodySize(request
->method
) == -1) {
637 debugs(11, 1, "keepaliveAccounting: Impossible keep-alive header from '" << entry
->url() << "'" );
638 // debugs(11, 2, "GOT HTTP REPLY HDR:\n---------\n" << readBuf->content() << "\n----------" );
639 flags
.keepalive_broken
= 1;
645 HttpStateData::checkDateSkew(HttpReply
*reply
)
647 if (reply
->date
> -1 && !_peer
) {
648 int skew
= abs((int)(reply
->date
- squid_curtime
));
651 debugs(11, 3, "" << request
->GetHost() << "'s clock is skewed by " << skew
<< " seconds!");
656 * This creates the error page itself.. its likely
657 * that the forward ported reply header max size patch
658 * generates non http conformant error pages - in which
659 * case the errors where should be 'BAD_GATEWAY' etc
662 HttpStateData::processReplyHeader()
664 /** Creates a blank header. If this routine is made incremental, this will not do */
665 Ctx ctx
= ctx_enter(entry
->mem_obj
->url
);
666 debugs(11, 3, "processReplyHeader: key '" << entry
->getMD5Text() << "'");
668 assert(!flags
.headers_parsed
);
670 http_status error
= HTTP_STATUS_NONE
;
672 HttpReply
*newrep
= new HttpReply
;
673 const bool parsed
= newrep
->parse(readBuf
, eof
, &error
);
675 if (!parsed
&& readBuf
->contentSize() > 5 && strncmp(readBuf
->content(), "HTTP/", 5) != 0) {
677 HttpReply
*tmprep
= new HttpReply
;
678 tmprep
->sline
.version
= HttpVersion(1, 0);
679 tmprep
->sline
.status
= HTTP_OK
;
680 tmprep
->header
.putTime(HDR_DATE
, squid_curtime
);
681 tmprep
->header
.putExt("X-Transformed-From", "HTTP/0.9");
683 newrep
->parse(mb
, eof
, &error
);
686 if (!parsed
&& error
> 0) { // unrecoverable parsing error
687 debugs(11, 3, "processReplyHeader: Non-HTTP-compliant header: '" << readBuf
->content() << "'");
688 flags
.headers_parsed
= 1;
689 newrep
->sline
.version
= HttpVersion(1, 0);
690 newrep
->sline
.status
= error
;
691 HttpReply
*vrep
= setVirginReply(newrep
);
692 entry
->replaceHttpReply(vrep
);
697 if (!parsed
) { // need more data
705 debugs(11, 9, "GOT HTTP REPLY HDR:\n---------\n" << readBuf
->content() << "\n----------");
707 header_bytes_read
= headersEnd(readBuf
->content(), readBuf
->contentSize());
708 readBuf
->consume(header_bytes_read
);
712 if (newrep
->header
.hasListMember(HDR_TRANSFER_ENCODING
, "chunked", ',')) {
714 httpChunkDecoder
= new ChunkedCodingParser
;
717 if (!peerSupportsConnectionPinning())
718 orig_request
->flags
.connection_auth_disabled
= 1;
720 HttpReply
*vrep
= setVirginReply(newrep
);
721 flags
.headers_parsed
= 1;
723 keepaliveAccounting(vrep
);
727 processSurrogateControl (vrep
);
729 /** \todo IF the reply is a 1.0 reply, AND it has a Connection: Header
730 * Parse the header and remove all referenced headers
738 * returns true if the peer can support connection pinning
740 bool HttpStateData::peerSupportsConnectionPinning() const
742 const HttpReply
*rep
= entry
->mem_obj
->getReply();
743 const HttpHeader
*hdr
= &rep
->header
;
750 /*If this peer does not support connection pinning (authenticated
751 connections) return false
753 if (!_peer
->connection_auth
)
756 /*The peer supports connection pinning and the http reply status
757 is not unauthorized, so the related connection can be pinned
759 if (rep
->sline
.status
!= HTTP_UNAUTHORIZED
)
762 /*The server respond with HTTP_UNAUTHORIZED and the peer configured
763 with "connection-auth=on" we know that the peer supports pinned
766 if (_peer
->connection_auth
== 1)
769 /*At this point peer has configured with "connection-auth=auto"
770 parameter so we need some extra checks to decide if we are going
771 to allow pinned connections or not
774 /*if the peer configured with originserver just allow connection
775 pinning (squid 2.6 behaviour)
777 if (_peer
->options
.originserver
)
780 /*if the connections it is already pinned it is OK*/
781 if (request
->flags
.pinned
)
784 /*Allow pinned connections only if the Proxy-support header exists in
785 reply and has in its list the "Session-Based-Authentication"
786 which means that the peer supports connection pinning.
788 if (!hdr
->has(HDR_PROXY_SUPPORT
))
791 header
= hdr
->getStrOrList(HDR_PROXY_SUPPORT
);
792 /* XXX This ought to be done in a case-insensitive manner */
793 rc
= (strstr(header
.buf(), "Session-Based-Authentication") != NULL
);
798 // Called when we parsed (and possibly adapted) the headers but
799 // had not starting storing (a.k.a., sending) the body yet.
801 HttpStateData::haveParsedReplyHeaders()
803 ServerStateData::haveParsedReplyHeaders();
805 Ctx ctx
= ctx_enter(entry
->mem_obj
->url
);
806 HttpReply
*rep
= finalReply();
808 if (rep
->sline
.status
== HTTP_PARTIAL_CONTENT
&&
810 currentOffset
= rep
->content_range
->spec
.offset
;
812 entry
->timestampsSet();
814 /* Check if object is cacheable or not based on reply code */
815 debugs(11, 3, "haveParsedReplyHeaders: HTTP CODE: " << rep
->sline
.status
);
817 if (neighbors_do_private_keys
)
818 httpMaybeRemovePublic(entry
, rep
->sline
.status
);
820 if (rep
->header
.has(HDR_VARY
)
821 #if X_ACCELERATOR_VARY
822 || rep
->header
.has(HDR_X_ACCELERATOR_VARY
)
825 const char *vary
= httpMakeVaryMark(orig_request
, rep
);
828 entry
->makePrivate();
833 entry
->mem_obj
->vary_headers
= xstrdup(vary
);
841 * If its not a reply that we will re-forward, then
842 * allow the client to get it.
844 if (!fwd
->reforwardableStatus(rep
->sline
.status
))
845 EBIT_CLR(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
847 switch (cacheableReply()) {
854 entry
->makePrivate();
860 if (Config
.negativeTtl
> 0)
861 entry
->cacheNegatively();
864 entry
->makePrivate();
876 if (!ignoreCacheControl
&& rep
->cache_control
) {
877 if (EBIT_TEST(rep
->cache_control
->mask
, CC_PROXY_REVALIDATE
))
878 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE
);
879 else if (EBIT_TEST(rep
->cache_control
->mask
, CC_MUST_REVALIDATE
))
880 EBIT_SET(entry
->flags
, ENTRY_REVALIDATE
);
884 headersLog(1, 0, request
->method
, rep
);
891 HttpStateData::ConnectionStatus
892 HttpStateData::statusIfComplete() const
894 const HttpReply
*rep
= virginReply();
896 * If the reply wants to close the connection, it takes precedence */
898 if (httpHeaderHasConnDir(&rep
->header
, "close"))
899 return COMPLETE_NONPERSISTENT_MSG
;
902 * If we didn't send a keep-alive request header, then this
903 * can not be a persistent connection.
905 if (!flags
.keepalive
)
906 return COMPLETE_NONPERSISTENT_MSG
;
909 * If we haven't sent the whole request then this can not be a persistent
912 if (!flags
.request_sent
) {
913 debugs(11, 1, "statusIfComplete: Request not yet fully sent \"" << RequestMethodStr(orig_request
->method
) << " " << entry
->url() << "\"" );
914 return COMPLETE_NONPERSISTENT_MSG
;
918 * What does the reply have to say about keep-alive?
922 * If the origin server (HTTP/1.0) does not send a keep-alive
923 * header, but keeps the connection open anyway, what happens?
924 * We'll return here and http.c waits for an EOF before changing
925 * store_status to STORE_OK. Combine this with ENTRY_FWD_HDR_WAIT
926 * and an error status code, and we might have to wait until
927 * the server times out the socket.
929 if (!rep
->keep_alive
)
930 return COMPLETE_NONPERSISTENT_MSG
;
932 return COMPLETE_PERSISTENT_MSG
;
935 HttpStateData::ConnectionStatus
936 HttpStateData::persistentConnStatus() const
938 debugs(11, 3, "persistentConnStatus: FD " << fd
<< " eof=" << eof
);
939 const HttpReply
*vrep
= virginReply();
940 debugs(11, 5, "persistentConnStatus: content_length=" << vrep
->content_length
);
942 /* If we haven't seen the end of reply headers, we are not done */
943 debugs(11, 5, "persistentConnStatus: flags.headers_parsed=" << flags
.headers_parsed
);
945 if (!flags
.headers_parsed
)
946 return INCOMPLETE_MSG
;
948 if (eof
) // already reached EOF
949 return COMPLETE_NONPERSISTENT_MSG
;
951 /* In chunked responce we do not know the content length but we are absolutelly
952 * sure about the end of response, so we are calling the statusIfComplete to
953 * decide if we can be persistant
955 if (lastChunk
&& flags
.chunked
)
956 return statusIfComplete();
958 const int64_t clen
= vrep
->bodySize(request
->method
);
960 debugs(11, 5, "persistentConnStatus: clen=" << clen
);
962 /* If the body size is unknown we must wait for EOF */
964 return INCOMPLETE_MSG
;
966 /* If the body size is known, we must wait until we've gotten all of it. */
969 // if (entry->mem_obj->endOffset() < vrep->content_length + vrep->hdr_sz)
970 const int64_t body_bytes_read
= reply_bytes_read
- header_bytes_read
;
971 debugs(11,5, "persistentConnStatus: body_bytes_read=" <<
972 body_bytes_read
<< " content_length=" << vrep
->content_length
);
974 if (body_bytes_read
< vrep
->content_length
)
975 return INCOMPLETE_MSG
;
978 /* If there is no message body or we got it all, we can be persistent */
979 return statusIfComplete();
983 * This is the callback after some data has been read from the network
987 HttpStateData::ReadReplyWrapper(int fd, char *buf, size_t len, comm_err_t flag, int xerrno, void *data)
989 HttpStateData *httpState = static_cast<HttpStateData *>(data);
990 assert (fd == httpState->fd);
991 // assert(buf == readBuf->content());
992 PROF_start(HttpStateData_readReply);
993 httpState->readReply (len, flag, xerrno);
994 PROF_stop(HttpStateData_readReply);
998 /* XXX this function is too long! */
1000 HttpStateData::readReply (const CommIoCbParams
&io
)
1006 assert(fd
== io
.fd
);
1008 flags
.do_next_read
= 0;
1010 debugs(11, 5, "httpReadReply: FD " << fd
<< ": len " << len
<< ".");
1012 // Bail out early on COMM_ERR_CLOSING - close handlers will tidy up for us
1013 if (io
.flag
== COMM_ERR_CLOSING
) {
1014 debugs(11, 3, "http socket closing");
1018 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1019 maybeReadVirginBody();
1023 // handle I/O errors
1024 if (io
.flag
!= COMM_OK
|| len
< 0) {
1025 debugs(11, 2, "httpReadReply: FD " << fd
<< ": read failure: " << xstrerror() << ".");
1027 if (ignoreErrno(io
.xerrno
)) {
1028 flags
.do_next_read
= 1;
1031 err
= errorCon(ERR_READ_ERROR
, HTTP_BAD_GATEWAY
, fwd
->request
);
1032 err
->xerrno
= io
.xerrno
;
1034 flags
.do_next_read
= 0;
1043 readBuf
->appended(len
);
1044 reply_bytes_read
+= len
;
1047 DelayId delayId
= entry
->mem_obj
->mostBytesAllowed();
1048 delayId
.bytesIn(len
);
1051 kb_incr(&statCounter
.server
.all
.kbytes_in
, len
);
1052 kb_incr(&statCounter
.server
.http
.kbytes_in
, len
);
1053 IOStats
.Http
.reads
++;
1055 for (clen
= len
- 1, bin
= 0; clen
; bin
++)
1058 IOStats
.Http
.read_hist
[bin
]++;
1062 * Here the RFC says we should ignore whitespace between replies, but we can't as
1063 * doing so breaks HTTP/0.9 replies beginning with witespace, and in addition
1064 * the response splitting countermeasures is extremely likely to trigger on this,
1065 * not allowing connection reuse in the first place.
1068 if (!flags
.headers_parsed
&& len
> 0 && fd_table
[fd
].uses
> 1) {
1069 /* Skip whitespace between replies */
1071 while (len
> 0 && xisspace(*buf
))
1072 xmemmove(buf
, buf
+ 1, len
--);
1075 /* Continue to read... */
1076 /* Timeout NOT increased. This whitespace was from previous reply */
1077 flags
.do_next_read
= 1;
1078 maybeReadVirginBody();
1085 if (len
== 0) { // reached EOF?
1087 flags
.do_next_read
= 0;
1090 if (!flags
.headers_parsed
) { // have not parsed headers yet?
1091 PROF_start(HttpStateData_processReplyHeader
);
1092 processReplyHeader();
1093 PROF_stop(HttpStateData_processReplyHeader
);
1095 if (!continueAfterParsingHeader()) // parsing error or need more data
1096 return; // TODO: send errors to ICAP
1098 adaptOrFinalizeReply();
1101 // kick more reads if needed and/or process the response body, if any
1102 PROF_start(HttpStateData_processReplyBody
);
1103 processReplyBody(); // may call serverComplete()
1104 PROF_stop(HttpStateData_processReplyBody
);
1108 \retval true if we can continue with processing the body or doing ICAP.
1111 HttpStateData::continueAfterParsingHeader()
1113 if (!flags
.headers_parsed
&& !eof
) {
1114 debugs(11, 9, HERE
<< "needs more at " << readBuf
->contentSize());
1115 flags
.do_next_read
= 1;
1116 /** \retval false If we have not finished parsing the headers and may get more data.
1117 * Schedules more reads to retrieve the missing data.
1119 maybeReadVirginBody(); // schedules all kinds of reads; TODO: rename
1123 /** If we are done with parsing, check for errors */
1125 err_type error
= ERR_NONE
;
1127 if (flags
.headers_parsed
) { // parsed headers, possibly with errors
1128 // check for header parsing errors
1129 if (HttpReply
*vrep
= virginReply()) {
1130 const http_status s
= vrep
->sline
.status
;
1131 const HttpVersion
&v
= vrep
->sline
.version
;
1132 if (s
== HTTP_INVALID_HEADER
&& v
!= HttpVersion(0,9)) {
1133 error
= ERR_INVALID_RESP
;
1135 if (s
== HTTP_HEADER_TOO_LARGE
) {
1136 fwd
->dontRetry(true);
1137 error
= ERR_TOO_BIG
;
1139 return true; // done parsing, got reply, and no error
1142 // parsed headers but got no reply
1143 error
= ERR_INVALID_RESP
;
1147 error
= readBuf
->hasContent() ?
1148 ERR_INVALID_RESP
: ERR_ZERO_SIZE_OBJECT
;
1151 assert(error
!= ERR_NONE
);
1153 fwd
->fail(errorCon(error
, HTTP_BAD_GATEWAY
, fwd
->request
));
1154 flags
.do_next_read
= 0;
1156 return false; // quit on error
1160 * Call this when there is data from the origin server
1161 * which should be sent to either StoreEntry, or to ICAP...
1164 HttpStateData::writeReplyBody()
1166 const char *data
= readBuf
->content();
1167 int len
= readBuf
->contentSize();
1168 addVirginReplyBody(data
, len
);
1169 readBuf
->consume(len
);
1173 HttpStateData::decodeAndWriteReplyBody()
1175 const char *data
= NULL
;
1177 bool status
= false;
1178 assert(flags
.chunked
);
1179 assert(httpChunkDecoder
);
1180 SQUID_ENTER_THROWING_CODE();
1183 const bool done
= httpChunkDecoder
->parse(readBuf
,&decodedData
);
1184 len
= decodedData
.contentSize();
1185 data
=decodedData
.content();
1186 addVirginReplyBody(data
, len
);
1189 flags
.do_next_read
= 0;
1191 SQUID_EXIT_THROWING_CODE(status
);
1196 * processReplyBody has two purposes:
1197 * 1 - take the reply body data, if any, and put it into either
1198 * the StoreEntry, or give it over to ICAP.
1199 * 2 - see if we made it to the end of the response (persistent
1200 * connections and such)
1203 HttpStateData::processReplyBody()
1205 AsyncCall::Pointer call
;
1206 IPAddress client_addr
;
1207 bool ispinned
= false;
1209 if (!flags
.headers_parsed
) {
1210 flags
.do_next_read
= 1;
1211 maybeReadVirginBody();
1216 if (adaptationAccessCheckPending
)
1222 * At this point the reply headers have been parsed and consumed.
1223 * That means header content has been removed from readBuf and
1224 * it contains only body data.
1226 if (flags
.chunked
) {
1227 if (!decodeAndWriteReplyBody()) {
1228 flags
.do_next_read
= 0;
1235 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
1237 * The above writeReplyBody() call could ABORT this entry,
1238 * in that case, the server FD should already be closed.
1239 * there's nothing for us to do.
1243 switch (persistentConnStatus()) {
1244 case INCOMPLETE_MSG
:
1245 debugs(11, 5, "processReplyBody: INCOMPLETE_MSG");
1246 /* Wait for more data or EOF condition */
1247 if (flags
.keepalive_broken
) {
1249 commSetTimeout(fd
, 10, call
);
1252 commSetTimeout(fd
, Config
.Timeout
.read
, call
);
1255 flags
.do_next_read
= 1;
1258 case COMPLETE_PERSISTENT_MSG
:
1259 debugs(11, 5, "processReplyBody: COMPLETE_PERSISTENT_MSG");
1260 /* yes we have to clear all these! */
1262 commSetTimeout(fd
, -1, call
);
1263 flags
.do_next_read
= 0;
1265 comm_remove_close_handler(fd
, closeHandler
);
1266 closeHandler
= NULL
;
1267 fwd
->unregister(fd
);
1269 if (orig_request
->flags
.spoof_client_ip
)
1270 client_addr
= orig_request
->client_addr
;
1273 if (request
->flags
.pinned
) {
1275 } else if (request
->flags
.connection_auth
&& request
->flags
.auth_sent
) {
1279 if (orig_request
->pinnedConnection() && ispinned
) {
1280 orig_request
->pinnedConnection()->pinConnection(fd
, orig_request
, _peer
,
1281 (request
->flags
.connection_auth
!= 0));
1283 if (_peer
->options
.originserver
)
1284 fwd
->pconnPush(fd
, _peer
->name
, orig_request
->port
, orig_request
->GetHost(), client_addr
);
1286 fwd
->pconnPush(fd
, _peer
->name
, _peer
->http_port
, NULL
, client_addr
);
1288 fwd
->pconnPush(fd
, request
->GetHost(), request
->port
, NULL
, client_addr
);
1296 case COMPLETE_NONPERSISTENT_MSG
:
1297 debugs(11, 5, "processReplyBody: COMPLETE_NONPERSISTENT_MSG");
1302 maybeReadVirginBody();
1306 HttpStateData::maybeReadVirginBody()
1308 int read_sz
= replyBodySpace(readBuf
->spaceSize());
1310 debugs(11,9, HERE
<< (flags
.do_next_read
? "may" : "wont") <<
1311 " read up to " << read_sz
<< " bytes from FD " << fd
);
1314 * why <2? Because delayAwareRead() won't actually read if
1315 * you ask it to read 1 byte. The delayed read request
1316 * just gets re-queued until the client side drains, then
1317 * the I/O thread hangs. Better to not register any read
1318 * handler until we get a notification from someone that
1319 * its okay to read again.
1322 if (flags
.headers_parsed
)
1328 if (flags
.do_next_read
) {
1329 flags
.do_next_read
= 0;
1330 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
1331 entry
->delayAwareRead(fd
, readBuf
->space(read_sz
), read_sz
,
1332 asyncCall(11, 5, "HttpStateData::readReply",
1333 Dialer(this, &HttpStateData::readReply
)));
1338 * This will be called when request write is complete.
1341 HttpStateData::sendComplete(const CommIoCbParams
&io
)
1343 debugs(11, 5, "httpSendComplete: FD " << fd
<< ": size " << io
.size
<< ": errflag " << io
.flag
<< ".");
1344 #if URL_CHECKSUM_DEBUG
1346 entry
->mem_obj
->checkUrlChecksum();
1350 fd_bytes(fd
, io
.size
, FD_WRITE
);
1351 kb_incr(&statCounter
.server
.all
.kbytes_out
, io
.size
);
1352 kb_incr(&statCounter
.server
.http
.kbytes_out
, io
.size
);
1355 if (io
.flag
== COMM_ERR_CLOSING
)
1360 err
= errorCon(ERR_WRITE_ERROR
, HTTP_BAD_GATEWAY
, fwd
->request
);
1361 err
->xerrno
= io
.xerrno
;
1368 * Set the read timeout here because it hasn't been set yet.
1369 * We only set the read timeout after the request has been
1370 * fully written to the server-side. If we start the timeout
1371 * after connection establishment, then we are likely to hit
1372 * the timeout for POST/PUT requests that have very large
1375 typedef CommCbMemFunT
<HttpStateData
, CommTimeoutCbParams
> TimeoutDialer
;
1376 AsyncCall::Pointer timeoutCall
= asyncCall(11, 5, "HttpStateData::httpTimeout",
1377 TimeoutDialer(this,&HttpStateData::httpTimeout
));
1379 commSetTimeout(fd
, Config
.Timeout
.read
, timeoutCall
);
1381 flags
.request_sent
= 1;
1384 // Close the HTTP server connection. Used by serverComplete().
1386 HttpStateData::closeServer()
1388 debugs(11,5, HERE
<< "closing HTTP server FD " << fd
<< " this " << this);
1391 fwd
->unregister(fd
);
1392 comm_remove_close_handler(fd
, closeHandler
);
1393 closeHandler
= NULL
;
1400 HttpStateData::doneWithServer() const
1406 * build request headers and append them to a given MemBuf
1407 * used by buildRequestPrefix()
1408 * note: initialised the HttpHeader, the caller is responsible for Clean()-ing
1411 HttpStateData::httpBuildRequestHeader(HttpRequest
* request
,
1412 HttpRequest
* orig_request
,
1414 HttpHeader
* hdr_out
,
1415 http_state_flags flags
)
1417 /* building buffer for complex strings */
1418 #define BBUF_SZ (MAX_URL+32)
1419 LOCAL_ARRAY(char, bbuf
, BBUF_SZ
);
1420 LOCAL_ARRAY(char, ntoabuf
, MAX_IPSTRLEN
);
1421 const HttpHeader
*hdr_in
= &orig_request
->header
;
1422 const HttpHeaderEntry
*e
= NULL
;
1424 HttpHeaderPos pos
= HttpHeaderInitPos
;
1425 assert (hdr_out
->owner
== hoRequest
);
1426 /* append our IMS header */
1428 if (request
->lastmod
> -1)
1429 hdr_out
->putTime(HDR_IF_MODIFIED_SINCE
, request
->lastmod
);
1431 bool we_do_ranges
= decideIfWeDoRanges (orig_request
);
1433 String
strConnection (hdr_in
->getList(HDR_CONNECTION
));
1435 while ((e
= hdr_in
->getEntry(&pos
)))
1436 copyOneHeaderFromClientsideRequestToUpstreamRequest(e
, strConnection
, request
, orig_request
, hdr_out
, we_do_ranges
, flags
);
1438 /* Abstraction break: We should interpret multipart/byterange responses
1439 * into offset-length data, and this works around our inability to do so.
1441 if (!we_do_ranges
&& orig_request
->multipartRangeRequest()) {
1442 /* don't cache the result */
1443 orig_request
->flags
.cachable
= 0;
1444 /* pretend it's not a range request */
1445 delete orig_request
->range
;
1446 orig_request
->range
= NULL
;
1447 orig_request
->flags
.range
= 0;
1451 if (Config
.onoff
.via
) {
1453 strVia
= hdr_in
->getList(HDR_VIA
);
1454 snprintf(bbuf
, BBUF_SZ
, "%d.%d %s",
1455 orig_request
->http_ver
.major
,
1456 orig_request
->http_ver
.minor
, ThisCache
);
1457 strListAdd(&strVia
, bbuf
, ',');
1458 hdr_out
->putStr(HDR_VIA
, strVia
.buf());
1464 /* Append Surrogate-Capabilities */
1465 String
strSurrogate (hdr_in
->getList(HDR_SURROGATE_CAPABILITY
));
1466 snprintf(bbuf
, BBUF_SZ
, "%s=\"Surrogate/1.0 ESI/1.0\"",
1467 Config
.Accel
.surrogate_id
);
1468 strListAdd(&strSurrogate
, bbuf
, ',');
1469 hdr_out
->putStr(HDR_SURROGATE_CAPABILITY
, strSurrogate
.buf());
1473 strFwd
= hdr_in
->getList(HDR_X_FORWARDED_FOR
);
1475 /** \pre Handle X-Forwarded-For */
1476 if (strcmp(opt_forwarded_for
, "delete") != 0) {
1477 if (strcmp(opt_forwarded_for
, "on") == 0) {
1478 /** If set to ON - append client IP or 'unknown'. */
1479 strFwd
= hdr_in
->getList(HDR_X_FORWARDED_FOR
);
1480 if ( orig_request
->client_addr
.IsNoAddr() )
1481 strListAdd(&strFwd
, "unknown", ',');
1483 strListAdd(&strFwd
, orig_request
->client_addr
.NtoA(ntoabuf
, MAX_IPSTRLEN
), ',');
1484 } else if (strcmp(opt_forwarded_for
, "off") == 0) {
1485 /** If set to OFF - append 'unknown'. */
1486 strFwd
= hdr_in
->getList(HDR_X_FORWARDED_FOR
);
1487 strListAdd(&strFwd
, "unknown", ',');
1488 } else if (strcmp(opt_forwarded_for
, "transparent") == 0) {
1489 /** If set to TRANSPARENT - pass through unchanged. */
1490 strFwd
= hdr_in
->getList(HDR_X_FORWARDED_FOR
);
1491 } else if (strcmp(opt_forwarded_for
, "truncate") == 0) {
1492 /** If set to TRUNCATE - drop existing list and replace with client IP or 'unknown'. */
1493 if ( orig_request
->client_addr
.IsNoAddr() )
1496 strFwd
= orig_request
->client_addr
.NtoA(ntoabuf
, MAX_IPSTRLEN
);
1498 if (strFwd
.size() > 0)
1499 hdr_out
->putStr(HDR_X_FORWARDED_FOR
, strFwd
.buf());
1501 /** If set to DELETE - do not copy through. */
1504 /* append Host if not there already */
1505 if (!hdr_out
->has(HDR_HOST
)) {
1506 if (orig_request
->peer_domain
) {
1507 hdr_out
->putStr(HDR_HOST
, orig_request
->peer_domain
);
1508 } else if (orig_request
->port
== urlDefaultPort(orig_request
->protocol
)) {
1509 /* use port# only if not default */
1510 hdr_out
->putStr(HDR_HOST
, orig_request
->GetHost());
1512 httpHeaderPutStrf(hdr_out
, HDR_HOST
, "%s:%d",
1513 orig_request
->GetHost(),
1514 (int) orig_request
->port
);
1518 /* append Authorization if known in URL, not in header and going direct */
1519 if (!hdr_out
->has(HDR_AUTHORIZATION
)) {
1520 if (!request
->flags
.proxying
&& *request
->login
) {
1521 httpHeaderPutStrf(hdr_out
, HDR_AUTHORIZATION
, "Basic %s",
1522 base64_encode(request
->login
));
1526 /* append Proxy-Authorization if configured for peer, and proxying */
1527 if (request
->flags
.proxying
&& orig_request
->peer_login
&&
1528 !hdr_out
->has(HDR_PROXY_AUTHORIZATION
)) {
1529 if (*orig_request
->peer_login
== '*') {
1530 /* Special mode, to pass the username to the upstream cache */
1532 const char *username
= "-";
1534 if (orig_request
->extacl_user
.size())
1535 username
= orig_request
->extacl_user
.buf();
1536 else if (orig_request
->auth_user_request
)
1537 username
= orig_request
->auth_user_request
->username();
1539 snprintf(loginbuf
, sizeof(loginbuf
), "%s%s", username
, orig_request
->peer_login
+ 1);
1541 httpHeaderPutStrf(hdr_out
, HDR_PROXY_AUTHORIZATION
, "Basic %s",
1542 base64_encode(loginbuf
));
1543 } else if (strcmp(orig_request
->peer_login
, "PASS") == 0) {
1544 if (orig_request
->extacl_user
.size() && orig_request
->extacl_passwd
.size()) {
1546 snprintf(loginbuf
, sizeof(loginbuf
), "%s:%s", orig_request
->extacl_user
.buf(), orig_request
->extacl_passwd
.buf());
1547 httpHeaderPutStrf(hdr_out
, HDR_PROXY_AUTHORIZATION
, "Basic %s",
1548 base64_encode(loginbuf
));
1550 } else if (strcmp(orig_request
->peer_login
, "PROXYPASS") == 0) {
1553 httpHeaderPutStrf(hdr_out
, HDR_PROXY_AUTHORIZATION
, "Basic %s",
1554 base64_encode(orig_request
->peer_login
));
1558 /* append WWW-Authorization if configured for peer */
1559 if (flags
.originpeer
&& orig_request
->peer_login
&&
1560 !hdr_out
->has(HDR_AUTHORIZATION
)) {
1561 if (strcmp(orig_request
->peer_login
, "PASS") == 0) {
1562 /* No credentials to forward.. (should have been done above if available) */
1563 } else if (strcmp(orig_request
->peer_login
, "PROXYPASS") == 0) {
1564 /* Special mode, convert proxy authentication to WWW authentication
1565 * (also applies to authentication provided by external acl)
1567 const char *auth
= hdr_in
->getStr(HDR_PROXY_AUTHORIZATION
);
1569 if (auth
&& strncasecmp(auth
, "basic ", 6) == 0) {
1570 hdr_out
->putStr(HDR_AUTHORIZATION
, auth
);
1571 } else if (orig_request
->extacl_user
.size() && orig_request
->extacl_passwd
.size()) {
1573 snprintf(loginbuf
, sizeof(loginbuf
), "%s:%s", orig_request
->extacl_user
.buf(), orig_request
->extacl_passwd
.buf());
1574 httpHeaderPutStrf(hdr_out
, HDR_AUTHORIZATION
, "Basic %s",
1575 base64_encode(loginbuf
));
1577 } else if (*orig_request
->peer_login
== '*') {
1578 /* Special mode, to pass the username to the upstream cache */
1580 const char *username
= "-";
1582 if (orig_request
->auth_user_request
)
1583 username
= orig_request
->auth_user_request
->username();
1584 else if (orig_request
->extacl_user
.size())
1585 username
= orig_request
->extacl_user
.buf();
1587 snprintf(loginbuf
, sizeof(loginbuf
), "%s%s", username
, orig_request
->peer_login
+ 1);
1589 httpHeaderPutStrf(hdr_out
, HDR_AUTHORIZATION
, "Basic %s",
1590 base64_encode(loginbuf
));
1592 /* Fixed login string */
1593 httpHeaderPutStrf(hdr_out
, HDR_AUTHORIZATION
, "Basic %s",
1594 base64_encode(orig_request
->peer_login
));
1598 /* append Cache-Control, add max-age if not there already */ {
1599 HttpHdrCc
*cc
= hdr_in
->getCc();
1602 cc
= httpHdrCcCreate();
1604 if (!EBIT_TEST(cc
->mask
, CC_MAX_AGE
)) {
1606 entry
? entry
->url() : urlCanonical(orig_request
);
1607 httpHdrCcSetMaxAge(cc
, getMaxAge(url
));
1609 if (request
->urlpath
.size())
1610 assert(strstr(url
, request
->urlpath
.buf()));
1613 /* Set no-cache if determined needed but not found */
1614 if (orig_request
->flags
.nocache
&& !hdr_in
->has(HDR_PRAGMA
))
1615 EBIT_SET(cc
->mask
, CC_NO_CACHE
);
1617 /* Enforce sibling relations */
1618 if (flags
.only_if_cached
)
1619 EBIT_SET(cc
->mask
, CC_ONLY_IF_CACHED
);
1623 httpHdrCcDestroy(cc
);
1626 /* maybe append Connection: keep-alive */
1627 if (flags
.keepalive
) {
1628 if (flags
.proxying
) {
1629 hdr_out
->putStr(HDR_PROXY_CONNECTION
, "keep-alive");
1631 hdr_out
->putStr(HDR_CONNECTION
, "keep-alive");
1635 /* append Front-End-Https */
1636 if (flags
.front_end_https
) {
1637 if (flags
.front_end_https
== 1 || request
->protocol
== PROTO_HTTPS
)
1638 hdr_out
->putStr(HDR_FRONT_END_HTTPS
, "On");
1641 /* Now mangle the headers. */
1642 if (Config2
.onoff
.mangle_request_headers
)
1643 httpHdrMangleList(hdr_out
, request
, ROR_REQUEST
);
1645 strConnection
.clean();
1649 copyOneHeaderFromClientsideRequestToUpstreamRequest(const HttpHeaderEntry
*e
, String strConnection
, HttpRequest
* request
, HttpRequest
* orig_request
, HttpHeader
* hdr_out
, int we_do_ranges
, http_state_flags flags
)
1651 debugs(11, 5, "httpBuildRequestHeader: " << e
->name
.buf() << ": " << e
->value
.buf());
1653 if (!httpRequestHdrAllowed(e
, &strConnection
)) {
1654 debugs(11, 2, "'" << e
->name
.buf() << "' header denied by anonymize_headers configuration");
1660 case HDR_PROXY_AUTHORIZATION
:
1661 /* Only pass on proxy authentication to peers for which
1662 * authentication forwarding is explicitly enabled
1665 if (flags
.proxying
&& orig_request
->peer_login
&&
1666 (strcmp(orig_request
->peer_login
, "PASS") == 0 ||
1667 strcmp(orig_request
->peer_login
, "PROXYPASS") == 0)) {
1668 hdr_out
->addEntry(e
->clone());
1673 case HDR_AUTHORIZATION
:
1674 /* Pass on WWW authentication */
1676 if (!flags
.originpeer
) {
1677 hdr_out
->addEntry(e
->clone());
1679 /* In accelerators, only forward authentication if enabled
1680 * (see also below for proxy->server authentication)
1683 if (orig_request
->peer_login
&&
1684 (strcmp(orig_request
->peer_login
, "PASS") == 0 ||
1685 strcmp(orig_request
->peer_login
, "PROXYPASS") == 0)) {
1686 hdr_out
->addEntry(e
->clone());
1694 * Normally Squid rewrites the Host: header.
1695 * However, there is one case when we don't: If the URL
1696 * went through our redirector and the admin configured
1697 * 'redir_rewrites_host' to be off.
1699 if (orig_request
->peer_domain
)
1700 hdr_out
->putStr(HDR_HOST
, orig_request
->peer_domain
);
1701 else if (request
->flags
.redirected
&& !Config
.onoff
.redir_rewrites_host
)
1702 hdr_out
->addEntry(e
->clone());
1704 /* use port# only if not default */
1706 if (orig_request
->port
== urlDefaultPort(orig_request
->protocol
)) {
1707 hdr_out
->putStr(HDR_HOST
, orig_request
->GetHost());
1709 httpHeaderPutStrf(hdr_out
, HDR_HOST
, "%s:%d",
1710 orig_request
->GetHost(),
1711 (int) orig_request
->port
);
1717 case HDR_IF_MODIFIED_SINCE
:
1718 /* append unless we added our own;
1719 * note: at most one client's ims header can pass through */
1721 if (!hdr_out
->has(HDR_IF_MODIFIED_SINCE
))
1722 hdr_out
->addEntry(e
->clone());
1726 case HDR_MAX_FORWARDS
:
1727 if (orig_request
->method
== METHOD_TRACE
) {
1728 const int hops
= e
->getInt();
1731 hdr_out
->putInt(HDR_MAX_FORWARDS
, hops
- 1);
1737 /* If Via is disabled then forward any received header as-is */
1739 if (!Config
.onoff
.via
)
1740 hdr_out
->addEntry(e
->clone());
1748 case HDR_REQUEST_RANGE
:
1750 hdr_out
->addEntry(e
->clone());
1754 case HDR_PROXY_CONNECTION
:
1756 case HDR_CONNECTION
:
1758 case HDR_X_FORWARDED_FOR
:
1760 case HDR_CACHE_CONTROL
:
1761 /* append these after the loop if needed */
1764 case HDR_FRONT_END_HTTPS
:
1765 if (!flags
.front_end_https
)
1766 hdr_out
->addEntry(e
->clone());
1771 /* pass on all other header fields */
1772 hdr_out
->addEntry(e
->clone());
1777 HttpStateData::decideIfWeDoRanges (HttpRequest
* orig_request
)
1780 /* decide if we want to do Ranges ourselves
1781 * and fetch the whole object now)
1782 * We want to handle Ranges ourselves iff
1783 * - we can actually parse client Range specs
1784 * - the specs are expected to be simple enough (e.g. no out-of-order ranges)
1785 * - reply will be cachable
1786 * (If the reply will be uncachable we have to throw it away after
1787 * serving this request, so it is better to forward ranges to
1788 * the server and fetch only the requested content)
1791 if (NULL
== orig_request
->range
|| !orig_request
->flags
.cachable
1792 || orig_request
->range
->offsetLimitExceeded() || orig_request
->flags
.connection_auth
)
1795 debugs(11, 8, "decideIfWeDoRanges: range specs: " <<
1796 orig_request
->range
<< ", cachable: " <<
1797 orig_request
->flags
.cachable
<< "; we_do_ranges: " << result
);
1802 /* build request prefix and append it to a given MemBuf;
1803 * return the length of the prefix */
1805 HttpStateData::buildRequestPrefix(HttpRequest
* request
,
1806 HttpRequest
* orig_request
,
1809 http_state_flags flags
)
1811 const int offset
= mb
->size
;
1812 HttpVersion
httpver(1, 0);
1813 mb
->Printf("%s %s HTTP/%d.%d\r\n",
1814 RequestMethodStr(request
->method
),
1815 request
->urlpath
.size() ? request
->urlpath
.buf() : "/",
1816 httpver
.major
,httpver
.minor
);
1817 /* build and pack headers */
1819 HttpHeader
hdr(hoRequest
);
1821 httpBuildRequestHeader(request
, orig_request
, entry
, &hdr
, flags
);
1823 if (request
->flags
.pinned
&& request
->flags
.connection_auth
)
1824 request
->flags
.auth_sent
= 1;
1825 else if (hdr
.has(HDR_AUTHORIZATION
))
1826 request
->flags
.auth_sent
= 1;
1828 packerToMemInit(&p
, mb
);
1833 /* append header terminator */
1834 mb
->append(crlf
, 2);
1835 return mb
->size
- offset
;
1838 /* This will be called when connect completes. Write request. */
1840 HttpStateData::sendRequest()
1844 debugs(11, 5, "httpSendRequest: FD " << fd
<< ", request " << request
<< ", this " << this << ".");
1845 typedef CommCbMemFunT
<HttpStateData
, CommTimeoutCbParams
> TimeoutDialer
;
1846 AsyncCall::Pointer timeoutCall
= asyncCall(11, 5, "HttpStateData::httpTimeout",
1847 TimeoutDialer(this,&HttpStateData::httpTimeout
));
1848 commSetTimeout(fd
, Config
.Timeout
.lifetime
, timeoutCall
);
1849 flags
.do_next_read
= 1;
1850 maybeReadVirginBody();
1852 if (orig_request
->body_pipe
!= NULL
) {
1853 if (!startRequestBodyFlow()) // register to receive body data
1855 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
1856 Dialer
dialer(this, &HttpStateData::sentRequestBody
);
1857 requestSender
= asyncCall(11,5, "HttpStateData::sentRequestBody", dialer
);
1859 assert(!requestBodySource
);
1860 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
1861 Dialer
dialer(this, &HttpStateData::sendComplete
);
1862 requestSender
= asyncCall(11,5, "HttpStateData::SendComplete", dialer
);
1865 if (_peer
!= NULL
) {
1866 if (_peer
->options
.originserver
) {
1868 flags
.originpeer
= 1;
1871 flags
.originpeer
= 0;
1875 flags
.originpeer
= 0;
1879 * Is keep-alive okay for all request methods?
1881 if (orig_request
->flags
.must_keepalive
)
1882 flags
.keepalive
= 1;
1883 else if (!Config
.onoff
.server_pconns
)
1884 flags
.keepalive
= 0;
1885 else if (_peer
== NULL
)
1886 flags
.keepalive
= 1;
1887 else if (_peer
->stats
.n_keepalives_sent
< 10)
1888 flags
.keepalive
= 1;
1889 else if ((double) _peer
->stats
.n_keepalives_recv
/
1890 (double) _peer
->stats
.n_keepalives_sent
> 0.50)
1891 flags
.keepalive
= 1;
1894 if (neighborType(_peer
, request
) == PEER_SIBLING
&&
1895 !_peer
->options
.allow_miss
)
1896 flags
.only_if_cached
= 1;
1898 flags
.front_end_https
= _peer
->front_end_https
;
1902 buildRequestPrefix(request
, orig_request
, entry
, &mb
, flags
);
1903 debugs(11, 6, "httpSendRequest: FD " << fd
<< ":\n" << mb
.buf
);
1904 comm_write_mbuf(fd
, &mb
, requestSender
);
1910 httpStart(FwdState
*fwd
)
1912 debugs(11, 3, "httpStart: \"" << RequestMethodStr(fwd
->request
->method
) << " " << fwd
->entry
->url() << "\"" );
1913 HttpStateData
*httpState
= new HttpStateData(fwd
);
1915 if (!httpState
->sendRequest()) {
1916 debugs(11, 3, "httpStart: aborted");
1921 statCounter
.server
.all
.requests
++;
1922 statCounter
.server
.http
.requests
++;
1925 * We used to set the read timeout here, but not any more.
1926 * Now its set in httpSendComplete() after the full request,
1927 * including request body, has been written to the server.
1932 HttpStateData::doneSendingRequestBody()
1935 debugs(11,5, HERE
<< "doneSendingRequestBody: FD " << fd
);
1936 ch
.request
= HTTPMSGLOCK(request
);
1938 if (Config
.accessList
.brokenPosts
)
1939 ch
.accessList
= cbdataReference(Config
.accessList
.brokenPosts
);
1941 /* cbdataReferenceDone() happens in either fastCheck() or ~ACLCheckList */
1943 if (!Config
.accessList
.brokenPosts
) {
1944 debugs(11, 5, "doneSendingRequestBody: No brokenPosts list");
1945 CommIoCbParams
io(NULL
);
1949 } else if (!ch
.fastCheck()) {
1950 debugs(11, 5, "doneSendingRequestBody: didn't match brokenPosts");
1951 CommIoCbParams
io(NULL
);
1956 debugs(11, 2, "doneSendingRequestBody: matched brokenPosts");
1957 typedef CommCbMemFunT
<HttpStateData
, CommIoCbParams
> Dialer
;
1958 Dialer
dialer(this, &HttpStateData::sendComplete
);
1959 AsyncCall::Pointer call
= asyncCall(11,5, "HttpStateData::SendComplete", dialer
);
1960 comm_write(fd
, "\r\n", 2, call
);
1964 // more origin request body data is available
1966 HttpStateData::handleMoreRequestBodyAvailable()
1968 if (eof
|| fd
< 0) {
1969 // XXX: we should check this condition in other callbacks then!
1970 // TODO: Check whether this can actually happen: We should unsubscribe
1971 // as a body consumer when the above condition(s) are detected.
1972 debugs(11, 1, HERE
<< "Transaction aborted while reading HTTP body");
1976 assert(requestBodySource
!= NULL
);
1978 if (requestBodySource
->buf().hasContent()) {
1979 // XXX: why does not this trigger a debug message on every request?
1981 if (flags
.headers_parsed
&& !flags
.abuse_detected
) {
1982 flags
.abuse_detected
= 1;
1983 debugs(11, 1, "http handleMoreRequestBodyAvailable: Likely proxy abuse detected '" << orig_request
->client_addr
<< "' -> '" << entry
->url() << "'" );
1985 if (virginReply()->sline
.status
== HTTP_INVALID_HEADER
) {
1992 HttpStateData::handleMoreRequestBodyAvailable();
1995 // premature end of the request body
1997 HttpStateData::handleRequestBodyProducerAborted()
1999 ServerStateData::handleRequestBodyProducerAborted();
2000 // XXX: SendComplete(COMM_ERR_CLOSING) does little. Is it enough?
2001 CommIoCbParams
io(NULL
);
2003 io
.flag
=COMM_ERR_CLOSING
;
2007 // called when we wrote request headers(!) or a part of the body
2009 HttpStateData::sentRequestBody(const CommIoCbParams
&io
)
2012 kb_incr(&statCounter
.server
.http
.kbytes_out
, io
.size
);
2014 ServerStateData::sentRequestBody(io
);
2017 // Quickly abort the transaction
2018 // TODO: destruction should be sufficient as the destructor should cleanup,
2019 // including canceling close handlers
2021 HttpStateData::abortTransaction(const char *reason
)
2023 debugs(11,5, HERE
<< "aborting transaction for " << reason
<<
2024 "; FD " << fd
<< ", this " << this);
2031 fwd
->handleUnregisteredServerEnd();
2032 deleteThis("HttpStateData::abortTransaction");
2037 httpBuildVersion(HttpVersion
* version
, unsigned int major
, unsigned int minor
)
2039 version
->major
= major
;
2040 version
->minor
= minor
;
2045 HttpStateData::originalRequest()
2047 return orig_request
;