]> git.ipfire.org Git - thirdparty/squid.git/blob - src/internal.cc
Merged from trunk (r12732, v3.3.3+).
[thirdparty/squid.git] / src / internal.cc
1
2 /*
3 * DEBUG: section 76 Internal Squid Object handling
4 * AUTHOR: Duane, Alex, Henrik
5 *
6 * SQUID Web Proxy Cache http://www.squid-cache.org/
7 * ----------------------------------------------------------
8 *
9 * Squid is the result of efforts by numerous individuals from
10 * the Internet community; see the CONTRIBUTORS file for full
11 * details. Many organizations have provided support for Squid's
12 * development; see the SPONSORS file for full details. Squid is
13 * Copyrighted (C) 2001 by the Regents of the University of
14 * California; see the COPYRIGHT file for full details. Squid
15 * incorporates software developed and/or copyrighted by other
16 * sources; see the CREDITS file for full details.
17 *
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
31 *
32 */
33
34 #include "squid.h"
35 #include "CacheManager.h"
36 #include "comm/Connection.h"
37 #include "errorpage.h"
38 #include "icmp/net_db.h"
39 #include "Store.h"
40 #include "HttpRequest.h"
41 #include "HttpReply.h"
42 #include "MemBuf.h"
43 #include "SquidConfig.h"
44 #include "SquidTime.h"
45 #include "tools.h"
46 #include "URL.h"
47 #include "wordlist.h"
48
49 /* called when we "miss" on an internal object;
50 * generate known dynamic objects,
51 * return Http::scNotFound for others
52 */
53 void
54 internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest * request, StoreEntry * entry)
55 {
56 ErrorState *err;
57 const char *upath = request->urlpath.termedBuf();
58 debugs(76, 3, HERE << clientConn << " requesting '" << upath << "'");
59
60 if (0 == strcmp(upath, "/squid-internal-dynamic/netdb")) {
61 netdbBinaryExchange(entry);
62 } else if (0 == strcmp(upath, "/squid-internal-periodic/store_digest")) {
63 #if USE_CACHE_DIGESTS
64 const char *msgbuf = "This cache is currently building its digest.\n";
65 #else
66
67 const char *msgbuf = "This cache does not support Cache Digests.\n";
68 #endif
69
70 HttpReply *reply = new HttpReply;
71 reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
72 entry->replaceHttpReply(reply);
73 entry->append(msgbuf, strlen(msgbuf));
74 entry->complete();
75 } else if (0 == strncmp(upath, "/squid-internal-mgr/", 20)) {
76 CacheManager::GetInstance()->Start(clientConn, request, entry);
77 } else {
78 debugObj(76, 1, "internalStart: unknown request:\n",
79 request, (ObjPackMethod) & httpRequestPack);
80 err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request);
81 errorAppendEntry(entry, err);
82 }
83 }
84
85 int
86 internalCheck(const char *urlpath)
87 {
88 return (0 == strncmp(urlpath, "/squid-internal-", 16));
89 }
90
91 int
92 internalStaticCheck(const char *urlpath)
93 {
94 return (0 == strncmp(urlpath, "/squid-internal-static", 22));
95 }
96
97 /*
98 * makes internal url with a given host and port (remote internal url)
99 */
100 char *
101 internalRemoteUri(const char *host, unsigned short port, const char *dir, const char *name)
102 {
103 static char lc_host[SQUIDHOSTNAMELEN];
104 assert(host && name);
105 /* convert host name to lower case */
106 xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
107 Tolower(lc_host);
108
109 /* check for an IP address and format appropriately if found */
110 Ip::Address test = lc_host;
111 if ( !test.IsAnyAddr() ) {
112 test.ToHostname(lc_host,SQUIDHOSTNAMELEN);
113 }
114
115 /*
116 * append the domain in order to mirror the requests with appended
117 * domains
118 */
119
120 /* For IPv6 addresses also check for a colon */
121 if (Config.appendDomain && !strchr(lc_host, '.') && !strchr(lc_host, ':'))
122 strncat(lc_host, Config.appendDomain, SQUIDHOSTNAMELEN -
123 strlen(lc_host) - 1);
124
125 /* build uri in mb */
126 static MemBuf mb;
127
128 mb.reset();
129
130 mb.Printf("http://%s", lc_host);
131
132 /* append port if not default */
133 if (port && port != urlDefaultPort(AnyP::PROTO_HTTP))
134 mb.Printf(":%d", port);
135
136 if (dir)
137 mb.Printf("%s", dir);
138
139 mb.Printf("%s", name);
140
141 /* return a pointer to a local static buffer */
142 return mb.buf;
143 }
144
145 /*
146 * makes internal url with local host and port
147 */
148 char *
149 internalLocalUri(const char *dir, const char *name)
150 {
151 return internalRemoteUri(getMyHostname(),
152 getMyPort(), dir, name);
153 }
154
155 const char *
156 internalHostname(void)
157 {
158 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
159 xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN);
160
161 /* For IPv6 addresses also check for a colon */
162 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
163 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
164 strlen(host) - 1);
165
166 Tolower(host);
167
168 return host;
169 }
170
171 int
172 internalHostnameIs(const char *arg)
173 {
174 wordlist *w;
175
176 if (0 == strcmp(arg, internalHostname()))
177 return 1;
178
179 for (w = Config.hostnameAliases; w; w = w->next)
180 if (0 == strcmp(arg, w->key))
181 return 1;
182
183 return 0;
184 }