2 * DEBUG: section 17 Request Forwarding
3 * AUTHOR: Duane Wessels
5 * SQUID Web Proxy Cache http://www.squid-cache.org/
6 * ----------------------------------------------------------
8 * Squid is the result of efforts by numerous individuals from
9 * the Internet community; see the CONTRIBUTORS file for full
10 * details. Many organizations have provided support for Squid's
11 * development; see the SPONSORS file for full details. Squid is
12 * Copyrighted (C) 2001 by the Regents of the University of
13 * California; see the COPYRIGHT file for full details. Squid
14 * incorporates software developed and/or copyrighted by other
15 * sources; see the CREDITS file for full details.
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
34 #include "AccessLogEntry.h"
35 #include "acl/AclAddress.h"
36 #include "acl/FilledChecklist.h"
37 #include "acl/Gadgets.h"
38 #include "anyp/PortCfg.h"
39 #include "CacheManager.h"
40 #include "CachePeer.h"
41 #include "client_side.h"
42 #include "comm/Connection.h"
43 #include "comm/ConnOpener.h"
44 #include "comm/Loops.h"
45 #include "CommCalls.h"
46 #include "errorpage.h"
54 #include "hier_code.h"
56 #include "HttpReply.h"
57 #include "HttpRequest.h"
58 #include "icmp/net_db.h"
60 #include "ip/Intercept.h"
61 #include "ip/QosConfig.h"
63 #include "MemObject.h"
64 #include "mgr/Registration.h"
65 #include "neighbors.h"
67 #include "PeerSelectState.h"
68 #include "SquidConfig.h"
69 #include "SquidTime.h"
71 #include "StoreClient.h"
75 #include "ssl/cert_validate_message.h"
76 #include "ssl/Config.h"
77 #include "ssl/ErrorDetail.h"
78 #include "ssl/helper.h"
79 #include "ssl/PeerConnector.h"
80 #include "ssl/ServerBump.h"
81 #include "ssl/support.h"
87 static PSC fwdPeerSelectionCompleteWrapper
;
88 static CLCB fwdServerClosedWrapper
;
89 static CNCB fwdConnectDoneWrapper
;
93 #define MAX_FWD_STATS_IDX 9
94 static int FwdReplyCodes
[MAX_FWD_STATS_IDX
+ 1][Http::scInvalidHeader
+ 1];
96 static PconnPool
*fwdPconnPool
= new PconnPool("server-side");
97 CBDATA_CLASS_INIT(FwdState
);
100 class FwdStatePeerAnswerDialer
: public CallDialer
, public Ssl::PeerConnector::CbDialer
103 typedef void (FwdState::*Method
)(Ssl::PeerConnectorAnswer
&);
105 FwdStatePeerAnswerDialer(Method method
, FwdState
*fwd
):
106 method_(method
), fwd_(fwd
), answer_() {}
109 virtual bool canDial(AsyncCall
&call
) { return fwd_
.valid(); }
110 void dial(AsyncCall
&call
) { ((&(*fwd_
))->*method_
)(answer_
); }
111 virtual void print(std::ostream
&os
) const {
112 os
<< '(' << fwd_
.get() << ", " << answer_
<< ')'; }
114 /* Ssl::PeerConnector::CbDialer API */
115 virtual Ssl::PeerConnectorAnswer
&answer() { return answer_
; }
119 CbcPointer
<FwdState
> fwd_
;
120 Ssl::PeerConnectorAnswer answer_
;
126 FwdState::abort(void* d
)
128 FwdState
* fwd
= (FwdState
*)d
;
129 Pointer tmp
= fwd
; // Grab a temporary pointer to keep the object alive during our scope.
131 if (Comm::IsConnOpen(fwd
->serverConnection())) {
132 comm_remove_close_handler(fwd
->serverConnection()->fd
, fwdServerClosedWrapper
, fwd
);
133 debugs(17, 3, HERE
<< "store entry aborted; closing " <<
134 fwd
->serverConnection());
135 fwd
->serverConnection()->close();
137 debugs(17, 7, HERE
<< "store entry aborted; no connection to close");
139 fwd
->serverDestinations
.clear();
143 /**** PUBLIC INTERFACE ********************************************************/
145 FwdState::FwdState(const Comm::ConnectionPointer
&client
, StoreEntry
* e
, HttpRequest
* r
, const AccessLogEntryPointer
&alp
):
148 debugs(17, 2, HERE
<< "Forwarding client request " << client
<< ", url=" << e
->url() );
152 HTTPMSGLOCK(request
);
153 pconnRace
= raceImpossible
;
154 start_t
= squid_curtime
;
155 serverDestinations
.reserve(Config
.forward_max_tries
);
157 EBIT_SET(e
->flags
, ENTRY_FWD_HDR_WAIT
);
160 // Called once, right after object creation, when it is safe to set self
161 void FwdState::start(Pointer aSelf
)
163 // Protect ourselves from being destroyed when the only Server pointing
164 // to us is gone (while we expect to talk to more Servers later).
165 // Once we set self, we are responsible for clearing it when we do not
166 // expect to talk to any servers.
167 self
= aSelf
; // refcounted
169 // We hope that either the store entry aborts or peer is selected.
170 // Otherwise we are going to leak our object.
172 entry
->registerAbort(FwdState::abort
, this);
174 #if STRICT_ORIGINAL_DST
175 // Bug 3243: CVE 2009-0801
176 // Bypass of browser same-origin access control in intercepted communication
177 // To resolve this we must force DIRECT and only to the original client destination.
178 const bool isIntercepted
= request
&& !request
->flags
.redirected
&& (request
->flags
.intercepted
|| request
->flags
.interceptTproxy
);
179 const bool useOriginalDst
= Config
.onoff
.client_dst_passthru
|| (request
&& !request
->flags
.hostVerified
);
180 if (isIntercepted
&& useOriginalDst
) {
181 selectPeerForIntercepted();
182 // 3.2 does not suppro re-wrapping inside CONNECT.
183 // our only alternative is to fake destination "found" and continue with the forwarding.
184 startConnectionOrFail();
189 // do full route options selection
190 peerSelect(&serverDestinations
, request
, al
, entry
, fwdPeerSelectionCompleteWrapper
, this);
193 #if STRICT_ORIGINAL_DST
194 /// bypasses peerSelect() when dealing with intercepted requests
196 FwdState::selectPeerForIntercepted()
198 // use pinned connection if available
199 Comm::ConnectionPointer p
;
200 if (ConnStateData
*client
= request
->pinnedConnection()) {
201 p
= client
->validatePinnedConnection(request
, NULL
);
202 if (Comm::IsConnOpen(p
)) {
203 /* duplicate peerSelectPinned() effects */
204 p
->peerType
= PINNED
;
205 entry
->ping_status
= PING_DONE
; /* Skip ICP */
207 debugs(17, 3, "reusing a pinned conn: " << *p
);
208 serverDestinations
.push_back(p
);
210 debugs(17,2, "Pinned connection is not valid: " << p
);
211 ErrorState
*anErr
= new ErrorState(ERR_ZERO_SIZE_OBJECT
, Http::scServiceUnavailable
, request
);
214 // Either use the valid pinned connection or fail if it is invalid.
218 // use client original destination as second preferred choice
219 p
= new Comm::Connection();
220 p
->peerType
= ORIGINAL_DST
;
221 p
->remote
= clientConn
->local
;
222 getOutgoingAddress(request
, p
);
224 debugs(17, 3, HERE
<< "using client original destination: " << *p
);
225 serverDestinations
.push_back(p
);
230 FwdState::completed()
232 if (flags
.forward_completed
) {
233 debugs(17, DBG_IMPORTANT
, HERE
<< "FwdState::completed called on a completed request! Bad!");
237 flags
.forward_completed
= true;
239 if (EBIT_TEST(entry
->flags
, ENTRY_ABORTED
)) {
240 debugs(17, 3, HERE
<< "entry aborted");
244 #if URL_CHECKSUM_DEBUG
246 entry
->mem_obj
->checkUrlChecksum();
249 if (entry
->store_status
== STORE_PENDING
) {
250 if (entry
->isEmpty()) {
251 if (!err
) // we quit (e.g., fd closed) before an error or content
252 fail(new ErrorState(ERR_READ_ERROR
, Http::scBadGateway
, request
));
254 errorAppendEntry(entry
, err
);
257 if (request
->flags
.sslPeek
&& request
->clientConnectionManager
.valid()) {
258 CallJobHere1(17, 4, request
->clientConnectionManager
, ConnStateData
,
259 ConnStateData::httpsPeeked
, Comm::ConnectionPointer(NULL
));
263 EBIT_CLR(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
265 entry
->releaseRequest();
269 if (storePendingNClients(entry
) > 0)
270 assert(!EBIT_TEST(entry
->flags
, ENTRY_FWD_HDR_WAIT
));
274 FwdState::~FwdState()
276 debugs(17, 3, HERE
<< "FwdState destructor starting");
278 if (! flags
.forward_completed
)
283 HTTPMSGUNLOCK(request
);
287 entry
->unregisterAbort();
289 entry
->unlock("FwdState");
293 if (calls
.connector
!= NULL
) {
294 calls
.connector
->cancel("FwdState destructed");
295 calls
.connector
= NULL
;
298 if (Comm::IsConnOpen(serverConn
)) {
299 comm_remove_close_handler(serverConnection()->fd
, fwdServerClosedWrapper
, this);
300 debugs(17, 3, HERE
<< "closing FD " << serverConnection()->fd
);
304 serverDestinations
.clear();
306 debugs(17, 3, HERE
<< "FwdState destructor done");
310 * This is the entry point for client-side to start forwarding
311 * a transaction. It is a static method that may or may not
312 * allocate a FwdState.
315 FwdState::Start(const Comm::ConnectionPointer
&clientConn
, StoreEntry
*entry
, HttpRequest
*request
, const AccessLogEntryPointer
&al
)
318 * client_addr == no_addr indicates this is an "internal" request
319 * from peer_digest.c, asn.c, netdb.c, etc and should always
320 * be allowed. yuck, I know.
323 if ( Config
.accessList
.miss
&& !request
->client_addr
.isNoAddr() &&
324 request
->protocol
!= AnyP::PROTO_INTERNAL
&& request
->protocol
!= AnyP::PROTO_CACHE_OBJECT
) {
326 * Check if this host is allowed to fetch MISSES from us (miss_access).
327 * Intentionally replace the src_addr automatically selected by the checklist code
328 * we do NOT want the indirect client address to be tested here.
330 ACLFilledChecklist
ch(Config
.accessList
.miss
, request
, NULL
);
331 ch
.src_addr
= request
->client_addr
;
332 if (ch
.fastCheck() == ACCESS_DENIED
) {
334 page_id
= aclGetDenyInfoPage(&Config
.denyInfoList
, AclMatchedName
, 1);
336 if (page_id
== ERR_NONE
)
337 page_id
= ERR_FORWARDING_DENIED
;
339 ErrorState
*anErr
= new ErrorState(page_id
, Http::scForbidden
, request
);
340 errorAppendEntry(entry
, anErr
); // frees anErr
345 debugs(17, 3, HERE
<< "'" << entry
->url() << "'");
347 * This seems like an odd place to bind mem_obj and request.
348 * Might want to assert that request is NULL at this point
350 entry
->mem_obj
->request
= request
;
351 HTTPMSGLOCK(entry
->mem_obj
->request
);
352 #if URL_CHECKSUM_DEBUG
354 entry
->mem_obj
->checkUrlChecksum();
359 ErrorState
*anErr
= new ErrorState(ERR_SHUTTING_DOWN
, Http::scServiceUnavailable
, request
);
360 errorAppendEntry(entry
, anErr
); // frees anErr
364 switch (request
->protocol
) {
366 case AnyP::PROTO_INTERNAL
:
367 internalStart(clientConn
, request
, entry
);
370 case AnyP::PROTO_CACHE_OBJECT
:
371 CacheManager::GetInstance()->Start(clientConn
, request
, entry
);
374 case AnyP::PROTO_URN
:
375 urnStart(request
, entry
);
379 FwdState::Pointer fwd
= new FwdState(clientConn
, entry
, request
, al
);
388 FwdState::fwdStart(const Comm::ConnectionPointer
&clientConn
, StoreEntry
*entry
, HttpRequest
*request
)
390 // Hides AccessLogEntry.h from code that does not supply ALE anyway.
391 Start(clientConn
, entry
, request
, NULL
);
395 FwdState::startConnectionOrFail()
397 debugs(17, 3, HERE
<< entry
->url());
399 if (serverDestinations
.size() > 0) {
400 // Ditch error page if it was created before.
401 // A new one will be created if there's another problem
405 // Update the logging information about this new server connection.
406 // Done here before anything else so the errors get logged for
407 // this server link regardless of what happens when connecting to it.
408 // IF sucessfuly connected this top destination will become the serverConnection().
409 request
->hier
.note(serverDestinations
[0], request
->GetHost());
410 request
->clearError();
414 debugs(17, 3, HERE
<< "Connection failed: " << entry
->url());
416 ErrorState
*anErr
= new ErrorState(ERR_CANNOT_FORWARD
, Http::scInternalServerError
, request
);
418 } // else use actual error from last connection attempt
419 self
= NULL
; // refcounted
424 FwdState::fail(ErrorState
* errorState
)
426 debugs(17, 3, err_type_str
[errorState
->type
] << " \"" << Http::StatusCodeString(errorState
->httpStatus
) << "\"\n\t" << entry
->url());
431 if (!errorState
->request
) {
432 errorState
->request
= request
;
433 HTTPMSGLOCK(errorState
->request
);
436 if (err
->type
!= ERR_ZERO_SIZE_OBJECT
)
439 if (pconnRace
== racePossible
) {
440 debugs(17, 5, HERE
<< "pconn race happened");
441 pconnRace
= raceHappened
;
444 if (ConnStateData
*pinned_connection
= request
->pinnedConnection()) {
445 pinned_connection
->pinning
.zeroReply
= true;
446 flags
.dont_retry
= true; // we want to propagate failure to the client
447 debugs(17, 4, "zero reply on pinned connection");
452 * Frees fwdState without closing FD or generating an abort
455 FwdState::unregister(Comm::ConnectionPointer
&conn
)
457 debugs(17, 3, HERE
<< entry
->url() );
458 assert(serverConnection() == conn
);
459 assert(Comm::IsConnOpen(conn
));
460 comm_remove_close_handler(conn
->fd
, fwdServerClosedWrapper
, this);
464 // Legacy method to be removed in favor of the above as soon as possible
466 FwdState::unregister(int fd
)
468 debugs(17, 3, HERE
<< entry
->url() );
469 assert(fd
== serverConnection()->fd
);
470 unregister(serverConn
);
474 * server-side modules call fwdComplete() when they are done
475 * downloading an object. Then, we either 1) re-forward the
476 * request somewhere else if needed, or 2) call storeComplete()
482 debugs(17, 3, HERE
<< entry
->url() << "\n\tstatus " << entry
->getReply()->sline
.status());
483 #if URL_CHECKSUM_DEBUG
485 entry
->mem_obj
->checkUrlChecksum();
488 logReplyStatus(n_tries
, entry
->getReply()->sline
.status());
491 debugs(17, 3, HERE
<< "re-forwarding " << entry
->getReply()->sline
.status() << " " << entry
->url());
493 if (Comm::IsConnOpen(serverConn
))
494 unregister(serverConn
);
498 // drop the last path off the selection list. try the next one.
499 serverDestinations
.erase(serverDestinations
.begin());
500 startConnectionOrFail();
503 if (Comm::IsConnOpen(serverConn
))
504 debugs(17, 3, HERE
<< "server FD " << serverConnection()->fd
<< " not re-forwarding status " << entry
->getReply()->sline
.status());
506 debugs(17, 3, HERE
<< "server (FD closed) not re-forwarding status " << entry
->getReply()->sline
.status());
507 EBIT_CLR(entry
->flags
, ENTRY_FWD_HDR_WAIT
);
510 if (!Comm::IsConnOpen(serverConn
))
513 self
= NULL
; // refcounted
517 /**** CALLBACK WRAPPERS ************************************************************/
520 fwdPeerSelectionCompleteWrapper(Comm::ConnectionList
* unused
, ErrorState
*err
, void *data
)
522 FwdState
*fwd
= (FwdState
*) data
;
525 fwd
->startConnectionOrFail();
529 fwdServerClosedWrapper(const CommCloseCbParams
¶ms
)
531 FwdState
*fwd
= (FwdState
*)params
.data
;
532 fwd
->serverClosed(params
.fd
);
536 fwdConnectDoneWrapper(const Comm::ConnectionPointer
&conn
, comm_err_t status
, int xerrno
, void *data
)
538 FwdState
*fwd
= (FwdState
*) data
;
539 fwd
->connectDone(conn
, status
, xerrno
);
542 /**** PRIVATE *****************************************************************/
545 * FwdState::checkRetry
547 * Return TRUE if the request SHOULD be retried. This method is
548 * called when the HTTP connection fails, or when the connection
549 * is closed before server-side read the end of HTTP headers.
552 FwdState::checkRetry()
557 if (!self
) { // we have aborted before the server called us back
558 debugs(17, 5, HERE
<< "not retrying because of earlier abort");
559 // we will be destroyed when the server clears its Pointer to us
563 if (entry
->store_status
!= STORE_PENDING
)
566 if (!entry
->isEmpty())
569 if (n_tries
> Config
.forward_max_tries
)
572 if (squid_curtime
- start_t
> Config
.Timeout
.forward
)
575 if (flags
.dont_retry
)
578 if (request
->bodyNibbled())
581 // NP: not yet actually connected anywhere. retry is safe.
582 if (!flags
.connected_okay
)
585 if (!checkRetriable())
592 * FwdState::checkRetriable
594 * Return TRUE if this is the kind of request that can be retried
595 * after a failure. If the request is not retriable then we don't
596 * want to risk sending it on a persistent connection. Instead we'll
597 * force it to go on a new HTTP connection.
600 FwdState::checkRetriable()
602 // Optimize: A compliant proxy may retry PUTs, but Squid lacks the [rather
603 // complicated] code required to protect the PUT request body from being
604 // nibbled during the first try. Thus, Squid cannot retry some PUTs today.
605 if (request
->body_pipe
!= NULL
)
608 // RFC2616 9.1 Safe and Idempotent Methods
609 return (request
->method
.isHttpSafe() || request
->method
.isIdempotent());
613 FwdState::serverClosed(int fd
)
615 debugs(17, 2, HERE
<< "FD " << fd
<< " " << entry
->url());
620 FwdState::retryOrBail()
623 debugs(17, 3, HERE
<< "re-forwarding (" << n_tries
<< " tries, " << (squid_curtime
- start_t
) << " secs)");
624 // we should retry the same destination if it failed due to pconn race
625 if (pconnRace
== raceHappened
)
626 debugs(17, 4, HERE
<< "retrying the same destination");
628 serverDestinations
.erase(serverDestinations
.begin()); // last one failed. try another.
629 startConnectionOrFail();
633 // TODO: should we call completed() here and move doneWithRetries there?
636 if (self
!= NULL
&& !err
&& shutting_down
) {
637 ErrorState
*anErr
= new ErrorState(ERR_SHUTTING_DOWN
, Http::scServiceUnavailable
, request
);
638 errorAppendEntry(entry
, anErr
);
641 self
= NULL
; // refcounted
644 // If the Server quits before nibbling at the request body, the body sender
645 // will not know (so that we can retry). Call this if we will not retry. We
646 // will notify the sender so that it does not get stuck waiting for space.
648 FwdState::doneWithRetries()
650 if (request
&& request
->body_pipe
!= NULL
)
651 request
->body_pipe
->expectNoConsumption();
654 // called by the server that failed after calling unregister()
656 FwdState::handleUnregisteredServerEnd()
658 debugs(17, 2, HERE
<< "self=" << self
<< " err=" << err
<< ' ' << entry
->url());
659 assert(!Comm::IsConnOpen(serverConn
));
664 FwdState::connectDone(const Comm::ConnectionPointer
&conn
, comm_err_t status
, int xerrno
)
666 if (status
!= COMM_OK
) {
667 ErrorState
*const anErr
= makeConnectingError(ERR_CONNECT_FAIL
);
668 anErr
->xerrno
= xerrno
;
671 /* it might have been a timeout with a partially open link */
674 peerConnectFailed(conn
->getPeer());
683 flags
.connected_okay
= true;
685 debugs(17, 3, HERE
<< serverConnection() << ": '" << entry
->url() << "'" );
687 comm_add_close_handler(serverConnection()->fd
, fwdServerClosedWrapper
, this);
689 if (serverConnection()->getPeer())
690 peerConnectSucceded(serverConnection()->getPeer());
693 if (!request
->flags
.pinned
) {
694 if ((serverConnection()->getPeer() && serverConnection()->getPeer()->use_ssl
) ||
695 (!serverConnection()->getPeer() && request
->protocol
== AnyP::PROTO_HTTPS
) ||
696 request
->flags
.sslPeek
) {
698 HttpRequest::Pointer requestPointer
= request
;
699 AsyncCall::Pointer callback
= asyncCall(17,4,
700 "FwdState::ConnectedToPeer",
701 FwdStatePeerAnswerDialer(&FwdState::connectedToPeer
, this));
702 Ssl::PeerConnector
*connector
=
703 new Ssl::PeerConnector(requestPointer
, serverConnection(), callback
);
704 AsyncJob::Start(connector
); // will call our callback
715 FwdState::connectedToPeer(Ssl::PeerConnectorAnswer
&answer
)
717 if (ErrorState
*error
= answer
.error
.get()) {
719 answer
.error
.clear(); // preserve error for errorSendComplete()
729 FwdState::connectTimeout(int fd
)
731 debugs(17, 2, "fwdConnectTimeout: FD " << fd
<< ": '" << entry
->url() << "'" );
732 assert(serverDestinations
[0] != NULL
);
733 assert(fd
== serverDestinations
[0]->fd
);
735 if (entry
->isEmpty()) {
736 ErrorState
*anErr
= new ErrorState(ERR_CONNECT_FAIL
, Http::scGatewayTimeout
, request
);
737 anErr
->xerrno
= ETIMEDOUT
;
740 /* This marks the peer DOWN ... */
741 if (serverDestinations
[0]->getPeer())
742 peerConnectFailed(serverDestinations
[0]->getPeer());
745 if (Comm::IsConnOpen(serverDestinations
[0])) {
746 serverDestinations
[0]->close();
751 * Called after Forwarding path selection (via peer select) has taken place.
752 * And whenever forwarding needs to attempt a new connection (routing failover)
753 * We have a vector of possible localIP->remoteIP paths now ready to start being connected.
756 FwdState::connectStart()
758 assert(serverDestinations
.size() > 0);
760 debugs(17, 3, "fwdConnectStart: " << entry
->url());
762 if (!request
->hier
.first_conn_start
.tv_sec
) // first attempt
763 request
->hier
.first_conn_start
= current_time
;
765 /* connection timeout */
767 if (serverDestinations
[0]->getPeer()) {
768 ctimeout
= serverDestinations
[0]->getPeer()->connect_timeout
> 0 ?
769 serverDestinations
[0]->getPeer()->connect_timeout
: Config
.Timeout
.peer_connect
;
771 ctimeout
= Config
.Timeout
.connect
;
774 /* calculate total forwarding timeout ??? */
775 int ftimeout
= Config
.Timeout
.forward
- (squid_curtime
- start_t
);
779 if (ftimeout
< ctimeout
)
782 if (serverDestinations
[0]->getPeer() && request
->flags
.sslBumped
) {
783 debugs(50, 4, "fwdConnectStart: Ssl bumped connections through parent proxy are not allowed");
784 ErrorState
*anErr
= new ErrorState(ERR_CANNOT_FORWARD
, Http::scServiceUnavailable
, request
);
786 self
= NULL
; // refcounted
790 request
->flags
.pinned
= false; // XXX: what if the ConnStateData set this to flag existing credentials?
791 // XXX: answer: the peer selection *should* catch it and give us only the pinned peer. so we reverse the =0 step below.
792 // XXX: also, logs will now lie if pinning is broken and leads to an error message.
793 if (serverDestinations
[0]->peerType
== PINNED
) {
794 ConnStateData
*pinned_connection
= request
->pinnedConnection();
795 debugs(17,7, "pinned peer connection: " << pinned_connection
);
796 // pinned_connection may become nil after a pconn race
797 if (pinned_connection
)
798 serverConn
= pinned_connection
->validatePinnedConnection(request
, serverDestinations
[0]->getPeer());
801 if (Comm::IsConnOpen(serverConn
)) {
802 pinned_connection
->stopPinnedConnectionMonitoring();
803 flags
.connected_okay
= true;
805 request
->flags
.pinned
= true;
806 request
->hier
.note(serverConn
, pinned_connection
->pinning
.host
);
807 if (pinned_connection
->pinnedAuth())
808 request
->flags
.auth
= true;
809 comm_add_close_handler(serverConn
->fd
, fwdServerClosedWrapper
, this);
810 // the server may close the pinned connection before this request
811 pconnRace
= racePossible
;
815 // Pinned connection failure.
816 debugs(17,2,HERE
<< "Pinned connection failed: " << pinned_connection
);
817 ErrorState
*anErr
= new ErrorState(ERR_ZERO_SIZE_OBJECT
, Http::scServiceUnavailable
, request
);
819 self
= NULL
; // refcounted
823 // Use pconn to avoid opening a new connection.
824 const char *host
= NULL
;
825 if (!serverDestinations
[0]->getPeer())
826 host
= request
->GetHost();
828 Comm::ConnectionPointer temp
;
829 // Avoid pconns after races so that the same client does not suffer twice.
830 // This does not increase the total number of connections because we just
831 // closed the connection that failed the race. And re-pinning assumes this.
832 if (pconnRace
!= raceHappened
)
833 temp
= fwdPconnPool
->pop(serverDestinations
[0], host
, checkRetriable());
835 const bool openedPconn
= Comm::IsConnOpen(temp
);
836 pconnRace
= openedPconn
? racePossible
: raceImpossible
;
838 // if we found an open persistent connection to use. use it.
841 flags
.connected_okay
= true;
842 debugs(17, 3, HERE
<< "reusing pconn " << serverConnection());
845 comm_add_close_handler(serverConnection()->fd
, fwdServerClosedWrapper
, this);
847 /* Update server side TOS and Netfilter mark on the connection. */
848 if (Ip::Qos::TheConfig
.isAclTosActive()) {
849 const tos_t tos
= GetTosToServer(request
);
850 Ip::Qos::setSockTos(temp
, tos
);
853 if (Ip::Qos::TheConfig
.isAclNfmarkActive()) {
854 const nfmark_t nfmark
= GetNfmarkToServer(request
);
855 Ip::Qos::setSockNfmark(temp
, nfmark
);
863 // We will try to open a new connection, possibly to the same destination.
864 // We reset serverDestinations[0] in case we are using it again because
865 // ConnOpener modifies its destination argument.
866 serverDestinations
[0]->local
.port(0);
869 #if URL_CHECKSUM_DEBUG
870 entry
->mem_obj
->checkUrlChecksum();
873 GetMarkingsToServer(request
, *serverDestinations
[0]);
875 calls
.connector
= commCbCall(17,3, "fwdConnectDoneWrapper", CommConnectCbPtrFun(fwdConnectDoneWrapper
, this));
876 Comm::ConnOpener
*cs
= new Comm::ConnOpener(serverDestinations
[0], calls
.connector
, ctimeout
);
885 debugs(17, 3, clientConn
<< ": Fetching " << request
->method
<< ' ' << entry
->url());
887 * Assert that server_fd is set. This is to guarantee that fwdState
888 * is attached to something and will be deallocated when server_fd
891 assert(Comm::IsConnOpen(serverConn
));
893 fd_note(serverConnection()->fd
, entry
->url());
895 fd_table
[serverConnection()->fd
].noteUse(fwdPconnPool
);
897 /*assert(!EBIT_TEST(entry->flags, ENTRY_DISPATCHED)); */
898 assert(entry
->ping_status
!= PING_WAITING
);
900 assert(entry
->locked());
902 EBIT_SET(entry
->flags
, ENTRY_DISPATCHED
);
904 netdbPingSite(request
->GetHost());
906 /* Retrieves remote server TOS or MARK value, and stores it as part of the
907 * original client request FD object. It is later used to forward
908 * remote server's TOS/MARK in the response to the client in case of a MISS.
910 if (Ip::Qos::TheConfig
.isHitNfmarkActive()) {
911 if (Comm::IsConnOpen(clientConn
) && Comm::IsConnOpen(serverConnection())) {
912 fde
* clientFde
= &fd_table
[clientConn
->fd
]; // XXX: move the fd_table access into Ip::Qos
913 /* Get the netfilter mark for the connection */
914 Ip::Qos::getNfmarkFromServer(serverConnection(), clientFde
);
919 /* Bug 2537: The TOS forward part of QOS only applies to patched Linux kernels. */
920 if (Ip::Qos::TheConfig
.isHitTosActive()) {
921 if (Comm::IsConnOpen(clientConn
)) {
922 fde
* clientFde
= &fd_table
[clientConn
->fd
]; // XXX: move the fd_table access into Ip::Qos
923 /* Get the TOS value for the packet */
924 Ip::Qos::getTosFromServer(serverConnection(), clientFde
);
930 if (request
->flags
.sslPeek
) {
931 CallJobHere1(17, 4, request
->clientConnectionManager
, ConnStateData
,
932 ConnStateData::httpsPeeked
, serverConnection());
933 unregister(serverConn
); // async call owns it now
934 complete(); // destroys us
939 if (serverConnection()->getPeer() != NULL
) {
940 ++ serverConnection()->getPeer()->stats
.fetches
;
941 request
->peer_login
= serverConnection()->getPeer()->login
;
942 request
->peer_domain
= serverConnection()->getPeer()->domain
;
945 assert(!request
->flags
.sslPeek
);
946 request
->peer_login
= NULL
;
947 request
->peer_domain
= NULL
;
949 switch (request
->protocol
) {
952 case AnyP::PROTO_HTTPS
:
957 case AnyP::PROTO_HTTP
:
961 case AnyP::PROTO_GOPHER
:
965 case AnyP::PROTO_FTP
:
969 case AnyP::PROTO_CACHE_OBJECT
:
971 case AnyP::PROTO_INTERNAL
:
973 case AnyP::PROTO_URN
:
974 fatal_dump("Should never get here");
977 case AnyP::PROTO_WHOIS
:
981 case AnyP::PROTO_WAIS
: /* Not implemented */
984 debugs(17, DBG_IMPORTANT
, "WARNING: Cannot retrieve '" << entry
->url() << "'.");
985 ErrorState
*anErr
= new ErrorState(ERR_UNSUP_REQ
, Http::scBadRequest
, request
);
987 // Set the dont_retry flag because this is not a transient (network) error.
988 flags
.dont_retry
= true;
989 if (Comm::IsConnOpen(serverConn
)) {
998 * FwdState::reforward
1000 * returns TRUE if the transaction SHOULD be re-forwarded to the
1001 * next choice in the serverDestinations list. This method is called when
1002 * server-side communication completes normally, or experiences
1003 * some error after receiving the end of HTTP headers.
1006 FwdState::reforward()
1008 StoreEntry
*e
= entry
;
1010 if (EBIT_TEST(e
->flags
, ENTRY_ABORTED
)) {
1011 debugs(17, 3, HERE
<< "entry aborted");
1015 assert(e
->store_status
== STORE_PENDING
);
1017 #if URL_CHECKSUM_DEBUG
1019 e
->mem_obj
->checkUrlChecksum();
1022 debugs(17, 3, HERE
<< e
->url() << "?" );
1024 if (!EBIT_TEST(e
->flags
, ENTRY_FWD_HDR_WAIT
)) {
1025 debugs(17, 3, HERE
<< "No, ENTRY_FWD_HDR_WAIT isn't set");
1029 if (n_tries
> Config
.forward_max_tries
)
1032 if (request
->bodyNibbled())
1035 if (serverDestinations
.size() <= 1) {
1036 // NP: <= 1 since total count includes the recently failed one.
1037 debugs(17, 3, HERE
<< "No alternative forwarding paths left");
1041 const Http::StatusCode s
= e
->getReply()->sline
.status();
1042 debugs(17, 3, HERE
<< "status " << s
);
1043 return reforwardableStatus(s
);
1047 * Create "503 Service Unavailable" or "504 Gateway Timeout" error depending
1048 * on whether this is a validation request. RFC 2616 says that we MUST reply
1049 * with "504 Gateway Timeout" if validation fails and cached reply has
1050 * proxy-revalidate, must-revalidate or s-maxage Cache-Control directive.
1053 FwdState::makeConnectingError(const err_type type
) const
1055 return new ErrorState(type
, request
->flags
.needValidation
?
1056 Http::scGatewayTimeout
: Http::scServiceUnavailable
, request
);
1060 fwdStats(StoreEntry
* s
)
1064 storeAppendPrintf(s
, "Status");
1066 for (j
= 1; j
< MAX_FWD_STATS_IDX
; ++j
) {
1067 storeAppendPrintf(s
, "\ttry#%d", j
);
1070 storeAppendPrintf(s
, "\n");
1072 for (i
= 0; i
<= (int) Http::scInvalidHeader
; ++i
) {
1073 if (FwdReplyCodes
[0][i
] == 0)
1076 storeAppendPrintf(s
, "%3d", i
);
1078 for (j
= 0; j
<= MAX_FWD_STATS_IDX
; ++j
) {
1079 storeAppendPrintf(s
, "\t%d", FwdReplyCodes
[j
][i
]);
1082 storeAppendPrintf(s
, "\n");
1086 /**** STATIC MEMBER FUNCTIONS *************************************************/
1089 FwdState::reforwardableStatus(const Http::StatusCode s
) const
1093 case Http::scBadGateway
:
1095 case Http::scGatewayTimeout
:
1098 case Http::scForbidden
:
1100 case Http::scInternalServerError
:
1102 case Http::scNotImplemented
:
1104 case Http::scServiceUnavailable
:
1105 return Config
.retry
.onerror
;
1115 * Decide where details need to be gathered to correctly describe a persistent connection.
1117 * - the address/port details about this link
1118 * - domain name of server at other end of this link (either peer or requested host)
1121 FwdState::pconnPush(Comm::ConnectionPointer
&conn
, const char *domain
)
1123 if (conn
->getPeer()) {
1124 fwdPconnPool
->push(conn
, NULL
);
1126 fwdPconnPool
->push(conn
, domain
);
1131 FwdState::initModule()
1133 RegisterWithCacheManager();
1137 FwdState::RegisterWithCacheManager(void)
1139 Mgr::RegisterAction("forward", "Request Forwarding Statistics", fwdStats
, 0, 1);
1143 FwdState::logReplyStatus(int tries
, const Http::StatusCode status
)
1145 if (status
> Http::scInvalidHeader
)
1150 if (tries
> MAX_FWD_STATS_IDX
)
1151 tries
= MAX_FWD_STATS_IDX
;
1153 ++ FwdReplyCodes
[tries
][status
];
1156 /**** PRIVATE NON-MEMBER FUNCTIONS ********************************************/
1160 * Formerly static, but now used by client_side_request.cc
1162 /// Checks for a TOS value to apply depending on the ACL
1164 aclMapTOS(acl_tos
* head
, ACLChecklist
* ch
)
1168 for (l
= head
; l
; l
= l
->next
) {
1169 if (!l
->aclList
|| ch
->fastCheck(l
->aclList
) == ACCESS_ALLOWED
)
1176 /// Checks for a netfilter mark value to apply depending on the ACL
1178 aclMapNfmark(acl_nfmark
* head
, ACLChecklist
* ch
)
1182 for (l
= head
; l
; l
= l
->next
) {
1183 if (!l
->aclList
|| ch
->fastCheck(l
->aclList
) == ACCESS_ALLOWED
)
1191 getOutgoingAddress(HttpRequest
* request
, Comm::ConnectionPointer conn
)
1193 // skip if an outgoing address is already set.
1194 if (!conn
->local
.isAnyAddr()) return;
1196 // ensure that at minimum the wildcard local matches remote protocol
1197 if (conn
->remote
.isIPv4())
1198 conn
->local
.setIPv4();
1200 // maybe use TPROXY client address
1201 if (request
&& request
->flags
.spoofClientIp
) {
1202 if (!conn
->getPeer() || !conn
->getPeer()->options
.no_tproxy
) {
1203 #if FOLLOW_X_FORWARDED_FOR && LINUX_NETFILTER
1204 if (Config
.onoff
.tproxy_uses_indirect_client
)
1205 conn
->local
= request
->indirect_client_addr
;
1208 conn
->local
= request
->client_addr
;
1209 // some flags need setting on the socket to use this address
1210 conn
->flags
|= COMM_DOBIND
;
1211 conn
->flags
|= COMM_TRANSPARENT
;
1214 // else no tproxy today ...
1217 if (!Config
.accessList
.outgoing_address
) {
1218 return; // anything will do.
1221 ACLFilledChecklist
ch(NULL
, request
, NULL
);
1222 ch
.dst_peer
= conn
->getPeer();
1223 ch
.dst_addr
= conn
->remote
;
1225 // TODO use the connection details in ACL.
1226 // needs a bit of rework in ACLFilledChecklist to use Comm::Connection instead of ConnStateData
1229 for (l
= Config
.accessList
.outgoing_address
; l
; l
= l
->next
) {
1231 /* check if the outgoing address is usable to the destination */
1232 if (conn
->remote
.isIPv4() != l
->addr
.isIPv4()) continue;
1234 /* check ACLs for this outgoing address */
1235 if (!l
->aclList
|| ch
.fastCheck(l
->aclList
) == ACCESS_ALLOWED
) {
1236 conn
->local
= l
->addr
;
1243 GetTosToServer(HttpRequest
* request
)
1245 ACLFilledChecklist
ch(NULL
, request
, NULL
);
1246 return aclMapTOS(Ip::Qos::TheConfig
.tosToServer
, &ch
);
1250 GetNfmarkToServer(HttpRequest
* request
)
1252 ACLFilledChecklist
ch(NULL
, request
, NULL
);
1253 return aclMapNfmark(Ip::Qos::TheConfig
.nfmarkToServer
, &ch
);
1257 GetMarkingsToServer(HttpRequest
* request
, Comm::Connection
&conn
)
1259 // Get the server side TOS and Netfilter mark to be set on the connection.
1260 if (Ip::Qos::TheConfig
.isAclTosActive()) {
1261 conn
.tos
= GetTosToServer(request
);
1262 debugs(17, 3, "from " << conn
.local
<< " tos " << int(conn
.tos
));
1265 #if SO_MARK && USE_LIBCAP
1266 conn
.nfmark
= GetNfmarkToServer(request
);
1267 debugs(17, 3, "from " << conn
.local
<< " netfilter mark " << conn
.nfmark
);