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"
41 #include "format/Format.h"
43 #include "HttpRequest.h"
44 #include "mgr/Registration.h"
48 #include "SquidConfig.h"
51 #include "auth/UserRequest.h"
54 #include "ssl/support.h"
57 /// url maximum lengh + extra informations passed to redirector
58 #define MAX_REDIRECTOR_REQUEST_STRLEN (MAX_URL + 1024)
60 class RedirectStateData
63 explicit RedirectStateData(const char *url
);
69 Ip::Address client_addr
;
70 const char *client_ident
;
75 CBDATA_CLASS2(RedirectStateData
);
78 static HLPCB redirectHandleReply
;
79 static HLPCB storeIdHandleReply
;
80 static helper
*redirectors
= NULL
;
81 static helper
*storeIds
= NULL
;
82 static OBJH redirectStats
;
83 static OBJH storeIdStats
;
84 static int redirectorBypassed
= 0;
85 static int storeIdBypassed
= 0;
86 static Format::Format
*redirectorExtrasFmt
= NULL
;
87 static Format::Format
*storeIdExtrasFmt
= NULL
;
89 CBDATA_CLASS_INIT(RedirectStateData
);
91 RedirectStateData::RedirectStateData(const char *url
) :
101 RedirectStateData::~RedirectStateData()
106 redirectHandleReply(void *data
, const HelperReply
&reply
)
108 RedirectStateData
*r
= static_cast<RedirectStateData
*>(data
);
109 debugs(61, 5, HERE
<< "reply=" << reply
);
111 // XXX: This function is now kept only to check for and display the garbage use-case
112 // and to map the old helper response format(s) into new format result code and key=value pairs
113 // it can be removed when the helpers are all updated to the normalized "OK/ERR kv-pairs" format
115 if (reply
.result
== HelperReply::Unknown
) {
116 // BACKWARD COMPATIBILITY 2012-06-15:
117 // Some nasty old helpers send back the entire input line including extra format keys.
118 // This is especially bad for simple perl search-replace filter scripts.
120 // * trim all but the first word off the response.
121 // * warn once every 50 responses that this will stop being fixed-up soon.
123 if (const char * res
= reply
.other().content()) {
124 if (const char *t
= strchr(res
, ' ')) {
126 debugs(61, (!(warn
++%50)? DBG_CRITICAL
:2), "UPGRADE WARNING: URL rewriter reponded with garbage '" << t
<<
127 "'. Future Squid will treat this as part of the URL.");
128 const mb_size_t garbageLength
= reply
.other().contentSize() - (t
-res
);
129 reply
.modifiableOther().truncate(garbageLength
);
131 if (reply
.other().hasContent() && *res
== '\0')
132 reply
.modifiableOther().clean(); // drop the whole buffer of garbage.
134 // if we still have anything in other() after all that
135 // parse it into status=, url= and rewrite-url= keys
136 if (reply
.other().hasContent()) {
137 /* 2012-06-28: This cast is due to urlParse() truncating too-long URLs itself.
138 * At this point altering the helper buffer in that way is not harmful, but annoying.
139 * When Bug 1961 is resolved and urlParse has a const API, this needs to die.
141 const char * result
= reply
.other().content();
142 const Http::StatusCode status
= static_cast<Http::StatusCode
>(atoi(result
));
144 HelperReply newReply
;
145 // BACKWARD COMPATIBILITY 2012-06-15:
146 // We got HelperReply::Unknown reply result but new
147 // RedirectStateData handlers require HelperReply::Okay,
148 // else will drop the helper reply
149 newReply
.result
= HelperReply::Okay
;
150 newReply
.notes
.append(&reply
.notes
);
152 if (status
== Http::scMovedPermanently
153 || status
== Http::scMovedTemporarily
154 || status
== Http::scSeeOther
155 || status
== Http::scPermanentRedirect
156 || status
== Http::scTemporaryRedirect
) {
158 if (const char *t
= strchr(result
, ':')) {
160 snprintf(statusBuf
, sizeof(statusBuf
),"%3u",status
);
161 newReply
.notes
.add("status", statusBuf
);
163 // TODO: validate the URL produced here is RFC 2616 compliant URI
164 newReply
.notes
.add("url", t
);
166 debugs(85, DBG_CRITICAL
, "ERROR: URL-rewrite produces invalid " << status
<< " redirect Location: " << result
);
169 // status code is not a redirect code (or does not exist)
170 // treat as a re-write URL request
171 // TODO: validate the URL produced here is RFC 2616 compliant URI
172 newReply
.notes
.add("rewrite-url", reply
.other().content());
176 if (cbdataReferenceValidDone(r
->data
, &cbdata
))
177 r
->handler(cbdata
, newReply
);
186 if (cbdataReferenceValidDone(r
->data
, &cbdata
))
187 r
->handler(cbdata
, reply
);
193 storeIdHandleReply(void *data
, const HelperReply
&reply
)
195 RedirectStateData
*r
= static_cast<RedirectStateData
*>(data
);
196 debugs(61, 5,"StoreId helper: reply=" << reply
);
198 // XXX: This function is now kept only to check for and display the garbage use-case
199 // and to map the old helper response format(s) into new format result code and key=value pairs
200 // it can be removed when the helpers are all updated to the normalized "OK/ERR kv-pairs" format
202 if (cbdataReferenceValidDone(r
->data
, &cbdata
))
203 r
->handler(cbdata
, reply
);
209 redirectStats(StoreEntry
* sentry
)
211 if (redirectors
== NULL
) {
212 storeAppendPrintf(sentry
, "No redirectors defined\n");
216 helperStats(sentry
, redirectors
, "Redirector Statistics");
218 if (Config
.onoff
.redirector_bypass
)
219 storeAppendPrintf(sentry
, "\nNumber of requests bypassed "
220 "because all redirectors were busy: %d\n", redirectorBypassed
);
224 storeIdStats(StoreEntry
* sentry
)
226 if (storeIds
== NULL
) {
227 storeAppendPrintf(sentry
, "No StoreId helpers defined\n");
231 helperStats(sentry
, storeIds
, "StoreId helper Statistics");
233 if (Config
.onoff
.store_id_bypass
)
234 storeAppendPrintf(sentry
, "\nNumber of requests bypassed "
235 "because all StoreId helpers were busy: %d\n", storeIdBypassed
);
239 constructHelperQuery(const char *name
, helper
*hlp
, HLPCB
*replyHandler
, ClientHttpRequest
* http
, HLPCB
*handler
, void *data
, Format::Format
*requestExtrasFmt
)
241 ConnStateData
* conn
= http
->getConn();
243 char buf
[MAX_REDIRECTOR_REQUEST_STRLEN
];
245 Http::StatusCode status
;
247 /** TODO: create a standalone method to initialize
248 * the RedirectStateData for all the helpers.
250 RedirectStateData
*r
= new RedirectStateData(http
->uri
);
252 r
->client_addr
= conn
->log_addr
;
254 r
->client_addr
.setNoAddr();
255 r
->client_ident
= NULL
;
257 if (http
->request
->auth_user_request
!= NULL
) {
258 r
->client_ident
= http
->request
->auth_user_request
->username();
259 debugs(61, 5, HERE
<< "auth-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
263 if (!r
->client_ident
&& http
->request
->extacl_user
.size() > 0) {
264 r
->client_ident
= http
->request
->extacl_user
.termedBuf();
265 debugs(61, 5, HERE
<< "acl-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
268 if (!r
->client_ident
&& conn
!= NULL
&& conn
->clientConnection
!= NULL
&& conn
->clientConnection
->rfc931
[0]) {
269 r
->client_ident
= conn
->clientConnection
->rfc931
;
270 debugs(61, 5, HERE
<< "ident-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
275 if (!r
->client_ident
&& conn
!= NULL
&& Comm::IsConnOpen(conn
->clientConnection
)) {
276 r
->client_ident
= sslGetUserEmail(fd_table
[conn
->clientConnection
->fd
].ssl
);
277 debugs(61, 5, HERE
<< "ssl-user=" << (r
->client_ident
?r
->client_ident
:"NULL"));
281 if (!r
->client_ident
)
282 r
->client_ident
= dash_str
;
284 r
->method_s
= RequestMethodStr(http
->request
->method
);
286 r
->handler
= handler
;
288 r
->data
= cbdataReference(data
);
290 if ((fqdn
= fqdncache_gethostbyaddr(r
->client_addr
, 0)) == NULL
)
293 static MemBuf requestExtras
;
294 requestExtras
.reset();
295 if (requestExtrasFmt
)
296 requestExtrasFmt
->assemble(requestExtras
, http
->al
, 0);
298 sz
= snprintf(buf
, MAX_REDIRECTOR_REQUEST_STRLEN
, "%s%s%s\n",
300 requestExtras
.hasContent() ? " " : "",
301 requestExtras
.hasContent() ? requestExtras
.content() : "");
303 if ((sz
<=0) || (sz
>=MAX_REDIRECTOR_REQUEST_STRLEN
)) {
305 status
= Http::scInternalServerError
;
306 debugs(61, DBG_CRITICAL
, "ERROR: Gateway Failure. Can not build request to be passed to " << name
<< ". Request ABORTED.");
308 status
= Http::scRequestUriTooLarge
;
309 debugs(61, DBG_CRITICAL
, "ERROR: Gateway Failure. Request passed to " << name
<< " exceeds MAX_REDIRECTOR_REQUEST_STRLEN (" << MAX_REDIRECTOR_REQUEST_STRLEN
<< "). Request ABORTED.");
312 clientStreamNode
*node
= (clientStreamNode
*)http
->client_stream
.tail
->prev
->data
;
313 clientReplyContext
*repContext
= dynamic_cast<clientReplyContext
*>(node
->data
.getRaw());
315 Ip::Address tmpnoaddr
;
316 tmpnoaddr
.setNoAddr();
317 repContext
->setReplyToError(ERR_GATEWAY_FAILURE
, status
,
318 http
->request
->method
, NULL
,
319 http
->getConn() != NULL
&& http
->getConn()->clientConnection
!= NULL
?
320 http
->getConn()->clientConnection
->remote
: tmpnoaddr
,
324 http
->getConn() != NULL
&& http
->getConn()->getAuth() != NULL
?
325 http
->getConn()->getAuth() : http
->request
->auth_user_request
);
330 node
= (clientStreamNode
*)http
->client_stream
.tail
->data
;
331 clientStreamRead(node
, http
, node
->readBuffer
);
335 debugs(61,6, HERE
<< "sending '" << buf
<< "' to the " << name
<< " helper");
336 helperSubmit(hlp
, buf
, replyHandler
, r
);
339 /**** PUBLIC FUNCTIONS ****/
342 redirectStart(ClientHttpRequest
* http
, HLPCB
* handler
, void *data
)
346 debugs(61, 5, "redirectStart: '" << http
->uri
<< "'");
348 if (Config
.onoff
.redirector_bypass
&& redirectors
->stats
.queue_size
) {
349 /* Skip redirector if there is one request queued */
350 ++redirectorBypassed
;
351 HelperReply bypassReply
;
352 bypassReply
.result
= HelperReply::Okay
;
353 bypassReply
.notes
.add("message","URL rewrite/redirect queue too long. Bypassed.");
354 handler(data
, bypassReply
);
358 constructHelperQuery("redirector", redirectors
, redirectHandleReply
, http
, handler
, data
, redirectorExtrasFmt
);
362 * Handles the StoreID feature helper starting.
363 * For now it cannot be done using the redirectStart method.
366 storeIdStart(ClientHttpRequest
* http
, HLPCB
* handler
, void *data
)
370 debugs(61, 5, "storeIdStart: '" << http
->uri
<< "'");
372 if (Config
.onoff
.store_id_bypass
&& storeIds
->stats
.queue_size
) {
373 /* Skip StoreID Helper if there is one request queued */
375 HelperReply bypassReply
;
377 bypassReply
.result
= HelperReply::Okay
;
379 bypassReply
.notes
.add("message","StoreId helper queue too long. Bypassed.");
380 handler(data
, bypassReply
);
384 constructHelperQuery("storeId helper", storeIds
, storeIdHandleReply
, http
, handler
, data
, storeIdExtrasFmt
);
390 static bool init
= false;
393 Mgr::RegisterAction("redirector", "URL Redirector Stats", redirectStats
, 0, 1);
394 Mgr::RegisterAction("store_id", "StoreId helper Stats", storeIdStats
, 0, 1);
397 if (Config
.Program
.redirect
) {
399 if (redirectors
== NULL
)
400 redirectors
= new helper("redirector");
402 redirectors
->cmdline
= Config
.Program
.redirect
;
404 redirectors
->childs
.updateLimits(Config
.redirectChildren
);
406 redirectors
->ipc_type
= IPC_STREAM
;
408 helperOpenServers(redirectors
);
411 if (Config
.Program
.store_id
) {
413 if (storeIds
== NULL
)
414 storeIds
= new helper("store_id");
416 storeIds
->cmdline
= Config
.Program
.store_id
;
418 storeIds
->childs
.updateLimits(Config
.storeIdChildren
);
420 storeIds
->ipc_type
= IPC_STREAM
;
422 helperOpenServers(storeIds
);
425 if (Config
.redirector_extras
) {
426 redirectorExtrasFmt
= new ::Format::Format("redirecor_extras");
427 (void)redirectorExtrasFmt
->parse(Config
.redirector_extras
);
430 if (Config
.storeId_extras
) {
431 storeIdExtrasFmt
= new ::Format::Format("storeId_extras");
432 (void)storeIdExtrasFmt
->parse(Config
.storeId_extras
);
439 redirectShutdown(void)
441 /** FIXME: Temporary unified helpers Shutdown
442 * When and if needed for more helpers a separated shutdown
443 * method will be added for each of them.
445 if (!storeIds
&& !redirectors
)
449 helperShutdown(redirectors
);
452 helperShutdown(storeIds
);
463 delete redirectorExtrasFmt
;
464 redirectorExtrasFmt
= NULL
;
466 delete storeIdExtrasFmt
;
467 storeIdExtrasFmt
= NULL
;