]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/Host.cc
2 * Copyright (C) 1996-2016 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"
22 const libecap::Name
Adaptation::Ecap::protocolInternal("internal", libecap::Name::NextId());
23 const libecap::Name
Adaptation::Ecap::protocolCacheObj("cache_object", libecap::Name::NextId());
24 const libecap::Name
Adaptation::Ecap::protocolIcp("ICP", libecap::Name::NextId());
26 const libecap::Name
Adaptation::Ecap::protocolHtcp("Htcp", libecap::Name::NextId());
28 const libecap::Name
Adaptation::Ecap::protocolIcy("ICY", libecap::Name::NextId());
29 const libecap::Name
Adaptation::Ecap::protocolUnknown("_unknown_", libecap::Name::NextId());
31 const libecap::Name
Adaptation::Ecap::metaBypassable("bypassable", libecap::Name::NextId());
33 /// the host application (i.e., Squid) wrapper registered with libecap
34 static libecap::shared_ptr
<Adaptation::Ecap::Host
> TheHost
;
36 Adaptation::Ecap::Host::Host()
38 // assign our host-specific IDs to well-known names
39 // this code can run only once
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);
48 libecap::protocolHttp
.assignHostId(AnyP::PROTO_HTTP
);
49 libecap::protocolHttps
.assignHostId(AnyP::PROTO_HTTPS
);
50 libecap::protocolFtp
.assignHostId(AnyP::PROTO_FTP
);
51 libecap::protocolGopher
.assignHostId(AnyP::PROTO_GOPHER
);
52 libecap::protocolWais
.assignHostId(AnyP::PROTO_WAIS
);
53 libecap::protocolUrn
.assignHostId(AnyP::PROTO_URN
);
54 libecap::protocolWhois
.assignHostId(AnyP::PROTO_WHOIS
);
55 protocolCacheObj
.assignHostId(AnyP::PROTO_CACHE_OBJECT
);
56 protocolIcp
.assignHostId(AnyP::PROTO_ICP
);
58 protocolHtcp
.assignHostId(AnyP::PROTO_HTCP
);
60 protocolIcy
.assignHostId(AnyP::PROTO_ICY
);
61 protocolUnknown
.assignHostId(AnyP::PROTO_UNKNOWN
);
63 // allows adapter to safely ignore this in adapter::Service::configure()
64 metaBypassable
.assignHostId(1);
68 Adaptation::Ecap::Host::uri() const
70 return "ecap://squid-cache.org/ecap/hosts/squid";
74 Adaptation::Ecap::Host::describe(std::ostream
&os
) const
76 os
<< PACKAGE_NAME
<< " v" << PACKAGE_VERSION
;
79 /// Strips libecap version components not affecting compatibility decisions.
81 EssentialVersion(const SBuf
&raw
)
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
91 /// If "their" libecap version is not compatible with what Squid has been built
92 /// with, then complain and return false.
94 SupportedVersion(const char *vTheir
, const char *them
)
96 if (!vTheir
|| !*vTheir
) {
97 debugs(93, DBG_CRITICAL
, "ERROR: Cannot use " << them
<<
98 " with libecap prior to v1.0.");
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
);
106 if (EssentialVersion(SBuf(vTheir
)) == EssentialVersion(vSupported
))
107 return true; // their version is supported
109 debugs(93, DBG_CRITICAL
, "ERROR: Cannot use " << them
<<
110 " with libecap v" << vTheir
<<
111 ": incompatible with supported libecap v" << vSupported
);
116 Adaptation::Ecap::Host::noteVersionedService(const char *vGiven
, const libecap::weak_ptr
<libecap::adapter::Service
> &weak
)
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
124 if (SupportedVersion(vGiven
, "eCAP service built")) {
125 Must(!weak
.expired());
126 RegisterAdapterService(weak
.lock());
131 SquidLogLevel(libecap::LogVerbosity lv
)
134 return DBG_CRITICAL
; // is it a good idea to ignore other flags?
137 return DBG_DATA
; // is it a good idea to ignore other flags?
139 if (lv
.application())
140 return lv
.normal() ? DBG_IMPORTANT
: 2;
142 return 2 + 2*lv
.debugging() + 3*lv
.operation() + 2*lv
.xaction();
146 Adaptation::Ecap::Host::openDebug(libecap::LogVerbosity lv
)
148 const int squidLevel
= SquidLogLevel(lv
);
149 const int squidSection
= 93; // XXX: this should be a global constant
150 // XXX: Debug.h should provide this to us
151 if ((Debug::level
= squidLevel
) <= Debug::Levels
[squidSection
])
152 return &Debug::getDebugOut();
158 Adaptation::Ecap::Host::closeDebug(std::ostream
*debug
)
161 Debug::finishDebug();
164 Adaptation::Ecap::Host::MessagePtr
165 Adaptation::Ecap::Host::newRequest() const
167 return MessagePtr(new Adaptation::Ecap::MessageRep(new HttpRequest
));
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
);