]> git.ipfire.org Git - thirdparty/squid.git/blob - src/internal.cc
Source Format Enforcement (#763)
[thirdparty/squid.git] / src / internal.cc
1 /*
2 * Copyright (C) 1996-2021 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 "CacheManager.h"
14 #include "comm/Connection.h"
15 #include "errorpage.h"
16 #include "HttpReply.h"
17 #include "HttpRequest.h"
18 #include "icmp/net_db.h"
19 #include "MemBuf.h"
20 #include "SquidConfig.h"
21 #include "SquidTime.h"
22 #include "Store.h"
23 #include "tools.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, const AccessLogEntry::Pointer &ale)
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, ale);
60 } else {
61 debugObj(76, 1, "internalStart: unknown request:\n",
62 request, (ObjPackMethod) & httpRequestPack);
63 err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request, ale);
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(bool encrypt, 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. If that fails, just use the hostname anyway.
103 */
104 (void)urlAppendDomain(lc_host);
105
106 /* build URI */
107 AnyP::Uri tmp(AnyP::PROTO_HTTP);
108 tmp.host(lc_host);
109 if (port)
110 tmp.port(port);
111
112 static MemBuf mb;
113
114 mb.reset();
115 mb.appendf("%s://" SQUIDSBUFPH, encrypt ? "https" : "http", SQUIDSBUFPRINT(tmp.authority()));
116
117 if (dir)
118 mb.append(dir, strlen(dir));
119
120 mb.append(name.rawContent(), name.length());
121
122 /* return a pointer to a local static buffer */
123 return mb.buf;
124 }
125
126 /*
127 * makes internal url with local host and port
128 */
129 char *
130 internalLocalUri(const char *dir, const SBuf &name)
131 {
132 // XXX: getMy*() may return https_port info, but we force http URIs
133 // because we have not checked whether the callers can handle https.
134 const bool secure = false;
135 return internalRemoteUri(secure, getMyHostname(),
136 getMyPort(), dir, name);
137 }
138
139 const char *
140 internalHostname(void)
141 {
142 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
143 xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN);
144
145 /* For IPv6 addresses also check for a colon */
146 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
147 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
148 strlen(host) - 1);
149
150 Tolower(host);
151
152 return host;
153 }
154
155 int
156 internalHostnameIs(const char *arg)
157 {
158 wordlist *w;
159
160 if (0 == strcmp(arg, internalHostname()))
161 return 1;
162
163 for (w = Config.hostnameAliases; w; w = w->next)
164 if (0 == strcmp(arg, w->key))
165 return 1;
166
167 return 0;
168 }
169