internal.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2018 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 "CacheManager.h"
13 #include "comm/Connection.h"
14 #include "errorpage.h"
15 #include "HttpReply.h"
16 #include "HttpRequest.h"
17 #include "icmp/net_db.h"
18 #include "MemBuf.h"
19 #include "SquidConfig.h"
20 #include "SquidTime.h"
21 #include "Store.h"
22 #include "tools.h"
23 #include "util.h"
24 #include "wordlist.h"
25 
26 /* called when we "miss" on an internal object;
27  * generate known dynamic objects,
28  * return Http::scNotFound for others
29  */
30 void
32 {
33  ErrorState *err;
34  const SBuf upath = request->url.path();
35  debugs(76, 3, clientConn << " requesting '" << upath << "'");
36 
37  static const SBuf netdbUri("/squid-internal-dynamic/netdb");
38  static const SBuf storeDigestUri("/squid-internal-periodic/store_digest");
39  static const SBuf mgrPfx("/squid-internal-mgr/");
40 
41  if (upath == netdbUri) {
42  netdbBinaryExchange(entry);
43  } else if (upath == storeDigestUri) {
44 #if USE_CACHE_DIGESTS
45  const char *msgbuf = "This cache is currently building its digest.\n";
46 #else
47 
48  const char *msgbuf = "This cache does not support Cache Digests.\n";
49 #endif
50 
51  HttpReply *reply = new HttpReply;
52  reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
53  entry->replaceHttpReply(reply);
54  entry->append(msgbuf, strlen(msgbuf));
55  entry->complete();
56  } else if (upath.startsWith(mgrPfx)) {
57  debugs(17, 2, "calling CacheManager due to URL-path " << mgrPfx);
58  CacheManager::GetInstance()->Start(clientConn, request, entry);
59  } else {
60  debugObj(76, 1, "internalStart: unknown request:\n",
61  request, (ObjPackMethod) & httpRequestPack);
62  err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request);
63  errorAppendEntry(entry, err);
64  }
65 }
66 
67 bool
68 internalCheck(const SBuf &urlPath)
69 {
70  static const SBuf InternalPfx("/squid-internal-");
71  return urlPath.startsWith(InternalPfx);
72 }
73 
74 bool
75 internalStaticCheck(const SBuf &urlPath)
76 {
77  static const SBuf InternalStaticPfx("/squid-internal-static");
78  return urlPath.startsWith(InternalStaticPfx);
79 }
80 
81 /*
82  * makes internal url with a given host and port (remote internal url)
83  */
84 char *
85 internalRemoteUri(const char *host, unsigned short port, const char *dir, const SBuf &name)
86 {
87  static char lc_host[SQUIDHOSTNAMELEN];
88  assert(host && !name.isEmpty());
89  /* convert host name to lower case */
90  xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
91  Tolower(lc_host);
92 
93  /* check for an IP address and format appropriately if found */
94  Ip::Address test = lc_host;
95  if ( !test.isAnyAddr() ) {
96  test.toHostStr(lc_host,SQUIDHOSTNAMELEN);
97  }
98 
99  /*
100  * append the domain in order to mirror the requests with appended
101  * domains
102  */
103 
104  /* For IPv6 addresses also check for a colon */
105  if (Config.appendDomain && !strchr(lc_host, '.') && !strchr(lc_host, ':'))
106  strncat(lc_host, Config.appendDomain, SQUIDHOSTNAMELEN -
107  strlen(lc_host) - 1);
108 
109  /* build URI */
111  tmp.host(lc_host);
112  if (port)
113  tmp.port(port);
114 
115  static MemBuf mb;
116 
117  mb.reset();
118  mb.appendf("http://" SQUIDSBUFPH, SQUIDSBUFPRINT(tmp.authority()));
119 
120  if (dir)
121  mb.append(dir, strlen(dir));
122 
123  mb.append(name.rawContent(), name.length());
124 
125  /* return a pointer to a local static buffer */
126  return mb.buf;
127 }
128 
129 /*
130  * makes internal url with local host and port
131  */
132 char *
133 internalLocalUri(const char *dir, const SBuf &name)
134 {
136  getMyPort(), dir, name);
137 }
138 
139 const 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 
155 int
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 
int internalHostnameIs(const char *arg)
Definition: internal.cc:156
#define assert(EX)
Definition: assert.h:17
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:216
bool internalCheck(const SBuf &urlPath)
Definition: internal.cc:68
void path(const char *p)
Definition: Uri.h:86
Definition: SBuf.h:86
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:823
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
char * key
Definition: wordlist.h:33
char * appendDomain
Definition: SquidConfig.h:215
bool isEmpty() const
Definition: SBuf.h:420
SQUIDCEXTERN void Tolower(char *)
Definition: util.c:28
const char * getMyHostname(void)
Definition: tools.cc:412
time_t squid_curtime
Definition: stub_time.cc:17
bool isAnyAddr() const
Definition: Address.cc:163
void Start(const Comm::ConnectionPointer &client, HttpRequest *request, StoreEntry *entry)
void replaceHttpReply(HttpReply *, bool andStartWriting=true)
Definition: store.cc:1782
static CacheManager * GetInstance()
void complete()
Definition: store.cc:1058
void EVH void * arg
Definition: stub_event.cc:16
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:404
int getMyPort(void)
Definition: tools.cc:1003
void port(unsigned short p)
Definition: Uri.h:83
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:124
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:161
void reset()
Definition: MemBuf.cc:132
AnyP::Uri url
the request URI
Definition: HttpRequest.h:107
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
const char * internalHostname(void)
Definition: internal.cc:140
void host(const char *src)
Definition: Uri.cc:47
static int port
Definition: ldap_backend.cc:69
#define LOCAL_ARRAY(type, name, size)
Definition: leakcheck.h:18
char * internalLocalUri(const char *dir, const SBuf &name)
Definition: internal.cc:133
wordlist * hostnameAliases
Definition: SquidConfig.h:223
char * buf
Definition: MemBuf.h:134
void Comm::ConnectionPointer & clientConn
Definition: stub_tunnel.cc:19
void httpRequestPack(void *obj, Packable *p)
Definition: HttpRequest.cc:371
Definition: Uri.h:30
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:452
void internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest *request, StoreEntry *entry)
Definition: internal.cc:31
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:845
void(* ObjPackMethod)(void *obj, Packable *p)
Definition: tools.h:33
#define SQUIDHOSTNAMELEN
Definition: rfc2181.h:30
void appendf(const char *fmt,...) PRINTF_FORMAT_ARG2
Append operation with printf-style arguments.
Definition: Packable.h:61
Definition: MemBuf.h:23
void errorAppendEntry(StoreEntry *entry, ErrorState *err)
Definition: errorpage.cc:575
char * internalRemoteUri(const char *host, unsigned short port, const char *dir, const SBuf &name)
Definition: internal.cc:85
#define SQUIDSBUFPH
Definition: SBuf.h:31
SBuf & authority(bool requirePort=false) const
Definition: Uri.cc:470
const char * rawContent() const
Definition: SBuf.cc:519
void netdbBinaryExchange(StoreEntry *s)
Definition: net_db.cc:1180
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:32
bool internalStaticCheck(const SBuf &urlPath)
Definition: internal.cc:75
class SquidConfig Config
Definition: SquidConfig.cc:12
void debugObj(int section, int level, const char *label, void *obj, ObjPackMethod pm)
Definition: tools.cc:900
wordlist * next
Definition: wordlist.h:34

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors