internal.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2022 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 "internal.h"
20#include "MemBuf.h"
21#include "SquidConfig.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 */
31void
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) {
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",
64 errorAppendEntry(entry, err);
65 }
66}
67
68bool
69internalCheck(const SBuf &urlPath)
70{
71 static const SBuf InternalPfx("/squid-internal-");
72 return urlPath.startsWith(InternalPfx);
73}
74
75bool
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 */
85char *
86internalRemoteUri(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 */
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 */
129char *
130internalLocalUri(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
139const char *
141{
142 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
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
155int
156internalHostnameIs(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
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:225
#define assert(EX)
Definition: assert.h:19
Definition: Uri.h:31
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:566
void port(unsigned short p)
Definition: Uri.h:94
void host(const char *src)
Definition: Uri.cc:99
static CacheManager * GetInstance()
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:167
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
virtual void append(const char *c, int sz)
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:220
wordlist * hostnameAliases
Definition: SquidConfig.h:228
void complete()
Definition: store.cc:1004
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:776
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1672
char * key
Definition: wordlist.h:32
wordlist * next
Definition: wordlist.h:33
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:196
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:719
bool internalStaticCheck(const SBuf &urlPath)
Definition: internal.cc:76
char * internalLocalUri(const char *dir, const SBuf &name)
Definition: internal.cc:130
bool internalCheck(const SBuf &urlPath)
Definition: internal.cc:69
char * internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
Definition: internal.cc:86
const char * internalHostname(void)
Definition: internal.cc:140
void internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest *request, StoreEntry *entry, const AccessLogEntry::Pointer &ale)
Definition: internal.cc:32
int internalHostnameIs(const char *arg)
Definition: internal.cc:156
@ PROTO_HTTP
Definition: ProtocolType.h:25
@ scNotFound
Definition: StatusCode.h:48
void netdbBinaryExchange(StoreEntry *s)
Definition: net_db.cc:1170
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:68
char * url
Definition: tcp-banger2.c:114
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
const char * getMyHostname(void)
Definition: tools.cc:464
int getMyPort(void)
Definition: tools.cc:1038
void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm)
Definition: tools.cc:935
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