]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/Host.cc
2 * Copyright (C) 1996-2018 The Squid Software Foundation and contributors
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.
9 /* DEBUG: section 93 eCAP Interface */
12 #include <libecap/adapter/service.h>
13 #include <libecap/common/names.h>
14 #include <libecap/common/registry.h>
15 #include "adaptation/ecap/Host.h"
16 #include "adaptation/ecap/MessageRep.h"
17 #include "adaptation/ecap/ServiceRep.h"
18 #include "base/TextException.h"
19 #include "HttpReply.h"
20 #include "HttpRequest.h"
21 #include "MasterXaction.h"
23 const libecap::Name
Adaptation::Ecap::protocolInternal("internal", libecap::Name::NextId());
24 const libecap::Name
Adaptation::Ecap::protocolCacheObj("cache_object", libecap::Name::NextId());
25 const libecap::Name
Adaptation::Ecap::protocolIcp("ICP", libecap::Name::NextId());
27 const libecap::Name
Adaptation::Ecap::protocolHtcp("Htcp", libecap::Name::NextId());
29 const libecap::Name
Adaptation::Ecap::protocolIcy("ICY", libecap::Name::NextId());
30 const libecap::Name
Adaptation::Ecap::protocolUnknown("_unknown_", libecap::Name::NextId());
32 const libecap::Name
Adaptation::Ecap::metaBypassable("bypassable", libecap::Name::NextId());
34 /// the host application (i.e., Squid) wrapper registered with libecap
35 static libecap::shared_ptr
<Adaptation::Ecap::Host
> TheHost
;
37 Adaptation::Ecap::Host::Host()
39 // assign our host-specific IDs to well-known names
40 // this code can run only once
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);
49 libecap::protocolHttp
.assignHostId(AnyP::PROTO_HTTP
);
50 libecap::protocolHttps
.assignHostId(AnyP::PROTO_HTTPS
);
51 libecap::protocolFtp
.assignHostId(AnyP::PROTO_FTP
);
52 libecap::protocolGopher
.assignHostId(AnyP::PROTO_GOPHER
);
53 libecap::protocolWais
.assignHostId(AnyP::PROTO_WAIS
);
54 libecap::protocolUrn
.assignHostId(AnyP::PROTO_URN
);
55 libecap::protocolWhois
.assignHostId(AnyP::PROTO_WHOIS
);
56 protocolCacheObj
.assignHostId(AnyP::PROTO_CACHE_OBJECT
);
57 protocolIcp
.assignHostId(AnyP::PROTO_ICP
);
59 protocolHtcp
.assignHostId(AnyP::PROTO_HTCP
);
61 protocolIcy
.assignHostId(AnyP::PROTO_ICY
);
62 protocolUnknown
.assignHostId(AnyP::PROTO_UNKNOWN
);
64 // allows adapter to safely ignore this in adapter::Service::configure()
65 metaBypassable
.assignHostId(1);
69 Adaptation::Ecap::Host::uri() const
71 return "ecap://squid-cache.org/ecap/hosts/squid";
75 Adaptation::Ecap::Host::describe(std::ostream
&os
) const
77 os
<< PACKAGE_NAME
<< " v" << PACKAGE_VERSION
;
80 /// Strips libecap version components not affecting compatibility decisions.
82 EssentialVersion(const SBuf
&raw
)
84 // all libecap x.y.* releases are supposed to be compatible so we strip
85 // everything after the second period
86 const SBuf::size_type minorPos
= raw
.find('.');
87 const SBuf::size_type microPos
= minorPos
== SBuf::npos
?
88 SBuf::npos
: raw
.find('.', minorPos
+1);
89 return raw
.substr(0, microPos
); // becomes raw if microPos is npos
92 /// If "their" libecap version is not compatible with what Squid has been built
93 /// with, then complain and return false.
95 SupportedVersion(const char *vTheir
, const char *them
)
97 if (!vTheir
|| !*vTheir
) {
98 debugs(93, DBG_CRITICAL
, "ERROR: Cannot use " << them
<<
99 " with libecap prior to v1.0.");
103 // we support what we are built with
104 const SBuf
vSupported(LIBECAP_VERSION
);
105 debugs(93, 2, them
<< " with libecap v" << vTheir
<< "; us: v" << vSupported
);
107 if (EssentialVersion(SBuf(vTheir
)) == EssentialVersion(vSupported
))
108 return true; // their version is supported
110 debugs(93, DBG_CRITICAL
, "ERROR: Cannot use " << them
<<
111 " with libecap v" << vTheir
<<
112 ": incompatible with supported libecap v" << vSupported
);
117 Adaptation::Ecap::Host::noteVersionedService(const char *vGiven
, const libecap::weak_ptr
<libecap::adapter::Service
> &weak
)
120 * Check that libecap used to build the service is compatible with ours.
121 * This has to be done using vGiven string and not Service object itself
122 * because dereferencing a Service pointer coming from an unsupported
125 if (SupportedVersion(vGiven
, "eCAP service built")) {
126 Must(!weak
.expired());
127 RegisterAdapterService(weak
.lock());
132 SquidLogLevel(libecap::LogVerbosity lv
)
135 return DBG_CRITICAL
; // is it a good idea to ignore other flags?
138 return DBG_DATA
; // is it a good idea to ignore other flags?
140 if (lv
.application())
141 return lv
.normal() ? DBG_IMPORTANT
: 2;
143 return 2 + 2*lv
.debugging() + 3*lv
.operation() + 2*lv
.xaction();
147 Adaptation::Ecap::Host::openDebug(libecap::LogVerbosity lv
)
149 const int squidLevel
= SquidLogLevel(lv
);
150 const int squidSection
= 93; // XXX: this should be a global constant
151 return Debug::Enabled(squidSection
, squidLevel
) ?
152 &Debug::Start(squidSection
, squidLevel
) :
157 Adaptation::Ecap::Host::closeDebug(std::ostream
*debug
)
163 Adaptation::Ecap::Host::MessagePtr
164 Adaptation::Ecap::Host::newRequest() const
166 static const MasterXaction::Pointer mx
= new MasterXaction(XactionInitiator::initAdaptationOrphan_
);
167 return MessagePtr(new Adaptation::Ecap::MessageRep(new HttpRequest(mx
)));
170 Adaptation::Ecap::Host::MessagePtr
171 Adaptation::Ecap::Host::newResponse() const
173 return MessagePtr(new Adaptation::Ecap::MessageRep(new HttpReply
));
177 Adaptation::Ecap::Host::Register()
179 if (!TheHost
&& SupportedVersion(libecap::VersionString(),
180 "Squid executable dynamically linked")) {
181 TheHost
.reset(new Adaptation::Ecap::Host
);
182 libecap::RegisterHost(TheHost
);