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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors