2 * DEBUG: section 61 Redirector
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 "acl/Checklist.h"
35 #include "client_side.h"
36 #include "client_side_reply.h"
37 #include "client_side_request.h"
38 #include "comm/Connection.h"
40 #include "fqdncache.h"
42 #include "HttpRequest.h"
44 #include "mgr/Registration.h"
49 #include "auth/UserRequest.h"
52 #include "ssl/support.h"
55 /// url maximum lengh + extra informations passed to redirector
56 #define MAX_REDIRECTOR_REQUEST_STRLEN (MAX_URL + 1024)
62 Ip::Address client_addr
;
63 const char *client_ident
;
68 static HLPCB redirectHandleReply
;
69 static void redirectStateFree(redirectStateData
* r
);
70 static helper
*redirectors
= NULL
;
71 static OBJH redirectStats
;
72 static int n_bypassed
= 0;
73 CBDATA_TYPE(redirectStateData
);
76 redirectHandleReply(void *data
, char *reply
)
78 redirectStateData
*r
= static_cast<redirectStateData
*>(data
);
81 debugs(61, 5, "redirectHandleRead: {" << (reply
&& *reply
!= '\0' ? reply
: "<NULL>") << "}");
84 if ((t
= strchr(reply
, ' ')))
91 if (cbdataReferenceValidDone(r
->data
, &cbdata
))
92 r
->handler(cbdata
, reply
);
98 redirectStateFree(redirectStateData
* r
)
100 safe_free(r
->orig_url
);
105 redirectStats(StoreEntry
* sentry
)
107 if (redirectors
== NULL
) {
108 storeAppendPrintf(sentry
, "No redirectors defined\n");
112 helperStats(sentry
, redirectors
, "Redirector Statistics");
114 if (Config
.onoff
.redirector_bypass
)
115 storeAppendPrintf(sentry
, "\nNumber of requests bypassed "
116 "because all redirectors were busy: %d\n", n_bypassed
);
119 /**** PUBLIC FUNCTIONS ****/
122 redirectStart(ClientHttpRequest
* http
, RH
* handler
, void *data
)
124 ConnStateData
* conn
= http
->getConn();
125 redirectStateData
*r
= NULL
;
127 char buf
[MAX_REDIRECTOR_REQUEST_STRLEN
];
130 char claddr
[MAX_IPSTRLEN
];
131 char myaddr
[MAX_IPSTRLEN
];
134 debugs(61, 5, "redirectStart: '" << http
->uri
<< "'");
136 if (Config
.onoff
.redirector_bypass
&& redirectors
->stats
.queue_size
) {
137 /* Skip redirector if there is one request queued */
143 r
= cbdataAlloc(redirectStateData
);
144 r
->orig_url
= xstrdup(http
->uri
);
146 r
->client_addr
= conn
->log_addr
;
148 r
->client_addr
.SetNoAddr();
149 r
->client_ident
= NULL
;
151 if (http
->request
->auth_user_request
!= NULL
) {
152 r
->client_ident
= http
->request
->auth_user_request
->username();
153 debugs(61, 5, HERE
<< "auth-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
157 // HttpRequest initializes with null_string. So we must check both defined() and size()
158 if (!r
->client_ident
&& http
->request
->extacl_user
.defined() && http
->request
->extacl_user
.size()) {
159 r
->client_ident
= http
->request
->extacl_user
.termedBuf();
160 debugs(61, 5, HERE
<< "acl-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
163 if (!r
->client_ident
&& conn
!= NULL
&& conn
->clientConnection
!= NULL
&& conn
->clientConnection
->rfc931
[0]) {
164 r
->client_ident
= conn
->clientConnection
->rfc931
;
165 debugs(61, 5, HERE
<< "ident-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
170 if (!r
->client_ident
&& conn
!= NULL
&& Comm::IsConnOpen(conn
->clientConnection
)) {
171 r
->client_ident
= sslGetUserEmail(fd_table
[conn
->clientConnection
->fd
].ssl
);
172 debugs(61, 5, HERE
<< "ssl-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
176 if (!r
->client_ident
)
177 r
->client_ident
= dash_str
;
179 r
->method_s
= RequestMethodStr(http
->request
->method
);
181 r
->handler
= handler
;
183 r
->data
= cbdataReference(data
);
185 if ((fqdn
= fqdncache_gethostbyaddr(r
->client_addr
, 0)) == NULL
)
188 sz
= snprintf(buf
, MAX_REDIRECTOR_REQUEST_STRLEN
, "%s %s/%s %s %s myip=%s myport=%d\n",
190 r
->client_addr
.NtoA(claddr
,MAX_IPSTRLEN
),
192 r
->client_ident
[0] ? rfc1738_escape(r
->client_ident
) : dash_str
,
194 http
->request
->my_addr
.NtoA(myaddr
,MAX_IPSTRLEN
),
195 http
->request
->my_addr
.GetPort());
197 if ((sz
<=0) || (sz
>=MAX_REDIRECTOR_REQUEST_STRLEN
)) {
199 status
= HTTP_INTERNAL_SERVER_ERROR
;
200 debugs(61, DBG_CRITICAL
, "ERROR: Gateway Failure. Can not build request to be passed to redirector. Request ABORTED.");
202 status
= HTTP_REQUEST_URI_TOO_LARGE
;
203 debugs(61, DBG_CRITICAL
, "ERROR: Gateway Failure. Request passed to redirector exceeds MAX_REDIRECTOR_REQUEST_STRLEN (" << MAX_REDIRECTOR_REQUEST_STRLEN
<< "). Request ABORTED.");
206 clientStreamNode
*node
= (clientStreamNode
*)http
->client_stream
.tail
->prev
->data
;
207 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
209 Ip::Address tmpnoaddr
;
210 tmpnoaddr
.SetNoAddr();
211 repContext
->setReplyToError(ERR_GATEWAY_FAILURE
, status
,
212 http
->request
->method
, NULL
,
213 http
->getConn() != NULL
&& http
->getConn()->clientConnection
!= NULL
?
214 http
->getConn()->clientConnection
->remote
: tmpnoaddr
,
218 http
->getConn() != NULL
&& http
->getConn()->auth_user_request
!= NULL
?
219 http
->getConn()->auth_user_request
: http
->request
->auth_user_request
);
224 node
= (clientStreamNode
*)http
->client_stream
.tail
->data
;
225 clientStreamRead(node
, http
, node
->readBuffer
);
229 debugs(61,6, HERE
<< "sending '" << buf
<< "' to the helper");
230 helperSubmit(redirectors
, buf
, redirectHandleReply
, r
);
234 redirectRegisterWithCacheManager(void)
236 Mgr::RegisterAction("redirector", "URL Redirector Stats", redirectStats
, 0, 1);
244 redirectRegisterWithCacheManager();
246 if (!Config
.Program
.redirect
)
249 if (redirectors
== NULL
)
250 redirectors
= new helper("redirector");
252 redirectors
->cmdline
= Config
.Program
.redirect
;
254 redirectors
->childs
.updateLimits(Config
.redirectChildren
);
256 redirectors
->ipc_type
= IPC_STREAM
;
258 helperOpenServers(redirectors
);
262 CBDATA_INIT_TYPE(redirectStateData
);
267 redirectShutdown(void)
272 helperShutdown(redirectors
);