]> git.ipfire.org Git - thirdparty/squid.git/blob - src/internal.cc
Renamed squid.h to squid-old.h and config.h to squid.h
[thirdparty/squid.git] / src / internal.cc
1
2 /*
3 * $Id$
4 *
5 * DEBUG: section 76 Internal Squid Object handling
6 * AUTHOR: Duane, Alex, Henrik
7 *
8 * SQUID Web Proxy Cache http://www.squid-cache.org/
9 * ----------------------------------------------------------
10 *
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.
19 *
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.
24 *
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.
29 *
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.
33 *
34 */
35
36 #include "squid-old.h"
37 #include "CacheManager.h"
38 #include "comm/Connection.h"
39 #include "errorpage.h"
40 #include "Store.h"
41 #include "HttpRequest.h"
42 #include "HttpReply.h"
43 #include "MemBuf.h"
44 #include "SquidTime.h"
45 #include "wordlist.h"
46 #include "icmp/net_db.h"
47
48 /* called when we "miss" on an internal object;
49 * generate known dynamic objects,
50 * return HTTP_NOT_FOUND for others
51 */
52 void
53 internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest * request, StoreEntry * entry)
54 {
55 ErrorState *err;
56 const char *upath = request->urlpath.termedBuf();
57 debugs(76, 3, HERE << clientConn << " requesting '" << upath << "'");
58
59 if (0 == strcmp(upath, "/squid-internal-dynamic/netdb")) {
60 netdbBinaryExchange(entry);
61 } else if (0 == strcmp(upath, "/squid-internal-periodic/store_digest")) {
62 #if USE_CACHE_DIGESTS
63 const char *msgbuf = "This cache is currently building its digest.\n";
64 #else
65
66 const char *msgbuf = "This cache does not support Cache Digests.\n";
67 #endif
68
69 HttpReply *reply = new HttpReply;
70 reply->setHeaders(HTTP_NOT_FOUND, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
71 entry->replaceHttpReply(reply);
72 entry->append(msgbuf, strlen(msgbuf));
73 entry->complete();
74 } else if (0 == strncmp(upath, "/squid-internal-mgr/", 20)) {
75 CacheManager::GetInstance()->Start(clientConn, request, entry);
76 } else {
77 debugObj(76, 1, "internalStart: unknown request:\n",
78 request, (ObjPackMethod) & httpRequestPack);
79 err = new ErrorState(ERR_INVALID_REQ, HTTP_NOT_FOUND, request);
80 errorAppendEntry(entry, err);
81 }
82 }
83
84 int
85 internalCheck(const char *urlpath)
86 {
87 return (0 == strncmp(urlpath, "/squid-internal-", 16));
88 }
89
90 int
91 internalStaticCheck(const char *urlpath)
92 {
93 return (0 == strncmp(urlpath, "/squid-internal-static", 22));
94 }
95
96 /*
97 * makes internal url with a given host and port (remote internal url)
98 */
99 char *
100 internalRemoteUri(const char *host, unsigned short port, const char *dir, const char *name)
101 {
102 static char lc_host[SQUIDHOSTNAMELEN];
103 assert(host && name);
104 /* convert host name to lower case */
105 xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
106 Tolower(lc_host);
107
108 /* check for an IP address and format appropriately if found */
109 Ip::Address test = lc_host;
110 if ( !test.IsAnyAddr() ) {
111 test.ToHostname(lc_host,SQUIDHOSTNAMELEN);
112 }
113
114 /*
115 * append the domain in order to mirror the requests with appended
116 * domains
117 */
118
119 /* For IPv6 addresses also check for a colon */
120 if (Config.appendDomain && !strchr(lc_host, '.') && !strchr(lc_host, ':'))
121 strncat(lc_host, Config.appendDomain, SQUIDHOSTNAMELEN -
122 strlen(lc_host) - 1);
123
124 /* build uri in mb */
125 static MemBuf mb;
126
127 mb.reset();
128
129 mb.Printf("http://%s", lc_host);
130
131 /* append port if not default */
132 if (port && port != urlDefaultPort(AnyP::PROTO_HTTP))
133 mb.Printf(":%d", port);
134
135 if (dir)
136 mb.Printf("%s", dir);
137
138 mb.Printf("%s", name);
139
140 /* return a pointer to a local static buffer */
141 return mb.buf;
142 }
143
144 /*
145 * makes internal url with local host and port
146 */
147 char *
148 internalLocalUri(const char *dir, const char *name)
149 {
150 return internalRemoteUri(getMyHostname(),
151 getMyPort(), dir, name);
152 }
153
154 const char *
155 internalHostname(void)
156 {
157 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
158 xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN);
159
160 /* For IPv6 addresses also check for a colon */
161 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
162 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
163 strlen(host) - 1);
164
165 Tolower(host);
166
167 return host;
168 }
169
170 int
171 internalHostnameIs(const char *arg)
172 {
173 wordlist *w;
174
175 if (0 == strcmp(arg, internalHostname()))
176 return 1;
177
178 for (w = Config.hostnameAliases; w; w = w->next)
179 if (0 == strcmp(arg, w->key))
180 return 1;
181
182 return 0;
183 }