]> git.ipfire.org Git - thirdparty/squid.git/blob - src/internal.cc
Bug 1961 partial: Move HttpRequest host:port to class URL
[thirdparty/squid.git] / src / internal.cc
1 /*
2 * Copyright (C) 1996-2015 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
32 internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest * request, StoreEntry * entry)
33 {
34 ErrorState *err;
35 const char *upath = request->urlpath.termedBuf();
36 debugs(76, 3, HERE << clientConn << " requesting '" << upath << "'");
37
38 if (0 == strcmp(upath, "/squid-internal-dynamic/netdb")) {
39 netdbBinaryExchange(entry);
40 } else if (0 == strcmp(upath, "/squid-internal-periodic/store_digest")) {
41 #if USE_CACHE_DIGESTS
42 const char *msgbuf = "This cache is currently building its digest.\n";
43 #else
44
45 const char *msgbuf = "This cache does not support Cache Digests.\n";
46 #endif
47
48 HttpReply *reply = new HttpReply;
49 reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
50 entry->replaceHttpReply(reply);
51 entry->append(msgbuf, strlen(msgbuf));
52 entry->complete();
53 } else if (0 == strncmp(upath, "/squid-internal-mgr/", 20)) {
54 debugs(17, 2, "calling CacheManager due to URL-path /squid-internal-mgr/");
55 CacheManager::GetInstance()->Start(clientConn, request, entry);
56 } else {
57 debugObj(76, 1, "internalStart: unknown request:\n",
58 request, (ObjPackMethod) & httpRequestPack);
59 err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request);
60 errorAppendEntry(entry, err);
61 }
62 }
63
64 int
65 internalCheck(const char *urlpath)
66 {
67 return (0 == strncmp(urlpath, "/squid-internal-", 16));
68 }
69
70 int
71 internalStaticCheck(const char *urlpath)
72 {
73 return (0 == strncmp(urlpath, "/squid-internal-static", 22));
74 }
75
76 /*
77 * makes internal url with a given host and port (remote internal url)
78 */
79 char *
80 internalRemoteUri(const char *host, unsigned short port, const char *dir, const char *name)
81 {
82 static char lc_host[SQUIDHOSTNAMELEN];
83 assert(host && name);
84 /* convert host name to lower case */
85 xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
86 Tolower(lc_host);
87
88 /* check for an IP address and format appropriately if found */
89 Ip::Address test = lc_host;
90 if ( !test.isAnyAddr() ) {
91 test.toHostStr(lc_host,SQUIDHOSTNAMELEN);
92 }
93
94 /*
95 * append the domain in order to mirror the requests with appended
96 * domains
97 */
98
99 /* For IPv6 addresses also check for a colon */
100 if (Config.appendDomain && !strchr(lc_host, '.') && !strchr(lc_host, ':'))
101 strncat(lc_host, Config.appendDomain, SQUIDHOSTNAMELEN -
102 strlen(lc_host) - 1);
103
104 /* build URI */
105 URL tmp(AnyP::PROTO_HTTP);
106 tmp.host(lc_host);
107 if (port)
108 tmp.port(port);
109
110 static MemBuf mb;
111
112 mb.reset();
113 mb.appendf("http://" SQUIDSBUFPH, SQUIDSBUFPRINT(tmp.authority()));
114
115 if (dir)
116 mb.append(dir, strlen(dir));
117
118 mb.append(name, strlen(name));
119
120 /* return a pointer to a local static buffer */
121 return mb.buf;
122 }
123
124 /*
125 * makes internal url with local host and port
126 */
127 char *
128 internalLocalUri(const char *dir, const char *name)
129 {
130 return internalRemoteUri(getMyHostname(),
131 getMyPort(), dir, name);
132 }
133
134 const char *
135 internalHostname(void)
136 {
137 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
138 xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN);
139
140 /* For IPv6 addresses also check for a colon */
141 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
142 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
143 strlen(host) - 1);
144
145 Tolower(host);
146
147 return host;
148 }
149
150 int
151 internalHostnameIs(const char *arg)
152 {
153 wordlist *w;
154
155 if (0 == strcmp(arg, internalHostname()))
156 return 1;
157
158 for (w = Config.hostnameAliases; w; w = w->next)
159 if (0 == strcmp(arg, w->key))
160 return 1;
161
162 return 0;
163 }
164