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 "adaptation/ecap/Host.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 
30 const libecap::Name Adaptation::Ecap::protocolInternal("internal", libecap::Name::NextId());
31 const libecap::Name Adaptation::Ecap::protocolIcp("ICP", libecap::Name::NextId());
32 #if USE_HTCP
33 const libecap::Name Adaptation::Ecap::protocolHtcp("Htcp", libecap::Name::NextId());
34 #endif
35 const libecap::Name Adaptation::Ecap::protocolIcy("ICY", libecap::Name::NextId());
36 const libecap::Name Adaptation::Ecap::protocolUnknown("_unknown_", libecap::Name::NextId());
37 
38 const libecap::Name Adaptation::Ecap::metaBypassable("bypassable", libecap::Name::NextId());
39 
41 static 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 
72 std::string
74 {
75  return "ecap://squid-cache.org/ecap/hosts/squid";
76 }
77 
78 void
79 Adaptation::Ecap::Host::describe(std::ostream &os) const
80 {
81  os << PACKAGE_NAME << " v" << PACKAGE_VERSION;
82 }
83 
85 static 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 
98 static bool
99 SupportedVersion(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 
120 void
121 Adaptation::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 
135 static int
136 SquidLogLevel(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 
150 std::ostream *
151 Adaptation::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 
160 void
162 {
163  if (debug)
164  Debug::Finish();
165 }
166 
169 {
170  static const auto mx = MasterXaction::MakePortless<XactionInitiator::initAdaptationOrphan_>();
172 }
173 
176 {
178 }
179 
180 void
182 {
183  if (!TheHost && SupportedVersion(libecap::VersionString(),
184  "Squid executable dynamically linked")) {
185  TheHost.reset(new Adaptation::Ecap::Host);
186  libecap::RegisterHost(TheHost);
187  }
188 }
189 
size_type find(char c, size_type startPos=0) const
Definition: SBuf.cc:584
const libecap::Name protocolIcp
#define DBG_CRITICAL
Definition: Stream.h:37
const libecap::Name metaBypassable
an ecap_service parameter
void debug(const char *format,...)
Definition: debug.cc:19
std::ostream * openDebug(libecap::LogVerbosity lv) override
Definition: Host.cc:151
Definition: SBuf.h:93
void noteVersionedService(const char *libEcapVersion, const libecap::weak_ptr< libecap::adapter::Service > &s) override
Definition: Host.cc:121
std::string uri() const override
Definition: Host.cc:73
static std::ostringstream & Start(const int section, const int level)
opens debugging context and returns output buffer
Definition: debug.cc:1342
void describe(std::ostream &os) const override
Definition: Host.cc:79
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:576
const libecap::Name protocolIcy
@ PROTO_UNKNOWN
Definition: ProtocolType.h:41
@ CONTENT_LENGTH
static bool Enabled(const int section, const int level)
whether debugging the given section and the given level produces output
Definition: Stream.h:75
#define DBG_DATA
Definition: Stream.h:40
@ PROTO_URN
Definition: ProtocolType.h:35
static SBuf EssentialVersion(const SBuf &raw)
Strips libecap version components not affecting compatibility decisions.
Definition: Host.cc:86
const libecap::Name protocolInternal
MemBlob::size_type size_type
Definition: SBuf.h:96
MessagePtr newRequest() const override
Definition: Host.cc:168
static void Finish()
logs output buffer created in Start() and closes debugging context
Definition: debug.cc:1366
void RegisterAdapterService(const ServiceRep::AdapterService &adapterService)
register loaded eCAP module service
Definition: ServiceRep.cc:325
static void Register()
register adaptation host
Definition: Host.cc:181
static libecap::shared_ptr< Adaptation::Ecap::Host > TheHost
the host application (i.e., Squid) wrapper registered with libecap
Definition: Host.cc:41
MessagePtr newResponse() const override
Definition: Host.cc:175
@ TRANSFER_ENCODING
static bool SupportedVersion(const char *vTheir, const char *them)
Definition: Host.cc:99
static const size_type npos
Definition: SBuf.h:100
libecap::shared_ptr< libecap::Message > MessagePtr
Definition: Host.h:33
@ PROTO_WHOIS
Definition: ProtocolType.h:36
@ PROTO_HTTPS
Definition: ProtocolType.h:27
@ PROTO_FTP
Definition: ProtocolType.h:26
@ PROTO_HTTP
Definition: ProtocolType.h:25
@ PROTO_WAIS
Definition: ProtocolType.h:30
#define Must(condition)
Definition: TextException.h:75
#define DBG_IMPORTANT
Definition: Stream.h:38
const libecap::Name protocolHtcp
@ PROTO_HTCP
Definition: ProtocolType.h:33
@ PROTO_ICY
Definition: ProtocolType.h:37
static int SquidLogLevel(libecap::LogVerbosity lv)
Definition: Host.cc:136
const libecap::Name protocolUnknown
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:192
void closeDebug(std::ostream *debug) override
Definition: Host.cc:161
@ PROTO_ICP
Definition: ProtocolType.h:31

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors