internal.cc
Go to the documentation of this file.
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#include "wordlist.h"
27
28/* called when we "miss" on an internal object;
29 * generate known dynamic objects,
30 * return Http::scNotFound for others
31 */
32void
34{
35 ErrorState *err;
36
37 Assure(request);
38 const SBuf upath = request->url.path();
39 debugs(76, 3, clientConn << " requesting '" << upath << "'");
40
41 Assure(request->flags.internal);
42
43 static const SBuf netdbUri("/squid-internal-dynamic/netdb");
44 static const SBuf storeDigestUri("/squid-internal-periodic/store_digest");
45
46 if (upath == netdbUri) {
48 } else if (upath == storeDigestUri) {
49#if USE_CACHE_DIGESTS
50 const char *msgbuf = "This cache is currently building its digest.\n";
51#else
52
53 const char *msgbuf = "This cache does not support Cache Digests.\n";
54#endif
55
56 HttpReply *reply = new HttpReply;
57 reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
58 entry->replaceHttpReply(reply);
59 entry->append(msgbuf, strlen(msgbuf));
60 entry->complete();
61 } else if (ForSomeCacheManager(upath)) {
62 debugs(17, 2, "calling CacheManager due to URL-path");
63 CacheManager::GetInstance()->start(clientConn, request, entry, ale);
64 } else {
65 debugObj(76, 1, "internalStart: unknown request:\n",
66 request, (ObjPackMethod) & httpRequestPack);
67 err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request, ale);
68 errorAppendEntry(entry, err);
69 }
70}
71
72bool
73internalCheck(const SBuf &urlPath)
74{
75 static const SBuf InternalPfx("/squid-internal-");
76 return urlPath.startsWith(InternalPfx);
77}
78
79bool
81{
82 static const SBuf InternalStaticPfx("/squid-internal-static");
83 return urlPath.startsWith(InternalStaticPfx);
84}
85
86bool
88{
90}
91
92/*
93 * makes internal url with a given host and port (remote internal url)
94 */
95char *
96internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
97{
98 static char lc_host[SQUIDHOSTNAMELEN];
99 assert(host && !name.isEmpty());
100 /* convert host name to lower case */
101 xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
102 Tolower(lc_host);
103
104 /* check for an IP address and format appropriately if found */
105 Ip::Address test = lc_host;
106 if ( !test.isAnyAddr() ) {
107 test.toHostStr(lc_host,SQUIDHOSTNAMELEN);
108 }
109
110 /*
111 * append the domain in order to mirror the requests with appended
112 * domains. If that fails, just use the hostname anyway.
113 */
114 (void)urlAppendDomain(lc_host);
115
116 /* build URI */
118 tmp.host(lc_host);
119 if (port)
120 tmp.port(port);
121
122 static MemBuf mb;
123
124 mb.reset();
125 mb.appendf("%s://" SQUIDSBUFPH, encrypt ? "https" : "http", SQUIDSBUFPRINT(tmp.authority()));
126
127 if (dir)
128 mb.append(dir, strlen(dir));
129
130 mb.append(name.rawContent(), name.length());
131
132 /* return a pointer to a local static buffer */
133 return mb.buf;
134}
135
136/*
137 * makes internal url with local host and port
138 */
139char *
140internalLocalUri(const char *dir, const SBuf &name)
141{
142 // XXX: getMy*() may return https_port info, but we force http URIs
143 // because we have not checked whether the callers can handle https.
144 const bool secure = false;
145 return internalRemoteUri(secure, getMyHostname(),
146 getMyPort(), dir, name);
147}
148
149const char *
151{
152 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
154
155 /* For IPv6 addresses also check for a colon */
156 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
157 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
158 strlen(host) - 1);
159
160 Tolower(host);
161
162 return host;
163}
164
165int
166internalHostnameIs(const char *arg)
167{
168 wordlist *w;
169
170 if (0 == strcmp(arg, internalHostname()))
171 return 1;
172
173 for (w = Config.hostnameAliases; w; w = w->next)
174 if (0 == strcmp(arg, w->key))
175 return 1;
176
177 return 0;
178}
179
#define Assure(condition)
Definition: Assure.h:35
void httpRequestPack(void *obj, Packable *p)
Definition: HttpRequest.cc:361
time_t squid_curtime
Definition: stub_libtime.cc:20
#define SQUIDSBUFPH
Definition: SBuf.h:31
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
class SquidConfig Config
Definition: SquidConfig.cc:12
bool urlAppendDomain(char *host)
apply append_domain config to the given hostname
Definition: Uri.cc:223
#define assert(EX)
Definition: assert.h:17
Definition: Uri.h:31
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:646
void path(const char *p)
Definition: Uri.h:101
void port(const Port p)
reset authority port subcomponent
Definition: Uri.h:95
void host(const char *src)
Definition: Uri.cc:100
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
Definition: HttpRequest.h:141
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:842
bool isAnyAddr() const
Definition: Address.cc:170
Definition: MemBuf.h:24
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
const char * rawContent() const
Definition: SBuf.cc:509
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:415
bool isEmpty() const
Definition: SBuf.h:431
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:442
char * appendDomain
Definition: SquidConfig.h:222
wordlist * hostnameAliases
Definition: SquidConfig.h:230
void complete()
Definition: store.cc:1017
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1691
void append(char const *, int) override
Appends a c-string to existing packed data.
Definition: store.cc:789
char * key
Definition: wordlist.h:32
wordlist * next
Definition: wordlist.h:33
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
static int port
Definition: ldap_backend.cc:70
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:717
bool ForSomeCacheManager(const SBuf &urlPath)
Definition: internal.cc:87
bool internalStaticCheck(const SBuf &urlPath)
Definition: internal.cc:80
char * internalLocalUri(const char *dir, const SBuf &name)
Definition: internal.cc:140
bool internalCheck(const SBuf &urlPath)
Definition: internal.cc:73
char * internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
Definition: internal.cc:96
const char * internalHostname(void)
Definition: internal.cc:150
void internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest *request, StoreEntry *entry, const AccessLogEntry::Pointer &ale)
Definition: internal.cc:33
int internalHostnameIs(const char *arg)
Definition: internal.cc:166
@ PROTO_HTTP
Definition: ProtocolType.h:25
@ scNotFound
Definition: StatusCode.h:48
void netdbBinaryExchange(StoreEntry *s)
Definition: net_db.cc:1107
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:68
const char * getMyHostname(void)
Definition: tools.cc:467
int getMyPort(void)
Definition: tools.cc:1041
void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm)
Definition: tools.cc:938
void(* ObjPackMethod)(void *obj, Packable *p)
Definition: tools.h:33
SQUIDCEXTERN void Tolower(char *)
Definition: util.c:28
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors