]> git.ipfire.org Git - thirdparty/squid.git/blob - src/adaptation/ecap/Host.cc
libecap v0.2.0 options support: supply client IP and user name to eCAP.
[thirdparty/squid.git] / src / adaptation / ecap / Host.cc
1 /*
2 * DEBUG: section 93 eCAP Interface
3 */
4 #include "squid.h"
5 #include <libecap/adapter/service.h>
6 #include <libecap/common/names.h>
7 #include <libecap/common/registry.h>
8 #include "base/TextException.h"
9 #include "adaptation/ecap/ServiceRep.h"
10 #include "adaptation/ecap/Host.h"
11 #include "adaptation/ecap/MessageRep.h"
12 #include "HttpRequest.h"
13 #include "HttpReply.h"
14
15 const libecap::Name Adaptation::Ecap::protocolInternal("internal", libecap::Name::NextId());
16 const libecap::Name Adaptation::Ecap::protocolCacheObj("cache_object", libecap::Name::NextId());
17 const libecap::Name Adaptation::Ecap::protocolIcp("ICP", libecap::Name::NextId());
18 #if USE_HTCP
19 const libecap::Name Adaptation::Ecap::protocolHtcp("Htcp", libecap::Name::NextId());
20 #endif
21 const libecap::Name Adaptation::Ecap::metaBypassable("bypassable", libecap::Name::NextId());
22
23 /// the host application (i.e., Squid) wrapper registered with libecap
24 static libecap::shared_ptr<Adaptation::Ecap::Host> TheHost;
25
26 Adaptation::Ecap::Host::Host()
27 {
28 // assign our host-specific IDs to well-known names
29 // this code can run only once
30
31 libecap::headerContentLength.assignHostId(HDR_CONTENT_LENGTH);
32 libecap::headerTransferEncoding.assignHostId(HDR_TRANSFER_ENCODING);
33 libecap::headerReferer.assignHostId(HDR_REFERER);
34 libecap::headerVia.assignHostId(HDR_VIA);
35 // TODO: libecap::headerXClientIp.assignHostId(HDR_X_CLIENT_IP);
36 // TODO: libecap::headerXServerIp.assignHostId(HDR_X_SERVER_IP);
37
38 libecap::protocolHttp.assignHostId(PROTO_HTTP);
39 libecap::protocolHttps.assignHostId(PROTO_HTTPS);
40 libecap::protocolFtp.assignHostId(PROTO_FTP);
41 libecap::protocolGopher.assignHostId(PROTO_GOPHER);
42 libecap::protocolWais.assignHostId(PROTO_WAIS);
43 libecap::protocolUrn.assignHostId(PROTO_URN);
44 libecap::protocolWhois.assignHostId(PROTO_WHOIS);
45 protocolInternal.assignHostId(PROTO_INTERNAL);
46 protocolCacheObj.assignHostId(PROTO_CACHEOBJ);
47 protocolIcp.assignHostId(PROTO_ICP);
48 #if USE_HTCP
49 protocolHtcp.assignHostId(PROTO_HTCP);
50 #endif
51
52 // allows adapter to safely ignore this in adapter::Service::configure()
53 metaBypassable.assignHostId(1);
54 }
55
56 std::string
57 Adaptation::Ecap::Host::uri() const
58 {
59 return "ecap://squid-cache.org/ecap/hosts/squid";
60 }
61
62 void
63 Adaptation::Ecap::Host::describe(std::ostream &os) const
64 {
65 os << PACKAGE_NAME << " v" << PACKAGE_VERSION;
66 }
67
68 void
69 Adaptation::Ecap::Host::noteService(const libecap::weak_ptr<libecap::adapter::Service> &weak)
70 {
71 Must(!weak.expired());
72 RegisterAdapterService(weak.lock());
73 }
74
75 static int
76 SquidLogLevel(libecap::LogVerbosity lv)
77 {
78 if (lv.critical())
79 return DBG_CRITICAL; // is it a good idea to ignore other flags?
80
81 if (lv.large())
82 return DBG_DATA; // is it a good idea to ignore other flags?
83
84 if (lv.application())
85 return DBG_IMPORTANT; // is it a good idea to ignore other flags?
86
87 return 2 + 2*lv.debugging() + 3*lv.operation() + 2*lv.xaction();
88 }
89
90 std::ostream *
91 Adaptation::Ecap::Host::openDebug(libecap::LogVerbosity lv)
92 {
93 const int squidLevel = SquidLogLevel(lv);
94 const int squidSection = 93; // XXX: this should be a global constant
95 // XXX: Debug.h should provide this to us
96 if ((Debug::level = squidLevel) <= Debug::Levels[squidSection])
97 return &Debug::getDebugOut();
98 else
99 return NULL;
100 }
101
102 void
103 Adaptation::Ecap::Host::closeDebug(std::ostream *debug)
104 {
105 if (debug)
106 Debug::finishDebug();
107 }
108
109
110 Adaptation::Ecap::Host::MessagePtr
111 Adaptation::Ecap::Host::newRequest() const
112 {
113 return MessagePtr(new Adaptation::Ecap::MessageRep(new HttpRequest));
114 }
115
116 Adaptation::Ecap::Host::MessagePtr
117 Adaptation::Ecap::Host::newResponse() const
118 {
119 return MessagePtr(new Adaptation::Ecap::MessageRep(new HttpReply));
120 }
121
122 void
123 Adaptation::Ecap::Host::Register()
124 {
125 if (!TheHost) {
126 TheHost.reset(new Adaptation::Ecap::Host);
127 libecap::RegisterHost(TheHost);
128 }
129 }