internal.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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 "URL.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  */
31 void
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) {
43  netdbBinaryExchange(entry);
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);
60  } else {
61  debugObj(76, 1, "internalStart: unknown request:\n",
62  request, (ObjPackMethod) & httpRequestPack);
63  err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request);
64  errorAppendEntry(entry, err);
65  }
66 }
67 
68 bool
69 internalCheck(const SBuf &urlPath)
70 {
71  static const SBuf InternalPfx("/squid-internal-");
72  return urlPath.startsWith(InternalPfx);
73 }
74 
75 bool
76 internalStaticCheck(const SBuf &urlPath)
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  */
85 char *
86 internalRemoteUri(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
103  */
104 
105  /* For IPv6 addresses also check for a colon */
106  if (Config.appendDomain && !strchr(lc_host, '.') && !strchr(lc_host, ':'))
107  strncat(lc_host, Config.appendDomain, SQUIDHOSTNAMELEN -
108  strlen(lc_host) - 1);
109 
110  /* build URI */
111  URL tmp(AnyP::PROTO_HTTP);
112  tmp.host(lc_host);
113  if (port)
114  tmp.port(port);
115 
116  static MemBuf mb;
117 
118  mb.reset();
119  mb.appendf("http://" SQUIDSBUFPH, SQUIDSBUFPRINT(tmp.authority()));
120 
121  if (dir)
122  mb.append(dir, strlen(dir));
123 
124  mb.append(name.rawContent(), name.length());
125 
126  /* return a pointer to a local static buffer */
127  return mb.buf;
128 }
129 
130 /*
131  * makes internal url with local host and port
132  */
133 char *
134 internalLocalUri(const char *dir, const SBuf &name)
135 {
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
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 
int internalHostnameIs(const char *arg)
Definition: internal.cc:157
#define assert(EX)
Definition: assert.h:17
virtual void append(const char *c, int sz)
Definition: MemBuf.cc:225
bool internalCheck(const SBuf &urlPath)
Definition: internal.cc:69
Definition: SBuf.h:87
virtual void append(char const *, int)
Appends a c-string to existing packed data.
Definition: store.cc:844
struct _request * request(char *urlin)
Definition: tcp-banger2.c:291
void port(unsigned short p)
Definition: URL.h:74
char * key
Definition: wordlist.h:33
char * appendDomain
Definition: SquidConfig.h:211
bool isEmpty() const
Definition: SBuf.h:422
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:1854
Definition: URL.h:24
void host(const char *src)
Definition: url.cc:47
static CacheManager * GetInstance()
virtual void complete()
Definition: store.cc:1088
void EVH void * arg
Definition: stub_event.cc:16
size_type length() const
Returns the number of bytes stored in SBuf.
Definition: SBuf.h:405
int getMyPort(void)
Definition: tools.cc:1003
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
void setHeaders(Http::StatusCode status, const char *reason, const char *ctype, int64_t clen, time_t lmt, time_t expires)
Definition: HttpReply.cc:151
SBuf & authority(bool requirePort=false) const
Definition: url.cc:470
void reset()
Definition: MemBuf.cc:141
char * xstrncpy(char *dst, const char *src, size_t n)
Definition: xstring.cc:37
const char * internalHostname(void)
Definition: internal.cc:141
static int port
Definition: ldap_backend.cc:69
URL url
the request URI
Definition: HttpRequest.h:103
#define LOCAL_ARRAY(type, name, size)
Definition: leakcheck.h:18
void path(const char *p)
Definition: URL.h:77
char * internalLocalUri(const char *dir, const SBuf &name)
Definition: internal.cc:134
wordlist * hostnameAliases
Definition: SquidConfig.h:219
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
bool startsWith(const SBuf &S, const SBufCaseSensitive isCaseSensitive=caseSensitive) const
Definition: SBuf.cc:472
void internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest *request, StoreEntry *entry)
Definition: internal.cc:32
unsigned int toHostStr(char *buf, const unsigned int len) const
Definition: Address.cc:860
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:576
char * internalRemoteUri(const char *host, unsigned short port, const char *dir, const SBuf &name)
Definition: internal.cc:86
#define SQUIDSBUFPH
Definition: SBuf.h:32
const char * rawContent() const
Definition: SBuf.cc:539
void netdbBinaryExchange(StoreEntry *s)
Definition: net_db.cc:1179
#define SQUIDSBUFPRINT(s)
Definition: SBuf.h:33
bool internalStaticCheck(const SBuf &urlPath)
Definition: internal.cc:76
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