Host.cc
Go to the documentation of this file.
1 /*
2  * Copyright (C) 1996-2017 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>
15 #include "adaptation/ecap/Host.h"
18 #include "base/TextException.h"
19 #include "HttpReply.h"
20 #include "HttpRequest.h"
21 #include "MasterXaction.h"
22 
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());
26 #if USE_HTCP
27 const libecap::Name Adaptation::Ecap::protocolHtcp("Htcp", libecap::Name::NextId());
28 #endif
29 const libecap::Name Adaptation::Ecap::protocolIcy("ICY", libecap::Name::NextId());
30 const libecap::Name Adaptation::Ecap::protocolUnknown("_unknown_", libecap::Name::NextId());
31 
32 const libecap::Name Adaptation::Ecap::metaBypassable("bypassable", libecap::Name::NextId());
33 
35 static 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::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);
57  protocolIcp.assignHostId(AnyP::PROTO_ICP);
58 #if USE_HTCP
59  protocolHtcp.assignHostId(AnyP::PROTO_HTCP);
60 #endif
61  protocolIcy.assignHostId(AnyP::PROTO_ICY);
63 
64  // allows adapter to safely ignore this in adapter::Service::configure()
65  metaBypassable.assignHostId(1);
66 }
67 
68 std::string
70 {
71  return "ecap://squid-cache.org/ecap/hosts/squid";
72 }
73 
74 void
75 Adaptation::Ecap::Host::describe(std::ostream &os) const
76 {
77  os << PACKAGE_NAME << " v" << PACKAGE_VERSION;
78 }
79 
81 static SBuf
83 {
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
90 }
91 
94 static bool
95 SupportedVersion(const char *vTheir, const char *them)
96 {
97  if (!vTheir || !*vTheir) {
98  debugs(93, DBG_CRITICAL, "ERROR: Cannot use " << them <<
99  " with libecap prior to v1.0.");
100  return false;
101  }
102 
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);
106 
107  if (EssentialVersion(SBuf(vTheir)) == EssentialVersion(vSupported))
108  return true; // their version is supported
109 
110  debugs(93, DBG_CRITICAL, "ERROR: Cannot use " << them <<
111  " with libecap v" << vTheir <<
112  ": incompatible with supported libecap v" << vSupported);
113  return false;
114 }
115 
116 void
117 Adaptation::Ecap::Host::noteVersionedService(const char *vGiven, const libecap::weak_ptr<libecap::adapter::Service> &weak)
118 {
119  /*
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
123  * version is unsafe.
124  */
125  if (SupportedVersion(vGiven, "eCAP service built")) {
126  Must(!weak.expired());
127  RegisterAdapterService(weak.lock());
128  }
129 }
130 
131 static int
132 SquidLogLevel(libecap::LogVerbosity lv)
133 {
134  if (lv.critical())
135  return DBG_CRITICAL; // is it a good idea to ignore other flags?
136 
137  if (lv.large())
138  return DBG_DATA; // is it a good idea to ignore other flags?
139 
140  if (lv.application())
141  return lv.normal() ? DBG_IMPORTANT : 2;
142 
143  return 2 + 2*lv.debugging() + 3*lv.operation() + 2*lv.xaction();
144 }
145 
146 std::ostream *
147 Adaptation::Ecap::Host::openDebug(libecap::LogVerbosity lv)
148 {
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) :
153  nullptr;
154 }
155 
156 void
158 {
159  if (debug)
160  Debug::Finish();
161 }
162 
165 {
168 }
169 
172 {
174 }
175 
176 void
178 {
179  if (!TheHost && SupportedVersion(libecap::VersionString(),
180  "Squid executable dynamically linked")) {
181  TheHost.reset(new Adaptation::Ecap::Host);
182  libecap::RegisterHost(TheHost);
183  }
184 }
185 
static void Finish()
logs output buffer created in Start() and closes debugging context
Definition: debug.cc:836
const libecap::Name protocolHtcp
const libecap::Name metaBypassable
an ecap_service parameter
Definition: SBuf.h:87
virtual void closeDebug(std::ostream *debug)
Definition: Host.cc:157
#define DBG_CRITICAL
Definition: Debug.h:44
static int SquidLogLevel(libecap::LogVerbosity lv)
Definition: Host.cc:132
virtual MessagePtr newRequest() const
Definition: Host.cc:164
virtual void describe(std::ostream &os) const
Definition: Host.cc:75
virtual void noteVersionedService(const char *libEcapVersion, const libecap::weak_ptr< libecap::adapter::Service > &s)
Definition: Host.cc:117
virtual std::ostream * openDebug(libecap::LogVerbosity lv)
Definition: Host.cc:147
static libecap::shared_ptr< Adaptation::Ecap::Host > TheHost
the host application (i.e., Squid) wrapper registered with libecap
Definition: Host.cc:35
const libecap::Name protocolUnknown
const libecap::Name protocolIcy
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Debug.h:123
#define DBG_IMPORTANT
Definition: Debug.h:45
static int debug
Definition: tcp-banger3.c:105
const libecap::Name protocolCacheObj
virtual MessagePtr newResponse() const
Definition: Host.cc:171
static SBuf EssentialVersion(const SBuf &raw)
Strips libecap version components not affecting compatibility decisions.
Definition: Host.cc:82
eCAP-created HTTP message w/o an associated HTTP transaction (not ACL-detectable) ...
#define Must(cond)
Definition: TextException.h:89
static std::ostringstream & Start(const int section, const int level)
opens debugging context and returns output buffer
Definition: debug.cc:815
static const size_type npos
Definition: SBuf.h:93
size_type find(char c, size_type startPos=0) const
Definition: SBuf.cc:614
libecap::shared_ptr< libecap::Message > MessagePtr
Definition: Host.h:31
const libecap::Name protocolIcp
virtual std::string uri() const
Definition: Host.cc:69
#define DBG_DATA
Definition: Debug.h:47
static bool Enabled(const int section, const int level)
whether debugging the given section and the given level produces output
Definition: Debug.h:73
MemBlob::size_type size_type
Definition: SBuf.h:90
void RegisterAdapterService(const ServiceRep::AdapterService &adapterService)
register loaded eCAP module service
Definition: ServiceRep.cc:316
SBuf substr(size_type pos, size_type n=npos) const
Definition: SBuf.cc:606
const libecap::Name protocolInternal
static void Register()
register adaptation host
Definition: Host.cc:177
static bool SupportedVersion(const char *vTheir, const char *them)
Definition: Host.cc:95

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors