Squid Web Cache master
Loading...
Searching...
No Matches
internal.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 */
31void
33{
34 ErrorState *err;
35
36 Assure(request);
37 const SBuf upath = request->url.absolutePath();
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) {
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
71bool
72internalCheck(const SBuf &urlPath)
73{
74 static const SBuf InternalPfx("/squid-internal-");
75 return urlPath.startsWith(InternalPfx);
76}
77
78bool
80{
81 static const SBuf InternalStaticPfx("/squid-internal-static");
82 return urlPath.startsWith(InternalStaticPfx);
83}
84
85bool
87{
89}
90
91/*
92 * makes internal url with a given host and port (remote internal url)
93 */
94char *
95internalRemoteUri(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 */
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 */
138char *
139internalLocalUri(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
148const char *
150{
151 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
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
164bool
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
#define Assure(condition)
Definition Assure.h:35
void httpRequestPack(void *obj, Packable *p)
time_t squid_curtime
#define SQUIDSBUFPH
Definition SBuf.h:31
#define SQUIDSBUFPRINT(s)
Definition SBuf.h:32
class SquidConfig Config
bool urlAppendDomain(char *host)
apply append_domain config to the given hostname
Definition Uri.cc:297
#define assert(EX)
Definition assert.h:17
SBuf & authority(bool requirePort=false) const
Definition Uri.cc:721
SBuf & absolutePath() const
RFC 3986 section 4.2 relative reference called 'absolute-path'.
Definition Uri.cc:775
void port(const Port p)
reset authority port subcomponent
Definition Uri.h:90
void host(const char *src)
Definition Uri.cc:154
static CacheManager * GetInstance()
static const SBuf & WellKnownUrlPathPrefix()
initial URL path characters that identify cache manager requests
void start(const Comm::ConnectionPointer &client, HttpRequest *request, StoreEntry *entry, const AccessLogEntryPointer &ale)
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition HttpReply.cc:170
RequestFlags flags
AnyP::Uri url
the request URI
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition Address.cc:854
bool isAnyAddr() const
Definition Address.cc:190
void append(const char *c, int sz) override
Definition MemBuf.cc:209
char * buf
Definition MemBuf.h:134
void reset()
Definition MemBuf.cc:129
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition Packable.h:61
Definition SBuf.h:94
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition SBuf.h:287
const char * rawContent() const
Definition SBuf.cc:509
size_type length() const
Returns the number of bytes stored in SBuf.
Definition SBuf.h:419
bool isEmpty() const
Definition SBuf.h:435
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition SBuf.cc:442
char * appendDomain
SBufList hostnameAliases
void complete()
Definition store.cc:1031
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition store.cc:1705
void append(char const *, int) override
Appends a c-string to existing packed data.
Definition store.cc:803
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
static int port
void encrypt(char *nachr, int decr)
Definition encrypt.c:188
@ ERR_INVALID_REQ
Definition forward.h:43
void errorAppendEntry(StoreEntry *entry, ErrorState *err)
Definition errorpage.cc:738
bool ForSomeCacheManager(const SBuf &urlPath)
Definition internal.cc:86
bool internalStaticCheck(const SBuf &urlPath)
Definition internal.cc:79
char * internalLocalUri(const char *dir, const SBuf &name)
Definition internal.cc:139
bool internalCheck(const SBuf &urlPath)
Definition internal.cc:72
char * internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
Definition internal.cc:95
const char * internalHostname(void)
Definition internal.cc:149
void internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest *request, StoreEntry *entry, const AccessLogEntry::Pointer &ale)
Definition internal.cc:32
bool internalHostnameIs(const SBuf &arg)
Definition internal.cc:165
@ PROTO_HTTP
@ scNotFound
Definition StatusCode.h:49
void netdbBinaryExchange(StoreEntry *s)
Definition net_db.cc:1091
#define SQUIDHOSTNAMELEN
Definition rfc2181.h:30
#define LOCAL_ARRAY(type, name, size)
Definition squid.h:62
const char * getMyHostname(void)
Definition tools.cc:468
int getMyPort(void)
Definition tools.cc:1042
void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm)
Definition tools.cc:939
void(* ObjPackMethod)(void *obj, Packable *p)
Definition tools.h:33
void Tolower(char *)
Definition util.cc:28
char * xstrncpy(char *dst, const char *src, size_t n)
Definition xstring.cc:37