]>
Commit | Line | Data |
---|---|---|
9cef6668 | 1 | /* |
f70aedc4 | 2 | * Copyright (C) 1996-2021 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" |
7e6eabbc | 12 | #include "AccessLogEntry.h" |
e37bd29b AJ |
13 | #include "CacheManager.h" |
14 | #include "comm/Connection.h" | |
aa839030 | 15 | #include "errorpage.h" |
528b2c61 | 16 | #include "HttpReply.h" |
602d9612 A |
17 | #include "HttpRequest.h" |
18 | #include "icmp/net_db.h" | |
0eb49b6d | 19 | #include "MemBuf.h" |
4d5904f7 | 20 | #include "SquidConfig.h" |
985c86bc | 21 | #include "SquidTime.h" |
602d9612 | 22 | #include "Store.h" |
4e540555 | 23 | #include "tools.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 | 31 | void |
7e6eabbc | 32 | internalStart(const Comm::ConnectionPointer &clientConn, HttpRequest * request, StoreEntry * entry, const AccessLogEntry::Pointer &ale) |
1da5651f | 33 | { |
865094d7 | 34 | ErrorState *err; |
51b5dcf5 AJ |
35 | const SBuf upath = request->url.path(); |
36 | debugs(76, 3, clientConn << " requesting '" << upath << "'"); | |
62e76326 | 37 | |
51b5dcf5 AJ |
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) { | |
62e76326 | 43 | netdbBinaryExchange(entry); |
51b5dcf5 | 44 | } else if (upath == storeDigestUri) { |
1f38f50a | 45 | #if USE_CACHE_DIGESTS |
62e76326 | 46 | const char *msgbuf = "This cache is currently building its digest.\n"; |
1f38f50a | 47 | #else |
62e76326 | 48 | |
9b5c4a9a | 49 | const char *msgbuf = "This cache does not support Cache Digests.\n"; |
1f38f50a | 50 | #endif |
62e76326 | 51 | |
06a5ae20 | 52 | HttpReply *reply = new HttpReply; |
955394ce | 53 | reply->setHeaders(Http::scNotFound, "Not Found", "text/plain", strlen(msgbuf), squid_curtime, -2); |
db237875 | 54 | entry->replaceHttpReply(reply); |
3900307b | 55 | entry->append(msgbuf, strlen(msgbuf)); |
62e76326 | 56 | entry->complete(); |
51b5dcf5 AJ |
57 | } else if (upath.startsWith(mgrPfx)) { |
58 | debugs(17, 2, "calling CacheManager due to URL-path " << mgrPfx); | |
7e6eabbc | 59 | CacheManager::GetInstance()->start(clientConn, request, entry, ale); |
1f38f50a | 60 | } else { |
62e76326 | 61 | debugObj(76, 1, "internalStart: unknown request:\n", |
62 | request, (ObjPackMethod) & httpRequestPack); | |
7e6eabbc | 63 | err = new ErrorState(ERR_INVALID_REQ, Http::scNotFound, request, ale); |
62e76326 | 64 | errorAppendEntry(entry, err); |
865094d7 | 65 | } |
1da5651f | 66 | } |
67 | ||
51b5dcf5 AJ |
68 | bool |
69 | internalCheck(const SBuf &urlPath) | |
1da5651f | 70 | { |
51b5dcf5 AJ |
71 | static const SBuf InternalPfx("/squid-internal-"); |
72 | return urlPath.startsWith(InternalPfx); | |
1da5651f | 73 | } |
74 | ||
51b5dcf5 AJ |
75 | bool |
76 | internalStaticCheck(const SBuf &urlPath) | |
ba26bca4 | 77 | { |
51b5dcf5 AJ |
78 | static const SBuf InternalStaticPfx("/squid-internal-static"); |
79 | return urlPath.startsWith(InternalStaticPfx); | |
ba26bca4 | 80 | } |
81 | ||
1da5651f | 82 | /* |
83 | * makes internal url with a given host and port (remote internal url) | |
84 | */ | |
85 | char * | |
c002f0e7 | 86 | internalRemoteUri(bool encrypt, const char *host, unsigned short port, const char *dir, const SBuf &name) |
1da5651f | 87 | { |
1da5651f | 88 | static char lc_host[SQUIDHOSTNAMELEN]; |
51b5dcf5 | 89 | assert(host && !name.isEmpty()); |
137ee196 | 90 | /* convert host name to lower case */ |
c8918a58 | 91 | xstrncpy(lc_host, host, SQUIDHOSTNAMELEN); |
1da5651f | 92 | Tolower(lc_host); |
cc192b50 | 93 | |
cc192b50 | 94 | /* check for an IP address and format appropriately if found */ |
b7ac5457 | 95 | Ip::Address test = lc_host; |
4dd643d5 AJ |
96 | if ( !test.isAnyAddr() ) { |
97 | test.toHostStr(lc_host,SQUIDHOSTNAMELEN); | |
cc192b50 | 98 | } |
cc192b50 | 99 | |
1224d740 | 100 | /* |
101 | * append the domain in order to mirror the requests with appended | |
38aa10ef | 102 | * domains. If that fails, just use the hostname anyway. |
1224d740 | 103 | */ |
38aa10ef | 104 | (void)urlAppendDomain(lc_host); |
62e76326 | 105 | |
5c51bffb | 106 | /* build URI */ |
c8ab5ec6 | 107 | AnyP::Uri tmp(AnyP::PROTO_HTTP); |
5c51bffb AJ |
108 | tmp.host(lc_host); |
109 | if (port) | |
110 | tmp.port(port); | |
111 | ||
032785bf | 112 | static MemBuf mb; |
113 | ||
2fe7eff9 | 114 | mb.reset(); |
c002f0e7 | 115 | mb.appendf("%s://" SQUIDSBUFPH, encrypt ? "https" : "http", SQUIDSBUFPRINT(tmp.authority())); |
62e76326 | 116 | |
1da5651f | 117 | if (dir) |
4391cd15 | 118 | mb.append(dir, strlen(dir)); |
62e76326 | 119 | |
51b5dcf5 | 120 | mb.append(name.rawContent(), name.length()); |
62e76326 | 121 | |
137ee196 | 122 | /* return a pointer to a local static buffer */ |
123 | return mb.buf; | |
1da5651f | 124 | } |
125 | ||
126 | /* | |
127 | * makes internal url with local host and port | |
128 | */ | |
129 | char * | |
51b5dcf5 | 130 | internalLocalUri(const char *dir, const SBuf &name) |
1da5651f | 131 | { |
c002f0e7 | 132 | // XXX: getMy*() may return https_port info, but we force http URIs |
133 | // because we have not checked whether the callers can handle https. | |
134 | const bool secure = false; | |
135 | return internalRemoteUri(secure, getMyHostname(), | |
62e76326 | 136 | getMyPort(), dir, name); |
1da5651f | 137 | } |
c68e9c6b | 138 | |
139 | const char * | |
140 | internalHostname(void) | |
141 | { | |
142 | LOCAL_ARRAY(char, host, SQUIDHOSTNAMELEN + 1); | |
143 | xstrncpy(host, getMyHostname(), SQUIDHOSTNAMELEN); | |
b3ef8c0d | 144 | |
055421ee | 145 | /* For IPv6 addresses also check for a colon */ |
532e5dd4 | 146 | if (Config.appendDomain && !strchr(host, '.') && !strchr(host, ':')) |
b3ef8c0d | 147 | strncat(host, Config.appendDomain, SQUIDHOSTNAMELEN - |
148 | strlen(host) - 1); | |
149 | ||
c68e9c6b | 150 | Tolower(host); |
b3ef8c0d | 151 | |
c68e9c6b | 152 | return host; |
153 | } | |
1f38f50a | 154 | |
155 | int | |
156 | internalHostnameIs(const char *arg) | |
157 | { | |
158 | wordlist *w; | |
62e76326 | 159 | |
1f38f50a | 160 | if (0 == strcmp(arg, internalHostname())) |
62e76326 | 161 | return 1; |
162 | ||
1f38f50a | 163 | for (w = Config.hostnameAliases; w; w = w->next) |
62e76326 | 164 | if (0 == strcmp(arg, w->key)) |
165 | return 1; | |
166 | ||
1f38f50a | 167 | return 0; |
168 | } | |
f53969cc | 169 |