1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
|
/*
* $Id: redirect.c,v 1.96 2006/07/08 16:01:12 serassio Exp $
*
* DEBUG: section 61 Redirector
* AUTHOR: Duane Wessels
*
* SQUID Web Proxy Cache http://www.squid-cache.org/
* ----------------------------------------------------------
*
* Squid is the result of efforts by numerous individuals from
* the Internet community; see the CONTRIBUTORS file for full
* details. Many organizations have provided support for Squid's
* development; see the SPONSORS file for full details. Squid is
* Copyrighted (C) 2001 by the Regents of the University of
* California; see the COPYRIGHT file for full details. Squid
* incorporates software developed and/or copyrighted by other
* sources; see the CREDITS file for full details.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
*
*/
#include "squid.h"
typedef struct {
void *data;
char *orig_url;
struct in_addr client_addr;
const char *client_ident;
const char *method_s;
RH *handler;
} redirectStateData;
static HLPCB redirectHandleReply;
static void redirectStateFree(redirectStateData * r);
static helper *redirectors = NULL;
static OBJH redirectStats;
static int n_bypassed = 0;
CBDATA_TYPE(redirectStateData);
static void
redirectHandleReply(void *data, char *reply)
{
redirectStateData *r = data;
int valid;
char *t;
debug(61, 5) ("redirectHandleRead: {%s}\n", reply ? reply : "<NULL>");
if (reply) {
if ((t = strchr(reply, ' ')))
*t = '\0';
if (*reply == '\0')
reply = NULL;
}
valid = cbdataValid(r->data);
cbdataUnlock(r->data);
if (valid)
r->handler(r->data, reply);
redirectStateFree(r);
}
static void
redirectStateFree(redirectStateData * r)
{
safe_free(r->orig_url);
cbdataFree(r);
}
static void
redirectStats(StoreEntry * sentry)
{
storeAppendPrintf(sentry, "Redirector Statistics:\n");
helperStats(sentry, redirectors);
if (Config.onoff.redirector_bypass)
storeAppendPrintf(sentry, "\nNumber of requests bypassed "
"because all redirectors were busy: %d\n", n_bypassed);
}
/**** PUBLIC FUNCTIONS ****/
void
redirectStart(clientHttpRequest * http, RH * handler, void *data)
{
ConnStateData *conn = http->conn;
redirectStateData *r = NULL;
const char *fqdn;
char *urlgroup = conn->port->urlgroup;
char buf[8192];
assert(http);
assert(handler);
debug(61, 5) ("redirectStart: '%s'\n", http->uri);
if (Config.onoff.redirector_bypass && redirectors->stats.queue_size) {
/* Skip redirector if there is one request queued */
n_bypassed++;
handler(data, NULL);
return;
}
r = cbdataAlloc(redirectStateData);
r->orig_url = xstrdup(http->uri);
r->client_addr = conn->log_addr;
r->client_ident = NULL;
if (http->request->auth_user_request)
r->client_ident = authenticateUserRequestUsername(http->request->auth_user_request);
else if (http->request->extacl_user) {
r->client_ident = http->request->extacl_user;
}
if (!r->client_ident && conn->rfc931[0])
r->client_ident = conn->rfc931;
#if USE_SSL
if (!r->client_ident)
r->client_ident = sslGetUserEmail(fd_table[conn->fd].ssl);
#endif
if (!r->client_ident)
r->client_ident = dash_str;
r->method_s = RequestMethodStr[http->request->method];
r->handler = handler;
r->data = data;
cbdataLock(r->data);
if ((fqdn = fqdncache_gethostbyaddr(r->client_addr, 0)) == NULL)
fqdn = dash_str;
snprintf(buf, 8192, "%s %s/%s %s %s %s\n",
r->orig_url,
inet_ntoa(r->client_addr),
fqdn,
r->client_ident[0] ? rfc1738_escape(r->client_ident) : dash_str,
r->method_s,
urlgroup ? urlgroup : "-");
helperSubmit(redirectors, buf, redirectHandleReply, r);
}
void
redirectInit(void)
{
static int init = 0;
if (!Config.Program.url_rewrite.command)
return;
if (redirectors == NULL)
redirectors = helperCreate("url_rewriter");
redirectors->cmdline = Config.Program.url_rewrite.command;
redirectors->n_to_start = Config.Program.url_rewrite.children;
redirectors->concurrency = Config.Program.url_rewrite.concurrency;
redirectors->ipc_type = IPC_STREAM;
helperOpenServers(redirectors);
if (!init) {
cachemgrRegister("url_rewriter",
"URL Rewriter Stats",
redirectStats, 0, 1);
init = 1;
CBDATA_INIT_TYPE(redirectStateData);
}
}
void
redirectShutdown(void)
{
if (!redirectors)
return;
helperShutdown(redirectors);
if (!shutting_down)
return;
helperFree(redirectors);
redirectors = NULL;
}
|