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 
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 {
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 */
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 *
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 
164 bool
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 
char * buf
Definition: MemBuf.h:134
SBufList hostnameAliases
Definition: SquidConfig.h:230
int getMyPort(void)
Definition: tools.cc:1041
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
AnyP::Uri url
the request URI
Definition: HttpRequest.h:115
Definition: Uri.h:31
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:62
void errorAppendEntry(StoreEntry *entry, ErrorState *err)
Definition: errorpage.cc:738
bool isEmpty() const
Definition: SBuf.h:435
RequestFlags flags
Definition: HttpRequest.h:141
void append(char const *, int) override
Appends a c-string to existing packed data.
Definition: store.cc:803
bool isAnyAddr() const
Definition: Address.cc:190
Definition: SBuf.h:93
void internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest *request, StoreEntry *entry, const AccessLogEntry::Pointer &ale)
Definition: internal.cc:32
void httpRequestPack(void *obj, Packable *p)
Definition: HttpRequest.cc:361
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:442
void replaceHttpReply(const HttpReplyPointer &, const bool andStartWriting=true)
Definition: store.cc:1705
static CacheManager * GetInstance()
bool internalHostnameIs(const SBuf &arg)
Definition: internal.cc:165
static int port
Definition: ldap_backend.cc:70
bool ForSomeCacheManager(const SBuf &urlPath)
Definition: internal.cc:86
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:862
static const SBuf & WellKnownUrlPathPrefix()
initial URL path characters that identify cache manager requests
void Tolower(char *)
Definition: util.cc:28
char * internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
Definition: internal.cc:95
void(* ObjPackMethod)(void *obj, Packable *p)
Definition: tools.h:33
void append(const char *c, int sz) override
Definition: MemBuf.cc:209
const char * rawContent() const
Definition: SBuf.cc:509
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
void start(const Comm::ConnectionPointer &client, HttpRequest *request, StoreEntry *entry, const AccessLogEntryPointer &ale)
Definition: MemBuf.h:23
const char * internalHostname(void)
Definition: internal.cc:149
#define assert(EX)
Definition: assert.h:17
#define Assure(condition)
Definition: Assure.h:35
void port(const Port p)
reset authority port subcomponent
Definition: Uri.h:90
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:689
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:419
time_t squid_curtime
Definition: stub_libtime.cc:20
void encrypt(char *nachr, int decr)
Definition: encrypt.c:188
void complete()
Definition: store.cc:1031
bool urlAppendDomain(char *host)
apply append_domain config to the given hostname
Definition: Uri.cc:266
void path(const char *p)
Definition: Uri.h:96
@ scNotFound
Definition: StatusCode.h:49
@ PROTO_HTTP
Definition: ProtocolType.h:25
const char * getMyHostname(void)
Definition: tools.cc:467
@ ERR_INVALID_REQ
Definition: forward.h:43
bool internalCheck(const SBuf &urlPath)
Definition: internal.cc:72
void reset()
Definition: MemBuf.cc:129
void netdbBinaryExchange(StoreEntry *s)
Definition: net_db.cc:1091
int caseCmp(const SBuf &S, const size_type n) const
shorthand version for case-insensitive compare()
Definition: SBuf.h:287
void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm)
Definition: tools.cc:938
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:170
char * appendDomain
Definition: SquidConfig.h:222
bool internalStaticCheck(const SBuf &urlPath)
Definition: internal.cc:79
char * internalLocalUri(const char *dir, const SBuf &name)
Definition: internal.cc:139
void host(const char *src)
Definition: Uri.cc:123
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
#define SQUIDSBUFPH
Definition: SBuf.h:31
class SquidConfig Config
Definition: SquidConfig.cc:12

 

Introduction

Documentation

Support

Miscellaneous