]> git.ipfire.org Git - thirdparty/squid.git/blob - src/internal.cc
NoNewGlobals for MapLabel (#1746)
[thirdparty/squid.git] / src / internal.cc
1 /*
2 * Copyright (C) 1996-2023 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 "AccessLogEntry.h"
13 #include "base/Assure.h"
14 #include "CacheManager.h"
15 #include "comm/Connection.h"
16 #include "errorpage.h"
17 #include "HttpReply.h"
18 #include "HttpRequest.h"
19 #include "icmp/net_db.h"
20 #include "internal.h"
21 #include "MemBuf.h"
22 #include "SquidConfig.h"
23 #include "Store.h"
24 #include "tools.h"
25 #include "util.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, const AccessLogEntry::Pointer &ale)
33 {
34 ErrorState *err;
35
36 Assure(request);
37 const SBuf upath = request->url.path();
38 debugs(76, 3, clientConn << " requesting '" << upath << "'");
39
40 Assure(request->flags.internal);
41
42 static const SBuf netdbUri("/squid-internal-dynamic/netdb");
43 static const SBuf storeDigestUri("/squid-internal-periodic/store_digest");
44
45 if (upath == netdbUri) {
46 netdbBinaryExchange(entry);
47 } else if (upath == storeDigestUri) {
48 #if USE_CACHE_DIGESTS
49 const char *msgbuf = "This cache is currently building its digest.\n";
50 #else
51
52 const char *msgbuf = "This cache does not support Cache Digests.\n";
53 #endif
54
55 HttpReply *reply = new HttpReply;
56 reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
57 entry->replaceHttpReply(reply);
58 entry->append(msgbuf, strlen(msgbuf));
59 entry->complete();
60 } else if (ForSomeCacheManager(upath)) {
61 debugs(17, 2, "calling CacheManager due to URL-path");
62 CacheManager::GetInstance()->start(clientConn, request, entry, ale);
63 } else {
64 debugObj(76, 1, "internalStart: unknown request:\n",
65 request, (ObjPackMethod) & httpRequestPack);
66 err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request, ale);
67 errorAppendEntry(entry, err);
68 }
69 }
70
71 bool
72 internalCheck(const SBuf &urlPath)
73 {
74 static const SBuf InternalPfx("/squid-internal-");
75 return urlPath.startsWith(InternalPfx);
76 }
77
78 bool
79 internalStaticCheck(const SBuf &urlPath)
80 {
81 static const SBuf InternalStaticPfx("/squid-internal-static");
82 return urlPath.startsWith(InternalStaticPfx);
83 }
84
85 bool
86 ForSomeCacheManager(const SBuf &urlPath)
87 {
88 return urlPath.startsWith(CacheManager::WellKnownUrlPathPrefix());
89 }
90
91 /*
92 * makes internal url with a given host and port (remote internal url)
93 */
94 char *
95 internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
96 {
97 static char lc_host[SQUIDHOSTNAMELEN];
98 assert(host && !name.isEmpty());
99 /* convert host name to lower case */
100 xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
101 Tolower(lc_host);
102
103 /* check for an IP address and format appropriately if found */
104 Ip::Address test = lc_host;
105 if ( !test.isAnyAddr() ) {
106 test.toHostStr(lc_host,SQUIDHOSTNAMELEN);
107 }
108
109 /*
110 * append the domain in order to mirror the requests with appended
111 * domains. If that fails, just use the hostname anyway.
112 */
113 (void)urlAppendDomain(lc_host);
114
115 /* build URI */
116 AnyP::Uri tmp(AnyP::PROTO_HTTP);
117 tmp.host(lc_host);
118 if (port)
119 tmp.port(port);
120
121 static MemBuf mb;
122
123 mb.reset();
124 mb.appendf("%s://" SQUIDSBUFPH, encrypt ? "https" : "http", SQUIDSBUFPRINT(tmp.authority()));
125
126 if (dir)
127 mb.append(dir, strlen(dir));
128
129 mb.append(name.rawContent(), name.length());
130
131 /* return a pointer to a local static buffer */
132 return mb.buf;
133 }
134
135 /*
136 * makes internal url with local host and port
137 */
138 char *
139 internalLocalUri(const char *dir, const SBuf &name)
140 {
141 // XXX: getMy*() may return https_port info, but we force http URIs
142 // because we have not checked whether the callers can handle https.
143 const bool secure = false;
144 return internalRemoteUri(secure, getMyHostname(),
145 getMyPort(), dir, name);
146 }
147
148 const char *
149 internalHostname(void)
150 {
151 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
152 xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN);
153
154 /* For IPv6 addresses also check for a colon */
155 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
156 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
157 strlen(host) - 1);
158
159 Tolower(host);
160
161 return host;
162 }
163
164 bool
165 internalHostnameIs(const SBuf &arg)
166 {
167 if (arg.caseCmp(internalHostname()) == 0)
168 return true;
169
170 for (const auto &w : Config.hostnameAliases) {
171 if (w.caseCmp(arg) == 0)
172 return true;
173 }
174
175 return false;
176 }
177