internal.cc
Go to the documentation of this file.
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 "internal.h"
20 #include "MemBuf.h"
21 #include "SquidConfig.h"
22 #include "SquidTime.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  */
32 void
34 {
35  ErrorState *err;
36  const SBuf upath = request->url.path();
37  debugs(76, 3, clientConn << " requesting '" << upath << "'");
38 
39  static const SBuf netdbUri("/squid-internal-dynamic/netdb");
40  static const SBuf storeDigestUri("/squid-internal-periodic/store_digest");
41  static const SBuf mgrPfx("/squid-internal-mgr/");
42 
43  if (upath == netdbUri) {
44  netdbBinaryExchange(entry);
45  } else if (upath == storeDigestUri) {
46 #if USE_CACHE_DIGESTS
47  const char *msgbuf = "This cache is currently building its digest.\n";
48 #else
49 
50  const char *msgbuf = "This cache does not support Cache Digests.\n";
51 #endif
52 
53  HttpReply *reply = new HttpReply;
54  reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
55  entry->replaceHttpReply(reply);
56  entry->append(msgbuf, strlen(msgbuf));
57  entry->complete();
58  } else if (upath.startsWith(mgrPfx)) {
59  debugs(17, 2, "calling CacheManager due to URL-path " << mgrPfx);
60  CacheManager::GetInstance()->start(clientConn, request, entry, ale);
61  } else {
62  debugObj(76, 1, "internalStart: unknown request:\n",
65  errorAppendEntry(entry, err);
66  }
67 }
68 
69 bool
70 internalCheck(const SBuf &urlPath)
71 {
72  static const SBuf InternalPfx("/squid-internal-");
73  return urlPath.startsWith(InternalPfx);
74 }
75 
76 bool
77 internalStaticCheck(const SBuf &urlPath)
78 {
79  static const SBuf InternalStaticPfx("/squid-internal-static");
80  return urlPath.startsWith(InternalStaticPfx);
81 }
82 
83 /*
84  * makes internal url with a given host and port (remote internal url)
85  */
86 char *
87 internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
88 {
89  static char lc_host[SQUIDHOSTNAMELEN];
90  assert(host && !name.isEmpty());
91  /* convert host name to lower case */
92  xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
93  Tolower(lc_host);
94 
95  /* check for an IP address and format appropriately if found */
96  Ip::Address test = lc_host;
97  if ( !test.isAnyAddr() ) {
98  test.toHostStr(lc_host,SQUIDHOSTNAMELEN);
99  }
100 
101  /*
102  * append the domain in order to mirror the requests with appended
103  * domains. If that fails, just use the hostname anyway.
104  */
105  (void)urlAppendDomain(lc_host);
106 
107  /* build URI */
109  tmp.host(lc_host);
110  if (port)
111  tmp.port(port);
112 
113  static MemBuf mb;
114 
115  mb.reset();
116  mb.appendf("%s://" SQUIDSBUFPH, encrypt ? "https" : "http", SQUIDSBUFPRINT(tmp.authority()));
117 
118  if (dir)
119  mb.append(dir, strlen(dir));
120 
121  mb.append(name.rawContent(), name.length());
122 
123  /* return a pointer to a local static buffer */
124  return mb.buf;
125 }
126 
127 /*
128  * makes internal url with local host and port
129  */
130 char *
131 internalLocalUri(const char *dir, const SBuf &name)
132 {
133  // XXX: getMy*() may return https_port info, but we force http URIs
134  // because we have not checked whether the callers can handle https.
135  const bool secure = false;
136  return internalRemoteUri(secure, getMyHostname(),
137  getMyPort(), dir, name);
138 }
139 
140 const char *
142 {
143  LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
145 
146  /* For IPv6 addresses also check for a colon */
147  if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
148  strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
149  strlen(host) - 1);
150 
151  Tolower(host);
152 
153  return host;
154 }
155 
156 int
157 internalHostnameIs(const char *arg)
158 {
159  wordlist *w;
160 
161  if (0 == strcmp(arg, internalHostname()))
162  return 1;
163 
164  for (w = Config.hostnameAliases; w; w = w->next)
165  if (0 == strcmp(arg, w->key))
166  return 1;
167 
168  return 0;
169 }
170 
char * buf
Definition: MemBuf.h:134
int getMyPort(void)
Definition: tools.cc:1008
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
void port(unsigned short p)
Definition: Uri.h:94
Definition: Uri.h:31
#define LOCAL_ARRAY(type, name, size)
Definition: squid.h:75
void errorAppendEntry(StoreEntry *entry, ErrorState *err)
Definition: errorpage.cc:720
bool isEmpty() const
Definition: SBuf.h:424
wordlist * hostnameAliases
Definition: SquidConfig.h:228
bool isAnyAddr() const
Definition: Address.cc:170
Definition: SBuf.h:87
void internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest *request, StoreEntry *entry, const AccessLogEntry::Pointer &ale)
Definition: internal.cc:33
void httpRequestPack(void *obj, Packable *p)
Definition: HttpRequest.cc:362
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:211
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:1722
static CacheManager * GetInstance()
SQUIDCEXTERN void Tolower(char *)
Definition: util.c:28
static int port
Definition: ldap_backend.cc:69
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:842
char * internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name)
Definition: internal.cc:87
void(* ObjPackMethod)(void *obj, Packable *p)
Definition: tools.h:33
const char * rawContent() const
Definition: SBuf.cc:509
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void start(const Comm::ConnectionPointer &client, HttpRequest *request, StoreEntry *entry, const AccessLogEntryPointer &ale)
Definition: MemBuf.h:24
const char * internalHostname(void)
Definition: internal.cc:141
#define assert(EX)
Definition: assert.h:19
int internalHostnameIs(const char *arg)
Definition: internal.cc:157
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:565
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:408
time_t squid_curtime
Definition: stub_time.cc:17
void encrypt(char *nachr, int decr)
Definition: encrypt.c:188
wordlist * next
Definition: wordlist.h:33
void complete()
Definition: store.cc:1045
bool urlAppendDomain(char *host)
apply append_domain config to the given hostname
Definition: Uri.cc:224
@ scNotFound
Definition: StatusCode.h:48
@ PROTO_HTTP
Definition: ProtocolType.h:25
char * key
Definition: wordlist.h:32
char * url
Definition: tcp-banger2.c:114
const char * getMyHostname(void)
Definition: tools.cc:414
@ ERR_INVALID_REQ
Definition: forward.h:43
bool internalCheck(const SBuf &urlPath)
Definition: internal.cc:70
void reset()
Definition: MemBuf.cc:131
void netdbBinaryExchange(StoreEntry *s)
Definition: net_db.cc:1203
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:817
void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm)
Definition: tools.cc:905
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:168
char * appendDomain
Definition: SquidConfig.h:220
bool internalStaticCheck(const SBuf &urlPath)
Definition: internal.cc:77
char * internalLocalUri(const char *dir, const SBuf &name)
Definition: internal.cc:131
void host(const char *src)
Definition: Uri.cc:98
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
#define SQUIDSBUFPH
Definition: SBuf.h:31
class SquidConfig Config
Definition: SquidConfig.cc:12

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors