Squid Web Cache master
Loading...
Searching...
No Matches
Host.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2025 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 93 eCAP Interface */
10
11#include "squid.h"
15#include "base/TextException.h"
16#include "HttpReply.h"
17#include "HttpRequest.h"
18#include "MasterXaction.h"
19
20#if HAVE_LIBECAP_ADAPTER_SERVICE_H
21#include <libecap/adapter/service.h>
22#endif
23#if HAVE_LIBECAP_COMMON_NAMES_H
24#include <libecap/common/names.h>
25#endif
26#if HAVE_LIBECAP_COMMON_REGISTRY_H
27#include <libecap/common/registry.h>
28#endif
29
30const libecap::Name Adaptation::Ecap::protocolInternal("internal", libecap::Name::NextId());
31const libecap::Name Adaptation::Ecap::protocolIcp("ICP", libecap::Name::NextId());
32#if USE_HTCP
33const libecap::Name Adaptation::Ecap::protocolHtcp("Htcp", libecap::Name::NextId());
34#endif
35const libecap::Name Adaptation::Ecap::protocolIcy("ICY", libecap::Name::NextId());
36const libecap::Name Adaptation::Ecap::protocolUnknown("_unknown_", libecap::Name::NextId());
37
38const libecap::Name Adaptation::Ecap::metaBypassable("bypassable", libecap::Name::NextId());
39
41static libecap::shared_ptr<Adaptation::Ecap::Host> TheHost;
42
44{
45 // assign our host-specific IDs to well-known names
46 // this code can run only once
47
48 libecap::headerTransferEncoding.assignHostId(Http::HdrType::TRANSFER_ENCODING);
49 libecap::headerReferer.assignHostId(Http::HdrType::REFERER);
50 libecap::headerContentLength.assignHostId(Http::HdrType::CONTENT_LENGTH);
51 libecap::headerVia.assignHostId(Http::HdrType::VIA);
52 // TODO: libecap::headerXClientIp.assignHostId(Http::HdrType::X_CLIENT_IP);
53 // TODO: libecap::headerXServerIp.assignHostId(Http::HdrType::X_SERVER_IP);
54
55 libecap::protocolHttp.assignHostId(AnyP::PROTO_HTTP);
56 libecap::protocolHttps.assignHostId(AnyP::PROTO_HTTPS);
57 libecap::protocolFtp.assignHostId(AnyP::PROTO_FTP);
58 libecap::protocolWais.assignHostId(AnyP::PROTO_WAIS);
59 libecap::protocolUrn.assignHostId(AnyP::PROTO_URN);
60 libecap::protocolWhois.assignHostId(AnyP::PROTO_WHOIS);
61 protocolIcp.assignHostId(AnyP::PROTO_ICP);
62#if USE_HTCP
63 protocolHtcp.assignHostId(AnyP::PROTO_HTCP);
64#endif
65 protocolIcy.assignHostId(AnyP::PROTO_ICY);
67
68 // allows adapter to safely ignore this in adapter::Service::configure()
69 metaBypassable.assignHostId(1);
70}
71
72std::string
74{
75 return "ecap://squid-cache.org/ecap/hosts/squid";
76}
77
78void
79Adaptation::Ecap::Host::describe(std::ostream &os) const
80{
81 os << PACKAGE_NAME << " v" << PACKAGE_VERSION;
82}
83
85static SBuf
87{
88 // all libecap x.y.* releases are supposed to be compatible so we strip
89 // everything after the second period
90 const SBuf::size_type minorPos = raw.find('.');
91 const SBuf::size_type microPos = minorPos == SBuf::npos ?
92 SBuf::npos : raw.find('.', minorPos+1);
93 return raw.substr(0, microPos); // becomes raw if microPos is npos
94}
95
98static bool
99SupportedVersion(const char *vTheir, const char *them)
100{
101 if (!vTheir || !*vTheir) {
102 debugs(93, DBG_CRITICAL, "ERROR: Cannot use " << them <<
103 " with libecap prior to v1.0.");
104 return false;
105 }
106
107 // we support what we are built with
108 const SBuf vSupported(LIBECAP_VERSION);
109 debugs(93, 2, them << " with libecap v" << vTheir << "; us: v" << vSupported);
110
111 if (EssentialVersion(SBuf(vTheir)) == EssentialVersion(vSupported))
112 return true; // their version is supported
113
114 debugs(93, DBG_CRITICAL, "ERROR: Cannot use " << them <<
115 " with libecap v" << vTheir <<
116 ": incompatible with supported libecap v" << vSupported);
117 return false;
118}
119
120void
121Adaptation::Ecap::Host::noteVersionedService(const char *vGiven, const libecap::weak_ptr<libecap::adapter::Service> &weak)
122{
123 /*
124 * Check that libecap used to build the service is compatible with ours.
125 * This has to be done using vGiven string and not Service object itself
126 * because dereferencing a Service pointer coming from an unsupported
127 * version is unsafe.
128 */
129 if (SupportedVersion(vGiven, "eCAP service built")) {
130 Must(!weak.expired());
131 RegisterAdapterService(weak.lock());
132 }
133}
134
135static int
136SquidLogLevel(libecap::LogVerbosity lv)
137{
138 if (lv.critical())
139 return DBG_CRITICAL; // is it a good idea to ignore other flags?
140
141 if (lv.large())
142 return DBG_DATA; // is it a good idea to ignore other flags?
143
144 if (lv.application())
145 return lv.normal() ? DBG_IMPORTANT : 2;
146
147 return 2 + 2*lv.debugging() + 3*lv.operation() + 2*lv.xaction();
148}
149
150std::ostream *
151Adaptation::Ecap::Host::openDebug(libecap::LogVerbosity lv)
152{
153 const int squidLevel = SquidLogLevel(lv);
154 const int squidSection = 93; // XXX: this should be a global constant
155 return Debug::Enabled(squidSection, squidLevel) ?
156 &Debug::Start(squidSection, squidLevel) :
157 nullptr;
158}
159
160void
162{
163 if (debug)
165}
166
169{
170 static const auto mx = MasterXaction::MakePortless<XactionInitiator::initAdaptationOrphan_>();
172}
173
179
180void
182{
183 if (!TheHost && SupportedVersion(libecap::VersionString(),
184 "Squid executable dynamically linked")) {
186 libecap::RegisterHost(TheHost);
187 }
188}
189
#define Must(condition)
static int SquidLogLevel(libecap::LogVerbosity lv)
Definition Host.cc:136
static SBuf EssentialVersion(const SBuf &raw)
Strips libecap version components not affecting compatibility decisions.
Definition Host.cc:86
static libecap::shared_ptr< Adaptation::Ecap::Host > TheHost
the host application (i.e., Squid) wrapper registered with libecap
Definition Host.cc:41
static bool SupportedVersion(const char *vTheir, const char *them)
Definition Host.cc:99
#define PACKAGE_NAME
Definition autoconf.h:1386
#define PACKAGE_VERSION
Definition autoconf.h:1398
void noteVersionedService(const char *libEcapVersion, const libecap::weak_ptr< libecap::adapter::Service > &s) override
Definition Host.cc:121
static void Register()
register adaptation host
Definition Host.cc:181
std::string uri() const override
Definition Host.cc:73
void describe(std::ostream &os) const override
Definition Host.cc:79
std::ostream * openDebug(libecap::LogVerbosity lv) override
Definition Host.cc:151
libecap::shared_ptr< libecap::Message > MessagePtr
Definition Host.h:33
MessagePtr newResponse() const override
Definition Host.cc:175
void closeDebug(std::ostream *debug) override
Definition Host.cc:161
MessagePtr newRequest() const override
Definition Host.cc:168
static bool Enabled(const int section, const int level)
whether debugging the given section and the given level produces output
Definition Stream.h:75
static void Finish()
logs output buffer created in Start() and closes debugging context
Definition debug.cc:1366
static std::ostringstream & Start(const int section, const int level)
opens debugging context and returns output buffer
Definition debug.cc:1342
Definition SBuf.h:94
static const size_type npos
Definition SBuf.h:100
size_type find(char c, size_type startPos=0) const
Definition SBuf.cc:584
SBuf substr(size_type pos, size_type n=npos) const
Definition SBuf.cc:576
MemBlob::size_type size_type
Definition SBuf.h:96
void debug(const char *format,...)
Definition debug.cc:19
#define DBG_DATA
Definition Stream.h:40
#define DBG_IMPORTANT
Definition Stream.h:38
#define debugs(SECTION, LEVEL, CONTENT)
Definition Stream.h:192
#define DBG_CRITICAL
Definition Stream.h:37
void RegisterAdapterService(const ServiceRep::AdapterService &adapterService)
register loaded eCAP module service
const libecap::Name protocolIcp
const libecap::Name protocolUnknown
const libecap::Name protocolHtcp
const libecap::Name metaBypassable
an ecap_service parameter
const libecap::Name protocolInternal
const libecap::Name protocolIcy
@ PROTO_HTTPS
@ PROTO_UNKNOWN
@ PROTO_HTCP
@ PROTO_ICY
@ PROTO_HTTP
@ PROTO_FTP
@ PROTO_WHOIS
@ PROTO_ICP
@ PROTO_URN
@ PROTO_WAIS
@ TRANSFER_ENCODING