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