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