]> git.ipfire.org Git - thirdparty/squid.git/blame - src/internal.cc
SourceLayout: shuffle memory pool allocators to mem/libmem.la
[thirdparty/squid.git] / src / internal.cc
CommitLineData
9cef6668 1/*
bbc27441 2 * Copyright (C) 1996-2014 The Squid Software Foundation and contributors
9cef6668 3 *
bbc27441
AJ
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.
9cef6668 7 */
8
bbc27441
AJ
9/* DEBUG: section 76 Internal Squid Object handling */
10
582c2af2 11#include "squid.h"
e37bd29b
AJ
12#include "CacheManager.h"
13#include "comm/Connection.h"
aa839030 14#include "errorpage.h"
528b2c61 15#include "HttpReply.h"
602d9612
A
16#include "HttpRequest.h"
17#include "icmp/net_db.h"
0eb49b6d 18#include "MemBuf.h"
4d5904f7 19#include "SquidConfig.h"
985c86bc 20#include "SquidTime.h"
602d9612 21#include "Store.h"
4e540555 22#include "tools.h"
b1bd952a 23#include "URL.h"
ed6e9fb9 24#include "util.h"
d295d770 25#include "wordlist.h"
1da5651f 26
a1f5c896 27/* called when we "miss" on an internal object;
26ac0430 28 * generate known dynamic objects,
955394ce 29 * return Http::scNotFound for others
a1f5c896 30 */
1da5651f 31void
e37bd29b 32internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest * request, StoreEntry * entry)
1da5651f 33{
865094d7 34 ErrorState *err;
b4f2886c 35 const char *upath = request->urlpath.termedBuf();
e37bd29b 36 debugs(76, 3, HERE << clientConn << " requesting '" << upath << "'");
62e76326 37
1f38f50a 38 if (0 == strcmp(upath, "/squid-internal-dynamic/netdb")) {
62e76326 39 netdbBinaryExchange(entry);
1f38f50a 40 } else if (0 == strcmp(upath, "/squid-internal-periodic/store_digest")) {
41#if USE_CACHE_DIGESTS
62e76326 42 const char *msgbuf = "This cache is currently building its digest.\n";
1f38f50a 43#else
62e76326 44
9b5c4a9a 45 const char *msgbuf = "This cache does not support Cache Digests.\n";
1f38f50a 46#endif
62e76326 47
06a5ae20 48 HttpReply *reply = new HttpReply;
955394ce 49 reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2);
db237875 50 entry->replaceHttpReply(reply);
3900307b 51 entry->append(msgbuf, strlen(msgbuf));
62e76326 52 entry->complete();
e37bd29b 53 } else if (0 == strncmp(upath, "/squid-internal-mgr/", 20)) {
4e3f4dc7 54 debugs(17, 2, "calling CacheManager due to URL-path /squid-internal-mgr/");
e37bd29b 55 CacheManager::GetInstance()->Start(clientConn, request, entry);
1f38f50a 56 } else {
62e76326 57 debugObj(76, 1, "internalStart: unknown request:\n",
58 request, (ObjPackMethod) & httpRequestPack);
955394ce 59 err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request);
62e76326 60 errorAppendEntry(entry, err);
865094d7 61 }
1da5651f 62}
63
64int
65internalCheck(const char *urlpath)
66{
5999b776 67 return (0 == strncmp(urlpath, "/squid-internal-", 16));
1da5651f 68}
69
ba26bca4 70int
71internalStaticCheck(const char *urlpath)
72{
5999b776 73 return (0 == strncmp(urlpath, "/squid-internal-static", 22));
ba26bca4 74}
75
1da5651f 76/*
77 * makes internal url with a given host and port (remote internal url)
78 */
79char *
f45dd259 80internalRemoteUri(const char *host, unsigned short port, const char *dir, const char *name)
1da5651f 81{
1da5651f 82 static char lc_host[SQUIDHOSTNAMELEN];
52f772de 83 assert(host && name);
137ee196 84 /* convert host name to lower case */
c8918a58 85 xstrncpy(lc_host, host, SQUIDHOSTNAMELEN);
1da5651f 86 Tolower(lc_host);
cc192b50 87
cc192b50 88 /* check for an IP address and format appropriately if found */
b7ac5457 89 Ip::Address test = lc_host;
4dd643d5
AJ
90 if ( !test.isAnyAddr() ) {
91 test.toHostStr(lc_host,SQUIDHOSTNAMELEN);
cc192b50 92 }
cc192b50 93
1224d740 94 /*
95 * append the domain in order to mirror the requests with appended
96 * domains
97 */
62e76326 98
055421ee 99 /* For IPv6 addresses also check for a colon */
532e5dd4 100 if (Config.appendDomain && !strchr(lc_host, '.') && !strchr(lc_host, ':'))
62e76326 101 strncat(lc_host, Config.appendDomain, SQUIDHOSTNAMELEN -
102 strlen(lc_host) - 1);
103
137ee196 104 /* build uri in mb */
032785bf 105 static MemBuf mb;
106
2fe7eff9 107 mb.reset();
62e76326 108
2fe7eff9 109 mb.Printf("http://%s", lc_host);
62e76326 110
137ee196 111 /* append port if not default */
0c3d3f65 112 if (port && port != urlDefaultPort(AnyP::PROTO_HTTP))
2fe7eff9 113 mb.Printf(":%d", port);
62e76326 114
1da5651f 115 if (dir)
2fe7eff9 116 mb.Printf("%s", dir);
62e76326 117
2fe7eff9 118 mb.Printf("%s", name);
62e76326 119
137ee196 120 /* return a pointer to a local static buffer */
121 return mb.buf;
1da5651f 122}
123
124/*
125 * makes internal url with local host and port
126 */
127char *
128internalLocalUri(const char *dir, const char *name)
129{
7e3ce7b9 130 return internalRemoteUri(getMyHostname(),
62e76326 131 getMyPort(), dir, name);
1da5651f 132}
c68e9c6b 133
134const char *
135internalHostname(void)
136{
137 LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1);
138 xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN);
b3ef8c0d 139
055421ee 140 /* For IPv6 addresses also check for a colon */
532e5dd4 141 if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':'))
b3ef8c0d 142 strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN -
143 strlen(host) - 1);
144
c68e9c6b 145 Tolower(host);
b3ef8c0d 146
c68e9c6b 147 return host;
148}
1f38f50a 149
150int
151internalHostnameIs(const char *arg)
152{
153 wordlist *w;
62e76326 154
1f38f50a 155 if (0 == strcmp(arg, internalHostname()))
62e76326 156 return 1;
157
1f38f50a 158 for (w = Config.hostnameAliases; w; w = w->next)
62e76326 159 if (0 == strcmp(arg, w->key))
160 return 1;
161
1f38f50a 162 return 0;
163}