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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors