]> git.ipfire.org Git - thirdparty/squid.git/blob - src/internal.cc
Docs: Copyright updates for 2018 (#114)
[thirdparty/squid.git] / src / internal.cc
1 /*
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9 /* DEBUG: section 76 Internal Squid Object handling */
10
11 #include "squid.h"
12 #include "CacheManager.h"
13 #include "comm/Connection.h"
14 #include "errorpage.h"
15 #include "HttpReply.h"
16 #include "HttpRequest.h"
17 #include "icmp/net_db.h"
18 #include "MemBuf.h"
19 #include "SquidConfig.h"
20 #include "SquidTime.h"
21 #include "Store.h"
22 #include "tools.h"
23 #include "URL.h"
24 #include "util.h"
25 #include "wordlist.h"
26
27 /* called when we "miss" on an internal object;
28 * generate known dynamic objects,
29 * return Http::scNotFound for others
30 */
31 void
32 internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest * request, StoreEntry * entry)
33 {
34 ErrorState *err;
35 const SBuf upath = request->url.path();
36 debugs(76, 3, clientConn << " requesting '" << upath << "'");
37
38 static const SBuf netdbUri("/squid-internal-dynamic/netdb");
39 static const SBuf storeDigestUri("/squid-internal-periodic/store_digest");
40 static const SBuf mgrPfx("/squid-internal-mgr/");
41
42 if (upath == netdbUri) {
43 netdbBinaryExchange(entry);
44 } else if (upath == storeDigestUri) {
45 #if USE_CACHE_DIGESTS
46 const char *msgbuf = "This cache is currently building its digest.\n";
47 #else
48
49 const char *msgbuf = "This cache does not support Cache Digests.\n";
50 #endif
51
52 HttpReply *reply = new HttpReply;
53 reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
54 entry->replaceHttpReply(reply);
55 entry->append(msgbuf, strlen(msgbuf));
56 entry->complete();
57 } else if (upath.startsWith(mgrPfx)) {
58 debugs(17, 2, "calling CacheManager due to URL-path " << mgrPfx);
59 CacheManager::GetInstance()->Start(clientConn, request, entry);
60 } else {
61 debugObj(76, 1, "internalStart: unknown request:\n",
62 request, (ObjPackMethod) & httpRequestPack);
63 err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request);
64 errorAppendEntry(entry, err);
65 }
66 }
67
68 bool
69 internalCheck(const SBuf &urlPath)
70 {
71 static const SBuf InternalPfx("/squid-internal-");
72 return urlPath.startsWith(InternalPfx);
73 }
74
75 bool
76 internalStaticCheck(const SBuf &urlPath)
77 {
78 static const SBuf InternalStaticPfx("/squid-internal-static");
79 return urlPath.startsWith(InternalStaticPfx);
80 }
81
82 /*
83 * makes internal url with a given host and port (remote internal url)
84 */
85 char *
86 internalRemoteUri(const char *host, unsigned short port, const char *dir, const SBuf &name)
87 {
88 static char lc_host[SQUIDHOSTNAMELEN];
89 assert(host && !name.isEmpty());
90 /* convert host name to lower case */
91 xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
92 Tolower(lc_host);
93
94 /* check for an IP address and format appropriately if found */
95 Ip::Address test = lc_host;
96 if ( !test.isAnyAddr() ) {
97 test.toHostStr(lc_host,SQUIDHOSTNAMELEN);
98 }
99
100 /*
101 * append the domain in order to mirror the requests with appended
102 * domains
103 */
104
105 /* For IPv6 addresses also check for a colon */
106 if (Config.appendDomain && !strchr(lc_host, '.') && !strchr(lc_host, ':'))
107 strncat(lc_host, Config.appendDomain, SQUIDHOSTNAMELEN -
108 strlen(lc_host) - 1);
109
110 /* build URI */
111 URL tmp(AnyP::PROTO_HTTP);
112 tmp.host(lc_host);
113 if (port)
114 tmp.port(port);
115
116 static MemBuf mb;
117
118 mb.reset();
119 mb.appendf("http://" SQUIDSBUFPH, SQUIDSBUFPRINT(tmp.authority()));
120
121 if (dir)
122 mb.append(dir, strlen(dir));
123
124 mb.append(name.rawContent(), name.length());
125
126 /* return a pointer to a local static buffer */
127 return mb.buf;
128 }
129
130 /*
131 * makes internal url with local host and port
132 */
133 char *
134 internalLocalUri(const char *dir, const SBuf &name)
135 {
136 return internalRemoteUri(getMyHostname(),
137 getMyPort(), dir, name);
138 }
139
140 const char *
141 internalHostname(void)
142 {
143 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
144 xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN);
145
146 /* For IPv6 addresses also check for a colon */
147 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
148 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
149 strlen(host) - 1);
150
151 Tolower(host);
152
153 return host;
154 }
155
156 int
157 internalHostnameIs(const char *arg)
158 {
159 wordlist *w;
160
161 if (0 == strcmp(arg, internalHostname()))
162 return 1;
163
164 for (w = Config.hostnameAliases; w; w = w->next)
165 if (0 == strcmp(arg, w->key))
166 return 1;
167
168 return 0;
169 }
170